blob: bd0b623335af1a3221a04ccd345af9ccd50ef9f2 [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>
Paul Mackerras857b99e2017-09-01 16:17:27 +100034#include <asm/thread_info.h>
Michael Neulinge4e38122014-03-25 10:47:02 +110035
Paul Mackerras2f272462017-05-22 16:25:14 +100036/* Sign-extend HDEC if not on POWER9 */
37#define EXTEND_HDEC(reg) \
38BEGIN_FTR_SECTION; \
39 extsw reg, reg; \
40END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
41
Michael Neulinge4e38122014-03-25 10:47:02 +110042#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
Paul Mackerrasde56a942011-06-29 00:21:34 +000043
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110044/* Values in HSTATE_NAPPING(r13) */
45#define NAPPING_CEDE 1
46#define NAPPING_NOVCPU 2
47
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100048/* Stack frame offsets for kvmppc_hv_entry */
Paul Mackerras769377f2017-02-15 14:30:17 +110049#define SFS 160
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100050#define STACK_SLOT_TRAP (SFS-4)
51#define STACK_SLOT_TID (SFS-16)
52#define STACK_SLOT_PSSCR (SFS-24)
53#define STACK_SLOT_PID (SFS-32)
54#define STACK_SLOT_IAMR (SFS-40)
55#define STACK_SLOT_CIABR (SFS-48)
56#define STACK_SLOT_DAWR (SFS-56)
57#define STACK_SLOT_DAWRX (SFS-64)
Paul Mackerras769377f2017-02-15 14:30:17 +110058#define STACK_SLOT_HFSCR (SFS-72)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +100059
Paul Mackerrasde56a942011-06-29 00:21:34 +000060/*
Paul Mackerras19ccb762011-07-23 17:42:46 +100061 * Call kvmppc_hv_entry in real mode.
Paul Mackerrasde56a942011-06-29 00:21:34 +000062 * Must be called with interrupts hard-disabled.
63 *
64 * Input Registers:
65 *
66 * LR = return address to continue at after eventually re-enabling MMU
67 */
Anton Blanchard6ed179b2014-06-12 18:16:53 +100068_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
Paul Mackerras218309b2013-09-06 13:23:44 +100069 mflr r0
70 std r0, PPC_LR_STKOFF(r1)
71 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +000072 mfmsr r10
Paul Mackerras8b24e692017-06-26 15:45:51 +100073 std r10, HSTATE_HOST_MSR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100074 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
Paul Mackerrasde56a942011-06-29 00:21:34 +000075 li r0,MSR_RI
76 andc r0,r10,r0
77 li r6,MSR_IR | MSR_DR
78 andc r6,r10,r6
79 mtmsrd r0,1 /* clear RI in MSR */
80 mtsrr0 r5
81 mtsrr1 r6
82 RFI
83
Paul Mackerras218309b2013-09-06 13:23:44 +100084kvmppc_call_hv_entry:
Paul Mackerrasc0101502017-10-19 14:11:23 +110085BEGIN_FTR_SECTION
86 /* On P9, do LPCR setting, if necessary */
87 ld r3, HSTATE_SPLIT_MODE(r13)
88 cmpdi r3, 0
89 beq 46f
90 lwz r4, KVM_SPLIT_DO_SET(r3)
91 cmpwi r4, 0
92 beq 46f
93 bl kvmhv_p9_set_lpcr
94 nop
9546:
96END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
97
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110098 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100099 bl kvmppc_hv_entry
100
101 /* Back from guest - restore host state and return to caller */
102
Michael Neulingeee7ff92014-01-08 21:25:19 +1100103BEGIN_FTR_SECTION
Paul Mackerras218309b2013-09-06 13:23:44 +1000104 /* Restore host DABR and DABRX */
105 ld r5,HSTATE_DABR(r13)
106 li r6,7
107 mtspr SPRN_DABR,r5
108 mtspr SPRN_DABRX,r6
Michael Neulingeee7ff92014-01-08 21:25:19 +1100109END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000110
111 /* Restore SPRG3 */
Scott Wood9d378df2014-03-10 17:29:38 -0500112 ld r3,PACA_SPRG_VDSO(r13)
113 mtspr SPRN_SPRG_VDSO_WRITE,r3
Paul Mackerras218309b2013-09-06 13:23:44 +1000114
Paul Mackerras218309b2013-09-06 13:23:44 +1000115 /* Reload the host's PMU registers */
116 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
117 lbz r4, LPPACA_PMCINUSE(r3)
118 cmpwi r4, 0
119 beq 23f /* skip if not */
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000120BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000121 ld r3, HSTATE_MMCR0(r13)
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000122 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
123 cmpwi r4, MMCR0_PMAO
124 beql kvmppc_fix_pmao
125END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000126 lwz r3, HSTATE_PMC1(r13)
127 lwz r4, HSTATE_PMC2(r13)
128 lwz r5, HSTATE_PMC3(r13)
129 lwz r6, HSTATE_PMC4(r13)
130 lwz r8, HSTATE_PMC5(r13)
131 lwz r9, HSTATE_PMC6(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000132 mtspr SPRN_PMC1, r3
133 mtspr SPRN_PMC2, r4
134 mtspr SPRN_PMC3, r5
135 mtspr SPRN_PMC4, r6
136 mtspr SPRN_PMC5, r8
137 mtspr SPRN_PMC6, r9
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000138 ld r3, HSTATE_MMCR0(r13)
139 ld r4, HSTATE_MMCR1(r13)
140 ld r5, HSTATE_MMCRA(r13)
141 ld r6, HSTATE_SIAR(r13)
142 ld r7, HSTATE_SDAR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000143 mtspr SPRN_MMCR1, r4
144 mtspr SPRN_MMCRA, r5
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100145 mtspr SPRN_SIAR, r6
146 mtspr SPRN_SDAR, r7
147BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000148 ld r8, HSTATE_MMCR2(r13)
149 ld r9, HSTATE_SIER(r13)
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100150 mtspr SPRN_MMCR2, r8
151 mtspr SPRN_SIER, r9
152END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000153 mtspr SPRN_MMCR0, r3
154 isync
15523:
156
157 /*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100158 * Reload DEC. HDEC interrupts were disabled when
159 * we reloaded the host's LPCR value.
160 */
161 ld r3, HSTATE_DECEXP(r13)
162 mftb r4
163 subf r4, r4, r3
164 mtspr SPRN_DEC, r4
165
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000166 /* hwthread_req may have got set by cede or no vcpu, so clear it */
167 li r0, 0
168 stb r0, HSTATE_HWTHREAD_REQ(r13)
169
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100170 /*
Aravinda Prasade20bbd32017-05-11 16:33:37 +0530171 * For external interrupts we need to call the Linux
172 * handler to process the interrupt. We do that by jumping
173 * to absolute address 0x500 for external interrupts.
174 * The [h]rfid at the end of the handler will return to
175 * the book3s_hv_interrupts.S code. For other interrupts
176 * we do the rfid to get back to the book3s_hv_interrupts.S
177 * code here.
Paul Mackerras218309b2013-09-06 13:23:44 +1000178 */
179 ld r8, 112+PPC_LR_STKOFF(r1)
180 addi r1, r1, 112
181 ld r7, HSTATE_HOST_MSR(r13)
182
Paul Mackerras8b24e692017-06-26 15:45:51 +1000183 /* Return the trap number on this thread as the return value */
184 mr r3, r12
185
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100186 /*
187 * If we came back from the guest via a relocation-on interrupt,
188 * we will be in virtual mode at this point, which makes it a
189 * little easier to get back to the caller.
190 */
191 mfmsr r0
192 andi. r0, r0, MSR_IR /* in real mode? */
193 bne .Lvirt_return
194
Paul Mackerras8b24e692017-06-26 15:45:51 +1000195 /* RFI into the highmem handler */
Paul Mackerras218309b2013-09-06 13:23:44 +1000196 mfmsr r6
197 li r0, MSR_RI
198 andc r6, r6, r0
199 mtmsrd r6, 1 /* Clear RI in MSR */
200 mtsrr0 r8
201 mtsrr1 r7
Paul Mackerras218309b2013-09-06 13:23:44 +1000202 RFI
203
Paul Mackerras8b24e692017-06-26 15:45:51 +1000204 /* Virtual-mode return */
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100205.Lvirt_return:
Paul Mackerras8b24e692017-06-26 15:45:51 +1000206 mtlr r8
Paul Mackerras53af3ba2017-01-30 21:21:51 +1100207 blr
208
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100209kvmppc_primary_no_guest:
210 /* We handle this much like a ceded vcpu */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100211 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
Paul Mackerras2f272462017-05-22 16:25:14 +1000212 /* HDEC may be larger than DEC for arch >= v3.00, but since the */
213 /* HDEC value came from DEC in the first place, it will fit */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100214 mfspr r3, SPRN_HDEC
215 mtspr SPRN_DEC, r3
Paul Mackerras6af27c82015-03-28 14:21:10 +1100216 /*
217 * Make sure the primary has finished the MMU switch.
218 * We should never get here on a secondary thread, but
219 * check it for robustness' sake.
220 */
221 ld r5, HSTATE_KVM_VCORE(r13)
22265: lbz r0, VCORE_IN_GUEST(r5)
223 cmpwi r0, 0
224 beq 65b
225 /* Set LPCR. */
226 ld r8,VCORE_LPCR(r5)
227 mtspr SPRN_LPCR,r8
228 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100229 /* set our bit in napping_threads */
230 ld r5, HSTATE_KVM_VCORE(r13)
231 lbz r7, HSTATE_PTID(r13)
232 li r0, 1
233 sld r0, r0, r7
234 addi r6, r5, VCORE_NAPPING_THREADS
2351: lwarx r3, 0, r6
236 or r3, r3, r0
237 stwcx. r3, 0, r6
238 bne 1b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100239 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100240 isync
241 li r12, 0
242 lwz r7, VCORE_ENTRY_EXIT(r5)
243 cmpwi r7, 0x100
244 bge kvm_novcpu_exit /* another thread already exiting */
245 li r3, NAPPING_NOVCPU
246 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100247
Paul Mackerrasccc07772015-03-28 14:21:07 +1100248 li r3, 0 /* Don't wake on privileged (OS) doorbell */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100249 b kvm_do_nap
250
Suresh Warrier37f55d32016-08-19 15:35:46 +1000251/*
252 * kvm_novcpu_wakeup
253 * Entered from kvm_start_guest if kvm_hstate.napping is set
254 * to NAPPING_NOVCPU
255 * r2 = kernel TOC
256 * r13 = paca
257 */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100258kvm_novcpu_wakeup:
259 ld r1, HSTATE_HOST_R1(r13)
260 ld r5, HSTATE_KVM_VCORE(r13)
261 li r0, 0
262 stb r0, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100263
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100264 /* check the wake reason */
265 bl kvmppc_check_wake_reason
Paul Mackerras6af27c82015-03-28 14:21:10 +1100266
Suresh Warrier37f55d32016-08-19 15:35:46 +1000267 /*
268 * Restore volatile registers since we could have called
269 * a C routine in kvmppc_check_wake_reason.
270 * r5 = VCORE
271 */
272 ld r5, HSTATE_KVM_VCORE(r13)
273
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100274 /* see if any other thread is already exiting */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100275 lwz r0, VCORE_ENTRY_EXIT(r5)
276 cmpwi r0, 0x100
277 bge kvm_novcpu_exit
278
279 /* clear our bit in napping_threads */
280 lbz r7, HSTATE_PTID(r13)
281 li r0, 1
282 sld r0, r0, r7
283 addi r6, r5, VCORE_NAPPING_THREADS
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002844: lwarx r7, 0, r6
285 andc r7, r7, r0
286 stwcx. r7, 0, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100287 bne 4b
288
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100289 /* See if the wake reason means we need to exit */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100290 cmpdi r3, 0
291 bge kvm_novcpu_exit
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100292
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100293 /* See if our timeslice has expired (HDEC is negative) */
294 mfspr r0, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +1000295 EXTEND_HDEC(r0)
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100296 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerras2f272462017-05-22 16:25:14 +1000297 cmpdi r0, 0
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100298 blt kvm_novcpu_exit
299
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100300 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
301 ld r4, HSTATE_KVM_VCPU(r13)
302 cmpdi r4, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100303 beq kvmppc_primary_no_guest
304
305#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
306 addi r3, r4, VCPU_TB_RMENTRY
307 bl kvmhv_start_timing
308#endif
309 b kvmppc_got_guest
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100310
311kvm_novcpu_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100312#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
313 ld r4, HSTATE_KVM_VCPU(r13)
314 cmpdi r4, 0
315 beq 13f
316 addi r3, r4, VCPU_TB_RMEXIT
317 bl kvmhv_accumulate_time
318#endif
Paul Mackerraseddb60f2015-03-28 14:21:11 +110031913: mr r3, r12
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000320 stw r12, STACK_SLOT_TRAP(r1)
Paul Mackerraseddb60f2015-03-28 14:21:11 +1100321 bl kvmhv_commence_exit
322 nop
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000323 lwz r12, STACK_SLOT_TRAP(r1)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100324 b kvmhv_switch_to_host
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100325
Paul Mackerras371fefd2011-06-29 00:23:08 +0000326/*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100327 * We come in here when wakened from nap mode.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000328 * Relocation is off and most register values are lost.
329 * r13 points to the PACA.
Nicholas Piggin9d292502017-06-13 23:05:51 +1000330 * r3 contains the SRR1 wakeup value, SRR1 is trashed.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000331 */
332 .globl kvm_start_guest
333kvm_start_guest:
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530334 /* Set runlatch bit the minute you wake up from nap */
Paul Mackerras1f09c3e2015-03-28 14:21:04 +1100335 mfspr r0, SPRN_CTRLF
336 ori r0, r0, 1
337 mtspr SPRN_CTRLT, r0
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530338
Nicholas Piggin9d292502017-06-13 23:05:51 +1000339 /*
340 * Could avoid this and pass it through in r3. For now,
341 * code expects it to be in SRR1.
342 */
343 mtspr SPRN_SRR1,r3
344
Paul Mackerras19ccb762011-07-23 17:42:46 +1000345 ld r2,PACATOC(r13)
346
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000347 li r0,KVM_HWTHREAD_IN_KVM
348 stb r0,HSTATE_HWTHREAD_STATE(r13)
349
350 /* NV GPR values from power7_idle() will no longer be valid */
351 li r0,1
352 stb r0,PACA_NAPSTATELOST(r13)
353
Paul Mackerras4619ac82013-04-17 20:31:41 +0000354 /* were we napping due to cede? */
355 lbz r0,HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100356 cmpwi r0,NAPPING_CEDE
357 beq kvm_end_cede
358 cmpwi r0,NAPPING_NOVCPU
359 beq kvm_novcpu_wakeup
360
361 ld r1,PACAEMERGSP(r13)
362 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras4619ac82013-04-17 20:31:41 +0000363
364 /*
365 * We weren't napping due to cede, so this must be a secondary
366 * thread being woken up to run a guest, or being woken up due
367 * to a stray IPI. (Or due to some machine check or hypervisor
368 * maintenance interrupt while the core is in KVM.)
369 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000370
371 /* Check the wake reason in SRR1 to see why we got here */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100372 bl kvmppc_check_wake_reason
Suresh Warrier37f55d32016-08-19 15:35:46 +1000373 /*
374 * kvmppc_check_wake_reason could invoke a C routine, but we
375 * have no volatile registers to restore when we return.
376 */
377
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100378 cmpdi r3, 0
379 bge kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000380
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000381 /* get vcore pointer, NULL if we have nothing to run */
382 ld r5,HSTATE_KVM_VCORE(r13)
383 cmpdi r5,0
384 /* if we have no vcore to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000385 beq kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000386
Paul Mackerras56548fc2014-12-03 14:48:40 +1100387kvm_secondary_got_guest:
388
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100389 /* Set HSTATE_DSCR(r13) to something sensible */
Anshuman Khandual1db36522015-05-21 12:13:03 +0530390 ld r6, PACA_DSCR_DEFAULT(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100391 std r6, HSTATE_DSCR(r13)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000392
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000393 /* On thread 0 of a subcore, set HDEC to max */
394 lbz r4, HSTATE_PTID(r13)
395 cmpwi r4, 0
396 bne 63f
Paul Mackerras2f272462017-05-22 16:25:14 +1000397 LOAD_REG_ADDR(r6, decrementer_max)
398 ld r6, 0(r6)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000399 mtspr SPRN_HDEC, r6
400 /* and set per-LPAR registers, if doing dynamic micro-threading */
401 ld r6, HSTATE_SPLIT_MODE(r13)
402 cmpdi r6, 0
403 beq 63f
Paul Mackerrasc0101502017-10-19 14:11:23 +1100404BEGIN_FTR_SECTION
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000405 ld r0, KVM_SPLIT_RPR(r6)
406 mtspr SPRN_RPR, r0
407 ld r0, KVM_SPLIT_PMMAR(r6)
408 mtspr SPRN_PMMAR, r0
409 ld r0, KVM_SPLIT_LDBAR(r6)
410 mtspr SPRN_LDBAR, r0
411 isync
Paul Mackerrasc0101502017-10-19 14:11:23 +1100412FTR_SECTION_ELSE
413 /* On P9 we use the split_info for coordinating LPCR changes */
414 lwz r4, KVM_SPLIT_DO_SET(r6)
415 cmpwi r4, 0
416 beq 63f
417 mr r3, r6
418 bl kvmhv_p9_set_lpcr
419 nop
420ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasb4deba52015-07-02 20:38:16 +100042163:
422 /* Order load of vcpu after load of vcore */
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100423 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000424 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100425 bl kvmppc_hv_entry
Paul Mackerras218309b2013-09-06 13:23:44 +1000426
427 /* Back from the guest, go back to nap */
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000428 /* Clear our vcpu and vcore pointers so we don't come back in early */
Paul Mackerras218309b2013-09-06 13:23:44 +1000429 li r0, 0
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000430 std r0, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100431 /*
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000432 * Once we clear HSTATE_KVM_VCORE(r13), the code in
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100433 * kvmppc_run_core() is going to assume that all our vcpu
434 * state is visible in memory. This lwsync makes sure
435 * that that is true.
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100436 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000437 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000438 std r0, HSTATE_KVM_VCORE(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000439
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530440 /*
441 * All secondaries exiting guest will fall through this path.
442 * Before proceeding, just check for HMI interrupt and
443 * invoke opal hmi handler. By now we are sure that the
444 * primary thread on this core/subcore has already made partition
445 * switch/TB resync and we are good to call opal hmi handler.
446 */
447 cmpwi r12, BOOK3S_INTERRUPT_HMI
448 bne kvm_no_guest
449
450 li r3,0 /* NULL argument */
451 bl hmi_exception_realmode
Paul Mackerras56548fc2014-12-03 14:48:40 +1100452/*
453 * At this point we have finished executing in the guest.
454 * We need to wait for hwthread_req to become zero, since
455 * we may not turn on the MMU while hwthread_req is non-zero.
456 * While waiting we also need to check if we get given a vcpu to run.
457 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000458kvm_no_guest:
Paul Mackerras56548fc2014-12-03 14:48:40 +1100459 lbz r3, HSTATE_HWTHREAD_REQ(r13)
460 cmpwi r3, 0
461 bne 53f
462 HMT_MEDIUM
463 li r0, KVM_HWTHREAD_IN_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000464 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerras56548fc2014-12-03 14:48:40 +1100465 /* need to recheck hwthread_req after a barrier, to avoid race */
466 sync
467 lbz r3, HSTATE_HWTHREAD_REQ(r13)
468 cmpwi r3, 0
469 bne 54f
470/*
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530471 * We jump to pnv_wakeup_loss, which will return to the caller
Paul Mackerras56548fc2014-12-03 14:48:40 +1100472 * of power7_nap in the powernv cpu offline loop. The value we
Nicholas Piggin9d292502017-06-13 23:05:51 +1000473 * put in r3 becomes the return value for power7_nap. pnv_wakeup_loss
474 * requires SRR1 in r12.
Paul Mackerras56548fc2014-12-03 14:48:40 +1100475 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000476 li r3, LPCR_PECE0
477 mfspr r4, SPRN_LPCR
478 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
479 mtspr SPRN_LPCR, r4
Paul Mackerras56548fc2014-12-03 14:48:40 +1100480 li r3, 0
Nicholas Piggin9d292502017-06-13 23:05:51 +1000481 mfspr r12,SPRN_SRR1
Shreyas B. Prabhu5fa6b6b2016-07-08 11:50:46 +0530482 b pnv_wakeup_loss
Paul Mackerras56548fc2014-12-03 14:48:40 +1100483
48453: HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000485 ld r5, HSTATE_KVM_VCORE(r13)
486 cmpdi r5, 0
487 bne 60f
488 ld r3, HSTATE_SPLIT_MODE(r13)
489 cmpdi r3, 0
490 beq kvm_no_guest
Paul Mackerrasc0101502017-10-19 14:11:23 +1100491 lwz r0, KVM_SPLIT_DO_SET(r3)
492 cmpwi r0, 0
493 bne kvmhv_do_set
494 lwz r0, KVM_SPLIT_DO_RESTORE(r3)
495 cmpwi r0, 0
496 bne kvmhv_do_restore
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000497 lbz r0, KVM_SPLIT_DO_NAP(r3)
498 cmpwi r0, 0
Paul Mackerras56548fc2014-12-03 14:48:40 +1100499 beq kvm_no_guest
500 HMT_MEDIUM
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000501 b kvm_unsplit_nap
50260: HMT_MEDIUM
Paul Mackerras56548fc2014-12-03 14:48:40 +1100503 b kvm_secondary_got_guest
504
50554: li r0, KVM_HWTHREAD_IN_KVM
506 stb r0, HSTATE_HWTHREAD_STATE(r13)
507 b kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000508
Paul Mackerrasc0101502017-10-19 14:11:23 +1100509kvmhv_do_set:
510 /* Set LPCR, LPIDR etc. on P9 */
511 HMT_MEDIUM
512 bl kvmhv_p9_set_lpcr
513 nop
514 b kvm_no_guest
515
516kvmhv_do_restore:
517 HMT_MEDIUM
518 bl kvmhv_p9_restore_lpcr
519 nop
520 b kvm_no_guest
521
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000522/*
523 * Here the primary thread is trying to return the core to
524 * whole-core mode, so we need to nap.
525 */
526kvm_unsplit_nap:
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530527 /*
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530528 * When secondaries are napping in kvm_unsplit_nap() with
529 * hwthread_req = 1, HMI goes ignored even though subcores are
530 * already exited the guest. Hence HMI keeps waking up secondaries
531 * from nap in a loop and secondaries always go back to nap since
532 * no vcore is assigned to them. This makes impossible for primary
533 * thread to get hold of secondary threads resulting into a soft
534 * lockup in KVM path.
535 *
536 * Let us check if HMI is pending and handle it before we go to nap.
537 */
538 cmpwi r12, BOOK3S_INTERRUPT_HMI
539 bne 55f
540 li r3, 0 /* NULL argument */
541 bl hmi_exception_realmode
54255:
543 /*
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530544 * Ensure that secondary doesn't nap when it has
545 * its vcore pointer set.
546 */
547 sync /* matches smp_mb() before setting split_info.do_nap */
548 ld r0, HSTATE_KVM_VCORE(r13)
549 cmpdi r0, 0
550 bne kvm_no_guest
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000551 /* clear any pending message */
552BEGIN_FTR_SECTION
553 lis r6, (PPC_DBELL_SERVER << (63-36))@h
554 PPC_MSGCLR(6)
555END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
556 /* Set kvm_split_mode.napped[tid] = 1 */
557 ld r3, HSTATE_SPLIT_MODE(r13)
558 li r0, 1
Paul Mackerrasc0101502017-10-19 14:11:23 +1100559 lbz r4, HSTATE_TID(r13)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000560 addi r4, r4, KVM_SPLIT_NAPPED
561 stbx r0, r3, r4
562 /* Check the do_nap flag again after setting napped[] */
563 sync
564 lbz r0, KVM_SPLIT_DO_NAP(r3)
565 cmpwi r0, 0
566 beq 57f
567 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
Paul Mackerrasbf53c882016-11-18 14:34:07 +1100568 mfspr r5, SPRN_LPCR
569 rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
570 b kvm_nap_sequence
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000571
57257: li r0, 0
573 stbx r0, r3, r4
574 b kvm_no_guest
575
Paul Mackerras218309b2013-09-06 13:23:44 +1000576/******************************************************************************
577 * *
578 * Entry code *
579 * *
580 *****************************************************************************/
581
Paul Mackerrasde56a942011-06-29 00:21:34 +0000582.global kvmppc_hv_entry
583kvmppc_hv_entry:
584
585 /* Required state:
586 *
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100587 * R4 = vcpu pointer (or NULL)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000588 * MSR = ~IR|DR
589 * R13 = PACA
590 * R1 = host R1
Michael Neuling06a29e42014-08-19 14:59:30 +1000591 * R2 = TOC
Paul Mackerrasde56a942011-06-29 00:21:34 +0000592 * all other volatile GPRS = free
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100593 * Does not preserve non-volatile GPRs or CR fields
Paul Mackerrasde56a942011-06-29 00:21:34 +0000594 */
595 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000596 std r0, PPC_LR_STKOFF(r1)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000597 stdu r1, -SFS(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000598
Paul Mackerrasde56a942011-06-29 00:21:34 +0000599 /* Save R1 in the PACA */
600 std r1, HSTATE_HOST_R1(r13)
601
Paul Mackerras44a3add2013-10-04 21:45:04 +1000602 li r6, KVM_GUEST_MODE_HOST_HV
603 stb r6, HSTATE_IN_GUEST(r13)
604
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100605#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
606 /* Store initial timestamp */
607 cmpdi r4, 0
608 beq 1f
609 addi r3, r4, VCPU_TB_RMENTRY
610 bl kvmhv_start_timing
6111:
612#endif
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100613
614 /* Use cr7 as an indication of radix mode */
615 ld r5, HSTATE_KVM_VCORE(r13)
616 ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
617 lbz r0, KVM_RADIX(r9)
618 cmpwi cr7, r0, 0
619
620 /* Clear out SLB if hash */
621 bne cr7, 2f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000622 li r6,0
623 slbmte r6,r6
624 slbia
625 ptesync
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11006262:
Paul Mackerras9e368f22011-06-29 00:40:08 +0000627 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100628 * POWER7/POWER8 host -> guest partition switch code.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000629 * We don't have to lock against concurrent tlbies,
630 * but we do have to coordinate across hardware threads.
631 */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100632 /* Set bit in entry map iff exit map is zero. */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100633 li r7, 1
634 lbz r6, HSTATE_PTID(r13)
635 sld r7, r7, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100636 addi r8, r5, VCORE_ENTRY_EXIT
63721: lwarx r3, 0, r8
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100638 cmpwi r3, 0x100 /* any threads starting to exit? */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000639 bge secondary_too_late /* if so we're too late to the party */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100640 or r3, r3, r7
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100641 stwcx. r3, 0, r8
Paul Mackerras371fefd2011-06-29 00:23:08 +0000642 bne 21b
643
644 /* Primary thread switches to guest partition. */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000645 cmpwi r6,0
Paul Mackerras6af27c82015-03-28 14:21:10 +1100646 bne 10f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000647 lwz r7,KVM_LPID(r9)
Paul Mackerras7a840842016-11-16 22:25:20 +1100648BEGIN_FTR_SECTION
649 ld r6,KVM_SDR1(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000650 li r0,LPID_RSVD /* switch to reserved LPID */
651 mtspr SPRN_LPID,r0
652 ptesync
653 mtspr SPRN_SDR1,r6 /* switch to partition page table */
Paul Mackerras7a840842016-11-16 22:25:20 +1100654END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000655 mtspr SPRN_LPID,r7
656 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000657
658 /* See if we need to flush the TLB */
659 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100660BEGIN_FTR_SECTION
661 /*
662 * On POWER9, individual threads can come in here, but the
663 * TLB is shared between the 4 threads in a core, hence
664 * invalidating on one thread invalidates for all.
665 * Thus we make all 4 threads use the same bit here.
666 */
667 clrrdi r6,r6,2
668END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000669 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
670 srdi r6,r6,6 /* doubleword number */
671 sldi r6,r6,3 /* address offset */
672 add r6,r6,r9
673 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100674 li r8,1
675 sld r8,r8,r7
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000676 ld r7,0(r6)
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100677 and. r7,r7,r8
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000678 beq 22f
Paul Mackerrasca252052014-01-08 21:25:22 +1100679 /* Flush the TLB of any entries for this LPID */
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100680 lwz r0,KVM_TLB_SETS(r9)
681 mtctr r0
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000682 li r7,0x800 /* IS field = 0b10 */
683 ptesync
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100684 li r0,0 /* RS for P9 version of tlbiel */
685 bne cr7, 29f
68628: tlbiel r7 /* On P9, rs=0, RIC=0, PRS=0, R=0 */
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000687 addi r7,r7,0x1000
688 bdnz 28b
Paul Mackerrasa29ebea2017-01-30 21:21:50 +1100689 b 30f
69029: PPC_TLBIEL(7,0,2,1,1) /* for radix, RIC=2, PRS=1, R=1 */
691 addi r7,r7,0x1000
692 bdnz 29b
69330: ptesync
69423: ldarx r7,0,r6 /* clear the bit after TLB flushed */
695 andc r7,r7,r8
696 stdcx. r7,0,r6
697 bne 23b
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000698
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000699 /* Add timebase offset onto timebase */
70022: ld r8,VCORE_TB_OFFSET(r5)
701 cmpdi r8,0
702 beq 37f
703 mftb r6 /* current host timebase */
704 add r8,r8,r6
705 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
706 mftb r7 /* check if lower 24 bits overflowed */
707 clrldi r6,r6,40
708 clrldi r7,r7,40
709 cmpld r7,r6
710 bge 37f
711 addis r8,r8,0x100 /* if so, increment upper 40 bits */
712 mtspr SPRN_TBU40,r8
713
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000714 /* Load guest PCR value to select appropriate compat mode */
71537: ld r7, VCORE_PCR(r5)
716 cmpdi r7, 0
717 beq 38f
718 mtspr SPRN_PCR, r7
71938:
Michael Neulingb005255e2014-01-08 21:25:21 +1100720
721BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000722 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +1100723 ld r8, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000724 ld r7, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +1100725 mtspr SPRN_DPDES, r8
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000726 mtspr SPRN_VTB, r7
Michael Neulingb005255e2014-01-08 21:25:21 +1100727END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
728
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530729 /* Mark the subcore state as inside guest */
730 bl kvmppc_subcore_enter_guest
731 nop
732 ld r5, HSTATE_KVM_VCORE(r13)
733 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000734 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000735 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000736
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100737 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110073810: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100739 beq kvmppc_primary_no_guest
740kvmppc_got_guest:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000741
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100742 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100743 lwz r5,VCPU_SLB_MAX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000744 cmpwi r5,0
745 beq 9f
746 mtctr r5
747 addi r6,r4,VCPU_SLB
7481: ld r8,VCPU_SLB_E(r6)
749 ld r9,VCPU_SLB_V(r6)
750 slbmte r9,r8
751 addi r6,r6,VCPU_SLB_SIZE
752 bdnz 1b
7539:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100754 /* Increment yield count if they have a VPA */
755 ld r3, VCPU_VPA(r4)
756 cmpdi r3, 0
757 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200758 li r6, LPPACA_YIELDCOUNT
759 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100760 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200761 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100762 li r6, 1
763 stb r6, VCPU_VPA_DIRTY(r4)
76425:
765
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100766 /* Save purr/spurr */
767 mfspr r5,SPRN_PURR
768 mfspr r6,SPRN_SPURR
769 std r5,HSTATE_PURR(r13)
770 std r6,HSTATE_SPURR(r13)
771 ld r7,VCPU_PURR(r4)
772 ld r8,VCPU_SPURR(r4)
773 mtspr SPRN_PURR,r7
774 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100775
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100776 /* Save host values of some registers */
777BEGIN_FTR_SECTION
778 mfspr r5, SPRN_TIDR
779 mfspr r6, SPRN_PSSCR
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100780 mfspr r7, SPRN_PID
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +1000781 mfspr r8, SPRN_IAMR
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100782 std r5, STACK_SLOT_TID(r1)
783 std r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +1100784 std r7, STACK_SLOT_PID(r1)
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +1000785 std r8, STACK_SLOT_IAMR(r1)
Paul Mackerras769377f2017-02-15 14:30:17 +1100786 mfspr r5, SPRN_HFSCR
787 std r5, STACK_SLOT_HFSCR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100788END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +1000789BEGIN_FTR_SECTION
790 mfspr r5, SPRN_CIABR
791 mfspr r6, SPRN_DAWR
792 mfspr r7, SPRN_DAWRX
793 std r5, STACK_SLOT_CIABR(r1)
794 std r6, STACK_SLOT_DAWR(r1)
795 std r7, STACK_SLOT_DAWRX(r1)
796END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100797
Michael Neulingeee7ff92014-01-08 21:25:19 +1100798BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000799 /* Set partition DABR */
800 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100801 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000802 ld r6,VCPU_DABR(r4)
803 mtspr SPRN_DABRX,r5
804 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000805 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100806END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000807
Michael Neulinge4e38122014-03-25 10:47:02 +1100808#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
809BEGIN_FTR_SECTION
Paul Mackerras67f8a8c2017-09-12 13:47:23 +1000810 /*
811 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
812 */
Paul Mackerrasf024ee02016-06-22 14:21:59 +1000813 bl kvmppc_restore_tm
814END_FTR_SECTION_IFSET(CPU_FTR_TM)
Michael Neulinge4e38122014-03-25 10:47:02 +1100815#endif
816
Paul Mackerrasde56a942011-06-29 00:21:34 +0000817 /* Load guest PMU registers */
818 /* R4 is live here (vcpu pointer) */
819 li r3, 1
820 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
821 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
822 isync
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000823BEGIN_FTR_SECTION
824 ld r3, VCPU_MMCR(r4)
825 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
826 cmpwi r5, MMCR0_PMAO
827 beql kvmppc_fix_pmao
828END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000829 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
830 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
831 lwz r6, VCPU_PMC + 8(r4)
832 lwz r7, VCPU_PMC + 12(r4)
833 lwz r8, VCPU_PMC + 16(r4)
834 lwz r9, VCPU_PMC + 20(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000835 mtspr SPRN_PMC1, r3
836 mtspr SPRN_PMC2, r5
837 mtspr SPRN_PMC3, r6
838 mtspr SPRN_PMC4, r7
839 mtspr SPRN_PMC5, r8
840 mtspr SPRN_PMC6, r9
Paul Mackerrasde56a942011-06-29 00:21:34 +0000841 ld r3, VCPU_MMCR(r4)
842 ld r5, VCPU_MMCR + 8(r4)
843 ld r6, VCPU_MMCR + 16(r4)
844 ld r7, VCPU_SIAR(r4)
845 ld r8, VCPU_SDAR(r4)
846 mtspr SPRN_MMCR1, r5
847 mtspr SPRN_MMCRA, r6
848 mtspr SPRN_SIAR, r7
849 mtspr SPRN_SDAR, r8
Michael Neulingb005255e2014-01-08 21:25:21 +1100850BEGIN_FTR_SECTION
851 ld r5, VCPU_MMCR + 24(r4)
852 ld r6, VCPU_SIER(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100853 mtspr SPRN_MMCR2, r5
854 mtspr SPRN_SIER, r6
855BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +1100856 lwz r7, VCPU_PMC + 24(r4)
857 lwz r8, VCPU_PMC + 28(r4)
858 ld r9, VCPU_MMCR + 32(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100859 mtspr SPRN_SPMC1, r7
860 mtspr SPRN_SPMC2, r8
861 mtspr SPRN_MMCRS, r9
Paul Mackerras83677f52016-11-16 22:33:27 +1100862END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +1100863END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000864 mtspr SPRN_MMCR0, r3
865 isync
866
867 /* Load up FP, VMX and VSX registers */
868 bl kvmppc_load_fp
869
870 ld r14, VCPU_GPR(R14)(r4)
871 ld r15, VCPU_GPR(R15)(r4)
872 ld r16, VCPU_GPR(R16)(r4)
873 ld r17, VCPU_GPR(R17)(r4)
874 ld r18, VCPU_GPR(R18)(r4)
875 ld r19, VCPU_GPR(R19)(r4)
876 ld r20, VCPU_GPR(R20)(r4)
877 ld r21, VCPU_GPR(R21)(r4)
878 ld r22, VCPU_GPR(R22)(r4)
879 ld r23, VCPU_GPR(R23)(r4)
880 ld r24, VCPU_GPR(R24)(r4)
881 ld r25, VCPU_GPR(R25)(r4)
882 ld r26, VCPU_GPR(R26)(r4)
883 ld r27, VCPU_GPR(R27)(r4)
884 ld r28, VCPU_GPR(R28)(r4)
885 ld r29, VCPU_GPR(R29)(r4)
886 ld r30, VCPU_GPR(R30)(r4)
887 ld r31, VCPU_GPR(R31)(r4)
888
Paul Mackerrasde56a942011-06-29 00:21:34 +0000889 /* Switch DSCR to guest value */
890 ld r5, VCPU_DSCR(r4)
891 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000892
Michael Neulingb005255e2014-01-08 21:25:21 +1100893BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100894 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100895 b 8f
896END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +1100897 /* Load up POWER8-specific registers */
898 ld r5, VCPU_IAMR(r4)
899 lwz r6, VCPU_PSPB(r4)
900 ld r7, VCPU_FSCR(r4)
901 mtspr SPRN_IAMR, r5
902 mtspr SPRN_PSPB, r6
903 mtspr SPRN_FSCR, r7
904 ld r5, VCPU_DAWR(r4)
905 ld r6, VCPU_DAWRX(r4)
906 ld r7, VCPU_CIABR(r4)
907 ld r8, VCPU_TAR(r4)
908 mtspr SPRN_DAWR, r5
909 mtspr SPRN_DAWRX, r6
910 mtspr SPRN_CIABR, r7
911 mtspr SPRN_TAR, r8
912 ld r5, VCPU_IC(r4)
Michael Neuling7b490412014-01-08 21:25:32 +1100913 ld r8, VCPU_EBBHR(r4)
Paul Mackerras88b02cf92016-09-15 13:42:52 +1000914 mtspr SPRN_IC, r5
Michael Neulingb005255e2014-01-08 21:25:21 +1100915 mtspr SPRN_EBBHR, r8
916 ld r5, VCPU_EBBRR(r4)
917 ld r6, VCPU_BESCR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100918 lwz r7, VCPU_GUEST_PID(r4)
919 ld r8, VCPU_WORT(r4)
Paul Mackerras83677f52016-11-16 22:33:27 +1100920 mtspr SPRN_EBBRR, r5
921 mtspr SPRN_BESCR, r6
Michael Neulingb005255e2014-01-08 21:25:21 +1100922 mtspr SPRN_PID, r7
923 mtspr SPRN_WORT, r8
Paul Mackerras83677f52016-11-16 22:33:27 +1100924BEGIN_FTR_SECTION
Paul Mackerrasf11f6f72017-01-30 21:21:52 +1100925 PPC_INVALIDATE_ERAT
926END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
927BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100928 /* POWER8-only registers */
Paul Mackerras83677f52016-11-16 22:33:27 +1100929 ld r5, VCPU_TCSCR(r4)
930 ld r6, VCPU_ACOP(r4)
931 ld r7, VCPU_CSIGR(r4)
932 ld r8, VCPU_TACR(r4)
933 mtspr SPRN_TCSCR, r5
934 mtspr SPRN_ACOP, r6
935 mtspr SPRN_CSIGR, r7
936 mtspr SPRN_TACR, r8
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100937FTR_SECTION_ELSE
938 /* POWER9-only registers */
939 ld r5, VCPU_TID(r4)
940 ld r6, VCPU_PSSCR(r4)
941 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
Paul Mackerras769377f2017-02-15 14:30:17 +1100942 ld r7, VCPU_HFSCR(r4)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100943 mtspr SPRN_TIDR, r5
944 mtspr SPRN_PSSCR, r6
Paul Mackerras769377f2017-02-15 14:30:17 +1100945 mtspr SPRN_HFSCR, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +1100946ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +11009478:
948
Paul Mackerrasde56a942011-06-29 00:21:34 +0000949 /*
950 * Set the decrementer to the guest decrementer.
951 */
952 ld r8,VCPU_DEC_EXPIRES(r4)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +1100953 /* r8 is a host timebase value here, convert to guest TB */
954 ld r5,HSTATE_KVM_VCORE(r13)
955 ld r6,VCORE_TB_OFFSET(r5)
956 add r8,r8,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000957 mftb r7
958 subf r3,r7,r8
959 mtspr SPRN_DEC,r3
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000960 std r3,VCPU_DEC(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000961
962 ld r5, VCPU_SPRG0(r4)
963 ld r6, VCPU_SPRG1(r4)
964 ld r7, VCPU_SPRG2(r4)
965 ld r8, VCPU_SPRG3(r4)
966 mtspr SPRN_SPRG0, r5
967 mtspr SPRN_SPRG1, r6
968 mtspr SPRN_SPRG2, r7
969 mtspr SPRN_SPRG3, r8
970
Paul Mackerrasde56a942011-06-29 00:21:34 +0000971 /* Load up DAR and DSISR */
972 ld r5, VCPU_DAR(r4)
973 lwz r6, VCPU_DSISR(r4)
974 mtspr SPRN_DAR, r5
975 mtspr SPRN_DSISR, r6
976
Paul Mackerrasde56a942011-06-29 00:21:34 +0000977 /* Restore AMR and UAMOR, set AMOR to all 1s */
978 ld r5,VCPU_AMR(r4)
979 ld r6,VCPU_UAMOR(r4)
980 li r7,-1
981 mtspr SPRN_AMR,r5
982 mtspr SPRN_UAMOR,r6
983 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000984
985 /* Restore state of CTRL run bit; assume 1 on entry */
986 lwz r5,VCPU_CTRL(r4)
987 andi. r5,r5,1
988 bne 4f
989 mfspr r6,SPRN_CTRLF
990 clrrdi r6,r6,1
991 mtspr SPRN_CTRLT,r6
9924:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100993 /* Secondary threads wait for primary to have done partition switch */
994 ld r5, HSTATE_KVM_VCORE(r13)
995 lbz r6, HSTATE_PTID(r13)
996 cmpwi r6, 0
997 beq 21f
998 lbz r0, VCORE_IN_GUEST(r5)
999 cmpwi r0, 0
1000 bne 21f
1001 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000100220: lwz r3, VCORE_ENTRY_EXIT(r5)
1003 cmpwi r3, 0x100
1004 bge no_switch_exit
1005 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001006 cmpwi r0, 0
1007 beq 20b
1008 HMT_MEDIUM
100921:
1010 /* Set LPCR. */
1011 ld r8,VCORE_LPCR(r5)
1012 mtspr SPRN_LPCR,r8
1013 isync
1014
1015 /* Check if HDEC expires soon */
1016 mfspr r3, SPRN_HDEC
Paul Mackerras2f272462017-05-22 16:25:14 +10001017 EXTEND_HDEC(r3)
1018 cmpdi r3, 512 /* 1 microsecond */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001019 blt hdec_soon
1020
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001021#ifdef CONFIG_KVM_XICS
1022 /* We are entering the guest on that thread, push VCPU to XIVE */
1023 ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
Andreas Schwab0bfa33c2017-08-15 14:37:01 +10001024 cmpldi cr0, r10, 0
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001025 beq no_xive
1026 ld r11, VCPU_XIVE_SAVED_STATE(r4)
1027 li r9, TM_QW1_OS
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001028 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001029 stdcix r11,r9,r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001030 lwz r11, VCPU_XIVE_CAM_WORD(r4)
1031 li r9, TM_QW1_OS + TM_WORD2
1032 stwcix r11,r9,r10
1033 li r9, 1
1034 stw r9, VCPU_XIVE_PUSHED(r4)
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001035 eieio
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001036no_xive:
1037#endif /* CONFIG_KVM_XICS */
1038
Suresh Warrier37f55d32016-08-19 15:35:46 +10001039deliver_guest_interrupt:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001040 ld r6, VCPU_CTR(r4)
Sam bobroffc63517c2015-05-27 09:56:57 +10001041 ld r7, VCPU_XER(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001042
1043 mtctr r6
1044 mtxer r7
1045
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001046kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerras4619ac82013-04-17 20:31:41 +00001047 ld r10, VCPU_PC(r4)
1048 ld r11, VCPU_MSR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001049 ld r6, VCPU_SRR0(r4)
1050 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001051 mtspr SPRN_SRR0, r6
1052 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001053
Paul Mackerras4619ac82013-04-17 20:31:41 +00001054 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001055 rldicl r11, r11, 63 - MSR_HV_LG, 1
1056 rotldi r11, r11, 1 + MSR_HV_LG
1057 ori r11, r11, MSR_ME
1058
Paul Mackerras19ccb762011-07-23 17:42:46 +10001059 /* Check if we can deliver an external or decrementer interrupt now */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001060 ld r0, VCPU_PENDING_EXC(r4)
1061 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
1062 cmpdi cr1, r0, 0
1063 andi. r8, r11, MSR_EE
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001064 mfspr r8, SPRN_LPCR
1065 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
1066 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
1067 mtspr SPRN_LPCR, r8
Paul Mackerras19ccb762011-07-23 17:42:46 +10001068 isync
Paul Mackerras19ccb762011-07-23 17:42:46 +10001069 beq 5f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001070 li r0, BOOK3S_INTERRUPT_EXTERNAL
1071 bne cr1, 12f
1072 mfspr r0, SPRN_DEC
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10001073BEGIN_FTR_SECTION
1074 /* On POWER9 check whether the guest has large decrementer enabled */
1075 andis. r8, r8, LPCR_LD@h
1076 bne 15f
1077END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1078 extsw r0, r0
107915: cmpdi r0, 0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001080 li r0, BOOK3S_INTERRUPT_DECREMENTER
1081 bge 5f
1082
108312: mtspr SPRN_SRR0, r10
Paul Mackerras19ccb762011-07-23 17:42:46 +10001084 mr r10,r0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001085 mtspr SPRN_SRR1, r11
Michael Neulinge4e38122014-03-25 10:47:02 +11001086 mr r9, r4
1087 bl kvmppc_msr_interrupt
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +110010885:
Paul Mackerras57900692017-05-16 16:41:20 +10001089BEGIN_FTR_SECTION
1090 b fast_guest_return
1091END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
1092 /* On POWER9, check for pending doorbell requests */
1093 lbz r0, VCPU_DBELL_REQ(r4)
1094 cmpwi r0, 0
1095 beq fast_guest_return
1096 ld r5, HSTATE_KVM_VCORE(r13)
1097 /* Set DPDES register so the CPU will take a doorbell interrupt */
1098 li r0, 1
1099 mtspr SPRN_DPDES, r0
1100 std r0, VCORE_DPDES(r5)
1101 /* Make sure other cpus see vcore->dpdes set before dbell req clear */
1102 lwsync
1103 /* Clear the pending doorbell request */
1104 li r0, 0
1105 stb r0, VCPU_DBELL_REQ(r4)
Paul Mackerras19ccb762011-07-23 17:42:46 +10001106
Liu Ping Fan27025a62013-11-19 14:12:48 +08001107/*
1108 * Required state:
1109 * R4 = vcpu
1110 * R10: value for HSRR0
1111 * R11: value for HSRR1
1112 * R13 = PACA
1113 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001114fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001115 li r0,0
1116 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001117 mtspr SPRN_HSRR0,r10
1118 mtspr SPRN_HSRR1,r11
1119
1120 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001121 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +00001122 stb r9, HSTATE_IN_GUEST(r13)
1123
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001124#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1125 /* Accumulate timing */
1126 addi r3, r4, VCPU_TB_GUEST
1127 bl kvmhv_accumulate_time
1128#endif
1129
Paul Mackerrasde56a942011-06-29 00:21:34 +00001130 /* Enter guest */
1131
Paul Mackerras0acb9112013-02-04 18:10:51 +00001132BEGIN_FTR_SECTION
1133 ld r5, VCPU_CFAR(r4)
1134 mtspr SPRN_CFAR, r5
1135END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001136BEGIN_FTR_SECTION
1137 ld r0, VCPU_PPR(r4)
1138END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001139
Paul Mackerrasde56a942011-06-29 00:21:34 +00001140 ld r5, VCPU_LR(r4)
1141 lwz r6, VCPU_CR(r4)
1142 mtlr r5
1143 mtcr r6
1144
Michael Neulingc75df6f2012-06-25 13:33:10 +00001145 ld r1, VCPU_GPR(R1)(r4)
1146 ld r2, VCPU_GPR(R2)(r4)
1147 ld r3, VCPU_GPR(R3)(r4)
1148 ld r5, VCPU_GPR(R5)(r4)
1149 ld r6, VCPU_GPR(R6)(r4)
1150 ld r7, VCPU_GPR(R7)(r4)
1151 ld r8, VCPU_GPR(R8)(r4)
1152 ld r9, VCPU_GPR(R9)(r4)
1153 ld r10, VCPU_GPR(R10)(r4)
1154 ld r11, VCPU_GPR(R11)(r4)
1155 ld r12, VCPU_GPR(R12)(r4)
1156 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001157
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001158BEGIN_FTR_SECTION
1159 mtspr SPRN_PPR, r0
1160END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Michael Neulinge001fa72017-09-15 15:26:14 +10001161
1162/* Move canary into DSISR to check for later */
1163BEGIN_FTR_SECTION
1164 li r0, 0x7fff
1165 mtspr SPRN_HDSISR, r0
1166END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1167
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001168 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001169 ld r4, VCPU_GPR(R4)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001170
1171 hrfid
1172 b .
1173
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001174secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001175 li r12, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001176 cmpdi r4, 0
1177 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001178 stw r12, VCPU_TRAP(r4)
1179#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001180 addi r3, r4, VCPU_TB_RMEXIT
1181 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001182#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100118311: b kvmhv_switch_to_host
1184
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001185no_switch_exit:
1186 HMT_MEDIUM
1187 li r12, 0
1188 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001189hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001190 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000119112: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001192 mr r9, r4
1193#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001194 addi r3, r4, VCPU_TB_RMEXIT
1195 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001196#endif
Paul Mackerras6af27c82015-03-28 14:21:10 +11001197 b guest_exit_cont
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001198
Paul Mackerrasde56a942011-06-29 00:21:34 +00001199/******************************************************************************
1200 * *
1201 * Exit code *
1202 * *
1203 *****************************************************************************/
1204
1205/*
1206 * We come here from the first-level interrupt handlers.
1207 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301208 .globl kvmppc_interrupt_hv
1209kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001210 /*
1211 * Register contents:
Nicholas Piggind3918e72016-12-22 04:29:25 +10001212 * R12 = (guest CR << 32) | interrupt vector
Paul Mackerrasde56a942011-06-29 00:21:34 +00001213 * R13 = PACA
Nicholas Piggind3918e72016-12-22 04:29:25 +10001214 * guest R12 saved in shadow VCPU SCRATCH0
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001215 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
Paul Mackerrasde56a942011-06-29 00:21:34 +00001216 * guest R13 saved in SPRN_SCRATCH0
1217 */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001218 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001219 lbz r9, HSTATE_IN_GUEST(r13)
1220 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1221 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301222#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1223 cmpwi r9, KVM_GUEST_MODE_GUEST
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001224 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301225 beq kvmppc_interrupt_pr
1226#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001227 /* We're now back in the host but in guest MMU context */
1228 li r9, KVM_GUEST_MODE_HOST_HV
1229 stb r9, HSTATE_IN_GUEST(r13)
1230
Paul Mackerrasde56a942011-06-29 00:21:34 +00001231 ld r9, HSTATE_KVM_VCPU(r13)
1232
1233 /* Save registers */
1234
Michael Neulingc75df6f2012-06-25 13:33:10 +00001235 std r0, VCPU_GPR(R0)(r9)
1236 std r1, VCPU_GPR(R1)(r9)
1237 std r2, VCPU_GPR(R2)(r9)
1238 std r3, VCPU_GPR(R3)(r9)
1239 std r4, VCPU_GPR(R4)(r9)
1240 std r5, VCPU_GPR(R5)(r9)
1241 std r6, VCPU_GPR(R6)(r9)
1242 std r7, VCPU_GPR(R7)(r9)
1243 std r8, VCPU_GPR(R8)(r9)
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001244 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001245 std r0, VCPU_GPR(R9)(r9)
1246 std r10, VCPU_GPR(R10)(r9)
1247 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001248 ld r3, HSTATE_SCRATCH0(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001249 std r3, VCPU_GPR(R12)(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001250 /* CR is in the high half of r12 */
1251 srdi r4, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001252 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001253BEGIN_FTR_SECTION
1254 ld r3, HSTATE_CFAR(r13)
1255 std r3, VCPU_CFAR(r9)
1256END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001257BEGIN_FTR_SECTION
1258 ld r4, HSTATE_PPR(r13)
1259 std r4, VCPU_PPR(r9)
1260END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001261
1262 /* Restore R1/R2 so we can handle faults */
1263 ld r1, HSTATE_HOST_R1(r13)
1264 ld r2, PACATOC(r13)
1265
1266 mfspr r10, SPRN_SRR0
1267 mfspr r11, SPRN_SRR1
1268 std r10, VCPU_SRR0(r9)
1269 std r11, VCPU_SRR1(r9)
Nicholas Piggind3918e72016-12-22 04:29:25 +10001270 /* trap is in the low half of r12, clear CR from the high half */
1271 clrldi r12, r12, 32
Paul Mackerrasde56a942011-06-29 00:21:34 +00001272 andi. r0, r12, 2 /* need to read HSRR0/1? */
1273 beq 1f
1274 mfspr r10, SPRN_HSRR0
1275 mfspr r11, SPRN_HSRR1
1276 clrrdi r12, r12, 2
12771: std r10, VCPU_PC(r9)
1278 std r11, VCPU_MSR(r9)
1279
1280 GET_SCRATCH0(r3)
1281 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001282 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001283 std r4, VCPU_LR(r9)
1284
Paul Mackerrasde56a942011-06-29 00:21:34 +00001285 stw r12,VCPU_TRAP(r9)
1286
Paul Mackerras8b24e692017-06-26 15:45:51 +10001287 /*
1288 * Now that we have saved away SRR0/1 and HSRR0/1,
1289 * interrupts are recoverable in principle, so set MSR_RI.
1290 * This becomes important for relocation-on interrupts from
1291 * the guest, which we can get in radix mode on POWER9.
1292 */
1293 li r0, MSR_RI
1294 mtmsrd r0, 1
1295
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001296#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1297 addi r3, r9, VCPU_TB_RMINTR
1298 mr r4, r9
1299 bl kvmhv_accumulate_time
1300 ld r5, VCPU_GPR(R5)(r9)
1301 ld r6, VCPU_GPR(R6)(r9)
1302 ld r7, VCPU_GPR(R7)(r9)
1303 ld r8, VCPU_GPR(R8)(r9)
1304#endif
1305
Paul Mackerras4a157d62014-12-03 13:30:39 +11001306 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001307 if this is an HEI (HV emulation interrupt, e40) */
1308 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001309 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001310 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1311 bne 11f
1312 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100131311: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001314
1315 /* these are volatile across C function calls */
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001316#ifdef CONFIG_RELOCATABLE
1317 ld r3, HSTATE_SCRATCH1(r13)
1318 mtctr r3
1319#else
Paul Mackerras697d3892011-12-12 12:36:37 +00001320 mfctr r3
Nicholas Piggina97a65d2017-01-27 14:00:34 +10001321#endif
Paul Mackerras697d3892011-12-12 12:36:37 +00001322 mfxer r4
1323 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001324 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001325
Paul Mackerras697d3892011-12-12 12:36:37 +00001326 /* If this is a page table miss then see if it's theirs or ours */
1327 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1328 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +00001329 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1330 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +00001331
Paul Mackerrasde56a942011-06-29 00:21:34 +00001332 /* See if this is a leftover HDEC interrupt */
1333 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1334 bne 2f
1335 mfspr r3,SPRN_HDEC
Paul Mackerrasa4faf2e2017-08-25 19:52:12 +10001336 EXTEND_HDEC(r3)
1337 cmpdi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001338 mr r4,r9
1339 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000013402:
Paul Mackerras697d3892011-12-12 12:36:37 +00001341 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001342 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1343 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001344
Paul Mackerras66feed62015-03-28 14:21:12 +11001345 /* Hypervisor doorbell - exit only if host IPI flag set */
1346 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1347 bne 3f
Nicholas Pigginbd0fdb12017-03-13 03:03:49 +10001348BEGIN_FTR_SECTION
1349 PPC_MSGSYNC
Nicholas Piggin2cde3712017-10-10 20:18:28 +10001350 lwsync
Nicholas Pigginbd0fdb12017-03-13 03:03:49 +10001351END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras66feed62015-03-28 14:21:12 +11001352 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301353 cmpwi r0, 0
Paul Mackerras66feed62015-03-28 14:21:12 +11001354 beq 4f
1355 b guest_exit_cont
13563:
Paul Mackerras769377f2017-02-15 14:30:17 +11001357 /* If it's a hypervisor facility unavailable interrupt, save HFSCR */
1358 cmpwi r12, BOOK3S_INTERRUPT_H_FAC_UNAVAIL
1359 bne 14f
1360 mfspr r3, SPRN_HFSCR
1361 std r3, VCPU_HFSCR(r9)
1362 b guest_exit_cont
136314:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001364 /* External interrupt ? */
1365 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001366 bne+ guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001367
1368 /* External interrupt, first check for host_ipi. If this is
1369 * set, we know the host wants us out so let's do it now
1370 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001371 bl kvmppc_read_intr
Suresh Warrier37f55d32016-08-19 15:35:46 +10001372
1373 /*
1374 * Restore the active volatile registers after returning from
1375 * a C function.
1376 */
1377 ld r9, HSTATE_KVM_VCPU(r13)
1378 li r12, BOOK3S_INTERRUPT_EXTERNAL
1379
1380 /*
1381 * kvmppc_read_intr return codes:
1382 *
1383 * Exit to host (r3 > 0)
1384 * 1 An interrupt is pending that needs to be handled by the host
1385 * Exit guest and return to host by branching to guest_exit_cont
1386 *
Suresh Warrierf7af5202016-08-19 15:35:52 +10001387 * 2 Passthrough that needs completion in the host
1388 * Exit guest and return to host by branching to guest_exit_cont
1389 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1390 * to indicate to the host to complete handling the interrupt
1391 *
Suresh Warrier37f55d32016-08-19 15:35:46 +10001392 * Before returning to guest, we check if any CPU is heading out
1393 * to the host and if so, we head out also. If no CPUs are heading
1394 * check return values <= 0.
1395 *
1396 * Return to guest (r3 <= 0)
1397 * 0 No external interrupt is pending
1398 * -1 A guest wakeup IPI (which has now been cleared)
1399 * In either case, we return to guest to deliver any pending
1400 * guest interrupts.
Suresh Warriere3c13e52016-08-19 15:35:51 +10001401 *
1402 * -2 A PCI passthrough external interrupt was handled
1403 * (interrupt was delivered directly to guest)
1404 * Return to guest to deliver any pending guest interrupts.
Suresh Warrier37f55d32016-08-19 15:35:46 +10001405 */
1406
Suresh Warrierf7af5202016-08-19 15:35:52 +10001407 cmpdi r3, 1
1408 ble 1f
1409
1410 /* Return code = 2 */
1411 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1412 stw r12, VCPU_TRAP(r9)
1413 b guest_exit_cont
1414
14151: /* Return code <= 1 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001416 cmpdi r3, 0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001417 bgt guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001418
Suresh Warrier37f55d32016-08-19 15:35:46 +10001419 /* Return code <= 0 */
Paul Mackerras66feed62015-03-28 14:21:12 +110014204: ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001421 lwz r0, VCORE_ENTRY_EXIT(r5)
1422 cmpwi r0, 0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001423 mr r4, r9
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001424 blt deliver_guest_interrupt
Paul Mackerrasde56a942011-06-29 00:21:34 +00001425
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001426guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001427#ifdef CONFIG_KVM_XICS
1428 /* We are exiting, pull the VP from the XIVE */
1429 lwz r0, VCPU_XIVE_PUSHED(r9)
1430 cmpwi cr0, r0, 0
1431 beq 1f
1432 li r7, TM_SPC_PULL_OS_CTX
1433 li r6, TM_QW1_OS
1434 mfmsr r0
1435 andi. r0, r0, MSR_IR /* in real mode? */
1436 beq 2f
1437 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
1438 cmpldi cr0, r10, 0
1439 beq 1f
1440 /* First load to pull the context, we ignore the value */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001441 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001442 lwzx r11, r7, r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001443 /* Second load to recover the context state (Words 0 and 1) */
1444 ldx r11, r6, r10
1445 b 3f
14462: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1447 cmpldi cr0, r10, 0
1448 beq 1f
1449 /* First load to pull the context, we ignore the value */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001450 eieio
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001451 lwzcix r11, r7, r10
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001452 /* Second load to recover the context state (Words 0 and 1) */
1453 ldcix r11, r6, r10
14543: std r11, VCPU_XIVE_SAVED_STATE(r9)
1455 /* Fixup some of the state for the next load */
1456 li r10, 0
1457 li r0, 0xff
1458 stw r10, VCPU_XIVE_PUSHED(r9)
1459 stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1460 stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
Benjamin Herrenschmidtad98dd12017-10-16 08:37:54 +11001461 eieio
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100014621:
1463#endif /* CONFIG_KVM_XICS */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001464 /* Save more register state */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001465 mfdar r6
1466 mfdsisr r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001467 std r6, VCPU_DAR(r9)
1468 stw r7, VCPU_DSISR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001469 /* don't overwrite fault_dar/fault_dsisr if HDSI */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001470 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
Paul Mackerras6af27c82015-03-28 14:21:10 +11001471 beq mc_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001472 std r6, VCPU_FAULT_DAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001473 stw r7, VCPU_FAULT_DSISR(r9)
1474
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001475 /* See if it is a machine check */
1476 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1477 beq machine_check_realmode
1478mc_cont:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001479#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1480 addi r3, r9, VCPU_TB_RMEXIT
1481 mr r4, r9
1482 bl kvmhv_accumulate_time
1483#endif
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001484
Gautham R. Shenoy7e022e72015-05-21 13:57:04 +05301485 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001486 /* Increment exit count, poke other threads to exit */
1487 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001488 nop
1489 ld r9, HSTATE_KVM_VCPU(r13)
1490 lwz r12, VCPU_TRAP(r9)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001491
Paul Mackerrasec257162015-06-24 21:18:03 +10001492 /* Stop others sending VCPU interrupts to this physical CPU */
1493 li r0, -1
1494 stw r0, VCPU_CPU(r9)
1495 stw r0, VCPU_THREAD_CPU(r9)
1496
Paul Mackerrasde56a942011-06-29 00:21:34 +00001497 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001498 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001499 stw r6,VCPU_CTRL(r9)
1500 andi. r0,r6,1
1501 bne 4f
1502 ori r6,r6,1
1503 mtspr SPRN_CTRLT,r6
15044:
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001505 /* Check if we are running hash or radix and store it in cr2 */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001506 ld r5, VCPU_KVM(r9)
1507 lbz r0, KVM_RADIX(r5)
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001508 cmpwi cr2,r0,0
1509
1510 /* Read the guest SLB and save it away */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001511 li r5, 0
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001512 bne cr2, 3f /* for radix, save 0 entries */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001513 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1514 mtctr r0
1515 li r6,0
1516 addi r7,r9,VCPU_SLB
Paul Mackerrasde56a942011-06-29 00:21:34 +000015171: slbmfee r8,r6
1518 andis. r0,r8,SLB_ESID_V@h
1519 beq 2f
1520 add r8,r8,r6 /* put index in */
1521 slbmfev r3,r6
1522 std r8,VCPU_SLB_E(r7)
1523 std r3,VCPU_SLB_V(r7)
1524 addi r7,r7,VCPU_SLB_SIZE
1525 addi r5,r5,1
15262: addi r6,r6,1
1527 bdnz 1b
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110015283: stw r5,VCPU_SLB_MAX(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001529
1530 /*
1531 * Save the guest PURR/SPURR
1532 */
1533 mfspr r5,SPRN_PURR
1534 mfspr r6,SPRN_SPURR
1535 ld r7,VCPU_PURR(r9)
1536 ld r8,VCPU_SPURR(r9)
1537 std r5,VCPU_PURR(r9)
1538 std r6,VCPU_SPURR(r9)
1539 subf r5,r7,r5
1540 subf r6,r8,r6
1541
1542 /*
1543 * Restore host PURR/SPURR and add guest times
1544 * so that the time in the guest gets accounted.
1545 */
1546 ld r3,HSTATE_PURR(r13)
1547 ld r4,HSTATE_SPURR(r13)
1548 add r3,r3,r5
1549 add r4,r4,r6
1550 mtspr SPRN_PURR,r3
1551 mtspr SPRN_SPURR,r4
1552
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001553 /* Save DEC */
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10001554 ld r3, HSTATE_KVM_VCORE(r13)
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001555 mfspr r5,SPRN_DEC
1556 mftb r6
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10001557 /* On P9, if the guest has large decr enabled, don't sign extend */
1558BEGIN_FTR_SECTION
1559 ld r4, VCORE_LPCR(r3)
1560 andis. r4, r4, LPCR_LD@h
1561 bne 16f
1562END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001563 extsw r5,r5
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000156416: add r5,r5,r6
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001565 /* r5 is a guest timebase value here, convert to host TB */
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001566 ld r4,VCORE_TB_OFFSET(r3)
1567 subf r5,r4,r5
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001568 std r5,VCPU_DEC_EXPIRES(r9)
1569
Michael Neulingb005255e2014-01-08 21:25:21 +11001570BEGIN_FTR_SECTION
1571 b 8f
1572END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001573 /* Save POWER8-specific registers */
1574 mfspr r5, SPRN_IAMR
1575 mfspr r6, SPRN_PSPB
1576 mfspr r7, SPRN_FSCR
1577 std r5, VCPU_IAMR(r9)
1578 stw r6, VCPU_PSPB(r9)
1579 std r7, VCPU_FSCR(r9)
1580 mfspr r5, SPRN_IC
Michael Neulingb005255e2014-01-08 21:25:21 +11001581 mfspr r7, SPRN_TAR
1582 std r5, VCPU_IC(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001583 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001584 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001585 std r8, VCPU_EBBHR(r9)
1586 mfspr r5, SPRN_EBBRR
1587 mfspr r6, SPRN_BESCR
Michael Neulingb005255e2014-01-08 21:25:21 +11001588 mfspr r7, SPRN_PID
1589 mfspr r8, SPRN_WORT
Paul Mackerras83677f52016-11-16 22:33:27 +11001590 std r5, VCPU_EBBRR(r9)
1591 std r6, VCPU_BESCR(r9)
Michael Neulingb005255e2014-01-08 21:25:21 +11001592 stw r7, VCPU_GUEST_PID(r9)
1593 std r8, VCPU_WORT(r9)
Paul Mackerras83677f52016-11-16 22:33:27 +11001594BEGIN_FTR_SECTION
1595 mfspr r5, SPRN_TCSCR
1596 mfspr r6, SPRN_ACOP
1597 mfspr r7, SPRN_CSIGR
1598 mfspr r8, SPRN_TACR
1599 std r5, VCPU_TCSCR(r9)
1600 std r6, VCPU_ACOP(r9)
1601 std r7, VCPU_CSIGR(r9)
1602 std r8, VCPU_TACR(r9)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001603FTR_SECTION_ELSE
1604 mfspr r5, SPRN_TIDR
1605 mfspr r6, SPRN_PSSCR
1606 std r5, VCPU_TID(r9)
1607 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1608 rotldi r6, r6, 60
1609 std r6, VCPU_PSSCR(r9)
Paul Mackerras769377f2017-02-15 14:30:17 +11001610 /* Restore host HFSCR value */
1611 ld r7, STACK_SLOT_HFSCR(r1)
1612 mtspr SPRN_HFSCR, r7
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001613ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001614 /*
1615 * Restore various registers to 0, where non-zero values
1616 * set by the guest could disrupt the host.
1617 */
1618 li r0, 0
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001619 mtspr SPRN_PSPB, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001620 mtspr SPRN_WORT, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001621BEGIN_FTR_SECTION
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001622 mtspr SPRN_IAMR, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001623 mtspr SPRN_TCSCR, r0
Paul Mackerrasccec4452016-03-05 19:34:39 +11001624 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1625 li r0, 1
1626 sldi r0, r0, 31
1627 mtspr SPRN_MMCRS, r0
Paul Mackerras83677f52016-11-16 22:33:27 +11001628END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Michael Neulingb005255e2014-01-08 21:25:21 +110016298:
1630
Paul Mackerrasde56a942011-06-29 00:21:34 +00001631 /* Save and reset AMR and UAMOR before turning on the MMU */
1632 mfspr r5,SPRN_AMR
1633 mfspr r6,SPRN_UAMOR
1634 std r5,VCPU_AMR(r9)
1635 std r6,VCPU_UAMOR(r9)
1636 li r6,0
1637 mtspr SPRN_AMR,r6
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001638 mtspr SPRN_UAMOR, r6
Paul Mackerrasde56a942011-06-29 00:21:34 +00001639
Paul Mackerrasde56a942011-06-29 00:21:34 +00001640 /* Switch DSCR back to host value */
1641 mfspr r8, SPRN_DSCR
1642 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001643 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001644 mtspr SPRN_DSCR, r7
1645
1646 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001647 std r14, VCPU_GPR(R14)(r9)
1648 std r15, VCPU_GPR(R15)(r9)
1649 std r16, VCPU_GPR(R16)(r9)
1650 std r17, VCPU_GPR(R17)(r9)
1651 std r18, VCPU_GPR(R18)(r9)
1652 std r19, VCPU_GPR(R19)(r9)
1653 std r20, VCPU_GPR(R20)(r9)
1654 std r21, VCPU_GPR(R21)(r9)
1655 std r22, VCPU_GPR(R22)(r9)
1656 std r23, VCPU_GPR(R23)(r9)
1657 std r24, VCPU_GPR(R24)(r9)
1658 std r25, VCPU_GPR(R25)(r9)
1659 std r26, VCPU_GPR(R26)(r9)
1660 std r27, VCPU_GPR(R27)(r9)
1661 std r28, VCPU_GPR(R28)(r9)
1662 std r29, VCPU_GPR(R29)(r9)
1663 std r30, VCPU_GPR(R30)(r9)
1664 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001665
1666 /* Save SPRGs */
1667 mfspr r3, SPRN_SPRG0
1668 mfspr r4, SPRN_SPRG1
1669 mfspr r5, SPRN_SPRG2
1670 mfspr r6, SPRN_SPRG3
1671 std r3, VCPU_SPRG0(r9)
1672 std r4, VCPU_SPRG1(r9)
1673 std r5, VCPU_SPRG2(r9)
1674 std r6, VCPU_SPRG3(r9)
1675
Paul Mackerras89436332012-03-02 01:38:23 +00001676 /* save FP state */
1677 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001678 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001679
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001680#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1681BEGIN_FTR_SECTION
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001682 /*
1683 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
1684 */
Paul Mackerrasf024ee02016-06-22 14:21:59 +10001685 bl kvmppc_save_tm
1686END_FTR_SECTION_IFSET(CPU_FTR_TM)
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001687#endif
1688
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001689 /* Increment yield count if they have a VPA */
1690 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1691 cmpdi r8, 0
1692 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001693 li r4, LPPACA_YIELDCOUNT
1694 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001695 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001696 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001697 li r3, 1
1698 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000169925:
1700 /* Save PMU registers if requested */
1701 /* r8 and cr0.eq are live here */
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001702BEGIN_FTR_SECTION
1703 /*
1704 * POWER8 seems to have a hardware bug where setting
1705 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1706 * when some counters are already negative doesn't seem
1707 * to cause a performance monitor alert (and hence interrupt).
1708 * The effect of this is that when saving the PMU state,
1709 * if there is no PMU alert pending when we read MMCR0
1710 * before freezing the counters, but one becomes pending
1711 * before we read the counters, we lose it.
1712 * To work around this, we need a way to freeze the counters
1713 * before reading MMCR0. Normally, freezing the counters
1714 * is done by writing MMCR0 (to set MMCR0[FC]) which
1715 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1716 * we can also freeze the counters using MMCR2, by writing
1717 * 1s to all the counter freeze condition bits (there are
1718 * 9 bits each for 6 counters).
1719 */
1720 li r3, -1 /* set all freeze bits */
1721 clrrdi r3, r3, 10
1722 mfspr r10, SPRN_MMCR2
1723 mtspr SPRN_MMCR2, r3
1724 isync
1725END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001726 li r3, 1
1727 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1728 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1729 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001730 mfspr r6, SPRN_MMCRA
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001731 /* Clear MMCRA in order to disable SDAR updates */
Paul Mackerras89436332012-03-02 01:38:23 +00001732 li r7, 0
1733 mtspr SPRN_MMCRA, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001734 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001735 beq 21f /* if no VPA, save PMU stuff anyway */
1736 lbz r7, LPPACA_PMCINUSE(r8)
1737 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1738 bne 21f
1739 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1740 b 22f
174121: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001742 mfspr r7, SPRN_SIAR
1743 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001744 std r4, VCPU_MMCR(r9)
1745 std r5, VCPU_MMCR + 8(r9)
1746 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001747BEGIN_FTR_SECTION
1748 std r10, VCPU_MMCR + 24(r9)
1749END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras14941782013-09-06 13:11:18 +10001750 std r7, VCPU_SIAR(r9)
1751 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001752 mfspr r3, SPRN_PMC1
1753 mfspr r4, SPRN_PMC2
1754 mfspr r5, SPRN_PMC3
1755 mfspr r6, SPRN_PMC4
1756 mfspr r7, SPRN_PMC5
1757 mfspr r8, SPRN_PMC6
1758 stw r3, VCPU_PMC(r9)
1759 stw r4, VCPU_PMC + 4(r9)
1760 stw r5, VCPU_PMC + 8(r9)
1761 stw r6, VCPU_PMC + 12(r9)
1762 stw r7, VCPU_PMC + 16(r9)
1763 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001764BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +11001765 mfspr r5, SPRN_SIER
Paul Mackerras83677f52016-11-16 22:33:27 +11001766 std r5, VCPU_SIER(r9)
1767BEGIN_FTR_SECTION_NESTED(96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001768 mfspr r6, SPRN_SPMC1
1769 mfspr r7, SPRN_SPMC2
1770 mfspr r8, SPRN_MMCRS
Michael Neulingb005255e2014-01-08 21:25:21 +11001771 stw r6, VCPU_PMC + 24(r9)
1772 stw r7, VCPU_PMC + 28(r9)
1773 std r8, VCPU_MMCR + 32(r9)
1774 lis r4, 0x8000
1775 mtspr SPRN_MMCRS, r4
Paul Mackerras83677f52016-11-16 22:33:27 +11001776END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
Michael Neulingb005255e2014-01-08 21:25:21 +11001777END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000177822:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001779
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001780 /* Restore host values of some registers */
1781BEGIN_FTR_SECTION
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10001782 ld r5, STACK_SLOT_CIABR(r1)
1783 ld r6, STACK_SLOT_DAWR(r1)
1784 ld r7, STACK_SLOT_DAWRX(r1)
1785 mtspr SPRN_CIABR, r5
1786 mtspr SPRN_DAWR, r6
1787 mtspr SPRN_DAWRX, r7
1788END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1789BEGIN_FTR_SECTION
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001790 ld r5, STACK_SLOT_TID(r1)
1791 ld r6, STACK_SLOT_PSSCR(r1)
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001792 ld r7, STACK_SLOT_PID(r1)
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001793 ld r8, STACK_SLOT_IAMR(r1)
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001794 mtspr SPRN_TIDR, r5
1795 mtspr SPRN_PSSCR, r6
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001796 mtspr SPRN_PID, r7
Paul Mackerras4c3bb4c2017-06-15 15:43:17 +10001797 mtspr SPRN_IAMR, r8
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001798END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001799
1800#ifdef CONFIG_PPC_RADIX_MMU
1801 /*
1802 * Are we running hash or radix ?
1803 */
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10001804 ld r5, VCPU_KVM(r9)
1805 lbz r0, KVM_RADIX(r5)
1806 cmpwi cr2, r0, 0
1807 beq cr2, 3f
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001808
1809 /* Radix: Handle the case where the guest used an illegal PID */
1810 LOAD_REG_ADDR(r4, mmu_base_pid)
1811 lwz r3, VCPU_GUEST_PID(r9)
1812 lwz r5, 0(r4)
1813 cmpw cr0,r3,r5
1814 blt 2f
1815
1816 /*
1817 * Illegal PID, the HW might have prefetched and cached in the TLB
1818 * some translations for the LPID 0 / guest PID combination which
1819 * Linux doesn't know about, so we need to flush that PID out of
1820 * the TLB. First we need to set LPIDR to 0 so tlbiel applies to
1821 * the right context.
1822 */
1823 li r0,0
1824 mtspr SPRN_LPID,r0
1825 isync
1826
1827 /* Then do a congruence class local flush */
1828 ld r6,VCPU_KVM(r9)
1829 lwz r0,KVM_TLB_SETS(r6)
1830 mtctr r0
1831 li r7,0x400 /* IS field = 0b01 */
1832 ptesync
1833 sldi r0,r3,32 /* RS has PID */
18341: PPC_TLBIEL(7,0,2,1,1) /* RIC=2, PRS=1, R=1 */
1835 addi r7,r7,0x1000
1836 bdnz 1b
1837 ptesync
1838
18392: /* Flush the ERAT on radix P9 DD1 guest exit */
Paul Mackerrasf11f6f72017-01-30 21:21:52 +11001840BEGIN_FTR_SECTION
1841 PPC_INVALIDATE_ERAT
1842END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001843 b 4f
1844#endif /* CONFIG_PPC_RADIX_MMU */
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11001845
Benjamin Herrenschmidta25bd722017-07-24 14:26:06 +10001846 /* Hash: clear out SLB */
18473: li r5,0
1848 slbmte r5,r5
1849 slbia
1850 ptesync
18514:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001852 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001853 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001854 * We don't have to lock against tlbies but we do
1855 * have to coordinate the hardware threads.
1856 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001857kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001858 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001859 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001860 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1861 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001862 cmpwi r3,0
1863 beq 15f
1864 HMT_LOW
186513: lbz r3,VCORE_IN_GUEST(r5)
1866 cmpwi r3,0
1867 bne 13b
1868 HMT_MEDIUM
1869 b 16f
1870
1871 /* Primary thread waits for all the secondaries to exit guest */
187215: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001873 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001874 clrldi r3,r3,56
1875 cmpw r3,r0
1876 bne 15b
1877 isync
1878
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001879 /* Did we actually switch to the guest at all? */
1880 lbz r6, VCORE_IN_GUEST(r5)
1881 cmpwi r6, 0
1882 beq 19f
1883
Paul Mackerrasde56a942011-06-29 00:21:34 +00001884 /* Primary thread switches back to host partition */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001885 lwz r7,KVM_HOST_LPID(r4)
Paul Mackerras7a840842016-11-16 22:25:20 +11001886BEGIN_FTR_SECTION
1887 ld r6,KVM_HOST_SDR1(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001888 li r8,LPID_RSVD /* switch to reserved LPID */
1889 mtspr SPRN_LPID,r8
1890 ptesync
Paul Mackerras7a840842016-11-16 22:25:20 +11001891 mtspr SPRN_SDR1,r6 /* switch to host page table */
1892END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001893 mtspr SPRN_LPID,r7
1894 isync
1895
Michael Neulingb005255e2014-01-08 21:25:21 +11001896BEGIN_FTR_SECTION
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001897 /* DPDES and VTB are shared between threads */
Michael Neulingb005255e2014-01-08 21:25:21 +11001898 mfspr r7, SPRN_DPDES
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001899 mfspr r8, SPRN_VTB
Michael Neulingb005255e2014-01-08 21:25:21 +11001900 std r7, VCORE_DPDES(r5)
Paul Mackerras88b02cf92016-09-15 13:42:52 +10001901 std r8, VCORE_VTB(r5)
Michael Neulingb005255e2014-01-08 21:25:21 +11001902 /* clear DPDES so we don't get guest doorbells in the host */
1903 li r8, 0
1904 mtspr SPRN_DPDES, r8
1905END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1906
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301907 /* If HMI, call kvmppc_realmode_hmi_handler() */
1908 cmpwi r12, BOOK3S_INTERRUPT_HMI
1909 bne 27f
1910 bl kvmppc_realmode_hmi_handler
1911 nop
Paul Mackerrasd0757452018-01-17 20:51:13 +11001912 cmpdi r3, 0
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301913 li r12, BOOK3S_INTERRUPT_HMI
1914 /*
Paul Mackerrasd0757452018-01-17 20:51:13 +11001915 * At this point kvmppc_realmode_hmi_handler may have resync-ed
1916 * the TB, and if it has, we must not subtract the guest timebase
1917 * offset from the timebase. So, skip it.
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301918 *
1919 * Also, do not call kvmppc_subcore_exit_guest() because it has
1920 * been invoked as part of kvmppc_realmode_hmi_handler().
1921 */
Paul Mackerrasd0757452018-01-17 20:51:13 +11001922 beq 30f
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301923
192427:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001925 /* Subtract timebase offset from timebase */
1926 ld r8,VCORE_TB_OFFSET(r5)
1927 cmpdi r8,0
1928 beq 17f
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001929 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001930 subf r8,r8,r6
1931 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1932 mftb r7 /* check if lower 24 bits overflowed */
1933 clrldi r6,r6,40
1934 clrldi r7,r7,40
1935 cmpld r7,r6
1936 bge 17f
1937 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1938 mtspr SPRN_TBU40,r8
1939
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530194017: bl kvmppc_subcore_exit_guest
1941 nop
194230: ld r5,HSTATE_KVM_VCORE(r13)
1943 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1944
Paul Mackerrasde56a942011-06-29 00:21:34 +00001945 /* Reset PCR */
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05301946 ld r0, VCORE_PCR(r5)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001947 cmpdi r0, 0
1948 beq 18f
1949 li r0, 0
1950 mtspr SPRN_PCR, r0
195118:
1952 /* Signal secondary CPUs to continue */
1953 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000195419: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001955 mtspr SPRN_HDEC,r8
1956
Paul Mackerrasc0101502017-10-19 14:11:23 +1100195716:
1958BEGIN_FTR_SECTION
1959 /* On POWER9 with HPT-on-radix we need to wait for all other threads */
1960 ld r3, HSTATE_SPLIT_MODE(r13)
1961 cmpdi r3, 0
1962 beq 47f
1963 lwz r8, KVM_SPLIT_DO_RESTORE(r3)
1964 cmpwi r8, 0
1965 beq 47f
1966 stw r12, STACK_SLOT_TRAP(r1)
1967 bl kvmhv_p9_restore_lpcr
1968 nop
1969 lwz r12, STACK_SLOT_TRAP(r1)
1970 b 48f
197147:
1972END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1973 ld r8,KVM_HOST_LPCR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001974 mtspr SPRN_LPCR,r8
1975 isync
Paul Mackerrasc0101502017-10-19 14:11:23 +1100197648:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001977 /* load host SLB entries */
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11001978BEGIN_MMU_FTR_SECTION
1979 b 0f
1980END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001981 ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001982
1983 .rept SLB_NUM_BOLTED
Alexander Graf0865a582014-06-11 10:36:17 +02001984 li r3, SLBSHADOW_SAVEAREA
1985 LDX_BE r5, r8, r3
1986 addi r3, r3, 8
1987 LDX_BE r6, r8, r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00001988 andis. r7,r5,SLB_ESID_V@h
1989 beq 1f
1990 slbmte r6,r5
19911: addi r8,r8,16
1992 .endr
Paul Mackerrasf4c51f82017-01-30 21:21:45 +110019930:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001994#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1995 /* Finish timing, if we have a vcpu */
1996 ld r4, HSTATE_KVM_VCPU(r13)
1997 cmpdi r4, 0
1998 li r3, 0
1999 beq 2f
2000 bl kvmhv_accumulate_time
20012:
2002#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00002003 /* Unset guest mode */
2004 li r0, KVM_GUEST_MODE_NONE
2005 stb r0, HSTATE_IN_GUEST(r13)
2006
Paul Mackerras7ceaa6d2017-06-16 11:53:19 +10002007 ld r0, SFS+PPC_LR_STKOFF(r1)
2008 addi r1, r1, SFS
Paul Mackerras218309b2013-09-06 13:23:44 +10002009 mtlr r0
2010 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002011
Paul Mackerras697d3892011-12-12 12:36:37 +00002012/*
2013 * Check whether an HDSI is an HPTE not found fault or something else.
2014 * If it is an HPTE not found fault that is due to the guest accessing
2015 * a page that they have mapped but which we have paged out, then
2016 * we continue on with the guest exit path. In all other cases,
2017 * reflect the HDSI to the guest as a DSI.
2018 */
2019kvmppc_hdsi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002020 ld r3, VCPU_KVM(r9)
2021 lbz r0, KVM_RADIX(r3)
Paul Mackerras697d3892011-12-12 12:36:37 +00002022 mfspr r4, SPRN_HDAR
2023 mfspr r6, SPRN_HDSISR
Michael Neulinge001fa72017-09-15 15:26:14 +10002024BEGIN_FTR_SECTION
2025 /* Look for DSISR canary. If we find it, retry instruction */
2026 cmpdi r6, 0x7fff
2027 beq 6f
2028END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2029 cmpwi r0, 0
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002030 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
Paul Mackerras4cf302b2011-12-12 12:38:51 +00002031 /* HPTE not found fault or protection fault? */
2032 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00002033 beq 1f /* if not, send it to the guest */
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11002034 andi. r0, r11, MSR_DR /* data relocation enabled? */
2035 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11002036BEGIN_FTR_SECTION
2037 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2038 b 4f
2039END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras697d3892011-12-12 12:36:37 +00002040 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00002041 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11002042 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
2043 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000020444: std r4, VCPU_FAULT_DAR(r9)
2045 stw r6, VCPU_FAULT_DSISR(r9)
2046
2047 /* Search the hash table. */
2048 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002049 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002050 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00002051 ld r9, HSTATE_KVM_VCPU(r13)
2052 ld r10, VCPU_PC(r9)
2053 ld r11, VCPU_MSR(r9)
2054 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
2055 cmpdi r3, 0 /* retry the instruction */
2056 beq 6f
2057 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002058 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00002059 cmpdi r3, -2 /* MMIO emulation; need instr word */
2060 beq 2f
2061
Paul Mackerrascf29b212015-10-27 16:10:20 +11002062 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00002063 ld r4, VCPU_FAULT_DAR(r9)
2064 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110020651: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00002066 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110020677: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00002068 mtspr SPRN_SRR0, r10
2069 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002070 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002071 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002072fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000020736: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10002074 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00002075 mtctr r7
2076 mtxer r8
2077 mr r4, r9
2078 b fast_guest_return
2079
20803: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
2081 ld r5, KVM_VRMA_SLB_V(r5)
2082 b 4b
2083
2084 /* If this is for emulated MMIO, load the instruction word */
20852: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
2086
2087 /* Set guest mode to 'jump over instruction' so if lwz faults
2088 * we'll just continue at the next IP. */
2089 li r0, KVM_GUEST_MODE_SKIP
2090 stb r0, HSTATE_IN_GUEST(r13)
2091
2092 /* Do the access with MSR:DR enabled */
2093 mfmsr r3
2094 ori r4, r3, MSR_DR /* Enable paging for data */
2095 mtmsrd r4
2096 lwz r8, 0(r10)
2097 mtmsrd r3
2098
2099 /* Store the result */
2100 stw r8, VCPU_LAST_INST(r9)
2101
2102 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10002103 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00002104 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002105 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00002106
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002107.Lradix_hdsi:
2108 std r4, VCPU_FAULT_DAR(r9)
2109 stw r6, VCPU_FAULT_DSISR(r9)
2110.Lradix_hisi:
2111 mfspr r5, SPRN_ASDR
2112 std r5, VCPU_FAULT_GPA(r9)
2113 b guest_exit_cont
2114
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002115/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00002116 * Similarly for an HISI, reflect it to the guest as an ISI unless
2117 * it is an HPTE not found fault for a page that we have paged out.
2118 */
2119kvmppc_hisi:
Paul Mackerrasf4c51f82017-01-30 21:21:45 +11002120 ld r3, VCPU_KVM(r9)
2121 lbz r0, KVM_RADIX(r3)
2122 cmpwi r0, 0
2123 bne .Lradix_hisi /* for radix, just save ASDR */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002124 andis. r0, r11, SRR1_ISI_NOPT@h
2125 beq 1f
Paul Mackerras4e5acdc2017-02-28 11:05:47 +11002126 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
2127 beq 3f
Paul Mackerrasef8c6402017-01-30 21:21:43 +11002128BEGIN_FTR_SECTION
2129 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2130 b 4f
2131END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras342d3db2011-12-12 12:38:05 +00002132 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00002133 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11002134 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
2135 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000021364:
2137 /* Search the hash table. */
2138 mr r3, r9 /* vcpu pointer */
2139 mr r4, r10
2140 mr r6, r11
2141 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002142 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00002143 ld r9, HSTATE_KVM_VCPU(r13)
2144 ld r10, VCPU_PC(r9)
2145 ld r11, VCPU_MSR(r9)
2146 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
2147 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002148 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002149 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002150 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00002151
Paul Mackerrascf29b212015-10-27 16:10:20 +11002152 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00002153 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110021541: li r0, BOOK3S_INTERRUPT_INST_STORAGE
21557: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00002156 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11002157 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11002158 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002159 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00002160
21613: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
2162 ld r5, KVM_VRMA_SLB_V(r6)
2163 b 4b
2164
2165/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002166 * Try to handle an hcall in real mode.
2167 * Returns to the guest if we handle it, or continues on up to
2168 * the kernel if we can't (i.e. if we don't have a handler for
2169 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002170 *
2171 * r5 - r8 contain hcall args,
2172 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002173 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002174hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00002175 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002176 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08002177 /* sc 1 from userspace - reflect to guest syscall */
2178 bne sc_1_fast_return
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002179 clrrdi r3,r3,2
2180 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002181 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002182 /* See if this hcall is enabled for in-kernel handling */
2183 ld r4, VCPU_KVM(r9)
2184 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
2185 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
2186 add r4, r4, r0
2187 ld r0, KVM_ENABLED_HCALLS(r4)
2188 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
2189 srd r0, r0, r4
2190 andi. r0, r0, 1
2191 beq guest_exit_cont
2192 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002193 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10002194 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002195 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002196 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10002197 add r12,r3,r4
2198 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002199 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002200 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002201 bctrl
2202 cmpdi r3,H_TOO_HARD
2203 beq hcall_real_fallback
2204 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00002205 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002206 ld r10,VCPU_PC(r4)
2207 ld r11,VCPU_MSR(r4)
2208 b fast_guest_return
2209
Liu Ping Fan27025a62013-11-19 14:12:48 +08002210sc_1_fast_return:
2211 mtspr SPRN_SRR0,r10
2212 mtspr SPRN_SRR1,r11
2213 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11002214 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08002215 mr r4,r9
2216 b fast_guest_return
2217
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002218 /* We've attempted a real mode hcall, but it's punted it back
2219 * to userspace. We need to restore some clobbered volatiles
2220 * before resuming the pass-it-to-qemu path */
2221hcall_real_fallback:
2222 li r12,BOOK3S_INTERRUPT_SYSCALL
2223 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002224
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002225 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002226
2227 .globl hcall_real_table
2228hcall_real_table:
2229 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002230 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
2231 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
2232 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10002233 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2234 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002235 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
2236 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002237 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002238 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002239 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002240 .long 0 /* 0x2c */
2241 .long 0 /* 0x30 */
2242 .long 0 /* 0x34 */
2243 .long 0 /* 0x38 */
2244 .long 0 /* 0x3c */
2245 .long 0 /* 0x40 */
2246 .long 0 /* 0x44 */
2247 .long 0 /* 0x48 */
2248 .long 0 /* 0x4c */
2249 .long 0 /* 0x50 */
2250 .long 0 /* 0x54 */
2251 .long 0 /* 0x58 */
2252 .long 0 /* 0x5c */
2253 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002254#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002255 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2256 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2257 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002258 .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002259 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00002260#else
2261 .long 0 /* 0x64 - H_EOI */
2262 .long 0 /* 0x68 - H_CPPR */
2263 .long 0 /* 0x6c - H_IPI */
2264 .long 0 /* 0x70 - H_IPOLL */
2265 .long 0 /* 0x74 - H_XIRR */
2266#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002267 .long 0 /* 0x78 */
2268 .long 0 /* 0x7c */
2269 .long 0 /* 0x80 */
2270 .long 0 /* 0x84 */
2271 .long 0 /* 0x88 */
2272 .long 0 /* 0x8c */
2273 .long 0 /* 0x90 */
2274 .long 0 /* 0x94 */
2275 .long 0 /* 0x98 */
2276 .long 0 /* 0x9c */
2277 .long 0 /* 0xa0 */
2278 .long 0 /* 0xa4 */
2279 .long 0 /* 0xa8 */
2280 .long 0 /* 0xac */
2281 .long 0 /* 0xb0 */
2282 .long 0 /* 0xb4 */
2283 .long 0 /* 0xb8 */
2284 .long 0 /* 0xbc */
2285 .long 0 /* 0xc0 */
2286 .long 0 /* 0xc4 */
2287 .long 0 /* 0xc8 */
2288 .long 0 /* 0xcc */
2289 .long 0 /* 0xd0 */
2290 .long 0 /* 0xd4 */
2291 .long 0 /* 0xd8 */
2292 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002293 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11002294 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002295 .long 0 /* 0xe8 */
2296 .long 0 /* 0xec */
2297 .long 0 /* 0xf0 */
2298 .long 0 /* 0xf4 */
2299 .long 0 /* 0xf8 */
2300 .long 0 /* 0xfc */
2301 .long 0 /* 0x100 */
2302 .long 0 /* 0x104 */
2303 .long 0 /* 0x108 */
2304 .long 0 /* 0x10c */
2305 .long 0 /* 0x110 */
2306 .long 0 /* 0x114 */
2307 .long 0 /* 0x118 */
2308 .long 0 /* 0x11c */
2309 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002310 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11002311 .long 0 /* 0x128 */
2312 .long 0 /* 0x12c */
2313 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002314 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002315 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11002316 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Michael Ellermane928e9c2015-03-20 20:39:41 +11002317 .long 0 /* 0x140 */
2318 .long 0 /* 0x144 */
2319 .long 0 /* 0x148 */
2320 .long 0 /* 0x14c */
2321 .long 0 /* 0x150 */
2322 .long 0 /* 0x154 */
2323 .long 0 /* 0x158 */
2324 .long 0 /* 0x15c */
2325 .long 0 /* 0x160 */
2326 .long 0 /* 0x164 */
2327 .long 0 /* 0x168 */
2328 .long 0 /* 0x16c */
2329 .long 0 /* 0x170 */
2330 .long 0 /* 0x174 */
2331 .long 0 /* 0x178 */
2332 .long 0 /* 0x17c */
2333 .long 0 /* 0x180 */
2334 .long 0 /* 0x184 */
2335 .long 0 /* 0x188 */
2336 .long 0 /* 0x18c */
2337 .long 0 /* 0x190 */
2338 .long 0 /* 0x194 */
2339 .long 0 /* 0x198 */
2340 .long 0 /* 0x19c */
2341 .long 0 /* 0x1a0 */
2342 .long 0 /* 0x1a4 */
2343 .long 0 /* 0x1a8 */
2344 .long 0 /* 0x1ac */
2345 .long 0 /* 0x1b0 */
2346 .long 0 /* 0x1b4 */
2347 .long 0 /* 0x1b8 */
2348 .long 0 /* 0x1bc */
2349 .long 0 /* 0x1c0 */
2350 .long 0 /* 0x1c4 */
2351 .long 0 /* 0x1c8 */
2352 .long 0 /* 0x1cc */
2353 .long 0 /* 0x1d0 */
2354 .long 0 /* 0x1d4 */
2355 .long 0 /* 0x1d8 */
2356 .long 0 /* 0x1dc */
2357 .long 0 /* 0x1e0 */
2358 .long 0 /* 0x1e4 */
2359 .long 0 /* 0x1e8 */
2360 .long 0 /* 0x1ec */
2361 .long 0 /* 0x1f0 */
2362 .long 0 /* 0x1f4 */
2363 .long 0 /* 0x1f8 */
2364 .long 0 /* 0x1fc */
2365 .long 0 /* 0x200 */
2366 .long 0 /* 0x204 */
2367 .long 0 /* 0x208 */
2368 .long 0 /* 0x20c */
2369 .long 0 /* 0x210 */
2370 .long 0 /* 0x214 */
2371 .long 0 /* 0x218 */
2372 .long 0 /* 0x21c */
2373 .long 0 /* 0x220 */
2374 .long 0 /* 0x224 */
2375 .long 0 /* 0x228 */
2376 .long 0 /* 0x22c */
2377 .long 0 /* 0x230 */
2378 .long 0 /* 0x234 */
2379 .long 0 /* 0x238 */
2380 .long 0 /* 0x23c */
2381 .long 0 /* 0x240 */
2382 .long 0 /* 0x244 */
2383 .long 0 /* 0x248 */
2384 .long 0 /* 0x24c */
2385 .long 0 /* 0x250 */
2386 .long 0 /* 0x254 */
2387 .long 0 /* 0x258 */
2388 .long 0 /* 0x25c */
2389 .long 0 /* 0x260 */
2390 .long 0 /* 0x264 */
2391 .long 0 /* 0x268 */
2392 .long 0 /* 0x26c */
2393 .long 0 /* 0x270 */
2394 .long 0 /* 0x274 */
2395 .long 0 /* 0x278 */
2396 .long 0 /* 0x27c */
2397 .long 0 /* 0x280 */
2398 .long 0 /* 0x284 */
2399 .long 0 /* 0x288 */
2400 .long 0 /* 0x28c */
2401 .long 0 /* 0x290 */
2402 .long 0 /* 0x294 */
2403 .long 0 /* 0x298 */
2404 .long 0 /* 0x29c */
2405 .long 0 /* 0x2a0 */
2406 .long 0 /* 0x2a4 */
2407 .long 0 /* 0x2a8 */
2408 .long 0 /* 0x2ac */
2409 .long 0 /* 0x2b0 */
2410 .long 0 /* 0x2b4 */
2411 .long 0 /* 0x2b8 */
2412 .long 0 /* 0x2bc */
2413 .long 0 /* 0x2c0 */
2414 .long 0 /* 0x2c4 */
2415 .long 0 /* 0x2c8 */
2416 .long 0 /* 0x2cc */
2417 .long 0 /* 0x2d0 */
2418 .long 0 /* 0x2d4 */
2419 .long 0 /* 0x2d8 */
2420 .long 0 /* 0x2dc */
2421 .long 0 /* 0x2e0 */
2422 .long 0 /* 0x2e4 */
2423 .long 0 /* 0x2e8 */
2424 .long 0 /* 0x2ec */
2425 .long 0 /* 0x2f0 */
2426 .long 0 /* 0x2f4 */
2427 .long 0 /* 0x2f8 */
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10002428#ifdef CONFIG_KVM_XICS
2429 .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2430#else
2431 .long 0 /* 0x2fc - H_XIRR_X*/
2432#endif
Michael Ellermane928e9c2015-03-20 20:39:41 +11002433 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002434 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002435hcall_real_table_end:
2436
Paul Mackerras8563bf52014-01-08 21:25:29 +11002437_GLOBAL(kvmppc_h_set_xdabr)
2438 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2439 beq 6f
2440 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2441 andc. r0, r5, r0
2442 beq 3f
24436: li r3, H_PARAMETER
2444 blr
2445
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002446_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002447 li r5, DABRX_USER | DABRX_KERNEL
24483:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002449BEGIN_FTR_SECTION
2450 b 2f
2451END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002452 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002453 stw r5, VCPU_DABRX(r3)
2454 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002455 /* Work around P7 bug where DABR can get corrupted on mtspr */
24561: mtspr SPRN_DABR,r4
2457 mfspr r5, SPRN_DABR
2458 cmpd r4, r5
2459 bne 1b
2460 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002461 li r3,0
2462 blr
2463
Paul Mackerras8563bf52014-01-08 21:25:29 +11002464 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
24652: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002466 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002467 clrrdi r4, r4, 3
2468 std r4, VCPU_DAWR(r3)
2469 std r5, VCPU_DAWRX(r3)
2470 mtspr SPRN_DAWR, r4
2471 mtspr SPRN_DAWRX, r5
2472 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002473 blr
2474
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002475_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002476 ori r11,r11,MSR_EE
2477 std r11,VCPU_MSR(r3)
2478 li r0,1
2479 stb r0,VCPU_CEDED(r3)
2480 sync /* order setting ceded vs. testing prodded */
2481 lbz r5,VCPU_PRODDED(r3)
2482 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002483 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002484 li r12,0 /* set trap to 0 to say hcall is handled */
2485 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002486 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002487 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002488
2489 /*
2490 * Set our bit in the bitmask of napping threads unless all the
2491 * other threads are already napping, in which case we send this
2492 * up to the host.
2493 */
2494 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002495 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002496 lwz r8,VCORE_ENTRY_EXIT(r5)
2497 clrldi r8,r8,56
2498 li r0,1
2499 sld r0,r0,r6
2500 addi r6,r5,VCORE_NAPPING_THREADS
250131: lwarx r4,0,r6
2502 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002503 cmpw r4,r8
2504 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002505 stwcx. r4,0,r6
2506 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002507 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002508 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002509 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002510 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002511 lwz r7,VCORE_ENTRY_EXIT(r5)
2512 cmpwi r7,0x100
2513 bge 33f /* another thread already exiting */
2514
2515/*
2516 * Although not specifically required by the architecture, POWER7
2517 * preserves the following registers in nap mode, even if an SMT mode
2518 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2519 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2520 */
2521 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002522 std r14, VCPU_GPR(R14)(r3)
2523 std r15, VCPU_GPR(R15)(r3)
2524 std r16, VCPU_GPR(R16)(r3)
2525 std r17, VCPU_GPR(R17)(r3)
2526 std r18, VCPU_GPR(R18)(r3)
2527 std r19, VCPU_GPR(R19)(r3)
2528 std r20, VCPU_GPR(R20)(r3)
2529 std r21, VCPU_GPR(R21)(r3)
2530 std r22, VCPU_GPR(R22)(r3)
2531 std r23, VCPU_GPR(R23)(r3)
2532 std r24, VCPU_GPR(R24)(r3)
2533 std r25, VCPU_GPR(R25)(r3)
2534 std r26, VCPU_GPR(R26)(r3)
2535 std r27, VCPU_GPR(R27)(r3)
2536 std r28, VCPU_GPR(R28)(r3)
2537 std r29, VCPU_GPR(R29)(r3)
2538 std r30, VCPU_GPR(R30)(r3)
2539 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002540
2541 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002542 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002543
Paul Mackerras93d17392016-06-22 15:52:55 +10002544#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2545BEGIN_FTR_SECTION
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002546 /*
2547 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
2548 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002549 ld r9, HSTATE_KVM_VCPU(r13)
2550 bl kvmppc_save_tm
2551END_FTR_SECTION_IFSET(CPU_FTR_TM)
2552#endif
2553
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002554 /*
2555 * Set DEC to the smaller of DEC and HDEC, so that we wake
2556 * no later than the end of our timeslice (HDEC interrupts
2557 * don't wake us from nap).
2558 */
2559 mfspr r3, SPRN_DEC
2560 mfspr r4, SPRN_HDEC
2561 mftb r5
Paul Mackerras1bc3fe82017-05-22 16:55:16 +10002562BEGIN_FTR_SECTION
2563 /* On P9 check whether the guest has large decrementer mode enabled */
2564 ld r6, HSTATE_KVM_VCORE(r13)
2565 ld r6, VCORE_LPCR(r6)
2566 andis. r6, r6, LPCR_LD@h
2567 bne 68f
2568END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras2f272462017-05-22 16:25:14 +10002569 extsw r3, r3
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000257068: EXTEND_HDEC(r4)
Paul Mackerras2f272462017-05-22 16:25:14 +10002571 cmpd r3, r4
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002572 ble 67f
2573 mtspr SPRN_DEC, r4
257467:
2575 /* save expiry time of guest decrementer */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002576 add r3, r3, r5
2577 ld r4, HSTATE_KVM_VCPU(r13)
2578 ld r5, HSTATE_KVM_VCORE(r13)
2579 ld r6, VCORE_TB_OFFSET(r5)
2580 subf r3, r6, r3 /* convert to host TB value */
2581 std r3, VCPU_DEC_EXPIRES(r4)
2582
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002583#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2584 ld r4, HSTATE_KVM_VCPU(r13)
2585 addi r3, r4, VCPU_TB_CEDE
2586 bl kvmhv_accumulate_time
2587#endif
2588
Paul Mackerrasccc07772015-03-28 14:21:07 +11002589 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2590
Paul Mackerras19ccb762011-07-23 17:42:46 +10002591 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002592 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002593 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002594 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002595 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002596 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002597kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002598 mfspr r0, SPRN_CTRLF
2599 clrrdi r0, r0, 1
2600 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302601
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002602 li r0,1
2603 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002604 mfspr r5,SPRN_LPCR
2605 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002606BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002607 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002608 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002609END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002610
2611kvm_nap_sequence: /* desired LPCR value in r5 */
2612BEGIN_FTR_SECTION
2613 /*
2614 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2615 * enable state loss = 1 (allow SMT mode switch)
2616 * requested level = 0 (just stop dispatching)
2617 */
2618 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2619 mtspr SPRN_PSSCR, r3
2620 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2621 li r4, LPCR_PECE_HVEE@higher
2622 sldi r4, r4, 32
2623 or r5, r5, r4
2624END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002625 mtspr SPRN_LPCR,r5
2626 isync
2627 li r0, 0
2628 std r0, HSTATE_SCRATCH0(r13)
2629 ptesync
2630 ld r0, HSTATE_SCRATCH0(r13)
26311: cmpd r0, r0
2632 bne 1b
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002633BEGIN_FTR_SECTION
Paul Mackerras19ccb762011-07-23 17:42:46 +10002634 nap
Paul Mackerrasbf53c882016-11-18 14:34:07 +11002635FTR_SECTION_ELSE
2636 PPC_STOP
2637ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002638 b .
2639
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100264033: mr r4, r3
2641 li r3, 0
2642 li r12, 0
2643 b 34f
2644
Paul Mackerras19ccb762011-07-23 17:42:46 +10002645kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002646 /* get vcpu pointer */
2647 ld r4, HSTATE_KVM_VCPU(r13)
2648
Paul Mackerras19ccb762011-07-23 17:42:46 +10002649 /* Woken by external or decrementer interrupt */
2650 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002651
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002652#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2653 addi r3, r4, VCPU_TB_RMINTR
2654 bl kvmhv_accumulate_time
2655#endif
2656
Paul Mackerras93d17392016-06-22 15:52:55 +10002657#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2658BEGIN_FTR_SECTION
Paul Mackerras67f8a8c2017-09-12 13:47:23 +10002659 /*
2660 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
2661 */
Paul Mackerras93d17392016-06-22 15:52:55 +10002662 bl kvmppc_restore_tm
2663END_FTR_SECTION_IFSET(CPU_FTR_TM)
2664#endif
2665
Paul Mackerras19ccb762011-07-23 17:42:46 +10002666 /* load up FP state */
2667 bl kvmppc_load_fp
2668
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002669 /* Restore guest decrementer */
2670 ld r3, VCPU_DEC_EXPIRES(r4)
2671 ld r5, HSTATE_KVM_VCORE(r13)
2672 ld r6, VCORE_TB_OFFSET(r5)
2673 add r3, r3, r6 /* convert host TB to guest TB value */
2674 mftb r7
2675 subf r3, r7, r3
2676 mtspr SPRN_DEC, r3
2677
Paul Mackerras19ccb762011-07-23 17:42:46 +10002678 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002679 ld r14, VCPU_GPR(R14)(r4)
2680 ld r15, VCPU_GPR(R15)(r4)
2681 ld r16, VCPU_GPR(R16)(r4)
2682 ld r17, VCPU_GPR(R17)(r4)
2683 ld r18, VCPU_GPR(R18)(r4)
2684 ld r19, VCPU_GPR(R19)(r4)
2685 ld r20, VCPU_GPR(R20)(r4)
2686 ld r21, VCPU_GPR(R21)(r4)
2687 ld r22, VCPU_GPR(R22)(r4)
2688 ld r23, VCPU_GPR(R23)(r4)
2689 ld r24, VCPU_GPR(R24)(r4)
2690 ld r25, VCPU_GPR(R25)(r4)
2691 ld r26, VCPU_GPR(R26)(r4)
2692 ld r27, VCPU_GPR(R27)(r4)
2693 ld r28, VCPU_GPR(R28)(r4)
2694 ld r29, VCPU_GPR(R29)(r4)
2695 ld r30, VCPU_GPR(R30)(r4)
2696 ld r31, VCPU_GPR(R31)(r4)
Suresh Warrier37f55d32016-08-19 15:35:46 +10002697
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002698 /* Check the wake reason in SRR1 to see why we got here */
2699 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002700
Suresh Warrier37f55d32016-08-19 15:35:46 +10002701 /*
2702 * Restore volatile registers since we could have called a
2703 * C routine in kvmppc_check_wake_reason
2704 * r4 = VCPU
2705 * r3 tells us whether we need to return to host or not
2706 * WARNING: it gets checked further down:
2707 * should not modify r3 until this check is done.
2708 */
2709 ld r4, HSTATE_KVM_VCPU(r13)
2710
Paul Mackerras19ccb762011-07-23 17:42:46 +10002711 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100271234: ld r5,HSTATE_KVM_VCORE(r13)
2713 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002714 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002715 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002716 addi r6,r5,VCORE_NAPPING_THREADS
271732: lwarx r7,0,r6
2718 andc r7,r7,r0
2719 stwcx. r7,0,r6
2720 bne 32b
2721 li r0,0
2722 stb r0,HSTATE_NAPPING(r13)
2723
Suresh Warrier37f55d32016-08-19 15:35:46 +10002724 /* See if the wake reason saved in r3 means we need to exit */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002725 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002726 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002727 cmpdi r3, 0
2728 bgt guest_exit_cont
Paul Mackerras4619ac82013-04-17 20:31:41 +00002729
Paul Mackerras19ccb762011-07-23 17:42:46 +10002730 /* see if any other thread is already exiting */
2731 lwz r0,VCORE_ENTRY_EXIT(r5)
2732 cmpwi r0,0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002733 bge guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002734
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002735 b kvmppc_cede_reentry /* if not go back to guest */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002736
2737 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002738kvm_cede_prodded:
2739 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002740 stb r0,VCPU_PRODDED(r3)
2741 sync /* order testing prodded vs. clearing ceded */
2742 stb r0,VCPU_CEDED(r3)
2743 li r3,H_SUCCESS
2744 blr
2745
2746 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002747kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002748 ld r9, HSTATE_KVM_VCPU(r13)
2749 b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002750
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002751 /* Try to handle a machine check in real mode */
2752machine_check_realmode:
2753 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002754 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002755 nop
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002756 ld r9, HSTATE_KVM_VCPU(r13)
2757 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302758 /*
Aravinda Prasade20bbd32017-05-11 16:33:37 +05302759 * For the guest that is FWNMI capable, deliver all the MCE errors
2760 * (handled/unhandled) by exiting the guest with KVM_EXIT_NMI exit
2761 * reason. This new approach injects machine check errors in guest
2762 * address space to guest with additional information in the form
2763 * of RTAS event, thus enabling guest kernel to suitably handle
2764 * such errors.
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302765 *
Aravinda Prasade20bbd32017-05-11 16:33:37 +05302766 * For the guest that is not FWNMI capable (old QEMU) fallback
2767 * to old behaviour for backward compatibility:
2768 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest either
2769 * through machine check interrupt (set HSRR0 to 0x200).
2770 * For handled errors (no-fatal), just go back to guest execution
2771 * with current HSRR0.
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302772 * if we receive machine check with MSR(RI=0) then deliver it to
2773 * guest as machine check causing guest to crash.
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302774 */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302775 ld r11, VCPU_MSR(r9)
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11002776 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2777 bne mc_cont /* if so, exit to host */
Aravinda Prasade20bbd32017-05-11 16:33:37 +05302778 /* Check if guest is capable of handling NMI exit */
2779 ld r10, VCPU_KVM(r9)
2780 lbz r10, KVM_FWNMI(r10)
2781 cmpdi r10, 1 /* FWNMI capable? */
2782 beq mc_cont /* if so, exit with KVM_EXIT_NMI. */
2783
2784 /* if not, fall through for backward compatibility. */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302785 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2786 beq 1f /* Deliver a machine check to guest */
2787 ld r10, VCPU_PC(r9)
2788 cmpdi r3, 0 /* Did we handle MCE ? */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302789 bne 2f /* Continue guest execution. */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002790 /* If not, deliver a machine check. SRR0/1 are already set */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +053027911: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
Michael Neulinge4e38122014-03-25 10:47:02 +11002792 bl kvmppc_msr_interrupt
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +053027932: b fast_interrupt_c_return
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002794
Paul Mackerrasde56a942011-06-29 00:21:34 +00002795/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002796 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002797 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002798 * 0 if nothing needs to be done
2799 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002800 * -1 if there was a guest wakeup (IPI or msgsnd)
Suresh Warriere3c13e52016-08-19 15:35:51 +10002801 * -2 if we handled a PCI passthrough interrupt (returned by
2802 * kvmppc_read_intr only)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002803 *
2804 * Also sets r12 to the interrupt vector for any interrupt that needs
2805 * to be handled now by the host (0x500 for external interrupt), or zero.
Suresh Warrier37f55d32016-08-19 15:35:46 +10002806 * Modifies all volatile registers (since it may call a C function).
2807 * This routine calls kvmppc_read_intr, a C function, if an external
2808 * interrupt is pending.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002809 */
2810kvmppc_check_wake_reason:
2811 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002812BEGIN_FTR_SECTION
2813 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2814FTR_SECTION_ELSE
2815 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2816ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2817 cmpwi r6, 8 /* was it an external interrupt? */
Suresh Warrier37f55d32016-08-19 15:35:46 +10002818 beq 7f /* if so, see what it was */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002819 li r3, 0
2820 li r12, 0
2821 cmpwi r6, 6 /* was it the decrementer? */
2822 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002823BEGIN_FTR_SECTION
2824 cmpwi r6, 5 /* privileged doorbell? */
2825 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002826 cmpwi r6, 3 /* hypervisor doorbell? */
2827 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002828END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302829 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2830 beq 4f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002831 li r3, 1 /* anything else, return 1 */
28320: blr
2833
Paul Mackerras5d00f662014-01-08 21:25:28 +11002834 /* hypervisor doorbell */
28353: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302836
2837 /*
2838 * Clear the doorbell as we will invoke the handler
2839 * explicitly in the guest exit path.
2840 */
2841 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2842 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11002843 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11002844 li r3, 1
Nicholas Piggin2cde3712017-10-10 20:18:28 +10002845BEGIN_FTR_SECTION
2846 PPC_MSGSYNC
2847 lwsync
2848END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
Paul Mackerras66feed62015-03-28 14:21:12 +11002849 lbz r0, HSTATE_HOST_IPI(r13)
2850 cmpwi r0, 0
2851 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302852 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002853 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11002854 blr
2855
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05302856 /* Woken up due to Hypervisor maintenance interrupt */
28574: li r12, BOOK3S_INTERRUPT_HMI
2858 li r3, 1
2859 blr
2860
Suresh Warrier37f55d32016-08-19 15:35:46 +10002861 /* external interrupt - create a stack frame so we can call C */
28627: mflr r0
2863 std r0, PPC_LR_STKOFF(r1)
2864 stdu r1, -PPC_MIN_STKFRM(r1)
2865 bl kvmppc_read_intr
2866 nop
2867 li r12, BOOK3S_INTERRUPT_EXTERNAL
Suresh Warrierf7af5202016-08-19 15:35:52 +10002868 cmpdi r3, 1
2869 ble 1f
2870
2871 /*
2872 * Return code of 2 means PCI passthrough interrupt, but
2873 * we need to return back to host to complete handling the
2874 * interrupt. Trap reason is expected in r12 by guest
2875 * exit code.
2876 */
2877 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
28781:
Suresh Warrier37f55d32016-08-19 15:35:46 +10002879 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2880 addi r1, r1, PPC_MIN_STKFRM
2881 mtlr r0
2882 blr
Paul Mackerrasde56a942011-06-29 00:21:34 +00002883
2884/*
2885 * Save away FP, VMX and VSX registers.
2886 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002887 * N.B. r30 and r31 are volatile across this function,
2888 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002889 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002890kvmppc_save_fp:
2891 mflr r30
2892 mr r31,r3
Paul Mackerras89436332012-03-02 01:38:23 +00002893 mfmsr r5
2894 ori r8,r5,MSR_FP
Paul Mackerrasde56a942011-06-29 00:21:34 +00002895#ifdef CONFIG_ALTIVEC
2896BEGIN_FTR_SECTION
2897 oris r8,r8,MSR_VEC@h
2898END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2899#endif
2900#ifdef CONFIG_VSX
2901BEGIN_FTR_SECTION
2902 oris r8,r8,MSR_VSX@h
2903END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2904#endif
2905 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002906 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002907 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002908#ifdef CONFIG_ALTIVEC
2909BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002910 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002911 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002912END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2913#endif
2914 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11002915 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11002916 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00002917 blr
2918
2919/*
2920 * Load up FP, VMX and VSX registers
2921 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002922 * N.B. r30 and r31 are volatile across this function,
2923 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002924 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002925kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11002926 mflr r30
2927 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00002928 mfmsr r9
2929 ori r8,r9,MSR_FP
2930#ifdef CONFIG_ALTIVEC
2931BEGIN_FTR_SECTION
2932 oris r8,r8,MSR_VEC@h
2933END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2934#endif
2935#ifdef CONFIG_VSX
2936BEGIN_FTR_SECTION
2937 oris r8,r8,MSR_VSX@h
2938END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2939#endif
2940 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002941 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002942 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002943#ifdef CONFIG_ALTIVEC
2944BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002945 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002946 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002947END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2948#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11002949 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002950 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11002951 mtlr r30
2952 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00002953 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10002954
Paul Mackerrasf024ee02016-06-22 14:21:59 +10002955#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2956/*
2957 * Save transactional state and TM-related registers.
2958 * Called with r9 pointing to the vcpu struct.
2959 * This can modify all checkpointed registers, but
2960 * restores r1, r2 and r9 (vcpu pointer) before exit.
2961 */
2962kvmppc_save_tm:
2963 mflr r0
2964 std r0, PPC_LR_STKOFF(r1)
2965
2966 /* Turn on TM. */
2967 mfmsr r8
2968 li r0, 1
2969 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
2970 mtmsrd r8
2971
2972 ld r5, VCPU_MSR(r9)
2973 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2974 beq 1f /* TM not active in guest. */
2975
2976 std r1, HSTATE_HOST_R1(r13)
2977 li r3, TM_CAUSE_KVM_RESCHED
2978
2979 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2980 li r5, 0
2981 mtmsrd r5, 1
2982
2983 /* All GPRs are volatile at this point. */
2984 TRECLAIM(R3)
2985
2986 /* Temporarily store r13 and r9 so we have some regs to play with */
2987 SET_SCRATCH0(r13)
2988 GET_PACA(r13)
2989 std r9, PACATMSCRATCH(r13)
2990 ld r9, HSTATE_KVM_VCPU(r13)
2991
2992 /* Get a few more GPRs free. */
2993 std r29, VCPU_GPRS_TM(29)(r9)
2994 std r30, VCPU_GPRS_TM(30)(r9)
2995 std r31, VCPU_GPRS_TM(31)(r9)
2996
2997 /* Save away PPR and DSCR soon so don't run with user values. */
2998 mfspr r31, SPRN_PPR
2999 HMT_MEDIUM
3000 mfspr r30, SPRN_DSCR
3001 ld r29, HSTATE_DSCR(r13)
3002 mtspr SPRN_DSCR, r29
3003
3004 /* Save all but r9, r13 & r29-r31 */
3005 reg = 0
3006 .rept 29
3007 .if (reg != 9) && (reg != 13)
3008 std reg, VCPU_GPRS_TM(reg)(r9)
3009 .endif
3010 reg = reg + 1
3011 .endr
3012 /* ... now save r13 */
3013 GET_SCRATCH0(r4)
3014 std r4, VCPU_GPRS_TM(13)(r9)
3015 /* ... and save r9 */
3016 ld r4, PACATMSCRATCH(r13)
3017 std r4, VCPU_GPRS_TM(9)(r9)
3018
3019 /* Reload stack pointer and TOC. */
3020 ld r1, HSTATE_HOST_R1(r13)
3021 ld r2, PACATOC(r13)
3022
3023 /* Set MSR RI now we have r1 and r13 back. */
3024 li r5, MSR_RI
3025 mtmsrd r5, 1
3026
3027 /* Save away checkpinted SPRs. */
3028 std r31, VCPU_PPR_TM(r9)
3029 std r30, VCPU_DSCR_TM(r9)
3030 mflr r5
3031 mfcr r6
3032 mfctr r7
3033 mfspr r8, SPRN_AMR
3034 mfspr r10, SPRN_TAR
Paul Mackerras0d808df2016-11-07 15:09:58 +11003035 mfxer r11
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003036 std r5, VCPU_LR_TM(r9)
3037 stw r6, VCPU_CR_TM(r9)
3038 std r7, VCPU_CTR_TM(r9)
3039 std r8, VCPU_AMR_TM(r9)
3040 std r10, VCPU_TAR_TM(r9)
Paul Mackerras0d808df2016-11-07 15:09:58 +11003041 std r11, VCPU_XER_TM(r9)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003042
3043 /* Restore r12 as trap number. */
3044 lwz r12, VCPU_TRAP(r9)
3045
3046 /* Save FP/VSX. */
3047 addi r3, r9, VCPU_FPRS_TM
3048 bl store_fp_state
3049 addi r3, r9, VCPU_VRS_TM
3050 bl store_vr_state
3051 mfspr r6, SPRN_VRSAVE
3052 stw r6, VCPU_VRSAVE_TM(r9)
30531:
3054 /*
3055 * We need to save these SPRs after the treclaim so that the software
3056 * error code is recorded correctly in the TEXASR. Also the user may
3057 * change these outside of a transaction, so they must always be
3058 * context switched.
3059 */
3060 mfspr r5, SPRN_TFHAR
3061 mfspr r6, SPRN_TFIAR
3062 mfspr r7, SPRN_TEXASR
3063 std r5, VCPU_TFHAR(r9)
3064 std r6, VCPU_TFIAR(r9)
3065 std r7, VCPU_TEXASR(r9)
3066
3067 ld r0, PPC_LR_STKOFF(r1)
3068 mtlr r0
3069 blr
3070
3071/*
3072 * Restore transactional state and TM-related registers.
3073 * Called with r4 pointing to the vcpu struct.
3074 * This potentially modifies all checkpointed registers.
3075 * It restores r1, r2, r4 from the PACA.
3076 */
3077kvmppc_restore_tm:
3078 mflr r0
3079 std r0, PPC_LR_STKOFF(r1)
3080
3081 /* Turn on TM/FP/VSX/VMX so we can restore them. */
3082 mfmsr r5
3083 li r6, MSR_TM >> 32
3084 sldi r6, r6, 32
3085 or r5, r5, r6
3086 ori r5, r5, MSR_FP
3087 oris r5, r5, (MSR_VEC | MSR_VSX)@h
3088 mtmsrd r5
3089
3090 /*
3091 * The user may change these outside of a transaction, so they must
3092 * always be context switched.
3093 */
3094 ld r5, VCPU_TFHAR(r4)
3095 ld r6, VCPU_TFIAR(r4)
3096 ld r7, VCPU_TEXASR(r4)
3097 mtspr SPRN_TFHAR, r5
3098 mtspr SPRN_TFIAR, r6
3099 mtspr SPRN_TEXASR, r7
3100
3101 ld r5, VCPU_MSR(r4)
3102 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
3103 beqlr /* TM not active in guest */
3104 std r1, HSTATE_HOST_R1(r13)
3105
3106 /* Make sure the failure summary is set, otherwise we'll program check
3107 * when we trechkpt. It's possible that this might have been not set
3108 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
3109 * host.
3110 */
3111 oris r7, r7, (TEXASR_FS)@h
3112 mtspr SPRN_TEXASR, r7
3113
3114 /*
3115 * We need to load up the checkpointed state for the guest.
3116 * We need to do this early as it will blow away any GPRs, VSRs and
3117 * some SPRs.
3118 */
3119
3120 mr r31, r4
3121 addi r3, r31, VCPU_FPRS_TM
3122 bl load_fp_state
3123 addi r3, r31, VCPU_VRS_TM
3124 bl load_vr_state
3125 mr r4, r31
3126 lwz r7, VCPU_VRSAVE_TM(r4)
3127 mtspr SPRN_VRSAVE, r7
3128
3129 ld r5, VCPU_LR_TM(r4)
3130 lwz r6, VCPU_CR_TM(r4)
3131 ld r7, VCPU_CTR_TM(r4)
3132 ld r8, VCPU_AMR_TM(r4)
3133 ld r9, VCPU_TAR_TM(r4)
Paul Mackerras0d808df2016-11-07 15:09:58 +11003134 ld r10, VCPU_XER_TM(r4)
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003135 mtlr r5
3136 mtcr r6
3137 mtctr r7
3138 mtspr SPRN_AMR, r8
3139 mtspr SPRN_TAR, r9
Paul Mackerras0d808df2016-11-07 15:09:58 +11003140 mtxer r10
Paul Mackerrasf024ee02016-06-22 14:21:59 +10003141
3142 /*
3143 * Load up PPR and DSCR values but don't put them in the actual SPRs
3144 * till the last moment to avoid running with userspace PPR and DSCR for
3145 * too long.
3146 */
3147 ld r29, VCPU_DSCR_TM(r4)
3148 ld r30, VCPU_PPR_TM(r4)
3149
3150 std r2, PACATMSCRATCH(r13) /* Save TOC */
3151
3152 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
3153 li r5, 0
3154 mtmsrd r5, 1
3155
3156 /* Load GPRs r0-r28 */
3157 reg = 0
3158 .rept 29
3159 ld reg, VCPU_GPRS_TM(reg)(r31)
3160 reg = reg + 1
3161 .endr
3162
3163 mtspr SPRN_DSCR, r29
3164 mtspr SPRN_PPR, r30
3165
3166 /* Load final GPRs */
3167 ld 29, VCPU_GPRS_TM(29)(r31)
3168 ld 30, VCPU_GPRS_TM(30)(r31)
3169 ld 31, VCPU_GPRS_TM(31)(r31)
3170
3171 /* TM checkpointed state is now setup. All GPRs are now volatile. */
3172 TRECHKPT
3173
3174 /* Now let's get back the state we need. */
3175 HMT_MEDIUM
3176 GET_PACA(r13)
3177 ld r29, HSTATE_DSCR(r13)
3178 mtspr SPRN_DSCR, r29
3179 ld r4, HSTATE_KVM_VCPU(r13)
3180 ld r1, HSTATE_HOST_R1(r13)
3181 ld r2, PACATMSCRATCH(r13)
3182
3183 /* Set the MSR RI since we have our registers back. */
3184 li r5, MSR_RI
3185 mtmsrd r5, 1
3186
3187 ld r0, PPC_LR_STKOFF(r1)
3188 mtlr r0
3189 blr
3190#endif
3191
Paul Mackerras44a3add2013-10-04 21:45:04 +10003192/*
3193 * We come here if we get any exception or interrupt while we are
3194 * executing host real mode code while in guest MMU context.
Paul Mackerras857b99e2017-09-01 16:17:27 +10003195 * r12 is (CR << 32) | vector
3196 * r13 points to our PACA
3197 * r12 is saved in HSTATE_SCRATCH0(r13)
3198 * ctr is saved in HSTATE_SCRATCH1(r13) if RELOCATABLE
3199 * r9 is saved in HSTATE_SCRATCH2(r13)
3200 * r13 is saved in HSPRG1
3201 * cfar is saved in HSTATE_CFAR(r13)
3202 * ppr is saved in HSTATE_PPR(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10003203 */
3204kvmppc_bad_host_intr:
Paul Mackerras857b99e2017-09-01 16:17:27 +10003205 /*
3206 * Switch to the emergency stack, but start half-way down in
3207 * case we were already on it.
3208 */
3209 mr r9, r1
3210 std r1, PACAR1(r13)
3211 ld r1, PACAEMERGSP(r13)
3212 subi r1, r1, THREAD_SIZE/2 + INT_FRAME_SIZE
3213 std r9, 0(r1)
3214 std r0, GPR0(r1)
3215 std r9, GPR1(r1)
3216 std r2, GPR2(r1)
3217 SAVE_4GPRS(3, r1)
3218 SAVE_2GPRS(7, r1)
3219 srdi r0, r12, 32
3220 clrldi r12, r12, 32
3221 std r0, _CCR(r1)
3222 std r12, _TRAP(r1)
3223 andi. r0, r12, 2
3224 beq 1f
3225 mfspr r3, SPRN_HSRR0
3226 mfspr r4, SPRN_HSRR1
3227 mfspr r5, SPRN_HDAR
3228 mfspr r6, SPRN_HDSISR
3229 b 2f
32301: mfspr r3, SPRN_SRR0
3231 mfspr r4, SPRN_SRR1
3232 mfspr r5, SPRN_DAR
3233 mfspr r6, SPRN_DSISR
32342: std r3, _NIP(r1)
3235 std r4, _MSR(r1)
3236 std r5, _DAR(r1)
3237 std r6, _DSISR(r1)
3238 ld r9, HSTATE_SCRATCH2(r13)
3239 ld r12, HSTATE_SCRATCH0(r13)
3240 GET_SCRATCH0(r0)
3241 SAVE_4GPRS(9, r1)
3242 std r0, GPR13(r1)
3243 SAVE_NVGPRS(r1)
3244 ld r5, HSTATE_CFAR(r13)
3245 std r5, ORIG_GPR3(r1)
3246 mflr r3
3247#ifdef CONFIG_RELOCATABLE
3248 ld r4, HSTATE_SCRATCH1(r13)
3249#else
3250 mfctr r4
3251#endif
3252 mfxer r5
3253 lbz r6, PACASOFTIRQEN(r13)
3254 std r3, _LINK(r1)
3255 std r4, _CTR(r1)
3256 std r5, _XER(r1)
3257 std r6, SOFTE(r1)
3258 ld r2, PACATOC(r13)
3259 LOAD_REG_IMMEDIATE(3, 0x7265677368657265)
3260 std r3, STACK_FRAME_OVERHEAD-16(r1)
3261
3262 /*
3263 * On POWER9 do a minimal restore of the MMU and call C code,
3264 * which will print a message and panic.
3265 * XXX On POWER7 and POWER8, we just spin here since we don't
3266 * know what the other threads are doing (and we don't want to
3267 * coordinate with them) - but at least we now have register state
3268 * in memory that we might be able to look at from another CPU.
3269 */
3270BEGIN_FTR_SECTION
Paul Mackerras44a3add2013-10-04 21:45:04 +10003271 b .
Paul Mackerras857b99e2017-09-01 16:17:27 +10003272END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
3273 ld r9, HSTATE_KVM_VCPU(r13)
3274 ld r10, VCPU_KVM(r9)
3275
3276 li r0, 0
3277 mtspr SPRN_AMR, r0
3278 mtspr SPRN_IAMR, r0
3279 mtspr SPRN_CIABR, r0
3280 mtspr SPRN_DAWRX, r0
3281
3282 /* Flush the ERAT on radix P9 DD1 guest exit */
3283BEGIN_FTR_SECTION
3284 PPC_INVALIDATE_ERAT
3285END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
3286
3287BEGIN_MMU_FTR_SECTION
3288 b 4f
3289END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
3290
3291 slbmte r0, r0
3292 slbia
3293 ptesync
3294 ld r8, PACA_SLBSHADOWPTR(r13)
3295 .rept SLB_NUM_BOLTED
3296 li r3, SLBSHADOW_SAVEAREA
3297 LDX_BE r5, r8, r3
3298 addi r3, r3, 8
3299 LDX_BE r6, r8, r3
3300 andis. r7, r5, SLB_ESID_V@h
3301 beq 3f
3302 slbmte r6, r5
33033: addi r8, r8, 16
3304 .endr
3305
33064: lwz r7, KVM_HOST_LPID(r10)
3307 mtspr SPRN_LPID, r7
3308 mtspr SPRN_PID, r0
3309 ld r8, KVM_HOST_LPCR(r10)
3310 mtspr SPRN_LPCR, r8
3311 isync
3312 li r0, KVM_GUEST_MODE_NONE
3313 stb r0, HSTATE_IN_GUEST(r13)
3314
3315 /*
3316 * Turn on the MMU and jump to C code
3317 */
3318 bcl 20, 31, .+4
33195: mflr r3
3320 addi r3, r3, 9f - 5b
3321 ld r4, PACAKMSR(r13)
3322 mtspr SPRN_SRR0, r3
3323 mtspr SPRN_SRR1, r4
3324 rfid
33259: addi r3, r1, STACK_FRAME_OVERHEAD
3326 bl kvmppc_bad_interrupt
3327 b 9b
Michael Neulinge4e38122014-03-25 10:47:02 +11003328
3329/*
3330 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
3331 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3332 * r11 has the guest MSR value (in/out)
3333 * r9 has a vcpu pointer (in)
3334 * r0 is used as a scratch register
3335 */
3336kvmppc_msr_interrupt:
3337 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
3338 cmpwi r0, 2 /* Check if we are in transactional state.. */
3339 ld r11, VCPU_INTR_MSR(r9)
3340 bne 1f
3341 /* ... if transactional, change to suspended */
3342 li r0, 1
33431: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3344 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10003345
3346/*
3347 * This works around a hardware bug on POWER8E processors, where
3348 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3349 * performance monitor interrupt. Instead, when we need to have
3350 * an interrupt pending, we have to arrange for a counter to overflow.
3351 */
3352kvmppc_fix_pmao:
3353 li r3, 0
3354 mtspr SPRN_MMCR2, r3
3355 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3356 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3357 mtspr SPRN_MMCR0, r3
3358 lis r3, 0x7fff
3359 ori r3, r3, 0xffff
3360 mtspr SPRN_PMC6, r3
3361 isync
3362 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11003363
3364#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3365/*
3366 * Start timing an activity
3367 * r3 = pointer to time accumulation struct, r4 = vcpu
3368 */
3369kvmhv_start_timing:
3370 ld r5, HSTATE_KVM_VCORE(r13)
3371 lbz r6, VCORE_IN_GUEST(r5)
3372 cmpwi r6, 0
3373 beq 5f /* if in guest, need to */
3374 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
33755: mftb r5
3376 subf r5, r6, r5
3377 std r3, VCPU_CUR_ACTIVITY(r4)
3378 std r5, VCPU_ACTIVITY_START(r4)
3379 blr
3380
3381/*
3382 * Accumulate time to one activity and start another.
3383 * r3 = pointer to new time accumulation struct, r4 = vcpu
3384 */
3385kvmhv_accumulate_time:
3386 ld r5, HSTATE_KVM_VCORE(r13)
3387 lbz r8, VCORE_IN_GUEST(r5)
3388 cmpwi r8, 0
3389 beq 4f /* if in guest, need to */
3390 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
33914: ld r5, VCPU_CUR_ACTIVITY(r4)
3392 ld r6, VCPU_ACTIVITY_START(r4)
3393 std r3, VCPU_CUR_ACTIVITY(r4)
3394 mftb r7
3395 subf r7, r8, r7
3396 std r7, VCPU_ACTIVITY_START(r4)
3397 cmpdi r5, 0
3398 beqlr
3399 subf r3, r6, r7
3400 ld r8, TAS_SEQCOUNT(r5)
3401 cmpdi r8, 0
3402 addi r8, r8, 1
3403 std r8, TAS_SEQCOUNT(r5)
3404 lwsync
3405 ld r7, TAS_TOTAL(r5)
3406 add r7, r7, r3
3407 std r7, TAS_TOTAL(r5)
3408 ld r6, TAS_MIN(r5)
3409 ld r7, TAS_MAX(r5)
3410 beq 3f
3411 cmpd r3, r6
3412 bge 1f
34133: std r3, TAS_MIN(r5)
34141: cmpd r3, r7
3415 ble 2f
3416 std r3, TAS_MAX(r5)
34172: lwsync
3418 addi r8, r8, 1
3419 std r8, TAS_SEQCOUNT(r5)
3420 blr
3421#endif