blob: 7c6477d1840aab488cbec7ffe813696fd63f861f [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.Vf64e80842016-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
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100151 /*
152 * If we came back from the guest via a relocation-on interrupt,
153 * we will be in virtual mode at this point, which makes it a
154 * little easier to get back to the caller.
155 */
156 mfmsr r0
157 andi. r0, r0, MSR_IR /* in real mode? */
158 bne .Lvirt_return
159
Paul Mackerras218309b2013-09-06 13:23:44 +1000160 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
161 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras218309b2013-09-06 13:23:44 +1000162 beq 11f
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +0530163 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
164 beq 15f /* Invoke the H_DOORBELL handler */
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530165 cmpwi cr2, r12, BOOK3S_INTERRUPT_HMI
166 beq cr2, 14f /* HMI check */
Paul Mackerras218309b2013-09-06 13:23:44 +1000167
168 /* RFI into the highmem handler, or branch to interrupt handler */
169 mfmsr r6
170 li r0, MSR_RI
171 andc r6, r6, r0
172 mtmsrd r6, 1 /* Clear RI in MSR */
173 mtsrr0 r8
174 mtsrr1 r7
Paul Mackerras218309b2013-09-06 13:23:44 +1000175 beq cr1, 13f /* machine check */
176 RFI
177
178 /* On POWER7, we have external interrupts set to use HSRR0/1 */
17911: mtspr SPRN_HSRR0, r8
180 mtspr SPRN_HSRR1, r7
181 ba 0x500
182
18313: b machine_check_fwnmi
184
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +053018514: mtspr SPRN_HSRR0, r8
186 mtspr SPRN_HSRR1, r7
187 b hmi_exception_after_realmode
188
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +053018915: mtspr SPRN_HSRR0, r8
190 mtspr SPRN_HSRR1, r7
191 ba 0xe80
192
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100193 /* Virtual-mode return - can't get here for HMI or machine check */
194.Lvirt_return:
195 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
196 beq 16f
197 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
198 beq 17f
199 andi. r0, r7, MSR_EE /* were interrupts hard-enabled? */
200 beq 18f
201 mtmsrd r7, 1 /* if so then re-enable them */
20218: mtlr r8
203 blr
204
20516: mtspr SPRN_HSRR0, r8 /* jump to reloc-on external vector */
206 mtspr SPRN_HSRR1, r7
207 b exc_virt_0x4500_hardware_interrupt
208
20917: mtspr SPRN_HSRR0, r8
210 mtspr SPRN_HSRR1, r7
211 b exc_virt_0x4e80_h_doorbell
212
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100213kvmppc_primary_no_guest:
214 /* We handle this much like a ceded vcpu */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100215 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
216 mfspr r3, SPRN_HDEC
217 mtspr SPRN_DEC, r3
Paul Mackerras6af27c82015-03-28 14:21:10 +1100218 /*
219 * Make sure the primary has finished the MMU switch.
220 * We should never get here on a secondary thread, but
221 * check it for robustness' sake.
222 */
223 ld r5, HSTATE_KVM_VCORE(r13)
22465: lbz r0, VCORE_IN_GUEST(r5)
225 cmpwi r0, 0
226 beq 65b
227 /* Set LPCR. */
228 ld r8,VCORE_LPCR(r5)
229 mtspr SPRN_LPCR,r8
230 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100231 /* set our bit in napping_threads */
232 ld r5, HSTATE_KVM_VCORE(r13)
233 lbz r7, HSTATE_PTID(r13)
234 li r0, 1
235 sld r0, r0, r7
236 addi r6, r5, VCORE_NAPPING_THREADS
2371: lwarx r3, 0, r6
238 or r3, r3, r0
239 stwcx. r3, 0, r6
240 bne 1b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100241 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100242 isync
243 li r12, 0
244 lwz r7, VCORE_ENTRY_EXIT(r5)
245 cmpwi r7, 0x100
246 bge kvm_novcpu_exit /* another thread already exiting */
247 li r3, NAPPING_NOVCPU
248 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100249
Paul Mackerrasccc07772015-03-28 14:21:07 +1100250 li r3, 0 /* Don't wake on privileged (OS) doorbell */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100251 b kvm_do_nap
252
Suresh Warrier37f55d32016-08-19 15:35:46 +1000253/*
254 * kvm_novcpu_wakeup
255 * Entered from kvm_start_guest if kvm_hstate.napping is set
256 * to NAPPING_NOVCPU
257 * r2 = kernel TOC
258 * r13 = paca
259 */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100260kvm_novcpu_wakeup:
261 ld r1, HSTATE_HOST_R1(r13)
262 ld r5, HSTATE_KVM_VCORE(r13)
263 li r0, 0
264 stb r0, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100265
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100266 /* check the wake reason */
267 bl kvmppc_check_wake_reason
Paul Mackerras6af27c82015-03-28 14:21:10 +1100268
Suresh Warrier37f55d32016-08-19 15:35:46 +1000269 /*
270 * Restore volatile registers since we could have called
271 * a C routine in kvmppc_check_wake_reason.
272 * r5 = VCORE
273 */
274 ld r5, HSTATE_KVM_VCORE(r13)
275
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100276 /* see if any other thread is already exiting */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100277 lwz r0, VCORE_ENTRY_EXIT(r5)
278 cmpwi r0, 0x100
279 bge kvm_novcpu_exit
280
281 /* clear our bit in napping_threads */
282 lbz r7, HSTATE_PTID(r13)
283 li r0, 1
284 sld r0, r0, r7
285 addi r6, r5, VCORE_NAPPING_THREADS
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002864: lwarx r7, 0, r6
287 andc r7, r7, r0
288 stwcx. r7, 0, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100289 bne 4b
290
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100291 /* See if the wake reason means we need to exit */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100292 cmpdi r3, 0
293 bge kvm_novcpu_exit
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100294
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100295 /* See if our timeslice has expired (HDEC is negative) */
296 mfspr r0, SPRN_HDEC
297 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
298 cmpwi r0, 0
299 blt kvm_novcpu_exit
300
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100301 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
302 ld r4, HSTATE_KVM_VCPU(r13)
303 cmpdi r4, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100304 beq kvmppc_primary_no_guest
305
306#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
307 addi r3, r4, VCPU_TB_RMENTRY
308 bl kvmhv_start_timing
309#endif
310 b kvmppc_got_guest
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100311
312kvm_novcpu_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100313#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
314 ld r4, HSTATE_KVM_VCPU(r13)
315 cmpdi r4, 0
316 beq 13f
317 addi r3, r4, VCPU_TB_RMEXIT
318 bl kvmhv_accumulate_time
319#endif
Paul Mackerraseddb60f2015-03-28 14:21:11 +110032013: mr r3, r12
321 stw r12, 112-4(r1)
322 bl kvmhv_commence_exit
323 nop
324 lwz r12, 112-4(r1)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100325 b kvmhv_switch_to_host
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100326
Paul Mackerras371fefd2011-06-29 00:23:08 +0000327/*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100328 * We come in here when wakened from nap mode.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000329 * Relocation is off and most register values are lost.
330 * r13 points to the PACA.
331 */
332 .globl kvm_start_guest
333kvm_start_guest:
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530334
335 /* Set runlatch bit the minute you wake up from nap */
Paul Mackerras1f09c3e2015-03-28 14:21:04 +1100336 mfspr r0, SPRN_CTRLF
337 ori r0, r0, 1
338 mtspr SPRN_CTRLT, r0
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530339
Paul Mackerras19ccb762011-07-23 17:42:46 +1000340 ld r2,PACATOC(r13)
341
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000342 li r0,KVM_HWTHREAD_IN_KVM
343 stb r0,HSTATE_HWTHREAD_STATE(r13)
344
345 /* NV GPR values from power7_idle() will no longer be valid */
346 li r0,1
347 stb r0,PACA_NAPSTATELOST(r13)
348
Paul Mackerras4619ac82013-04-17 20:31:41 +0000349 /* were we napping due to cede? */
350 lbz r0,HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100351 cmpwi r0,NAPPING_CEDE
352 beq kvm_end_cede
353 cmpwi r0,NAPPING_NOVCPU
354 beq kvm_novcpu_wakeup
355
356 ld r1,PACAEMERGSP(r13)
357 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras4619ac82013-04-17 20:31:41 +0000358
359 /*
360 * We weren't napping due to cede, so this must be a secondary
361 * thread being woken up to run a guest, or being woken up due
362 * to a stray IPI. (Or due to some machine check or hypervisor
363 * maintenance interrupt while the core is in KVM.)
364 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000365
366 /* Check the wake reason in SRR1 to see why we got here */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100367 bl kvmppc_check_wake_reason
Suresh Warrier37f55d32016-08-19 15:35:46 +1000368 /*
369 * kvmppc_check_wake_reason could invoke a C routine, but we
370 * have no volatile registers to restore when we return.
371 */
372
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100373 cmpdi r3, 0
374 bge kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000375
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000376 /* get vcore pointer, NULL if we have nothing to run */
377 ld r5,HSTATE_KVM_VCORE(r13)
378 cmpdi r5,0
379 /* if we have no vcore to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000380 beq kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000381
Paul Mackerras56548fc2014-12-03 14:48:40 +1100382kvm_secondary_got_guest:
383
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100384 /* Set HSTATE_DSCR(r13) to something sensible */
Anshuman Khandual1db36522015-05-21 12:13:03 +0530385 ld r6, PACA_DSCR_DEFAULT(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100386 std r6, HSTATE_DSCR(r13)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000387
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000388 /* On thread 0 of a subcore, set HDEC to max */
389 lbz r4, HSTATE_PTID(r13)
390 cmpwi r4, 0
391 bne 63f
392 lis r6, 0x7fff
393 ori r6, r6, 0xffff
394 mtspr SPRN_HDEC, r6
395 /* and set per-LPAR registers, if doing dynamic micro-threading */
396 ld r6, HSTATE_SPLIT_MODE(r13)
397 cmpdi r6, 0
398 beq 63f
399 ld r0, KVM_SPLIT_RPR(r6)
400 mtspr SPRN_RPR, r0
401 ld r0, KVM_SPLIT_PMMAR(r6)
402 mtspr SPRN_PMMAR, r0
403 ld r0, KVM_SPLIT_LDBAR(r6)
404 mtspr SPRN_LDBAR, r0
405 isync
40663:
407 /* Order load of vcpu after load of vcore */
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100408 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000409 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100410 bl kvmppc_hv_entry
Paul Mackerras218309b2013-09-06 13:23:44 +1000411
412 /* Back from the guest, go back to nap */
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000413 /* Clear our vcpu and vcore pointers so we don't come back in early */
Paul Mackerras218309b2013-09-06 13:23:44 +1000414 li r0, 0
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000415 std r0, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100416 /*
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000417 * Once we clear HSTATE_KVM_VCORE(r13), the code in
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100418 * kvmppc_run_core() is going to assume that all our vcpu
419 * state is visible in memory. This lwsync makes sure
420 * that that is true.
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100421 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000422 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000423 std r0, HSTATE_KVM_VCORE(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000424
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530425 /*
426 * All secondaries exiting guest will fall through this path.
427 * Before proceeding, just check for HMI interrupt and
428 * invoke opal hmi handler. By now we are sure that the
429 * primary thread on this core/subcore has already made partition
430 * switch/TB resync and we are good to call opal hmi handler.
431 */
432 cmpwi r12, BOOK3S_INTERRUPT_HMI
433 bne kvm_no_guest
434
435 li r3,0 /* NULL argument */
436 bl hmi_exception_realmode
Paul Mackerras56548fc2014-12-03 14:48:40 +1100437/*
438 * At this point we have finished executing in the guest.
439 * We need to wait for hwthread_req to become zero, since
440 * we may not turn on the MMU while hwthread_req is non-zero.
441 * While waiting we also need to check if we get given a vcpu to run.
442 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000443kvm_no_guest:
Paul Mackerras56548fc2014-12-03 14:48:40 +1100444 lbz r3, HSTATE_HWTHREAD_REQ(r13)
445 cmpwi r3, 0
446 bne 53f
447 HMT_MEDIUM
448 li r0, KVM_HWTHREAD_IN_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000449 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerras56548fc2014-12-03 14:48:40 +1100450 /* need to recheck hwthread_req after a barrier, to avoid race */
451 sync
452 lbz r3, HSTATE_HWTHREAD_REQ(r13)
453 cmpwi r3, 0
454 bne 54f
455/*
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530456 * We jump to pnv_wakeup_loss, which will return to the caller
Paul Mackerras56548fc2014-12-03 14:48:40 +1100457 * of power7_nap in the powernv cpu offline loop. The value we
458 * put in r3 becomes the return value for power7_nap.
459 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000460 li r3, LPCR_PECE0
461 mfspr r4, SPRN_LPCR
462 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
463 mtspr SPRN_LPCR, r4
Paul Mackerras56548fc2014-12-03 14:48:40 +1100464 li r3, 0
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530465 b pnv_wakeup_loss
Paul Mackerras56548fc2014-12-03 14:48:40 +1100466
46753: HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000468 ld r5, HSTATE_KVM_VCORE(r13)
469 cmpdi r5, 0
470 bne 60f
471 ld r3, HSTATE_SPLIT_MODE(r13)
472 cmpdi r3, 0
473 beq kvm_no_guest
474 lbz r0, KVM_SPLIT_DO_NAP(r3)
475 cmpwi r0, 0
Paul Mackerras56548fc2014-12-03 14:48:40 +1100476 beq kvm_no_guest
477 HMT_MEDIUM
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000478 b kvm_unsplit_nap
47960: HMT_MEDIUM
Paul Mackerras56548fc2014-12-03 14:48:40 +1100480 b kvm_secondary_got_guest
481
48254: li r0, KVM_HWTHREAD_IN_KVM
483 stb r0, HSTATE_HWTHREAD_STATE(r13)
484 b kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000485
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000486/*
487 * Here the primary thread is trying to return the core to
488 * whole-core mode, so we need to nap.
489 */
490kvm_unsplit_nap:
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530491 /*
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530492 * When secondaries are napping in kvm_unsplit_nap() with
493 * hwthread_req = 1, HMI goes ignored even though subcores are
494 * already exited the guest. Hence HMI keeps waking up secondaries
495 * from nap in a loop and secondaries always go back to nap since
496 * no vcore is assigned to them. This makes impossible for primary
497 * thread to get hold of secondary threads resulting into a soft
498 * lockup in KVM path.
499 *
500 * Let us check if HMI is pending and handle it before we go to nap.
501 */
502 cmpwi r12, BOOK3S_INTERRUPT_HMI
503 bne 55f
504 li r3, 0 /* NULL argument */
505 bl hmi_exception_realmode
50655:
507 /*
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530508 * Ensure that secondary doesn't nap when it has
509 * its vcore pointer set.
510 */
511 sync /* matches smp_mb() before setting split_info.do_nap */
512 ld r0, HSTATE_KVM_VCORE(r13)
513 cmpdi r0, 0
514 bne kvm_no_guest
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000515 /* clear any pending message */
516BEGIN_FTR_SECTION
517 lis r6, (PPC_DBELL_SERVER << (63-36))@h
518 PPC_MSGCLR(6)
519END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
520 /* Set kvm_split_mode.napped[tid] = 1 */
521 ld r3, HSTATE_SPLIT_MODE(r13)
522 li r0, 1
523 lhz r4, PACAPACAINDEX(r13)
524 clrldi r4, r4, 61 /* micro-threading => P8 => 8 threads/core */
525 addi r4, r4, KVM_SPLIT_NAPPED
526 stbx r0, r3, r4
527 /* Check the do_nap flag again after setting napped[] */
528 sync
529 lbz r0, KVM_SPLIT_DO_NAP(r3)
530 cmpwi r0, 0
531 beq 57f
532 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
Paul Mackerrasbf53c882016-11-18 14:34:07 +1100533 mfspr r5, SPRN_LPCR
534 rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
535 b kvm_nap_sequence
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000536
53757: li r0, 0
538 stbx r0, r3, r4
539 b kvm_no_guest
540
Paul Mackerras218309b2013-09-06 13:23:44 +1000541/******************************************************************************
542 * *
543 * Entry code *
544 * *
545 *****************************************************************************/
546
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100547/* Stack frame offsets */
548#define STACK_SLOT_TID (112-16)
549#define STACK_SLOT_PSSCR (112-24)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100550#define STACK_SLOT_PID (112-32)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100551
Paul Mackerrasde56a942011-06-29 00:21:34 +0000552.global kvmppc_hv_entry
553kvmppc_hv_entry:
554
555 /* Required state:
556 *
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100557 * R4 = vcpu pointer (or NULL)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000558 * MSR = ~IR|DR
559 * R13 = PACA
560 * R1 = host R1
Michael Neuling06a29e42014-08-19 14:59:30 +1000561 * R2 = TOC
Paul Mackerrasde56a942011-06-29 00:21:34 +0000562 * all other volatile GPRS = free
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100563 * Does not preserve non-volatile GPRs or CR fields
Paul Mackerrasde56a942011-06-29 00:21:34 +0000564 */
565 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000566 std r0, PPC_LR_STKOFF(r1)
567 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000568
Paul Mackerrasde56a942011-06-29 00:21:34 +0000569 /* Save R1 in the PACA */
570 std r1, HSTATE_HOST_R1(r13)
571
Paul Mackerras44a3add2013-10-04 21:45:04 +1000572 li r6, KVM_GUEST_MODE_HOST_HV
573 stb r6, HSTATE_IN_GUEST(r13)
574
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100575#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
576 /* Store initial timestamp */
577 cmpdi r4, 0
578 beq 1f
579 addi r3, r4, VCPU_TB_RMENTRY
580 bl kvmhv_start_timing
5811:
582#endif
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100583
584 /* Use cr7 as an indication of radix mode */
585 ld r5, HSTATE_KVM_VCORE(r13)
586 ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
587 lbz r0, KVM_RADIX(r9)
588 cmpwi cr7, r0, 0
589
590 /* Clear out SLB if hash */
591 bne cr7, 2f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000592 li r6,0
593 slbmte r6,r6
594 slbia
595 ptesync
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11005962:
Paul Mackerras9e368f22011-06-29 00:40:08 +0000597 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100598 * POWER7/POWER8 host -> guest partition switch code.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000599 * We don't have to lock against concurrent tlbies,
600 * but we do have to coordinate across hardware threads.
601 */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100602 /* Set bit in entry map iff exit map is zero. */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100603 li r7, 1
604 lbz r6, HSTATE_PTID(r13)
605 sld r7, r7, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100606 addi r8, r5, VCORE_ENTRY_EXIT
60721: lwarx r3, 0, r8
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100608 cmpwi r3, 0x100 /* any threads starting to exit? */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000609 bge secondary_too_late /* if so we're too late to the party */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100610 or r3, r3, r7
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100611 stwcx. r3, 0, r8
Paul Mackerras371fefd2011-06-29 00:23:08 +0000612 bne 21b
613
614 /* Primary thread switches to guest partition. */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000615 cmpwi r6,0
Paul Mackerras6af27c82015-03-28 14:21:10 +1100616 bne 10f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000617 lwz r7,KVM_LPID(r9)
Paul Mackerras7a840842016-11-16 22:25:20 +1100618BEGIN_FTR_SECTION
619 ld r6,KVM_SDR1(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000620 li r0,LPID_RSVD /* switch to reserved LPID */
621 mtspr SPRN_LPID,r0
622 ptesync
623 mtspr SPRN_SDR1,r6 /* switch to partition page table */
Paul Mackerras7a840842016-11-16 22:25:20 +1100624END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000625 mtspr SPRN_LPID,r7
626 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000627
628 /* See if we need to flush the TLB */
629 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100630BEGIN_FTR_SECTION
631 /*
632 * On POWER9, individual threads can come in here, but the
633 * TLB is shared between the 4 threads in a core, hence
634 * invalidating on one thread invalidates for all.
635 * Thus we make all 4 threads use the same bit here.
636 */
637 clrrdi r6,r6,2
638END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000639 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
640 srdi r6,r6,6 /* doubleword number */
641 sldi r6,r6,3 /* address offset */
642 add r6,r6,r9
643 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100644 li r8,1
645 sld r8,r8,r7
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000646 ld r7,0(r6)
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100647 and. r7,r7,r8
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000648 beq 22f
Paul Mackerrasca252052014-01-08 21:25:22 +1100649 /* Flush the TLB of any entries for this LPID */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100650 lwz r0,KVM_TLB_SETS(r9)
651 mtctr r0
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000652 li r7,0x800 /* IS field = 0b10 */
653 ptesync
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100654 li r0,0 /* RS for P9 version of tlbiel */
655 bne cr7, 29f
65628: tlbiel r7 /* On P9, rs=0, RIC=0, PRS=0, R=0 */
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000657 addi r7,r7,0x1000
658 bdnz 28b
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100659 b 30f
66029: PPC_TLBIEL(7,0,2,1,1) /* for radix, RIC=2, PRS=1, R=1 */
661 addi r7,r7,0x1000
662 bdnz 29b
66330: ptesync
66423: ldarx r7,0,r6 /* clear the bit after TLB flushed */
665 andc r7,r7,r8
666 stdcx. r7,0,r6
667 bne 23b
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000668
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000669 /* Add timebase offset onto timebase */
67022: ld r8,VCORE_TB_OFFSET(r5)
671 cmpdi r8,0
672 beq 37f
673 mftb r6 /* current host timebase */
674 add r8,r8,r6
675 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
676 mftb r7 /* check if lower 24 bits overflowed */
677 clrldi r6,r6,40
678 clrldi r7,r7,40
679 cmpld r7,r6
680 bge 37f
681 addis r8,r8,0x100 /* if so, increment upper 40 bits */
682 mtspr SPRN_TBU40,r8
683
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000684 /* Load guest PCR value to select appropriate compat mode */
68537: ld r7, VCORE_PCR(r5)
686 cmpdi r7, 0
687 beq 38f
688 mtspr SPRN_PCR, r7
68938:
Michael Neulingb005255e2014-01-08 21:25:21 +1100690
691BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000692 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +1100693 ld r8, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000694 ld r7, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +1100695 mtspr SPRN_DPDES, r8
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000696 mtspr SPRN_VTB, r7
Michael Neulingb005255e2014-01-08 21:25:21 +1100697END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
698
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530699 /* Mark the subcore state as inside guest */
700 bl kvmppc_subcore_enter_guest
701 nop
702 ld r5, HSTATE_KVM_VCORE(r13)
703 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000704 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000705 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000706
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100707 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110070810: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100709 beq kvmppc_primary_no_guest
710kvmppc_got_guest:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000711
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100712 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100713 lwz r5,VCPU_SLB_MAX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000714 cmpwi r5,0
715 beq 9f
716 mtctr r5
717 addi r6,r4,VCPU_SLB
7181: ld r8,VCPU_SLB_E(r6)
719 ld r9,VCPU_SLB_V(r6)
720 slbmte r9,r8
721 addi r6,r6,VCPU_SLB_SIZE
722 bdnz 1b
7239:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100724 /* Increment yield count if they have a VPA */
725 ld r3, VCPU_VPA(r4)
726 cmpdi r3, 0
727 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200728 li r6, LPPACA_YIELDCOUNT
729 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100730 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200731 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100732 li r6, 1
733 stb r6, VCPU_VPA_DIRTY(r4)
73425:
735
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100736 /* Save purr/spurr */
737 mfspr r5,SPRN_PURR
738 mfspr r6,SPRN_SPURR
739 std r5,HSTATE_PURR(r13)
740 std r6,HSTATE_SPURR(r13)
741 ld r7,VCPU_PURR(r4)
742 ld r8,VCPU_SPURR(r4)
743 mtspr SPRN_PURR,r7
744 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100745
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100746 /* Save host values of some registers */
747BEGIN_FTR_SECTION
748 mfspr r5, SPRN_TIDR
749 mfspr r6, SPRN_PSSCR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100750 mfspr r7, SPRN_PID
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100751 std r5, STACK_SLOT_TID(r1)
752 std r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100753 std r7, STACK_SLOT_PID(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100754END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
755
Michael Neulingeee7ff92014-01-08 21:25:19 +1100756BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000757 /* Set partition DABR */
758 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100759 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000760 ld r6,VCPU_DABR(r4)
761 mtspr SPRN_DABRX,r5
762 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000763 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100764END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000765
Michael Neulinge4e38122014-03-25 10:47:02 +1100766#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
767BEGIN_FTR_SECTION
Paul Mackerrasf024ee02016-06-22 14:21:59 +1000768 bl kvmppc_restore_tm
769END_FTR_SECTION_IFSET(CPU_FTR_TM)
Michael Neulinge4e38122014-03-25 10:47:02 +1100770#endif
771
Paul Mackerrasde56a942011-06-29 00:21:34 +0000772 /* Load guest PMU registers */
773 /* R4 is live here (vcpu pointer) */
774 li r3, 1
775 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
776 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
777 isync
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000778BEGIN_FTR_SECTION
779 ld r3, VCPU_MMCR(r4)
780 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
781 cmpwi r5, MMCR0_PMAO
782 beql kvmppc_fix_pmao
783END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000784 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
785 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
786 lwz r6, VCPU_PMC + 8(r4)
787 lwz r7, VCPU_PMC + 12(r4)
788 lwz r8, VCPU_PMC + 16(r4)
789 lwz r9, VCPU_PMC + 20(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000790 mtspr SPRN_PMC1, r3
791 mtspr SPRN_PMC2, r5
792 mtspr SPRN_PMC3, r6
793 mtspr SPRN_PMC4, r7
794 mtspr SPRN_PMC5, r8
795 mtspr SPRN_PMC6, r9
Paul Mackerrasde56a942011-06-29 00:21:34 +0000796 ld r3, VCPU_MMCR(r4)
797 ld r5, VCPU_MMCR + 8(r4)
798 ld r6, VCPU_MMCR + 16(r4)
799 ld r7, VCPU_SIAR(r4)
800 ld r8, VCPU_SDAR(r4)
801 mtspr SPRN_MMCR1, r5
802 mtspr SPRN_MMCRA, r6
803 mtspr SPRN_SIAR, r7
804 mtspr SPRN_SDAR, r8
Michael Neulingb005255e2014-01-08 21:25:21 +1100805BEGIN_FTR_SECTION
806 ld r5, VCPU_MMCR + 24(r4)
807 ld r6, VCPU_SIER(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100808 mtspr SPRN_MMCR2, r5
809 mtspr SPRN_SIER, r6
810BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +1100811 lwz r7, VCPU_PMC + 24(r4)
812 lwz r8, VCPU_PMC + 28(r4)
813 ld r9, VCPU_MMCR + 32(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100814 mtspr SPRN_SPMC1, r7
815 mtspr SPRN_SPMC2, r8
816 mtspr SPRN_MMCRS, r9
Paul Mackerras83677f52016-11-16 22:33:27 +1100817END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +1100818END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000819 mtspr SPRN_MMCR0, r3
820 isync
821
822 /* Load up FP, VMX and VSX registers */
823 bl kvmppc_load_fp
824
825 ld r14, VCPU_GPR(R14)(r4)
826 ld r15, VCPU_GPR(R15)(r4)
827 ld r16, VCPU_GPR(R16)(r4)
828 ld r17, VCPU_GPR(R17)(r4)
829 ld r18, VCPU_GPR(R18)(r4)
830 ld r19, VCPU_GPR(R19)(r4)
831 ld r20, VCPU_GPR(R20)(r4)
832 ld r21, VCPU_GPR(R21)(r4)
833 ld r22, VCPU_GPR(R22)(r4)
834 ld r23, VCPU_GPR(R23)(r4)
835 ld r24, VCPU_GPR(R24)(r4)
836 ld r25, VCPU_GPR(R25)(r4)
837 ld r26, VCPU_GPR(R26)(r4)
838 ld r27, VCPU_GPR(R27)(r4)
839 ld r28, VCPU_GPR(R28)(r4)
840 ld r29, VCPU_GPR(R29)(r4)
841 ld r30, VCPU_GPR(R30)(r4)
842 ld r31, VCPU_GPR(R31)(r4)
843
Paul Mackerrasde56a942011-06-29 00:21:34 +0000844 /* Switch DSCR to guest value */
845 ld r5, VCPU_DSCR(r4)
846 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000847
Michael Neulingb005255e2014-01-08 21:25:21 +1100848BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100849 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100850 b 8f
851END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +1100852 /* Load up POWER8-specific registers */
853 ld r5, VCPU_IAMR(r4)
854 lwz r6, VCPU_PSPB(r4)
855 ld r7, VCPU_FSCR(r4)
856 mtspr SPRN_IAMR, r5
857 mtspr SPRN_PSPB, r6
858 mtspr SPRN_FSCR, r7
859 ld r5, VCPU_DAWR(r4)
860 ld r6, VCPU_DAWRX(r4)
861 ld r7, VCPU_CIABR(r4)
862 ld r8, VCPU_TAR(r4)
863 mtspr SPRN_DAWR, r5
864 mtspr SPRN_DAWRX, r6
865 mtspr SPRN_CIABR, r7
866 mtspr SPRN_TAR, r8
867 ld r5, VCPU_IC(r4)
Michael Neuling7b490412014-01-08 21:25:32 +1100868 ld r8, VCPU_EBBHR(r4)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000869 mtspr SPRN_IC, r5
Michael Neulingb005255e2014-01-08 21:25:21 +1100870 mtspr SPRN_EBBHR, r8
871 ld r5, VCPU_EBBRR(r4)
872 ld r6, VCPU_BESCR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100873 lwz r7, VCPU_GUEST_PID(r4)
874 ld r8, VCPU_WORT(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100875 mtspr SPRN_EBBRR, r5
876 mtspr SPRN_BESCR, r6
Michael Neulingb005255e2014-01-08 21:25:21 +1100877 mtspr SPRN_PID, r7
878 mtspr SPRN_WORT, r8
Paul Mackerras83677f52016-11-16 22:33:27 +1100879BEGIN_FTR_SECTION
Paul Mackerrasf11f6f72017-01-30 21:21:52 +1100880 PPC_INVALIDATE_ERAT
881END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
882BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100883 /* POWER8-only registers */
Paul Mackerras83677f52016-11-16 22:33:27 +1100884 ld r5, VCPU_TCSCR(r4)
885 ld r6, VCPU_ACOP(r4)
886 ld r7, VCPU_CSIGR(r4)
887 ld r8, VCPU_TACR(r4)
888 mtspr SPRN_TCSCR, r5
889 mtspr SPRN_ACOP, r6
890 mtspr SPRN_CSIGR, r7
891 mtspr SPRN_TACR, r8
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100892FTR_SECTION_ELSE
893 /* POWER9-only registers */
894 ld r5, VCPU_TID(r4)
895 ld r6, VCPU_PSSCR(r4)
896 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
897 mtspr SPRN_TIDR, r5
898 mtspr SPRN_PSSCR, r6
899ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11009008:
901
Paul Mackerrasde56a942011-06-29 00:21:34 +0000902 /*
903 * Set the decrementer to the guest decrementer.
904 */
905 ld r8,VCPU_DEC_EXPIRES(r4)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +1100906 /* r8 is a host timebase value here, convert to guest TB */
907 ld r5,HSTATE_KVM_VCORE(r13)
908 ld r6,VCORE_TB_OFFSET(r5)
909 add r8,r8,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000910 mftb r7
911 subf r3,r7,r8
912 mtspr SPRN_DEC,r3
913 stw r3,VCPU_DEC(r4)
914
915 ld r5, VCPU_SPRG0(r4)
916 ld r6, VCPU_SPRG1(r4)
917 ld r7, VCPU_SPRG2(r4)
918 ld r8, VCPU_SPRG3(r4)
919 mtspr SPRN_SPRG0, r5
920 mtspr SPRN_SPRG1, r6
921 mtspr SPRN_SPRG2, r7
922 mtspr SPRN_SPRG3, r8
923
Paul Mackerrasde56a942011-06-29 00:21:34 +0000924 /* Load up DAR and DSISR */
925 ld r5, VCPU_DAR(r4)
926 lwz r6, VCPU_DSISR(r4)
927 mtspr SPRN_DAR, r5
928 mtspr SPRN_DSISR, r6
929
Paul Mackerrasde56a942011-06-29 00:21:34 +0000930 /* Restore AMR and UAMOR, set AMOR to all 1s */
931 ld r5,VCPU_AMR(r4)
932 ld r6,VCPU_UAMOR(r4)
933 li r7,-1
934 mtspr SPRN_AMR,r5
935 mtspr SPRN_UAMOR,r6
936 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000937
938 /* Restore state of CTRL run bit; assume 1 on entry */
939 lwz r5,VCPU_CTRL(r4)
940 andi. r5,r5,1
941 bne 4f
942 mfspr r6,SPRN_CTRLF
943 clrrdi r6,r6,1
944 mtspr SPRN_CTRLT,r6
9454:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100946 /* Secondary threads wait for primary to have done partition switch */
947 ld r5, HSTATE_KVM_VCORE(r13)
948 lbz r6, HSTATE_PTID(r13)
949 cmpwi r6, 0
950 beq 21f
951 lbz r0, VCORE_IN_GUEST(r5)
952 cmpwi r0, 0
953 bne 21f
954 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +100095520: lwz r3, VCORE_ENTRY_EXIT(r5)
956 cmpwi r3, 0x100
957 bge no_switch_exit
958 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100959 cmpwi r0, 0
960 beq 20b
961 HMT_MEDIUM
96221:
963 /* Set LPCR. */
964 ld r8,VCORE_LPCR(r5)
965 mtspr SPRN_LPCR,r8
966 isync
967
968 /* Check if HDEC expires soon */
969 mfspr r3, SPRN_HDEC
970 cmpwi r3, 512 /* 1 microsecond */
971 blt hdec_soon
972
Suresh Warrier37f55d32016-08-19 15:35:46 +1000973deliver_guest_interrupt:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000974 ld r6, VCPU_CTR(r4)
Sam bobroffc63517c2015-05-27 09:56:57 +1000975 ld r7, VCPU_XER(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000976
977 mtctr r6
978 mtxer r7
979
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100980kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000981 ld r10, VCPU_PC(r4)
982 ld r11, VCPU_MSR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000983 ld r6, VCPU_SRR0(r4)
984 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100985 mtspr SPRN_SRR0, r6
986 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000987
Paul Mackerras4619ac82013-04-17 20:31:41 +0000988 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000989 rldicl r11, r11, 63 - MSR_HV_LG, 1
990 rotldi r11, r11, 1 + MSR_HV_LG
991 ori r11, r11, MSR_ME
992
Paul Mackerras19ccb762011-07-23 17:42:46 +1000993 /* Check if we can deliver an external or decrementer interrupt now */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100994 ld r0, VCPU_PENDING_EXC(r4)
995 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
996 cmpdi cr1, r0, 0
997 andi. r8, r11, MSR_EE
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100998 mfspr r8, SPRN_LPCR
999 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
1000 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
1001 mtspr SPRN_LPCR, r8
Paul Mackerras19ccb762011-07-23 17:42:46 +10001002 isync
Paul Mackerras19ccb762011-07-23 17:42:46 +10001003 beq 5f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001004 li r0, BOOK3S_INTERRUPT_EXTERNAL
1005 bne cr1, 12f
1006 mfspr r0, SPRN_DEC
1007 cmpwi r0, 0
1008 li r0, BOOK3S_INTERRUPT_DECREMENTER
1009 bge 5f
1010
101112: mtspr SPRN_SRR0, r10
Paul Mackerras19ccb762011-07-23 17:42:46 +10001012 mr r10,r0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001013 mtspr SPRN_SRR1, r11
Michael Neulinge4e38122014-03-25 10:47:02 +11001014 mr r9, r4
1015 bl kvmppc_msr_interrupt
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +110010165:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001017
Liu Ping Fan27025a62013-11-19 14:12:48 +08001018/*
1019 * Required state:
1020 * R4 = vcpu
1021 * R10: value for HSRR0
1022 * R11: value for HSRR1
1023 * R13 = PACA
1024 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001025fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001026 li r0,0
1027 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001028 mtspr SPRN_HSRR0,r10
1029 mtspr SPRN_HSRR1,r11
1030
1031 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001032 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +00001033 stb r9, HSTATE_IN_GUEST(r13)
1034
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001035#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1036 /* Accumulate timing */
1037 addi r3, r4, VCPU_TB_GUEST
1038 bl kvmhv_accumulate_time
1039#endif
1040
Paul Mackerrasde56a942011-06-29 00:21:34 +00001041 /* Enter guest */
1042
Paul Mackerras0acb9112013-02-04 18:10:51 +00001043BEGIN_FTR_SECTION
1044 ld r5, VCPU_CFAR(r4)
1045 mtspr SPRN_CFAR, r5
1046END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001047BEGIN_FTR_SECTION
1048 ld r0, VCPU_PPR(r4)
1049END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001050
Paul Mackerrasde56a942011-06-29 00:21:34 +00001051 ld r5, VCPU_LR(r4)
1052 lwz r6, VCPU_CR(r4)
1053 mtlr r5
1054 mtcr r6
1055
Michael Neulingc75df6f2012-06-25 13:33:10 +00001056 ld r1, VCPU_GPR(R1)(r4)
1057 ld r2, VCPU_GPR(R2)(r4)
1058 ld r3, VCPU_GPR(R3)(r4)
1059 ld r5, VCPU_GPR(R5)(r4)
1060 ld r6, VCPU_GPR(R6)(r4)
1061 ld r7, VCPU_GPR(R7)(r4)
1062 ld r8, VCPU_GPR(R8)(r4)
1063 ld r9, VCPU_GPR(R9)(r4)
1064 ld r10, VCPU_GPR(R10)(r4)
1065 ld r11, VCPU_GPR(R11)(r4)
1066 ld r12, VCPU_GPR(R12)(r4)
1067 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001068
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001069BEGIN_FTR_SECTION
1070 mtspr SPRN_PPR, r0
1071END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1072 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001073 ld r4, VCPU_GPR(R4)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001074
1075 hrfid
1076 b .
1077
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001078secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001079 li r12, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001080 cmpdi r4, 0
1081 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001082 stw r12, VCPU_TRAP(r4)
1083#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001084 addi r3, r4, VCPU_TB_RMEXIT
1085 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001086#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100108711: b kvmhv_switch_to_host
1088
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001089no_switch_exit:
1090 HMT_MEDIUM
1091 li r12, 0
1092 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001093hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001094 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000109512: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001096 mr r9, r4
1097#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001098 addi r3, r4, VCPU_TB_RMEXIT
1099 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001100#endif
Paul Mackerras6af27c82015-03-28 14:21:10 +11001101 b guest_exit_cont
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001102
Paul Mackerrasde56a942011-06-29 00:21:34 +00001103/******************************************************************************
1104 * *
1105 * Exit code *
1106 * *
1107 *****************************************************************************/
1108
1109/*
1110 * We come here from the first-level interrupt handlers.
1111 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301112 .globl kvmppc_interrupt_hv
1113kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001114 /*
1115 * Register contents:
Nicholas Piggind3918e72016-12-22 04:29:25 +10001116 * R12 = (guest CR << 32) | interrupt vector
Paul Mackerrasde56a942011-06-29 00:21:34 +00001117 * R13 = PACA
Nicholas Piggind3918e72016-12-22 04:29:25 +10001118 * guest R12 saved in shadow VCPU SCRATCH0
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001119 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
Paul Mackerrasde56a942011-06-29 00:21:34 +00001120 * guest R13 saved in SPRN_SCRATCH0
1121 */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001122 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001123 lbz r9, HSTATE_IN_GUEST(r13)
1124 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1125 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301126#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1127 cmpwi r9, KVM_GUEST_MODE_GUEST
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001128 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301129 beq kvmppc_interrupt_pr
1130#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001131 /* We're now back in the host but in guest MMU context */
1132 li r9, KVM_GUEST_MODE_HOST_HV
1133 stb r9, HSTATE_IN_GUEST(r13)
1134
Paul Mackerrasde56a942011-06-29 00:21:34 +00001135 ld r9, HSTATE_KVM_VCPU(r13)
1136
1137 /* Save registers */
1138
Michael Neulingc75df6f2012-06-25 13:33:10 +00001139 std r0, VCPU_GPR(R0)(r9)
1140 std r1, VCPU_GPR(R1)(r9)
1141 std r2, VCPU_GPR(R2)(r9)
1142 std r3, VCPU_GPR(R3)(r9)
1143 std r4, VCPU_GPR(R4)(r9)
1144 std r5, VCPU_GPR(R5)(r9)
1145 std r6, VCPU_GPR(R6)(r9)
1146 std r7, VCPU_GPR(R7)(r9)
1147 std r8, VCPU_GPR(R8)(r9)
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001148 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001149 std r0, VCPU_GPR(R9)(r9)
1150 std r10, VCPU_GPR(R10)(r9)
1151 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001152 ld r3, HSTATE_SCRATCH0(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001153 std r3, VCPU_GPR(R12)(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001154 /* CR is in the high half of r12 */
1155 srdi r4, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001156 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001157BEGIN_FTR_SECTION
1158 ld r3, HSTATE_CFAR(r13)
1159 std r3, VCPU_CFAR(r9)
1160END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001161BEGIN_FTR_SECTION
1162 ld r4, HSTATE_PPR(r13)
1163 std r4, VCPU_PPR(r9)
1164END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001165
1166 /* Restore R1/R2 so we can handle faults */
1167 ld r1, HSTATE_HOST_R1(r13)
1168 ld r2, PACATOC(r13)
1169
1170 mfspr r10, SPRN_SRR0
1171 mfspr r11, SPRN_SRR1
1172 std r10, VCPU_SRR0(r9)
1173 std r11, VCPU_SRR1(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001174 /* trap is in the low half of r12, clear CR from the high half */
1175 clrldi r12, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001176 andi. r0, r12, 2 /* need to read HSRR0/1? */
1177 beq 1f
1178 mfspr r10, SPRN_HSRR0
1179 mfspr r11, SPRN_HSRR1
1180 clrrdi r12, r12, 2
11811: std r10, VCPU_PC(r9)
1182 std r11, VCPU_MSR(r9)
1183
1184 GET_SCRATCH0(r3)
1185 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001186 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001187 std r4, VCPU_LR(r9)
1188
Paul Mackerrasde56a942011-06-29 00:21:34 +00001189 stw r12,VCPU_TRAP(r9)
1190
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001191#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1192 addi r3, r9, VCPU_TB_RMINTR
1193 mr r4, r9
1194 bl kvmhv_accumulate_time
1195 ld r5, VCPU_GPR(R5)(r9)
1196 ld r6, VCPU_GPR(R6)(r9)
1197 ld r7, VCPU_GPR(R7)(r9)
1198 ld r8, VCPU_GPR(R8)(r9)
1199#endif
1200
Paul Mackerras4a157d62014-12-03 13:30:39 +11001201 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001202 if this is an HEI (HV emulation interrupt, e40) */
1203 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001204 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001205 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1206 bne 11f
1207 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100120811: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001209
1210 /* these are volatile across C function calls */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001211#ifdef CONFIG_RELOCATABLE
1212 ld r3, HSTATE_SCRATCH1(r13)
1213 mtctr r3
1214#else
Paul Mackerras697d3892011-12-12 12:36:37 +00001215 mfctr r3
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001216#endif
Paul Mackerras697d3892011-12-12 12:36:37 +00001217 mfxer r4
1218 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001219 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001220
Paul Mackerras697d3892011-12-12 12:36:37 +00001221 /* If this is a page table miss then see if it's theirs or ours */
1222 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1223 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +00001224 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1225 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +00001226
Paul Mackerrasde56a942011-06-29 00:21:34 +00001227 /* See if this is a leftover HDEC interrupt */
1228 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1229 bne 2f
1230 mfspr r3,SPRN_HDEC
1231 cmpwi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001232 mr r4,r9
1233 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000012342:
Paul Mackerras697d3892011-12-12 12:36:37 +00001235 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001236 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1237 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001238
Paul Mackerras66feed62015-03-28 14:21:12 +11001239 /* Hypervisor doorbell - exit only if host IPI flag set */
1240 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1241 bne 3f
1242 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301243 cmpwi r0, 0
Paul Mackerras66feed62015-03-28 14:21:12 +11001244 beq 4f
1245 b guest_exit_cont
12463:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001247 /* External interrupt ? */
1248 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001249 bne+ guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001250
1251 /* External interrupt, first check for host_ipi. If this is
1252 * set, we know the host wants us out so let's do it now
1253 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001254 bl kvmppc_read_intr
Suresh Warrier37f55d32016-08-19 15:35:46 +10001255
1256 /*
1257 * Restore the active volatile registers after returning from
1258 * a C function.
1259 */
1260 ld r9, HSTATE_KVM_VCPU(r13)
1261 li r12, BOOK3S_INTERRUPT_EXTERNAL
1262
1263 /*
1264 * kvmppc_read_intr return codes:
1265 *
1266 * Exit to host (r3 > 0)
1267 * 1 An interrupt is pending that needs to be handled by the host
1268 * Exit guest and return to host by branching to guest_exit_cont
1269 *
Suresh Warrierf7af5202016-08-19 15:35:52 +10001270 * 2 Passthrough that needs completion in the host
1271 * Exit guest and return to host by branching to guest_exit_cont
1272 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1273 * to indicate to the host to complete handling the interrupt
1274 *
Suresh Warrier37f55d32016-08-19 15:35:46 +10001275 * Before returning to guest, we check if any CPU is heading out
1276 * to the host and if so, we head out also. If no CPUs are heading
1277 * check return values <= 0.
1278 *
1279 * Return to guest (r3 <= 0)
1280 * 0 No external interrupt is pending
1281 * -1 A guest wakeup IPI (which has now been cleared)
1282 * In either case, we return to guest to deliver any pending
1283 * guest interrupts.
Suresh Warriere3c13e52016-08-19 15:35:51 +10001284 *
1285 * -2 A PCI passthrough external interrupt was handled
1286 * (interrupt was delivered directly to guest)
1287 * Return to guest to deliver any pending guest interrupts.
Suresh Warrier37f55d32016-08-19 15:35:46 +10001288 */
1289
Suresh Warrierf7af5202016-08-19 15:35:52 +10001290 cmpdi r3, 1
1291 ble 1f
1292
1293 /* Return code = 2 */
1294 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1295 stw r12, VCPU_TRAP(r9)
1296 b guest_exit_cont
1297
12981: /* Return code <= 1 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001299 cmpdi r3, 0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001300 bgt guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001301
Suresh Warrier37f55d32016-08-19 15:35:46 +10001302 /* Return code <= 0 */
Paul Mackerras66feed62015-03-28 14:21:12 +110013034: ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001304 lwz r0, VCORE_ENTRY_EXIT(r5)
1305 cmpwi r0, 0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001306 mr r4, r9
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001307 blt deliver_guest_interrupt
Paul Mackerrasde56a942011-06-29 00:21:34 +00001308
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001309guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001310 /* Save more register state */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001311 mfdar r6
1312 mfdsisr r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001313 std r6, VCPU_DAR(r9)
1314 stw r7, VCPU_DSISR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001315 /* don't overwrite fault_dar/fault_dsisr if HDSI */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001316 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
Paul Mackerras6af27c82015-03-28 14:21:10 +11001317 beq mc_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001318 std r6, VCPU_FAULT_DAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001319 stw r7, VCPU_FAULT_DSISR(r9)
1320
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001321 /* See if it is a machine check */
1322 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1323 beq machine_check_realmode
1324mc_cont:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001325#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1326 addi r3, r9, VCPU_TB_RMEXIT
1327 mr r4, r9
1328 bl kvmhv_accumulate_time
1329#endif
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001330
Gautham R. Shenoy7e022e72015-05-21 13:57:04 +05301331 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001332 /* Increment exit count, poke other threads to exit */
1333 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001334 nop
1335 ld r9, HSTATE_KVM_VCPU(r13)
1336 lwz r12, VCPU_TRAP(r9)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001337
Paul Mackerrasec257162015-06-24 21:18:03 +10001338 /* Stop others sending VCPU interrupts to this physical CPU */
1339 li r0, -1
1340 stw r0, VCPU_CPU(r9)
1341 stw r0, VCPU_THREAD_CPU(r9)
1342
Paul Mackerrasde56a942011-06-29 00:21:34 +00001343 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001344 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001345 stw r6,VCPU_CTRL(r9)
1346 andi. r0,r6,1
1347 bne 4f
1348 ori r6,r6,1
1349 mtspr SPRN_CTRLT,r6
13504:
1351 /* Read the guest SLB and save it away */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001352 ld r5, VCPU_KVM(r9)
1353 lbz r0, KVM_RADIX(r5)
1354 cmpwi r0, 0
1355 li r5, 0
1356 bne 3f /* for radix, save 0 entries */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001357 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1358 mtctr r0
1359 li r6,0
1360 addi r7,r9,VCPU_SLB
Paul Mackerrasde56a942011-06-29 00:21:34 +000013611: slbmfee r8,r6
1362 andis. r0,r8,SLB_ESID_V@h
1363 beq 2f
1364 add r8,r8,r6 /* put index in */
1365 slbmfev r3,r6
1366 std r8,VCPU_SLB_E(r7)
1367 std r3,VCPU_SLB_V(r7)
1368 addi r7,r7,VCPU_SLB_SIZE
1369 addi r5,r5,1
13702: addi r6,r6,1
1371 bdnz 1b
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110013723: stw r5,VCPU_SLB_MAX(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001373
1374 /*
1375 * Save the guest PURR/SPURR
1376 */
1377 mfspr r5,SPRN_PURR
1378 mfspr r6,SPRN_SPURR
1379 ld r7,VCPU_PURR(r9)
1380 ld r8,VCPU_SPURR(r9)
1381 std r5,VCPU_PURR(r9)
1382 std r6,VCPU_SPURR(r9)
1383 subf r5,r7,r5
1384 subf r6,r8,r6
1385
1386 /*
1387 * Restore host PURR/SPURR and add guest times
1388 * so that the time in the guest gets accounted.
1389 */
1390 ld r3,HSTATE_PURR(r13)
1391 ld r4,HSTATE_SPURR(r13)
1392 add r3,r3,r5
1393 add r4,r4,r6
1394 mtspr SPRN_PURR,r3
1395 mtspr SPRN_SPURR,r4
1396
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001397 /* Save DEC */
1398 mfspr r5,SPRN_DEC
1399 mftb r6
1400 extsw r5,r5
1401 add r5,r5,r6
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001402 /* r5 is a guest timebase value here, convert to host TB */
1403 ld r3,HSTATE_KVM_VCORE(r13)
1404 ld r4,VCORE_TB_OFFSET(r3)
1405 subf r5,r4,r5
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001406 std r5,VCPU_DEC_EXPIRES(r9)
1407
Michael Neulingb005255e2014-01-08 21:25:21 +11001408BEGIN_FTR_SECTION
1409 b 8f
1410END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001411 /* Save POWER8-specific registers */
1412 mfspr r5, SPRN_IAMR
1413 mfspr r6, SPRN_PSPB
1414 mfspr r7, SPRN_FSCR
1415 std r5, VCPU_IAMR(r9)
1416 stw r6, VCPU_PSPB(r9)
1417 std r7, VCPU_FSCR(r9)
1418 mfspr r5, SPRN_IC
Michael Neulingb005255e2014-01-08 21:25:21 +11001419 mfspr r7, SPRN_TAR
1420 std r5, VCPU_IC(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001421 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001422 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001423 std r8, VCPU_EBBHR(r9)
1424 mfspr r5, SPRN_EBBRR
1425 mfspr r6, SPRN_BESCR
Michael Neulingb005255e2014-01-08 21:25:21 +11001426 mfspr r7, SPRN_PID
1427 mfspr r8, SPRN_WORT
Paul Mackerras83677f52016-11-16 22:33:27 +11001428 std r5, VCPU_EBBRR(r9)
1429 std r6, VCPU_BESCR(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001430 stw r7, VCPU_GUEST_PID(r9)
1431 std r8, VCPU_WORT(r9)
Paul Mackerras83677f52016-11-16 22:33:27 +11001432BEGIN_FTR_SECTION
1433 mfspr r5, SPRN_TCSCR
1434 mfspr r6, SPRN_ACOP
1435 mfspr r7, SPRN_CSIGR
1436 mfspr r8, SPRN_TACR
1437 std r5, VCPU_TCSCR(r9)
1438 std r6, VCPU_ACOP(r9)
1439 std r7, VCPU_CSIGR(r9)
1440 std r8, VCPU_TACR(r9)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001441FTR_SECTION_ELSE
1442 mfspr r5, SPRN_TIDR
1443 mfspr r6, SPRN_PSSCR
1444 std r5, VCPU_TID(r9)
1445 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1446 rotldi r6, r6, 60
1447 std r6, VCPU_PSSCR(r9)
1448ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001449 /*
1450 * Restore various registers to 0, where non-zero values
1451 * set by the guest could disrupt the host.
1452 */
1453 li r0, 0
1454 mtspr SPRN_IAMR, r0
1455 mtspr SPRN_CIABR, r0
1456 mtspr SPRN_DAWRX, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001457 mtspr SPRN_WORT, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001458BEGIN_FTR_SECTION
1459 mtspr SPRN_TCSCR, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001460 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1461 li r0, 1
1462 sldi r0, r0, 31
1463 mtspr SPRN_MMCRS, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001464END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +110014658:
1466
Paul Mackerrasde56a942011-06-29 00:21:34 +00001467 /* Save and reset AMR and UAMOR before turning on the MMU */
1468 mfspr r5,SPRN_AMR
1469 mfspr r6,SPRN_UAMOR
1470 std r5,VCPU_AMR(r9)
1471 std r6,VCPU_UAMOR(r9)
1472 li r6,0
1473 mtspr SPRN_AMR,r6
1474
Paul Mackerrasde56a942011-06-29 00:21:34 +00001475 /* Switch DSCR back to host value */
1476 mfspr r8, SPRN_DSCR
1477 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001478 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001479 mtspr SPRN_DSCR, r7
1480
1481 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001482 std r14, VCPU_GPR(R14)(r9)
1483 std r15, VCPU_GPR(R15)(r9)
1484 std r16, VCPU_GPR(R16)(r9)
1485 std r17, VCPU_GPR(R17)(r9)
1486 std r18, VCPU_GPR(R18)(r9)
1487 std r19, VCPU_GPR(R19)(r9)
1488 std r20, VCPU_GPR(R20)(r9)
1489 std r21, VCPU_GPR(R21)(r9)
1490 std r22, VCPU_GPR(R22)(r9)
1491 std r23, VCPU_GPR(R23)(r9)
1492 std r24, VCPU_GPR(R24)(r9)
1493 std r25, VCPU_GPR(R25)(r9)
1494 std r26, VCPU_GPR(R26)(r9)
1495 std r27, VCPU_GPR(R27)(r9)
1496 std r28, VCPU_GPR(R28)(r9)
1497 std r29, VCPU_GPR(R29)(r9)
1498 std r30, VCPU_GPR(R30)(r9)
1499 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001500
1501 /* Save SPRGs */
1502 mfspr r3, SPRN_SPRG0
1503 mfspr r4, SPRN_SPRG1
1504 mfspr r5, SPRN_SPRG2
1505 mfspr r6, SPRN_SPRG3
1506 std r3, VCPU_SPRG0(r9)
1507 std r4, VCPU_SPRG1(r9)
1508 std r5, VCPU_SPRG2(r9)
1509 std r6, VCPU_SPRG3(r9)
1510
Paul Mackerras89436332012-03-02 01:38:23 +00001511 /* save FP state */
1512 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001513 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001514
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001515#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1516BEGIN_FTR_SECTION
Paul Mackerrasf024ee02016-06-22 14:21:59 +10001517 bl kvmppc_save_tm
1518END_FTR_SECTION_IFSET(CPU_FTR_TM)
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001519#endif
1520
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001521 /* Increment yield count if they have a VPA */
1522 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1523 cmpdi r8, 0
1524 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001525 li r4, LPPACA_YIELDCOUNT
1526 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001527 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001528 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001529 li r3, 1
1530 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000153125:
1532 /* Save PMU registers if requested */
1533 /* r8 and cr0.eq are live here */
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001534BEGIN_FTR_SECTION
1535 /*
1536 * POWER8 seems to have a hardware bug where setting
1537 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1538 * when some counters are already negative doesn't seem
1539 * to cause a performance monitor alert (and hence interrupt).
1540 * The effect of this is that when saving the PMU state,
1541 * if there is no PMU alert pending when we read MMCR0
1542 * before freezing the counters, but one becomes pending
1543 * before we read the counters, we lose it.
1544 * To work around this, we need a way to freeze the counters
1545 * before reading MMCR0. Normally, freezing the counters
1546 * is done by writing MMCR0 (to set MMCR0[FC]) which
1547 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1548 * we can also freeze the counters using MMCR2, by writing
1549 * 1s to all the counter freeze condition bits (there are
1550 * 9 bits each for 6 counters).
1551 */
1552 li r3, -1 /* set all freeze bits */
1553 clrrdi r3, r3, 10
1554 mfspr r10, SPRN_MMCR2
1555 mtspr SPRN_MMCR2, r3
1556 isync
1557END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001558 li r3, 1
1559 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1560 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1561 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001562 mfspr r6, SPRN_MMCRA
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001563 /* Clear MMCRA in order to disable SDAR updates */
Paul Mackerras89436332012-03-02 01:38:23 +00001564 li r7, 0
1565 mtspr SPRN_MMCRA, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001566 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001567 beq 21f /* if no VPA, save PMU stuff anyway */
1568 lbz r7, LPPACA_PMCINUSE(r8)
1569 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1570 bne 21f
1571 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1572 b 22f
157321: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001574 mfspr r7, SPRN_SIAR
1575 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001576 std r4, VCPU_MMCR(r9)
1577 std r5, VCPU_MMCR + 8(r9)
1578 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001579BEGIN_FTR_SECTION
1580 std r10, VCPU_MMCR + 24(r9)
1581END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras14941782013-09-06 13:11:18 +10001582 std r7, VCPU_SIAR(r9)
1583 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001584 mfspr r3, SPRN_PMC1
1585 mfspr r4, SPRN_PMC2
1586 mfspr r5, SPRN_PMC3
1587 mfspr r6, SPRN_PMC4
1588 mfspr r7, SPRN_PMC5
1589 mfspr r8, SPRN_PMC6
1590 stw r3, VCPU_PMC(r9)
1591 stw r4, VCPU_PMC + 4(r9)
1592 stw r5, VCPU_PMC + 8(r9)
1593 stw r6, VCPU_PMC + 12(r9)
1594 stw r7, VCPU_PMC + 16(r9)
1595 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001596BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +11001597 mfspr r5, SPRN_SIER
Paul Mackerras83677f52016-11-16 22:33:27 +11001598 std r5, VCPU_SIER(r9)
1599BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001600 mfspr r6, SPRN_SPMC1
1601 mfspr r7, SPRN_SPMC2
1602 mfspr r8, SPRN_MMCRS
Michael Neulingb005255e2014-01-08 21:25:21 +11001603 stw r6, VCPU_PMC + 24(r9)
1604 stw r7, VCPU_PMC + 28(r9)
1605 std r8, VCPU_MMCR + 32(r9)
1606 lis r4, 0x8000
1607 mtspr SPRN_MMCRS, r4
Paul Mackerras83677f52016-11-16 22:33:27 +11001608END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001609END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000161022:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001611 /* Clear out SLB */
1612 li r5,0
1613 slbmte r5,r5
1614 slbia
1615 ptesync
1616
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001617 /* Restore host values of some registers */
1618BEGIN_FTR_SECTION
1619 ld r5, STACK_SLOT_TID(r1)
1620 ld r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001621 ld r7, STACK_SLOT_PID(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001622 mtspr SPRN_TIDR, r5
1623 mtspr SPRN_PSSCR, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001624 mtspr SPRN_PID, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001625END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerrasf11f6f72017-01-30 21:21:52 +11001626BEGIN_FTR_SECTION
1627 PPC_INVALIDATE_ERAT
1628END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001629
Paul Mackerrasde56a942011-06-29 00:21:34 +00001630 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001631 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001632 * We don't have to lock against tlbies but we do
1633 * have to coordinate the hardware threads.
1634 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001635kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001636 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001637 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001638 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1639 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001640 cmpwi r3,0
1641 beq 15f
1642 HMT_LOW
164313: lbz r3,VCORE_IN_GUEST(r5)
1644 cmpwi r3,0
1645 bne 13b
1646 HMT_MEDIUM
1647 b 16f
1648
1649 /* Primary thread waits for all the secondaries to exit guest */
165015: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001651 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001652 clrldi r3,r3,56
1653 cmpw r3,r0
1654 bne 15b
1655 isync
1656
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001657 /* Did we actually switch to the guest at all? */
1658 lbz r6, VCORE_IN_GUEST(r5)
1659 cmpwi r6, 0
1660 beq 19f
1661
Paul Mackerrasde56a942011-06-29 00:21:34 +00001662 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001663 lwz r7,KVM_HOST_LPID(r4)
Paul Mackerras7a840842016-11-16 22:25:20 +11001664BEGIN_FTR_SECTION
1665 ld r6,KVM_HOST_SDR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001666 li r8,LPID_RSVD /* switch to reserved LPID */
1667 mtspr SPRN_LPID,r8
1668 ptesync
Paul Mackerras7a840842016-11-16 22:25:20 +11001669 mtspr SPRN_SDR1,r6 /* switch to host page table */
1670END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001671 mtspr SPRN_LPID,r7
1672 isync
1673
Michael Neulingb005255e2014-01-08 21:25:21 +11001674BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001675 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +11001676 mfspr r7, SPRN_DPDES
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001677 mfspr r8, SPRN_VTB
Michael Neulingb005255e2014-01-08 21:25:21 +11001678 std r7, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001679 std r8, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +11001680 /* clear DPDES so we don't get guest doorbells in the host */
1681 li r8, 0
1682 mtspr SPRN_DPDES, r8
1683END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1684
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301685 /* If HMI, call kvmppc_realmode_hmi_handler() */
1686 cmpwi r12, BOOK3S_INTERRUPT_HMI
1687 bne 27f
1688 bl kvmppc_realmode_hmi_handler
1689 nop
1690 li r12, BOOK3S_INTERRUPT_HMI
1691 /*
1692 * At this point kvmppc_realmode_hmi_handler would have resync-ed
1693 * the TB. Hence it is not required to subtract guest timebase
1694 * offset from timebase. So, skip it.
1695 *
1696 * Also, do not call kvmppc_subcore_exit_guest() because it has
1697 * been invoked as part of kvmppc_realmode_hmi_handler().
1698 */
1699 b 30f
1700
170127:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001702 /* Subtract timebase offset from timebase */
1703 ld r8,VCORE_TB_OFFSET(r5)
1704 cmpdi r8,0
1705 beq 17f
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001706 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001707 subf r8,r8,r6
1708 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1709 mftb r7 /* check if lower 24 bits overflowed */
1710 clrldi r6,r6,40
1711 clrldi r7,r7,40
1712 cmpld r7,r6
1713 bge 17f
1714 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1715 mtspr SPRN_TBU40,r8
1716
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530171717: bl kvmppc_subcore_exit_guest
1718 nop
171930: ld r5,HSTATE_KVM_VCORE(r13)
1720 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1721
Paul Mackerrasde56a942011-06-29 00:21:34 +00001722 /* Reset PCR */
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301723 ld r0, VCORE_PCR(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001724 cmpdi r0, 0
1725 beq 18f
1726 li r0, 0
1727 mtspr SPRN_PCR, r0
172818:
1729 /* Signal secondary CPUs to continue */
1730 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000173119: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001732 mtspr SPRN_HDEC,r8
1733
173416: ld r8,KVM_HOST_LPCR(r4)
1735 mtspr SPRN_LPCR,r8
1736 isync
Paul Mackerrasde56a942011-06-29 00:21:34 +00001737
1738 /* load host SLB entries */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001739BEGIN_MMU_FTR_SECTION
1740 b 0f
1741END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001742 ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001743
1744 .rept SLB_NUM_BOLTED
Alexander Graf0865a582014-06-11 10:36:17 +02001745 li r3, SLBSHADOW_SAVEAREA
1746 LDX_BE r5, r8, r3
1747 addi r3, r3, 8
1748 LDX_BE r6, r8, r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00001749 andis. r7,r5,SLB_ESID_V@h
1750 beq 1f
1751 slbmte r6,r5
17521: addi r8,r8,16
1753 .endr
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110017540:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001755#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1756 /* Finish timing, if we have a vcpu */
1757 ld r4, HSTATE_KVM_VCPU(r13)
1758 cmpdi r4, 0
1759 li r3, 0
1760 beq 2f
1761 bl kvmhv_accumulate_time
17622:
1763#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001764 /* Unset guest mode */
1765 li r0, KVM_GUEST_MODE_NONE
1766 stb r0, HSTATE_IN_GUEST(r13)
1767
Paul Mackerras218309b2013-09-06 13:23:44 +10001768 ld r0, 112+PPC_LR_STKOFF(r1)
1769 addi r1, r1, 112
1770 mtlr r0
1771 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001772
Paul Mackerras697d3892011-12-12 12:36:37 +00001773/*
1774 * Check whether an HDSI is an HPTE not found fault or something else.
1775 * If it is an HPTE not found fault that is due to the guest accessing
1776 * a page that they have mapped but which we have paged out, then
1777 * we continue on with the guest exit path. In all other cases,
1778 * reflect the HDSI to the guest as a DSI.
1779 */
1780kvmppc_hdsi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001781 ld r3, VCPU_KVM(r9)
1782 lbz r0, KVM_RADIX(r3)
1783 cmpwi r0, 0
Paul Mackerras697d3892011-12-12 12:36:37 +00001784 mfspr r4, SPRN_HDAR
1785 mfspr r6, SPRN_HDSISR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001786 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
Paul Mackerras4cf302b2011-12-12 12:38:51 +00001787 /* HPTE not found fault or protection fault? */
1788 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00001789 beq 1f /* if not, send it to the guest */
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11001790 andi. r0, r11, MSR_DR /* data relocation enabled? */
1791 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11001792BEGIN_FTR_SECTION
1793 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
1794 b 4f
1795END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras697d3892011-12-12 12:36:37 +00001796 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001797 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001798 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
1799 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000018004: std r4, VCPU_FAULT_DAR(r9)
1801 stw r6, VCPU_FAULT_DSISR(r9)
1802
1803 /* Search the hash table. */
1804 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001805 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001806 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00001807 ld r9, HSTATE_KVM_VCPU(r13)
1808 ld r10, VCPU_PC(r9)
1809 ld r11, VCPU_MSR(r9)
1810 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1811 cmpdi r3, 0 /* retry the instruction */
1812 beq 6f
1813 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001814 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001815 cmpdi r3, -2 /* MMIO emulation; need instr word */
1816 beq 2f
1817
Paul Mackerrascf29b212015-10-27 16:10:20 +11001818 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00001819 ld r4, VCPU_FAULT_DAR(r9)
1820 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110018211: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00001822 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110018237: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00001824 mtspr SPRN_SRR0, r10
1825 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001826 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001827 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001828fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000018296: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001830 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001831 mtctr r7
1832 mtxer r8
1833 mr r4, r9
1834 b fast_guest_return
1835
18363: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1837 ld r5, KVM_VRMA_SLB_V(r5)
1838 b 4b
1839
1840 /* If this is for emulated MMIO, load the instruction word */
18412: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1842
1843 /* Set guest mode to 'jump over instruction' so if lwz faults
1844 * we'll just continue at the next IP. */
1845 li r0, KVM_GUEST_MODE_SKIP
1846 stb r0, HSTATE_IN_GUEST(r13)
1847
1848 /* Do the access with MSR:DR enabled */
1849 mfmsr r3
1850 ori r4, r3, MSR_DR /* Enable paging for data */
1851 mtmsrd r4
1852 lwz r8, 0(r10)
1853 mtmsrd r3
1854
1855 /* Store the result */
1856 stw r8, VCPU_LAST_INST(r9)
1857
1858 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001859 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00001860 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001861 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00001862
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001863.Lradix_hdsi:
1864 std r4, VCPU_FAULT_DAR(r9)
1865 stw r6, VCPU_FAULT_DSISR(r9)
1866.Lradix_hisi:
1867 mfspr r5, SPRN_ASDR
1868 std r5, VCPU_FAULT_GPA(r9)
1869 b guest_exit_cont
1870
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001871/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001872 * Similarly for an HISI, reflect it to the guest as an ISI unless
1873 * it is an HPTE not found fault for a page that we have paged out.
1874 */
1875kvmppc_hisi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001876 ld r3, VCPU_KVM(r9)
1877 lbz r0, KVM_RADIX(r3)
1878 cmpwi r0, 0
1879 bne .Lradix_hisi /* for radix, just save ASDR */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001880 andis. r0, r11, SRR1_ISI_NOPT@h
1881 beq 1f
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11001882 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1883 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11001884BEGIN_FTR_SECTION
1885 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
1886 b 4f
1887END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras342d3db2011-12-12 12:38:05 +00001888 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001889 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001890 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
1891 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000018924:
1893 /* Search the hash table. */
1894 mr r3, r9 /* vcpu pointer */
1895 mr r4, r10
1896 mr r6, r11
1897 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001898 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00001899 ld r9, HSTATE_KVM_VCPU(r13)
1900 ld r10, VCPU_PC(r9)
1901 ld r11, VCPU_MSR(r9)
1902 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1903 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001904 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001905 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001906 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00001907
Paul Mackerrascf29b212015-10-27 16:10:20 +11001908 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001909 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110019101: li r0, BOOK3S_INTERRUPT_INST_STORAGE
19117: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00001912 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001913 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001914 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001915 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001916
19173: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1918 ld r5, KVM_VRMA_SLB_V(r6)
1919 b 4b
1920
1921/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001922 * Try to handle an hcall in real mode.
1923 * Returns to the guest if we handle it, or continues on up to
1924 * the kernel if we can't (i.e. if we don't have a handler for
1925 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001926 *
1927 * r5 - r8 contain hcall args,
1928 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001929 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001930hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00001931 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001932 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08001933 /* sc 1 from userspace - reflect to guest syscall */
1934 bne sc_1_fast_return
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001935 clrrdi r3,r3,2
1936 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001937 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10001938 /* See if this hcall is enabled for in-kernel handling */
1939 ld r4, VCPU_KVM(r9)
1940 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
1941 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
1942 add r4, r4, r0
1943 ld r0, KVM_ENABLED_HCALLS(r4)
1944 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
1945 srd r0, r0, r4
1946 andi. r0, r0, 1
1947 beq guest_exit_cont
1948 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001949 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10001950 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001951 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001952 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10001953 add r12,r3,r4
1954 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001955 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001956 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001957 bctrl
1958 cmpdi r3,H_TOO_HARD
1959 beq hcall_real_fallback
1960 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001961 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001962 ld r10,VCPU_PC(r4)
1963 ld r11,VCPU_MSR(r4)
1964 b fast_guest_return
1965
Liu Ping Fan27025a62013-11-19 14:12:48 +08001966sc_1_fast_return:
1967 mtspr SPRN_SRR0,r10
1968 mtspr SPRN_SRR1,r11
1969 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11001970 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08001971 mr r4,r9
1972 b fast_guest_return
1973
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001974 /* We've attempted a real mode hcall, but it's punted it back
1975 * to userspace. We need to restore some clobbered volatiles
1976 * before resuming the pass-it-to-qemu path */
1977hcall_real_fallback:
1978 li r12,BOOK3S_INTERRUPT_SYSCALL
1979 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001980
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001981 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001982
1983 .globl hcall_real_table
1984hcall_real_table:
1985 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001986 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
1987 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
1988 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10001989 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
1990 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001991 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
1992 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11001993 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001994 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001995 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001996 .long 0 /* 0x2c */
1997 .long 0 /* 0x30 */
1998 .long 0 /* 0x34 */
1999 .long 0 /* 0x38 */
2000 .long 0 /* 0x3c */
2001 .long 0 /* 0x40 */
2002 .long 0 /* 0x44 */
2003 .long 0 /* 0x48 */
2004 .long 0 /* 0x4c */
2005 .long 0 /* 0x50 */
2006 .long 0 /* 0x54 */
2007 .long 0 /* 0x58 */
2008 .long 0 /* 0x5c */
2009 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002010#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002011 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2012 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2013 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002014 .long 0 /* 0x70 - H_IPOLL */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002015 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002016#else
2017 .long 0 /* 0x64 - H_EOI */
2018 .long 0 /* 0x68 - H_CPPR */
2019 .long 0 /* 0x6c - H_IPI */
2020 .long 0 /* 0x70 - H_IPOLL */
2021 .long 0 /* 0x74 - H_XIRR */
2022#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002023 .long 0 /* 0x78 */
2024 .long 0 /* 0x7c */
2025 .long 0 /* 0x80 */
2026 .long 0 /* 0x84 */
2027 .long 0 /* 0x88 */
2028 .long 0 /* 0x8c */
2029 .long 0 /* 0x90 */
2030 .long 0 /* 0x94 */
2031 .long 0 /* 0x98 */
2032 .long 0 /* 0x9c */
2033 .long 0 /* 0xa0 */
2034 .long 0 /* 0xa4 */
2035 .long 0 /* 0xa8 */
2036 .long 0 /* 0xac */
2037 .long 0 /* 0xb0 */
2038 .long 0 /* 0xb4 */
2039 .long 0 /* 0xb8 */
2040 .long 0 /* 0xbc */
2041 .long 0 /* 0xc0 */
2042 .long 0 /* 0xc4 */
2043 .long 0 /* 0xc8 */
2044 .long 0 /* 0xcc */
2045 .long 0 /* 0xd0 */
2046 .long 0 /* 0xd4 */
2047 .long 0 /* 0xd8 */
2048 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002049 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11002050 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002051 .long 0 /* 0xe8 */
2052 .long 0 /* 0xec */
2053 .long 0 /* 0xf0 */
2054 .long 0 /* 0xf4 */
2055 .long 0 /* 0xf8 */
2056 .long 0 /* 0xfc */
2057 .long 0 /* 0x100 */
2058 .long 0 /* 0x104 */
2059 .long 0 /* 0x108 */
2060 .long 0 /* 0x10c */
2061 .long 0 /* 0x110 */
2062 .long 0 /* 0x114 */
2063 .long 0 /* 0x118 */
2064 .long 0 /* 0x11c */
2065 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002066 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11002067 .long 0 /* 0x128 */
2068 .long 0 /* 0x12c */
2069 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002070 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002071 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11002072 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Michael Ellermane928e9c2015-03-20 20:39:41 +11002073 .long 0 /* 0x140 */
2074 .long 0 /* 0x144 */
2075 .long 0 /* 0x148 */
2076 .long 0 /* 0x14c */
2077 .long 0 /* 0x150 */
2078 .long 0 /* 0x154 */
2079 .long 0 /* 0x158 */
2080 .long 0 /* 0x15c */
2081 .long 0 /* 0x160 */
2082 .long 0 /* 0x164 */
2083 .long 0 /* 0x168 */
2084 .long 0 /* 0x16c */
2085 .long 0 /* 0x170 */
2086 .long 0 /* 0x174 */
2087 .long 0 /* 0x178 */
2088 .long 0 /* 0x17c */
2089 .long 0 /* 0x180 */
2090 .long 0 /* 0x184 */
2091 .long 0 /* 0x188 */
2092 .long 0 /* 0x18c */
2093 .long 0 /* 0x190 */
2094 .long 0 /* 0x194 */
2095 .long 0 /* 0x198 */
2096 .long 0 /* 0x19c */
2097 .long 0 /* 0x1a0 */
2098 .long 0 /* 0x1a4 */
2099 .long 0 /* 0x1a8 */
2100 .long 0 /* 0x1ac */
2101 .long 0 /* 0x1b0 */
2102 .long 0 /* 0x1b4 */
2103 .long 0 /* 0x1b8 */
2104 .long 0 /* 0x1bc */
2105 .long 0 /* 0x1c0 */
2106 .long 0 /* 0x1c4 */
2107 .long 0 /* 0x1c8 */
2108 .long 0 /* 0x1cc */
2109 .long 0 /* 0x1d0 */
2110 .long 0 /* 0x1d4 */
2111 .long 0 /* 0x1d8 */
2112 .long 0 /* 0x1dc */
2113 .long 0 /* 0x1e0 */
2114 .long 0 /* 0x1e4 */
2115 .long 0 /* 0x1e8 */
2116 .long 0 /* 0x1ec */
2117 .long 0 /* 0x1f0 */
2118 .long 0 /* 0x1f4 */
2119 .long 0 /* 0x1f8 */
2120 .long 0 /* 0x1fc */
2121 .long 0 /* 0x200 */
2122 .long 0 /* 0x204 */
2123 .long 0 /* 0x208 */
2124 .long 0 /* 0x20c */
2125 .long 0 /* 0x210 */
2126 .long 0 /* 0x214 */
2127 .long 0 /* 0x218 */
2128 .long 0 /* 0x21c */
2129 .long 0 /* 0x220 */
2130 .long 0 /* 0x224 */
2131 .long 0 /* 0x228 */
2132 .long 0 /* 0x22c */
2133 .long 0 /* 0x230 */
2134 .long 0 /* 0x234 */
2135 .long 0 /* 0x238 */
2136 .long 0 /* 0x23c */
2137 .long 0 /* 0x240 */
2138 .long 0 /* 0x244 */
2139 .long 0 /* 0x248 */
2140 .long 0 /* 0x24c */
2141 .long 0 /* 0x250 */
2142 .long 0 /* 0x254 */
2143 .long 0 /* 0x258 */
2144 .long 0 /* 0x25c */
2145 .long 0 /* 0x260 */
2146 .long 0 /* 0x264 */
2147 .long 0 /* 0x268 */
2148 .long 0 /* 0x26c */
2149 .long 0 /* 0x270 */
2150 .long 0 /* 0x274 */
2151 .long 0 /* 0x278 */
2152 .long 0 /* 0x27c */
2153 .long 0 /* 0x280 */
2154 .long 0 /* 0x284 */
2155 .long 0 /* 0x288 */
2156 .long 0 /* 0x28c */
2157 .long 0 /* 0x290 */
2158 .long 0 /* 0x294 */
2159 .long 0 /* 0x298 */
2160 .long 0 /* 0x29c */
2161 .long 0 /* 0x2a0 */
2162 .long 0 /* 0x2a4 */
2163 .long 0 /* 0x2a8 */
2164 .long 0 /* 0x2ac */
2165 .long 0 /* 0x2b0 */
2166 .long 0 /* 0x2b4 */
2167 .long 0 /* 0x2b8 */
2168 .long 0 /* 0x2bc */
2169 .long 0 /* 0x2c0 */
2170 .long 0 /* 0x2c4 */
2171 .long 0 /* 0x2c8 */
2172 .long 0 /* 0x2cc */
2173 .long 0 /* 0x2d0 */
2174 .long 0 /* 0x2d4 */
2175 .long 0 /* 0x2d8 */
2176 .long 0 /* 0x2dc */
2177 .long 0 /* 0x2e0 */
2178 .long 0 /* 0x2e4 */
2179 .long 0 /* 0x2e8 */
2180 .long 0 /* 0x2ec */
2181 .long 0 /* 0x2f0 */
2182 .long 0 /* 0x2f4 */
2183 .long 0 /* 0x2f8 */
2184 .long 0 /* 0x2fc */
2185 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002186 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002187hcall_real_table_end:
2188
Paul Mackerras8563bf52014-01-08 21:25:29 +11002189_GLOBAL(kvmppc_h_set_xdabr)
2190 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2191 beq 6f
2192 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2193 andc. r0, r5, r0
2194 beq 3f
21956: li r3, H_PARAMETER
2196 blr
2197
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002198_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002199 li r5, DABRX_USER | DABRX_KERNEL
22003:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002201BEGIN_FTR_SECTION
2202 b 2f
2203END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002204 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002205 stw r5, VCPU_DABRX(r3)
2206 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002207 /* Work around P7 bug where DABR can get corrupted on mtspr */
22081: mtspr SPRN_DABR,r4
2209 mfspr r5, SPRN_DABR
2210 cmpd r4, r5
2211 bne 1b
2212 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002213 li r3,0
2214 blr
2215
Paul Mackerras8563bf52014-01-08 21:25:29 +11002216 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
22172: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002218 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002219 clrrdi r4, r4, 3
2220 std r4, VCPU_DAWR(r3)
2221 std r5, VCPU_DAWRX(r3)
2222 mtspr SPRN_DAWR, r4
2223 mtspr SPRN_DAWRX, r5
2224 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002225 blr
2226
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002227_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002228 ori r11,r11,MSR_EE
2229 std r11,VCPU_MSR(r3)
2230 li r0,1
2231 stb r0,VCPU_CEDED(r3)
2232 sync /* order setting ceded vs. testing prodded */
2233 lbz r5,VCPU_PRODDED(r3)
2234 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002235 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002236 li r12,0 /* set trap to 0 to say hcall is handled */
2237 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002238 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002239 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002240
2241 /*
2242 * Set our bit in the bitmask of napping threads unless all the
2243 * other threads are already napping, in which case we send this
2244 * up to the host.
2245 */
2246 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002247 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002248 lwz r8,VCORE_ENTRY_EXIT(r5)
2249 clrldi r8,r8,56
2250 li r0,1
2251 sld r0,r0,r6
2252 addi r6,r5,VCORE_NAPPING_THREADS
225331: lwarx r4,0,r6
2254 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002255 cmpw r4,r8
2256 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002257 stwcx. r4,0,r6
2258 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002259 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002260 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002261 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002262 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002263 lwz r7,VCORE_ENTRY_EXIT(r5)
2264 cmpwi r7,0x100
2265 bge 33f /* another thread already exiting */
2266
2267/*
2268 * Although not specifically required by the architecture, POWER7
2269 * preserves the following registers in nap mode, even if an SMT mode
2270 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2271 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2272 */
2273 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002274 std r14, VCPU_GPR(R14)(r3)
2275 std r15, VCPU_GPR(R15)(r3)
2276 std r16, VCPU_GPR(R16)(r3)
2277 std r17, VCPU_GPR(R17)(r3)
2278 std r18, VCPU_GPR(R18)(r3)
2279 std r19, VCPU_GPR(R19)(r3)
2280 std r20, VCPU_GPR(R20)(r3)
2281 std r21, VCPU_GPR(R21)(r3)
2282 std r22, VCPU_GPR(R22)(r3)
2283 std r23, VCPU_GPR(R23)(r3)
2284 std r24, VCPU_GPR(R24)(r3)
2285 std r25, VCPU_GPR(R25)(r3)
2286 std r26, VCPU_GPR(R26)(r3)
2287 std r27, VCPU_GPR(R27)(r3)
2288 std r28, VCPU_GPR(R28)(r3)
2289 std r29, VCPU_GPR(R29)(r3)
2290 std r30, VCPU_GPR(R30)(r3)
2291 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002292
2293 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002294 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002295
Paul Mackerras93d17392016-06-22 15:52:55 +10002296#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2297BEGIN_FTR_SECTION
2298 ld r9, HSTATE_KVM_VCPU(r13)
2299 bl kvmppc_save_tm
2300END_FTR_SECTION_IFSET(CPU_FTR_TM)
2301#endif
2302
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002303 /*
2304 * Set DEC to the smaller of DEC and HDEC, so that we wake
2305 * no later than the end of our timeslice (HDEC interrupts
2306 * don't wake us from nap).
2307 */
2308 mfspr r3, SPRN_DEC
2309 mfspr r4, SPRN_HDEC
2310 mftb r5
2311 cmpw r3, r4
2312 ble 67f
2313 mtspr SPRN_DEC, r4
231467:
2315 /* save expiry time of guest decrementer */
2316 extsw r3, r3
2317 add r3, r3, r5
2318 ld r4, HSTATE_KVM_VCPU(r13)
2319 ld r5, HSTATE_KVM_VCORE(r13)
2320 ld r6, VCORE_TB_OFFSET(r5)
2321 subf r3, r6, r3 /* convert to host TB value */
2322 std r3, VCPU_DEC_EXPIRES(r4)
2323
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002324#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2325 ld r4, HSTATE_KVM_VCPU(r13)
2326 addi r3, r4, VCPU_TB_CEDE
2327 bl kvmhv_accumulate_time
2328#endif
2329
Paul Mackerrasccc07772015-03-28 14:21:07 +11002330 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2331
Paul Mackerras19ccb762011-07-23 17:42:46 +10002332 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002333 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002334 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002335 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002336 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002337 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002338kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002339 mfspr r0, SPRN_CTRLF
2340 clrrdi r0, r0, 1
2341 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302342
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002343 li r0,1
2344 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002345 mfspr r5,SPRN_LPCR
2346 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002347BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002348 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002349 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002350END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002351
2352kvm_nap_sequence: /* desired LPCR value in r5 */
2353BEGIN_FTR_SECTION
2354 /*
2355 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2356 * enable state loss = 1 (allow SMT mode switch)
2357 * requested level = 0 (just stop dispatching)
2358 */
2359 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2360 mtspr SPRN_PSSCR, r3
2361 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2362 li r4, LPCR_PECE_HVEE@higher
2363 sldi r4, r4, 32
2364 or r5, r5, r4
2365END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002366 mtspr SPRN_LPCR,r5
2367 isync
2368 li r0, 0
2369 std r0, HSTATE_SCRATCH0(r13)
2370 ptesync
2371 ld r0, HSTATE_SCRATCH0(r13)
23721: cmpd r0, r0
2373 bne 1b
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002374BEGIN_FTR_SECTION
Paul Mackerras19ccb762011-07-23 17:42:46 +10002375 nap
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002376FTR_SECTION_ELSE
2377 PPC_STOP
2378ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002379 b .
2380
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100238133: mr r4, r3
2382 li r3, 0
2383 li r12, 0
2384 b 34f
2385
Paul Mackerras19ccb762011-07-23 17:42:46 +10002386kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002387 /* get vcpu pointer */
2388 ld r4, HSTATE_KVM_VCPU(r13)
2389
Paul Mackerras19ccb762011-07-23 17:42:46 +10002390 /* Woken by external or decrementer interrupt */
2391 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002392
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002393#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2394 addi r3, r4, VCPU_TB_RMINTR
2395 bl kvmhv_accumulate_time
2396#endif
2397
Paul Mackerras93d17392016-06-22 15:52:55 +10002398#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2399BEGIN_FTR_SECTION
2400 bl kvmppc_restore_tm
2401END_FTR_SECTION_IFSET(CPU_FTR_TM)
2402#endif
2403
Paul Mackerras19ccb762011-07-23 17:42:46 +10002404 /* load up FP state */
2405 bl kvmppc_load_fp
2406
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002407 /* Restore guest decrementer */
2408 ld r3, VCPU_DEC_EXPIRES(r4)
2409 ld r5, HSTATE_KVM_VCORE(r13)
2410 ld r6, VCORE_TB_OFFSET(r5)
2411 add r3, r3, r6 /* convert host TB to guest TB value */
2412 mftb r7
2413 subf r3, r7, r3
2414 mtspr SPRN_DEC, r3
2415
Paul Mackerras19ccb762011-07-23 17:42:46 +10002416 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002417 ld r14, VCPU_GPR(R14)(r4)
2418 ld r15, VCPU_GPR(R15)(r4)
2419 ld r16, VCPU_GPR(R16)(r4)
2420 ld r17, VCPU_GPR(R17)(r4)
2421 ld r18, VCPU_GPR(R18)(r4)
2422 ld r19, VCPU_GPR(R19)(r4)
2423 ld r20, VCPU_GPR(R20)(r4)
2424 ld r21, VCPU_GPR(R21)(r4)
2425 ld r22, VCPU_GPR(R22)(r4)
2426 ld r23, VCPU_GPR(R23)(r4)
2427 ld r24, VCPU_GPR(R24)(r4)
2428 ld r25, VCPU_GPR(R25)(r4)
2429 ld r26, VCPU_GPR(R26)(r4)
2430 ld r27, VCPU_GPR(R27)(r4)
2431 ld r28, VCPU_GPR(R28)(r4)
2432 ld r29, VCPU_GPR(R29)(r4)
2433 ld r30, VCPU_GPR(R30)(r4)
2434 ld r31, VCPU_GPR(R31)(r4)
Suresh Warrier37f55d32016-08-19 15:35:46 +10002435
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002436 /* Check the wake reason in SRR1 to see why we got here */
2437 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002438
Suresh Warrier37f55d32016-08-19 15:35:46 +10002439 /*
2440 * Restore volatile registers since we could have called a
2441 * C routine in kvmppc_check_wake_reason
2442 * r4 = VCPU
2443 * r3 tells us whether we need to return to host or not
2444 * WARNING: it gets checked further down:
2445 * should not modify r3 until this check is done.
2446 */
2447 ld r4, HSTATE_KVM_VCPU(r13)
2448
Paul Mackerras19ccb762011-07-23 17:42:46 +10002449 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100245034: ld r5,HSTATE_KVM_VCORE(r13)
2451 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002452 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002453 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002454 addi r6,r5,VCORE_NAPPING_THREADS
245532: lwarx r7,0,r6
2456 andc r7,r7,r0
2457 stwcx. r7,0,r6
2458 bne 32b
2459 li r0,0
2460 stb r0,HSTATE_NAPPING(r13)
2461
Suresh Warrier37f55d32016-08-19 15:35:46 +10002462 /* See if the wake reason saved in r3 means we need to exit */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002463 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002464 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002465 cmpdi r3, 0
2466 bgt guest_exit_cont
Paul Mackerras4619ac82013-04-17 20:31:41 +00002467
Paul Mackerras19ccb762011-07-23 17:42:46 +10002468 /* see if any other thread is already exiting */
2469 lwz r0,VCORE_ENTRY_EXIT(r5)
2470 cmpwi r0,0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002471 bge guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002472
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002473 b kvmppc_cede_reentry /* if not go back to guest */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002474
2475 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002476kvm_cede_prodded:
2477 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002478 stb r0,VCPU_PRODDED(r3)
2479 sync /* order testing prodded vs. clearing ceded */
2480 stb r0,VCPU_CEDED(r3)
2481 li r3,H_SUCCESS
2482 blr
2483
2484 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002485kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002486 ld r9, HSTATE_KVM_VCPU(r13)
2487 b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002488
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002489 /* Try to handle a machine check in real mode */
2490machine_check_realmode:
2491 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002492 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002493 nop
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002494 ld r9, HSTATE_KVM_VCPU(r13)
2495 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302496 /*
2497 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2498 * machine check interrupt (set HSRR0 to 0x200). And for handled
2499 * errors (no-fatal), just go back to guest execution with current
2500 * HSRR0 instead of exiting guest. This new approach will inject
2501 * machine check to guest for fatal error causing guest to crash.
2502 *
2503 * The old code used to return to host for unhandled errors which
2504 * was causing guest to hang with soft lockups inside guest and
2505 * makes it difficult to recover guest instance.
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302506 *
2507 * if we receive machine check with MSR(RI=0) then deliver it to
2508 * guest as machine check causing guest to crash.
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302509 */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302510 ld r11, VCPU_MSR(r9)
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11002511 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2512 bne mc_cont /* if so, exit to host */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302513 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2514 beq 1f /* Deliver a machine check to guest */
2515 ld r10, VCPU_PC(r9)
2516 cmpdi r3, 0 /* Did we handle MCE ? */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302517 bne 2f /* Continue guest execution. */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002518 /* If not, deliver a machine check. SRR0/1 are already set */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +053025191: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
Michael Neulinge4e38122014-03-25 10:47:02 +11002520 bl kvmppc_msr_interrupt
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +053025212: b fast_interrupt_c_return
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002522
Paul Mackerrasde56a942011-06-29 00:21:34 +00002523/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002524 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002525 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002526 * 0 if nothing needs to be done
2527 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002528 * -1 if there was a guest wakeup (IPI or msgsnd)
Suresh Warriere3c13e52016-08-19 15:35:51 +10002529 * -2 if we handled a PCI passthrough interrupt (returned by
2530 * kvmppc_read_intr only)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002531 *
2532 * Also sets r12 to the interrupt vector for any interrupt that needs
2533 * to be handled now by the host (0x500 for external interrupt), or zero.
Suresh Warrier37f55d32016-08-19 15:35:46 +10002534 * Modifies all volatile registers (since it may call a C function).
2535 * This routine calls kvmppc_read_intr, a C function, if an external
2536 * interrupt is pending.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002537 */
2538kvmppc_check_wake_reason:
2539 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002540BEGIN_FTR_SECTION
2541 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2542FTR_SECTION_ELSE
2543 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2544ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2545 cmpwi r6, 8 /* was it an external interrupt? */
Suresh Warrier37f55d32016-08-19 15:35:46 +10002546 beq 7f /* if so, see what it was */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002547 li r3, 0
2548 li r12, 0
2549 cmpwi r6, 6 /* was it the decrementer? */
2550 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002551BEGIN_FTR_SECTION
2552 cmpwi r6, 5 /* privileged doorbell? */
2553 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002554 cmpwi r6, 3 /* hypervisor doorbell? */
2555 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002556END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302557 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2558 beq 4f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002559 li r3, 1 /* anything else, return 1 */
25600: blr
2561
Paul Mackerras5d00f662014-01-08 21:25:28 +11002562 /* hypervisor doorbell */
25633: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302564
2565 /*
2566 * Clear the doorbell as we will invoke the handler
2567 * explicitly in the guest exit path.
2568 */
2569 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2570 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11002571 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11002572 li r3, 1
Paul Mackerras66feed62015-03-28 14:21:12 +11002573 lbz r0, HSTATE_HOST_IPI(r13)
2574 cmpwi r0, 0
2575 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302576 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002577 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11002578 blr
2579
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302580 /* Woken up due to Hypervisor maintenance interrupt */
25814: li r12, BOOK3S_INTERRUPT_HMI
2582 li r3, 1
2583 blr
2584
Suresh Warrier37f55d32016-08-19 15:35:46 +10002585 /* external interrupt - create a stack frame so we can call C */
25867: mflr r0
2587 std r0, PPC_LR_STKOFF(r1)
2588 stdu r1, -PPC_MIN_STKFRM(r1)
2589 bl kvmppc_read_intr
2590 nop
2591 li r12, BOOK3S_INTERRUPT_EXTERNAL
Suresh Warrierf7af5202016-08-19 15:35:52 +10002592 cmpdi r3, 1
2593 ble 1f
2594
2595 /*
2596 * Return code of 2 means PCI passthrough interrupt, but
2597 * we need to return back to host to complete handling the
2598 * interrupt. Trap reason is expected in r12 by guest
2599 * exit code.
2600 */
2601 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
26021:
Suresh Warrier37f55d32016-08-19 15:35:46 +10002603 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2604 addi r1, r1, PPC_MIN_STKFRM
2605 mtlr r0
2606 blr
Paul Mackerrasde56a942011-06-29 00:21:34 +00002607
2608/*
2609 * Save away FP, VMX and VSX registers.
2610 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002611 * N.B. r30 and r31 are volatile across this function,
2612 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002613 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002614kvmppc_save_fp:
2615 mflr r30
2616 mr r31,r3
Paul Mackerras89436332012-03-02 01:38:23 +00002617 mfmsr r5
2618 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00002619#ifdef CONFIG_ALTIVEC
2620BEGIN_FTR_SECTION
2621 oris r8,r8,MSR_VEC@h
2622END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2623#endif
2624#ifdef CONFIG_VSX
2625BEGIN_FTR_SECTION
2626 oris r8,r8,MSR_VSX@h
2627END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2628#endif
2629 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002630 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002631 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002632#ifdef CONFIG_ALTIVEC
2633BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002634 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002635 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002636END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2637#endif
2638 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11002639 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11002640 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00002641 blr
2642
2643/*
2644 * Load up FP, VMX and VSX registers
2645 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002646 * N.B. r30 and r31 are volatile across this function,
2647 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002648 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002649kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11002650 mflr r30
2651 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00002652 mfmsr r9
2653 ori r8,r9,MSR_FP
2654#ifdef CONFIG_ALTIVEC
2655BEGIN_FTR_SECTION
2656 oris r8,r8,MSR_VEC@h
2657END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2658#endif
2659#ifdef CONFIG_VSX
2660BEGIN_FTR_SECTION
2661 oris r8,r8,MSR_VSX@h
2662END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2663#endif
2664 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002665 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002666 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002667#ifdef CONFIG_ALTIVEC
2668BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002669 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002670 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002671END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2672#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11002673 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002674 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11002675 mtlr r30
2676 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00002677 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10002678
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002679#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2680/*
2681 * Save transactional state and TM-related registers.
2682 * Called with r9 pointing to the vcpu struct.
2683 * This can modify all checkpointed registers, but
2684 * restores r1, r2 and r9 (vcpu pointer) before exit.
2685 */
2686kvmppc_save_tm:
2687 mflr r0
2688 std r0, PPC_LR_STKOFF(r1)
2689
2690 /* Turn on TM. */
2691 mfmsr r8
2692 li r0, 1
2693 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
2694 mtmsrd r8
2695
2696 ld r5, VCPU_MSR(r9)
2697 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2698 beq 1f /* TM not active in guest. */
2699
2700 std r1, HSTATE_HOST_R1(r13)
2701 li r3, TM_CAUSE_KVM_RESCHED
2702
2703 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2704 li r5, 0
2705 mtmsrd r5, 1
2706
2707 /* All GPRs are volatile at this point. */
2708 TRECLAIM(R3)
2709
2710 /* Temporarily store r13 and r9 so we have some regs to play with */
2711 SET_SCRATCH0(r13)
2712 GET_PACA(r13)
2713 std r9, PACATMSCRATCH(r13)
2714 ld r9, HSTATE_KVM_VCPU(r13)
2715
2716 /* Get a few more GPRs free. */
2717 std r29, VCPU_GPRS_TM(29)(r9)
2718 std r30, VCPU_GPRS_TM(30)(r9)
2719 std r31, VCPU_GPRS_TM(31)(r9)
2720
2721 /* Save away PPR and DSCR soon so don't run with user values. */
2722 mfspr r31, SPRN_PPR
2723 HMT_MEDIUM
2724 mfspr r30, SPRN_DSCR
2725 ld r29, HSTATE_DSCR(r13)
2726 mtspr SPRN_DSCR, r29
2727
2728 /* Save all but r9, r13 & r29-r31 */
2729 reg = 0
2730 .rept 29
2731 .if (reg != 9) && (reg != 13)
2732 std reg, VCPU_GPRS_TM(reg)(r9)
2733 .endif
2734 reg = reg + 1
2735 .endr
2736 /* ... now save r13 */
2737 GET_SCRATCH0(r4)
2738 std r4, VCPU_GPRS_TM(13)(r9)
2739 /* ... and save r9 */
2740 ld r4, PACATMSCRATCH(r13)
2741 std r4, VCPU_GPRS_TM(9)(r9)
2742
2743 /* Reload stack pointer and TOC. */
2744 ld r1, HSTATE_HOST_R1(r13)
2745 ld r2, PACATOC(r13)
2746
2747 /* Set MSR RI now we have r1 and r13 back. */
2748 li r5, MSR_RI
2749 mtmsrd r5, 1
2750
2751 /* Save away checkpinted SPRs. */
2752 std r31, VCPU_PPR_TM(r9)
2753 std r30, VCPU_DSCR_TM(r9)
2754 mflr r5
2755 mfcr r6
2756 mfctr r7
2757 mfspr r8, SPRN_AMR
2758 mfspr r10, SPRN_TAR
Paul Mackerras0d808df2016-11-07 15:09:58 +11002759 mfxer r11
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002760 std r5, VCPU_LR_TM(r9)
2761 stw r6, VCPU_CR_TM(r9)
2762 std r7, VCPU_CTR_TM(r9)
2763 std r8, VCPU_AMR_TM(r9)
2764 std r10, VCPU_TAR_TM(r9)
Paul Mackerras0d808df2016-11-07 15:09:58 +11002765 std r11, VCPU_XER_TM(r9)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002766
2767 /* Restore r12 as trap number. */
2768 lwz r12, VCPU_TRAP(r9)
2769
2770 /* Save FP/VSX. */
2771 addi r3, r9, VCPU_FPRS_TM
2772 bl store_fp_state
2773 addi r3, r9, VCPU_VRS_TM
2774 bl store_vr_state
2775 mfspr r6, SPRN_VRSAVE
2776 stw r6, VCPU_VRSAVE_TM(r9)
27771:
2778 /*
2779 * We need to save these SPRs after the treclaim so that the software
2780 * error code is recorded correctly in the TEXASR. Also the user may
2781 * change these outside of a transaction, so they must always be
2782 * context switched.
2783 */
2784 mfspr r5, SPRN_TFHAR
2785 mfspr r6, SPRN_TFIAR
2786 mfspr r7, SPRN_TEXASR
2787 std r5, VCPU_TFHAR(r9)
2788 std r6, VCPU_TFIAR(r9)
2789 std r7, VCPU_TEXASR(r9)
2790
2791 ld r0, PPC_LR_STKOFF(r1)
2792 mtlr r0
2793 blr
2794
2795/*
2796 * Restore transactional state and TM-related registers.
2797 * Called with r4 pointing to the vcpu struct.
2798 * This potentially modifies all checkpointed registers.
2799 * It restores r1, r2, r4 from the PACA.
2800 */
2801kvmppc_restore_tm:
2802 mflr r0
2803 std r0, PPC_LR_STKOFF(r1)
2804
2805 /* Turn on TM/FP/VSX/VMX so we can restore them. */
2806 mfmsr r5
2807 li r6, MSR_TM >> 32
2808 sldi r6, r6, 32
2809 or r5, r5, r6
2810 ori r5, r5, MSR_FP
2811 oris r5, r5, (MSR_VEC | MSR_VSX)@h
2812 mtmsrd r5
2813
2814 /*
2815 * The user may change these outside of a transaction, so they must
2816 * always be context switched.
2817 */
2818 ld r5, VCPU_TFHAR(r4)
2819 ld r6, VCPU_TFIAR(r4)
2820 ld r7, VCPU_TEXASR(r4)
2821 mtspr SPRN_TFHAR, r5
2822 mtspr SPRN_TFIAR, r6
2823 mtspr SPRN_TEXASR, r7
2824
2825 ld r5, VCPU_MSR(r4)
2826 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2827 beqlr /* TM not active in guest */
2828 std r1, HSTATE_HOST_R1(r13)
2829
2830 /* Make sure the failure summary is set, otherwise we'll program check
2831 * when we trechkpt. It's possible that this might have been not set
2832 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
2833 * host.
2834 */
2835 oris r7, r7, (TEXASR_FS)@h
2836 mtspr SPRN_TEXASR, r7
2837
2838 /*
2839 * We need to load up the checkpointed state for the guest.
2840 * We need to do this early as it will blow away any GPRs, VSRs and
2841 * some SPRs.
2842 */
2843
2844 mr r31, r4
2845 addi r3, r31, VCPU_FPRS_TM
2846 bl load_fp_state
2847 addi r3, r31, VCPU_VRS_TM
2848 bl load_vr_state
2849 mr r4, r31
2850 lwz r7, VCPU_VRSAVE_TM(r4)
2851 mtspr SPRN_VRSAVE, r7
2852
2853 ld r5, VCPU_LR_TM(r4)
2854 lwz r6, VCPU_CR_TM(r4)
2855 ld r7, VCPU_CTR_TM(r4)
2856 ld r8, VCPU_AMR_TM(r4)
2857 ld r9, VCPU_TAR_TM(r4)
Paul Mackerras0d808df2016-11-07 15:09:58 +11002858 ld r10, VCPU_XER_TM(r4)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002859 mtlr r5
2860 mtcr r6
2861 mtctr r7
2862 mtspr SPRN_AMR, r8
2863 mtspr SPRN_TAR, r9
Paul Mackerras0d808df2016-11-07 15:09:58 +11002864 mtxer r10
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002865
2866 /*
2867 * Load up PPR and DSCR values but don't put them in the actual SPRs
2868 * till the last moment to avoid running with userspace PPR and DSCR for
2869 * too long.
2870 */
2871 ld r29, VCPU_DSCR_TM(r4)
2872 ld r30, VCPU_PPR_TM(r4)
2873
2874 std r2, PACATMSCRATCH(r13) /* Save TOC */
2875
2876 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2877 li r5, 0
2878 mtmsrd r5, 1
2879
2880 /* Load GPRs r0-r28 */
2881 reg = 0
2882 .rept 29
2883 ld reg, VCPU_GPRS_TM(reg)(r31)
2884 reg = reg + 1
2885 .endr
2886
2887 mtspr SPRN_DSCR, r29
2888 mtspr SPRN_PPR, r30
2889
2890 /* Load final GPRs */
2891 ld 29, VCPU_GPRS_TM(29)(r31)
2892 ld 30, VCPU_GPRS_TM(30)(r31)
2893 ld 31, VCPU_GPRS_TM(31)(r31)
2894
2895 /* TM checkpointed state is now setup. All GPRs are now volatile. */
2896 TRECHKPT
2897
2898 /* Now let's get back the state we need. */
2899 HMT_MEDIUM
2900 GET_PACA(r13)
2901 ld r29, HSTATE_DSCR(r13)
2902 mtspr SPRN_DSCR, r29
2903 ld r4, HSTATE_KVM_VCPU(r13)
2904 ld r1, HSTATE_HOST_R1(r13)
2905 ld r2, PACATMSCRATCH(r13)
2906
2907 /* Set the MSR RI since we have our registers back. */
2908 li r5, MSR_RI
2909 mtmsrd r5, 1
2910
2911 ld r0, PPC_LR_STKOFF(r1)
2912 mtlr r0
2913 blr
2914#endif
2915
Paul Mackerras44a3add2013-10-04 21:45:04 +10002916/*
2917 * We come here if we get any exception or interrupt while we are
2918 * executing host real mode code while in guest MMU context.
2919 * For now just spin, but we should do something better.
2920 */
2921kvmppc_bad_host_intr:
2922 b .
Michael Neulinge4e38122014-03-25 10:47:02 +11002923
2924/*
2925 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
2926 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
2927 * r11 has the guest MSR value (in/out)
2928 * r9 has a vcpu pointer (in)
2929 * r0 is used as a scratch register
2930 */
2931kvmppc_msr_interrupt:
2932 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
2933 cmpwi r0, 2 /* Check if we are in transactional state.. */
2934 ld r11, VCPU_INTR_MSR(r9)
2935 bne 1f
2936 /* ... if transactional, change to suspended */
2937 li r0, 1
29381: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
2939 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10002940
2941/*
2942 * This works around a hardware bug on POWER8E processors, where
2943 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
2944 * performance monitor interrupt. Instead, when we need to have
2945 * an interrupt pending, we have to arrange for a counter to overflow.
2946 */
2947kvmppc_fix_pmao:
2948 li r3, 0
2949 mtspr SPRN_MMCR2, r3
2950 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
2951 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
2952 mtspr SPRN_MMCR0, r3
2953 lis r3, 0x7fff
2954 ori r3, r3, 0xffff
2955 mtspr SPRN_PMC6, r3
2956 isync
2957 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002958
2959#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2960/*
2961 * Start timing an activity
2962 * r3 = pointer to time accumulation struct, r4 = vcpu
2963 */
2964kvmhv_start_timing:
2965 ld r5, HSTATE_KVM_VCORE(r13)
2966 lbz r6, VCORE_IN_GUEST(r5)
2967 cmpwi r6, 0
2968 beq 5f /* if in guest, need to */
2969 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
29705: mftb r5
2971 subf r5, r6, r5
2972 std r3, VCPU_CUR_ACTIVITY(r4)
2973 std r5, VCPU_ACTIVITY_START(r4)
2974 blr
2975
2976/*
2977 * Accumulate time to one activity and start another.
2978 * r3 = pointer to new time accumulation struct, r4 = vcpu
2979 */
2980kvmhv_accumulate_time:
2981 ld r5, HSTATE_KVM_VCORE(r13)
2982 lbz r8, VCORE_IN_GUEST(r5)
2983 cmpwi r8, 0
2984 beq 4f /* if in guest, need to */
2985 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
29864: ld r5, VCPU_CUR_ACTIVITY(r4)
2987 ld r6, VCPU_ACTIVITY_START(r4)
2988 std r3, VCPU_CUR_ACTIVITY(r4)
2989 mftb r7
2990 subf r7, r8, r7
2991 std r7, VCPU_ACTIVITY_START(r4)
2992 cmpdi r5, 0
2993 beqlr
2994 subf r3, r6, r7
2995 ld r8, TAS_SEQCOUNT(r5)
2996 cmpdi r8, 0
2997 addi r8, r8, 1
2998 std r8, TAS_SEQCOUNT(r5)
2999 lwsync
3000 ld r7, TAS_TOTAL(r5)
3001 add r7, r7, r3
3002 std r7, TAS_TOTAL(r5)
3003 ld r6, TAS_MIN(r5)
3004 ld r7, TAS_MAX(r5)
3005 beq 3f
3006 cmpd r3, r6
3007 bge 1f
30083: std r3, TAS_MIN(r5)
30091: cmpd r3, r7
3010 ble 2f
3011 std r3, TAS_MAX(r5)
30122: lwsync
3013 addi r8, r8, 1
3014 std r8, TAS_SEQCOUNT(r5)
3015 blr
3016#endif