blob: e571ad277398fd6625499c05ae03cde47a46d20a [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>
32
33#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
Paul Mackerrasde56a942011-06-29 00:21:34 +000034
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110035/* Values in HSTATE_NAPPING(r13) */
36#define NAPPING_CEDE 1
37#define NAPPING_NOVCPU 2
38
Paul Mackerrasde56a942011-06-29 00:21:34 +000039/*
Paul Mackerras19ccb762011-07-23 17:42:46 +100040 * Call kvmppc_hv_entry in real mode.
Paul Mackerrasde56a942011-06-29 00:21:34 +000041 * Must be called with interrupts hard-disabled.
42 *
43 * Input Registers:
44 *
45 * LR = return address to continue at after eventually re-enabling MMU
46 */
Anton Blanchard6ed179b2014-06-12 18:16:53 +100047_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
Paul Mackerras218309b2013-09-06 13:23:44 +100048 mflr r0
49 std r0, PPC_LR_STKOFF(r1)
50 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +000051 mfmsr r10
Paul Mackerras218309b2013-09-06 13:23:44 +100052 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
Paul Mackerrasde56a942011-06-29 00:21:34 +000053 li r0,MSR_RI
54 andc r0,r10,r0
55 li r6,MSR_IR | MSR_DR
56 andc r6,r10,r6
57 mtmsrd r0,1 /* clear RI in MSR */
58 mtsrr0 r5
59 mtsrr1 r6
60 RFI
61
Paul Mackerras218309b2013-09-06 13:23:44 +100062kvmppc_call_hv_entry:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +110063 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100064 bl kvmppc_hv_entry
65
66 /* Back from guest - restore host state and return to caller */
67
Michael Neulingeee7ff92014-01-08 21:25:19 +110068BEGIN_FTR_SECTION
Paul Mackerras218309b2013-09-06 13:23:44 +100069 /* Restore host DABR and DABRX */
70 ld r5,HSTATE_DABR(r13)
71 li r6,7
72 mtspr SPRN_DABR,r5
73 mtspr SPRN_DABRX,r6
Michael Neulingeee7ff92014-01-08 21:25:19 +110074END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +100075
76 /* Restore SPRG3 */
Scott Wood9d378df2014-03-10 17:29:38 -050077 ld r3,PACA_SPRG_VDSO(r13)
78 mtspr SPRN_SPRG_VDSO_WRITE,r3
Paul Mackerras218309b2013-09-06 13:23:44 +100079
Paul Mackerras218309b2013-09-06 13:23:44 +100080 /* Reload the host's PMU registers */
81 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
82 lbz r4, LPPACA_PMCINUSE(r3)
83 cmpwi r4, 0
84 beq 23f /* skip if not */
Paul Mackerras9bc01a92014-05-26 19:48:40 +100085BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +100086 ld r3, HSTATE_MMCR0(r13)
Paul Mackerras9bc01a92014-05-26 19:48:40 +100087 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
88 cmpwi r4, MMCR0_PMAO
89 beql kvmppc_fix_pmao
90END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +100091 lwz r3, HSTATE_PMC1(r13)
92 lwz r4, HSTATE_PMC2(r13)
93 lwz r5, HSTATE_PMC3(r13)
94 lwz r6, HSTATE_PMC4(r13)
95 lwz r8, HSTATE_PMC5(r13)
96 lwz r9, HSTATE_PMC6(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +100097 mtspr SPRN_PMC1, r3
98 mtspr SPRN_PMC2, r4
99 mtspr SPRN_PMC3, r5
100 mtspr SPRN_PMC4, r6
101 mtspr SPRN_PMC5, r8
102 mtspr SPRN_PMC6, r9
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000103 ld r3, HSTATE_MMCR0(r13)
104 ld r4, HSTATE_MMCR1(r13)
105 ld r5, HSTATE_MMCRA(r13)
106 ld r6, HSTATE_SIAR(r13)
107 ld r7, HSTATE_SDAR(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000108 mtspr SPRN_MMCR1, r4
109 mtspr SPRN_MMCRA, r5
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100110 mtspr SPRN_SIAR, r6
111 mtspr SPRN_SDAR, r7
112BEGIN_FTR_SECTION
Michael Ellerman9a4fc4e2014-07-10 19:34:31 +1000113 ld r8, HSTATE_MMCR2(r13)
114 ld r9, HSTATE_SIER(r13)
Paul Mackerras72cde5a2014-03-25 10:47:08 +1100115 mtspr SPRN_MMCR2, r8
116 mtspr SPRN_SIER, r9
117END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras218309b2013-09-06 13:23:44 +1000118 mtspr SPRN_MMCR0, r3
119 isync
12023:
121
122 /*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100123 * Reload DEC. HDEC interrupts were disabled when
124 * we reloaded the host's LPCR value.
125 */
126 ld r3, HSTATE_DECEXP(r13)
127 mftb r4
128 subf r4, r4, r3
129 mtspr SPRN_DEC, r4
130
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000131 /* hwthread_req may have got set by cede or no vcpu, so clear it */
132 li r0, 0
133 stb r0, HSTATE_HWTHREAD_REQ(r13)
134
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100135 /*
Paul Mackerras218309b2013-09-06 13:23:44 +1000136 * For external and machine check interrupts, we need
137 * to call the Linux handler to process the interrupt.
138 * We do that by jumping to absolute address 0x500 for
139 * external interrupts, or the machine_check_fwnmi label
140 * for machine checks (since firmware might have patched
141 * the vector area at 0x200). The [h]rfid at the end of the
142 * handler will return to the book3s_hv_interrupts.S code.
143 * For other interrupts we do the rfid to get back
144 * to the book3s_hv_interrupts.S code here.
145 */
146 ld r8, 112+PPC_LR_STKOFF(r1)
147 addi r1, r1, 112
148 ld r7, HSTATE_HOST_MSR(r13)
149
150 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
151 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras218309b2013-09-06 13:23:44 +1000152 beq 11f
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +0530153 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
154 beq 15f /* Invoke the H_DOORBELL handler */
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530155 cmpwi cr2, r12, BOOK3S_INTERRUPT_HMI
156 beq cr2, 14f /* HMI check */
Paul Mackerras218309b2013-09-06 13:23:44 +1000157
158 /* RFI into the highmem handler, or branch to interrupt handler */
159 mfmsr r6
160 li r0, MSR_RI
161 andc r6, r6, r0
162 mtmsrd r6, 1 /* Clear RI in MSR */
163 mtsrr0 r8
164 mtsrr1 r7
Paul Mackerras218309b2013-09-06 13:23:44 +1000165 beq cr1, 13f /* machine check */
166 RFI
167
168 /* On POWER7, we have external interrupts set to use HSRR0/1 */
16911: mtspr SPRN_HSRR0, r8
170 mtspr SPRN_HSRR1, r7
171 ba 0x500
172
17313: b machine_check_fwnmi
174
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +053017514: mtspr SPRN_HSRR0, r8
176 mtspr SPRN_HSRR1, r7
177 b hmi_exception_after_realmode
178
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +053017915: mtspr SPRN_HSRR0, r8
180 mtspr SPRN_HSRR1, r7
181 ba 0xe80
182
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100183kvmppc_primary_no_guest:
184 /* We handle this much like a ceded vcpu */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100185 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
186 mfspr r3, SPRN_HDEC
187 mtspr SPRN_DEC, r3
Paul Mackerras6af27c82015-03-28 14:21:10 +1100188 /*
189 * Make sure the primary has finished the MMU switch.
190 * We should never get here on a secondary thread, but
191 * check it for robustness' sake.
192 */
193 ld r5, HSTATE_KVM_VCORE(r13)
19465: lbz r0, VCORE_IN_GUEST(r5)
195 cmpwi r0, 0
196 beq 65b
197 /* Set LPCR. */
198 ld r8,VCORE_LPCR(r5)
199 mtspr SPRN_LPCR,r8
200 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100201 /* set our bit in napping_threads */
202 ld r5, HSTATE_KVM_VCORE(r13)
203 lbz r7, HSTATE_PTID(r13)
204 li r0, 1
205 sld r0, r0, r7
206 addi r6, r5, VCORE_NAPPING_THREADS
2071: lwarx r3, 0, r6
208 or r3, r3, r0
209 stwcx. r3, 0, r6
210 bne 1b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100211 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100212 isync
213 li r12, 0
214 lwz r7, VCORE_ENTRY_EXIT(r5)
215 cmpwi r7, 0x100
216 bge kvm_novcpu_exit /* another thread already exiting */
217 li r3, NAPPING_NOVCPU
218 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100219
Paul Mackerrasccc07772015-03-28 14:21:07 +1100220 li r3, 0 /* Don't wake on privileged (OS) doorbell */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100221 b kvm_do_nap
222
223kvm_novcpu_wakeup:
224 ld r1, HSTATE_HOST_R1(r13)
225 ld r5, HSTATE_KVM_VCORE(r13)
226 li r0, 0
227 stb r0, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100228
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100229 /* check the wake reason */
230 bl kvmppc_check_wake_reason
Paul Mackerras6af27c82015-03-28 14:21:10 +1100231
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100232 /* see if any other thread is already exiting */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100233 lwz r0, VCORE_ENTRY_EXIT(r5)
234 cmpwi r0, 0x100
235 bge kvm_novcpu_exit
236
237 /* clear our bit in napping_threads */
238 lbz r7, HSTATE_PTID(r13)
239 li r0, 1
240 sld r0, r0, r7
241 addi r6, r5, VCORE_NAPPING_THREADS
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002424: lwarx r7, 0, r6
243 andc r7, r7, r0
244 stwcx. r7, 0, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100245 bne 4b
246
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100247 /* See if the wake reason means we need to exit */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100248 cmpdi r3, 0
249 bge kvm_novcpu_exit
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100250
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100251 /* See if our timeslice has expired (HDEC is negative) */
252 mfspr r0, SPRN_HDEC
253 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
254 cmpwi r0, 0
255 blt kvm_novcpu_exit
256
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100257 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
258 ld r4, HSTATE_KVM_VCPU(r13)
259 cmpdi r4, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100260 beq kvmppc_primary_no_guest
261
262#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
263 addi r3, r4, VCPU_TB_RMENTRY
264 bl kvmhv_start_timing
265#endif
266 b kvmppc_got_guest
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100267
268kvm_novcpu_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100269#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
270 ld r4, HSTATE_KVM_VCPU(r13)
271 cmpdi r4, 0
272 beq 13f
273 addi r3, r4, VCPU_TB_RMEXIT
274 bl kvmhv_accumulate_time
275#endif
Paul Mackerraseddb60f2015-03-28 14:21:11 +110027613: mr r3, r12
277 stw r12, 112-4(r1)
278 bl kvmhv_commence_exit
279 nop
280 lwz r12, 112-4(r1)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100281 b kvmhv_switch_to_host
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100282
Paul Mackerras371fefd2011-06-29 00:23:08 +0000283/*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100284 * We come in here when wakened from nap mode.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000285 * Relocation is off and most register values are lost.
286 * r13 points to the PACA.
287 */
288 .globl kvm_start_guest
289kvm_start_guest:
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530290
291 /* Set runlatch bit the minute you wake up from nap */
Paul Mackerras1f09c3e2015-03-28 14:21:04 +1100292 mfspr r0, SPRN_CTRLF
293 ori r0, r0, 1
294 mtspr SPRN_CTRLT, r0
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530295
Paul Mackerras19ccb762011-07-23 17:42:46 +1000296 ld r2,PACATOC(r13)
297
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000298 li r0,KVM_HWTHREAD_IN_KVM
299 stb r0,HSTATE_HWTHREAD_STATE(r13)
300
301 /* NV GPR values from power7_idle() will no longer be valid */
302 li r0,1
303 stb r0,PACA_NAPSTATELOST(r13)
304
Paul Mackerras4619ac82013-04-17 20:31:41 +0000305 /* were we napping due to cede? */
306 lbz r0,HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100307 cmpwi r0,NAPPING_CEDE
308 beq kvm_end_cede
309 cmpwi r0,NAPPING_NOVCPU
310 beq kvm_novcpu_wakeup
311
312 ld r1,PACAEMERGSP(r13)
313 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras4619ac82013-04-17 20:31:41 +0000314
315 /*
316 * We weren't napping due to cede, so this must be a secondary
317 * thread being woken up to run a guest, or being woken up due
318 * to a stray IPI. (Or due to some machine check or hypervisor
319 * maintenance interrupt while the core is in KVM.)
320 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000321
322 /* Check the wake reason in SRR1 to see why we got here */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100323 bl kvmppc_check_wake_reason
324 cmpdi r3, 0
325 bge kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000326
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000327 /* get vcore pointer, NULL if we have nothing to run */
328 ld r5,HSTATE_KVM_VCORE(r13)
329 cmpdi r5,0
330 /* if we have no vcore to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000331 beq kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000332
Paul Mackerras56548fc2014-12-03 14:48:40 +1100333kvm_secondary_got_guest:
334
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100335 /* Set HSTATE_DSCR(r13) to something sensible */
Anshuman Khandual1db36522015-05-21 12:13:03 +0530336 ld r6, PACA_DSCR_DEFAULT(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100337 std r6, HSTATE_DSCR(r13)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000338
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000339 /* On thread 0 of a subcore, set HDEC to max */
340 lbz r4, HSTATE_PTID(r13)
341 cmpwi r4, 0
342 bne 63f
343 lis r6, 0x7fff
344 ori r6, r6, 0xffff
345 mtspr SPRN_HDEC, r6
346 /* and set per-LPAR registers, if doing dynamic micro-threading */
347 ld r6, HSTATE_SPLIT_MODE(r13)
348 cmpdi r6, 0
349 beq 63f
350 ld r0, KVM_SPLIT_RPR(r6)
351 mtspr SPRN_RPR, r0
352 ld r0, KVM_SPLIT_PMMAR(r6)
353 mtspr SPRN_PMMAR, r0
354 ld r0, KVM_SPLIT_LDBAR(r6)
355 mtspr SPRN_LDBAR, r0
356 isync
35763:
358 /* Order load of vcpu after load of vcore */
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100359 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000360 ld r4, HSTATE_KVM_VCPU(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100361 bl kvmppc_hv_entry
Paul Mackerras218309b2013-09-06 13:23:44 +1000362
363 /* Back from the guest, go back to nap */
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000364 /* Clear our vcpu and vcore pointers so we don't come back in early */
Paul Mackerras218309b2013-09-06 13:23:44 +1000365 li r0, 0
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000366 std r0, HSTATE_KVM_VCPU(r13)
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100367 /*
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000368 * Once we clear HSTATE_KVM_VCORE(r13), the code in
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100369 * kvmppc_run_core() is going to assume that all our vcpu
370 * state is visible in memory. This lwsync makes sure
371 * that that is true.
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100372 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000373 lwsync
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000374 std r0, HSTATE_KVM_VCORE(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000375
Paul Mackerras56548fc2014-12-03 14:48:40 +1100376/*
377 * At this point we have finished executing in the guest.
378 * We need to wait for hwthread_req to become zero, since
379 * we may not turn on the MMU while hwthread_req is non-zero.
380 * While waiting we also need to check if we get given a vcpu to run.
381 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000382kvm_no_guest:
Paul Mackerras56548fc2014-12-03 14:48:40 +1100383 lbz r3, HSTATE_HWTHREAD_REQ(r13)
384 cmpwi r3, 0
385 bne 53f
386 HMT_MEDIUM
387 li r0, KVM_HWTHREAD_IN_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000388 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerras56548fc2014-12-03 14:48:40 +1100389 /* need to recheck hwthread_req after a barrier, to avoid race */
390 sync
391 lbz r3, HSTATE_HWTHREAD_REQ(r13)
392 cmpwi r3, 0
393 bne 54f
394/*
395 * We jump to power7_wakeup_loss, which will return to the caller
396 * of power7_nap in the powernv cpu offline loop. The value we
397 * put in r3 becomes the return value for power7_nap.
398 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000399 li r3, LPCR_PECE0
400 mfspr r4, SPRN_LPCR
401 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
402 mtspr SPRN_LPCR, r4
Paul Mackerras56548fc2014-12-03 14:48:40 +1100403 li r3, 0
404 b power7_wakeup_loss
405
40653: HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000407 ld r5, HSTATE_KVM_VCORE(r13)
408 cmpdi r5, 0
409 bne 60f
410 ld r3, HSTATE_SPLIT_MODE(r13)
411 cmpdi r3, 0
412 beq kvm_no_guest
413 lbz r0, KVM_SPLIT_DO_NAP(r3)
414 cmpwi r0, 0
Paul Mackerras56548fc2014-12-03 14:48:40 +1100415 beq kvm_no_guest
416 HMT_MEDIUM
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000417 b kvm_unsplit_nap
41860: HMT_MEDIUM
Paul Mackerras56548fc2014-12-03 14:48:40 +1100419 b kvm_secondary_got_guest
420
42154: li r0, KVM_HWTHREAD_IN_KVM
422 stb r0, HSTATE_HWTHREAD_STATE(r13)
423 b kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000424
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000425/*
426 * Here the primary thread is trying to return the core to
427 * whole-core mode, so we need to nap.
428 */
429kvm_unsplit_nap:
Gautham R. Shenoy7f235322015-09-02 21:48:58 +0530430 /*
431 * Ensure that secondary doesn't nap when it has
432 * its vcore pointer set.
433 */
434 sync /* matches smp_mb() before setting split_info.do_nap */
435 ld r0, HSTATE_KVM_VCORE(r13)
436 cmpdi r0, 0
437 bne kvm_no_guest
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000438 /* clear any pending message */
439BEGIN_FTR_SECTION
440 lis r6, (PPC_DBELL_SERVER << (63-36))@h
441 PPC_MSGCLR(6)
442END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
443 /* Set kvm_split_mode.napped[tid] = 1 */
444 ld r3, HSTATE_SPLIT_MODE(r13)
445 li r0, 1
446 lhz r4, PACAPACAINDEX(r13)
447 clrldi r4, r4, 61 /* micro-threading => P8 => 8 threads/core */
448 addi r4, r4, KVM_SPLIT_NAPPED
449 stbx r0, r3, r4
450 /* Check the do_nap flag again after setting napped[] */
451 sync
452 lbz r0, KVM_SPLIT_DO_NAP(r3)
453 cmpwi r0, 0
454 beq 57f
455 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
456 mfspr r4, SPRN_LPCR
457 rlwimi r4, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
458 mtspr SPRN_LPCR, r4
459 isync
460 std r0, HSTATE_SCRATCH0(r13)
461 ptesync
462 ld r0, HSTATE_SCRATCH0(r13)
4631: cmpd r0, r0
464 bne 1b
465 nap
466 b .
467
46857: li r0, 0
469 stbx r0, r3, r4
470 b kvm_no_guest
471
Paul Mackerras218309b2013-09-06 13:23:44 +1000472/******************************************************************************
473 * *
474 * Entry code *
475 * *
476 *****************************************************************************/
477
Paul Mackerrasde56a942011-06-29 00:21:34 +0000478.global kvmppc_hv_entry
479kvmppc_hv_entry:
480
481 /* Required state:
482 *
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100483 * R4 = vcpu pointer (or NULL)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000484 * MSR = ~IR|DR
485 * R13 = PACA
486 * R1 = host R1
Michael Neuling06a29e42014-08-19 14:59:30 +1000487 * R2 = TOC
Paul Mackerrasde56a942011-06-29 00:21:34 +0000488 * all other volatile GPRS = free
489 */
490 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000491 std r0, PPC_LR_STKOFF(r1)
492 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000493
Paul Mackerrasde56a942011-06-29 00:21:34 +0000494 /* Save R1 in the PACA */
495 std r1, HSTATE_HOST_R1(r13)
496
Paul Mackerras44a3add2013-10-04 21:45:04 +1000497 li r6, KVM_GUEST_MODE_HOST_HV
498 stb r6, HSTATE_IN_GUEST(r13)
499
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100500#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
501 /* Store initial timestamp */
502 cmpdi r4, 0
503 beq 1f
504 addi r3, r4, VCPU_TB_RMENTRY
505 bl kvmhv_start_timing
5061:
507#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +0000508 /* Clear out SLB */
509 li r6,0
510 slbmte r6,r6
511 slbia
512 ptesync
513
Paul Mackerras9e368f22011-06-29 00:40:08 +0000514 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100515 * POWER7/POWER8 host -> guest partition switch code.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000516 * We don't have to lock against concurrent tlbies,
517 * but we do have to coordinate across hardware threads.
518 */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100519 /* Set bit in entry map iff exit map is zero. */
520 ld r5, HSTATE_KVM_VCORE(r13)
521 li r7, 1
522 lbz r6, HSTATE_PTID(r13)
523 sld r7, r7, r6
524 addi r9, r5, VCORE_ENTRY_EXIT
52521: lwarx r3, 0, r9
526 cmpwi r3, 0x100 /* any threads starting to exit? */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000527 bge secondary_too_late /* if so we're too late to the party */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100528 or r3, r3, r7
529 stwcx. r3, 0, r9
Paul Mackerras371fefd2011-06-29 00:23:08 +0000530 bne 21b
531
532 /* Primary thread switches to guest partition. */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100533 ld r9,VCORE_KVM(r5) /* pointer to struct kvm */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000534 cmpwi r6,0
Paul Mackerras6af27c82015-03-28 14:21:10 +1100535 bne 10f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000536 ld r6,KVM_SDR1(r9)
537 lwz r7,KVM_LPID(r9)
538 li r0,LPID_RSVD /* switch to reserved LPID */
539 mtspr SPRN_LPID,r0
540 ptesync
541 mtspr SPRN_SDR1,r6 /* switch to partition page table */
542 mtspr SPRN_LPID,r7
543 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000544
545 /* See if we need to flush the TLB */
546 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
547 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
548 srdi r6,r6,6 /* doubleword number */
549 sldi r6,r6,3 /* address offset */
550 add r6,r6,r9
551 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000552 li r0,1
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000553 sld r0,r0,r7
554 ld r7,0(r6)
555 and. r7,r7,r0
556 beq 22f
55723: ldarx r7,0,r6 /* if set, clear the bit */
558 andc r7,r7,r0
559 stdcx. r7,0,r6
560 bne 23b
Paul Mackerrasca252052014-01-08 21:25:22 +1100561 /* Flush the TLB of any entries for this LPID */
562 /* use arch 2.07S as a proxy for POWER8 */
563BEGIN_FTR_SECTION
564 li r6,512 /* POWER8 has 512 sets */
565FTR_SECTION_ELSE
566 li r6,128 /* POWER7 has 128 sets */
567ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000568 mtctr r6
569 li r7,0x800 /* IS field = 0b10 */
570 ptesync
57128: tlbiel r7
572 addi r7,r7,0x1000
573 bdnz 28b
574 ptesync
575
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000576 /* Add timebase offset onto timebase */
57722: ld r8,VCORE_TB_OFFSET(r5)
578 cmpdi r8,0
579 beq 37f
580 mftb r6 /* current host timebase */
581 add r8,r8,r6
582 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
583 mftb r7 /* check if lower 24 bits overflowed */
584 clrldi r6,r6,40
585 clrldi r7,r7,40
586 cmpld r7,r6
587 bge 37f
588 addis r8,r8,0x100 /* if so, increment upper 40 bits */
589 mtspr SPRN_TBU40,r8
590
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000591 /* Load guest PCR value to select appropriate compat mode */
59237: ld r7, VCORE_PCR(r5)
593 cmpdi r7, 0
594 beq 38f
595 mtspr SPRN_PCR, r7
59638:
Michael Neulingb005255e2014-01-08 21:25:21 +1100597
598BEGIN_FTR_SECTION
599 /* DPDES is shared between threads */
600 ld r8, VCORE_DPDES(r5)
601 mtspr SPRN_DPDES, r8
602END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
603
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000604 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000605 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000606
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100607 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110060810: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100609 beq kvmppc_primary_no_guest
610kvmppc_got_guest:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000611
612 /* Load up guest SLB entries */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100613 lwz r5,VCPU_SLB_MAX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000614 cmpwi r5,0
615 beq 9f
616 mtctr r5
617 addi r6,r4,VCPU_SLB
6181: ld r8,VCPU_SLB_E(r6)
619 ld r9,VCPU_SLB_V(r6)
620 slbmte r9,r8
621 addi r6,r6,VCPU_SLB_SIZE
622 bdnz 1b
6239:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100624 /* Increment yield count if they have a VPA */
625 ld r3, VCPU_VPA(r4)
626 cmpdi r3, 0
627 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200628 li r6, LPPACA_YIELDCOUNT
629 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100630 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200631 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100632 li r6, 1
633 stb r6, VCPU_VPA_DIRTY(r4)
63425:
635
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100636 /* Save purr/spurr */
637 mfspr r5,SPRN_PURR
638 mfspr r6,SPRN_SPURR
639 std r5,HSTATE_PURR(r13)
640 std r6,HSTATE_SPURR(r13)
641 ld r7,VCPU_PURR(r4)
642 ld r8,VCPU_SPURR(r4)
643 mtspr SPRN_PURR,r7
644 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100645
Michael Neulingeee7ff92014-01-08 21:25:19 +1100646BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000647 /* Set partition DABR */
648 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100649 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000650 ld r6,VCPU_DABR(r4)
651 mtspr SPRN_DABRX,r5
652 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000653 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100654END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000655
Michael Neulinge4e38122014-03-25 10:47:02 +1100656#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
657BEGIN_FTR_SECTION
658 b skip_tm
659END_FTR_SECTION_IFCLR(CPU_FTR_TM)
660
661 /* Turn on TM/FP/VSX/VMX so we can restore them. */
662 mfmsr r5
663 li r6, MSR_TM >> 32
664 sldi r6, r6, 32
665 or r5, r5, r6
666 ori r5, r5, MSR_FP
667 oris r5, r5, (MSR_VEC | MSR_VSX)@h
668 mtmsrd r5
669
670 /*
671 * The user may change these outside of a transaction, so they must
672 * always be context switched.
673 */
674 ld r5, VCPU_TFHAR(r4)
675 ld r6, VCPU_TFIAR(r4)
676 ld r7, VCPU_TEXASR(r4)
677 mtspr SPRN_TFHAR, r5
678 mtspr SPRN_TFIAR, r6
679 mtspr SPRN_TEXASR, r7
680
681 ld r5, VCPU_MSR(r4)
682 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
683 beq skip_tm /* TM not active in guest */
684
685 /* Make sure the failure summary is set, otherwise we'll program check
686 * when we trechkpt. It's possible that this might have been not set
687 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
688 * host.
689 */
690 oris r7, r7, (TEXASR_FS)@h
691 mtspr SPRN_TEXASR, r7
692
693 /*
694 * We need to load up the checkpointed state for the guest.
695 * We need to do this early as it will blow away any GPRs, VSRs and
696 * some SPRs.
697 */
698
699 mr r31, r4
700 addi r3, r31, VCPU_FPRS_TM
Alexander Graf9bf163f2014-06-16 14:41:15 +0200701 bl load_fp_state
Michael Neulinge4e38122014-03-25 10:47:02 +1100702 addi r3, r31, VCPU_VRS_TM
Alexander Graf9bf163f2014-06-16 14:41:15 +0200703 bl load_vr_state
Michael Neulinge4e38122014-03-25 10:47:02 +1100704 mr r4, r31
705 lwz r7, VCPU_VRSAVE_TM(r4)
706 mtspr SPRN_VRSAVE, r7
707
708 ld r5, VCPU_LR_TM(r4)
709 lwz r6, VCPU_CR_TM(r4)
710 ld r7, VCPU_CTR_TM(r4)
711 ld r8, VCPU_AMR_TM(r4)
712 ld r9, VCPU_TAR_TM(r4)
713 mtlr r5
714 mtcr r6
715 mtctr r7
716 mtspr SPRN_AMR, r8
717 mtspr SPRN_TAR, r9
718
719 /*
720 * Load up PPR and DSCR values but don't put them in the actual SPRs
721 * till the last moment to avoid running with userspace PPR and DSCR for
722 * too long.
723 */
724 ld r29, VCPU_DSCR_TM(r4)
725 ld r30, VCPU_PPR_TM(r4)
726
727 std r2, PACATMSCRATCH(r13) /* Save TOC */
728
729 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
730 li r5, 0
731 mtmsrd r5, 1
732
733 /* Load GPRs r0-r28 */
734 reg = 0
735 .rept 29
736 ld reg, VCPU_GPRS_TM(reg)(r31)
737 reg = reg + 1
738 .endr
739
740 mtspr SPRN_DSCR, r29
741 mtspr SPRN_PPR, r30
742
743 /* Load final GPRs */
744 ld 29, VCPU_GPRS_TM(29)(r31)
745 ld 30, VCPU_GPRS_TM(30)(r31)
746 ld 31, VCPU_GPRS_TM(31)(r31)
747
748 /* TM checkpointed state is now setup. All GPRs are now volatile. */
749 TRECHKPT
750
751 /* Now let's get back the state we need. */
752 HMT_MEDIUM
753 GET_PACA(r13)
754 ld r29, HSTATE_DSCR(r13)
755 mtspr SPRN_DSCR, r29
756 ld r4, HSTATE_KVM_VCPU(r13)
757 ld r1, HSTATE_HOST_R1(r13)
758 ld r2, PACATMSCRATCH(r13)
759
760 /* Set the MSR RI since we have our registers back. */
761 li r5, MSR_RI
762 mtmsrd r5, 1
763skip_tm:
764#endif
765
Paul Mackerrasde56a942011-06-29 00:21:34 +0000766 /* Load guest PMU registers */
767 /* R4 is live here (vcpu pointer) */
768 li r3, 1
769 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
770 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
771 isync
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000772BEGIN_FTR_SECTION
773 ld r3, VCPU_MMCR(r4)
774 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
775 cmpwi r5, MMCR0_PMAO
776 beql kvmppc_fix_pmao
777END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000778 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
779 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
780 lwz r6, VCPU_PMC + 8(r4)
781 lwz r7, VCPU_PMC + 12(r4)
782 lwz r8, VCPU_PMC + 16(r4)
783 lwz r9, VCPU_PMC + 20(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000784 mtspr SPRN_PMC1, r3
785 mtspr SPRN_PMC2, r5
786 mtspr SPRN_PMC3, r6
787 mtspr SPRN_PMC4, r7
788 mtspr SPRN_PMC5, r8
789 mtspr SPRN_PMC6, r9
Paul Mackerrasde56a942011-06-29 00:21:34 +0000790 ld r3, VCPU_MMCR(r4)
791 ld r5, VCPU_MMCR + 8(r4)
792 ld r6, VCPU_MMCR + 16(r4)
793 ld r7, VCPU_SIAR(r4)
794 ld r8, VCPU_SDAR(r4)
795 mtspr SPRN_MMCR1, r5
796 mtspr SPRN_MMCRA, r6
797 mtspr SPRN_SIAR, r7
798 mtspr SPRN_SDAR, r8
Michael Neulingb005255e2014-01-08 21:25:21 +1100799BEGIN_FTR_SECTION
800 ld r5, VCPU_MMCR + 24(r4)
801 ld r6, VCPU_SIER(r4)
802 lwz r7, VCPU_PMC + 24(r4)
803 lwz r8, VCPU_PMC + 28(r4)
804 ld r9, VCPU_MMCR + 32(r4)
805 mtspr SPRN_MMCR2, r5
806 mtspr SPRN_SIER, r6
807 mtspr SPRN_SPMC1, r7
808 mtspr SPRN_SPMC2, r8
809 mtspr SPRN_MMCRS, r9
810END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000811 mtspr SPRN_MMCR0, r3
812 isync
813
814 /* Load up FP, VMX and VSX registers */
815 bl kvmppc_load_fp
816
817 ld r14, VCPU_GPR(R14)(r4)
818 ld r15, VCPU_GPR(R15)(r4)
819 ld r16, VCPU_GPR(R16)(r4)
820 ld r17, VCPU_GPR(R17)(r4)
821 ld r18, VCPU_GPR(R18)(r4)
822 ld r19, VCPU_GPR(R19)(r4)
823 ld r20, VCPU_GPR(R20)(r4)
824 ld r21, VCPU_GPR(R21)(r4)
825 ld r22, VCPU_GPR(R22)(r4)
826 ld r23, VCPU_GPR(R23)(r4)
827 ld r24, VCPU_GPR(R24)(r4)
828 ld r25, VCPU_GPR(R25)(r4)
829 ld r26, VCPU_GPR(R26)(r4)
830 ld r27, VCPU_GPR(R27)(r4)
831 ld r28, VCPU_GPR(R28)(r4)
832 ld r29, VCPU_GPR(R29)(r4)
833 ld r30, VCPU_GPR(R30)(r4)
834 ld r31, VCPU_GPR(R31)(r4)
835
Paul Mackerrasde56a942011-06-29 00:21:34 +0000836 /* Switch DSCR to guest value */
837 ld r5, VCPU_DSCR(r4)
838 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000839
Michael Neulingb005255e2014-01-08 21:25:21 +1100840BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100841 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100842 b 8f
843END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
844 /* Turn on TM so we can access TFHAR/TFIAR/TEXASR */
845 mfmsr r8
846 li r0, 1
847 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
848 mtmsrd r8
849
850 /* Load up POWER8-specific registers */
851 ld r5, VCPU_IAMR(r4)
852 lwz r6, VCPU_PSPB(r4)
853 ld r7, VCPU_FSCR(r4)
854 mtspr SPRN_IAMR, r5
855 mtspr SPRN_PSPB, r6
856 mtspr SPRN_FSCR, r7
857 ld r5, VCPU_DAWR(r4)
858 ld r6, VCPU_DAWRX(r4)
859 ld r7, VCPU_CIABR(r4)
860 ld r8, VCPU_TAR(r4)
861 mtspr SPRN_DAWR, r5
862 mtspr SPRN_DAWRX, r6
863 mtspr SPRN_CIABR, r7
864 mtspr SPRN_TAR, r8
865 ld r5, VCPU_IC(r4)
866 ld r6, VCPU_VTB(r4)
867 mtspr SPRN_IC, r5
868 mtspr SPRN_VTB, r6
Michael Neuling7b490412014-01-08 21:25:32 +1100869 ld r8, VCPU_EBBHR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100870 mtspr SPRN_EBBHR, r8
871 ld r5, VCPU_EBBRR(r4)
872 ld r6, VCPU_BESCR(r4)
873 ld r7, VCPU_CSIGR(r4)
874 ld r8, VCPU_TACR(r4)
875 mtspr SPRN_EBBRR, r5
876 mtspr SPRN_BESCR, r6
877 mtspr SPRN_CSIGR, r7
878 mtspr SPRN_TACR, r8
879 ld r5, VCPU_TCSCR(r4)
880 ld r6, VCPU_ACOP(r4)
881 lwz r7, VCPU_GUEST_PID(r4)
882 ld r8, VCPU_WORT(r4)
883 mtspr SPRN_TCSCR, r5
884 mtspr SPRN_ACOP, r6
885 mtspr SPRN_PID, r7
886 mtspr SPRN_WORT, r8
8878:
888
Paul Mackerrasde56a942011-06-29 00:21:34 +0000889 /*
890 * Set the decrementer to the guest decrementer.
891 */
892 ld r8,VCPU_DEC_EXPIRES(r4)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +1100893 /* r8 is a host timebase value here, convert to guest TB */
894 ld r5,HSTATE_KVM_VCORE(r13)
895 ld r6,VCORE_TB_OFFSET(r5)
896 add r8,r8,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000897 mftb r7
898 subf r3,r7,r8
899 mtspr SPRN_DEC,r3
900 stw r3,VCPU_DEC(r4)
901
902 ld r5, VCPU_SPRG0(r4)
903 ld r6, VCPU_SPRG1(r4)
904 ld r7, VCPU_SPRG2(r4)
905 ld r8, VCPU_SPRG3(r4)
906 mtspr SPRN_SPRG0, r5
907 mtspr SPRN_SPRG1, r6
908 mtspr SPRN_SPRG2, r7
909 mtspr SPRN_SPRG3, r8
910
Paul Mackerrasde56a942011-06-29 00:21:34 +0000911 /* Load up DAR and DSISR */
912 ld r5, VCPU_DAR(r4)
913 lwz r6, VCPU_DSISR(r4)
914 mtspr SPRN_DAR, r5
915 mtspr SPRN_DSISR, r6
916
Paul Mackerrasde56a942011-06-29 00:21:34 +0000917 /* Restore AMR and UAMOR, set AMOR to all 1s */
918 ld r5,VCPU_AMR(r4)
919 ld r6,VCPU_UAMOR(r4)
920 li r7,-1
921 mtspr SPRN_AMR,r5
922 mtspr SPRN_UAMOR,r6
923 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000924
925 /* Restore state of CTRL run bit; assume 1 on entry */
926 lwz r5,VCPU_CTRL(r4)
927 andi. r5,r5,1
928 bne 4f
929 mfspr r6,SPRN_CTRLF
930 clrrdi r6,r6,1
931 mtspr SPRN_CTRLT,r6
9324:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100933 /* Secondary threads wait for primary to have done partition switch */
934 ld r5, HSTATE_KVM_VCORE(r13)
935 lbz r6, HSTATE_PTID(r13)
936 cmpwi r6, 0
937 beq 21f
938 lbz r0, VCORE_IN_GUEST(r5)
939 cmpwi r0, 0
940 bne 21f
941 HMT_LOW
Paul Mackerrasb4deba52015-07-02 20:38:16 +100094220: lwz r3, VCORE_ENTRY_EXIT(r5)
943 cmpwi r3, 0x100
944 bge no_switch_exit
945 lbz r0, VCORE_IN_GUEST(r5)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100946 cmpwi r0, 0
947 beq 20b
948 HMT_MEDIUM
94921:
950 /* Set LPCR. */
951 ld r8,VCORE_LPCR(r5)
952 mtspr SPRN_LPCR,r8
953 isync
954
955 /* Check if HDEC expires soon */
956 mfspr r3, SPRN_HDEC
957 cmpwi r3, 512 /* 1 microsecond */
958 blt hdec_soon
959
Paul Mackerrasde56a942011-06-29 00:21:34 +0000960 ld r6, VCPU_CTR(r4)
Sam bobroffc63517c2015-05-27 09:56:57 +1000961 ld r7, VCPU_XER(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000962
963 mtctr r6
964 mtxer r7
965
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100966kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000967 ld r10, VCPU_PC(r4)
968 ld r11, VCPU_MSR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000969 ld r6, VCPU_SRR0(r4)
970 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100971 mtspr SPRN_SRR0, r6
972 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000973
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100974deliver_guest_interrupt:
Paul Mackerras4619ac82013-04-17 20:31:41 +0000975 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000976 rldicl r11, r11, 63 - MSR_HV_LG, 1
977 rotldi r11, r11, 1 + MSR_HV_LG
978 ori r11, r11, MSR_ME
979
Paul Mackerras19ccb762011-07-23 17:42:46 +1000980 /* Check if we can deliver an external or decrementer interrupt now */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100981 ld r0, VCPU_PENDING_EXC(r4)
982 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
983 cmpdi cr1, r0, 0
984 andi. r8, r11, MSR_EE
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100985 mfspr r8, SPRN_LPCR
986 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
987 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
988 mtspr SPRN_LPCR, r8
Paul Mackerras19ccb762011-07-23 17:42:46 +1000989 isync
Paul Mackerras19ccb762011-07-23 17:42:46 +1000990 beq 5f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100991 li r0, BOOK3S_INTERRUPT_EXTERNAL
992 bne cr1, 12f
993 mfspr r0, SPRN_DEC
994 cmpwi r0, 0
995 li r0, BOOK3S_INTERRUPT_DECREMENTER
996 bge 5f
997
99812: mtspr SPRN_SRR0, r10
Paul Mackerras19ccb762011-07-23 17:42:46 +1000999 mr r10,r0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001000 mtspr SPRN_SRR1, r11
Michael Neulinge4e38122014-03-25 10:47:02 +11001001 mr r9, r4
1002 bl kvmppc_msr_interrupt
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +110010035:
Paul Mackerras19ccb762011-07-23 17:42:46 +10001004
Liu Ping Fan27025a62013-11-19 14:12:48 +08001005/*
1006 * Required state:
1007 * R4 = vcpu
1008 * R10: value for HSRR0
1009 * R11: value for HSRR1
1010 * R13 = PACA
1011 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001012fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +00001013 li r0,0
1014 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001015 mtspr SPRN_HSRR0,r10
1016 mtspr SPRN_HSRR1,r11
1017
1018 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001019 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +00001020 stb r9, HSTATE_IN_GUEST(r13)
1021
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001022#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1023 /* Accumulate timing */
1024 addi r3, r4, VCPU_TB_GUEST
1025 bl kvmhv_accumulate_time
1026#endif
1027
Paul Mackerrasde56a942011-06-29 00:21:34 +00001028 /* Enter guest */
1029
Paul Mackerras0acb9112013-02-04 18:10:51 +00001030BEGIN_FTR_SECTION
1031 ld r5, VCPU_CFAR(r4)
1032 mtspr SPRN_CFAR, r5
1033END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001034BEGIN_FTR_SECTION
1035 ld r0, VCPU_PPR(r4)
1036END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001037
Paul Mackerrasde56a942011-06-29 00:21:34 +00001038 ld r5, VCPU_LR(r4)
1039 lwz r6, VCPU_CR(r4)
1040 mtlr r5
1041 mtcr r6
1042
Michael Neulingc75df6f2012-06-25 13:33:10 +00001043 ld r1, VCPU_GPR(R1)(r4)
1044 ld r2, VCPU_GPR(R2)(r4)
1045 ld r3, VCPU_GPR(R3)(r4)
1046 ld r5, VCPU_GPR(R5)(r4)
1047 ld r6, VCPU_GPR(R6)(r4)
1048 ld r7, VCPU_GPR(R7)(r4)
1049 ld r8, VCPU_GPR(R8)(r4)
1050 ld r9, VCPU_GPR(R9)(r4)
1051 ld r10, VCPU_GPR(R10)(r4)
1052 ld r11, VCPU_GPR(R11)(r4)
1053 ld r12, VCPU_GPR(R12)(r4)
1054 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001055
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001056BEGIN_FTR_SECTION
1057 mtspr SPRN_PPR, r0
1058END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1059 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001060 ld r4, VCPU_GPR(R4)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001061
1062 hrfid
1063 b .
1064
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001065secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001066 li r12, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001067 cmpdi r4, 0
1068 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +11001069 stw r12, VCPU_TRAP(r4)
1070#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001071 addi r3, r4, VCPU_TB_RMEXIT
1072 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +11001073#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100107411: b kvmhv_switch_to_host
1075
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001076no_switch_exit:
1077 HMT_MEDIUM
1078 li r12, 0
1079 b 12f
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001080hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +11001081 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000108212: stw r12, VCPU_TRAP(r4)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001083 mr r9, r4
1084#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001085 addi r3, r4, VCPU_TB_RMEXIT
1086 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001087#endif
Paul Mackerras6af27c82015-03-28 14:21:10 +11001088 b guest_exit_cont
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001089
Paul Mackerrasde56a942011-06-29 00:21:34 +00001090/******************************************************************************
1091 * *
1092 * Exit code *
1093 * *
1094 *****************************************************************************/
1095
1096/*
1097 * We come here from the first-level interrupt handlers.
1098 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301099 .globl kvmppc_interrupt_hv
1100kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001101 /*
1102 * Register contents:
1103 * R12 = interrupt vector
1104 * R13 = PACA
1105 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
1106 * guest R13 saved in SPRN_SCRATCH0
1107 */
Aneesh Kumar K.V36e7bb32013-11-11 19:29:47 +05301108 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001109
1110 lbz r9, HSTATE_IN_GUEST(r13)
1111 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1112 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301113#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1114 cmpwi r9, KVM_GUEST_MODE_GUEST
Aneesh Kumar K.V36e7bb32013-11-11 19:29:47 +05301115 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301116 beq kvmppc_interrupt_pr
1117#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001118 /* We're now back in the host but in guest MMU context */
1119 li r9, KVM_GUEST_MODE_HOST_HV
1120 stb r9, HSTATE_IN_GUEST(r13)
1121
Paul Mackerrasde56a942011-06-29 00:21:34 +00001122 ld r9, HSTATE_KVM_VCPU(r13)
1123
1124 /* Save registers */
1125
Michael Neulingc75df6f2012-06-25 13:33:10 +00001126 std r0, VCPU_GPR(R0)(r9)
1127 std r1, VCPU_GPR(R1)(r9)
1128 std r2, VCPU_GPR(R2)(r9)
1129 std r3, VCPU_GPR(R3)(r9)
1130 std r4, VCPU_GPR(R4)(r9)
1131 std r5, VCPU_GPR(R5)(r9)
1132 std r6, VCPU_GPR(R6)(r9)
1133 std r7, VCPU_GPR(R7)(r9)
1134 std r8, VCPU_GPR(R8)(r9)
Aneesh Kumar K.V36e7bb32013-11-11 19:29:47 +05301135 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001136 std r0, VCPU_GPR(R9)(r9)
1137 std r10, VCPU_GPR(R10)(r9)
1138 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001139 ld r3, HSTATE_SCRATCH0(r13)
1140 lwz r4, HSTATE_SCRATCH1(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001141 std r3, VCPU_GPR(R12)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001142 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001143BEGIN_FTR_SECTION
1144 ld r3, HSTATE_CFAR(r13)
1145 std r3, VCPU_CFAR(r9)
1146END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001147BEGIN_FTR_SECTION
1148 ld r4, HSTATE_PPR(r13)
1149 std r4, VCPU_PPR(r9)
1150END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001151
1152 /* Restore R1/R2 so we can handle faults */
1153 ld r1, HSTATE_HOST_R1(r13)
1154 ld r2, PACATOC(r13)
1155
1156 mfspr r10, SPRN_SRR0
1157 mfspr r11, SPRN_SRR1
1158 std r10, VCPU_SRR0(r9)
1159 std r11, VCPU_SRR1(r9)
1160 andi. r0, r12, 2 /* need to read HSRR0/1? */
1161 beq 1f
1162 mfspr r10, SPRN_HSRR0
1163 mfspr r11, SPRN_HSRR1
1164 clrrdi r12, r12, 2
11651: std r10, VCPU_PC(r9)
1166 std r11, VCPU_MSR(r9)
1167
1168 GET_SCRATCH0(r3)
1169 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001170 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001171 std r4, VCPU_LR(r9)
1172
Paul Mackerrasde56a942011-06-29 00:21:34 +00001173 stw r12,VCPU_TRAP(r9)
1174
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001175#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1176 addi r3, r9, VCPU_TB_RMINTR
1177 mr r4, r9
1178 bl kvmhv_accumulate_time
1179 ld r5, VCPU_GPR(R5)(r9)
1180 ld r6, VCPU_GPR(R6)(r9)
1181 ld r7, VCPU_GPR(R7)(r9)
1182 ld r8, VCPU_GPR(R8)(r9)
1183#endif
1184
Paul Mackerras4a157d62014-12-03 13:30:39 +11001185 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001186 if this is an HEI (HV emulation interrupt, e40) */
1187 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001188 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001189 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1190 bne 11f
1191 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100119211: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001193
1194 /* these are volatile across C function calls */
1195 mfctr r3
1196 mfxer r4
1197 std r3, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001198 std r4, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001199
Paul Mackerras697d3892011-12-12 12:36:37 +00001200 /* If this is a page table miss then see if it's theirs or ours */
1201 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1202 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +00001203 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1204 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +00001205
Paul Mackerrasde56a942011-06-29 00:21:34 +00001206 /* See if this is a leftover HDEC interrupt */
1207 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1208 bne 2f
1209 mfspr r3,SPRN_HDEC
1210 cmpwi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001211 mr r4,r9
1212 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000012132:
Paul Mackerras697d3892011-12-12 12:36:37 +00001214 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001215 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1216 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001217
Paul Mackerras66feed62015-03-28 14:21:12 +11001218 /* Hypervisor doorbell - exit only if host IPI flag set */
1219 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1220 bne 3f
1221 lbz r0, HSTATE_HOST_IPI(r13)
Gautham R. Shenoy06554d92015-08-07 17:41:20 +05301222 cmpwi r0, 0
Paul Mackerras66feed62015-03-28 14:21:12 +11001223 beq 4f
1224 b guest_exit_cont
12253:
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001226 /* External interrupt ? */
1227 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001228 bne+ guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001229
1230 /* External interrupt, first check for host_ipi. If this is
1231 * set, we know the host wants us out so let's do it now
1232 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001233 bl kvmppc_read_intr
1234 cmpdi r3, 0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001235 bgt guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001236
Paul Mackerras4619ac82013-04-17 20:31:41 +00001237 /* Check if any CPU is heading out to the host, if so head out too */
Paul Mackerras66feed62015-03-28 14:21:12 +110012384: ld r5, HSTATE_KVM_VCORE(r13)
Paul Mackerras4619ac82013-04-17 20:31:41 +00001239 lwz r0, VCORE_ENTRY_EXIT(r5)
1240 cmpwi r0, 0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001241 mr r4, r9
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001242 blt deliver_guest_interrupt
Paul Mackerrasde56a942011-06-29 00:21:34 +00001243
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001244guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001245 /* Save more register state */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001246 mfdar r6
1247 mfdsisr r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001248 std r6, VCPU_DAR(r9)
1249 stw r7, VCPU_DSISR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001250 /* don't overwrite fault_dar/fault_dsisr if HDSI */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001251 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
Paul Mackerras6af27c82015-03-28 14:21:10 +11001252 beq mc_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001253 std r6, VCPU_FAULT_DAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001254 stw r7, VCPU_FAULT_DSISR(r9)
1255
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001256 /* See if it is a machine check */
1257 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1258 beq machine_check_realmode
1259mc_cont:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001260#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1261 addi r3, r9, VCPU_TB_RMEXIT
1262 mr r4, r9
1263 bl kvmhv_accumulate_time
1264#endif
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001265
Gautham R. Shenoy7e022e72015-05-21 13:57:04 +05301266 mr r3, r12
Paul Mackerras6af27c82015-03-28 14:21:10 +11001267 /* Increment exit count, poke other threads to exit */
1268 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001269 nop
1270 ld r9, HSTATE_KVM_VCPU(r13)
1271 lwz r12, VCPU_TRAP(r9)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001272
Paul Mackerrasec257162015-06-24 21:18:03 +10001273 /* Stop others sending VCPU interrupts to this physical CPU */
1274 li r0, -1
1275 stw r0, VCPU_CPU(r9)
1276 stw r0, VCPU_THREAD_CPU(r9)
1277
Paul Mackerrasde56a942011-06-29 00:21:34 +00001278 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001279 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001280 stw r6,VCPU_CTRL(r9)
1281 andi. r0,r6,1
1282 bne 4f
1283 ori r6,r6,1
1284 mtspr SPRN_CTRLT,r6
12854:
1286 /* Read the guest SLB and save it away */
1287 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1288 mtctr r0
1289 li r6,0
1290 addi r7,r9,VCPU_SLB
1291 li r5,0
12921: slbmfee r8,r6
1293 andis. r0,r8,SLB_ESID_V@h
1294 beq 2f
1295 add r8,r8,r6 /* put index in */
1296 slbmfev r3,r6
1297 std r8,VCPU_SLB_E(r7)
1298 std r3,VCPU_SLB_V(r7)
1299 addi r7,r7,VCPU_SLB_SIZE
1300 addi r5,r5,1
13012: addi r6,r6,1
1302 bdnz 1b
1303 stw r5,VCPU_SLB_MAX(r9)
1304
1305 /*
1306 * Save the guest PURR/SPURR
1307 */
1308 mfspr r5,SPRN_PURR
1309 mfspr r6,SPRN_SPURR
1310 ld r7,VCPU_PURR(r9)
1311 ld r8,VCPU_SPURR(r9)
1312 std r5,VCPU_PURR(r9)
1313 std r6,VCPU_SPURR(r9)
1314 subf r5,r7,r5
1315 subf r6,r8,r6
1316
1317 /*
1318 * Restore host PURR/SPURR and add guest times
1319 * so that the time in the guest gets accounted.
1320 */
1321 ld r3,HSTATE_PURR(r13)
1322 ld r4,HSTATE_SPURR(r13)
1323 add r3,r3,r5
1324 add r4,r4,r6
1325 mtspr SPRN_PURR,r3
1326 mtspr SPRN_SPURR,r4
1327
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001328 /* Save DEC */
1329 mfspr r5,SPRN_DEC
1330 mftb r6
1331 extsw r5,r5
1332 add r5,r5,r6
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001333 /* r5 is a guest timebase value here, convert to host TB */
1334 ld r3,HSTATE_KVM_VCORE(r13)
1335 ld r4,VCORE_TB_OFFSET(r3)
1336 subf r5,r4,r5
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001337 std r5,VCPU_DEC_EXPIRES(r9)
1338
Michael Neulingb005255e2014-01-08 21:25:21 +11001339BEGIN_FTR_SECTION
1340 b 8f
1341END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001342 /* Save POWER8-specific registers */
1343 mfspr r5, SPRN_IAMR
1344 mfspr r6, SPRN_PSPB
1345 mfspr r7, SPRN_FSCR
1346 std r5, VCPU_IAMR(r9)
1347 stw r6, VCPU_PSPB(r9)
1348 std r7, VCPU_FSCR(r9)
1349 mfspr r5, SPRN_IC
1350 mfspr r6, SPRN_VTB
1351 mfspr r7, SPRN_TAR
1352 std r5, VCPU_IC(r9)
1353 std r6, VCPU_VTB(r9)
1354 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001355 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001356 std r8, VCPU_EBBHR(r9)
1357 mfspr r5, SPRN_EBBRR
1358 mfspr r6, SPRN_BESCR
1359 mfspr r7, SPRN_CSIGR
1360 mfspr r8, SPRN_TACR
1361 std r5, VCPU_EBBRR(r9)
1362 std r6, VCPU_BESCR(r9)
1363 std r7, VCPU_CSIGR(r9)
1364 std r8, VCPU_TACR(r9)
1365 mfspr r5, SPRN_TCSCR
1366 mfspr r6, SPRN_ACOP
1367 mfspr r7, SPRN_PID
1368 mfspr r8, SPRN_WORT
1369 std r5, VCPU_TCSCR(r9)
1370 std r6, VCPU_ACOP(r9)
1371 stw r7, VCPU_GUEST_PID(r9)
1372 std r8, VCPU_WORT(r9)
Paul Mackerrasccec4452016-03-05 19:34:39 +11001373 /*
1374 * Restore various registers to 0, where non-zero values
1375 * set by the guest could disrupt the host.
1376 */
1377 li r0, 0
1378 mtspr SPRN_IAMR, r0
1379 mtspr SPRN_CIABR, r0
1380 mtspr SPRN_DAWRX, r0
1381 mtspr SPRN_TCSCR, r0
1382 mtspr SPRN_WORT, r0
1383 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1384 li r0, 1
1385 sldi r0, r0, 31
1386 mtspr SPRN_MMCRS, r0
Michael Neulingb005255e2014-01-08 21:25:21 +110013878:
1388
Paul Mackerrasde56a942011-06-29 00:21:34 +00001389 /* Save and reset AMR and UAMOR before turning on the MMU */
1390 mfspr r5,SPRN_AMR
1391 mfspr r6,SPRN_UAMOR
1392 std r5,VCPU_AMR(r9)
1393 std r6,VCPU_UAMOR(r9)
1394 li r6,0
1395 mtspr SPRN_AMR,r6
1396
Paul Mackerrasde56a942011-06-29 00:21:34 +00001397 /* Switch DSCR back to host value */
1398 mfspr r8, SPRN_DSCR
1399 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001400 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001401 mtspr SPRN_DSCR, r7
1402
1403 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001404 std r14, VCPU_GPR(R14)(r9)
1405 std r15, VCPU_GPR(R15)(r9)
1406 std r16, VCPU_GPR(R16)(r9)
1407 std r17, VCPU_GPR(R17)(r9)
1408 std r18, VCPU_GPR(R18)(r9)
1409 std r19, VCPU_GPR(R19)(r9)
1410 std r20, VCPU_GPR(R20)(r9)
1411 std r21, VCPU_GPR(R21)(r9)
1412 std r22, VCPU_GPR(R22)(r9)
1413 std r23, VCPU_GPR(R23)(r9)
1414 std r24, VCPU_GPR(R24)(r9)
1415 std r25, VCPU_GPR(R25)(r9)
1416 std r26, VCPU_GPR(R26)(r9)
1417 std r27, VCPU_GPR(R27)(r9)
1418 std r28, VCPU_GPR(R28)(r9)
1419 std r29, VCPU_GPR(R29)(r9)
1420 std r30, VCPU_GPR(R30)(r9)
1421 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001422
1423 /* Save SPRGs */
1424 mfspr r3, SPRN_SPRG0
1425 mfspr r4, SPRN_SPRG1
1426 mfspr r5, SPRN_SPRG2
1427 mfspr r6, SPRN_SPRG3
1428 std r3, VCPU_SPRG0(r9)
1429 std r4, VCPU_SPRG1(r9)
1430 std r5, VCPU_SPRG2(r9)
1431 std r6, VCPU_SPRG3(r9)
1432
Paul Mackerras89436332012-03-02 01:38:23 +00001433 /* save FP state */
1434 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001435 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001436
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001437#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1438BEGIN_FTR_SECTION
1439 b 2f
1440END_FTR_SECTION_IFCLR(CPU_FTR_TM)
1441 /* Turn on TM. */
1442 mfmsr r8
1443 li r0, 1
1444 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
1445 mtmsrd r8
1446
1447 ld r5, VCPU_MSR(r9)
1448 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
1449 beq 1f /* TM not active in guest. */
1450
1451 li r3, TM_CAUSE_KVM_RESCHED
1452
1453 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
1454 li r5, 0
1455 mtmsrd r5, 1
1456
1457 /* All GPRs are volatile at this point. */
1458 TRECLAIM(R3)
1459
1460 /* Temporarily store r13 and r9 so we have some regs to play with */
1461 SET_SCRATCH0(r13)
1462 GET_PACA(r13)
1463 std r9, PACATMSCRATCH(r13)
1464 ld r9, HSTATE_KVM_VCPU(r13)
1465
1466 /* Get a few more GPRs free. */
1467 std r29, VCPU_GPRS_TM(29)(r9)
1468 std r30, VCPU_GPRS_TM(30)(r9)
1469 std r31, VCPU_GPRS_TM(31)(r9)
1470
1471 /* Save away PPR and DSCR soon so don't run with user values. */
1472 mfspr r31, SPRN_PPR
1473 HMT_MEDIUM
1474 mfspr r30, SPRN_DSCR
1475 ld r29, HSTATE_DSCR(r13)
1476 mtspr SPRN_DSCR, r29
1477
1478 /* Save all but r9, r13 & r29-r31 */
1479 reg = 0
1480 .rept 29
1481 .if (reg != 9) && (reg != 13)
1482 std reg, VCPU_GPRS_TM(reg)(r9)
1483 .endif
1484 reg = reg + 1
1485 .endr
1486 /* ... now save r13 */
1487 GET_SCRATCH0(r4)
1488 std r4, VCPU_GPRS_TM(13)(r9)
1489 /* ... and save r9 */
1490 ld r4, PACATMSCRATCH(r13)
1491 std r4, VCPU_GPRS_TM(9)(r9)
1492
1493 /* Reload stack pointer and TOC. */
1494 ld r1, HSTATE_HOST_R1(r13)
1495 ld r2, PACATOC(r13)
1496
1497 /* Set MSR RI now we have r1 and r13 back. */
1498 li r5, MSR_RI
1499 mtmsrd r5, 1
1500
1501 /* Save away checkpinted SPRs. */
1502 std r31, VCPU_PPR_TM(r9)
1503 std r30, VCPU_DSCR_TM(r9)
1504 mflr r5
1505 mfcr r6
1506 mfctr r7
1507 mfspr r8, SPRN_AMR
1508 mfspr r10, SPRN_TAR
1509 std r5, VCPU_LR_TM(r9)
1510 stw r6, VCPU_CR_TM(r9)
1511 std r7, VCPU_CTR_TM(r9)
1512 std r8, VCPU_AMR_TM(r9)
1513 std r10, VCPU_TAR_TM(r9)
1514
1515 /* Restore r12 as trap number. */
1516 lwz r12, VCPU_TRAP(r9)
1517
1518 /* Save FP/VSX. */
1519 addi r3, r9, VCPU_FPRS_TM
Alexander Graf9bf163f2014-06-16 14:41:15 +02001520 bl store_fp_state
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001521 addi r3, r9, VCPU_VRS_TM
Alexander Graf9bf163f2014-06-16 14:41:15 +02001522 bl store_vr_state
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001523 mfspr r6, SPRN_VRSAVE
1524 stw r6, VCPU_VRSAVE_TM(r9)
15251:
1526 /*
1527 * We need to save these SPRs after the treclaim so that the software
1528 * error code is recorded correctly in the TEXASR. Also the user may
1529 * change these outside of a transaction, so they must always be
1530 * context switched.
1531 */
1532 mfspr r5, SPRN_TFHAR
1533 mfspr r6, SPRN_TFIAR
1534 mfspr r7, SPRN_TEXASR
1535 std r5, VCPU_TFHAR(r9)
1536 std r6, VCPU_TFIAR(r9)
1537 std r7, VCPU_TEXASR(r9)
15382:
1539#endif
1540
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001541 /* Increment yield count if they have a VPA */
1542 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1543 cmpdi r8, 0
1544 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001545 li r4, LPPACA_YIELDCOUNT
1546 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001547 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001548 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001549 li r3, 1
1550 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000155125:
1552 /* Save PMU registers if requested */
1553 /* r8 and cr0.eq are live here */
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001554BEGIN_FTR_SECTION
1555 /*
1556 * POWER8 seems to have a hardware bug where setting
1557 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1558 * when some counters are already negative doesn't seem
1559 * to cause a performance monitor alert (and hence interrupt).
1560 * The effect of this is that when saving the PMU state,
1561 * if there is no PMU alert pending when we read MMCR0
1562 * before freezing the counters, but one becomes pending
1563 * before we read the counters, we lose it.
1564 * To work around this, we need a way to freeze the counters
1565 * before reading MMCR0. Normally, freezing the counters
1566 * is done by writing MMCR0 (to set MMCR0[FC]) which
1567 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1568 * we can also freeze the counters using MMCR2, by writing
1569 * 1s to all the counter freeze condition bits (there are
1570 * 9 bits each for 6 counters).
1571 */
1572 li r3, -1 /* set all freeze bits */
1573 clrrdi r3, r3, 10
1574 mfspr r10, SPRN_MMCR2
1575 mtspr SPRN_MMCR2, r3
1576 isync
1577END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001578 li r3, 1
1579 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1580 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1581 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001582 mfspr r6, SPRN_MMCRA
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001583 /* Clear MMCRA in order to disable SDAR updates */
Paul Mackerras89436332012-03-02 01:38:23 +00001584 li r7, 0
1585 mtspr SPRN_MMCRA, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001586 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001587 beq 21f /* if no VPA, save PMU stuff anyway */
1588 lbz r7, LPPACA_PMCINUSE(r8)
1589 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1590 bne 21f
1591 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1592 b 22f
159321: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001594 mfspr r7, SPRN_SIAR
1595 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001596 std r4, VCPU_MMCR(r9)
1597 std r5, VCPU_MMCR + 8(r9)
1598 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001599BEGIN_FTR_SECTION
1600 std r10, VCPU_MMCR + 24(r9)
1601END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras14941782013-09-06 13:11:18 +10001602 std r7, VCPU_SIAR(r9)
1603 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001604 mfspr r3, SPRN_PMC1
1605 mfspr r4, SPRN_PMC2
1606 mfspr r5, SPRN_PMC3
1607 mfspr r6, SPRN_PMC4
1608 mfspr r7, SPRN_PMC5
1609 mfspr r8, SPRN_PMC6
1610 stw r3, VCPU_PMC(r9)
1611 stw r4, VCPU_PMC + 4(r9)
1612 stw r5, VCPU_PMC + 8(r9)
1613 stw r6, VCPU_PMC + 12(r9)
1614 stw r7, VCPU_PMC + 16(r9)
1615 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001616BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +11001617 mfspr r5, SPRN_SIER
1618 mfspr r6, SPRN_SPMC1
1619 mfspr r7, SPRN_SPMC2
1620 mfspr r8, SPRN_MMCRS
Michael Neulingb005255e2014-01-08 21:25:21 +11001621 std r5, VCPU_SIER(r9)
1622 stw r6, VCPU_PMC + 24(r9)
1623 stw r7, VCPU_PMC + 28(r9)
1624 std r8, VCPU_MMCR + 32(r9)
1625 lis r4, 0x8000
1626 mtspr SPRN_MMCRS, r4
1627END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000162822:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001629 /* Clear out SLB */
1630 li r5,0
1631 slbmte r5,r5
1632 slbia
1633 ptesync
1634
Paul Mackerrasde56a942011-06-29 00:21:34 +00001635 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001636 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001637 * We don't have to lock against tlbies but we do
1638 * have to coordinate the hardware threads.
1639 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001640kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001641 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001642 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001643 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1644 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001645 cmpwi r3,0
1646 beq 15f
1647 HMT_LOW
164813: lbz r3,VCORE_IN_GUEST(r5)
1649 cmpwi r3,0
1650 bne 13b
1651 HMT_MEDIUM
1652 b 16f
1653
1654 /* Primary thread waits for all the secondaries to exit guest */
165515: lwz r3,VCORE_ENTRY_EXIT(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001656 rlwinm r0,r3,32-8,0xff
Paul Mackerrasde56a942011-06-29 00:21:34 +00001657 clrldi r3,r3,56
1658 cmpw r3,r0
1659 bne 15b
1660 isync
1661
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001662 /* Did we actually switch to the guest at all? */
1663 lbz r6, VCORE_IN_GUEST(r5)
1664 cmpwi r6, 0
1665 beq 19f
1666
Paul Mackerrasde56a942011-06-29 00:21:34 +00001667 /* Primary thread switches back to host partition */
1668 ld r6,KVM_HOST_SDR1(r4)
1669 lwz r7,KVM_HOST_LPID(r4)
1670 li r8,LPID_RSVD /* switch to reserved LPID */
1671 mtspr SPRN_LPID,r8
1672 ptesync
1673 mtspr SPRN_SDR1,r6 /* switch to partition page table */
1674 mtspr SPRN_LPID,r7
1675 isync
1676
Michael Neulingb005255e2014-01-08 21:25:21 +11001677BEGIN_FTR_SECTION
1678 /* DPDES is shared between threads */
1679 mfspr r7, SPRN_DPDES
1680 std r7, VCORE_DPDES(r5)
1681 /* clear DPDES so we don't get guest doorbells in the host */
1682 li r8, 0
1683 mtspr SPRN_DPDES, r8
1684END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1685
Paul Mackerrasde56a942011-06-29 00:21:34 +00001686 /* Subtract timebase offset from timebase */
1687 ld r8,VCORE_TB_OFFSET(r5)
1688 cmpdi r8,0
1689 beq 17f
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001690 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001691 subf r8,r8,r6
1692 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1693 mftb r7 /* check if lower 24 bits overflowed */
1694 clrldi r6,r6,40
1695 clrldi r7,r7,40
1696 cmpld r7,r6
1697 bge 17f
1698 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1699 mtspr SPRN_TBU40,r8
1700
1701 /* Reset PCR */
170217: ld r0, VCORE_PCR(r5)
1703 cmpdi r0, 0
1704 beq 18f
1705 li r0, 0
1706 mtspr SPRN_PCR, r0
170718:
1708 /* Signal secondary CPUs to continue */
1709 stb r0,VCORE_IN_GUEST(r5)
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000171019: lis r8,0x7fff /* MAX_INT@h */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001711 mtspr SPRN_HDEC,r8
1712
171316: ld r8,KVM_HOST_LPCR(r4)
1714 mtspr SPRN_LPCR,r8
1715 isync
Paul Mackerrasde56a942011-06-29 00:21:34 +00001716
1717 /* load host SLB entries */
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001718 ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001719
1720 .rept SLB_NUM_BOLTED
Alexander Graf0865a582014-06-11 10:36:17 +02001721 li r3, SLBSHADOW_SAVEAREA
1722 LDX_BE r5, r8, r3
1723 addi r3, r3, 8
1724 LDX_BE r6, r8, r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00001725 andis. r7,r5,SLB_ESID_V@h
1726 beq 1f
1727 slbmte r6,r5
17281: addi r8,r8,16
1729 .endr
1730
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001731#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1732 /* Finish timing, if we have a vcpu */
1733 ld r4, HSTATE_KVM_VCPU(r13)
1734 cmpdi r4, 0
1735 li r3, 0
1736 beq 2f
1737 bl kvmhv_accumulate_time
17382:
1739#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001740 /* Unset guest mode */
1741 li r0, KVM_GUEST_MODE_NONE
1742 stb r0, HSTATE_IN_GUEST(r13)
1743
Paul Mackerras218309b2013-09-06 13:23:44 +10001744 ld r0, 112+PPC_LR_STKOFF(r1)
1745 addi r1, r1, 112
1746 mtlr r0
1747 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001748
Paul Mackerras697d3892011-12-12 12:36:37 +00001749/*
1750 * Check whether an HDSI is an HPTE not found fault or something else.
1751 * If it is an HPTE not found fault that is due to the guest accessing
1752 * a page that they have mapped but which we have paged out, then
1753 * we continue on with the guest exit path. In all other cases,
1754 * reflect the HDSI to the guest as a DSI.
1755 */
1756kvmppc_hdsi:
1757 mfspr r4, SPRN_HDAR
1758 mfspr r6, SPRN_HDSISR
Paul Mackerras4cf302b2011-12-12 12:38:51 +00001759 /* HPTE not found fault or protection fault? */
1760 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00001761 beq 1f /* if not, send it to the guest */
1762 andi. r0, r11, MSR_DR /* data relocation enabled? */
1763 beq 3f
1764 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001765 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001766 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
1767 bne 7f /* if no SLB entry found */
Paul Mackerras697d3892011-12-12 12:36:37 +000017684: std r4, VCPU_FAULT_DAR(r9)
1769 stw r6, VCPU_FAULT_DSISR(r9)
1770
1771 /* Search the hash table. */
1772 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001773 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001774 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00001775 ld r9, HSTATE_KVM_VCPU(r13)
1776 ld r10, VCPU_PC(r9)
1777 ld r11, VCPU_MSR(r9)
1778 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1779 cmpdi r3, 0 /* retry the instruction */
1780 beq 6f
1781 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001782 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001783 cmpdi r3, -2 /* MMIO emulation; need instr word */
1784 beq 2f
1785
Paul Mackerrascf29b212015-10-27 16:10:20 +11001786 /* Synthesize a DSI (or DSegI) for the guest */
Paul Mackerras697d3892011-12-12 12:36:37 +00001787 ld r4, VCPU_FAULT_DAR(r9)
1788 mr r6, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110017891: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
Paul Mackerras697d3892011-12-12 12:36:37 +00001790 mtspr SPRN_DSISR, r6
Paul Mackerrascf29b212015-10-27 16:10:20 +110017917: mtspr SPRN_DAR, r4
Paul Mackerras697d3892011-12-12 12:36:37 +00001792 mtspr SPRN_SRR0, r10
1793 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001794 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001795 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001796fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000017976: ld r7, VCPU_CTR(r9)
Sam bobroffc63517c2015-05-27 09:56:57 +10001798 ld r8, VCPU_XER(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001799 mtctr r7
1800 mtxer r8
1801 mr r4, r9
1802 b fast_guest_return
1803
18043: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1805 ld r5, KVM_VRMA_SLB_V(r5)
1806 b 4b
1807
1808 /* If this is for emulated MMIO, load the instruction word */
18092: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1810
1811 /* Set guest mode to 'jump over instruction' so if lwz faults
1812 * we'll just continue at the next IP. */
1813 li r0, KVM_GUEST_MODE_SKIP
1814 stb r0, HSTATE_IN_GUEST(r13)
1815
1816 /* Do the access with MSR:DR enabled */
1817 mfmsr r3
1818 ori r4, r3, MSR_DR /* Enable paging for data */
1819 mtmsrd r4
1820 lwz r8, 0(r10)
1821 mtmsrd r3
1822
1823 /* Store the result */
1824 stw r8, VCPU_LAST_INST(r9)
1825
1826 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001827 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00001828 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001829 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00001830
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001831/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001832 * Similarly for an HISI, reflect it to the guest as an ISI unless
1833 * it is an HPTE not found fault for a page that we have paged out.
1834 */
1835kvmppc_hisi:
1836 andis. r0, r11, SRR1_ISI_NOPT@h
1837 beq 1f
1838 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1839 beq 3f
1840 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001841 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerrascf29b212015-10-27 16:10:20 +11001842 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
1843 bne 7f /* if no SLB entry found */
Paul Mackerras342d3db2011-12-12 12:38:05 +000018444:
1845 /* Search the hash table. */
1846 mr r3, r9 /* vcpu pointer */
1847 mr r4, r10
1848 mr r6, r11
1849 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001850 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00001851 ld r9, HSTATE_KVM_VCPU(r13)
1852 ld r10, VCPU_PC(r9)
1853 ld r11, VCPU_MSR(r9)
1854 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1855 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001856 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001857 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001858 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00001859
Paul Mackerrascf29b212015-10-27 16:10:20 +11001860 /* Synthesize an ISI (or ISegI) for the guest */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001861 mr r11, r3
Paul Mackerrascf29b212015-10-27 16:10:20 +110018621: li r0, BOOK3S_INTERRUPT_INST_STORAGE
18637: mtspr SPRN_SRR0, r10
Paul Mackerras342d3db2011-12-12 12:38:05 +00001864 mtspr SPRN_SRR1, r11
Paul Mackerrascf29b212015-10-27 16:10:20 +11001865 mr r10, r0
Michael Neulinge4e38122014-03-25 10:47:02 +11001866 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001867 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001868
18693: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1870 ld r5, KVM_VRMA_SLB_V(r6)
1871 b 4b
1872
1873/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001874 * Try to handle an hcall in real mode.
1875 * Returns to the guest if we handle it, or continues on up to
1876 * the kernel if we can't (i.e. if we don't have a handler for
1877 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001878 *
1879 * r5 - r8 contain hcall args,
1880 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001881 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001882hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00001883 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001884 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08001885 /* sc 1 from userspace - reflect to guest syscall */
1886 bne sc_1_fast_return
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001887 clrrdi r3,r3,2
1888 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001889 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10001890 /* See if this hcall is enabled for in-kernel handling */
1891 ld r4, VCPU_KVM(r9)
1892 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
1893 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
1894 add r4, r4, r0
1895 ld r0, KVM_ENABLED_HCALLS(r4)
1896 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
1897 srd r0, r0, r4
1898 andi. r0, r0, 1
1899 beq guest_exit_cont
1900 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001901 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10001902 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001903 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001904 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10001905 add r12,r3,r4
1906 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001907 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001908 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001909 bctrl
1910 cmpdi r3,H_TOO_HARD
1911 beq hcall_real_fallback
1912 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001913 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001914 ld r10,VCPU_PC(r4)
1915 ld r11,VCPU_MSR(r4)
1916 b fast_guest_return
1917
Liu Ping Fan27025a62013-11-19 14:12:48 +08001918sc_1_fast_return:
1919 mtspr SPRN_SRR0,r10
1920 mtspr SPRN_SRR1,r11
1921 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11001922 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08001923 mr r4,r9
1924 b fast_guest_return
1925
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001926 /* We've attempted a real mode hcall, but it's punted it back
1927 * to userspace. We need to restore some clobbered volatiles
1928 * before resuming the pass-it-to-qemu path */
1929hcall_real_fallback:
1930 li r12,BOOK3S_INTERRUPT_SYSCALL
1931 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001932
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001933 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001934
1935 .globl hcall_real_table
1936hcall_real_table:
1937 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001938 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
1939 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
1940 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrascdeee512015-06-24 21:18:07 +10001941 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
1942 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001943 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
1944 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11001945 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001946 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001947 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001948 .long 0 /* 0x2c */
1949 .long 0 /* 0x30 */
1950 .long 0 /* 0x34 */
1951 .long 0 /* 0x38 */
1952 .long 0 /* 0x3c */
1953 .long 0 /* 0x40 */
1954 .long 0 /* 0x44 */
1955 .long 0 /* 0x48 */
1956 .long 0 /* 0x4c */
1957 .long 0 /* 0x50 */
1958 .long 0 /* 0x54 */
1959 .long 0 /* 0x58 */
1960 .long 0 /* 0x5c */
1961 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001962#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001963 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
1964 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
1965 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001966 .long 0 /* 0x70 - H_IPOLL */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001967 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001968#else
1969 .long 0 /* 0x64 - H_EOI */
1970 .long 0 /* 0x68 - H_CPPR */
1971 .long 0 /* 0x6c - H_IPI */
1972 .long 0 /* 0x70 - H_IPOLL */
1973 .long 0 /* 0x74 - H_XIRR */
1974#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001975 .long 0 /* 0x78 */
1976 .long 0 /* 0x7c */
1977 .long 0 /* 0x80 */
1978 .long 0 /* 0x84 */
1979 .long 0 /* 0x88 */
1980 .long 0 /* 0x8c */
1981 .long 0 /* 0x90 */
1982 .long 0 /* 0x94 */
1983 .long 0 /* 0x98 */
1984 .long 0 /* 0x9c */
1985 .long 0 /* 0xa0 */
1986 .long 0 /* 0xa4 */
1987 .long 0 /* 0xa8 */
1988 .long 0 /* 0xac */
1989 .long 0 /* 0xb0 */
1990 .long 0 /* 0xb4 */
1991 .long 0 /* 0xb8 */
1992 .long 0 /* 0xbc */
1993 .long 0 /* 0xc0 */
1994 .long 0 /* 0xc4 */
1995 .long 0 /* 0xc8 */
1996 .long 0 /* 0xcc */
1997 .long 0 /* 0xd0 */
1998 .long 0 /* 0xd4 */
1999 .long 0 /* 0xd8 */
2000 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002001 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11002002 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002003 .long 0 /* 0xe8 */
2004 .long 0 /* 0xec */
2005 .long 0 /* 0xf0 */
2006 .long 0 /* 0xf4 */
2007 .long 0 /* 0xf8 */
2008 .long 0 /* 0xfc */
2009 .long 0 /* 0x100 */
2010 .long 0 /* 0x104 */
2011 .long 0 /* 0x108 */
2012 .long 0 /* 0x10c */
2013 .long 0 /* 0x110 */
2014 .long 0 /* 0x114 */
2015 .long 0 /* 0x118 */
2016 .long 0 /* 0x11c */
2017 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002018 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11002019 .long 0 /* 0x128 */
2020 .long 0 /* 0x12c */
2021 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11002022 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Alexey Kardashevskiy31217db2016-03-18 13:50:42 +11002023 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11002024 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
Michael Ellermane928e9c2015-03-20 20:39:41 +11002025 .long 0 /* 0x140 */
2026 .long 0 /* 0x144 */
2027 .long 0 /* 0x148 */
2028 .long 0 /* 0x14c */
2029 .long 0 /* 0x150 */
2030 .long 0 /* 0x154 */
2031 .long 0 /* 0x158 */
2032 .long 0 /* 0x15c */
2033 .long 0 /* 0x160 */
2034 .long 0 /* 0x164 */
2035 .long 0 /* 0x168 */
2036 .long 0 /* 0x16c */
2037 .long 0 /* 0x170 */
2038 .long 0 /* 0x174 */
2039 .long 0 /* 0x178 */
2040 .long 0 /* 0x17c */
2041 .long 0 /* 0x180 */
2042 .long 0 /* 0x184 */
2043 .long 0 /* 0x188 */
2044 .long 0 /* 0x18c */
2045 .long 0 /* 0x190 */
2046 .long 0 /* 0x194 */
2047 .long 0 /* 0x198 */
2048 .long 0 /* 0x19c */
2049 .long 0 /* 0x1a0 */
2050 .long 0 /* 0x1a4 */
2051 .long 0 /* 0x1a8 */
2052 .long 0 /* 0x1ac */
2053 .long 0 /* 0x1b0 */
2054 .long 0 /* 0x1b4 */
2055 .long 0 /* 0x1b8 */
2056 .long 0 /* 0x1bc */
2057 .long 0 /* 0x1c0 */
2058 .long 0 /* 0x1c4 */
2059 .long 0 /* 0x1c8 */
2060 .long 0 /* 0x1cc */
2061 .long 0 /* 0x1d0 */
2062 .long 0 /* 0x1d4 */
2063 .long 0 /* 0x1d8 */
2064 .long 0 /* 0x1dc */
2065 .long 0 /* 0x1e0 */
2066 .long 0 /* 0x1e4 */
2067 .long 0 /* 0x1e8 */
2068 .long 0 /* 0x1ec */
2069 .long 0 /* 0x1f0 */
2070 .long 0 /* 0x1f4 */
2071 .long 0 /* 0x1f8 */
2072 .long 0 /* 0x1fc */
2073 .long 0 /* 0x200 */
2074 .long 0 /* 0x204 */
2075 .long 0 /* 0x208 */
2076 .long 0 /* 0x20c */
2077 .long 0 /* 0x210 */
2078 .long 0 /* 0x214 */
2079 .long 0 /* 0x218 */
2080 .long 0 /* 0x21c */
2081 .long 0 /* 0x220 */
2082 .long 0 /* 0x224 */
2083 .long 0 /* 0x228 */
2084 .long 0 /* 0x22c */
2085 .long 0 /* 0x230 */
2086 .long 0 /* 0x234 */
2087 .long 0 /* 0x238 */
2088 .long 0 /* 0x23c */
2089 .long 0 /* 0x240 */
2090 .long 0 /* 0x244 */
2091 .long 0 /* 0x248 */
2092 .long 0 /* 0x24c */
2093 .long 0 /* 0x250 */
2094 .long 0 /* 0x254 */
2095 .long 0 /* 0x258 */
2096 .long 0 /* 0x25c */
2097 .long 0 /* 0x260 */
2098 .long 0 /* 0x264 */
2099 .long 0 /* 0x268 */
2100 .long 0 /* 0x26c */
2101 .long 0 /* 0x270 */
2102 .long 0 /* 0x274 */
2103 .long 0 /* 0x278 */
2104 .long 0 /* 0x27c */
2105 .long 0 /* 0x280 */
2106 .long 0 /* 0x284 */
2107 .long 0 /* 0x288 */
2108 .long 0 /* 0x28c */
2109 .long 0 /* 0x290 */
2110 .long 0 /* 0x294 */
2111 .long 0 /* 0x298 */
2112 .long 0 /* 0x29c */
2113 .long 0 /* 0x2a0 */
2114 .long 0 /* 0x2a4 */
2115 .long 0 /* 0x2a8 */
2116 .long 0 /* 0x2ac */
2117 .long 0 /* 0x2b0 */
2118 .long 0 /* 0x2b4 */
2119 .long 0 /* 0x2b8 */
2120 .long 0 /* 0x2bc */
2121 .long 0 /* 0x2c0 */
2122 .long 0 /* 0x2c4 */
2123 .long 0 /* 0x2c8 */
2124 .long 0 /* 0x2cc */
2125 .long 0 /* 0x2d0 */
2126 .long 0 /* 0x2d4 */
2127 .long 0 /* 0x2d8 */
2128 .long 0 /* 0x2dc */
2129 .long 0 /* 0x2e0 */
2130 .long 0 /* 0x2e4 */
2131 .long 0 /* 0x2e8 */
2132 .long 0 /* 0x2ec */
2133 .long 0 /* 0x2f0 */
2134 .long 0 /* 0x2f4 */
2135 .long 0 /* 0x2f8 */
2136 .long 0 /* 0x2fc */
2137 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002138 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002139hcall_real_table_end:
2140
Paul Mackerras8563bf52014-01-08 21:25:29 +11002141_GLOBAL(kvmppc_h_set_xdabr)
2142 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2143 beq 6f
2144 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2145 andc. r0, r5, r0
2146 beq 3f
21476: li r3, H_PARAMETER
2148 blr
2149
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002150_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002151 li r5, DABRX_USER | DABRX_KERNEL
21523:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002153BEGIN_FTR_SECTION
2154 b 2f
2155END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002156 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002157 stw r5, VCPU_DABRX(r3)
2158 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002159 /* Work around P7 bug where DABR can get corrupted on mtspr */
21601: mtspr SPRN_DABR,r4
2161 mfspr r5, SPRN_DABR
2162 cmpd r4, r5
2163 bne 1b
2164 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002165 li r3,0
2166 blr
2167
Paul Mackerras8563bf52014-01-08 21:25:29 +11002168 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
21692: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
Thomas Huth760a7362015-11-20 09:11:45 +01002170 rlwimi r5, r4, 2, DAWRX_WT
Paul Mackerras8563bf52014-01-08 21:25:29 +11002171 clrrdi r4, r4, 3
2172 std r4, VCPU_DAWR(r3)
2173 std r5, VCPU_DAWRX(r3)
2174 mtspr SPRN_DAWR, r4
2175 mtspr SPRN_DAWRX, r5
2176 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002177 blr
2178
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002179_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002180 ori r11,r11,MSR_EE
2181 std r11,VCPU_MSR(r3)
2182 li r0,1
2183 stb r0,VCPU_CEDED(r3)
2184 sync /* order setting ceded vs. testing prodded */
2185 lbz r5,VCPU_PRODDED(r3)
2186 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002187 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002188 li r12,0 /* set trap to 0 to say hcall is handled */
2189 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002190 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002191 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002192
2193 /*
2194 * Set our bit in the bitmask of napping threads unless all the
2195 * other threads are already napping, in which case we send this
2196 * up to the host.
2197 */
2198 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002199 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002200 lwz r8,VCORE_ENTRY_EXIT(r5)
2201 clrldi r8,r8,56
2202 li r0,1
2203 sld r0,r0,r6
2204 addi r6,r5,VCORE_NAPPING_THREADS
220531: lwarx r4,0,r6
2206 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002207 cmpw r4,r8
2208 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002209 stwcx. r4,0,r6
2210 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002211 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002212 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002213 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002214 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002215 lwz r7,VCORE_ENTRY_EXIT(r5)
2216 cmpwi r7,0x100
2217 bge 33f /* another thread already exiting */
2218
2219/*
2220 * Although not specifically required by the architecture, POWER7
2221 * preserves the following registers in nap mode, even if an SMT mode
2222 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2223 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2224 */
2225 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002226 std r14, VCPU_GPR(R14)(r3)
2227 std r15, VCPU_GPR(R15)(r3)
2228 std r16, VCPU_GPR(R16)(r3)
2229 std r17, VCPU_GPR(R17)(r3)
2230 std r18, VCPU_GPR(R18)(r3)
2231 std r19, VCPU_GPR(R19)(r3)
2232 std r20, VCPU_GPR(R20)(r3)
2233 std r21, VCPU_GPR(R21)(r3)
2234 std r22, VCPU_GPR(R22)(r3)
2235 std r23, VCPU_GPR(R23)(r3)
2236 std r24, VCPU_GPR(R24)(r3)
2237 std r25, VCPU_GPR(R25)(r3)
2238 std r26, VCPU_GPR(R26)(r3)
2239 std r27, VCPU_GPR(R27)(r3)
2240 std r28, VCPU_GPR(R28)(r3)
2241 std r29, VCPU_GPR(R29)(r3)
2242 std r30, VCPU_GPR(R30)(r3)
2243 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002244
2245 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002246 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002247
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002248 /*
2249 * Set DEC to the smaller of DEC and HDEC, so that we wake
2250 * no later than the end of our timeslice (HDEC interrupts
2251 * don't wake us from nap).
2252 */
2253 mfspr r3, SPRN_DEC
2254 mfspr r4, SPRN_HDEC
2255 mftb r5
2256 cmpw r3, r4
2257 ble 67f
2258 mtspr SPRN_DEC, r4
225967:
2260 /* save expiry time of guest decrementer */
2261 extsw r3, r3
2262 add r3, r3, r5
2263 ld r4, HSTATE_KVM_VCPU(r13)
2264 ld r5, HSTATE_KVM_VCORE(r13)
2265 ld r6, VCORE_TB_OFFSET(r5)
2266 subf r3, r6, r3 /* convert to host TB value */
2267 std r3, VCPU_DEC_EXPIRES(r4)
2268
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002269#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2270 ld r4, HSTATE_KVM_VCPU(r13)
2271 addi r3, r4, VCPU_TB_CEDE
2272 bl kvmhv_accumulate_time
2273#endif
2274
Paul Mackerrasccc07772015-03-28 14:21:07 +11002275 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2276
Paul Mackerras19ccb762011-07-23 17:42:46 +10002277 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002278 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002279 * occurs, with PECE1 and PECE0 set in LPCR.
Paul Mackerras66feed62015-03-28 14:21:12 +11002280 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
Paul Mackerrasccc07772015-03-28 14:21:07 +11002281 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002282 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002283kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002284 mfspr r0, SPRN_CTRLF
2285 clrrdi r0, r0, 1
2286 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302287
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002288 li r0,1
2289 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002290 mfspr r5,SPRN_LPCR
2291 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002292BEGIN_FTR_SECTION
Paul Mackerras66feed62015-03-28 14:21:12 +11002293 ori r5, r5, LPCR_PECEDH
Paul Mackerrasccc07772015-03-28 14:21:07 +11002294 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002295END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002296 mtspr SPRN_LPCR,r5
2297 isync
2298 li r0, 0
2299 std r0, HSTATE_SCRATCH0(r13)
2300 ptesync
2301 ld r0, HSTATE_SCRATCH0(r13)
23021: cmpd r0, r0
2303 bne 1b
2304 nap
2305 b .
2306
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100230733: mr r4, r3
2308 li r3, 0
2309 li r12, 0
2310 b 34f
2311
Paul Mackerras19ccb762011-07-23 17:42:46 +10002312kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002313 /* get vcpu pointer */
2314 ld r4, HSTATE_KVM_VCPU(r13)
2315
Paul Mackerras19ccb762011-07-23 17:42:46 +10002316 /* Woken by external or decrementer interrupt */
2317 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002318
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002319#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2320 addi r3, r4, VCPU_TB_RMINTR
2321 bl kvmhv_accumulate_time
2322#endif
2323
Paul Mackerras19ccb762011-07-23 17:42:46 +10002324 /* load up FP state */
2325 bl kvmppc_load_fp
2326
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002327 /* Restore guest decrementer */
2328 ld r3, VCPU_DEC_EXPIRES(r4)
2329 ld r5, HSTATE_KVM_VCORE(r13)
2330 ld r6, VCORE_TB_OFFSET(r5)
2331 add r3, r3, r6 /* convert host TB to guest TB value */
2332 mftb r7
2333 subf r3, r7, r3
2334 mtspr SPRN_DEC, r3
2335
Paul Mackerras19ccb762011-07-23 17:42:46 +10002336 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002337 ld r14, VCPU_GPR(R14)(r4)
2338 ld r15, VCPU_GPR(R15)(r4)
2339 ld r16, VCPU_GPR(R16)(r4)
2340 ld r17, VCPU_GPR(R17)(r4)
2341 ld r18, VCPU_GPR(R18)(r4)
2342 ld r19, VCPU_GPR(R19)(r4)
2343 ld r20, VCPU_GPR(R20)(r4)
2344 ld r21, VCPU_GPR(R21)(r4)
2345 ld r22, VCPU_GPR(R22)(r4)
2346 ld r23, VCPU_GPR(R23)(r4)
2347 ld r24, VCPU_GPR(R24)(r4)
2348 ld r25, VCPU_GPR(R25)(r4)
2349 ld r26, VCPU_GPR(R26)(r4)
2350 ld r27, VCPU_GPR(R27)(r4)
2351 ld r28, VCPU_GPR(R28)(r4)
2352 ld r29, VCPU_GPR(R29)(r4)
2353 ld r30, VCPU_GPR(R30)(r4)
2354 ld r31, VCPU_GPR(R31)(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002355
2356 /* Check the wake reason in SRR1 to see why we got here */
2357 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002358
2359 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100236034: ld r5,HSTATE_KVM_VCORE(r13)
2361 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002362 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002363 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002364 addi r6,r5,VCORE_NAPPING_THREADS
236532: lwarx r7,0,r6
2366 andc r7,r7,r0
2367 stwcx. r7,0,r6
2368 bne 32b
2369 li r0,0
2370 stb r0,HSTATE_NAPPING(r13)
2371
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002372 /* See if the wake reason means we need to exit */
2373 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002374 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002375 cmpdi r3, 0
2376 bgt guest_exit_cont
Paul Mackerras4619ac82013-04-17 20:31:41 +00002377
Paul Mackerras19ccb762011-07-23 17:42:46 +10002378 /* see if any other thread is already exiting */
2379 lwz r0,VCORE_ENTRY_EXIT(r5)
2380 cmpwi r0,0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002381 bge guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002382
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002383 b kvmppc_cede_reentry /* if not go back to guest */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002384
2385 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002386kvm_cede_prodded:
2387 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002388 stb r0,VCPU_PRODDED(r3)
2389 sync /* order testing prodded vs. clearing ceded */
2390 stb r0,VCPU_CEDED(r3)
2391 li r3,H_SUCCESS
2392 blr
2393
2394 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002395kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002396 ld r9, HSTATE_KVM_VCPU(r13)
2397 b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002398
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002399 /* Try to handle a machine check in real mode */
2400machine_check_realmode:
2401 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002402 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002403 nop
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002404 ld r9, HSTATE_KVM_VCPU(r13)
2405 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302406 /*
2407 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2408 * machine check interrupt (set HSRR0 to 0x200). And for handled
2409 * errors (no-fatal), just go back to guest execution with current
2410 * HSRR0 instead of exiting guest. This new approach will inject
2411 * machine check to guest for fatal error causing guest to crash.
2412 *
2413 * The old code used to return to host for unhandled errors which
2414 * was causing guest to hang with soft lockups inside guest and
2415 * makes it difficult to recover guest instance.
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302416 *
2417 * if we receive machine check with MSR(RI=0) then deliver it to
2418 * guest as machine check causing guest to crash.
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302419 */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302420 ld r11, VCPU_MSR(r9)
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11002421 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2422 bne mc_cont /* if so, exit to host */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +05302423 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2424 beq 1f /* Deliver a machine check to guest */
2425 ld r10, VCPU_PC(r9)
2426 cmpdi r3, 0 /* Did we handle MCE ? */
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302427 bne 2f /* Continue guest execution. */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002428 /* If not, deliver a machine check. SRR0/1 are already set */
Mahesh Salgaonkar966d7132015-03-23 22:24:45 +053024291: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
Michael Neulinge4e38122014-03-25 10:47:02 +11002430 bl kvmppc_msr_interrupt
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +053024312: b fast_interrupt_c_return
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002432
Paul Mackerrasde56a942011-06-29 00:21:34 +00002433/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002434 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002435 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002436 * 0 if nothing needs to be done
2437 * 1 if something happened that needs to be handled by the host
Paul Mackerras66feed62015-03-28 14:21:12 +11002438 * -1 if there was a guest wakeup (IPI or msgsnd)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002439 *
2440 * Also sets r12 to the interrupt vector for any interrupt that needs
2441 * to be handled now by the host (0x500 for external interrupt), or zero.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002442 * Modifies r0, r6, r7, r8.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002443 */
2444kvmppc_check_wake_reason:
2445 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002446BEGIN_FTR_SECTION
2447 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2448FTR_SECTION_ELSE
2449 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2450ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2451 cmpwi r6, 8 /* was it an external interrupt? */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002452 li r12, BOOK3S_INTERRUPT_EXTERNAL
2453 beq kvmppc_read_intr /* if so, see what it was */
2454 li r3, 0
2455 li r12, 0
2456 cmpwi r6, 6 /* was it the decrementer? */
2457 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002458BEGIN_FTR_SECTION
2459 cmpwi r6, 5 /* privileged doorbell? */
2460 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002461 cmpwi r6, 3 /* hypervisor doorbell? */
2462 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002463END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002464 li r3, 1 /* anything else, return 1 */
24650: blr
2466
Paul Mackerras5d00f662014-01-08 21:25:28 +11002467 /* hypervisor doorbell */
24683: li r12, BOOK3S_INTERRUPT_H_DOORBELL
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302469
2470 /*
2471 * Clear the doorbell as we will invoke the handler
2472 * explicitly in the guest exit path.
2473 */
2474 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2475 PPC_MSGCLR(6)
Paul Mackerras66feed62015-03-28 14:21:12 +11002476 /* see if it's a host IPI */
Paul Mackerras5d00f662014-01-08 21:25:28 +11002477 li r3, 1
Paul Mackerras66feed62015-03-28 14:21:12 +11002478 lbz r0, HSTATE_HOST_IPI(r13)
2479 cmpwi r0, 0
2480 bnelr
Gautham R. Shenoy70aa3962015-10-15 11:29:58 +05302481 /* if not, return -1 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002482 li r3, -1
Paul Mackerras5d00f662014-01-08 21:25:28 +11002483 blr
2484
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002485/*
Paul Mackerrasc9342432013-09-06 13:24:13 +10002486 * Determine what sort of external interrupt is pending (if any).
2487 * Returns:
2488 * 0 if no interrupt is pending
2489 * 1 if an interrupt is pending that needs to be handled by the host
2490 * -1 if there was a guest wakeup IPI (which has now been cleared)
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002491 * Modifies r0, r6, r7, r8, returns value in r3.
Paul Mackerrasc9342432013-09-06 13:24:13 +10002492 */
2493kvmppc_read_intr:
2494 /* see if a host IPI is pending */
2495 li r3, 1
2496 lbz r0, HSTATE_HOST_IPI(r13)
2497 cmpwi r0, 0
2498 bne 1f
Paul Mackerrasde56a942011-06-29 00:21:34 +00002499
Paul Mackerrasc9342432013-09-06 13:24:13 +10002500 /* Now read the interrupt from the ICP */
2501 ld r6, HSTATE_XICS_PHYS(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002502 li r7, XICS_XIRR
Paul Mackerrasc9342432013-09-06 13:24:13 +10002503 cmpdi r6, 0
2504 beq- 1f
2505 lwzcix r0, r6, r7
Alexander Graf76d072f2014-06-11 10:37:52 +02002506 /*
2507 * Save XIRR for later. Since we get in in reverse endian on LE
2508 * systems, save it byte reversed and fetch it back in host endian.
2509 */
2510 li r3, HSTATE_SAVED_XIRR
2511 STWX_BE r0, r3, r13
2512#ifdef __LITTLE_ENDIAN__
2513 lwz r3, HSTATE_SAVED_XIRR(r13)
2514#else
2515 mr r3, r0
2516#endif
2517 rlwinm. r3, r3, 0, 0xffffff
Paul Mackerrasde56a942011-06-29 00:21:34 +00002518 sync
Paul Mackerrasc9342432013-09-06 13:24:13 +10002519 beq 1f /* if nothing pending in the ICP */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002520
Paul Mackerrasc9342432013-09-06 13:24:13 +10002521 /* We found something in the ICP...
2522 *
2523 * If it's not an IPI, stash it in the PACA and return to
2524 * the host, we don't (yet) handle directing real external
2525 * interrupts directly to the guest
2526 */
2527 cmpwi r3, XICS_IPI /* if there is, is it an IPI? */
Paul Mackerrasc9342432013-09-06 13:24:13 +10002528 bne 42f
Paul Mackerrasde56a942011-06-29 00:21:34 +00002529
Paul Mackerrasc9342432013-09-06 13:24:13 +10002530 /* It's an IPI, clear the MFRR and EOI it */
2531 li r3, 0xff
2532 li r8, XICS_MFRR
2533 stbcix r3, r6, r8 /* clear the IPI */
2534 stwcix r0, r6, r7 /* EOI it */
2535 sync
Paul Mackerrasde56a942011-06-29 00:21:34 +00002536
Paul Mackerrasc9342432013-09-06 13:24:13 +10002537 /* We need to re-check host IPI now in case it got set in the
2538 * meantime. If it's clear, we bounce the interrupt to the
2539 * guest
2540 */
2541 lbz r0, HSTATE_HOST_IPI(r13)
2542 cmpwi r0, 0
2543 bne- 43f
2544
2545 /* OK, it's an IPI for us */
Paul Mackerras6af27c82015-03-28 14:21:10 +11002546 li r12, 0
Paul Mackerrasc9342432013-09-06 13:24:13 +10002547 li r3, -1
25481: blr
2549
Alexander Graf76d072f2014-06-11 10:37:52 +0200255042: /* It's not an IPI and it's for the host. We saved a copy of XIRR in
2551 * the PACA earlier, it will be picked up by the host ICP driver
Paul Mackerrasc9342432013-09-06 13:24:13 +10002552 */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002553 li r3, 1
Paul Mackerrasc9342432013-09-06 13:24:13 +10002554 b 1b
2555
255643: /* We raced with the host, we need to resend that IPI, bummer */
2557 li r0, IPI_PRIORITY
2558 stbcix r0, r6, r8 /* set the IPI */
2559 sync
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002560 li r3, 1
Paul Mackerrasc9342432013-09-06 13:24:13 +10002561 b 1b
Paul Mackerrasde56a942011-06-29 00:21:34 +00002562
2563/*
2564 * Save away FP, VMX and VSX registers.
2565 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002566 * N.B. r30 and r31 are volatile across this function,
2567 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002568 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002569kvmppc_save_fp:
2570 mflr r30
2571 mr r31,r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00002572 mfmsr r5
2573 ori r8,r5,MSR_FP
2574#ifdef CONFIG_ALTIVEC
2575BEGIN_FTR_SECTION
2576 oris r8,r8,MSR_VEC@h
2577END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2578#endif
2579#ifdef CONFIG_VSX
2580BEGIN_FTR_SECTION
2581 oris r8,r8,MSR_VSX@h
2582END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2583#endif
2584 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002585 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002586 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002587#ifdef CONFIG_ALTIVEC
2588BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002589 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002590 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002591END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2592#endif
2593 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11002594 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11002595 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00002596 blr
2597
2598/*
2599 * Load up FP, VMX and VSX registers
2600 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002601 * N.B. r30 and r31 are volatile across this function,
2602 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002603 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002604kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11002605 mflr r30
2606 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00002607 mfmsr r9
2608 ori r8,r9,MSR_FP
2609#ifdef CONFIG_ALTIVEC
2610BEGIN_FTR_SECTION
2611 oris r8,r8,MSR_VEC@h
2612END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2613#endif
2614#ifdef CONFIG_VSX
2615BEGIN_FTR_SECTION
2616 oris r8,r8,MSR_VSX@h
2617END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2618#endif
2619 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002620 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002621 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002622#ifdef CONFIG_ALTIVEC
2623BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002624 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002625 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002626END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2627#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11002628 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002629 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11002630 mtlr r30
2631 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00002632 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10002633
2634/*
2635 * We come here if we get any exception or interrupt while we are
2636 * executing host real mode code while in guest MMU context.
2637 * For now just spin, but we should do something better.
2638 */
2639kvmppc_bad_host_intr:
2640 b .
Michael Neulinge4e38122014-03-25 10:47:02 +11002641
2642/*
2643 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
2644 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
2645 * r11 has the guest MSR value (in/out)
2646 * r9 has a vcpu pointer (in)
2647 * r0 is used as a scratch register
2648 */
2649kvmppc_msr_interrupt:
2650 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
2651 cmpwi r0, 2 /* Check if we are in transactional state.. */
2652 ld r11, VCPU_INTR_MSR(r9)
2653 bne 1f
2654 /* ... if transactional, change to suspended */
2655 li r0, 1
26561: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
2657 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10002658
2659/*
2660 * This works around a hardware bug on POWER8E processors, where
2661 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
2662 * performance monitor interrupt. Instead, when we need to have
2663 * an interrupt pending, we have to arrange for a counter to overflow.
2664 */
2665kvmppc_fix_pmao:
2666 li r3, 0
2667 mtspr SPRN_MMCR2, r3
2668 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
2669 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
2670 mtspr SPRN_MMCR0, r3
2671 lis r3, 0x7fff
2672 ori r3, r3, 0xffff
2673 mtspr SPRN_PMC6, r3
2674 isync
2675 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002676
2677#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2678/*
2679 * Start timing an activity
2680 * r3 = pointer to time accumulation struct, r4 = vcpu
2681 */
2682kvmhv_start_timing:
2683 ld r5, HSTATE_KVM_VCORE(r13)
2684 lbz r6, VCORE_IN_GUEST(r5)
2685 cmpwi r6, 0
2686 beq 5f /* if in guest, need to */
2687 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
26885: mftb r5
2689 subf r5, r6, r5
2690 std r3, VCPU_CUR_ACTIVITY(r4)
2691 std r5, VCPU_ACTIVITY_START(r4)
2692 blr
2693
2694/*
2695 * Accumulate time to one activity and start another.
2696 * r3 = pointer to new time accumulation struct, r4 = vcpu
2697 */
2698kvmhv_accumulate_time:
2699 ld r5, HSTATE_KVM_VCORE(r13)
2700 lbz r8, VCORE_IN_GUEST(r5)
2701 cmpwi r8, 0
2702 beq 4f /* if in guest, need to */
2703 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
27044: ld r5, VCPU_CUR_ACTIVITY(r4)
2705 ld r6, VCPU_ACTIVITY_START(r4)
2706 std r3, VCPU_CUR_ACTIVITY(r4)
2707 mftb r7
2708 subf r7, r8, r7
2709 std r7, VCPU_ACTIVITY_START(r4)
2710 cmpdi r5, 0
2711 beqlr
2712 subf r3, r6, r7
2713 ld r8, TAS_SEQCOUNT(r5)
2714 cmpdi r8, 0
2715 addi r8, r8, 1
2716 std r8, TAS_SEQCOUNT(r5)
2717 lwsync
2718 ld r7, TAS_TOTAL(r5)
2719 add r7, r7, r3
2720 std r7, TAS_TOTAL(r5)
2721 ld r6, TAS_MIN(r5)
2722 ld r7, TAS_MAX(r5)
2723 beq 3f
2724 cmpd r3, r6
2725 bge 1f
27263: std r3, TAS_MIN(r5)
27271: cmpd r3, r7
2728 ble 2f
2729 std r3, TAS_MAX(r5)
27302: lwsync
2731 addi r8, r8, 1
2732 std r8, TAS_SEQCOUNT(r5)
2733 blr
2734#endif