blob: 46c1c1fe55c887fd0416c95514fca3af74501415 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
12 *
13 * Derived from book3s_rmhandlers.S and other files, which are:
14 *
15 * Copyright SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#include <asm/ppc_asm.h>
21#include <asm/kvm_asm.h>
22#include <asm/reg.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100023#include <asm/mmu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000024#include <asm/page.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100025#include <asm/ptrace.h>
26#include <asm/hvcall.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000027#include <asm/asm-offsets.h>
28#include <asm/exception-64s.h>
Paul Mackerrasf0888f72012-02-03 00:54:17 +000029#include <asm/kvm_book3s_asm.h>
Aneesh Kumar K.Vf64e8082016-03-01 12:59:20 +053030#include <asm/book3s/64/mmu-hash.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110031#include <asm/tm.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053032#include <asm/opal.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110033
34#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
Paul Mackerrasde56a942011-06-29 00:21:34 +000035
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110036/* Values in HSTATE_NAPPING(r13) */
37#define NAPPING_CEDE 1
38#define NAPPING_NOVCPU 2
39
Paul Mackerrasde56a942011-06-29 00:21:34 +000040/*
Paul Mackerras19ccb762011-07-23 17:42:46 +100041 * Call kvmppc_hv_entry in real mode.
Paul Mackerrasde56a942011-06-29 00:21:34 +000042 * Must be called with interrupts hard-disabled.
43 *
44 * Input Registers:
45 *
46 * LR = return address to continue at after eventually re-enabling MMU
47 */
Anton Blanchard6ed179b2014-06-12 18:16:53 +100048_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
Paul Mackerras218309b2013-09-06 13:23:44 +100049 mflr r0
50 std r0, PPC_LR_STKOFF(r1)
51 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +000052 mfmsr r10
Paul Mackerras218309b2013-09-06 13:23:44 +100053 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
Paul Mackerrasde56a942011-06-29 00:21:34 +000054 li r0,MSR_RI
55 andc r0,r10,r0
56 li r6,MSR_IR | MSR_DR
57 andc r6,r10,r6
58 mtmsrd r0,1 /* clear RI in MSR */
59 mtsrr0 r5
60 mtsrr1 r6
61 RFI
62
Paul Mackerras218309b2013-09-06 13:23:44 +100063kvmppc_call_hv_entry:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110064 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100065 bl kvmppc_hv_entry
66
67 /* Back from guest - restore host state and return to caller */
68
Michael Neulingeee7ff92014-01-08 21:25:19 +110069BEGIN_FTR_SECTION
Paul Mackerras218309b2013-09-06 13:23:44 +100070 /* Restore host DABR and DABRX */
71 ld r5,HSTATE_DABR(r13)
72 li r6,7
73 mtspr SPRN_DABR,r5
74 mtspr SPRN_DABRX,r6
Michael Neulingeee7ff92014-01-08 21:25:19 +110075END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +100076
77 /* Restore SPRG3 */
Scott Wood9d378df2014-03-10 17:29:38 -050078 ld r3,PACA_SPRG_VDSO(r13)
79 mtspr SPRN_SPRG_VDSO_WRITE,r3
Paul Mackerras218309b2013-09-06 13:23:44 +100080
Paul Mackerras218309b2013-09-06 13:23:44 +100081 /* Reload the host's PMU registers */
82 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
83 lbz r4, LPPACA_PMCINUSE(r3)
84 cmpwi r4, 0
85 beq 23f /* skip if not */
Paul Mackerras9bc01a92014-05-26 19:48:40 +100086BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +100087 ld r3, HSTATE_MMCR0(r13)
Paul Mackerras9bc01a92014-05-26 19:48:40 +100088 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
89 cmpwi r4, MMCR0_PMAO
90 beql kvmppc_fix_pmao
91END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +100092 lwz r3, HSTATE_PMC1(r13)
93 lwz r4, HSTATE_PMC2(r13)
94 lwz r5, HSTATE_PMC3(r13)
95 lwz r6, HSTATE_PMC4(r13)
96 lwz r8, HSTATE_PMC5(r13)
97 lwz r9, HSTATE_PMC6(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100098 mtspr SPRN_PMC1, r3
99 mtspr SPRN_PMC2, r4
100 mtspr SPRN_PMC3, r5
101 mtspr SPRN_PMC4, r6
102 mtspr SPRN_PMC5, r8
103 mtspr SPRN_PMC6, r9
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000104 ld r3, HSTATE_MMCR0(r13)
105 ld r4, HSTATE_MMCR1(r13)
106 ld r5, HSTATE_MMCRA(r13)
107 ld r6, HSTATE_SIAR(r13)
108 ld r7, HSTATE_SDAR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000109 mtspr SPRN_MMCR1, r4
110 mtspr SPRN_MMCRA, r5
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100111 mtspr SPRN_SIAR, r6
112 mtspr SPRN_SDAR, r7
113BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000114 ld r8, HSTATE_MMCR2(r13)
115 ld r9, HSTATE_SIER(r13)
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100116 mtspr SPRN_MMCR2, r8
117 mtspr SPRN_SIER, r9
118END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000119 mtspr SPRN_MMCR0, r3
120 isync
12123:
122
123 /*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100124 * Reload DEC. HDEC interrupts were disabled when
125 * we reloaded the host's LPCR value.
126 */
127 ld r3, HSTATE_DECEXP(r13)
128 mftb r4
129 subf r4, r4, r3
130 mtspr SPRN_DEC, r4
131
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000132 /* hwthread_req may have got set by cede or no vcpu, so clear it */
133 li r0, 0
134 stb r0, HSTATE_HWTHREAD_REQ(r13)
135
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100136 /*
Paul Mackerras218309b2013-09-06 13:23:44 +1000137 * For external and machine check interrupts, we need
138 * to call the Linux handler to process the interrupt.
139 * We do that by jumping to absolute address 0x500 for
140 * external interrupts, or the machine_check_fwnmi label
141 * for machine checks (since firmware might have patched
142 * the vector area at 0x200). The [h]rfid at the end of the
143 * handler will return to the book3s_hv_interrupts.S code.
144 * For other interrupts we do the rfid to get back
145 * to the book3s_hv_interrupts.S code here.
146 */
147 ld r8, 112+PPC_LR_STKOFF(r1)
148 addi r1, r1, 112
149 ld r7, HSTATE_HOST_MSR(r13)
150
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 Mackerrase9cf1e02016-11-18 13:11:42 +1100880 /* POWER8-only registers */
Paul Mackerras83677f52016-11-16 22:33:27 +1100881 ld r5, VCPU_TCSCR(r4)
882 ld r6, VCPU_ACOP(r4)
883 ld r7, VCPU_CSIGR(r4)
884 ld r8, VCPU_TACR(r4)
885 mtspr SPRN_TCSCR, r5
886 mtspr SPRN_ACOP, r6
887 mtspr SPRN_CSIGR, r7
888 mtspr SPRN_TACR, r8
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100889FTR_SECTION_ELSE
890 /* POWER9-only registers */
891 ld r5, VCPU_TID(r4)
892 ld r6, VCPU_PSSCR(r4)
893 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
894 mtspr SPRN_TIDR, r5
895 mtspr SPRN_PSSCR, r6
896ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11008978:
898
Paul Mackerrasde56a942011-06-29 00:21:34 +0000899 /*
900 * Set the decrementer to the guest decrementer.
901 */
902 ld r8,VCPU_DEC_EXPIRES(r4)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +1100903 /* r8 is a host timebase value here, convert to guest TB */
904 ld r5,HSTATE_KVM_VCORE(r13)
905 ld r6,VCORE_TB_OFFSET(r5)
906 add r8,r8,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000907 mftb r7
908 subf r3,r7,r8
909 mtspr SPRN_DEC,r3
910 stw r3,VCPU_DEC(r4)
911
912 ld r5, VCPU_SPRG0(r4)
913 ld r6, VCPU_SPRG1(r4)
914 ld r7, VCPU_SPRG2(r4)
915 ld r8, VCPU_SPRG3(r4)
916 mtspr SPRN_SPRG0, r5
917 mtspr SPRN_SPRG1, r6
918 mtspr SPRN_SPRG2, r7
919 mtspr SPRN_SPRG3, r8
920
Paul Mackerrasde56a942011-06-29 00:21:34 +0000921 /* Load up DAR and DSISR */
922 ld r5, VCPU_DAR(r4)
923 lwz r6, VCPU_DSISR(r4)
924 mtspr SPRN_DAR, r5
925 mtspr SPRN_DSISR, r6
926
Paul Mackerrasde56a942011-06-29 00:21:34 +0000927 /* Restore AMR and UAMOR, set AMOR to all 1s */
928 ld r5,VCPU_AMR(r4)
929 ld r6,VCPU_UAMOR(r4)
930 li r7,-1
931 mtspr SPRN_AMR,r5
932 mtspr SPRN_UAMOR,r6
933 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000934
935 /* Restore state of CTRL run bit; assume 1 on entry */
936 lwz r5,VCPU_CTRL(r4)
937 andi. r5,r5,1
938 bne 4f
939 mfspr r6,SPRN_CTRLF
940 clrrdi r6,r6,1
941 mtspr SPRN_CTRLT,r6
9424:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100943 /* Secondary threads wait for primary to have done partition switch */
944 ld r5, HSTATE_KVM_VCORE(r13)
945 lbz r6, HSTATE_PTID(r13)
946 cmpwi r6, 0
947 beq 21f
948 lbz r0, VCORE_IN_GUEST(r5)
949 cmpwi r0, 0
950 bne 21f
951 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +100095220: lwz r3, VCORE_ENTRY_EXIT(r5)
953 cmpwi r3, 0x100
954 bge no_switch_exit
955 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100956 cmpwi r0, 0
957 beq 20b
958 HMT_MEDIUM
95921:
960 /* Set LPCR. */
961 ld r8,VCORE_LPCR(r5)
962 mtspr SPRN_LPCR,r8
963 isync
964
965 /* Check if HDEC expires soon */
966 mfspr r3, SPRN_HDEC
967 cmpwi r3, 512 /* 1 microsecond */
968 blt hdec_soon
969
Suresh Warrier37f55d32016-08-19 15:35:46 +1000970deliver_guest_interrupt:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000971 ld r6, VCPU_CTR(r4)
Sam bobroffc63517c2015-05-27 09:56:57 +1000972 ld r7, VCPU_XER(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000973
974 mtctr r6
975 mtxer r7
976
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100977kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000978 ld r10, VCPU_PC(r4)
979 ld r11, VCPU_MSR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000980 ld r6, VCPU_SRR0(r4)
981 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100982 mtspr SPRN_SRR0, r6
983 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000984
Paul Mackerras4619ac82013-04-17 20:31:41 +0000985 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000986 rldicl r11, r11, 63 - MSR_HV_LG, 1
987 rotldi r11, r11, 1 + MSR_HV_LG
988 ori r11, r11, MSR_ME
989
Paul Mackerras19ccb762011-07-23 17:42:46 +1000990 /* Check if we can deliver an external or decrementer interrupt now */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100991 ld r0, VCPU_PENDING_EXC(r4)
992 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
993 cmpdi cr1, r0, 0
994 andi. r8, r11, MSR_EE
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100995 mfspr r8, SPRN_LPCR
996 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
997 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
998 mtspr SPRN_LPCR, r8
Paul Mackerras19ccb762011-07-23 17:42:46 +1000999 isync
Paul Mackerras19ccb762011-07-23 17:42:46 +10001000 beq 5f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001001 li r0, BOOK3S_INTERRUPT_EXTERNAL
1002 bne cr1, 12f
1003 mfspr r0, SPRN_DEC
1004 cmpwi r0, 0
1005 li r0, BOOK3S_INTERRUPT_DECREMENTER
1006 bge 5f
1007
100812: mtspr SPRN_SRR0, r10
Paul Mackerras19ccb762011-07-23 17:42:46 +10001009 mr r10,r0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001010 mtspr SPRN_SRR1, r11
Michael Neulinge4e38122014-03-25 10:47:02 +11001011 mr r9, r4
1012 bl kvmppc_msr_interrupt
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +110010135:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001014
Liu Ping Fan27025a62013-11-19 14:12:48 +08001015/*
1016 * Required state:
1017 * R4 = vcpu
1018 * R10: value for HSRR0
1019 * R11: value for HSRR1
1020 * R13 = PACA
1021 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001022fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001023 li r0,0
1024 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001025 mtspr SPRN_HSRR0,r10
1026 mtspr SPRN_HSRR1,r11
1027
1028 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001029 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +00001030 stb r9, HSTATE_IN_GUEST(r13)
1031
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001032#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1033 /* Accumulate timing */
1034 addi r3, r4, VCPU_TB_GUEST
1035 bl kvmhv_accumulate_time
1036#endif
1037
Paul Mackerrasde56a942011-06-29 00:21:34 +00001038 /* Enter guest */
1039
Paul Mackerras0acb9112013-02-04 18:10:51 +00001040BEGIN_FTR_SECTION
1041 ld r5, VCPU_CFAR(r4)
1042 mtspr SPRN_CFAR, r5
1043END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001044BEGIN_FTR_SECTION
1045 ld r0, VCPU_PPR(r4)
1046END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001047
Paul Mackerrasde56a942011-06-29 00:21:34 +00001048 ld r5, VCPU_LR(r4)
1049 lwz r6, VCPU_CR(r4)
1050 mtlr r5
1051 mtcr r6
1052
Michael Neulingc75df6f2012-06-25 13:33:10 +00001053 ld r1, VCPU_GPR(R1)(r4)
1054 ld r2, VCPU_GPR(R2)(r4)
1055 ld r3, VCPU_GPR(R3)(r4)
1056 ld r5, VCPU_GPR(R5)(r4)
1057 ld r6, VCPU_GPR(R6)(r4)
1058 ld r7, VCPU_GPR(R7)(r4)
1059 ld r8, VCPU_GPR(R8)(r4)
1060 ld r9, VCPU_GPR(R9)(r4)
1061 ld r10, VCPU_GPR(R10)(r4)
1062 ld r11, VCPU_GPR(R11)(r4)
1063 ld r12, VCPU_GPR(R12)(r4)
1064 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001065
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001066BEGIN_FTR_SECTION
1067 mtspr SPRN_PPR, r0
1068END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1069 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001070 ld r4, VCPU_GPR(R4)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001071
1072 hrfid
1073 b .
1074
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001075secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001076 li r12, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001077 cmpdi r4, 0
1078 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001079 stw r12, VCPU_TRAP(r4)
1080#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001081 addi r3, r4, VCPU_TB_RMEXIT
1082 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001083#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100108411: b kvmhv_switch_to_host
1085
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001086no_switch_exit:
1087 HMT_MEDIUM
1088 li r12, 0
1089 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001090hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001091 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000109212: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001093 mr r9, r4
1094#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001095 addi r3, r4, VCPU_TB_RMEXIT
1096 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001097#endif
Paul Mackerras6af27c82015-03-28 14:21:10 +11001098 b guest_exit_cont
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001099
Paul Mackerrasde56a942011-06-29 00:21:34 +00001100/******************************************************************************
1101 * *
1102 * Exit code *
1103 * *
1104 *****************************************************************************/
1105
1106/*
1107 * We come here from the first-level interrupt handlers.
1108 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301109 .globl kvmppc_interrupt_hv
1110kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001111 /*
1112 * Register contents:
Nicholas Piggind3918e72016-12-22 04:29:25 +10001113 * R12 = (guest CR << 32) | interrupt vector
Paul Mackerrasde56a942011-06-29 00:21:34 +00001114 * R13 = PACA
Nicholas Piggind3918e72016-12-22 04:29:25 +10001115 * guest R12 saved in shadow VCPU SCRATCH0
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001116 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
Paul Mackerrasde56a942011-06-29 00:21:34 +00001117 * guest R13 saved in SPRN_SCRATCH0
1118 */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001119 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001120 lbz r9, HSTATE_IN_GUEST(r13)
1121 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1122 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301123#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1124 cmpwi r9, KVM_GUEST_MODE_GUEST
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001125 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301126 beq kvmppc_interrupt_pr
1127#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001128 /* We're now back in the host but in guest MMU context */
1129 li r9, KVM_GUEST_MODE_HOST_HV
1130 stb r9, HSTATE_IN_GUEST(r13)
1131
Paul Mackerrasde56a942011-06-29 00:21:34 +00001132 ld r9, HSTATE_KVM_VCPU(r13)
1133
1134 /* Save registers */
1135
Michael Neulingc75df6f2012-06-25 13:33:10 +00001136 std r0, VCPU_GPR(R0)(r9)
1137 std r1, VCPU_GPR(R1)(r9)
1138 std r2, VCPU_GPR(R2)(r9)
1139 std r3, VCPU_GPR(R3)(r9)
1140 std r4, VCPU_GPR(R4)(r9)
1141 std r5, VCPU_GPR(R5)(r9)
1142 std r6, VCPU_GPR(R6)(r9)
1143 std r7, VCPU_GPR(R7)(r9)
1144 std r8, VCPU_GPR(R8)(r9)
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001145 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001146 std r0, VCPU_GPR(R9)(r9)
1147 std r10, VCPU_GPR(R10)(r9)
1148 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001149 ld r3, HSTATE_SCRATCH0(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001150 std r3, VCPU_GPR(R12)(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001151 /* CR is in the high half of r12 */
1152 srdi r4, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001153 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001154BEGIN_FTR_SECTION
1155 ld r3, HSTATE_CFAR(r13)
1156 std r3, VCPU_CFAR(r9)
1157END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001158BEGIN_FTR_SECTION
1159 ld r4, HSTATE_PPR(r13)
1160 std r4, VCPU_PPR(r9)
1161END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001162
1163 /* Restore R1/R2 so we can handle faults */
1164 ld r1, HSTATE_HOST_R1(r13)
1165 ld r2, PACATOC(r13)
1166
1167 mfspr r10, SPRN_SRR0
1168 mfspr r11, SPRN_SRR1
1169 std r10, VCPU_SRR0(r9)
1170 std r11, VCPU_SRR1(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001171 /* trap is in the low half of r12, clear CR from the high half */
1172 clrldi r12, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001173 andi. r0, r12, 2 /* need to read HSRR0/1? */
1174 beq 1f
1175 mfspr r10, SPRN_HSRR0
1176 mfspr r11, SPRN_HSRR1
1177 clrrdi r12, r12, 2
11781: std r10, VCPU_PC(r9)
1179 std r11, VCPU_MSR(r9)
1180
1181 GET_SCRATCH0(r3)
1182 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001183 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001184 std r4, VCPU_LR(r9)
1185
Paul Mackerrasde56a942011-06-29 00:21:34 +00001186 stw r12,VCPU_TRAP(r9)
1187
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001188#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1189 addi r3, r9, VCPU_TB_RMINTR
1190 mr r4, r9
1191 bl kvmhv_accumulate_time
1192 ld r5, VCPU_GPR(R5)(r9)
1193 ld r6, VCPU_GPR(R6)(r9)
1194 ld r7, VCPU_GPR(R7)(r9)
1195 ld r8, VCPU_GPR(R8)(r9)
1196#endif
1197
Paul Mackerras4a157d62014-12-03 13:30:39 +11001198 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001199 if this is an HEI (HV emulation interrupt, e40) */
1200 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001201 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001202 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1203 bne 11f
1204 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100120511: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001206
1207 /* these are volatile across C function calls */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001208#ifdef CONFIG_RELOCATABLE
1209 ld r3, HSTATE_SCRATCH1(r13)
1210 mtctr r3
1211#else
Paul Mackerras697d3892011-12-12 12:36:37 +00001212 mfctr r3
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001213#endif
Paul Mackerras697d3892011-12-12 12:36:37 +00001214 mfxer r4
1215 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001216 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001217
Paul Mackerras697d3892011-12-12 12:36:37 +00001218 /* If this is a page table miss then see if it's theirs or ours */
1219 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1220 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +00001221 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1222 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +00001223
Paul Mackerrasde56a942011-06-29 00:21:34 +00001224 /* See if this is a leftover HDEC interrupt */
1225 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1226 bne 2f
1227 mfspr r3,SPRN_HDEC
1228 cmpwi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001229 mr r4,r9
1230 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000012312:
Paul Mackerras697d3892011-12-12 12:36:37 +00001232 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001233 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1234 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001235
Paul Mackerras66feed62015-03-28 14:21:12 +11001236 /* Hypervisor doorbell - exit only if host IPI flag set */
1237 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1238 bne 3f
1239 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301240 cmpwi r0, 0
Paul Mackerras66feed62015-03-28 14:21:12 +11001241 beq 4f
1242 b guest_exit_cont
12433:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001244 /* External interrupt ? */
1245 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001246 bne+ guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001247
1248 /* External interrupt, first check for host_ipi. If this is
1249 * set, we know the host wants us out so let's do it now
1250 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001251 bl kvmppc_read_intr
Suresh Warrier37f55d32016-08-19 15:35:46 +10001252
1253 /*
1254 * Restore the active volatile registers after returning from
1255 * a C function.
1256 */
1257 ld r9, HSTATE_KVM_VCPU(r13)
1258 li r12, BOOK3S_INTERRUPT_EXTERNAL
1259
1260 /*
1261 * kvmppc_read_intr return codes:
1262 *
1263 * Exit to host (r3 > 0)
1264 * 1 An interrupt is pending that needs to be handled by the host
1265 * Exit guest and return to host by branching to guest_exit_cont
1266 *
Suresh Warrierf7af5202016-08-19 15:35:52 +10001267 * 2 Passthrough that needs completion in the host
1268 * Exit guest and return to host by branching to guest_exit_cont
1269 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1270 * to indicate to the host to complete handling the interrupt
1271 *
Suresh Warrier37f55d32016-08-19 15:35:46 +10001272 * Before returning to guest, we check if any CPU is heading out
1273 * to the host and if so, we head out also. If no CPUs are heading
1274 * check return values <= 0.
1275 *
1276 * Return to guest (r3 <= 0)
1277 * 0 No external interrupt is pending
1278 * -1 A guest wakeup IPI (which has now been cleared)
1279 * In either case, we return to guest to deliver any pending
1280 * guest interrupts.
Suresh Warriere3c13e52016-08-19 15:35:51 +10001281 *
1282 * -2 A PCI passthrough external interrupt was handled
1283 * (interrupt was delivered directly to guest)
1284 * Return to guest to deliver any pending guest interrupts.
Suresh Warrier37f55d32016-08-19 15:35:46 +10001285 */
1286
Suresh Warrierf7af5202016-08-19 15:35:52 +10001287 cmpdi r3, 1
1288 ble 1f
1289
1290 /* Return code = 2 */
1291 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1292 stw r12, VCPU_TRAP(r9)
1293 b guest_exit_cont
1294
12951: /* Return code <= 1 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001296 cmpdi r3, 0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001297 bgt guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001298
Suresh Warrier37f55d32016-08-19 15:35:46 +10001299 /* Return code <= 0 */
Paul Mackerras66feed62015-03-28 14:21:12 +110013004: ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001301 lwz r0, VCORE_ENTRY_EXIT(r5)
1302 cmpwi r0, 0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001303 mr r4, r9
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001304 blt deliver_guest_interrupt
Paul Mackerrasde56a942011-06-29 00:21:34 +00001305
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001306guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001307 /* Save more register state */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001308 mfdar r6
1309 mfdsisr r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001310 std r6, VCPU_DAR(r9)
1311 stw r7, VCPU_DSISR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001312 /* don't overwrite fault_dar/fault_dsisr if HDSI */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001313 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
Paul Mackerras6af27c82015-03-28 14:21:10 +11001314 beq mc_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001315 std r6, VCPU_FAULT_DAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001316 stw r7, VCPU_FAULT_DSISR(r9)
1317
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001318 /* See if it is a machine check */
1319 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1320 beq machine_check_realmode
1321mc_cont:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001322#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1323 addi r3, r9, VCPU_TB_RMEXIT
1324 mr r4, r9
1325 bl kvmhv_accumulate_time
1326#endif
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001327
Gautham R. Shenoy7e022e72015-05-21 13:57:04 +05301328 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001329 /* Increment exit count, poke other threads to exit */
1330 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001331 nop
1332 ld r9, HSTATE_KVM_VCPU(r13)
1333 lwz r12, VCPU_TRAP(r9)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001334
Paul Mackerrasec257162015-06-24 21:18:03 +10001335 /* Stop others sending VCPU interrupts to this physical CPU */
1336 li r0, -1
1337 stw r0, VCPU_CPU(r9)
1338 stw r0, VCPU_THREAD_CPU(r9)
1339
Paul Mackerrasde56a942011-06-29 00:21:34 +00001340 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001341 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001342 stw r6,VCPU_CTRL(r9)
1343 andi. r0,r6,1
1344 bne 4f
1345 ori r6,r6,1
1346 mtspr SPRN_CTRLT,r6
13474:
1348 /* Read the guest SLB and save it away */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001349 ld r5, VCPU_KVM(r9)
1350 lbz r0, KVM_RADIX(r5)
1351 cmpwi r0, 0
1352 li r5, 0
1353 bne 3f /* for radix, save 0 entries */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001354 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1355 mtctr r0
1356 li r6,0
1357 addi r7,r9,VCPU_SLB
Paul Mackerrasde56a942011-06-29 00:21:34 +000013581: slbmfee r8,r6
1359 andis. r0,r8,SLB_ESID_V@h
1360 beq 2f
1361 add r8,r8,r6 /* put index in */
1362 slbmfev r3,r6
1363 std r8,VCPU_SLB_E(r7)
1364 std r3,VCPU_SLB_V(r7)
1365 addi r7,r7,VCPU_SLB_SIZE
1366 addi r5,r5,1
13672: addi r6,r6,1
1368 bdnz 1b
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110013693: stw r5,VCPU_SLB_MAX(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001370
1371 /*
1372 * Save the guest PURR/SPURR
1373 */
1374 mfspr r5,SPRN_PURR
1375 mfspr r6,SPRN_SPURR
1376 ld r7,VCPU_PURR(r9)
1377 ld r8,VCPU_SPURR(r9)
1378 std r5,VCPU_PURR(r9)
1379 std r6,VCPU_SPURR(r9)
1380 subf r5,r7,r5
1381 subf r6,r8,r6
1382
1383 /*
1384 * Restore host PURR/SPURR and add guest times
1385 * so that the time in the guest gets accounted.
1386 */
1387 ld r3,HSTATE_PURR(r13)
1388 ld r4,HSTATE_SPURR(r13)
1389 add r3,r3,r5
1390 add r4,r4,r6
1391 mtspr SPRN_PURR,r3
1392 mtspr SPRN_SPURR,r4
1393
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001394 /* Save DEC */
1395 mfspr r5,SPRN_DEC
1396 mftb r6
1397 extsw r5,r5
1398 add r5,r5,r6
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001399 /* r5 is a guest timebase value here, convert to host TB */
1400 ld r3,HSTATE_KVM_VCORE(r13)
1401 ld r4,VCORE_TB_OFFSET(r3)
1402 subf r5,r4,r5
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001403 std r5,VCPU_DEC_EXPIRES(r9)
1404
Michael Neulingb005255e2014-01-08 21:25:21 +11001405BEGIN_FTR_SECTION
1406 b 8f
1407END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001408 /* Save POWER8-specific registers */
1409 mfspr r5, SPRN_IAMR
1410 mfspr r6, SPRN_PSPB
1411 mfspr r7, SPRN_FSCR
1412 std r5, VCPU_IAMR(r9)
1413 stw r6, VCPU_PSPB(r9)
1414 std r7, VCPU_FSCR(r9)
1415 mfspr r5, SPRN_IC
Michael Neulingb005255e2014-01-08 21:25:21 +11001416 mfspr r7, SPRN_TAR
1417 std r5, VCPU_IC(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001418 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001419 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001420 std r8, VCPU_EBBHR(r9)
1421 mfspr r5, SPRN_EBBRR
1422 mfspr r6, SPRN_BESCR
Michael Neulingb005255e2014-01-08 21:25:21 +11001423 mfspr r7, SPRN_PID
1424 mfspr r8, SPRN_WORT
Paul Mackerras83677f52016-11-16 22:33:27 +11001425 std r5, VCPU_EBBRR(r9)
1426 std r6, VCPU_BESCR(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001427 stw r7, VCPU_GUEST_PID(r9)
1428 std r8, VCPU_WORT(r9)
Paul Mackerras83677f52016-11-16 22:33:27 +11001429BEGIN_FTR_SECTION
1430 mfspr r5, SPRN_TCSCR
1431 mfspr r6, SPRN_ACOP
1432 mfspr r7, SPRN_CSIGR
1433 mfspr r8, SPRN_TACR
1434 std r5, VCPU_TCSCR(r9)
1435 std r6, VCPU_ACOP(r9)
1436 std r7, VCPU_CSIGR(r9)
1437 std r8, VCPU_TACR(r9)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001438FTR_SECTION_ELSE
1439 mfspr r5, SPRN_TIDR
1440 mfspr r6, SPRN_PSSCR
1441 std r5, VCPU_TID(r9)
1442 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1443 rotldi r6, r6, 60
1444 std r6, VCPU_PSSCR(r9)
1445ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001446 /*
1447 * Restore various registers to 0, where non-zero values
1448 * set by the guest could disrupt the host.
1449 */
1450 li r0, 0
1451 mtspr SPRN_IAMR, r0
1452 mtspr SPRN_CIABR, r0
1453 mtspr SPRN_DAWRX, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001454 mtspr SPRN_WORT, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001455BEGIN_FTR_SECTION
1456 mtspr SPRN_TCSCR, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001457 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1458 li r0, 1
1459 sldi r0, r0, 31
1460 mtspr SPRN_MMCRS, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001461END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +110014628:
1463
Paul Mackerrasde56a942011-06-29 00:21:34 +00001464 /* Save and reset AMR and UAMOR before turning on the MMU */
1465 mfspr r5,SPRN_AMR
1466 mfspr r6,SPRN_UAMOR
1467 std r5,VCPU_AMR(r9)
1468 std r6,VCPU_UAMOR(r9)
1469 li r6,0
1470 mtspr SPRN_AMR,r6
1471
Paul Mackerrasde56a942011-06-29 00:21:34 +00001472 /* Switch DSCR back to host value */
1473 mfspr r8, SPRN_DSCR
1474 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001475 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001476 mtspr SPRN_DSCR, r7
1477
1478 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001479 std r14, VCPU_GPR(R14)(r9)
1480 std r15, VCPU_GPR(R15)(r9)
1481 std r16, VCPU_GPR(R16)(r9)
1482 std r17, VCPU_GPR(R17)(r9)
1483 std r18, VCPU_GPR(R18)(r9)
1484 std r19, VCPU_GPR(R19)(r9)
1485 std r20, VCPU_GPR(R20)(r9)
1486 std r21, VCPU_GPR(R21)(r9)
1487 std r22, VCPU_GPR(R22)(r9)
1488 std r23, VCPU_GPR(R23)(r9)
1489 std r24, VCPU_GPR(R24)(r9)
1490 std r25, VCPU_GPR(R25)(r9)
1491 std r26, VCPU_GPR(R26)(r9)
1492 std r27, VCPU_GPR(R27)(r9)
1493 std r28, VCPU_GPR(R28)(r9)
1494 std r29, VCPU_GPR(R29)(r9)
1495 std r30, VCPU_GPR(R30)(r9)
1496 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001497
1498 /* Save SPRGs */
1499 mfspr r3, SPRN_SPRG0
1500 mfspr r4, SPRN_SPRG1
1501 mfspr r5, SPRN_SPRG2
1502 mfspr r6, SPRN_SPRG3
1503 std r3, VCPU_SPRG0(r9)
1504 std r4, VCPU_SPRG1(r9)
1505 std r5, VCPU_SPRG2(r9)
1506 std r6, VCPU_SPRG3(r9)
1507
Paul Mackerras89436332012-03-02 01:38:23 +00001508 /* save FP state */
1509 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001510 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001511
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001512#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1513BEGIN_FTR_SECTION
Paul Mackerrasf024ee02016-06-22 14:21:59 +10001514 bl kvmppc_save_tm
1515END_FTR_SECTION_IFSET(CPU_FTR_TM)
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001516#endif
1517
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001518 /* Increment yield count if they have a VPA */
1519 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1520 cmpdi r8, 0
1521 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001522 li r4, LPPACA_YIELDCOUNT
1523 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001524 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001525 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001526 li r3, 1
1527 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000152825:
1529 /* Save PMU registers if requested */
1530 /* r8 and cr0.eq are live here */
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001531BEGIN_FTR_SECTION
1532 /*
1533 * POWER8 seems to have a hardware bug where setting
1534 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1535 * when some counters are already negative doesn't seem
1536 * to cause a performance monitor alert (and hence interrupt).
1537 * The effect of this is that when saving the PMU state,
1538 * if there is no PMU alert pending when we read MMCR0
1539 * before freezing the counters, but one becomes pending
1540 * before we read the counters, we lose it.
1541 * To work around this, we need a way to freeze the counters
1542 * before reading MMCR0. Normally, freezing the counters
1543 * is done by writing MMCR0 (to set MMCR0[FC]) which
1544 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1545 * we can also freeze the counters using MMCR2, by writing
1546 * 1s to all the counter freeze condition bits (there are
1547 * 9 bits each for 6 counters).
1548 */
1549 li r3, -1 /* set all freeze bits */
1550 clrrdi r3, r3, 10
1551 mfspr r10, SPRN_MMCR2
1552 mtspr SPRN_MMCR2, r3
1553 isync
1554END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001555 li r3, 1
1556 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1557 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1558 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001559 mfspr r6, SPRN_MMCRA
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001560 /* Clear MMCRA in order to disable SDAR updates */
Paul Mackerras89436332012-03-02 01:38:23 +00001561 li r7, 0
1562 mtspr SPRN_MMCRA, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001563 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001564 beq 21f /* if no VPA, save PMU stuff anyway */
1565 lbz r7, LPPACA_PMCINUSE(r8)
1566 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1567 bne 21f
1568 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1569 b 22f
157021: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001571 mfspr r7, SPRN_SIAR
1572 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001573 std r4, VCPU_MMCR(r9)
1574 std r5, VCPU_MMCR + 8(r9)
1575 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001576BEGIN_FTR_SECTION
1577 std r10, VCPU_MMCR + 24(r9)
1578END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras14941782013-09-06 13:11:18 +10001579 std r7, VCPU_SIAR(r9)
1580 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001581 mfspr r3, SPRN_PMC1
1582 mfspr r4, SPRN_PMC2
1583 mfspr r5, SPRN_PMC3
1584 mfspr r6, SPRN_PMC4
1585 mfspr r7, SPRN_PMC5
1586 mfspr r8, SPRN_PMC6
1587 stw r3, VCPU_PMC(r9)
1588 stw r4, VCPU_PMC + 4(r9)
1589 stw r5, VCPU_PMC + 8(r9)
1590 stw r6, VCPU_PMC + 12(r9)
1591 stw r7, VCPU_PMC + 16(r9)
1592 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001593BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +11001594 mfspr r5, SPRN_SIER
Paul Mackerras83677f52016-11-16 22:33:27 +11001595 std r5, VCPU_SIER(r9)
1596BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001597 mfspr r6, SPRN_SPMC1
1598 mfspr r7, SPRN_SPMC2
1599 mfspr r8, SPRN_MMCRS
Michael Neulingb005255e2014-01-08 21:25:21 +11001600 stw r6, VCPU_PMC + 24(r9)
1601 stw r7, VCPU_PMC + 28(r9)
1602 std r8, VCPU_MMCR + 32(r9)
1603 lis r4, 0x8000
1604 mtspr SPRN_MMCRS, r4
Paul Mackerras83677f52016-11-16 22:33:27 +11001605END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001606END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000160722:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001608 /* Clear out SLB */
1609 li r5,0
1610 slbmte r5,r5
1611 slbia
1612 ptesync
1613
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001614 /* Restore host values of some registers */
1615BEGIN_FTR_SECTION
1616 ld r5, STACK_SLOT_TID(r1)
1617 ld r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001618 ld r7, STACK_SLOT_PID(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001619 mtspr SPRN_TIDR, r5
1620 mtspr SPRN_PSSCR, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001621 mtspr SPRN_PID, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001622END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1623
Paul Mackerrasde56a942011-06-29 00:21:34 +00001624 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001625 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001626 * We don't have to lock against tlbies but we do
1627 * have to coordinate the hardware threads.
1628 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001629kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001630 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001631 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001632 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1633 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001634 cmpwi r3,0
1635 beq 15f
1636 HMT_LOW
163713: lbz r3,VCORE_IN_GUEST(r5)
1638 cmpwi r3,0
1639 bne 13b
1640 HMT_MEDIUM
1641 b 16f
1642
1643 /* Primary thread waits for all the secondaries to exit guest */
164415: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001645 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001646 clrldi r3,r3,56
1647 cmpw r3,r0
1648 bne 15b
1649 isync
1650
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001651 /* Did we actually switch to the guest at all? */
1652 lbz r6, VCORE_IN_GUEST(r5)
1653 cmpwi r6, 0
1654 beq 19f
1655
Paul Mackerrasde56a942011-06-29 00:21:34 +00001656 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001657 lwz r7,KVM_HOST_LPID(r4)
Paul Mackerras7a840842016-11-16 22:25:20 +11001658BEGIN_FTR_SECTION
1659 ld r6,KVM_HOST_SDR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001660 li r8,LPID_RSVD /* switch to reserved LPID */
1661 mtspr SPRN_LPID,r8
1662 ptesync
Paul Mackerras7a840842016-11-16 22:25:20 +11001663 mtspr SPRN_SDR1,r6 /* switch to host page table */
1664END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001665 mtspr SPRN_LPID,r7
1666 isync
1667
Michael Neulingb005255e2014-01-08 21:25:21 +11001668BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001669 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +11001670 mfspr r7, SPRN_DPDES
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001671 mfspr r8, SPRN_VTB
Michael Neulingb005255e2014-01-08 21:25:21 +11001672 std r7, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001673 std r8, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +11001674 /* clear DPDES so we don't get guest doorbells in the host */
1675 li r8, 0
1676 mtspr SPRN_DPDES, r8
1677END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1678
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301679 /* If HMI, call kvmppc_realmode_hmi_handler() */
1680 cmpwi r12, BOOK3S_INTERRUPT_HMI
1681 bne 27f
1682 bl kvmppc_realmode_hmi_handler
1683 nop
1684 li r12, BOOK3S_INTERRUPT_HMI
1685 /*
1686 * At this point kvmppc_realmode_hmi_handler would have resync-ed
1687 * the TB. Hence it is not required to subtract guest timebase
1688 * offset from timebase. So, skip it.
1689 *
1690 * Also, do not call kvmppc_subcore_exit_guest() because it has
1691 * been invoked as part of kvmppc_realmode_hmi_handler().
1692 */
1693 b 30f
1694
169527:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001696 /* Subtract timebase offset from timebase */
1697 ld r8,VCORE_TB_OFFSET(r5)
1698 cmpdi r8,0
1699 beq 17f
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001700 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001701 subf r8,r8,r6
1702 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1703 mftb r7 /* check if lower 24 bits overflowed */
1704 clrldi r6,r6,40
1705 clrldi r7,r7,40
1706 cmpld r7,r6
1707 bge 17f
1708 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1709 mtspr SPRN_TBU40,r8
1710
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530171117: bl kvmppc_subcore_exit_guest
1712 nop
171330: ld r5,HSTATE_KVM_VCORE(r13)
1714 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1715
Paul Mackerrasde56a942011-06-29 00:21:34 +00001716 /* Reset PCR */
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301717 ld r0, VCORE_PCR(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001718 cmpdi r0, 0
1719 beq 18f
1720 li r0, 0
1721 mtspr SPRN_PCR, r0
172218:
1723 /* Signal secondary CPUs to continue */
1724 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000172519: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001726 mtspr SPRN_HDEC,r8
1727
172816: ld r8,KVM_HOST_LPCR(r4)
1729 mtspr SPRN_LPCR,r8
1730 isync
Paul Mackerrasde56a942011-06-29 00:21:34 +00001731
1732 /* load host SLB entries */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001733BEGIN_MMU_FTR_SECTION
1734 b 0f
1735END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001736 ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001737
1738 .rept SLB_NUM_BOLTED
Alexander Graf0865a582014-06-11 10:36:17 +02001739 li r3, SLBSHADOW_SAVEAREA
1740 LDX_BE r5, r8, r3
1741 addi r3, r3, 8
1742 LDX_BE r6, r8, r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00001743 andis. r7,r5,SLB_ESID_V@h
1744 beq 1f
1745 slbmte r6,r5
17461: addi r8,r8,16
1747 .endr
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110017480:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001749#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1750 /* Finish timing, if we have a vcpu */
1751 ld r4, HSTATE_KVM_VCPU(r13)
1752 cmpdi r4, 0
1753 li r3, 0
1754 beq 2f
1755 bl kvmhv_accumulate_time
17562:
1757#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001758 /* Unset guest mode */
1759 li r0, KVM_GUEST_MODE_NONE
1760 stb r0, HSTATE_IN_GUEST(r13)
1761
Paul Mackerras218309b2013-09-06 13:23:44 +10001762 ld r0, 112+PPC_LR_STKOFF(r1)
1763 addi r1, r1, 112
1764 mtlr r0
1765 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001766
Paul Mackerras697d3892011-12-12 12:36:37 +00001767/*
1768 * Check whether an HDSI is an HPTE not found fault or something else.
1769 * If it is an HPTE not found fault that is due to the guest accessing
1770 * a page that they have mapped but which we have paged out, then
1771 * we continue on with the guest exit path. In all other cases,
1772 * reflect the HDSI to the guest as a DSI.
1773 */
1774kvmppc_hdsi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001775 ld r3, VCPU_KVM(r9)
1776 lbz r0, KVM_RADIX(r3)
1777 cmpwi r0, 0
Paul Mackerras697d3892011-12-12 12:36:37 +00001778 mfspr r4, SPRN_HDAR
1779 mfspr r6, SPRN_HDSISR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001780 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
Paul Mackerras4cf302b2011-12-12 12:38:51 +00001781 /* HPTE not found fault or protection fault? */
1782 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00001783 beq 1f /* if not, send it to the guest */
Paul Mackerrasef8c6402017-01-30 21:21:43 +11001784BEGIN_FTR_SECTION
1785 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
1786 b 4f
1787END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras697d3892011-12-12 12:36:37 +00001788 andi. r0, r11, MSR_DR /* data relocation enabled? */
1789 beq 3f
1790 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001791 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001792 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
1793 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000017944: std r4, VCPU_FAULT_DAR(r9)
1795 stw r6, VCPU_FAULT_DSISR(r9)
1796
1797 /* Search the hash table. */
1798 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001799 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001800 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00001801 ld r9, HSTATE_KVM_VCPU(r13)
1802 ld r10, VCPU_PC(r9)
1803 ld r11, VCPU_MSR(r9)
1804 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1805 cmpdi r3, 0 /* retry the instruction */
1806 beq 6f
1807 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001808 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001809 cmpdi r3, -2 /* MMIO emulation; need instr word */
1810 beq 2f
1811
Paul Mackerrascf29b212015-10-27 16:10:20 +11001812 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00001813 ld r4, VCPU_FAULT_DAR(r9)
1814 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110018151: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00001816 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110018177: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00001818 mtspr SPRN_SRR0, r10
1819 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001820 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001821 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001822fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000018236: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001824 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001825 mtctr r7
1826 mtxer r8
1827 mr r4, r9
1828 b fast_guest_return
1829
18303: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1831 ld r5, KVM_VRMA_SLB_V(r5)
1832 b 4b
1833
1834 /* If this is for emulated MMIO, load the instruction word */
18352: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1836
1837 /* Set guest mode to 'jump over instruction' so if lwz faults
1838 * we'll just continue at the next IP. */
1839 li r0, KVM_GUEST_MODE_SKIP
1840 stb r0, HSTATE_IN_GUEST(r13)
1841
1842 /* Do the access with MSR:DR enabled */
1843 mfmsr r3
1844 ori r4, r3, MSR_DR /* Enable paging for data */
1845 mtmsrd r4
1846 lwz r8, 0(r10)
1847 mtmsrd r3
1848
1849 /* Store the result */
1850 stw r8, VCPU_LAST_INST(r9)
1851
1852 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001853 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00001854 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001855 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00001856
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001857.Lradix_hdsi:
1858 std r4, VCPU_FAULT_DAR(r9)
1859 stw r6, VCPU_FAULT_DSISR(r9)
1860.Lradix_hisi:
1861 mfspr r5, SPRN_ASDR
1862 std r5, VCPU_FAULT_GPA(r9)
1863 b guest_exit_cont
1864
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001865/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001866 * Similarly for an HISI, reflect it to the guest as an ISI unless
1867 * it is an HPTE not found fault for a page that we have paged out.
1868 */
1869kvmppc_hisi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001870 ld r3, VCPU_KVM(r9)
1871 lbz r0, KVM_RADIX(r3)
1872 cmpwi r0, 0
1873 bne .Lradix_hisi /* for radix, just save ASDR */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001874 andis. r0, r11, SRR1_ISI_NOPT@h
1875 beq 1f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11001876BEGIN_FTR_SECTION
1877 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
1878 b 4f
1879END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras342d3db2011-12-12 12:38:05 +00001880 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1881 beq 3f
1882 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001883 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001884 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
1885 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000018864:
1887 /* Search the hash table. */
1888 mr r3, r9 /* vcpu pointer */
1889 mr r4, r10
1890 mr r6, r11
1891 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001892 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00001893 ld r9, HSTATE_KVM_VCPU(r13)
1894 ld r10, VCPU_PC(r9)
1895 ld r11, VCPU_MSR(r9)
1896 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1897 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001898 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001899 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001900 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00001901
Paul Mackerrascf29b212015-10-27 16:10:20 +11001902 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001903 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110019041: li r0, BOOK3S_INTERRUPT_INST_STORAGE
19057: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00001906 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001907 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001908 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001909 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001910
19113: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1912 ld r5, KVM_VRMA_SLB_V(r6)
1913 b 4b
1914
1915/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001916 * Try to handle an hcall in real mode.
1917 * Returns to the guest if we handle it, or continues on up to
1918 * the kernel if we can't (i.e. if we don't have a handler for
1919 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001920 *
1921 * r5 - r8 contain hcall args,
1922 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001923 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001924hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00001925 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001926 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08001927 /* sc 1 from userspace - reflect to guest syscall */
1928 bne sc_1_fast_return
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001929 clrrdi r3,r3,2
1930 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001931 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10001932 /* See if this hcall is enabled for in-kernel handling */
1933 ld r4, VCPU_KVM(r9)
1934 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
1935 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
1936 add r4, r4, r0
1937 ld r0, KVM_ENABLED_HCALLS(r4)
1938 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
1939 srd r0, r0, r4
1940 andi. r0, r0, 1
1941 beq guest_exit_cont
1942 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001943 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10001944 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001945 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001946 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10001947 add r12,r3,r4
1948 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001949 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001950 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001951 bctrl
1952 cmpdi r3,H_TOO_HARD
1953 beq hcall_real_fallback
1954 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001955 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001956 ld r10,VCPU_PC(r4)
1957 ld r11,VCPU_MSR(r4)
1958 b fast_guest_return
1959
Liu Ping Fan27025a62013-11-19 14:12:48 +08001960sc_1_fast_return:
1961 mtspr SPRN_SRR0,r10
1962 mtspr SPRN_SRR1,r11
1963 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11001964 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08001965 mr r4,r9
1966 b fast_guest_return
1967
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001968 /* We've attempted a real mode hcall, but it's punted it back
1969 * to userspace. We need to restore some clobbered volatiles
1970 * before resuming the pass-it-to-qemu path */
1971hcall_real_fallback:
1972 li r12,BOOK3S_INTERRUPT_SYSCALL
1973 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001974
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001975 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001976
1977 .globl hcall_real_table
1978hcall_real_table:
1979 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001980 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
1981 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
1982 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10001983 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
1984 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001985 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
1986 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11001987 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001988 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001989 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001990 .long 0 /* 0x2c */
1991 .long 0 /* 0x30 */
1992 .long 0 /* 0x34 */
1993 .long 0 /* 0x38 */
1994 .long 0 /* 0x3c */
1995 .long 0 /* 0x40 */
1996 .long 0 /* 0x44 */
1997 .long 0 /* 0x48 */
1998 .long 0 /* 0x4c */
1999 .long 0 /* 0x50 */
2000 .long 0 /* 0x54 */
2001 .long 0 /* 0x58 */
2002 .long 0 /* 0x5c */
2003 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002004#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002005 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2006 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2007 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002008 .long 0 /* 0x70 - H_IPOLL */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002009 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002010#else
2011 .long 0 /* 0x64 - H_EOI */
2012 .long 0 /* 0x68 - H_CPPR */
2013 .long 0 /* 0x6c - H_IPI */
2014 .long 0 /* 0x70 - H_IPOLL */
2015 .long 0 /* 0x74 - H_XIRR */
2016#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002017 .long 0 /* 0x78 */
2018 .long 0 /* 0x7c */
2019 .long 0 /* 0x80 */
2020 .long 0 /* 0x84 */
2021 .long 0 /* 0x88 */
2022 .long 0 /* 0x8c */
2023 .long 0 /* 0x90 */
2024 .long 0 /* 0x94 */
2025 .long 0 /* 0x98 */
2026 .long 0 /* 0x9c */
2027 .long 0 /* 0xa0 */
2028 .long 0 /* 0xa4 */
2029 .long 0 /* 0xa8 */
2030 .long 0 /* 0xac */
2031 .long 0 /* 0xb0 */
2032 .long 0 /* 0xb4 */
2033 .long 0 /* 0xb8 */
2034 .long 0 /* 0xbc */
2035 .long 0 /* 0xc0 */
2036 .long 0 /* 0xc4 */
2037 .long 0 /* 0xc8 */
2038 .long 0 /* 0xcc */
2039 .long 0 /* 0xd0 */
2040 .long 0 /* 0xd4 */
2041 .long 0 /* 0xd8 */
2042 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002043 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11002044 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002045 .long 0 /* 0xe8 */
2046 .long 0 /* 0xec */
2047 .long 0 /* 0xf0 */
2048 .long 0 /* 0xf4 */
2049 .long 0 /* 0xf8 */
2050 .long 0 /* 0xfc */
2051 .long 0 /* 0x100 */
2052 .long 0 /* 0x104 */
2053 .long 0 /* 0x108 */
2054 .long 0 /* 0x10c */
2055 .long 0 /* 0x110 */
2056 .long 0 /* 0x114 */
2057 .long 0 /* 0x118 */
2058 .long 0 /* 0x11c */
2059 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002060 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11002061 .long 0 /* 0x128 */
2062 .long 0 /* 0x12c */
2063 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002064 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002065 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11002066 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Michael Ellermane928e9c2015-03-20 20:39:41 +11002067 .long 0 /* 0x140 */
2068 .long 0 /* 0x144 */
2069 .long 0 /* 0x148 */
2070 .long 0 /* 0x14c */
2071 .long 0 /* 0x150 */
2072 .long 0 /* 0x154 */
2073 .long 0 /* 0x158 */
2074 .long 0 /* 0x15c */
2075 .long 0 /* 0x160 */
2076 .long 0 /* 0x164 */
2077 .long 0 /* 0x168 */
2078 .long 0 /* 0x16c */
2079 .long 0 /* 0x170 */
2080 .long 0 /* 0x174 */
2081 .long 0 /* 0x178 */
2082 .long 0 /* 0x17c */
2083 .long 0 /* 0x180 */
2084 .long 0 /* 0x184 */
2085 .long 0 /* 0x188 */
2086 .long 0 /* 0x18c */
2087 .long 0 /* 0x190 */
2088 .long 0 /* 0x194 */
2089 .long 0 /* 0x198 */
2090 .long 0 /* 0x19c */
2091 .long 0 /* 0x1a0 */
2092 .long 0 /* 0x1a4 */
2093 .long 0 /* 0x1a8 */
2094 .long 0 /* 0x1ac */
2095 .long 0 /* 0x1b0 */
2096 .long 0 /* 0x1b4 */
2097 .long 0 /* 0x1b8 */
2098 .long 0 /* 0x1bc */
2099 .long 0 /* 0x1c0 */
2100 .long 0 /* 0x1c4 */
2101 .long 0 /* 0x1c8 */
2102 .long 0 /* 0x1cc */
2103 .long 0 /* 0x1d0 */
2104 .long 0 /* 0x1d4 */
2105 .long 0 /* 0x1d8 */
2106 .long 0 /* 0x1dc */
2107 .long 0 /* 0x1e0 */
2108 .long 0 /* 0x1e4 */
2109 .long 0 /* 0x1e8 */
2110 .long 0 /* 0x1ec */
2111 .long 0 /* 0x1f0 */
2112 .long 0 /* 0x1f4 */
2113 .long 0 /* 0x1f8 */
2114 .long 0 /* 0x1fc */
2115 .long 0 /* 0x200 */
2116 .long 0 /* 0x204 */
2117 .long 0 /* 0x208 */
2118 .long 0 /* 0x20c */
2119 .long 0 /* 0x210 */
2120 .long 0 /* 0x214 */
2121 .long 0 /* 0x218 */
2122 .long 0 /* 0x21c */
2123 .long 0 /* 0x220 */
2124 .long 0 /* 0x224 */
2125 .long 0 /* 0x228 */
2126 .long 0 /* 0x22c */
2127 .long 0 /* 0x230 */
2128 .long 0 /* 0x234 */
2129 .long 0 /* 0x238 */
2130 .long 0 /* 0x23c */
2131 .long 0 /* 0x240 */
2132 .long 0 /* 0x244 */
2133 .long 0 /* 0x248 */
2134 .long 0 /* 0x24c */
2135 .long 0 /* 0x250 */
2136 .long 0 /* 0x254 */
2137 .long 0 /* 0x258 */
2138 .long 0 /* 0x25c */
2139 .long 0 /* 0x260 */
2140 .long 0 /* 0x264 */
2141 .long 0 /* 0x268 */
2142 .long 0 /* 0x26c */
2143 .long 0 /* 0x270 */
2144 .long 0 /* 0x274 */
2145 .long 0 /* 0x278 */
2146 .long 0 /* 0x27c */
2147 .long 0 /* 0x280 */
2148 .long 0 /* 0x284 */
2149 .long 0 /* 0x288 */
2150 .long 0 /* 0x28c */
2151 .long 0 /* 0x290 */
2152 .long 0 /* 0x294 */
2153 .long 0 /* 0x298 */
2154 .long 0 /* 0x29c */
2155 .long 0 /* 0x2a0 */
2156 .long 0 /* 0x2a4 */
2157 .long 0 /* 0x2a8 */
2158 .long 0 /* 0x2ac */
2159 .long 0 /* 0x2b0 */
2160 .long 0 /* 0x2b4 */
2161 .long 0 /* 0x2b8 */
2162 .long 0 /* 0x2bc */
2163 .long 0 /* 0x2c0 */
2164 .long 0 /* 0x2c4 */
2165 .long 0 /* 0x2c8 */
2166 .long 0 /* 0x2cc */
2167 .long 0 /* 0x2d0 */
2168 .long 0 /* 0x2d4 */
2169 .long 0 /* 0x2d8 */
2170 .long 0 /* 0x2dc */
2171 .long 0 /* 0x2e0 */
2172 .long 0 /* 0x2e4 */
2173 .long 0 /* 0x2e8 */
2174 .long 0 /* 0x2ec */
2175 .long 0 /* 0x2f0 */
2176 .long 0 /* 0x2f4 */
2177 .long 0 /* 0x2f8 */
2178 .long 0 /* 0x2fc */
2179 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002180 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002181hcall_real_table_end:
2182
Paul Mackerras8563bf52014-01-08 21:25:29 +11002183_GLOBAL(kvmppc_h_set_xdabr)
2184 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2185 beq 6f
2186 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2187 andc. r0, r5, r0
2188 beq 3f
21896: li r3, H_PARAMETER
2190 blr
2191
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002192_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002193 li r5, DABRX_USER | DABRX_KERNEL
21943:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002195BEGIN_FTR_SECTION
2196 b 2f
2197END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002198 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002199 stw r5, VCPU_DABRX(r3)
2200 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002201 /* Work around P7 bug where DABR can get corrupted on mtspr */
22021: mtspr SPRN_DABR,r4
2203 mfspr r5, SPRN_DABR
2204 cmpd r4, r5
2205 bne 1b
2206 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002207 li r3,0
2208 blr
2209
Paul Mackerras8563bf52014-01-08 21:25:29 +11002210 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
22112: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002212 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002213 clrrdi r4, r4, 3
2214 std r4, VCPU_DAWR(r3)
2215 std r5, VCPU_DAWRX(r3)
2216 mtspr SPRN_DAWR, r4
2217 mtspr SPRN_DAWRX, r5
2218 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002219 blr
2220
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002221_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002222 ori r11,r11,MSR_EE
2223 std r11,VCPU_MSR(r3)
2224 li r0,1
2225 stb r0,VCPU_CEDED(r3)
2226 sync /* order setting ceded vs. testing prodded */
2227 lbz r5,VCPU_PRODDED(r3)
2228 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002229 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002230 li r12,0 /* set trap to 0 to say hcall is handled */
2231 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002232 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002233 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002234
2235 /*
2236 * Set our bit in the bitmask of napping threads unless all the
2237 * other threads are already napping, in which case we send this
2238 * up to the host.
2239 */
2240 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002241 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002242 lwz r8,VCORE_ENTRY_EXIT(r5)
2243 clrldi r8,r8,56
2244 li r0,1
2245 sld r0,r0,r6
2246 addi r6,r5,VCORE_NAPPING_THREADS
224731: lwarx r4,0,r6
2248 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002249 cmpw r4,r8
2250 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002251 stwcx. r4,0,r6
2252 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002253 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002254 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002255 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002256 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002257 lwz r7,VCORE_ENTRY_EXIT(r5)
2258 cmpwi r7,0x100
2259 bge 33f /* another thread already exiting */
2260
2261/*
2262 * Although not specifically required by the architecture, POWER7
2263 * preserves the following registers in nap mode, even if an SMT mode
2264 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2265 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2266 */
2267 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002268 std r14, VCPU_GPR(R14)(r3)
2269 std r15, VCPU_GPR(R15)(r3)
2270 std r16, VCPU_GPR(R16)(r3)
2271 std r17, VCPU_GPR(R17)(r3)
2272 std r18, VCPU_GPR(R18)(r3)
2273 std r19, VCPU_GPR(R19)(r3)
2274 std r20, VCPU_GPR(R20)(r3)
2275 std r21, VCPU_GPR(R21)(r3)
2276 std r22, VCPU_GPR(R22)(r3)
2277 std r23, VCPU_GPR(R23)(r3)
2278 std r24, VCPU_GPR(R24)(r3)
2279 std r25, VCPU_GPR(R25)(r3)
2280 std r26, VCPU_GPR(R26)(r3)
2281 std r27, VCPU_GPR(R27)(r3)
2282 std r28, VCPU_GPR(R28)(r3)
2283 std r29, VCPU_GPR(R29)(r3)
2284 std r30, VCPU_GPR(R30)(r3)
2285 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002286
2287 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002288 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002289
Paul Mackerras93d17392016-06-22 15:52:55 +10002290#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2291BEGIN_FTR_SECTION
2292 ld r9, HSTATE_KVM_VCPU(r13)
2293 bl kvmppc_save_tm
2294END_FTR_SECTION_IFSET(CPU_FTR_TM)
2295#endif
2296
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002297 /*
2298 * Set DEC to the smaller of DEC and HDEC, so that we wake
2299 * no later than the end of our timeslice (HDEC interrupts
2300 * don't wake us from nap).
2301 */
2302 mfspr r3, SPRN_DEC
2303 mfspr r4, SPRN_HDEC
2304 mftb r5
2305 cmpw r3, r4
2306 ble 67f
2307 mtspr SPRN_DEC, r4
230867:
2309 /* save expiry time of guest decrementer */
2310 extsw r3, r3
2311 add r3, r3, r5
2312 ld r4, HSTATE_KVM_VCPU(r13)
2313 ld r5, HSTATE_KVM_VCORE(r13)
2314 ld r6, VCORE_TB_OFFSET(r5)
2315 subf r3, r6, r3 /* convert to host TB value */
2316 std r3, VCPU_DEC_EXPIRES(r4)
2317
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002318#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2319 ld r4, HSTATE_KVM_VCPU(r13)
2320 addi r3, r4, VCPU_TB_CEDE
2321 bl kvmhv_accumulate_time
2322#endif
2323
Paul Mackerrasccc07772015-03-28 14:21:07 +11002324 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2325
Paul Mackerras19ccb762011-07-23 17:42:46 +10002326 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002327 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002328 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002329 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002330 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002331 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002332kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002333 mfspr r0, SPRN_CTRLF
2334 clrrdi r0, r0, 1
2335 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302336
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002337 li r0,1
2338 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002339 mfspr r5,SPRN_LPCR
2340 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002341BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002342 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002343 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002344END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002345
2346kvm_nap_sequence: /* desired LPCR value in r5 */
2347BEGIN_FTR_SECTION
2348 /*
2349 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2350 * enable state loss = 1 (allow SMT mode switch)
2351 * requested level = 0 (just stop dispatching)
2352 */
2353 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2354 mtspr SPRN_PSSCR, r3
2355 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2356 li r4, LPCR_PECE_HVEE@higher
2357 sldi r4, r4, 32
2358 or r5, r5, r4
2359END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002360 mtspr SPRN_LPCR,r5
2361 isync
2362 li r0, 0
2363 std r0, HSTATE_SCRATCH0(r13)
2364 ptesync
2365 ld r0, HSTATE_SCRATCH0(r13)
23661: cmpd r0, r0
2367 bne 1b
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002368BEGIN_FTR_SECTION
Paul Mackerras19ccb762011-07-23 17:42:46 +10002369 nap
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002370FTR_SECTION_ELSE
2371 PPC_STOP
2372ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002373 b .
2374
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100237533: mr r4, r3
2376 li r3, 0
2377 li r12, 0
2378 b 34f
2379
Paul Mackerras19ccb762011-07-23 17:42:46 +10002380kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002381 /* get vcpu pointer */
2382 ld r4, HSTATE_KVM_VCPU(r13)
2383
Paul Mackerras19ccb762011-07-23 17:42:46 +10002384 /* Woken by external or decrementer interrupt */
2385 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002386
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002387#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2388 addi r3, r4, VCPU_TB_RMINTR
2389 bl kvmhv_accumulate_time
2390#endif
2391
Paul Mackerras93d17392016-06-22 15:52:55 +10002392#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2393BEGIN_FTR_SECTION
2394 bl kvmppc_restore_tm
2395END_FTR_SECTION_IFSET(CPU_FTR_TM)
2396#endif
2397
Paul Mackerras19ccb762011-07-23 17:42:46 +10002398 /* load up FP state */
2399 bl kvmppc_load_fp
2400
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002401 /* Restore guest decrementer */
2402 ld r3, VCPU_DEC_EXPIRES(r4)
2403 ld r5, HSTATE_KVM_VCORE(r13)
2404 ld r6, VCORE_TB_OFFSET(r5)
2405 add r3, r3, r6 /* convert host TB to guest TB value */
2406 mftb r7
2407 subf r3, r7, r3
2408 mtspr SPRN_DEC, r3
2409
Paul Mackerras19ccb762011-07-23 17:42:46 +10002410 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002411 ld r14, VCPU_GPR(R14)(r4)
2412 ld r15, VCPU_GPR(R15)(r4)
2413 ld r16, VCPU_GPR(R16)(r4)
2414 ld r17, VCPU_GPR(R17)(r4)
2415 ld r18, VCPU_GPR(R18)(r4)
2416 ld r19, VCPU_GPR(R19)(r4)
2417 ld r20, VCPU_GPR(R20)(r4)
2418 ld r21, VCPU_GPR(R21)(r4)
2419 ld r22, VCPU_GPR(R22)(r4)
2420 ld r23, VCPU_GPR(R23)(r4)
2421 ld r24, VCPU_GPR(R24)(r4)
2422 ld r25, VCPU_GPR(R25)(r4)
2423 ld r26, VCPU_GPR(R26)(r4)
2424 ld r27, VCPU_GPR(R27)(r4)
2425 ld r28, VCPU_GPR(R28)(r4)
2426 ld r29, VCPU_GPR(R29)(r4)
2427 ld r30, VCPU_GPR(R30)(r4)
2428 ld r31, VCPU_GPR(R31)(r4)
Suresh Warrier37f55d32016-08-19 15:35:46 +10002429
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002430 /* Check the wake reason in SRR1 to see why we got here */
2431 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002432
Suresh Warrier37f55d32016-08-19 15:35:46 +10002433 /*
2434 * Restore volatile registers since we could have called a
2435 * C routine in kvmppc_check_wake_reason
2436 * r4 = VCPU
2437 * r3 tells us whether we need to return to host or not
2438 * WARNING: it gets checked further down:
2439 * should not modify r3 until this check is done.
2440 */
2441 ld r4, HSTATE_KVM_VCPU(r13)
2442
Paul Mackerras19ccb762011-07-23 17:42:46 +10002443 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100244434: ld r5,HSTATE_KVM_VCORE(r13)
2445 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002446 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002447 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002448 addi r6,r5,VCORE_NAPPING_THREADS
244932: lwarx r7,0,r6
2450 andc r7,r7,r0
2451 stwcx. r7,0,r6
2452 bne 32b
2453 li r0,0
2454 stb r0,HSTATE_NAPPING(r13)
2455
Suresh Warrier37f55d32016-08-19 15:35:46 +10002456 /* See if the wake reason saved in r3 means we need to exit */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002457 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002458 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002459 cmpdi r3, 0
2460 bgt guest_exit_cont
Paul Mackerras4619ac82013-04-17 20:31:41 +00002461
Paul Mackerras19ccb762011-07-23 17:42:46 +10002462 /* see if any other thread is already exiting */
2463 lwz r0,VCORE_ENTRY_EXIT(r5)
2464 cmpwi r0,0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002465 bge guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002466
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002467 b kvmppc_cede_reentry /* if not go back to guest */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002468
2469 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002470kvm_cede_prodded:
2471 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002472 stb r0,VCPU_PRODDED(r3)
2473 sync /* order testing prodded vs. clearing ceded */
2474 stb r0,VCPU_CEDED(r3)
2475 li r3,H_SUCCESS
2476 blr
2477
2478 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002479kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002480 ld r9, HSTATE_KVM_VCPU(r13)
2481 b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002482
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002483 /* Try to handle a machine check in real mode */
2484machine_check_realmode:
2485 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002486 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002487 nop
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002488 ld r9, HSTATE_KVM_VCPU(r13)
2489 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302490 /*
2491 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2492 * machine check interrupt (set HSRR0 to 0x200). And for handled
2493 * errors (no-fatal), just go back to guest execution with current
2494 * HSRR0 instead of exiting guest. This new approach will inject
2495 * machine check to guest for fatal error causing guest to crash.
2496 *
2497 * The old code used to return to host for unhandled errors which
2498 * was causing guest to hang with soft lockups inside guest and
2499 * makes it difficult to recover guest instance.
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302500 *
2501 * if we receive machine check with MSR(RI=0) then deliver it to
2502 * guest as machine check causing guest to crash.
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302503 */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302504 ld r11, VCPU_MSR(r9)
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11002505 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2506 bne mc_cont /* if so, exit to host */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302507 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2508 beq 1f /* Deliver a machine check to guest */
2509 ld r10, VCPU_PC(r9)
2510 cmpdi r3, 0 /* Did we handle MCE ? */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302511 bne 2f /* Continue guest execution. */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002512 /* If not, deliver a machine check. SRR0/1 are already set */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +053025131: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
Michael Neulinge4e38122014-03-25 10:47:02 +11002514 bl kvmppc_msr_interrupt
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +053025152: b fast_interrupt_c_return
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002516
Paul Mackerrasde56a942011-06-29 00:21:34 +00002517/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002518 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002519 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002520 * 0 if nothing needs to be done
2521 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002522 * -1 if there was a guest wakeup (IPI or msgsnd)
Suresh Warriere3c13e52016-08-19 15:35:51 +10002523 * -2 if we handled a PCI passthrough interrupt (returned by
2524 * kvmppc_read_intr only)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002525 *
2526 * Also sets r12 to the interrupt vector for any interrupt that needs
2527 * to be handled now by the host (0x500 for external interrupt), or zero.
Suresh Warrier37f55d32016-08-19 15:35:46 +10002528 * Modifies all volatile registers (since it may call a C function).
2529 * This routine calls kvmppc_read_intr, a C function, if an external
2530 * interrupt is pending.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002531 */
2532kvmppc_check_wake_reason:
2533 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002534BEGIN_FTR_SECTION
2535 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2536FTR_SECTION_ELSE
2537 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2538ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2539 cmpwi r6, 8 /* was it an external interrupt? */
Suresh Warrier37f55d32016-08-19 15:35:46 +10002540 beq 7f /* if so, see what it was */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002541 li r3, 0
2542 li r12, 0
2543 cmpwi r6, 6 /* was it the decrementer? */
2544 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002545BEGIN_FTR_SECTION
2546 cmpwi r6, 5 /* privileged doorbell? */
2547 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002548 cmpwi r6, 3 /* hypervisor doorbell? */
2549 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002550END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302551 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2552 beq 4f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002553 li r3, 1 /* anything else, return 1 */
25540: blr
2555
Paul Mackerras5d00f662014-01-08 21:25:28 +11002556 /* hypervisor doorbell */
25573: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302558
2559 /*
2560 * Clear the doorbell as we will invoke the handler
2561 * explicitly in the guest exit path.
2562 */
2563 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2564 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11002565 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11002566 li r3, 1
Paul Mackerras66feed62015-03-28 14:21:12 +11002567 lbz r0, HSTATE_HOST_IPI(r13)
2568 cmpwi r0, 0
2569 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302570 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002571 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11002572 blr
2573
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302574 /* Woken up due to Hypervisor maintenance interrupt */
25754: li r12, BOOK3S_INTERRUPT_HMI
2576 li r3, 1
2577 blr
2578
Suresh Warrier37f55d32016-08-19 15:35:46 +10002579 /* external interrupt - create a stack frame so we can call C */
25807: mflr r0
2581 std r0, PPC_LR_STKOFF(r1)
2582 stdu r1, -PPC_MIN_STKFRM(r1)
2583 bl kvmppc_read_intr
2584 nop
2585 li r12, BOOK3S_INTERRUPT_EXTERNAL
Suresh Warrierf7af5202016-08-19 15:35:52 +10002586 cmpdi r3, 1
2587 ble 1f
2588
2589 /*
2590 * Return code of 2 means PCI passthrough interrupt, but
2591 * we need to return back to host to complete handling the
2592 * interrupt. Trap reason is expected in r12 by guest
2593 * exit code.
2594 */
2595 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
25961:
Suresh Warrier37f55d32016-08-19 15:35:46 +10002597 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2598 addi r1, r1, PPC_MIN_STKFRM
2599 mtlr r0
2600 blr
Paul Mackerrasde56a942011-06-29 00:21:34 +00002601
2602/*
2603 * Save away FP, VMX and VSX registers.
2604 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002605 * N.B. r30 and r31 are volatile across this function,
2606 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002607 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002608kvmppc_save_fp:
2609 mflr r30
2610 mr r31,r3
Paul Mackerras89436332012-03-02 01:38:23 +00002611 mfmsr r5
2612 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00002613#ifdef CONFIG_ALTIVEC
2614BEGIN_FTR_SECTION
2615 oris r8,r8,MSR_VEC@h
2616END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2617#endif
2618#ifdef CONFIG_VSX
2619BEGIN_FTR_SECTION
2620 oris r8,r8,MSR_VSX@h
2621END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2622#endif
2623 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002624 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002625 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002626#ifdef CONFIG_ALTIVEC
2627BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002628 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002629 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002630END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2631#endif
2632 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11002633 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11002634 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00002635 blr
2636
2637/*
2638 * Load up FP, VMX and VSX registers
2639 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002640 * N.B. r30 and r31 are volatile across this function,
2641 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002642 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002643kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11002644 mflr r30
2645 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00002646 mfmsr r9
2647 ori r8,r9,MSR_FP
2648#ifdef CONFIG_ALTIVEC
2649BEGIN_FTR_SECTION
2650 oris r8,r8,MSR_VEC@h
2651END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2652#endif
2653#ifdef CONFIG_VSX
2654BEGIN_FTR_SECTION
2655 oris r8,r8,MSR_VSX@h
2656END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2657#endif
2658 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002659 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002660 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002661#ifdef CONFIG_ALTIVEC
2662BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002663 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002664 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002665END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2666#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11002667 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002668 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11002669 mtlr r30
2670 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00002671 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10002672
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002673#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2674/*
2675 * Save transactional state and TM-related registers.
2676 * Called with r9 pointing to the vcpu struct.
2677 * This can modify all checkpointed registers, but
2678 * restores r1, r2 and r9 (vcpu pointer) before exit.
2679 */
2680kvmppc_save_tm:
2681 mflr r0
2682 std r0, PPC_LR_STKOFF(r1)
2683
2684 /* Turn on TM. */
2685 mfmsr r8
2686 li r0, 1
2687 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
2688 mtmsrd r8
2689
2690 ld r5, VCPU_MSR(r9)
2691 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2692 beq 1f /* TM not active in guest. */
2693
2694 std r1, HSTATE_HOST_R1(r13)
2695 li r3, TM_CAUSE_KVM_RESCHED
2696
2697 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2698 li r5, 0
2699 mtmsrd r5, 1
2700
2701 /* All GPRs are volatile at this point. */
2702 TRECLAIM(R3)
2703
2704 /* Temporarily store r13 and r9 so we have some regs to play with */
2705 SET_SCRATCH0(r13)
2706 GET_PACA(r13)
2707 std r9, PACATMSCRATCH(r13)
2708 ld r9, HSTATE_KVM_VCPU(r13)
2709
2710 /* Get a few more GPRs free. */
2711 std r29, VCPU_GPRS_TM(29)(r9)
2712 std r30, VCPU_GPRS_TM(30)(r9)
2713 std r31, VCPU_GPRS_TM(31)(r9)
2714
2715 /* Save away PPR and DSCR soon so don't run with user values. */
2716 mfspr r31, SPRN_PPR
2717 HMT_MEDIUM
2718 mfspr r30, SPRN_DSCR
2719 ld r29, HSTATE_DSCR(r13)
2720 mtspr SPRN_DSCR, r29
2721
2722 /* Save all but r9, r13 & r29-r31 */
2723 reg = 0
2724 .rept 29
2725 .if (reg != 9) && (reg != 13)
2726 std reg, VCPU_GPRS_TM(reg)(r9)
2727 .endif
2728 reg = reg + 1
2729 .endr
2730 /* ... now save r13 */
2731 GET_SCRATCH0(r4)
2732 std r4, VCPU_GPRS_TM(13)(r9)
2733 /* ... and save r9 */
2734 ld r4, PACATMSCRATCH(r13)
2735 std r4, VCPU_GPRS_TM(9)(r9)
2736
2737 /* Reload stack pointer and TOC. */
2738 ld r1, HSTATE_HOST_R1(r13)
2739 ld r2, PACATOC(r13)
2740
2741 /* Set MSR RI now we have r1 and r13 back. */
2742 li r5, MSR_RI
2743 mtmsrd r5, 1
2744
2745 /* Save away checkpinted SPRs. */
2746 std r31, VCPU_PPR_TM(r9)
2747 std r30, VCPU_DSCR_TM(r9)
2748 mflr r5
2749 mfcr r6
2750 mfctr r7
2751 mfspr r8, SPRN_AMR
2752 mfspr r10, SPRN_TAR
Paul Mackerras0d808df2016-11-07 15:09:58 +11002753 mfxer r11
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002754 std r5, VCPU_LR_TM(r9)
2755 stw r6, VCPU_CR_TM(r9)
2756 std r7, VCPU_CTR_TM(r9)
2757 std r8, VCPU_AMR_TM(r9)
2758 std r10, VCPU_TAR_TM(r9)
Paul Mackerras0d808df2016-11-07 15:09:58 +11002759 std r11, VCPU_XER_TM(r9)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002760
2761 /* Restore r12 as trap number. */
2762 lwz r12, VCPU_TRAP(r9)
2763
2764 /* Save FP/VSX. */
2765 addi r3, r9, VCPU_FPRS_TM
2766 bl store_fp_state
2767 addi r3, r9, VCPU_VRS_TM
2768 bl store_vr_state
2769 mfspr r6, SPRN_VRSAVE
2770 stw r6, VCPU_VRSAVE_TM(r9)
27711:
2772 /*
2773 * We need to save these SPRs after the treclaim so that the software
2774 * error code is recorded correctly in the TEXASR. Also the user may
2775 * change these outside of a transaction, so they must always be
2776 * context switched.
2777 */
2778 mfspr r5, SPRN_TFHAR
2779 mfspr r6, SPRN_TFIAR
2780 mfspr r7, SPRN_TEXASR
2781 std r5, VCPU_TFHAR(r9)
2782 std r6, VCPU_TFIAR(r9)
2783 std r7, VCPU_TEXASR(r9)
2784
2785 ld r0, PPC_LR_STKOFF(r1)
2786 mtlr r0
2787 blr
2788
2789/*
2790 * Restore transactional state and TM-related registers.
2791 * Called with r4 pointing to the vcpu struct.
2792 * This potentially modifies all checkpointed registers.
2793 * It restores r1, r2, r4 from the PACA.
2794 */
2795kvmppc_restore_tm:
2796 mflr r0
2797 std r0, PPC_LR_STKOFF(r1)
2798
2799 /* Turn on TM/FP/VSX/VMX so we can restore them. */
2800 mfmsr r5
2801 li r6, MSR_TM >> 32
2802 sldi r6, r6, 32
2803 or r5, r5, r6
2804 ori r5, r5, MSR_FP
2805 oris r5, r5, (MSR_VEC | MSR_VSX)@h
2806 mtmsrd r5
2807
2808 /*
2809 * The user may change these outside of a transaction, so they must
2810 * always be context switched.
2811 */
2812 ld r5, VCPU_TFHAR(r4)
2813 ld r6, VCPU_TFIAR(r4)
2814 ld r7, VCPU_TEXASR(r4)
2815 mtspr SPRN_TFHAR, r5
2816 mtspr SPRN_TFIAR, r6
2817 mtspr SPRN_TEXASR, r7
2818
2819 ld r5, VCPU_MSR(r4)
2820 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2821 beqlr /* TM not active in guest */
2822 std r1, HSTATE_HOST_R1(r13)
2823
2824 /* Make sure the failure summary is set, otherwise we'll program check
2825 * when we trechkpt. It's possible that this might have been not set
2826 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
2827 * host.
2828 */
2829 oris r7, r7, (TEXASR_FS)@h
2830 mtspr SPRN_TEXASR, r7
2831
2832 /*
2833 * We need to load up the checkpointed state for the guest.
2834 * We need to do this early as it will blow away any GPRs, VSRs and
2835 * some SPRs.
2836 */
2837
2838 mr r31, r4
2839 addi r3, r31, VCPU_FPRS_TM
2840 bl load_fp_state
2841 addi r3, r31, VCPU_VRS_TM
2842 bl load_vr_state
2843 mr r4, r31
2844 lwz r7, VCPU_VRSAVE_TM(r4)
2845 mtspr SPRN_VRSAVE, r7
2846
2847 ld r5, VCPU_LR_TM(r4)
2848 lwz r6, VCPU_CR_TM(r4)
2849 ld r7, VCPU_CTR_TM(r4)
2850 ld r8, VCPU_AMR_TM(r4)
2851 ld r9, VCPU_TAR_TM(r4)
Paul Mackerras0d808df2016-11-07 15:09:58 +11002852 ld r10, VCPU_XER_TM(r4)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002853 mtlr r5
2854 mtcr r6
2855 mtctr r7
2856 mtspr SPRN_AMR, r8
2857 mtspr SPRN_TAR, r9
Paul Mackerras0d808df2016-11-07 15:09:58 +11002858 mtxer r10
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002859
2860 /*
2861 * Load up PPR and DSCR values but don't put them in the actual SPRs
2862 * till the last moment to avoid running with userspace PPR and DSCR for
2863 * too long.
2864 */
2865 ld r29, VCPU_DSCR_TM(r4)
2866 ld r30, VCPU_PPR_TM(r4)
2867
2868 std r2, PACATMSCRATCH(r13) /* Save TOC */
2869
2870 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2871 li r5, 0
2872 mtmsrd r5, 1
2873
2874 /* Load GPRs r0-r28 */
2875 reg = 0
2876 .rept 29
2877 ld reg, VCPU_GPRS_TM(reg)(r31)
2878 reg = reg + 1
2879 .endr
2880
2881 mtspr SPRN_DSCR, r29
2882 mtspr SPRN_PPR, r30
2883
2884 /* Load final GPRs */
2885 ld 29, VCPU_GPRS_TM(29)(r31)
2886 ld 30, VCPU_GPRS_TM(30)(r31)
2887 ld 31, VCPU_GPRS_TM(31)(r31)
2888
2889 /* TM checkpointed state is now setup. All GPRs are now volatile. */
2890 TRECHKPT
2891
2892 /* Now let's get back the state we need. */
2893 HMT_MEDIUM
2894 GET_PACA(r13)
2895 ld r29, HSTATE_DSCR(r13)
2896 mtspr SPRN_DSCR, r29
2897 ld r4, HSTATE_KVM_VCPU(r13)
2898 ld r1, HSTATE_HOST_R1(r13)
2899 ld r2, PACATMSCRATCH(r13)
2900
2901 /* Set the MSR RI since we have our registers back. */
2902 li r5, MSR_RI
2903 mtmsrd r5, 1
2904
2905 ld r0, PPC_LR_STKOFF(r1)
2906 mtlr r0
2907 blr
2908#endif
2909
Paul Mackerras44a3add2013-10-04 21:45:04 +10002910/*
2911 * We come here if we get any exception or interrupt while we are
2912 * executing host real mode code while in guest MMU context.
2913 * For now just spin, but we should do something better.
2914 */
2915kvmppc_bad_host_intr:
2916 b .
Michael Neulinge4e38122014-03-25 10:47:02 +11002917
2918/*
2919 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
2920 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
2921 * r11 has the guest MSR value (in/out)
2922 * r9 has a vcpu pointer (in)
2923 * r0 is used as a scratch register
2924 */
2925kvmppc_msr_interrupt:
2926 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
2927 cmpwi r0, 2 /* Check if we are in transactional state.. */
2928 ld r11, VCPU_INTR_MSR(r9)
2929 bne 1f
2930 /* ... if transactional, change to suspended */
2931 li r0, 1
29321: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
2933 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10002934
2935/*
2936 * This works around a hardware bug on POWER8E processors, where
2937 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
2938 * performance monitor interrupt. Instead, when we need to have
2939 * an interrupt pending, we have to arrange for a counter to overflow.
2940 */
2941kvmppc_fix_pmao:
2942 li r3, 0
2943 mtspr SPRN_MMCR2, r3
2944 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
2945 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
2946 mtspr SPRN_MMCR0, r3
2947 lis r3, 0x7fff
2948 ori r3, r3, 0xffff
2949 mtspr SPRN_PMC6, r3
2950 isync
2951 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002952
2953#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2954/*
2955 * Start timing an activity
2956 * r3 = pointer to time accumulation struct, r4 = vcpu
2957 */
2958kvmhv_start_timing:
2959 ld r5, HSTATE_KVM_VCORE(r13)
2960 lbz r6, VCORE_IN_GUEST(r5)
2961 cmpwi r6, 0
2962 beq 5f /* if in guest, need to */
2963 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
29645: mftb r5
2965 subf r5, r6, r5
2966 std r3, VCPU_CUR_ACTIVITY(r4)
2967 std r5, VCPU_ACTIVITY_START(r4)
2968 blr
2969
2970/*
2971 * Accumulate time to one activity and start another.
2972 * r3 = pointer to new time accumulation struct, r4 = vcpu
2973 */
2974kvmhv_accumulate_time:
2975 ld r5, HSTATE_KVM_VCORE(r13)
2976 lbz r8, VCORE_IN_GUEST(r5)
2977 cmpwi r8, 0
2978 beq 4f /* if in guest, need to */
2979 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
29804: ld r5, VCPU_CUR_ACTIVITY(r4)
2981 ld r6, VCPU_ACTIVITY_START(r4)
2982 std r3, VCPU_CUR_ACTIVITY(r4)
2983 mftb r7
2984 subf r7, r8, r7
2985 std r7, VCPU_ACTIVITY_START(r4)
2986 cmpdi r5, 0
2987 beqlr
2988 subf r3, r6, r7
2989 ld r8, TAS_SEQCOUNT(r5)
2990 cmpdi r8, 0
2991 addi r8, r8, 1
2992 std r8, TAS_SEQCOUNT(r5)
2993 lwsync
2994 ld r7, TAS_TOTAL(r5)
2995 add r7, r7, r3
2996 std r7, TAS_TOTAL(r5)
2997 ld r6, TAS_MIN(r5)
2998 ld r7, TAS_MAX(r5)
2999 beq 3f
3000 cmpd r3, r6
3001 bge 1f
30023: std r3, TAS_MIN(r5)
30031: cmpd r3, r7
3004 ble 2f
3005 std r3, TAS_MAX(r5)
30062: lwsync
3007 addi r8, r8, 1
3008 std r8, TAS_SEQCOUNT(r5)
3009 blr
3010#endif