blob: cc2a86bf09887a4b2625c099183b2fc9e4af3428 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
12 *
13 * Derived from book3s_rmhandlers.S and other files, which are:
14 *
15 * Copyright SUSE Linux Products GmbH 2009
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#include <asm/ppc_asm.h>
21#include <asm/kvm_asm.h>
22#include <asm/reg.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100023#include <asm/mmu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000024#include <asm/page.h>
Paul Mackerras177339d2011-07-23 17:41:11 +100025#include <asm/ptrace.h>
26#include <asm/hvcall.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000027#include <asm/asm-offsets.h>
28#include <asm/exception-64s.h>
Paul Mackerrasf0888f72012-02-03 00:54:17 +000029#include <asm/kvm_book3s_asm.h>
Aneesh Kumar K.Vf64e8082016-03-01 12:59:20 +053030#include <asm/book3s/64/mmu-hash.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110031#include <asm/tm.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053032#include <asm/opal.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100033#include <asm/xive-regs.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110034
Paul Mackerras2f272462017-05-22 16:25:14 +100035/* Sign-extend HDEC if not on POWER9 */
36#define EXTEND_HDEC(reg) \
37BEGIN_FTR_SECTION; \
38 extsw reg, reg; \
39END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
40
Michael Neulinge4e38122014-03-25 10:47:02 +110041#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
Paul Mackerrasde56a942011-06-29 00:21:34 +000042
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110043/* Values in HSTATE_NAPPING(r13) */
44#define NAPPING_CEDE 1
45#define NAPPING_NOVCPU 2
46
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100047/* Stack frame offsets for kvmppc_hv_entry */
48#define SFS 144
49#define STACK_SLOT_TRAP (SFS-4)
50#define STACK_SLOT_TID (SFS-16)
51#define STACK_SLOT_PSSCR (SFS-24)
52#define STACK_SLOT_PID (SFS-32)
53#define STACK_SLOT_IAMR (SFS-40)
54#define STACK_SLOT_CIABR (SFS-48)
55#define STACK_SLOT_DAWR (SFS-56)
56#define STACK_SLOT_DAWRX (SFS-64)
57
Paul Mackerrasde56a942011-06-29 00:21:34 +000058/*
Paul Mackerras19ccb762011-07-23 17:42:46 +100059 * Call kvmppc_hv_entry in real mode.
Paul Mackerrasde56a942011-06-29 00:21:34 +000060 * Must be called with interrupts hard-disabled.
61 *
62 * Input Registers:
63 *
64 * LR = return address to continue at after eventually re-enabling MMU
65 */
Anton Blanchard6ed179b2014-06-12 18:16:53 +100066_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
Paul Mackerras218309b2013-09-06 13:23:44 +100067 mflr r0
68 std r0, PPC_LR_STKOFF(r1)
69 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +000070 mfmsr r10
Paul Mackerras218309b2013-09-06 13:23:44 +100071 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
Paul Mackerrasde56a942011-06-29 00:21:34 +000072 li r0,MSR_RI
73 andc r0,r10,r0
74 li r6,MSR_IR | MSR_DR
75 andc r6,r10,r6
76 mtmsrd r0,1 /* clear RI in MSR */
77 mtsrr0 r5
78 mtsrr1 r6
79 RFI
80
Paul Mackerras218309b2013-09-06 13:23:44 +100081kvmppc_call_hv_entry:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110082 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100083 bl kvmppc_hv_entry
84
85 /* Back from guest - restore host state and return to caller */
86
Michael Neulingeee7ff92014-01-08 21:25:19 +110087BEGIN_FTR_SECTION
Paul Mackerras218309b2013-09-06 13:23:44 +100088 /* Restore host DABR and DABRX */
89 ld r5,HSTATE_DABR(r13)
90 li r6,7
91 mtspr SPRN_DABR,r5
92 mtspr SPRN_DABRX,r6
Michael Neulingeee7ff92014-01-08 21:25:19 +110093END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +100094
95 /* Restore SPRG3 */
Scott Wood9d378df2014-03-10 17:29:38 -050096 ld r3,PACA_SPRG_VDSO(r13)
97 mtspr SPRN_SPRG_VDSO_WRITE,r3
Paul Mackerras218309b2013-09-06 13:23:44 +100098
Paul Mackerras218309b2013-09-06 13:23:44 +100099 /* Reload the host's PMU registers */
100 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
101 lbz r4, LPPACA_PMCINUSE(r3)
102 cmpwi r4, 0
103 beq 23f /* skip if not */
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000104BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000105 ld r3, HSTATE_MMCR0(r13)
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000106 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
107 cmpwi r4, MMCR0_PMAO
108 beql kvmppc_fix_pmao
109END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000110 lwz r3, HSTATE_PMC1(r13)
111 lwz r4, HSTATE_PMC2(r13)
112 lwz r5, HSTATE_PMC3(r13)
113 lwz r6, HSTATE_PMC4(r13)
114 lwz r8, HSTATE_PMC5(r13)
115 lwz r9, HSTATE_PMC6(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000116 mtspr SPRN_PMC1, r3
117 mtspr SPRN_PMC2, r4
118 mtspr SPRN_PMC3, r5
119 mtspr SPRN_PMC4, r6
120 mtspr SPRN_PMC5, r8
121 mtspr SPRN_PMC6, r9
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000122 ld r3, HSTATE_MMCR0(r13)
123 ld r4, HSTATE_MMCR1(r13)
124 ld r5, HSTATE_MMCRA(r13)
125 ld r6, HSTATE_SIAR(r13)
126 ld r7, HSTATE_SDAR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000127 mtspr SPRN_MMCR1, r4
128 mtspr SPRN_MMCRA, r5
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100129 mtspr SPRN_SIAR, r6
130 mtspr SPRN_SDAR, r7
131BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000132 ld r8, HSTATE_MMCR2(r13)
133 ld r9, HSTATE_SIER(r13)
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100134 mtspr SPRN_MMCR2, r8
135 mtspr SPRN_SIER, r9
136END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000137 mtspr SPRN_MMCR0, r3
138 isync
13923:
140
141 /*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100142 * Reload DEC. HDEC interrupts were disabled when
143 * we reloaded the host's LPCR value.
144 */
145 ld r3, HSTATE_DECEXP(r13)
146 mftb r4
147 subf r4, r4, r3
148 mtspr SPRN_DEC, r4
149
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000150 /* hwthread_req may have got set by cede or no vcpu, so clear it */
151 li r0, 0
152 stb r0, HSTATE_HWTHREAD_REQ(r13)
153
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100154 /*
Paul Mackerras218309b2013-09-06 13:23:44 +1000155 * For external and machine check interrupts, we need
156 * to call the Linux handler to process the interrupt.
157 * We do that by jumping to absolute address 0x500 for
158 * external interrupts, or the machine_check_fwnmi label
159 * for machine checks (since firmware might have patched
160 * the vector area at 0x200). The [h]rfid at the end of the
161 * handler will return to the book3s_hv_interrupts.S code.
162 * For other interrupts we do the rfid to get back
163 * to the book3s_hv_interrupts.S code here.
164 */
165 ld r8, 112+PPC_LR_STKOFF(r1)
166 addi r1, r1, 112
167 ld r7, HSTATE_HOST_MSR(r13)
168
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100169 /*
170 * If we came back from the guest via a relocation-on interrupt,
171 * we will be in virtual mode at this point, which makes it a
172 * little easier to get back to the caller.
173 */
174 mfmsr r0
175 andi. r0, r0, MSR_IR /* in real mode? */
176 bne .Lvirt_return
177
Paul Mackerras218309b2013-09-06 13:23:44 +1000178 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
179 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras218309b2013-09-06 13:23:44 +1000180 beq 11f
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +0530181 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
182 beq 15f /* Invoke the H_DOORBELL handler */
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530183 cmpwi cr2, r12, BOOK3S_INTERRUPT_HMI
184 beq cr2, 14f /* HMI check */
Paul Mackerras218309b2013-09-06 13:23:44 +1000185
186 /* RFI into the highmem handler, or branch to interrupt handler */
187 mfmsr r6
188 li r0, MSR_RI
189 andc r6, r6, r0
190 mtmsrd r6, 1 /* Clear RI in MSR */
191 mtsrr0 r8
192 mtsrr1 r7
Paul Mackerras218309b2013-09-06 13:23:44 +1000193 beq cr1, 13f /* machine check */
194 RFI
195
196 /* On POWER7, we have external interrupts set to use HSRR0/1 */
19711: mtspr SPRN_HSRR0, r8
198 mtspr SPRN_HSRR1, r7
199 ba 0x500
200
20113: b machine_check_fwnmi
202
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +053020314: mtspr SPRN_HSRR0, r8
204 mtspr SPRN_HSRR1, r7
205 b hmi_exception_after_realmode
206
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +053020715: mtspr SPRN_HSRR0, r8
208 mtspr SPRN_HSRR1, r7
209 ba 0xe80
210
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100211 /* Virtual-mode return - can't get here for HMI or machine check */
212.Lvirt_return:
213 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
214 beq 16f
215 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
216 beq 17f
217 andi. r0, r7, MSR_EE /* were interrupts hard-enabled? */
218 beq 18f
219 mtmsrd r7, 1 /* if so then re-enable them */
22018: mtlr r8
221 blr
222
22316: mtspr SPRN_HSRR0, r8 /* jump to reloc-on external vector */
224 mtspr SPRN_HSRR1, r7
225 b exc_virt_0x4500_hardware_interrupt
226
22717: mtspr SPRN_HSRR0, r8
228 mtspr SPRN_HSRR1, r7
229 b exc_virt_0x4e80_h_doorbell
230
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100231kvmppc_primary_no_guest:
232 /* We handle this much like a ceded vcpu */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100233 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
Paul Mackerras2f272462017-05-22 16:25:14 +1000234 /* HDEC may be larger than DEC for arch >= v3.00, but since the */
235 /* HDEC value came from DEC in the first place, it will fit */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100236 mfspr r3, SPRN_HDEC
237 mtspr SPRN_DEC, r3
Paul Mackerras6af27c82015-03-28 14:21:10 +1100238 /*
239 * Make sure the primary has finished the MMU switch.
240 * We should never get here on a secondary thread, but
241 * check it for robustness' sake.
242 */
243 ld r5, HSTATE_KVM_VCORE(r13)
24465: lbz r0, VCORE_IN_GUEST(r5)
245 cmpwi r0, 0
246 beq 65b
247 /* Set LPCR. */
248 ld r8,VCORE_LPCR(r5)
249 mtspr SPRN_LPCR,r8
250 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100251 /* set our bit in napping_threads */
252 ld r5, HSTATE_KVM_VCORE(r13)
253 lbz r7, HSTATE_PTID(r13)
254 li r0, 1
255 sld r0, r0, r7
256 addi r6, r5, VCORE_NAPPING_THREADS
2571: lwarx r3, 0, r6
258 or r3, r3, r0
259 stwcx. r3, 0, r6
260 bne 1b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100261 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100262 isync
263 li r12, 0
264 lwz r7, VCORE_ENTRY_EXIT(r5)
265 cmpwi r7, 0x100
266 bge kvm_novcpu_exit /* another thread already exiting */
267 li r3, NAPPING_NOVCPU
268 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100269
Paul Mackerrasccc07772015-03-28 14:21:07 +1100270 li r3, 0 /* Don't wake on privileged (OS) doorbell */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100271 b kvm_do_nap
272
Suresh Warrier37f55d32016-08-19 15:35:46 +1000273/*
274 * kvm_novcpu_wakeup
275 * Entered from kvm_start_guest if kvm_hstate.napping is set
276 * to NAPPING_NOVCPU
277 * r2 = kernel TOC
278 * r13 = paca
279 */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100280kvm_novcpu_wakeup:
281 ld r1, HSTATE_HOST_R1(r13)
282 ld r5, HSTATE_KVM_VCORE(r13)
283 li r0, 0
284 stb r0, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100285
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100286 /* check the wake reason */
287 bl kvmppc_check_wake_reason
Paul Mackerras6af27c82015-03-28 14:21:10 +1100288
Suresh Warrier37f55d32016-08-19 15:35:46 +1000289 /*
290 * Restore volatile registers since we could have called
291 * a C routine in kvmppc_check_wake_reason.
292 * r5 = VCORE
293 */
294 ld r5, HSTATE_KVM_VCORE(r13)
295
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100296 /* see if any other thread is already exiting */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100297 lwz r0, VCORE_ENTRY_EXIT(r5)
298 cmpwi r0, 0x100
299 bge kvm_novcpu_exit
300
301 /* clear our bit in napping_threads */
302 lbz r7, HSTATE_PTID(r13)
303 li r0, 1
304 sld r0, r0, r7
305 addi r6, r5, VCORE_NAPPING_THREADS
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11003064: lwarx r7, 0, r6
307 andc r7, r7, r0
308 stwcx. r7, 0, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100309 bne 4b
310
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100311 /* See if the wake reason means we need to exit */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100312 cmpdi r3, 0
313 bge kvm_novcpu_exit
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100314
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100315 /* See if our timeslice has expired (HDEC is negative) */
316 mfspr r0, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000317 EXTEND_HDEC(r0)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100318 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerras2f272462017-05-22 16:25:14 +1000319 cmpdi r0, 0
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100320 blt kvm_novcpu_exit
321
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100322 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
323 ld r4, HSTATE_KVM_VCPU(r13)
324 cmpdi r4, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100325 beq kvmppc_primary_no_guest
326
327#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
328 addi r3, r4, VCPU_TB_RMENTRY
329 bl kvmhv_start_timing
330#endif
331 b kvmppc_got_guest
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100332
333kvm_novcpu_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100334#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
335 ld r4, HSTATE_KVM_VCPU(r13)
336 cmpdi r4, 0
337 beq 13f
338 addi r3, r4, VCPU_TB_RMEXIT
339 bl kvmhv_accumulate_time
340#endif
Paul Mackerraseddb60f2015-03-28 14:21:11 +110034113: mr r3, r12
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000342 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerraseddb60f2015-03-28 14:21:11 +1100343 bl kvmhv_commence_exit
344 nop
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000345 lwz r12, STACK_SLOT_TRAP(r1)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100346 b kvmhv_switch_to_host
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100347
Paul Mackerras371fefd2011-06-29 00:23:08 +0000348/*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100349 * We come in here when wakened from nap mode.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000350 * Relocation is off and most register values are lost.
351 * r13 points to the PACA.
352 */
353 .globl kvm_start_guest
354kvm_start_guest:
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530355
356 /* Set runlatch bit the minute you wake up from nap */
Paul Mackerras1f09c3e2015-03-28 14:21:04 +1100357 mfspr r0, SPRN_CTRLF
358 ori r0, r0, 1
359 mtspr SPRN_CTRLT, r0
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530360
Paul Mackerras19ccb762011-07-23 17:42:46 +1000361 ld r2,PACATOC(r13)
362
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000363 li r0,KVM_HWTHREAD_IN_KVM
364 stb r0,HSTATE_HWTHREAD_STATE(r13)
365
366 /* NV GPR values from power7_idle() will no longer be valid */
367 li r0,1
368 stb r0,PACA_NAPSTATELOST(r13)
369
Paul Mackerras4619ac82013-04-17 20:31:41 +0000370 /* were we napping due to cede? */
371 lbz r0,HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100372 cmpwi r0,NAPPING_CEDE
373 beq kvm_end_cede
374 cmpwi r0,NAPPING_NOVCPU
375 beq kvm_novcpu_wakeup
376
377 ld r1,PACAEMERGSP(r13)
378 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras4619ac82013-04-17 20:31:41 +0000379
380 /*
381 * We weren't napping due to cede, so this must be a secondary
382 * thread being woken up to run a guest, or being woken up due
383 * to a stray IPI. (Or due to some machine check or hypervisor
384 * maintenance interrupt while the core is in KVM.)
385 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000386
387 /* Check the wake reason in SRR1 to see why we got here */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100388 bl kvmppc_check_wake_reason
Suresh Warrier37f55d32016-08-19 15:35:46 +1000389 /*
390 * kvmppc_check_wake_reason could invoke a C routine, but we
391 * have no volatile registers to restore when we return.
392 */
393
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100394 cmpdi r3, 0
395 bge kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000396
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000397 /* get vcore pointer, NULL if we have nothing to run */
398 ld r5,HSTATE_KVM_VCORE(r13)
399 cmpdi r5,0
400 /* if we have no vcore to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000401 beq kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000402
Paul Mackerras56548fc2014-12-03 14:48:40 +1100403kvm_secondary_got_guest:
404
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100405 /* Set HSTATE_DSCR(r13) to something sensible */
Anshuman Khandual1db36522015-05-21 12:13:03 +0530406 ld r6, PACA_DSCR_DEFAULT(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100407 std r6, HSTATE_DSCR(r13)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000408
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000409 /* On thread 0 of a subcore, set HDEC to max */
410 lbz r4, HSTATE_PTID(r13)
411 cmpwi r4, 0
412 bne 63f
Paul Mackerras2f272462017-05-22 16:25:14 +1000413 LOAD_REG_ADDR(r6, decrementer_max)
414 ld r6, 0(r6)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000415 mtspr SPRN_HDEC, r6
416 /* and set per-LPAR registers, if doing dynamic micro-threading */
417 ld r6, HSTATE_SPLIT_MODE(r13)
418 cmpdi r6, 0
419 beq 63f
420 ld r0, KVM_SPLIT_RPR(r6)
421 mtspr SPRN_RPR, r0
422 ld r0, KVM_SPLIT_PMMAR(r6)
423 mtspr SPRN_PMMAR, r0
424 ld r0, KVM_SPLIT_LDBAR(r6)
425 mtspr SPRN_LDBAR, r0
426 isync
42763:
428 /* Order load of vcpu after load of vcore */
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100429 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000430 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100431 bl kvmppc_hv_entry
Paul Mackerras218309b2013-09-06 13:23:44 +1000432
433 /* Back from the guest, go back to nap */
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000434 /* Clear our vcpu and vcore pointers so we don't come back in early */
Paul Mackerras218309b2013-09-06 13:23:44 +1000435 li r0, 0
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000436 std r0, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100437 /*
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000438 * Once we clear HSTATE_KVM_VCORE(r13), the code in
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100439 * kvmppc_run_core() is going to assume that all our vcpu
440 * state is visible in memory. This lwsync makes sure
441 * that that is true.
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100442 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000443 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000444 std r0, HSTATE_KVM_VCORE(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000445
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530446 /*
447 * All secondaries exiting guest will fall through this path.
448 * Before proceeding, just check for HMI interrupt and
449 * invoke opal hmi handler. By now we are sure that the
450 * primary thread on this core/subcore has already made partition
451 * switch/TB resync and we are good to call opal hmi handler.
452 */
453 cmpwi r12, BOOK3S_INTERRUPT_HMI
454 bne kvm_no_guest
455
456 li r3,0 /* NULL argument */
457 bl hmi_exception_realmode
Paul Mackerras56548fc2014-12-03 14:48:40 +1100458/*
459 * At this point we have finished executing in the guest.
460 * We need to wait for hwthread_req to become zero, since
461 * we may not turn on the MMU while hwthread_req is non-zero.
462 * While waiting we also need to check if we get given a vcpu to run.
463 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000464kvm_no_guest:
Paul Mackerras56548fc2014-12-03 14:48:40 +1100465 lbz r3, HSTATE_HWTHREAD_REQ(r13)
466 cmpwi r3, 0
467 bne 53f
468 HMT_MEDIUM
469 li r0, KVM_HWTHREAD_IN_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000470 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerras56548fc2014-12-03 14:48:40 +1100471 /* need to recheck hwthread_req after a barrier, to avoid race */
472 sync
473 lbz r3, HSTATE_HWTHREAD_REQ(r13)
474 cmpwi r3, 0
475 bne 54f
476/*
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530477 * We jump to pnv_wakeup_loss, which will return to the caller
Paul Mackerras56548fc2014-12-03 14:48:40 +1100478 * of power7_nap in the powernv cpu offline loop. The value we
479 * put in r3 becomes the return value for power7_nap.
480 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000481 li r3, LPCR_PECE0
482 mfspr r4, SPRN_LPCR
483 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
484 mtspr SPRN_LPCR, r4
Paul Mackerras56548fc2014-12-03 14:48:40 +1100485 li r3, 0
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530486 b pnv_wakeup_loss
Paul Mackerras56548fc2014-12-03 14:48:40 +1100487
48853: HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000489 ld r5, HSTATE_KVM_VCORE(r13)
490 cmpdi r5, 0
491 bne 60f
492 ld r3, HSTATE_SPLIT_MODE(r13)
493 cmpdi r3, 0
494 beq kvm_no_guest
495 lbz r0, KVM_SPLIT_DO_NAP(r3)
496 cmpwi r0, 0
Paul Mackerras56548fc2014-12-03 14:48:40 +1100497 beq kvm_no_guest
498 HMT_MEDIUM
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000499 b kvm_unsplit_nap
50060: HMT_MEDIUM
Paul Mackerras56548fc2014-12-03 14:48:40 +1100501 b kvm_secondary_got_guest
502
50354: li r0, KVM_HWTHREAD_IN_KVM
504 stb r0, HSTATE_HWTHREAD_STATE(r13)
505 b kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000506
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000507/*
508 * Here the primary thread is trying to return the core to
509 * whole-core mode, so we need to nap.
510 */
511kvm_unsplit_nap:
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530512 /*
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530513 * When secondaries are napping in kvm_unsplit_nap() with
514 * hwthread_req = 1, HMI goes ignored even though subcores are
515 * already exited the guest. Hence HMI keeps waking up secondaries
516 * from nap in a loop and secondaries always go back to nap since
517 * no vcore is assigned to them. This makes impossible for primary
518 * thread to get hold of secondary threads resulting into a soft
519 * lockup in KVM path.
520 *
521 * Let us check if HMI is pending and handle it before we go to nap.
522 */
523 cmpwi r12, BOOK3S_INTERRUPT_HMI
524 bne 55f
525 li r3, 0 /* NULL argument */
526 bl hmi_exception_realmode
52755:
528 /*
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530529 * Ensure that secondary doesn't nap when it has
530 * its vcore pointer set.
531 */
532 sync /* matches smp_mb() before setting split_info.do_nap */
533 ld r0, HSTATE_KVM_VCORE(r13)
534 cmpdi r0, 0
535 bne kvm_no_guest
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000536 /* clear any pending message */
537BEGIN_FTR_SECTION
538 lis r6, (PPC_DBELL_SERVER << (63-36))@h
539 PPC_MSGCLR(6)
540END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
541 /* Set kvm_split_mode.napped[tid] = 1 */
542 ld r3, HSTATE_SPLIT_MODE(r13)
543 li r0, 1
544 lhz r4, PACAPACAINDEX(r13)
545 clrldi r4, r4, 61 /* micro-threading => P8 => 8 threads/core */
546 addi r4, r4, KVM_SPLIT_NAPPED
547 stbx r0, r3, r4
548 /* Check the do_nap flag again after setting napped[] */
549 sync
550 lbz r0, KVM_SPLIT_DO_NAP(r3)
551 cmpwi r0, 0
552 beq 57f
553 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
Paul Mackerrasbf53c882016-11-18 14:34:07 +1100554 mfspr r5, SPRN_LPCR
555 rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
556 b kvm_nap_sequence
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000557
55857: li r0, 0
559 stbx r0, r3, r4
560 b kvm_no_guest
561
Paul Mackerras218309b2013-09-06 13:23:44 +1000562/******************************************************************************
563 * *
564 * Entry code *
565 * *
566 *****************************************************************************/
567
Paul Mackerrasde56a942011-06-29 00:21:34 +0000568.global kvmppc_hv_entry
569kvmppc_hv_entry:
570
571 /* Required state:
572 *
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100573 * R4 = vcpu pointer (or NULL)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000574 * MSR = ~IR|DR
575 * R13 = PACA
576 * R1 = host R1
Michael Neuling06a29e42014-08-19 14:59:30 +1000577 * R2 = TOC
Paul Mackerrasde56a942011-06-29 00:21:34 +0000578 * all other volatile GPRS = free
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100579 * Does not preserve non-volatile GPRs or CR fields
Paul Mackerrasde56a942011-06-29 00:21:34 +0000580 */
581 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000582 std r0, PPC_LR_STKOFF(r1)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000583 stdu r1, -SFS(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000584
Paul Mackerrasde56a942011-06-29 00:21:34 +0000585 /* Save R1 in the PACA */
586 std r1, HSTATE_HOST_R1(r13)
587
Paul Mackerras44a3add2013-10-04 21:45:04 +1000588 li r6, KVM_GUEST_MODE_HOST_HV
589 stb r6, HSTATE_IN_GUEST(r13)
590
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100591#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
592 /* Store initial timestamp */
593 cmpdi r4, 0
594 beq 1f
595 addi r3, r4, VCPU_TB_RMENTRY
596 bl kvmhv_start_timing
5971:
598#endif
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100599
600 /* Use cr7 as an indication of radix mode */
601 ld r5, HSTATE_KVM_VCORE(r13)
602 ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
603 lbz r0, KVM_RADIX(r9)
604 cmpwi cr7, r0, 0
605
606 /* Clear out SLB if hash */
607 bne cr7, 2f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000608 li r6,0
609 slbmte r6,r6
610 slbia
611 ptesync
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11006122:
Paul Mackerras9e368f22011-06-29 00:40:08 +0000613 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100614 * POWER7/POWER8 host -> guest partition switch code.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000615 * We don't have to lock against concurrent tlbies,
616 * but we do have to coordinate across hardware threads.
617 */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100618 /* Set bit in entry map iff exit map is zero. */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100619 li r7, 1
620 lbz r6, HSTATE_PTID(r13)
621 sld r7, r7, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100622 addi r8, r5, VCORE_ENTRY_EXIT
62321: lwarx r3, 0, r8
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100624 cmpwi r3, 0x100 /* any threads starting to exit? */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000625 bge secondary_too_late /* if so we're too late to the party */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100626 or r3, r3, r7
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100627 stwcx. r3, 0, r8
Paul Mackerras371fefd2011-06-29 00:23:08 +0000628 bne 21b
629
630 /* Primary thread switches to guest partition. */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000631 cmpwi r6,0
Paul Mackerras6af27c82015-03-28 14:21:10 +1100632 bne 10f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000633 lwz r7,KVM_LPID(r9)
Paul Mackerras7a840842016-11-16 22:25:20 +1100634BEGIN_FTR_SECTION
635 ld r6,KVM_SDR1(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000636 li r0,LPID_RSVD /* switch to reserved LPID */
637 mtspr SPRN_LPID,r0
638 ptesync
639 mtspr SPRN_SDR1,r6 /* switch to partition page table */
Paul Mackerras7a840842016-11-16 22:25:20 +1100640END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000641 mtspr SPRN_LPID,r7
642 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000643
644 /* See if we need to flush the TLB */
645 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100646BEGIN_FTR_SECTION
647 /*
648 * On POWER9, individual threads can come in here, but the
649 * TLB is shared between the 4 threads in a core, hence
650 * invalidating on one thread invalidates for all.
651 * Thus we make all 4 threads use the same bit here.
652 */
653 clrrdi r6,r6,2
654END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000655 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
656 srdi r6,r6,6 /* doubleword number */
657 sldi r6,r6,3 /* address offset */
658 add r6,r6,r9
659 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100660 li r8,1
661 sld r8,r8,r7
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000662 ld r7,0(r6)
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100663 and. r7,r7,r8
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000664 beq 22f
Paul Mackerrasca252052014-01-08 21:25:22 +1100665 /* Flush the TLB of any entries for this LPID */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100666 lwz r0,KVM_TLB_SETS(r9)
667 mtctr r0
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000668 li r7,0x800 /* IS field = 0b10 */
669 ptesync
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100670 li r0,0 /* RS for P9 version of tlbiel */
671 bne cr7, 29f
67228: tlbiel r7 /* On P9, rs=0, RIC=0, PRS=0, R=0 */
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000673 addi r7,r7,0x1000
674 bdnz 28b
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100675 b 30f
67629: PPC_TLBIEL(7,0,2,1,1) /* for radix, RIC=2, PRS=1, R=1 */
677 addi r7,r7,0x1000
678 bdnz 29b
67930: ptesync
68023: ldarx r7,0,r6 /* clear the bit after TLB flushed */
681 andc r7,r7,r8
682 stdcx. r7,0,r6
683 bne 23b
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000684
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000685 /* Add timebase offset onto timebase */
68622: ld r8,VCORE_TB_OFFSET(r5)
687 cmpdi r8,0
688 beq 37f
689 mftb r6 /* current host timebase */
690 add r8,r8,r6
691 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
692 mftb r7 /* check if lower 24 bits overflowed */
693 clrldi r6,r6,40
694 clrldi r7,r7,40
695 cmpld r7,r6
696 bge 37f
697 addis r8,r8,0x100 /* if so, increment upper 40 bits */
698 mtspr SPRN_TBU40,r8
699
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000700 /* Load guest PCR value to select appropriate compat mode */
70137: ld r7, VCORE_PCR(r5)
702 cmpdi r7, 0
703 beq 38f
704 mtspr SPRN_PCR, r7
70538:
Michael Neulingb005255e2014-01-08 21:25:21 +1100706
707BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000708 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +1100709 ld r8, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000710 ld r7, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +1100711 mtspr SPRN_DPDES, r8
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000712 mtspr SPRN_VTB, r7
Michael Neulingb005255e2014-01-08 21:25:21 +1100713END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
714
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530715 /* Mark the subcore state as inside guest */
716 bl kvmppc_subcore_enter_guest
717 nop
718 ld r5, HSTATE_KVM_VCORE(r13)
719 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000720 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000721 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000722
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100723 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110072410: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100725 beq kvmppc_primary_no_guest
726kvmppc_got_guest:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000727
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100728 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100729 lwz r5,VCPU_SLB_MAX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000730 cmpwi r5,0
731 beq 9f
732 mtctr r5
733 addi r6,r4,VCPU_SLB
7341: ld r8,VCPU_SLB_E(r6)
735 ld r9,VCPU_SLB_V(r6)
736 slbmte r9,r8
737 addi r6,r6,VCPU_SLB_SIZE
738 bdnz 1b
7399:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100740 /* Increment yield count if they have a VPA */
741 ld r3, VCPU_VPA(r4)
742 cmpdi r3, 0
743 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200744 li r6, LPPACA_YIELDCOUNT
745 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100746 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200747 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100748 li r6, 1
749 stb r6, VCPU_VPA_DIRTY(r4)
75025:
751
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100752 /* Save purr/spurr */
753 mfspr r5,SPRN_PURR
754 mfspr r6,SPRN_SPURR
755 std r5,HSTATE_PURR(r13)
756 std r6,HSTATE_SPURR(r13)
757 ld r7,VCPU_PURR(r4)
758 ld r8,VCPU_SPURR(r4)
759 mtspr SPRN_PURR,r7
760 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100761
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100762 /* Save host values of some registers */
763BEGIN_FTR_SECTION
764 mfspr r5, SPRN_TIDR
765 mfspr r6, SPRN_PSSCR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100766 mfspr r7, SPRN_PID
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +1000767 mfspr r8, SPRN_IAMR
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100768 std r5, STACK_SLOT_TID(r1)
769 std r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100770 std r7, STACK_SLOT_PID(r1)
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +1000771 std r8, STACK_SLOT_IAMR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100772END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000773BEGIN_FTR_SECTION
774 mfspr r5, SPRN_CIABR
775 mfspr r6, SPRN_DAWR
776 mfspr r7, SPRN_DAWRX
777 std r5, STACK_SLOT_CIABR(r1)
778 std r6, STACK_SLOT_DAWR(r1)
779 std r7, STACK_SLOT_DAWRX(r1)
780END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100781
Michael Neulingeee7ff92014-01-08 21:25:19 +1100782BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000783 /* Set partition DABR */
784 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100785 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000786 ld r6,VCPU_DABR(r4)
787 mtspr SPRN_DABRX,r5
788 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000789 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100790END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000791
Michael Neulinge4e38122014-03-25 10:47:02 +1100792#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
793BEGIN_FTR_SECTION
Paul Mackerrasf024ee02016-06-22 14:21:59 +1000794 bl kvmppc_restore_tm
795END_FTR_SECTION_IFSET(CPU_FTR_TM)
Michael Neulinge4e38122014-03-25 10:47:02 +1100796#endif
797
Paul Mackerrasde56a942011-06-29 00:21:34 +0000798 /* Load guest PMU registers */
799 /* R4 is live here (vcpu pointer) */
800 li r3, 1
801 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
802 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
803 isync
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000804BEGIN_FTR_SECTION
805 ld r3, VCPU_MMCR(r4)
806 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
807 cmpwi r5, MMCR0_PMAO
808 beql kvmppc_fix_pmao
809END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000810 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
811 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
812 lwz r6, VCPU_PMC + 8(r4)
813 lwz r7, VCPU_PMC + 12(r4)
814 lwz r8, VCPU_PMC + 16(r4)
815 lwz r9, VCPU_PMC + 20(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000816 mtspr SPRN_PMC1, r3
817 mtspr SPRN_PMC2, r5
818 mtspr SPRN_PMC3, r6
819 mtspr SPRN_PMC4, r7
820 mtspr SPRN_PMC5, r8
821 mtspr SPRN_PMC6, r9
Paul Mackerrasde56a942011-06-29 00:21:34 +0000822 ld r3, VCPU_MMCR(r4)
823 ld r5, VCPU_MMCR + 8(r4)
824 ld r6, VCPU_MMCR + 16(r4)
825 ld r7, VCPU_SIAR(r4)
826 ld r8, VCPU_SDAR(r4)
827 mtspr SPRN_MMCR1, r5
828 mtspr SPRN_MMCRA, r6
829 mtspr SPRN_SIAR, r7
830 mtspr SPRN_SDAR, r8
Michael Neulingb005255e2014-01-08 21:25:21 +1100831BEGIN_FTR_SECTION
832 ld r5, VCPU_MMCR + 24(r4)
833 ld r6, VCPU_SIER(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100834 mtspr SPRN_MMCR2, r5
835 mtspr SPRN_SIER, r6
836BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +1100837 lwz r7, VCPU_PMC + 24(r4)
838 lwz r8, VCPU_PMC + 28(r4)
839 ld r9, VCPU_MMCR + 32(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100840 mtspr SPRN_SPMC1, r7
841 mtspr SPRN_SPMC2, r8
842 mtspr SPRN_MMCRS, r9
Paul Mackerras83677f52016-11-16 22:33:27 +1100843END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +1100844END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000845 mtspr SPRN_MMCR0, r3
846 isync
847
848 /* Load up FP, VMX and VSX registers */
849 bl kvmppc_load_fp
850
851 ld r14, VCPU_GPR(R14)(r4)
852 ld r15, VCPU_GPR(R15)(r4)
853 ld r16, VCPU_GPR(R16)(r4)
854 ld r17, VCPU_GPR(R17)(r4)
855 ld r18, VCPU_GPR(R18)(r4)
856 ld r19, VCPU_GPR(R19)(r4)
857 ld r20, VCPU_GPR(R20)(r4)
858 ld r21, VCPU_GPR(R21)(r4)
859 ld r22, VCPU_GPR(R22)(r4)
860 ld r23, VCPU_GPR(R23)(r4)
861 ld r24, VCPU_GPR(R24)(r4)
862 ld r25, VCPU_GPR(R25)(r4)
863 ld r26, VCPU_GPR(R26)(r4)
864 ld r27, VCPU_GPR(R27)(r4)
865 ld r28, VCPU_GPR(R28)(r4)
866 ld r29, VCPU_GPR(R29)(r4)
867 ld r30, VCPU_GPR(R30)(r4)
868 ld r31, VCPU_GPR(R31)(r4)
869
Paul Mackerrasde56a942011-06-29 00:21:34 +0000870 /* Switch DSCR to guest value */
871 ld r5, VCPU_DSCR(r4)
872 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000873
Michael Neulingb005255e2014-01-08 21:25:21 +1100874BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100875 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100876 b 8f
877END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +1100878 /* Load up POWER8-specific registers */
879 ld r5, VCPU_IAMR(r4)
880 lwz r6, VCPU_PSPB(r4)
881 ld r7, VCPU_FSCR(r4)
882 mtspr SPRN_IAMR, r5
883 mtspr SPRN_PSPB, r6
884 mtspr SPRN_FSCR, r7
885 ld r5, VCPU_DAWR(r4)
886 ld r6, VCPU_DAWRX(r4)
887 ld r7, VCPU_CIABR(r4)
888 ld r8, VCPU_TAR(r4)
889 mtspr SPRN_DAWR, r5
890 mtspr SPRN_DAWRX, r6
891 mtspr SPRN_CIABR, r7
892 mtspr SPRN_TAR, r8
893 ld r5, VCPU_IC(r4)
Michael Neuling7b490412014-01-08 21:25:32 +1100894 ld r8, VCPU_EBBHR(r4)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000895 mtspr SPRN_IC, r5
Michael Neulingb005255e2014-01-08 21:25:21 +1100896 mtspr SPRN_EBBHR, r8
897 ld r5, VCPU_EBBRR(r4)
898 ld r6, VCPU_BESCR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100899 lwz r7, VCPU_GUEST_PID(r4)
900 ld r8, VCPU_WORT(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100901 mtspr SPRN_EBBRR, r5
902 mtspr SPRN_BESCR, r6
Michael Neulingb005255e2014-01-08 21:25:21 +1100903 mtspr SPRN_PID, r7
904 mtspr SPRN_WORT, r8
Paul Mackerras83677f52016-11-16 22:33:27 +1100905BEGIN_FTR_SECTION
Paul Mackerrasf11f6f72017-01-30 21:21:52 +1100906 PPC_INVALIDATE_ERAT
907END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
908BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100909 /* POWER8-only registers */
Paul Mackerras83677f52016-11-16 22:33:27 +1100910 ld r5, VCPU_TCSCR(r4)
911 ld r6, VCPU_ACOP(r4)
912 ld r7, VCPU_CSIGR(r4)
913 ld r8, VCPU_TACR(r4)
914 mtspr SPRN_TCSCR, r5
915 mtspr SPRN_ACOP, r6
916 mtspr SPRN_CSIGR, r7
917 mtspr SPRN_TACR, r8
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100918FTR_SECTION_ELSE
919 /* POWER9-only registers */
920 ld r5, VCPU_TID(r4)
921 ld r6, VCPU_PSSCR(r4)
922 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
923 mtspr SPRN_TIDR, r5
924 mtspr SPRN_PSSCR, r6
925ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11009268:
927
Paul Mackerrasde56a942011-06-29 00:21:34 +0000928 /*
929 * Set the decrementer to the guest decrementer.
930 */
931 ld r8,VCPU_DEC_EXPIRES(r4)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +1100932 /* r8 is a host timebase value here, convert to guest TB */
933 ld r5,HSTATE_KVM_VCORE(r13)
934 ld r6,VCORE_TB_OFFSET(r5)
935 add r8,r8,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000936 mftb r7
937 subf r3,r7,r8
938 mtspr SPRN_DEC,r3
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000939 std r3,VCPU_DEC(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000940
941 ld r5, VCPU_SPRG0(r4)
942 ld r6, VCPU_SPRG1(r4)
943 ld r7, VCPU_SPRG2(r4)
944 ld r8, VCPU_SPRG3(r4)
945 mtspr SPRN_SPRG0, r5
946 mtspr SPRN_SPRG1, r6
947 mtspr SPRN_SPRG2, r7
948 mtspr SPRN_SPRG3, r8
949
Paul Mackerrasde56a942011-06-29 00:21:34 +0000950 /* Load up DAR and DSISR */
951 ld r5, VCPU_DAR(r4)
952 lwz r6, VCPU_DSISR(r4)
953 mtspr SPRN_DAR, r5
954 mtspr SPRN_DSISR, r6
955
Paul Mackerrasde56a942011-06-29 00:21:34 +0000956 /* Restore AMR and UAMOR, set AMOR to all 1s */
957 ld r5,VCPU_AMR(r4)
958 ld r6,VCPU_UAMOR(r4)
959 li r7,-1
960 mtspr SPRN_AMR,r5
961 mtspr SPRN_UAMOR,r6
962 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000963
964 /* Restore state of CTRL run bit; assume 1 on entry */
965 lwz r5,VCPU_CTRL(r4)
966 andi. r5,r5,1
967 bne 4f
968 mfspr r6,SPRN_CTRLF
969 clrrdi r6,r6,1
970 mtspr SPRN_CTRLT,r6
9714:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100972 /* Secondary threads wait for primary to have done partition switch */
973 ld r5, HSTATE_KVM_VCORE(r13)
974 lbz r6, HSTATE_PTID(r13)
975 cmpwi r6, 0
976 beq 21f
977 lbz r0, VCORE_IN_GUEST(r5)
978 cmpwi r0, 0
979 bne 21f
980 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +100098120: lwz r3, VCORE_ENTRY_EXIT(r5)
982 cmpwi r3, 0x100
983 bge no_switch_exit
984 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100985 cmpwi r0, 0
986 beq 20b
987 HMT_MEDIUM
98821:
989 /* Set LPCR. */
990 ld r8,VCORE_LPCR(r5)
991 mtspr SPRN_LPCR,r8
992 isync
993
994 /* Check if HDEC expires soon */
995 mfspr r3, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000996 EXTEND_HDEC(r3)
997 cmpdi r3, 512 /* 1 microsecond */
Paul Mackerras6af27c82015-03-28 14:21:10 +1100998 blt hdec_soon
999
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001000#ifdef CONFIG_KVM_XICS
1001 /* We are entering the guest on that thread, push VCPU to XIVE */
1002 ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1003 cmpldi cr0, r10, r0
1004 beq no_xive
1005 ld r11, VCPU_XIVE_SAVED_STATE(r4)
1006 li r9, TM_QW1_OS
1007 stdcix r11,r9,r10
1008 eieio
1009 lwz r11, VCPU_XIVE_CAM_WORD(r4)
1010 li r9, TM_QW1_OS + TM_WORD2
1011 stwcix r11,r9,r10
1012 li r9, 1
1013 stw r9, VCPU_XIVE_PUSHED(r4)
1014no_xive:
1015#endif /* CONFIG_KVM_XICS */
1016
Suresh Warrier37f55d32016-08-19 15:35:46 +10001017deliver_guest_interrupt:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001018 ld r6, VCPU_CTR(r4)
Sam bobroffc63517c2015-05-27 09:56:57 +10001019 ld r7, VCPU_XER(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001020
1021 mtctr r6
1022 mtxer r7
1023
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001024kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerras4619ac82013-04-17 20:31:41 +00001025 ld r10, VCPU_PC(r4)
1026 ld r11, VCPU_MSR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001027 ld r6, VCPU_SRR0(r4)
1028 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001029 mtspr SPRN_SRR0, r6
1030 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001031
Paul Mackerras4619ac82013-04-17 20:31:41 +00001032 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001033 rldicl r11, r11, 63 - MSR_HV_LG, 1
1034 rotldi r11, r11, 1 + MSR_HV_LG
1035 ori r11, r11, MSR_ME
1036
Paul Mackerras19ccb762011-07-23 17:42:46 +10001037 /* Check if we can deliver an external or decrementer interrupt now */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001038 ld r0, VCPU_PENDING_EXC(r4)
1039 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
1040 cmpdi cr1, r0, 0
1041 andi. r8, r11, MSR_EE
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001042 mfspr r8, SPRN_LPCR
1043 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
1044 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
1045 mtspr SPRN_LPCR, r8
Paul Mackerras19ccb762011-07-23 17:42:46 +10001046 isync
Paul Mackerras19ccb762011-07-23 17:42:46 +10001047 beq 5f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001048 li r0, BOOK3S_INTERRUPT_EXTERNAL
1049 bne cr1, 12f
1050 mfspr r0, SPRN_DEC
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10001051BEGIN_FTR_SECTION
1052 /* On POWER9 check whether the guest has large decrementer enabled */
1053 andis. r8, r8, LPCR_LD@h
1054 bne 15f
1055END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1056 extsw r0, r0
105715: cmpdi r0, 0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001058 li r0, BOOK3S_INTERRUPT_DECREMENTER
1059 bge 5f
1060
106112: mtspr SPRN_SRR0, r10
Paul Mackerras19ccb762011-07-23 17:42:46 +10001062 mr r10,r0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001063 mtspr SPRN_SRR1, r11
Michael Neulinge4e38122014-03-25 10:47:02 +11001064 mr r9, r4
1065 bl kvmppc_msr_interrupt
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +110010665:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001067
Liu Ping Fan27025a62013-11-19 14:12:48 +08001068/*
1069 * Required state:
1070 * R4 = vcpu
1071 * R10: value for HSRR0
1072 * R11: value for HSRR1
1073 * R13 = PACA
1074 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001075fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001076 li r0,0
1077 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001078 mtspr SPRN_HSRR0,r10
1079 mtspr SPRN_HSRR1,r11
1080
1081 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001082 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +00001083 stb r9, HSTATE_IN_GUEST(r13)
1084
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001085#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1086 /* Accumulate timing */
1087 addi r3, r4, VCPU_TB_GUEST
1088 bl kvmhv_accumulate_time
1089#endif
1090
Paul Mackerrasde56a942011-06-29 00:21:34 +00001091 /* Enter guest */
1092
Paul Mackerras0acb9112013-02-04 18:10:51 +00001093BEGIN_FTR_SECTION
1094 ld r5, VCPU_CFAR(r4)
1095 mtspr SPRN_CFAR, r5
1096END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001097BEGIN_FTR_SECTION
1098 ld r0, VCPU_PPR(r4)
1099END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001100
Paul Mackerrasde56a942011-06-29 00:21:34 +00001101 ld r5, VCPU_LR(r4)
1102 lwz r6, VCPU_CR(r4)
1103 mtlr r5
1104 mtcr r6
1105
Michael Neulingc75df6f2012-06-25 13:33:10 +00001106 ld r1, VCPU_GPR(R1)(r4)
1107 ld r2, VCPU_GPR(R2)(r4)
1108 ld r3, VCPU_GPR(R3)(r4)
1109 ld r5, VCPU_GPR(R5)(r4)
1110 ld r6, VCPU_GPR(R6)(r4)
1111 ld r7, VCPU_GPR(R7)(r4)
1112 ld r8, VCPU_GPR(R8)(r4)
1113 ld r9, VCPU_GPR(R9)(r4)
1114 ld r10, VCPU_GPR(R10)(r4)
1115 ld r11, VCPU_GPR(R11)(r4)
1116 ld r12, VCPU_GPR(R12)(r4)
1117 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001118
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001119BEGIN_FTR_SECTION
1120 mtspr SPRN_PPR, r0
1121END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1122 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001123 ld r4, VCPU_GPR(R4)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001124
1125 hrfid
1126 b .
1127
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001128secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001129 li r12, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001130 cmpdi r4, 0
1131 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001132 stw r12, VCPU_TRAP(r4)
1133#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001134 addi r3, r4, VCPU_TB_RMEXIT
1135 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001136#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100113711: b kvmhv_switch_to_host
1138
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001139no_switch_exit:
1140 HMT_MEDIUM
1141 li r12, 0
1142 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001143hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001144 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000114512: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001146 mr r9, r4
1147#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001148 addi r3, r4, VCPU_TB_RMEXIT
1149 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001150#endif
Paul Mackerras6af27c82015-03-28 14:21:10 +11001151 b guest_exit_cont
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001152
Paul Mackerrasde56a942011-06-29 00:21:34 +00001153/******************************************************************************
1154 * *
1155 * Exit code *
1156 * *
1157 *****************************************************************************/
1158
1159/*
1160 * We come here from the first-level interrupt handlers.
1161 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301162 .globl kvmppc_interrupt_hv
1163kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001164 /*
1165 * Register contents:
Nicholas Piggind3918e72016-12-22 04:29:25 +10001166 * R12 = (guest CR << 32) | interrupt vector
Paul Mackerrasde56a942011-06-29 00:21:34 +00001167 * R13 = PACA
Nicholas Piggind3918e72016-12-22 04:29:25 +10001168 * guest R12 saved in shadow VCPU SCRATCH0
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001169 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
Paul Mackerrasde56a942011-06-29 00:21:34 +00001170 * guest R13 saved in SPRN_SCRATCH0
1171 */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001172 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001173 lbz r9, HSTATE_IN_GUEST(r13)
1174 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1175 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301176#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1177 cmpwi r9, KVM_GUEST_MODE_GUEST
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001178 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301179 beq kvmppc_interrupt_pr
1180#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001181 /* We're now back in the host but in guest MMU context */
1182 li r9, KVM_GUEST_MODE_HOST_HV
1183 stb r9, HSTATE_IN_GUEST(r13)
1184
Paul Mackerrasde56a942011-06-29 00:21:34 +00001185 ld r9, HSTATE_KVM_VCPU(r13)
1186
1187 /* Save registers */
1188
Michael Neulingc75df6f2012-06-25 13:33:10 +00001189 std r0, VCPU_GPR(R0)(r9)
1190 std r1, VCPU_GPR(R1)(r9)
1191 std r2, VCPU_GPR(R2)(r9)
1192 std r3, VCPU_GPR(R3)(r9)
1193 std r4, VCPU_GPR(R4)(r9)
1194 std r5, VCPU_GPR(R5)(r9)
1195 std r6, VCPU_GPR(R6)(r9)
1196 std r7, VCPU_GPR(R7)(r9)
1197 std r8, VCPU_GPR(R8)(r9)
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001198 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001199 std r0, VCPU_GPR(R9)(r9)
1200 std r10, VCPU_GPR(R10)(r9)
1201 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001202 ld r3, HSTATE_SCRATCH0(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001203 std r3, VCPU_GPR(R12)(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001204 /* CR is in the high half of r12 */
1205 srdi r4, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001206 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001207BEGIN_FTR_SECTION
1208 ld r3, HSTATE_CFAR(r13)
1209 std r3, VCPU_CFAR(r9)
1210END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001211BEGIN_FTR_SECTION
1212 ld r4, HSTATE_PPR(r13)
1213 std r4, VCPU_PPR(r9)
1214END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001215
1216 /* Restore R1/R2 so we can handle faults */
1217 ld r1, HSTATE_HOST_R1(r13)
1218 ld r2, PACATOC(r13)
1219
1220 mfspr r10, SPRN_SRR0
1221 mfspr r11, SPRN_SRR1
1222 std r10, VCPU_SRR0(r9)
1223 std r11, VCPU_SRR1(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001224 /* trap is in the low half of r12, clear CR from the high half */
1225 clrldi r12, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001226 andi. r0, r12, 2 /* need to read HSRR0/1? */
1227 beq 1f
1228 mfspr r10, SPRN_HSRR0
1229 mfspr r11, SPRN_HSRR1
1230 clrrdi r12, r12, 2
12311: std r10, VCPU_PC(r9)
1232 std r11, VCPU_MSR(r9)
1233
1234 GET_SCRATCH0(r3)
1235 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001236 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001237 std r4, VCPU_LR(r9)
1238
Paul Mackerrasde56a942011-06-29 00:21:34 +00001239 stw r12,VCPU_TRAP(r9)
1240
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001241#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1242 addi r3, r9, VCPU_TB_RMINTR
1243 mr r4, r9
1244 bl kvmhv_accumulate_time
1245 ld r5, VCPU_GPR(R5)(r9)
1246 ld r6, VCPU_GPR(R6)(r9)
1247 ld r7, VCPU_GPR(R7)(r9)
1248 ld r8, VCPU_GPR(R8)(r9)
1249#endif
1250
Paul Mackerras4a157d62014-12-03 13:30:39 +11001251 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001252 if this is an HEI (HV emulation interrupt, e40) */
1253 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001254 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001255 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1256 bne 11f
1257 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100125811: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001259
1260 /* these are volatile across C function calls */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001261#ifdef CONFIG_RELOCATABLE
1262 ld r3, HSTATE_SCRATCH1(r13)
1263 mtctr r3
1264#else
Paul Mackerras697d3892011-12-12 12:36:37 +00001265 mfctr r3
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001266#endif
Paul Mackerras697d3892011-12-12 12:36:37 +00001267 mfxer r4
1268 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001269 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001270
Paul Mackerras697d3892011-12-12 12:36:37 +00001271 /* If this is a page table miss then see if it's theirs or ours */
1272 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1273 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +00001274 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1275 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +00001276
Paul Mackerrasde56a942011-06-29 00:21:34 +00001277 /* See if this is a leftover HDEC interrupt */
1278 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1279 bne 2f
1280 mfspr r3,SPRN_HDEC
1281 cmpwi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001282 mr r4,r9
1283 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000012842:
Paul Mackerras697d3892011-12-12 12:36:37 +00001285 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001286 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1287 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001288
Paul Mackerras66feed62015-03-28 14:21:12 +11001289 /* Hypervisor doorbell - exit only if host IPI flag set */
1290 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1291 bne 3f
1292 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301293 cmpwi r0, 0
Paul Mackerras66feed62015-03-28 14:21:12 +11001294 beq 4f
1295 b guest_exit_cont
12963:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001297 /* External interrupt ? */
1298 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001299 bne+ guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001300
1301 /* External interrupt, first check for host_ipi. If this is
1302 * set, we know the host wants us out so let's do it now
1303 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001304 bl kvmppc_read_intr
Suresh Warrier37f55d32016-08-19 15:35:46 +10001305
1306 /*
1307 * Restore the active volatile registers after returning from
1308 * a C function.
1309 */
1310 ld r9, HSTATE_KVM_VCPU(r13)
1311 li r12, BOOK3S_INTERRUPT_EXTERNAL
1312
1313 /*
1314 * kvmppc_read_intr return codes:
1315 *
1316 * Exit to host (r3 > 0)
1317 * 1 An interrupt is pending that needs to be handled by the host
1318 * Exit guest and return to host by branching to guest_exit_cont
1319 *
Suresh Warrierf7af5202016-08-19 15:35:52 +10001320 * 2 Passthrough that needs completion in the host
1321 * Exit guest and return to host by branching to guest_exit_cont
1322 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1323 * to indicate to the host to complete handling the interrupt
1324 *
Suresh Warrier37f55d32016-08-19 15:35:46 +10001325 * Before returning to guest, we check if any CPU is heading out
1326 * to the host and if so, we head out also. If no CPUs are heading
1327 * check return values <= 0.
1328 *
1329 * Return to guest (r3 <= 0)
1330 * 0 No external interrupt is pending
1331 * -1 A guest wakeup IPI (which has now been cleared)
1332 * In either case, we return to guest to deliver any pending
1333 * guest interrupts.
Suresh Warriere3c13e52016-08-19 15:35:51 +10001334 *
1335 * -2 A PCI passthrough external interrupt was handled
1336 * (interrupt was delivered directly to guest)
1337 * Return to guest to deliver any pending guest interrupts.
Suresh Warrier37f55d32016-08-19 15:35:46 +10001338 */
1339
Suresh Warrierf7af5202016-08-19 15:35:52 +10001340 cmpdi r3, 1
1341 ble 1f
1342
1343 /* Return code = 2 */
1344 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1345 stw r12, VCPU_TRAP(r9)
1346 b guest_exit_cont
1347
13481: /* Return code <= 1 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001349 cmpdi r3, 0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001350 bgt guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001351
Suresh Warrier37f55d32016-08-19 15:35:46 +10001352 /* Return code <= 0 */
Paul Mackerras66feed62015-03-28 14:21:12 +110013534: ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001354 lwz r0, VCORE_ENTRY_EXIT(r5)
1355 cmpwi r0, 0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001356 mr r4, r9
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001357 blt deliver_guest_interrupt
Paul Mackerrasde56a942011-06-29 00:21:34 +00001358
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001359guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001360#ifdef CONFIG_KVM_XICS
1361 /* We are exiting, pull the VP from the XIVE */
1362 lwz r0, VCPU_XIVE_PUSHED(r9)
1363 cmpwi cr0, r0, 0
1364 beq 1f
1365 li r7, TM_SPC_PULL_OS_CTX
1366 li r6, TM_QW1_OS
1367 mfmsr r0
1368 andi. r0, r0, MSR_IR /* in real mode? */
1369 beq 2f
1370 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
1371 cmpldi cr0, r10, 0
1372 beq 1f
1373 /* First load to pull the context, we ignore the value */
1374 lwzx r11, r7, r10
1375 eieio
1376 /* Second load to recover the context state (Words 0 and 1) */
1377 ldx r11, r6, r10
1378 b 3f
13792: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1380 cmpldi cr0, r10, 0
1381 beq 1f
1382 /* First load to pull the context, we ignore the value */
1383 lwzcix r11, r7, r10
1384 eieio
1385 /* Second load to recover the context state (Words 0 and 1) */
1386 ldcix r11, r6, r10
13873: std r11, VCPU_XIVE_SAVED_STATE(r9)
1388 /* Fixup some of the state for the next load */
1389 li r10, 0
1390 li r0, 0xff
1391 stw r10, VCPU_XIVE_PUSHED(r9)
1392 stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1393 stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
13941:
1395#endif /* CONFIG_KVM_XICS */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001396 /* Save more register state */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001397 mfdar r6
1398 mfdsisr r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001399 std r6, VCPU_DAR(r9)
1400 stw r7, VCPU_DSISR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001401 /* don't overwrite fault_dar/fault_dsisr if HDSI */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001402 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
Paul Mackerras6af27c82015-03-28 14:21:10 +11001403 beq mc_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001404 std r6, VCPU_FAULT_DAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001405 stw r7, VCPU_FAULT_DSISR(r9)
1406
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001407 /* See if it is a machine check */
1408 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1409 beq machine_check_realmode
1410mc_cont:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001411#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1412 addi r3, r9, VCPU_TB_RMEXIT
1413 mr r4, r9
1414 bl kvmhv_accumulate_time
1415#endif
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001416
Gautham R. Shenoy7e022e72015-05-21 13:57:04 +05301417 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001418 /* Increment exit count, poke other threads to exit */
1419 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001420 nop
1421 ld r9, HSTATE_KVM_VCPU(r13)
1422 lwz r12, VCPU_TRAP(r9)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001423
Paul Mackerrasec257162015-06-24 21:18:03 +10001424 /* Stop others sending VCPU interrupts to this physical CPU */
1425 li r0, -1
1426 stw r0, VCPU_CPU(r9)
1427 stw r0, VCPU_THREAD_CPU(r9)
1428
Paul Mackerrasde56a942011-06-29 00:21:34 +00001429 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001430 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001431 stw r6,VCPU_CTRL(r9)
1432 andi. r0,r6,1
1433 bne 4f
1434 ori r6,r6,1
1435 mtspr SPRN_CTRLT,r6
14364:
1437 /* Read the guest SLB and save it away */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001438 ld r5, VCPU_KVM(r9)
1439 lbz r0, KVM_RADIX(r5)
1440 cmpwi r0, 0
1441 li r5, 0
1442 bne 3f /* for radix, save 0 entries */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001443 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1444 mtctr r0
1445 li r6,0
1446 addi r7,r9,VCPU_SLB
Paul Mackerrasde56a942011-06-29 00:21:34 +000014471: slbmfee r8,r6
1448 andis. r0,r8,SLB_ESID_V@h
1449 beq 2f
1450 add r8,r8,r6 /* put index in */
1451 slbmfev r3,r6
1452 std r8,VCPU_SLB_E(r7)
1453 std r3,VCPU_SLB_V(r7)
1454 addi r7,r7,VCPU_SLB_SIZE
1455 addi r5,r5,1
14562: addi r6,r6,1
1457 bdnz 1b
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110014583: stw r5,VCPU_SLB_MAX(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001459
1460 /*
1461 * Save the guest PURR/SPURR
1462 */
1463 mfspr r5,SPRN_PURR
1464 mfspr r6,SPRN_SPURR
1465 ld r7,VCPU_PURR(r9)
1466 ld r8,VCPU_SPURR(r9)
1467 std r5,VCPU_PURR(r9)
1468 std r6,VCPU_SPURR(r9)
1469 subf r5,r7,r5
1470 subf r6,r8,r6
1471
1472 /*
1473 * Restore host PURR/SPURR and add guest times
1474 * so that the time in the guest gets accounted.
1475 */
1476 ld r3,HSTATE_PURR(r13)
1477 ld r4,HSTATE_SPURR(r13)
1478 add r3,r3,r5
1479 add r4,r4,r6
1480 mtspr SPRN_PURR,r3
1481 mtspr SPRN_SPURR,r4
1482
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001483 /* Save DEC */
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10001484 ld r3, HSTATE_KVM_VCORE(r13)
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001485 mfspr r5,SPRN_DEC
1486 mftb r6
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10001487 /* On P9, if the guest has large decr enabled, don't sign extend */
1488BEGIN_FTR_SECTION
1489 ld r4, VCORE_LPCR(r3)
1490 andis. r4, r4, LPCR_LD@h
1491 bne 16f
1492END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001493 extsw r5,r5
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000149416: add r5,r5,r6
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001495 /* r5 is a guest timebase value here, convert to host TB */
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001496 ld r4,VCORE_TB_OFFSET(r3)
1497 subf r5,r4,r5
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001498 std r5,VCPU_DEC_EXPIRES(r9)
1499
Michael Neulingb005255e2014-01-08 21:25:21 +11001500BEGIN_FTR_SECTION
1501 b 8f
1502END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001503 /* Save POWER8-specific registers */
1504 mfspr r5, SPRN_IAMR
1505 mfspr r6, SPRN_PSPB
1506 mfspr r7, SPRN_FSCR
1507 std r5, VCPU_IAMR(r9)
1508 stw r6, VCPU_PSPB(r9)
1509 std r7, VCPU_FSCR(r9)
1510 mfspr r5, SPRN_IC
Michael Neulingb005255e2014-01-08 21:25:21 +11001511 mfspr r7, SPRN_TAR
1512 std r5, VCPU_IC(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001513 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001514 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001515 std r8, VCPU_EBBHR(r9)
1516 mfspr r5, SPRN_EBBRR
1517 mfspr r6, SPRN_BESCR
Michael Neulingb005255e2014-01-08 21:25:21 +11001518 mfspr r7, SPRN_PID
1519 mfspr r8, SPRN_WORT
Paul Mackerras83677f52016-11-16 22:33:27 +11001520 std r5, VCPU_EBBRR(r9)
1521 std r6, VCPU_BESCR(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001522 stw r7, VCPU_GUEST_PID(r9)
1523 std r8, VCPU_WORT(r9)
Paul Mackerras83677f52016-11-16 22:33:27 +11001524BEGIN_FTR_SECTION
1525 mfspr r5, SPRN_TCSCR
1526 mfspr r6, SPRN_ACOP
1527 mfspr r7, SPRN_CSIGR
1528 mfspr r8, SPRN_TACR
1529 std r5, VCPU_TCSCR(r9)
1530 std r6, VCPU_ACOP(r9)
1531 std r7, VCPU_CSIGR(r9)
1532 std r8, VCPU_TACR(r9)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001533FTR_SECTION_ELSE
1534 mfspr r5, SPRN_TIDR
1535 mfspr r6, SPRN_PSSCR
1536 std r5, VCPU_TID(r9)
1537 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1538 rotldi r6, r6, 60
1539 std r6, VCPU_PSSCR(r9)
1540ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001541 /*
1542 * Restore various registers to 0, where non-zero values
1543 * set by the guest could disrupt the host.
1544 */
1545 li r0, 0
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001546 mtspr SPRN_PSPB, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001547 mtspr SPRN_WORT, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001548BEGIN_FTR_SECTION
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001549 mtspr SPRN_IAMR, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001550 mtspr SPRN_TCSCR, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001551 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1552 li r0, 1
1553 sldi r0, r0, 31
1554 mtspr SPRN_MMCRS, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001555END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +110015568:
1557
Paul Mackerrasde56a942011-06-29 00:21:34 +00001558 /* Save and reset AMR and UAMOR before turning on the MMU */
1559 mfspr r5,SPRN_AMR
1560 mfspr r6,SPRN_UAMOR
1561 std r5,VCPU_AMR(r9)
1562 std r6,VCPU_UAMOR(r9)
1563 li r6,0
1564 mtspr SPRN_AMR,r6
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001565 mtspr SPRN_UAMOR, r6
Paul Mackerrasde56a942011-06-29 00:21:34 +00001566
Paul Mackerrasde56a942011-06-29 00:21:34 +00001567 /* Switch DSCR back to host value */
1568 mfspr r8, SPRN_DSCR
1569 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001570 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001571 mtspr SPRN_DSCR, r7
1572
1573 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001574 std r14, VCPU_GPR(R14)(r9)
1575 std r15, VCPU_GPR(R15)(r9)
1576 std r16, VCPU_GPR(R16)(r9)
1577 std r17, VCPU_GPR(R17)(r9)
1578 std r18, VCPU_GPR(R18)(r9)
1579 std r19, VCPU_GPR(R19)(r9)
1580 std r20, VCPU_GPR(R20)(r9)
1581 std r21, VCPU_GPR(R21)(r9)
1582 std r22, VCPU_GPR(R22)(r9)
1583 std r23, VCPU_GPR(R23)(r9)
1584 std r24, VCPU_GPR(R24)(r9)
1585 std r25, VCPU_GPR(R25)(r9)
1586 std r26, VCPU_GPR(R26)(r9)
1587 std r27, VCPU_GPR(R27)(r9)
1588 std r28, VCPU_GPR(R28)(r9)
1589 std r29, VCPU_GPR(R29)(r9)
1590 std r30, VCPU_GPR(R30)(r9)
1591 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001592
1593 /* Save SPRGs */
1594 mfspr r3, SPRN_SPRG0
1595 mfspr r4, SPRN_SPRG1
1596 mfspr r5, SPRN_SPRG2
1597 mfspr r6, SPRN_SPRG3
1598 std r3, VCPU_SPRG0(r9)
1599 std r4, VCPU_SPRG1(r9)
1600 std r5, VCPU_SPRG2(r9)
1601 std r6, VCPU_SPRG3(r9)
1602
Paul Mackerras89436332012-03-02 01:38:23 +00001603 /* save FP state */
1604 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001605 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001606
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001607#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1608BEGIN_FTR_SECTION
Paul Mackerrasf024ee02016-06-22 14:21:59 +10001609 bl kvmppc_save_tm
1610END_FTR_SECTION_IFSET(CPU_FTR_TM)
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001611#endif
1612
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001613 /* Increment yield count if they have a VPA */
1614 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1615 cmpdi r8, 0
1616 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001617 li r4, LPPACA_YIELDCOUNT
1618 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001619 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001620 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001621 li r3, 1
1622 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000162325:
1624 /* Save PMU registers if requested */
1625 /* r8 and cr0.eq are live here */
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001626BEGIN_FTR_SECTION
1627 /*
1628 * POWER8 seems to have a hardware bug where setting
1629 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1630 * when some counters are already negative doesn't seem
1631 * to cause a performance monitor alert (and hence interrupt).
1632 * The effect of this is that when saving the PMU state,
1633 * if there is no PMU alert pending when we read MMCR0
1634 * before freezing the counters, but one becomes pending
1635 * before we read the counters, we lose it.
1636 * To work around this, we need a way to freeze the counters
1637 * before reading MMCR0. Normally, freezing the counters
1638 * is done by writing MMCR0 (to set MMCR0[FC]) which
1639 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1640 * we can also freeze the counters using MMCR2, by writing
1641 * 1s to all the counter freeze condition bits (there are
1642 * 9 bits each for 6 counters).
1643 */
1644 li r3, -1 /* set all freeze bits */
1645 clrrdi r3, r3, 10
1646 mfspr r10, SPRN_MMCR2
1647 mtspr SPRN_MMCR2, r3
1648 isync
1649END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001650 li r3, 1
1651 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1652 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1653 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001654 mfspr r6, SPRN_MMCRA
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001655 /* Clear MMCRA in order to disable SDAR updates */
Paul Mackerras89436332012-03-02 01:38:23 +00001656 li r7, 0
1657 mtspr SPRN_MMCRA, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001658 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001659 beq 21f /* if no VPA, save PMU stuff anyway */
1660 lbz r7, LPPACA_PMCINUSE(r8)
1661 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1662 bne 21f
1663 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1664 b 22f
166521: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001666 mfspr r7, SPRN_SIAR
1667 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001668 std r4, VCPU_MMCR(r9)
1669 std r5, VCPU_MMCR + 8(r9)
1670 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001671BEGIN_FTR_SECTION
1672 std r10, VCPU_MMCR + 24(r9)
1673END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras14941782013-09-06 13:11:18 +10001674 std r7, VCPU_SIAR(r9)
1675 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001676 mfspr r3, SPRN_PMC1
1677 mfspr r4, SPRN_PMC2
1678 mfspr r5, SPRN_PMC3
1679 mfspr r6, SPRN_PMC4
1680 mfspr r7, SPRN_PMC5
1681 mfspr r8, SPRN_PMC6
1682 stw r3, VCPU_PMC(r9)
1683 stw r4, VCPU_PMC + 4(r9)
1684 stw r5, VCPU_PMC + 8(r9)
1685 stw r6, VCPU_PMC + 12(r9)
1686 stw r7, VCPU_PMC + 16(r9)
1687 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001688BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +11001689 mfspr r5, SPRN_SIER
Paul Mackerras83677f52016-11-16 22:33:27 +11001690 std r5, VCPU_SIER(r9)
1691BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001692 mfspr r6, SPRN_SPMC1
1693 mfspr r7, SPRN_SPMC2
1694 mfspr r8, SPRN_MMCRS
Michael Neulingb005255e2014-01-08 21:25:21 +11001695 stw r6, VCPU_PMC + 24(r9)
1696 stw r7, VCPU_PMC + 28(r9)
1697 std r8, VCPU_MMCR + 32(r9)
1698 lis r4, 0x8000
1699 mtspr SPRN_MMCRS, r4
Paul Mackerras83677f52016-11-16 22:33:27 +11001700END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001701END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000170222:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001703 /* Clear out SLB */
1704 li r5,0
1705 slbmte r5,r5
1706 slbia
1707 ptesync
1708
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001709 /* Restore host values of some registers */
1710BEGIN_FTR_SECTION
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001711 ld r5, STACK_SLOT_CIABR(r1)
1712 ld r6, STACK_SLOT_DAWR(r1)
1713 ld r7, STACK_SLOT_DAWRX(r1)
1714 mtspr SPRN_CIABR, r5
1715 mtspr SPRN_DAWR, r6
1716 mtspr SPRN_DAWRX, r7
1717END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1718BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001719 ld r5, STACK_SLOT_TID(r1)
1720 ld r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001721 ld r7, STACK_SLOT_PID(r1)
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001722 ld r8, STACK_SLOT_IAMR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001723 mtspr SPRN_TIDR, r5
1724 mtspr SPRN_PSSCR, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001725 mtspr SPRN_PID, r7
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001726 mtspr SPRN_IAMR, r8
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001727END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerrasf11f6f72017-01-30 21:21:52 +11001728BEGIN_FTR_SECTION
1729 PPC_INVALIDATE_ERAT
1730END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001731
Paul Mackerrasde56a942011-06-29 00:21:34 +00001732 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001733 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001734 * We don't have to lock against tlbies but we do
1735 * have to coordinate the hardware threads.
1736 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001737kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001738 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001739 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001740 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1741 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001742 cmpwi r3,0
1743 beq 15f
1744 HMT_LOW
174513: lbz r3,VCORE_IN_GUEST(r5)
1746 cmpwi r3,0
1747 bne 13b
1748 HMT_MEDIUM
1749 b 16f
1750
1751 /* Primary thread waits for all the secondaries to exit guest */
175215: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001753 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001754 clrldi r3,r3,56
1755 cmpw r3,r0
1756 bne 15b
1757 isync
1758
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001759 /* Did we actually switch to the guest at all? */
1760 lbz r6, VCORE_IN_GUEST(r5)
1761 cmpwi r6, 0
1762 beq 19f
1763
Paul Mackerrasde56a942011-06-29 00:21:34 +00001764 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001765 lwz r7,KVM_HOST_LPID(r4)
Paul Mackerras7a840842016-11-16 22:25:20 +11001766BEGIN_FTR_SECTION
1767 ld r6,KVM_HOST_SDR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001768 li r8,LPID_RSVD /* switch to reserved LPID */
1769 mtspr SPRN_LPID,r8
1770 ptesync
Paul Mackerras7a840842016-11-16 22:25:20 +11001771 mtspr SPRN_SDR1,r6 /* switch to host page table */
1772END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001773 mtspr SPRN_LPID,r7
1774 isync
1775
Michael Neulingb005255e2014-01-08 21:25:21 +11001776BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001777 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +11001778 mfspr r7, SPRN_DPDES
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001779 mfspr r8, SPRN_VTB
Michael Neulingb005255e2014-01-08 21:25:21 +11001780 std r7, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001781 std r8, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +11001782 /* clear DPDES so we don't get guest doorbells in the host */
1783 li r8, 0
1784 mtspr SPRN_DPDES, r8
1785END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1786
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301787 /* If HMI, call kvmppc_realmode_hmi_handler() */
1788 cmpwi r12, BOOK3S_INTERRUPT_HMI
1789 bne 27f
1790 bl kvmppc_realmode_hmi_handler
1791 nop
1792 li r12, BOOK3S_INTERRUPT_HMI
1793 /*
1794 * At this point kvmppc_realmode_hmi_handler would have resync-ed
1795 * the TB. Hence it is not required to subtract guest timebase
1796 * offset from timebase. So, skip it.
1797 *
1798 * Also, do not call kvmppc_subcore_exit_guest() because it has
1799 * been invoked as part of kvmppc_realmode_hmi_handler().
1800 */
1801 b 30f
1802
180327:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001804 /* Subtract timebase offset from timebase */
1805 ld r8,VCORE_TB_OFFSET(r5)
1806 cmpdi r8,0
1807 beq 17f
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001808 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001809 subf r8,r8,r6
1810 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1811 mftb r7 /* check if lower 24 bits overflowed */
1812 clrldi r6,r6,40
1813 clrldi r7,r7,40
1814 cmpld r7,r6
1815 bge 17f
1816 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1817 mtspr SPRN_TBU40,r8
1818
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530181917: bl kvmppc_subcore_exit_guest
1820 nop
182130: ld r5,HSTATE_KVM_VCORE(r13)
1822 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1823
Paul Mackerrasde56a942011-06-29 00:21:34 +00001824 /* Reset PCR */
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301825 ld r0, VCORE_PCR(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001826 cmpdi r0, 0
1827 beq 18f
1828 li r0, 0
1829 mtspr SPRN_PCR, r0
183018:
1831 /* Signal secondary CPUs to continue */
1832 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000183319: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001834 mtspr SPRN_HDEC,r8
1835
183616: ld r8,KVM_HOST_LPCR(r4)
1837 mtspr SPRN_LPCR,r8
1838 isync
Paul Mackerrasde56a942011-06-29 00:21:34 +00001839
1840 /* load host SLB entries */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001841BEGIN_MMU_FTR_SECTION
1842 b 0f
1843END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001844 ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001845
1846 .rept SLB_NUM_BOLTED
Alexander Graf0865a582014-06-11 10:36:17 +02001847 li r3, SLBSHADOW_SAVEAREA
1848 LDX_BE r5, r8, r3
1849 addi r3, r3, 8
1850 LDX_BE r6, r8, r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00001851 andis. r7,r5,SLB_ESID_V@h
1852 beq 1f
1853 slbmte r6,r5
18541: addi r8,r8,16
1855 .endr
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110018560:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001857#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1858 /* Finish timing, if we have a vcpu */
1859 ld r4, HSTATE_KVM_VCPU(r13)
1860 cmpdi r4, 0
1861 li r3, 0
1862 beq 2f
1863 bl kvmhv_accumulate_time
18642:
1865#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001866 /* Unset guest mode */
1867 li r0, KVM_GUEST_MODE_NONE
1868 stb r0, HSTATE_IN_GUEST(r13)
1869
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001870 ld r0, SFS+PPC_LR_STKOFF(r1)
1871 addi r1, r1, SFS
Paul Mackerras218309b2013-09-06 13:23:44 +10001872 mtlr r0
1873 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001874
Paul Mackerras697d3892011-12-12 12:36:37 +00001875/*
1876 * Check whether an HDSI is an HPTE not found fault or something else.
1877 * If it is an HPTE not found fault that is due to the guest accessing
1878 * a page that they have mapped but which we have paged out, then
1879 * we continue on with the guest exit path. In all other cases,
1880 * reflect the HDSI to the guest as a DSI.
1881 */
1882kvmppc_hdsi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001883 ld r3, VCPU_KVM(r9)
1884 lbz r0, KVM_RADIX(r3)
1885 cmpwi r0, 0
Paul Mackerras697d3892011-12-12 12:36:37 +00001886 mfspr r4, SPRN_HDAR
1887 mfspr r6, SPRN_HDSISR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001888 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
Paul Mackerras4cf302b2011-12-12 12:38:51 +00001889 /* HPTE not found fault or protection fault? */
1890 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00001891 beq 1f /* if not, send it to the guest */
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11001892 andi. r0, r11, MSR_DR /* data relocation enabled? */
1893 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11001894BEGIN_FTR_SECTION
1895 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
1896 b 4f
1897END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras697d3892011-12-12 12:36:37 +00001898 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001899 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001900 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
1901 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000019024: std r4, VCPU_FAULT_DAR(r9)
1903 stw r6, VCPU_FAULT_DSISR(r9)
1904
1905 /* Search the hash table. */
1906 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001907 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001908 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00001909 ld r9, HSTATE_KVM_VCPU(r13)
1910 ld r10, VCPU_PC(r9)
1911 ld r11, VCPU_MSR(r9)
1912 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1913 cmpdi r3, 0 /* retry the instruction */
1914 beq 6f
1915 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001916 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001917 cmpdi r3, -2 /* MMIO emulation; need instr word */
1918 beq 2f
1919
Paul Mackerrascf29b212015-10-27 16:10:20 +11001920 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00001921 ld r4, VCPU_FAULT_DAR(r9)
1922 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110019231: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00001924 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110019257: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00001926 mtspr SPRN_SRR0, r10
1927 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001928 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001929 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001930fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000019316: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001932 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001933 mtctr r7
1934 mtxer r8
1935 mr r4, r9
1936 b fast_guest_return
1937
19383: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1939 ld r5, KVM_VRMA_SLB_V(r5)
1940 b 4b
1941
1942 /* If this is for emulated MMIO, load the instruction word */
19432: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1944
1945 /* Set guest mode to 'jump over instruction' so if lwz faults
1946 * we'll just continue at the next IP. */
1947 li r0, KVM_GUEST_MODE_SKIP
1948 stb r0, HSTATE_IN_GUEST(r13)
1949
1950 /* Do the access with MSR:DR enabled */
1951 mfmsr r3
1952 ori r4, r3, MSR_DR /* Enable paging for data */
1953 mtmsrd r4
1954 lwz r8, 0(r10)
1955 mtmsrd r3
1956
1957 /* Store the result */
1958 stw r8, VCPU_LAST_INST(r9)
1959
1960 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001961 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00001962 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001963 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00001964
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001965.Lradix_hdsi:
1966 std r4, VCPU_FAULT_DAR(r9)
1967 stw r6, VCPU_FAULT_DSISR(r9)
1968.Lradix_hisi:
1969 mfspr r5, SPRN_ASDR
1970 std r5, VCPU_FAULT_GPA(r9)
1971 b guest_exit_cont
1972
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001973/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001974 * Similarly for an HISI, reflect it to the guest as an ISI unless
1975 * it is an HPTE not found fault for a page that we have paged out.
1976 */
1977kvmppc_hisi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001978 ld r3, VCPU_KVM(r9)
1979 lbz r0, KVM_RADIX(r3)
1980 cmpwi r0, 0
1981 bne .Lradix_hisi /* for radix, just save ASDR */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001982 andis. r0, r11, SRR1_ISI_NOPT@h
1983 beq 1f
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11001984 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1985 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11001986BEGIN_FTR_SECTION
1987 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
1988 b 4f
1989END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras342d3db2011-12-12 12:38:05 +00001990 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001991 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001992 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
1993 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000019944:
1995 /* Search the hash table. */
1996 mr r3, r9 /* vcpu pointer */
1997 mr r4, r10
1998 mr r6, r11
1999 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002000 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00002001 ld r9, HSTATE_KVM_VCPU(r13)
2002 ld r10, VCPU_PC(r9)
2003 ld r11, VCPU_MSR(r9)
2004 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
2005 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002006 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002007 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002008 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00002009
Paul Mackerrascf29b212015-10-27 16:10:20 +11002010 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002011 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110020121: li r0, BOOK3S_INTERRUPT_INST_STORAGE
20137: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00002014 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002015 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002016 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002017 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002018
20193: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
2020 ld r5, KVM_VRMA_SLB_V(r6)
2021 b 4b
2022
2023/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002024 * Try to handle an hcall in real mode.
2025 * Returns to the guest if we handle it, or continues on up to
2026 * the kernel if we can't (i.e. if we don't have a handler for
2027 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002028 *
2029 * r5 - r8 contain hcall args,
2030 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002031 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002032hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00002033 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002034 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08002035 /* sc 1 from userspace - reflect to guest syscall */
2036 bne sc_1_fast_return
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002037 clrrdi r3,r3,2
2038 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002039 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002040 /* See if this hcall is enabled for in-kernel handling */
2041 ld r4, VCPU_KVM(r9)
2042 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
2043 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
2044 add r4, r4, r0
2045 ld r0, KVM_ENABLED_HCALLS(r4)
2046 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
2047 srd r0, r0, r4
2048 andi. r0, r0, 1
2049 beq guest_exit_cont
2050 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002051 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10002052 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002053 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002054 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10002055 add r12,r3,r4
2056 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002057 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002058 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002059 bctrl
2060 cmpdi r3,H_TOO_HARD
2061 beq hcall_real_fallback
2062 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00002063 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002064 ld r10,VCPU_PC(r4)
2065 ld r11,VCPU_MSR(r4)
2066 b fast_guest_return
2067
Liu Ping Fan27025a62013-11-19 14:12:48 +08002068sc_1_fast_return:
2069 mtspr SPRN_SRR0,r10
2070 mtspr SPRN_SRR1,r11
2071 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11002072 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08002073 mr r4,r9
2074 b fast_guest_return
2075
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002076 /* We've attempted a real mode hcall, but it's punted it back
2077 * to userspace. We need to restore some clobbered volatiles
2078 * before resuming the pass-it-to-qemu path */
2079hcall_real_fallback:
2080 li r12,BOOK3S_INTERRUPT_SYSCALL
2081 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002082
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002083 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002084
2085 .globl hcall_real_table
2086hcall_real_table:
2087 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002088 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
2089 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
2090 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10002091 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2092 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002093 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
2094 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002095 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002096 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002097 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002098 .long 0 /* 0x2c */
2099 .long 0 /* 0x30 */
2100 .long 0 /* 0x34 */
2101 .long 0 /* 0x38 */
2102 .long 0 /* 0x3c */
2103 .long 0 /* 0x40 */
2104 .long 0 /* 0x44 */
2105 .long 0 /* 0x48 */
2106 .long 0 /* 0x4c */
2107 .long 0 /* 0x50 */
2108 .long 0 /* 0x54 */
2109 .long 0 /* 0x58 */
2110 .long 0 /* 0x5c */
2111 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002112#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002113 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2114 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2115 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002116 .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002117 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002118#else
2119 .long 0 /* 0x64 - H_EOI */
2120 .long 0 /* 0x68 - H_CPPR */
2121 .long 0 /* 0x6c - H_IPI */
2122 .long 0 /* 0x70 - H_IPOLL */
2123 .long 0 /* 0x74 - H_XIRR */
2124#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002125 .long 0 /* 0x78 */
2126 .long 0 /* 0x7c */
2127 .long 0 /* 0x80 */
2128 .long 0 /* 0x84 */
2129 .long 0 /* 0x88 */
2130 .long 0 /* 0x8c */
2131 .long 0 /* 0x90 */
2132 .long 0 /* 0x94 */
2133 .long 0 /* 0x98 */
2134 .long 0 /* 0x9c */
2135 .long 0 /* 0xa0 */
2136 .long 0 /* 0xa4 */
2137 .long 0 /* 0xa8 */
2138 .long 0 /* 0xac */
2139 .long 0 /* 0xb0 */
2140 .long 0 /* 0xb4 */
2141 .long 0 /* 0xb8 */
2142 .long 0 /* 0xbc */
2143 .long 0 /* 0xc0 */
2144 .long 0 /* 0xc4 */
2145 .long 0 /* 0xc8 */
2146 .long 0 /* 0xcc */
2147 .long 0 /* 0xd0 */
2148 .long 0 /* 0xd4 */
2149 .long 0 /* 0xd8 */
2150 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002151 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11002152 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002153 .long 0 /* 0xe8 */
2154 .long 0 /* 0xec */
2155 .long 0 /* 0xf0 */
2156 .long 0 /* 0xf4 */
2157 .long 0 /* 0xf8 */
2158 .long 0 /* 0xfc */
2159 .long 0 /* 0x100 */
2160 .long 0 /* 0x104 */
2161 .long 0 /* 0x108 */
2162 .long 0 /* 0x10c */
2163 .long 0 /* 0x110 */
2164 .long 0 /* 0x114 */
2165 .long 0 /* 0x118 */
2166 .long 0 /* 0x11c */
2167 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002168 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11002169 .long 0 /* 0x128 */
2170 .long 0 /* 0x12c */
2171 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002172 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002173 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11002174 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Michael Ellermane928e9c2015-03-20 20:39:41 +11002175 .long 0 /* 0x140 */
2176 .long 0 /* 0x144 */
2177 .long 0 /* 0x148 */
2178 .long 0 /* 0x14c */
2179 .long 0 /* 0x150 */
2180 .long 0 /* 0x154 */
2181 .long 0 /* 0x158 */
2182 .long 0 /* 0x15c */
2183 .long 0 /* 0x160 */
2184 .long 0 /* 0x164 */
2185 .long 0 /* 0x168 */
2186 .long 0 /* 0x16c */
2187 .long 0 /* 0x170 */
2188 .long 0 /* 0x174 */
2189 .long 0 /* 0x178 */
2190 .long 0 /* 0x17c */
2191 .long 0 /* 0x180 */
2192 .long 0 /* 0x184 */
2193 .long 0 /* 0x188 */
2194 .long 0 /* 0x18c */
2195 .long 0 /* 0x190 */
2196 .long 0 /* 0x194 */
2197 .long 0 /* 0x198 */
2198 .long 0 /* 0x19c */
2199 .long 0 /* 0x1a0 */
2200 .long 0 /* 0x1a4 */
2201 .long 0 /* 0x1a8 */
2202 .long 0 /* 0x1ac */
2203 .long 0 /* 0x1b0 */
2204 .long 0 /* 0x1b4 */
2205 .long 0 /* 0x1b8 */
2206 .long 0 /* 0x1bc */
2207 .long 0 /* 0x1c0 */
2208 .long 0 /* 0x1c4 */
2209 .long 0 /* 0x1c8 */
2210 .long 0 /* 0x1cc */
2211 .long 0 /* 0x1d0 */
2212 .long 0 /* 0x1d4 */
2213 .long 0 /* 0x1d8 */
2214 .long 0 /* 0x1dc */
2215 .long 0 /* 0x1e0 */
2216 .long 0 /* 0x1e4 */
2217 .long 0 /* 0x1e8 */
2218 .long 0 /* 0x1ec */
2219 .long 0 /* 0x1f0 */
2220 .long 0 /* 0x1f4 */
2221 .long 0 /* 0x1f8 */
2222 .long 0 /* 0x1fc */
2223 .long 0 /* 0x200 */
2224 .long 0 /* 0x204 */
2225 .long 0 /* 0x208 */
2226 .long 0 /* 0x20c */
2227 .long 0 /* 0x210 */
2228 .long 0 /* 0x214 */
2229 .long 0 /* 0x218 */
2230 .long 0 /* 0x21c */
2231 .long 0 /* 0x220 */
2232 .long 0 /* 0x224 */
2233 .long 0 /* 0x228 */
2234 .long 0 /* 0x22c */
2235 .long 0 /* 0x230 */
2236 .long 0 /* 0x234 */
2237 .long 0 /* 0x238 */
2238 .long 0 /* 0x23c */
2239 .long 0 /* 0x240 */
2240 .long 0 /* 0x244 */
2241 .long 0 /* 0x248 */
2242 .long 0 /* 0x24c */
2243 .long 0 /* 0x250 */
2244 .long 0 /* 0x254 */
2245 .long 0 /* 0x258 */
2246 .long 0 /* 0x25c */
2247 .long 0 /* 0x260 */
2248 .long 0 /* 0x264 */
2249 .long 0 /* 0x268 */
2250 .long 0 /* 0x26c */
2251 .long 0 /* 0x270 */
2252 .long 0 /* 0x274 */
2253 .long 0 /* 0x278 */
2254 .long 0 /* 0x27c */
2255 .long 0 /* 0x280 */
2256 .long 0 /* 0x284 */
2257 .long 0 /* 0x288 */
2258 .long 0 /* 0x28c */
2259 .long 0 /* 0x290 */
2260 .long 0 /* 0x294 */
2261 .long 0 /* 0x298 */
2262 .long 0 /* 0x29c */
2263 .long 0 /* 0x2a0 */
2264 .long 0 /* 0x2a4 */
2265 .long 0 /* 0x2a8 */
2266 .long 0 /* 0x2ac */
2267 .long 0 /* 0x2b0 */
2268 .long 0 /* 0x2b4 */
2269 .long 0 /* 0x2b8 */
2270 .long 0 /* 0x2bc */
2271 .long 0 /* 0x2c0 */
2272 .long 0 /* 0x2c4 */
2273 .long 0 /* 0x2c8 */
2274 .long 0 /* 0x2cc */
2275 .long 0 /* 0x2d0 */
2276 .long 0 /* 0x2d4 */
2277 .long 0 /* 0x2d8 */
2278 .long 0 /* 0x2dc */
2279 .long 0 /* 0x2e0 */
2280 .long 0 /* 0x2e4 */
2281 .long 0 /* 0x2e8 */
2282 .long 0 /* 0x2ec */
2283 .long 0 /* 0x2f0 */
2284 .long 0 /* 0x2f4 */
2285 .long 0 /* 0x2f8 */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002286#ifdef CONFIG_KVM_XICS
2287 .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2288#else
2289 .long 0 /* 0x2fc - H_XIRR_X*/
2290#endif
Michael Ellermane928e9c2015-03-20 20:39:41 +11002291 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002292 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002293hcall_real_table_end:
2294
Paul Mackerras8563bf52014-01-08 21:25:29 +11002295_GLOBAL(kvmppc_h_set_xdabr)
2296 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2297 beq 6f
2298 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2299 andc. r0, r5, r0
2300 beq 3f
23016: li r3, H_PARAMETER
2302 blr
2303
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002304_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002305 li r5, DABRX_USER | DABRX_KERNEL
23063:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002307BEGIN_FTR_SECTION
2308 b 2f
2309END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002310 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002311 stw r5, VCPU_DABRX(r3)
2312 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002313 /* Work around P7 bug where DABR can get corrupted on mtspr */
23141: mtspr SPRN_DABR,r4
2315 mfspr r5, SPRN_DABR
2316 cmpd r4, r5
2317 bne 1b
2318 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002319 li r3,0
2320 blr
2321
Paul Mackerras8563bf52014-01-08 21:25:29 +11002322 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
23232: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002324 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002325 clrrdi r4, r4, 3
2326 std r4, VCPU_DAWR(r3)
2327 std r5, VCPU_DAWRX(r3)
2328 mtspr SPRN_DAWR, r4
2329 mtspr SPRN_DAWRX, r5
2330 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002331 blr
2332
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002333_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002334 ori r11,r11,MSR_EE
2335 std r11,VCPU_MSR(r3)
2336 li r0,1
2337 stb r0,VCPU_CEDED(r3)
2338 sync /* order setting ceded vs. testing prodded */
2339 lbz r5,VCPU_PRODDED(r3)
2340 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002341 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002342 li r12,0 /* set trap to 0 to say hcall is handled */
2343 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002344 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002345 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002346
2347 /*
2348 * Set our bit in the bitmask of napping threads unless all the
2349 * other threads are already napping, in which case we send this
2350 * up to the host.
2351 */
2352 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002353 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002354 lwz r8,VCORE_ENTRY_EXIT(r5)
2355 clrldi r8,r8,56
2356 li r0,1
2357 sld r0,r0,r6
2358 addi r6,r5,VCORE_NAPPING_THREADS
235931: lwarx r4,0,r6
2360 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002361 cmpw r4,r8
2362 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002363 stwcx. r4,0,r6
2364 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002365 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002366 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002367 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002368 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002369 lwz r7,VCORE_ENTRY_EXIT(r5)
2370 cmpwi r7,0x100
2371 bge 33f /* another thread already exiting */
2372
2373/*
2374 * Although not specifically required by the architecture, POWER7
2375 * preserves the following registers in nap mode, even if an SMT mode
2376 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2377 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2378 */
2379 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002380 std r14, VCPU_GPR(R14)(r3)
2381 std r15, VCPU_GPR(R15)(r3)
2382 std r16, VCPU_GPR(R16)(r3)
2383 std r17, VCPU_GPR(R17)(r3)
2384 std r18, VCPU_GPR(R18)(r3)
2385 std r19, VCPU_GPR(R19)(r3)
2386 std r20, VCPU_GPR(R20)(r3)
2387 std r21, VCPU_GPR(R21)(r3)
2388 std r22, VCPU_GPR(R22)(r3)
2389 std r23, VCPU_GPR(R23)(r3)
2390 std r24, VCPU_GPR(R24)(r3)
2391 std r25, VCPU_GPR(R25)(r3)
2392 std r26, VCPU_GPR(R26)(r3)
2393 std r27, VCPU_GPR(R27)(r3)
2394 std r28, VCPU_GPR(R28)(r3)
2395 std r29, VCPU_GPR(R29)(r3)
2396 std r30, VCPU_GPR(R30)(r3)
2397 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002398
2399 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002400 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002401
Paul Mackerras93d17392016-06-22 15:52:55 +10002402#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2403BEGIN_FTR_SECTION
2404 ld r9, HSTATE_KVM_VCPU(r13)
2405 bl kvmppc_save_tm
2406END_FTR_SECTION_IFSET(CPU_FTR_TM)
2407#endif
2408
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002409 /*
2410 * Set DEC to the smaller of DEC and HDEC, so that we wake
2411 * no later than the end of our timeslice (HDEC interrupts
2412 * don't wake us from nap).
2413 */
2414 mfspr r3, SPRN_DEC
2415 mfspr r4, SPRN_HDEC
2416 mftb r5
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10002417BEGIN_FTR_SECTION
2418 /* On P9 check whether the guest has large decrementer mode enabled */
2419 ld r6, HSTATE_KVM_VCORE(r13)
2420 ld r6, VCORE_LPCR(r6)
2421 andis. r6, r6, LPCR_LD@h
2422 bne 68f
2423END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras2f272462017-05-22 16:25:14 +10002424 extsw r3, r3
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000242568: EXTEND_HDEC(r4)
Paul Mackerras2f272462017-05-22 16:25:14 +10002426 cmpd r3, r4
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002427 ble 67f
2428 mtspr SPRN_DEC, r4
242967:
2430 /* save expiry time of guest decrementer */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002431 add r3, r3, r5
2432 ld r4, HSTATE_KVM_VCPU(r13)
2433 ld r5, HSTATE_KVM_VCORE(r13)
2434 ld r6, VCORE_TB_OFFSET(r5)
2435 subf r3, r6, r3 /* convert to host TB value */
2436 std r3, VCPU_DEC_EXPIRES(r4)
2437
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002438#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2439 ld r4, HSTATE_KVM_VCPU(r13)
2440 addi r3, r4, VCPU_TB_CEDE
2441 bl kvmhv_accumulate_time
2442#endif
2443
Paul Mackerrasccc07772015-03-28 14:21:07 +11002444 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2445
Paul Mackerras19ccb762011-07-23 17:42:46 +10002446 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002447 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002448 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002449 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002450 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002451 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002452kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002453 mfspr r0, SPRN_CTRLF
2454 clrrdi r0, r0, 1
2455 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302456
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002457 li r0,1
2458 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002459 mfspr r5,SPRN_LPCR
2460 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002461BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002462 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002463 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002464END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002465
2466kvm_nap_sequence: /* desired LPCR value in r5 */
2467BEGIN_FTR_SECTION
2468 /*
2469 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2470 * enable state loss = 1 (allow SMT mode switch)
2471 * requested level = 0 (just stop dispatching)
2472 */
2473 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2474 mtspr SPRN_PSSCR, r3
2475 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2476 li r4, LPCR_PECE_HVEE@higher
2477 sldi r4, r4, 32
2478 or r5, r5, r4
2479END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002480 mtspr SPRN_LPCR,r5
2481 isync
2482 li r0, 0
2483 std r0, HSTATE_SCRATCH0(r13)
2484 ptesync
2485 ld r0, HSTATE_SCRATCH0(r13)
24861: cmpd r0, r0
2487 bne 1b
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002488BEGIN_FTR_SECTION
Paul Mackerras19ccb762011-07-23 17:42:46 +10002489 nap
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002490FTR_SECTION_ELSE
2491 PPC_STOP
2492ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002493 b .
2494
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100249533: mr r4, r3
2496 li r3, 0
2497 li r12, 0
2498 b 34f
2499
Paul Mackerras19ccb762011-07-23 17:42:46 +10002500kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002501 /* get vcpu pointer */
2502 ld r4, HSTATE_KVM_VCPU(r13)
2503
Paul Mackerras19ccb762011-07-23 17:42:46 +10002504 /* Woken by external or decrementer interrupt */
2505 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002506
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002507#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2508 addi r3, r4, VCPU_TB_RMINTR
2509 bl kvmhv_accumulate_time
2510#endif
2511
Paul Mackerras93d17392016-06-22 15:52:55 +10002512#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2513BEGIN_FTR_SECTION
2514 bl kvmppc_restore_tm
2515END_FTR_SECTION_IFSET(CPU_FTR_TM)
2516#endif
2517
Paul Mackerras19ccb762011-07-23 17:42:46 +10002518 /* load up FP state */
2519 bl kvmppc_load_fp
2520
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002521 /* Restore guest decrementer */
2522 ld r3, VCPU_DEC_EXPIRES(r4)
2523 ld r5, HSTATE_KVM_VCORE(r13)
2524 ld r6, VCORE_TB_OFFSET(r5)
2525 add r3, r3, r6 /* convert host TB to guest TB value */
2526 mftb r7
2527 subf r3, r7, r3
2528 mtspr SPRN_DEC, r3
2529
Paul Mackerras19ccb762011-07-23 17:42:46 +10002530 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002531 ld r14, VCPU_GPR(R14)(r4)
2532 ld r15, VCPU_GPR(R15)(r4)
2533 ld r16, VCPU_GPR(R16)(r4)
2534 ld r17, VCPU_GPR(R17)(r4)
2535 ld r18, VCPU_GPR(R18)(r4)
2536 ld r19, VCPU_GPR(R19)(r4)
2537 ld r20, VCPU_GPR(R20)(r4)
2538 ld r21, VCPU_GPR(R21)(r4)
2539 ld r22, VCPU_GPR(R22)(r4)
2540 ld r23, VCPU_GPR(R23)(r4)
2541 ld r24, VCPU_GPR(R24)(r4)
2542 ld r25, VCPU_GPR(R25)(r4)
2543 ld r26, VCPU_GPR(R26)(r4)
2544 ld r27, VCPU_GPR(R27)(r4)
2545 ld r28, VCPU_GPR(R28)(r4)
2546 ld r29, VCPU_GPR(R29)(r4)
2547 ld r30, VCPU_GPR(R30)(r4)
2548 ld r31, VCPU_GPR(R31)(r4)
Suresh Warrier37f55d32016-08-19 15:35:46 +10002549
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002550 /* Check the wake reason in SRR1 to see why we got here */
2551 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002552
Suresh Warrier37f55d32016-08-19 15:35:46 +10002553 /*
2554 * Restore volatile registers since we could have called a
2555 * C routine in kvmppc_check_wake_reason
2556 * r4 = VCPU
2557 * r3 tells us whether we need to return to host or not
2558 * WARNING: it gets checked further down:
2559 * should not modify r3 until this check is done.
2560 */
2561 ld r4, HSTATE_KVM_VCPU(r13)
2562
Paul Mackerras19ccb762011-07-23 17:42:46 +10002563 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100256434: ld r5,HSTATE_KVM_VCORE(r13)
2565 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002566 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002567 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002568 addi r6,r5,VCORE_NAPPING_THREADS
256932: lwarx r7,0,r6
2570 andc r7,r7,r0
2571 stwcx. r7,0,r6
2572 bne 32b
2573 li r0,0
2574 stb r0,HSTATE_NAPPING(r13)
2575
Suresh Warrier37f55d32016-08-19 15:35:46 +10002576 /* See if the wake reason saved in r3 means we need to exit */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002577 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002578 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002579 cmpdi r3, 0
2580 bgt guest_exit_cont
Paul Mackerras4619ac82013-04-17 20:31:41 +00002581
Paul Mackerras19ccb762011-07-23 17:42:46 +10002582 /* see if any other thread is already exiting */
2583 lwz r0,VCORE_ENTRY_EXIT(r5)
2584 cmpwi r0,0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002585 bge guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002586
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002587 b kvmppc_cede_reentry /* if not go back to guest */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002588
2589 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002590kvm_cede_prodded:
2591 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002592 stb r0,VCPU_PRODDED(r3)
2593 sync /* order testing prodded vs. clearing ceded */
2594 stb r0,VCPU_CEDED(r3)
2595 li r3,H_SUCCESS
2596 blr
2597
2598 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002599kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002600 ld r9, HSTATE_KVM_VCPU(r13)
2601 b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002602
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002603 /* Try to handle a machine check in real mode */
2604machine_check_realmode:
2605 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002606 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002607 nop
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002608 ld r9, HSTATE_KVM_VCPU(r13)
2609 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302610 /*
2611 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2612 * machine check interrupt (set HSRR0 to 0x200). And for handled
2613 * errors (no-fatal), just go back to guest execution with current
2614 * HSRR0 instead of exiting guest. This new approach will inject
2615 * machine check to guest for fatal error causing guest to crash.
2616 *
2617 * The old code used to return to host for unhandled errors which
2618 * was causing guest to hang with soft lockups inside guest and
2619 * makes it difficult to recover guest instance.
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302620 *
2621 * if we receive machine check with MSR(RI=0) then deliver it to
2622 * guest as machine check causing guest to crash.
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302623 */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302624 ld r11, VCPU_MSR(r9)
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11002625 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2626 bne mc_cont /* if so, exit to host */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302627 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2628 beq 1f /* Deliver a machine check to guest */
2629 ld r10, VCPU_PC(r9)
2630 cmpdi r3, 0 /* Did we handle MCE ? */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302631 bne 2f /* Continue guest execution. */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002632 /* If not, deliver a machine check. SRR0/1 are already set */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +053026331: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
Michael Neulinge4e38122014-03-25 10:47:02 +11002634 bl kvmppc_msr_interrupt
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +053026352: b fast_interrupt_c_return
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002636
Paul Mackerrasde56a942011-06-29 00:21:34 +00002637/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002638 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002639 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002640 * 0 if nothing needs to be done
2641 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002642 * -1 if there was a guest wakeup (IPI or msgsnd)
Suresh Warriere3c13e52016-08-19 15:35:51 +10002643 * -2 if we handled a PCI passthrough interrupt (returned by
2644 * kvmppc_read_intr only)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002645 *
2646 * Also sets r12 to the interrupt vector for any interrupt that needs
2647 * to be handled now by the host (0x500 for external interrupt), or zero.
Suresh Warrier37f55d32016-08-19 15:35:46 +10002648 * Modifies all volatile registers (since it may call a C function).
2649 * This routine calls kvmppc_read_intr, a C function, if an external
2650 * interrupt is pending.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002651 */
2652kvmppc_check_wake_reason:
2653 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002654BEGIN_FTR_SECTION
2655 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2656FTR_SECTION_ELSE
2657 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2658ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2659 cmpwi r6, 8 /* was it an external interrupt? */
Suresh Warrier37f55d32016-08-19 15:35:46 +10002660 beq 7f /* if so, see what it was */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002661 li r3, 0
2662 li r12, 0
2663 cmpwi r6, 6 /* was it the decrementer? */
2664 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002665BEGIN_FTR_SECTION
2666 cmpwi r6, 5 /* privileged doorbell? */
2667 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002668 cmpwi r6, 3 /* hypervisor doorbell? */
2669 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002670END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302671 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2672 beq 4f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002673 li r3, 1 /* anything else, return 1 */
26740: blr
2675
Paul Mackerras5d00f662014-01-08 21:25:28 +11002676 /* hypervisor doorbell */
26773: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302678
2679 /*
2680 * Clear the doorbell as we will invoke the handler
2681 * explicitly in the guest exit path.
2682 */
2683 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2684 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11002685 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11002686 li r3, 1
Paul Mackerras66feed62015-03-28 14:21:12 +11002687 lbz r0, HSTATE_HOST_IPI(r13)
2688 cmpwi r0, 0
2689 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302690 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002691 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11002692 blr
2693
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302694 /* Woken up due to Hypervisor maintenance interrupt */
26954: li r12, BOOK3S_INTERRUPT_HMI
2696 li r3, 1
2697 blr
2698
Suresh Warrier37f55d32016-08-19 15:35:46 +10002699 /* external interrupt - create a stack frame so we can call C */
27007: mflr r0
2701 std r0, PPC_LR_STKOFF(r1)
2702 stdu r1, -PPC_MIN_STKFRM(r1)
2703 bl kvmppc_read_intr
2704 nop
2705 li r12, BOOK3S_INTERRUPT_EXTERNAL
Suresh Warrierf7af5202016-08-19 15:35:52 +10002706 cmpdi r3, 1
2707 ble 1f
2708
2709 /*
2710 * Return code of 2 means PCI passthrough interrupt, but
2711 * we need to return back to host to complete handling the
2712 * interrupt. Trap reason is expected in r12 by guest
2713 * exit code.
2714 */
2715 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
27161:
Suresh Warrier37f55d32016-08-19 15:35:46 +10002717 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2718 addi r1, r1, PPC_MIN_STKFRM
2719 mtlr r0
2720 blr
Paul Mackerrasde56a942011-06-29 00:21:34 +00002721
2722/*
2723 * Save away FP, VMX and VSX registers.
2724 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002725 * N.B. r30 and r31 are volatile across this function,
2726 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002727 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002728kvmppc_save_fp:
2729 mflr r30
2730 mr r31,r3
Paul Mackerras89436332012-03-02 01:38:23 +00002731 mfmsr r5
2732 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00002733#ifdef CONFIG_ALTIVEC
2734BEGIN_FTR_SECTION
2735 oris r8,r8,MSR_VEC@h
2736END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2737#endif
2738#ifdef CONFIG_VSX
2739BEGIN_FTR_SECTION
2740 oris r8,r8,MSR_VSX@h
2741END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2742#endif
2743 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002744 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002745 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002746#ifdef CONFIG_ALTIVEC
2747BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002748 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002749 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002750END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2751#endif
2752 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11002753 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11002754 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00002755 blr
2756
2757/*
2758 * Load up FP, VMX and VSX registers
2759 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002760 * N.B. r30 and r31 are volatile across this function,
2761 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002762 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002763kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11002764 mflr r30
2765 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00002766 mfmsr r9
2767 ori r8,r9,MSR_FP
2768#ifdef CONFIG_ALTIVEC
2769BEGIN_FTR_SECTION
2770 oris r8,r8,MSR_VEC@h
2771END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2772#endif
2773#ifdef CONFIG_VSX
2774BEGIN_FTR_SECTION
2775 oris r8,r8,MSR_VSX@h
2776END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2777#endif
2778 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002779 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002780 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002781#ifdef CONFIG_ALTIVEC
2782BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002783 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002784 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002785END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2786#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11002787 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002788 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11002789 mtlr r30
2790 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00002791 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10002792
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002793#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2794/*
2795 * Save transactional state and TM-related registers.
2796 * Called with r9 pointing to the vcpu struct.
2797 * This can modify all checkpointed registers, but
2798 * restores r1, r2 and r9 (vcpu pointer) before exit.
2799 */
2800kvmppc_save_tm:
2801 mflr r0
2802 std r0, PPC_LR_STKOFF(r1)
2803
2804 /* Turn on TM. */
2805 mfmsr r8
2806 li r0, 1
2807 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
2808 mtmsrd r8
2809
2810 ld r5, VCPU_MSR(r9)
2811 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2812 beq 1f /* TM not active in guest. */
2813
2814 std r1, HSTATE_HOST_R1(r13)
2815 li r3, TM_CAUSE_KVM_RESCHED
2816
2817 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2818 li r5, 0
2819 mtmsrd r5, 1
2820
2821 /* All GPRs are volatile at this point. */
2822 TRECLAIM(R3)
2823
2824 /* Temporarily store r13 and r9 so we have some regs to play with */
2825 SET_SCRATCH0(r13)
2826 GET_PACA(r13)
2827 std r9, PACATMSCRATCH(r13)
2828 ld r9, HSTATE_KVM_VCPU(r13)
2829
2830 /* Get a few more GPRs free. */
2831 std r29, VCPU_GPRS_TM(29)(r9)
2832 std r30, VCPU_GPRS_TM(30)(r9)
2833 std r31, VCPU_GPRS_TM(31)(r9)
2834
2835 /* Save away PPR and DSCR soon so don't run with user values. */
2836 mfspr r31, SPRN_PPR
2837 HMT_MEDIUM
2838 mfspr r30, SPRN_DSCR
2839 ld r29, HSTATE_DSCR(r13)
2840 mtspr SPRN_DSCR, r29
2841
2842 /* Save all but r9, r13 & r29-r31 */
2843 reg = 0
2844 .rept 29
2845 .if (reg != 9) && (reg != 13)
2846 std reg, VCPU_GPRS_TM(reg)(r9)
2847 .endif
2848 reg = reg + 1
2849 .endr
2850 /* ... now save r13 */
2851 GET_SCRATCH0(r4)
2852 std r4, VCPU_GPRS_TM(13)(r9)
2853 /* ... and save r9 */
2854 ld r4, PACATMSCRATCH(r13)
2855 std r4, VCPU_GPRS_TM(9)(r9)
2856
2857 /* Reload stack pointer and TOC. */
2858 ld r1, HSTATE_HOST_R1(r13)
2859 ld r2, PACATOC(r13)
2860
2861 /* Set MSR RI now we have r1 and r13 back. */
2862 li r5, MSR_RI
2863 mtmsrd r5, 1
2864
2865 /* Save away checkpinted SPRs. */
2866 std r31, VCPU_PPR_TM(r9)
2867 std r30, VCPU_DSCR_TM(r9)
2868 mflr r5
2869 mfcr r6
2870 mfctr r7
2871 mfspr r8, SPRN_AMR
2872 mfspr r10, SPRN_TAR
Paul Mackerras0d808df2016-11-07 15:09:58 +11002873 mfxer r11
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002874 std r5, VCPU_LR_TM(r9)
2875 stw r6, VCPU_CR_TM(r9)
2876 std r7, VCPU_CTR_TM(r9)
2877 std r8, VCPU_AMR_TM(r9)
2878 std r10, VCPU_TAR_TM(r9)
Paul Mackerras0d808df2016-11-07 15:09:58 +11002879 std r11, VCPU_XER_TM(r9)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002880
2881 /* Restore r12 as trap number. */
2882 lwz r12, VCPU_TRAP(r9)
2883
2884 /* Save FP/VSX. */
2885 addi r3, r9, VCPU_FPRS_TM
2886 bl store_fp_state
2887 addi r3, r9, VCPU_VRS_TM
2888 bl store_vr_state
2889 mfspr r6, SPRN_VRSAVE
2890 stw r6, VCPU_VRSAVE_TM(r9)
28911:
2892 /*
2893 * We need to save these SPRs after the treclaim so that the software
2894 * error code is recorded correctly in the TEXASR. Also the user may
2895 * change these outside of a transaction, so they must always be
2896 * context switched.
2897 */
2898 mfspr r5, SPRN_TFHAR
2899 mfspr r6, SPRN_TFIAR
2900 mfspr r7, SPRN_TEXASR
2901 std r5, VCPU_TFHAR(r9)
2902 std r6, VCPU_TFIAR(r9)
2903 std r7, VCPU_TEXASR(r9)
2904
2905 ld r0, PPC_LR_STKOFF(r1)
2906 mtlr r0
2907 blr
2908
2909/*
2910 * Restore transactional state and TM-related registers.
2911 * Called with r4 pointing to the vcpu struct.
2912 * This potentially modifies all checkpointed registers.
2913 * It restores r1, r2, r4 from the PACA.
2914 */
2915kvmppc_restore_tm:
2916 mflr r0
2917 std r0, PPC_LR_STKOFF(r1)
2918
2919 /* Turn on TM/FP/VSX/VMX so we can restore them. */
2920 mfmsr r5
2921 li r6, MSR_TM >> 32
2922 sldi r6, r6, 32
2923 or r5, r5, r6
2924 ori r5, r5, MSR_FP
2925 oris r5, r5, (MSR_VEC | MSR_VSX)@h
2926 mtmsrd r5
2927
2928 /*
2929 * The user may change these outside of a transaction, so they must
2930 * always be context switched.
2931 */
2932 ld r5, VCPU_TFHAR(r4)
2933 ld r6, VCPU_TFIAR(r4)
2934 ld r7, VCPU_TEXASR(r4)
2935 mtspr SPRN_TFHAR, r5
2936 mtspr SPRN_TFIAR, r6
2937 mtspr SPRN_TEXASR, r7
2938
2939 ld r5, VCPU_MSR(r4)
2940 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2941 beqlr /* TM not active in guest */
2942 std r1, HSTATE_HOST_R1(r13)
2943
2944 /* Make sure the failure summary is set, otherwise we'll program check
2945 * when we trechkpt. It's possible that this might have been not set
2946 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
2947 * host.
2948 */
2949 oris r7, r7, (TEXASR_FS)@h
2950 mtspr SPRN_TEXASR, r7
2951
2952 /*
2953 * We need to load up the checkpointed state for the guest.
2954 * We need to do this early as it will blow away any GPRs, VSRs and
2955 * some SPRs.
2956 */
2957
2958 mr r31, r4
2959 addi r3, r31, VCPU_FPRS_TM
2960 bl load_fp_state
2961 addi r3, r31, VCPU_VRS_TM
2962 bl load_vr_state
2963 mr r4, r31
2964 lwz r7, VCPU_VRSAVE_TM(r4)
2965 mtspr SPRN_VRSAVE, r7
2966
2967 ld r5, VCPU_LR_TM(r4)
2968 lwz r6, VCPU_CR_TM(r4)
2969 ld r7, VCPU_CTR_TM(r4)
2970 ld r8, VCPU_AMR_TM(r4)
2971 ld r9, VCPU_TAR_TM(r4)
Paul Mackerras0d808df2016-11-07 15:09:58 +11002972 ld r10, VCPU_XER_TM(r4)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002973 mtlr r5
2974 mtcr r6
2975 mtctr r7
2976 mtspr SPRN_AMR, r8
2977 mtspr SPRN_TAR, r9
Paul Mackerras0d808df2016-11-07 15:09:58 +11002978 mtxer r10
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002979
2980 /*
2981 * Load up PPR and DSCR values but don't put them in the actual SPRs
2982 * till the last moment to avoid running with userspace PPR and DSCR for
2983 * too long.
2984 */
2985 ld r29, VCPU_DSCR_TM(r4)
2986 ld r30, VCPU_PPR_TM(r4)
2987
2988 std r2, PACATMSCRATCH(r13) /* Save TOC */
2989
2990 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2991 li r5, 0
2992 mtmsrd r5, 1
2993
2994 /* Load GPRs r0-r28 */
2995 reg = 0
2996 .rept 29
2997 ld reg, VCPU_GPRS_TM(reg)(r31)
2998 reg = reg + 1
2999 .endr
3000
3001 mtspr SPRN_DSCR, r29
3002 mtspr SPRN_PPR, r30
3003
3004 /* Load final GPRs */
3005 ld 29, VCPU_GPRS_TM(29)(r31)
3006 ld 30, VCPU_GPRS_TM(30)(r31)
3007 ld 31, VCPU_GPRS_TM(31)(r31)
3008
3009 /* TM checkpointed state is now setup. All GPRs are now volatile. */
3010 TRECHKPT
3011
3012 /* Now let's get back the state we need. */
3013 HMT_MEDIUM
3014 GET_PACA(r13)
3015 ld r29, HSTATE_DSCR(r13)
3016 mtspr SPRN_DSCR, r29
3017 ld r4, HSTATE_KVM_VCPU(r13)
3018 ld r1, HSTATE_HOST_R1(r13)
3019 ld r2, PACATMSCRATCH(r13)
3020
3021 /* Set the MSR RI since we have our registers back. */
3022 li r5, MSR_RI
3023 mtmsrd r5, 1
3024
3025 ld r0, PPC_LR_STKOFF(r1)
3026 mtlr r0
3027 blr
3028#endif
3029
Paul Mackerras44a3add2013-10-04 21:45:04 +10003030/*
3031 * We come here if we get any exception or interrupt while we are
3032 * executing host real mode code while in guest MMU context.
3033 * For now just spin, but we should do something better.
3034 */
3035kvmppc_bad_host_intr:
3036 b .
Michael Neulinge4e38122014-03-25 10:47:02 +11003037
3038/*
3039 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
3040 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3041 * r11 has the guest MSR value (in/out)
3042 * r9 has a vcpu pointer (in)
3043 * r0 is used as a scratch register
3044 */
3045kvmppc_msr_interrupt:
3046 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
3047 cmpwi r0, 2 /* Check if we are in transactional state.. */
3048 ld r11, VCPU_INTR_MSR(r9)
3049 bne 1f
3050 /* ... if transactional, change to suspended */
3051 li r0, 1
30521: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3053 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10003054
3055/*
3056 * This works around a hardware bug on POWER8E processors, where
3057 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3058 * performance monitor interrupt. Instead, when we need to have
3059 * an interrupt pending, we have to arrange for a counter to overflow.
3060 */
3061kvmppc_fix_pmao:
3062 li r3, 0
3063 mtspr SPRN_MMCR2, r3
3064 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3065 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3066 mtspr SPRN_MMCR0, r3
3067 lis r3, 0x7fff
3068 ori r3, r3, 0xffff
3069 mtspr SPRN_PMC6, r3
3070 isync
3071 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003072
3073#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3074/*
3075 * Start timing an activity
3076 * r3 = pointer to time accumulation struct, r4 = vcpu
3077 */
3078kvmhv_start_timing:
3079 ld r5, HSTATE_KVM_VCORE(r13)
3080 lbz r6, VCORE_IN_GUEST(r5)
3081 cmpwi r6, 0
3082 beq 5f /* if in guest, need to */
3083 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
30845: mftb r5
3085 subf r5, r6, r5
3086 std r3, VCPU_CUR_ACTIVITY(r4)
3087 std r5, VCPU_ACTIVITY_START(r4)
3088 blr
3089
3090/*
3091 * Accumulate time to one activity and start another.
3092 * r3 = pointer to new time accumulation struct, r4 = vcpu
3093 */
3094kvmhv_accumulate_time:
3095 ld r5, HSTATE_KVM_VCORE(r13)
3096 lbz r8, VCORE_IN_GUEST(r5)
3097 cmpwi r8, 0
3098 beq 4f /* if in guest, need to */
3099 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
31004: ld r5, VCPU_CUR_ACTIVITY(r4)
3101 ld r6, VCPU_ACTIVITY_START(r4)
3102 std r3, VCPU_CUR_ACTIVITY(r4)
3103 mftb r7
3104 subf r7, r8, r7
3105 std r7, VCPU_ACTIVITY_START(r4)
3106 cmpdi r5, 0
3107 beqlr
3108 subf r3, r6, r7
3109 ld r8, TAS_SEQCOUNT(r5)
3110 cmpdi r8, 0
3111 addi r8, r8, 1
3112 std r8, TAS_SEQCOUNT(r5)
3113 lwsync
3114 ld r7, TAS_TOTAL(r5)
3115 add r7, r7, r3
3116 std r7, TAS_TOTAL(r5)
3117 ld r6, TAS_MIN(r5)
3118 ld r7, TAS_MAX(r5)
3119 beq 3f
3120 cmpd r3, r6
3121 bge 1f
31223: std r3, TAS_MIN(r5)
31231: cmpd r3, r7
3124 ble 2f
3125 std r3, TAS_MAX(r5)
31262: lwsync
3127 addi r8, r8, 1
3128 std r8, TAS_SEQCOUNT(r5)
3129 blr
3130#endif