blob: fcf3a617cc8af063ea48d6f360103519435e47a1 [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>
Paul Mackerrasb4072df2012-11-23 22:37:50 +000030#include <asm/mmu-hash64.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
131 /*
Paul Mackerras218309b2013-09-06 13:23:44 +1000132 * For external and machine check interrupts, we need
133 * to call the Linux handler to process the interrupt.
134 * We do that by jumping to absolute address 0x500 for
135 * external interrupts, or the machine_check_fwnmi label
136 * for machine checks (since firmware might have patched
137 * the vector area at 0x200). The [h]rfid at the end of the
138 * handler will return to the book3s_hv_interrupts.S code.
139 * For other interrupts we do the rfid to get back
140 * to the book3s_hv_interrupts.S code here.
141 */
142 ld r8, 112+PPC_LR_STKOFF(r1)
143 addi r1, r1, 112
144 ld r7, HSTATE_HOST_MSR(r13)
145
146 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
147 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras218309b2013-09-06 13:23:44 +1000148 beq 11f
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530149 cmpwi cr2, r12, BOOK3S_INTERRUPT_HMI
150 beq cr2, 14f /* HMI check */
Paul Mackerras218309b2013-09-06 13:23:44 +1000151
152 /* RFI into the highmem handler, or branch to interrupt handler */
153 mfmsr r6
154 li r0, MSR_RI
155 andc r6, r6, r0
156 mtmsrd r6, 1 /* Clear RI in MSR */
157 mtsrr0 r8
158 mtsrr1 r7
Paul Mackerras218309b2013-09-06 13:23:44 +1000159 beq cr1, 13f /* machine check */
160 RFI
161
162 /* On POWER7, we have external interrupts set to use HSRR0/1 */
16311: mtspr SPRN_HSRR0, r8
164 mtspr SPRN_HSRR1, r7
165 ba 0x500
166
16713: b machine_check_fwnmi
168
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +053016914: mtspr SPRN_HSRR0, r8
170 mtspr SPRN_HSRR1, r7
171 b hmi_exception_after_realmode
172
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100173kvmppc_primary_no_guest:
174 /* We handle this much like a ceded vcpu */
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100175 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
176 mfspr r3, SPRN_HDEC
177 mtspr SPRN_DEC, r3
Paul Mackerras6af27c82015-03-28 14:21:10 +1100178 /*
179 * Make sure the primary has finished the MMU switch.
180 * We should never get here on a secondary thread, but
181 * check it for robustness' sake.
182 */
183 ld r5, HSTATE_KVM_VCORE(r13)
18465: lbz r0, VCORE_IN_GUEST(r5)
185 cmpwi r0, 0
186 beq 65b
187 /* Set LPCR. */
188 ld r8,VCORE_LPCR(r5)
189 mtspr SPRN_LPCR,r8
190 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100191 /* set our bit in napping_threads */
192 ld r5, HSTATE_KVM_VCORE(r13)
193 lbz r7, HSTATE_PTID(r13)
194 li r0, 1
195 sld r0, r0, r7
196 addi r6, r5, VCORE_NAPPING_THREADS
1971: lwarx r3, 0, r6
198 or r3, r3, r0
199 stwcx. r3, 0, r6
200 bne 1b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100201 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100202 isync
203 li r12, 0
204 lwz r7, VCORE_ENTRY_EXIT(r5)
205 cmpwi r7, 0x100
206 bge kvm_novcpu_exit /* another thread already exiting */
207 li r3, NAPPING_NOVCPU
208 stb r3, HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100209
Paul Mackerrasccc07772015-03-28 14:21:07 +1100210 li r3, 0 /* Don't wake on privileged (OS) doorbell */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100211 b kvm_do_nap
212
213kvm_novcpu_wakeup:
214 ld r1, HSTATE_HOST_R1(r13)
215 ld r5, HSTATE_KVM_VCORE(r13)
216 li r0, 0
217 stb r0, HSTATE_NAPPING(r13)
218 stb r0, HSTATE_HWTHREAD_REQ(r13)
219
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100220 /* check the wake reason */
221 bl kvmppc_check_wake_reason
Paul Mackerras6af27c82015-03-28 14:21:10 +1100222
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100223 /* see if any other thread is already exiting */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100224 lwz r0, VCORE_ENTRY_EXIT(r5)
225 cmpwi r0, 0x100
226 bge kvm_novcpu_exit
227
228 /* clear our bit in napping_threads */
229 lbz r7, HSTATE_PTID(r13)
230 li r0, 1
231 sld r0, r0, r7
232 addi r6, r5, VCORE_NAPPING_THREADS
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002334: lwarx r7, 0, r6
234 andc r7, r7, r0
235 stwcx. r7, 0, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100236 bne 4b
237
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100238 /* See if the wake reason means we need to exit */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100239 cmpdi r3, 0
240 bge kvm_novcpu_exit
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100241
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +1100242 /* See if our timeslice has expired (HDEC is negative) */
243 mfspr r0, SPRN_HDEC
244 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
245 cmpwi r0, 0
246 blt kvm_novcpu_exit
247
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100248 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
249 ld r4, HSTATE_KVM_VCPU(r13)
250 cmpdi r4, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100251 beq kvmppc_primary_no_guest
252
253#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
254 addi r3, r4, VCPU_TB_RMENTRY
255 bl kvmhv_start_timing
256#endif
257 b kvmppc_got_guest
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100258
259kvm_novcpu_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100260#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
261 ld r4, HSTATE_KVM_VCPU(r13)
262 cmpdi r4, 0
263 beq 13f
264 addi r3, r4, VCPU_TB_RMEXIT
265 bl kvmhv_accumulate_time
266#endif
Paul Mackerraseddb60f2015-03-28 14:21:11 +110026713: mr r3, r12
268 stw r12, 112-4(r1)
269 bl kvmhv_commence_exit
270 nop
271 lwz r12, 112-4(r1)
Paul Mackerras6af27c82015-03-28 14:21:10 +1100272 b kvmhv_switch_to_host
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100273
Paul Mackerras371fefd2011-06-29 00:23:08 +0000274/*
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100275 * We come in here when wakened from nap mode.
Paul Mackerras371fefd2011-06-29 00:23:08 +0000276 * Relocation is off and most register values are lost.
277 * r13 points to the PACA.
278 */
279 .globl kvm_start_guest
280kvm_start_guest:
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530281
282 /* Set runlatch bit the minute you wake up from nap */
Paul Mackerras1f09c3e2015-03-28 14:21:04 +1100283 mfspr r0, SPRN_CTRLF
284 ori r0, r0, 1
285 mtspr SPRN_CTRLT, r0
Preeti U Murthyfd17dc72014-04-11 16:01:58 +0530286
Paul Mackerras19ccb762011-07-23 17:42:46 +1000287 ld r2,PACATOC(r13)
288
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000289 li r0,KVM_HWTHREAD_IN_KVM
290 stb r0,HSTATE_HWTHREAD_STATE(r13)
291
292 /* NV GPR values from power7_idle() will no longer be valid */
293 li r0,1
294 stb r0,PACA_NAPSTATELOST(r13)
295
Paul Mackerras4619ac82013-04-17 20:31:41 +0000296 /* were we napping due to cede? */
297 lbz r0,HSTATE_NAPPING(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100298 cmpwi r0,NAPPING_CEDE
299 beq kvm_end_cede
300 cmpwi r0,NAPPING_NOVCPU
301 beq kvm_novcpu_wakeup
302
303 ld r1,PACAEMERGSP(r13)
304 subi r1,r1,STACK_FRAME_OVERHEAD
Paul Mackerras4619ac82013-04-17 20:31:41 +0000305
306 /*
307 * We weren't napping due to cede, so this must be a secondary
308 * thread being woken up to run a guest, or being woken up due
309 * to a stray IPI. (Or due to some machine check or hypervisor
310 * maintenance interrupt while the core is in KVM.)
311 */
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000312
313 /* Check the wake reason in SRR1 to see why we got here */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100314 bl kvmppc_check_wake_reason
315 cmpdi r3, 0
316 bge kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000317
Paul Mackerras4619ac82013-04-17 20:31:41 +0000318 /* get vcpu pointer, NULL if we have no vcpu to run */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000319 ld r4,HSTATE_KVM_VCPU(r13)
320 cmpdi r4,0
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000321 /* if we have no vcpu to run, go back to sleep */
Paul Mackerras7b444c62012-10-15 01:16:14 +0000322 beq kvm_no_guest
Paul Mackerrasf0888f72012-02-03 00:54:17 +0000323
Paul Mackerras56548fc2014-12-03 14:48:40 +1100324kvm_secondary_got_guest:
325
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100326 /* Set HSTATE_DSCR(r13) to something sensible */
Sam bobroff1739ea92014-05-21 16:32:38 +1000327 ld r6, PACA_DSCR(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100328 std r6, HSTATE_DSCR(r13)
Paul Mackerras371fefd2011-06-29 00:23:08 +0000329
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100330 /* Order load of vcore, ptid etc. after load of vcpu */
331 lwsync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100332 bl kvmppc_hv_entry
Paul Mackerras218309b2013-09-06 13:23:44 +1000333
334 /* Back from the guest, go back to nap */
335 /* Clear our vcpu pointer so we don't come back in early */
336 li r0, 0
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100337 /*
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100338 * Once we clear HSTATE_KVM_VCPU(r13), the code in
339 * kvmppc_run_core() is going to assume that all our vcpu
340 * state is visible in memory. This lwsync makes sure
341 * that that is true.
Paul Mackerrasf019b7a2013-11-16 17:46:03 +1100342 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000343 lwsync
Paul Mackerras5d5b99c2015-03-28 14:21:06 +1100344 std r0, HSTATE_KVM_VCPU(r13)
Paul Mackerras218309b2013-09-06 13:23:44 +1000345
Paul Mackerras56548fc2014-12-03 14:48:40 +1100346/*
347 * At this point we have finished executing in the guest.
348 * We need to wait for hwthread_req to become zero, since
349 * we may not turn on the MMU while hwthread_req is non-zero.
350 * While waiting we also need to check if we get given a vcpu to run.
351 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000352kvm_no_guest:
Paul Mackerras56548fc2014-12-03 14:48:40 +1100353 lbz r3, HSTATE_HWTHREAD_REQ(r13)
354 cmpwi r3, 0
355 bne 53f
356 HMT_MEDIUM
357 li r0, KVM_HWTHREAD_IN_KERNEL
Paul Mackerras218309b2013-09-06 13:23:44 +1000358 stb r0, HSTATE_HWTHREAD_STATE(r13)
Paul Mackerras56548fc2014-12-03 14:48:40 +1100359 /* need to recheck hwthread_req after a barrier, to avoid race */
360 sync
361 lbz r3, HSTATE_HWTHREAD_REQ(r13)
362 cmpwi r3, 0
363 bne 54f
364/*
365 * We jump to power7_wakeup_loss, which will return to the caller
366 * of power7_nap in the powernv cpu offline loop. The value we
367 * put in r3 becomes the return value for power7_nap.
368 */
Paul Mackerras218309b2013-09-06 13:23:44 +1000369 li r3, LPCR_PECE0
370 mfspr r4, SPRN_LPCR
371 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
372 mtspr SPRN_LPCR, r4
Paul Mackerras56548fc2014-12-03 14:48:40 +1100373 li r3, 0
374 b power7_wakeup_loss
375
37653: HMT_LOW
377 ld r4, HSTATE_KVM_VCPU(r13)
378 cmpdi r4, 0
379 beq kvm_no_guest
380 HMT_MEDIUM
381 b kvm_secondary_got_guest
382
38354: li r0, KVM_HWTHREAD_IN_KVM
384 stb r0, HSTATE_HWTHREAD_STATE(r13)
385 b kvm_no_guest
Paul Mackerras218309b2013-09-06 13:23:44 +1000386
387/******************************************************************************
388 * *
389 * Entry code *
390 * *
391 *****************************************************************************/
392
Paul Mackerrasde56a942011-06-29 00:21:34 +0000393.global kvmppc_hv_entry
394kvmppc_hv_entry:
395
396 /* Required state:
397 *
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100398 * R4 = vcpu pointer (or NULL)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000399 * MSR = ~IR|DR
400 * R13 = PACA
401 * R1 = host R1
Michael Neuling06a29e42014-08-19 14:59:30 +1000402 * R2 = TOC
Paul Mackerrasde56a942011-06-29 00:21:34 +0000403 * all other volatile GPRS = free
404 */
405 mflr r0
Paul Mackerras218309b2013-09-06 13:23:44 +1000406 std r0, PPC_LR_STKOFF(r1)
407 stdu r1, -112(r1)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000408
Paul Mackerrasde56a942011-06-29 00:21:34 +0000409 /* Save R1 in the PACA */
410 std r1, HSTATE_HOST_R1(r13)
411
Paul Mackerras44a3add2013-10-04 21:45:04 +1000412 li r6, KVM_GUEST_MODE_HOST_HV
413 stb r6, HSTATE_IN_GUEST(r13)
414
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100415#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
416 /* Store initial timestamp */
417 cmpdi r4, 0
418 beq 1f
419 addi r3, r4, VCPU_TB_RMENTRY
420 bl kvmhv_start_timing
4211:
422#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +0000423 /* Clear out SLB */
424 li r6,0
425 slbmte r6,r6
426 slbia
427 ptesync
428
Paul Mackerras9e368f22011-06-29 00:40:08 +0000429 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100430 * POWER7/POWER8 host -> guest partition switch code.
Paul Mackerras9e368f22011-06-29 00:40:08 +0000431 * We don't have to lock against concurrent tlbies,
432 * but we do have to coordinate across hardware threads.
433 */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100434 /* Set bit in entry map iff exit map is zero. */
435 ld r5, HSTATE_KVM_VCORE(r13)
436 li r7, 1
437 lbz r6, HSTATE_PTID(r13)
438 sld r7, r7, r6
439 addi r9, r5, VCORE_ENTRY_EXIT
44021: lwarx r3, 0, r9
441 cmpwi r3, 0x100 /* any threads starting to exit? */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000442 bge secondary_too_late /* if so we're too late to the party */
Paul Mackerras7d6c40d2015-03-28 14:21:09 +1100443 or r3, r3, r7
444 stwcx. r3, 0, r9
Paul Mackerras371fefd2011-06-29 00:23:08 +0000445 bne 21b
446
447 /* Primary thread switches to guest partition. */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100448 ld r9,VCORE_KVM(r5) /* pointer to struct kvm */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000449 cmpwi r6,0
Paul Mackerras6af27c82015-03-28 14:21:10 +1100450 bne 10f
Paul Mackerrasde56a942011-06-29 00:21:34 +0000451 ld r6,KVM_SDR1(r9)
452 lwz r7,KVM_LPID(r9)
453 li r0,LPID_RSVD /* switch to reserved LPID */
454 mtspr SPRN_LPID,r0
455 ptesync
456 mtspr SPRN_SDR1,r6 /* switch to partition page table */
457 mtspr SPRN_LPID,r7
458 isync
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000459
460 /* See if we need to flush the TLB */
461 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
462 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
463 srdi r6,r6,6 /* doubleword number */
464 sldi r6,r6,3 /* address offset */
465 add r6,r6,r9
466 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
Paul Mackerras371fefd2011-06-29 00:23:08 +0000467 li r0,1
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000468 sld r0,r0,r7
469 ld r7,0(r6)
470 and. r7,r7,r0
471 beq 22f
47223: ldarx r7,0,r6 /* if set, clear the bit */
473 andc r7,r7,r0
474 stdcx. r7,0,r6
475 bne 23b
Paul Mackerrasca252052014-01-08 21:25:22 +1100476 /* Flush the TLB of any entries for this LPID */
477 /* use arch 2.07S as a proxy for POWER8 */
478BEGIN_FTR_SECTION
479 li r6,512 /* POWER8 has 512 sets */
480FTR_SECTION_ELSE
481 li r6,128 /* POWER7 has 128 sets */
482ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras1b400ba2012-11-21 23:28:08 +0000483 mtctr r6
484 li r7,0x800 /* IS field = 0b10 */
485 ptesync
48628: tlbiel r7
487 addi r7,r7,0x1000
488 bdnz 28b
489 ptesync
490
Paul Mackerras93b0f4d2013-09-06 13:17:46 +1000491 /* Add timebase offset onto timebase */
49222: ld r8,VCORE_TB_OFFSET(r5)
493 cmpdi r8,0
494 beq 37f
495 mftb r6 /* current host timebase */
496 add r8,r8,r6
497 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
498 mftb r7 /* check if lower 24 bits overflowed */
499 clrldi r6,r6,40
500 clrldi r7,r7,40
501 cmpld r7,r6
502 bge 37f
503 addis r8,r8,0x100 /* if so, increment upper 40 bits */
504 mtspr SPRN_TBU40,r8
505
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000506 /* Load guest PCR value to select appropriate compat mode */
50737: ld r7, VCORE_PCR(r5)
508 cmpdi r7, 0
509 beq 38f
510 mtspr SPRN_PCR, r7
51138:
Michael Neulingb005255e2014-01-08 21:25:21 +1100512
513BEGIN_FTR_SECTION
514 /* DPDES is shared between threads */
515 ld r8, VCORE_DPDES(r5)
516 mtspr SPRN_DPDES, r8
517END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
518
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000519 li r0,1
Paul Mackerras371fefd2011-06-29 00:23:08 +0000520 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000521
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100522 /* Do we have a guest vcpu to run? */
Paul Mackerras6af27c82015-03-28 14:21:10 +110052310: cmpdi r4, 0
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100524 beq kvmppc_primary_no_guest
525kvmppc_got_guest:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000526
527 /* Load up guest SLB entries */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100528 lwz r5,VCPU_SLB_MAX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000529 cmpwi r5,0
530 beq 9f
531 mtctr r5
532 addi r6,r4,VCPU_SLB
5331: ld r8,VCPU_SLB_E(r6)
534 ld r9,VCPU_SLB_V(r6)
535 slbmte r9,r8
536 addi r6,r6,VCPU_SLB_SIZE
537 bdnz 1b
5389:
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100539 /* Increment yield count if they have a VPA */
540 ld r3, VCPU_VPA(r4)
541 cmpdi r3, 0
542 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +0200543 li r6, LPPACA_YIELDCOUNT
544 LWZX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100545 addi r5, r5, 1
Alexander Graf0865a582014-06-11 10:36:17 +0200546 STWX_BE r5, r3, r6
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100547 li r6, 1
548 stb r6, VCPU_VPA_DIRTY(r4)
54925:
550
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100551 /* Save purr/spurr */
552 mfspr r5,SPRN_PURR
553 mfspr r6,SPRN_SPURR
554 std r5,HSTATE_PURR(r13)
555 std r6,HSTATE_SPURR(r13)
556 ld r7,VCPU_PURR(r4)
557 ld r8,VCPU_SPURR(r4)
558 mtspr SPRN_PURR,r7
559 mtspr SPRN_SPURR,r8
Paul Mackerrase0b7ec02014-01-08 21:25:20 +1100560
Michael Neulingeee7ff92014-01-08 21:25:19 +1100561BEGIN_FTR_SECTION
Paul Mackerrasde56a942011-06-29 00:21:34 +0000562 /* Set partition DABR */
563 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
Paul Mackerras8563bf52014-01-08 21:25:29 +1100564 lwz r5,VCPU_DABRX(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000565 ld r6,VCPU_DABR(r4)
566 mtspr SPRN_DABRX,r5
567 mtspr SPRN_DABR,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000568 isync
Michael Neulingeee7ff92014-01-08 21:25:19 +1100569END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000570
Michael Neulinge4e38122014-03-25 10:47:02 +1100571#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
572BEGIN_FTR_SECTION
573 b skip_tm
574END_FTR_SECTION_IFCLR(CPU_FTR_TM)
575
576 /* Turn on TM/FP/VSX/VMX so we can restore them. */
577 mfmsr r5
578 li r6, MSR_TM >> 32
579 sldi r6, r6, 32
580 or r5, r5, r6
581 ori r5, r5, MSR_FP
582 oris r5, r5, (MSR_VEC | MSR_VSX)@h
583 mtmsrd r5
584
585 /*
586 * The user may change these outside of a transaction, so they must
587 * always be context switched.
588 */
589 ld r5, VCPU_TFHAR(r4)
590 ld r6, VCPU_TFIAR(r4)
591 ld r7, VCPU_TEXASR(r4)
592 mtspr SPRN_TFHAR, r5
593 mtspr SPRN_TFIAR, r6
594 mtspr SPRN_TEXASR, r7
595
596 ld r5, VCPU_MSR(r4)
597 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
598 beq skip_tm /* TM not active in guest */
599
600 /* Make sure the failure summary is set, otherwise we'll program check
601 * when we trechkpt. It's possible that this might have been not set
602 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
603 * host.
604 */
605 oris r7, r7, (TEXASR_FS)@h
606 mtspr SPRN_TEXASR, r7
607
608 /*
609 * We need to load up the checkpointed state for the guest.
610 * We need to do this early as it will blow away any GPRs, VSRs and
611 * some SPRs.
612 */
613
614 mr r31, r4
615 addi r3, r31, VCPU_FPRS_TM
Alexander Graf9bf163f2014-06-16 14:41:15 +0200616 bl load_fp_state
Michael Neulinge4e38122014-03-25 10:47:02 +1100617 addi r3, r31, VCPU_VRS_TM
Alexander Graf9bf163f2014-06-16 14:41:15 +0200618 bl load_vr_state
Michael Neulinge4e38122014-03-25 10:47:02 +1100619 mr r4, r31
620 lwz r7, VCPU_VRSAVE_TM(r4)
621 mtspr SPRN_VRSAVE, r7
622
623 ld r5, VCPU_LR_TM(r4)
624 lwz r6, VCPU_CR_TM(r4)
625 ld r7, VCPU_CTR_TM(r4)
626 ld r8, VCPU_AMR_TM(r4)
627 ld r9, VCPU_TAR_TM(r4)
628 mtlr r5
629 mtcr r6
630 mtctr r7
631 mtspr SPRN_AMR, r8
632 mtspr SPRN_TAR, r9
633
634 /*
635 * Load up PPR and DSCR values but don't put them in the actual SPRs
636 * till the last moment to avoid running with userspace PPR and DSCR for
637 * too long.
638 */
639 ld r29, VCPU_DSCR_TM(r4)
640 ld r30, VCPU_PPR_TM(r4)
641
642 std r2, PACATMSCRATCH(r13) /* Save TOC */
643
644 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
645 li r5, 0
646 mtmsrd r5, 1
647
648 /* Load GPRs r0-r28 */
649 reg = 0
650 .rept 29
651 ld reg, VCPU_GPRS_TM(reg)(r31)
652 reg = reg + 1
653 .endr
654
655 mtspr SPRN_DSCR, r29
656 mtspr SPRN_PPR, r30
657
658 /* Load final GPRs */
659 ld 29, VCPU_GPRS_TM(29)(r31)
660 ld 30, VCPU_GPRS_TM(30)(r31)
661 ld 31, VCPU_GPRS_TM(31)(r31)
662
663 /* TM checkpointed state is now setup. All GPRs are now volatile. */
664 TRECHKPT
665
666 /* Now let's get back the state we need. */
667 HMT_MEDIUM
668 GET_PACA(r13)
669 ld r29, HSTATE_DSCR(r13)
670 mtspr SPRN_DSCR, r29
671 ld r4, HSTATE_KVM_VCPU(r13)
672 ld r1, HSTATE_HOST_R1(r13)
673 ld r2, PACATMSCRATCH(r13)
674
675 /* Set the MSR RI since we have our registers back. */
676 li r5, MSR_RI
677 mtmsrd r5, 1
678skip_tm:
679#endif
680
Paul Mackerrasde56a942011-06-29 00:21:34 +0000681 /* Load guest PMU registers */
682 /* R4 is live here (vcpu pointer) */
683 li r3, 1
684 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
685 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
686 isync
Paul Mackerras9bc01a92014-05-26 19:48:40 +1000687BEGIN_FTR_SECTION
688 ld r3, VCPU_MMCR(r4)
689 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
690 cmpwi r5, MMCR0_PMAO
691 beql kvmppc_fix_pmao
692END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000693 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
694 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
695 lwz r6, VCPU_PMC + 8(r4)
696 lwz r7, VCPU_PMC + 12(r4)
697 lwz r8, VCPU_PMC + 16(r4)
698 lwz r9, VCPU_PMC + 20(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000699 mtspr SPRN_PMC1, r3
700 mtspr SPRN_PMC2, r5
701 mtspr SPRN_PMC3, r6
702 mtspr SPRN_PMC4, r7
703 mtspr SPRN_PMC5, r8
704 mtspr SPRN_PMC6, r9
Paul Mackerrasde56a942011-06-29 00:21:34 +0000705 ld r3, VCPU_MMCR(r4)
706 ld r5, VCPU_MMCR + 8(r4)
707 ld r6, VCPU_MMCR + 16(r4)
708 ld r7, VCPU_SIAR(r4)
709 ld r8, VCPU_SDAR(r4)
710 mtspr SPRN_MMCR1, r5
711 mtspr SPRN_MMCRA, r6
712 mtspr SPRN_SIAR, r7
713 mtspr SPRN_SDAR, r8
Michael Neulingb005255e2014-01-08 21:25:21 +1100714BEGIN_FTR_SECTION
715 ld r5, VCPU_MMCR + 24(r4)
716 ld r6, VCPU_SIER(r4)
717 lwz r7, VCPU_PMC + 24(r4)
718 lwz r8, VCPU_PMC + 28(r4)
719 ld r9, VCPU_MMCR + 32(r4)
720 mtspr SPRN_MMCR2, r5
721 mtspr SPRN_SIER, r6
722 mtspr SPRN_SPMC1, r7
723 mtspr SPRN_SPMC2, r8
724 mtspr SPRN_MMCRS, r9
725END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000726 mtspr SPRN_MMCR0, r3
727 isync
728
729 /* Load up FP, VMX and VSX registers */
730 bl kvmppc_load_fp
731
732 ld r14, VCPU_GPR(R14)(r4)
733 ld r15, VCPU_GPR(R15)(r4)
734 ld r16, VCPU_GPR(R16)(r4)
735 ld r17, VCPU_GPR(R17)(r4)
736 ld r18, VCPU_GPR(R18)(r4)
737 ld r19, VCPU_GPR(R19)(r4)
738 ld r20, VCPU_GPR(R20)(r4)
739 ld r21, VCPU_GPR(R21)(r4)
740 ld r22, VCPU_GPR(R22)(r4)
741 ld r23, VCPU_GPR(R23)(r4)
742 ld r24, VCPU_GPR(R24)(r4)
743 ld r25, VCPU_GPR(R25)(r4)
744 ld r26, VCPU_GPR(R26)(r4)
745 ld r27, VCPU_GPR(R27)(r4)
746 ld r28, VCPU_GPR(R28)(r4)
747 ld r29, VCPU_GPR(R29)(r4)
748 ld r30, VCPU_GPR(R30)(r4)
749 ld r31, VCPU_GPR(R31)(r4)
750
Paul Mackerrasde56a942011-06-29 00:21:34 +0000751 /* Switch DSCR to guest value */
752 ld r5, VCPU_DSCR(r4)
753 mtspr SPRN_DSCR, r5
Paul Mackerrasde56a942011-06-29 00:21:34 +0000754
Michael Neulingb005255e2014-01-08 21:25:21 +1100755BEGIN_FTR_SECTION
Paul Mackerrasc17b98c2014-12-03 13:30:38 +1100756 /* Skip next section on POWER7 */
Michael Neulingb005255e2014-01-08 21:25:21 +1100757 b 8f
758END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
759 /* Turn on TM so we can access TFHAR/TFIAR/TEXASR */
760 mfmsr r8
761 li r0, 1
762 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
763 mtmsrd r8
764
765 /* Load up POWER8-specific registers */
766 ld r5, VCPU_IAMR(r4)
767 lwz r6, VCPU_PSPB(r4)
768 ld r7, VCPU_FSCR(r4)
769 mtspr SPRN_IAMR, r5
770 mtspr SPRN_PSPB, r6
771 mtspr SPRN_FSCR, r7
772 ld r5, VCPU_DAWR(r4)
773 ld r6, VCPU_DAWRX(r4)
774 ld r7, VCPU_CIABR(r4)
775 ld r8, VCPU_TAR(r4)
776 mtspr SPRN_DAWR, r5
777 mtspr SPRN_DAWRX, r6
778 mtspr SPRN_CIABR, r7
779 mtspr SPRN_TAR, r8
780 ld r5, VCPU_IC(r4)
781 ld r6, VCPU_VTB(r4)
782 mtspr SPRN_IC, r5
783 mtspr SPRN_VTB, r6
Michael Neuling7b490412014-01-08 21:25:32 +1100784 ld r8, VCPU_EBBHR(r4)
Michael Neulingb005255e2014-01-08 21:25:21 +1100785 mtspr SPRN_EBBHR, r8
786 ld r5, VCPU_EBBRR(r4)
787 ld r6, VCPU_BESCR(r4)
788 ld r7, VCPU_CSIGR(r4)
789 ld r8, VCPU_TACR(r4)
790 mtspr SPRN_EBBRR, r5
791 mtspr SPRN_BESCR, r6
792 mtspr SPRN_CSIGR, r7
793 mtspr SPRN_TACR, r8
794 ld r5, VCPU_TCSCR(r4)
795 ld r6, VCPU_ACOP(r4)
796 lwz r7, VCPU_GUEST_PID(r4)
797 ld r8, VCPU_WORT(r4)
798 mtspr SPRN_TCSCR, r5
799 mtspr SPRN_ACOP, r6
800 mtspr SPRN_PID, r7
801 mtspr SPRN_WORT, r8
8028:
803
Paul Mackerrasde56a942011-06-29 00:21:34 +0000804 /*
805 * Set the decrementer to the guest decrementer.
806 */
807 ld r8,VCPU_DEC_EXPIRES(r4)
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +1100808 /* r8 is a host timebase value here, convert to guest TB */
809 ld r5,HSTATE_KVM_VCORE(r13)
810 ld r6,VCORE_TB_OFFSET(r5)
811 add r8,r8,r6
Paul Mackerrasde56a942011-06-29 00:21:34 +0000812 mftb r7
813 subf r3,r7,r8
814 mtspr SPRN_DEC,r3
815 stw r3,VCPU_DEC(r4)
816
817 ld r5, VCPU_SPRG0(r4)
818 ld r6, VCPU_SPRG1(r4)
819 ld r7, VCPU_SPRG2(r4)
820 ld r8, VCPU_SPRG3(r4)
821 mtspr SPRN_SPRG0, r5
822 mtspr SPRN_SPRG1, r6
823 mtspr SPRN_SPRG2, r7
824 mtspr SPRN_SPRG3, r8
825
Paul Mackerrasde56a942011-06-29 00:21:34 +0000826 /* Load up DAR and DSISR */
827 ld r5, VCPU_DAR(r4)
828 lwz r6, VCPU_DSISR(r4)
829 mtspr SPRN_DAR, r5
830 mtspr SPRN_DSISR, r6
831
Paul Mackerrasde56a942011-06-29 00:21:34 +0000832 /* Restore AMR and UAMOR, set AMOR to all 1s */
833 ld r5,VCPU_AMR(r4)
834 ld r6,VCPU_UAMOR(r4)
835 li r7,-1
836 mtspr SPRN_AMR,r5
837 mtspr SPRN_UAMOR,r6
838 mtspr SPRN_AMOR,r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000839
840 /* Restore state of CTRL run bit; assume 1 on entry */
841 lwz r5,VCPU_CTRL(r4)
842 andi. r5,r5,1
843 bne 4f
844 mfspr r6,SPRN_CTRLF
845 clrrdi r6,r6,1
846 mtspr SPRN_CTRLT,r6
8474:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100848 /* Secondary threads wait for primary to have done partition switch */
849 ld r5, HSTATE_KVM_VCORE(r13)
850 lbz r6, HSTATE_PTID(r13)
851 cmpwi r6, 0
852 beq 21f
853 lbz r0, VCORE_IN_GUEST(r5)
854 cmpwi r0, 0
855 bne 21f
856 HMT_LOW
85720: lbz r0, VCORE_IN_GUEST(r5)
858 cmpwi r0, 0
859 beq 20b
860 HMT_MEDIUM
86121:
862 /* Set LPCR. */
863 ld r8,VCORE_LPCR(r5)
864 mtspr SPRN_LPCR,r8
865 isync
866
867 /* Check if HDEC expires soon */
868 mfspr r3, SPRN_HDEC
869 cmpwi r3, 512 /* 1 microsecond */
870 blt hdec_soon
871
Paul Mackerrasde56a942011-06-29 00:21:34 +0000872 ld r6, VCPU_CTR(r4)
873 lwz r7, VCPU_XER(r4)
874
875 mtctr r6
876 mtxer r7
877
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100878kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
Paul Mackerras4619ac82013-04-17 20:31:41 +0000879 ld r10, VCPU_PC(r4)
880 ld r11, VCPU_MSR(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000881 ld r6, VCPU_SRR0(r4)
882 ld r7, VCPU_SRR1(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100883 mtspr SPRN_SRR0, r6
884 mtspr SPRN_SRR1, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +0000885
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100886deliver_guest_interrupt:
Paul Mackerras4619ac82013-04-17 20:31:41 +0000887 /* r11 = vcpu->arch.msr & ~MSR_HV */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000888 rldicl r11, r11, 63 - MSR_HV_LG, 1
889 rotldi r11, r11, 1 + MSR_HV_LG
890 ori r11, r11, MSR_ME
891
Paul Mackerras19ccb762011-07-23 17:42:46 +1000892 /* Check if we can deliver an external or decrementer interrupt now */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100893 ld r0, VCPU_PENDING_EXC(r4)
894 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
895 cmpdi cr1, r0, 0
896 andi. r8, r11, MSR_EE
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100897 mfspr r8, SPRN_LPCR
898 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
899 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
900 mtspr SPRN_LPCR, r8
Paul Mackerras19ccb762011-07-23 17:42:46 +1000901 isync
Paul Mackerras19ccb762011-07-23 17:42:46 +1000902 beq 5f
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100903 li r0, BOOK3S_INTERRUPT_EXTERNAL
904 bne cr1, 12f
905 mfspr r0, SPRN_DEC
906 cmpwi r0, 0
907 li r0, BOOK3S_INTERRUPT_DECREMENTER
908 bge 5f
909
91012: mtspr SPRN_SRR0, r10
Paul Mackerras19ccb762011-07-23 17:42:46 +1000911 mr r10,r0
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100912 mtspr SPRN_SRR1, r11
Michael Neulinge4e38122014-03-25 10:47:02 +1100913 mr r9, r4
914 bl kvmppc_msr_interrupt
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11009155:
Paul Mackerras19ccb762011-07-23 17:42:46 +1000916
Liu Ping Fan27025a62013-11-19 14:12:48 +0800917/*
918 * Required state:
919 * R4 = vcpu
920 * R10: value for HSRR0
921 * R11: value for HSRR1
922 * R13 = PACA
923 */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000924fast_guest_return:
Paul Mackerras4619ac82013-04-17 20:31:41 +0000925 li r0,0
926 stb r0,VCPU_CEDED(r4) /* cancel cede */
Paul Mackerrasde56a942011-06-29 00:21:34 +0000927 mtspr SPRN_HSRR0,r10
928 mtspr SPRN_HSRR1,r11
929
930 /* Activate guest mode, so faults get handled by KVM */
Paul Mackerras44a3add2013-10-04 21:45:04 +1000931 li r9, KVM_GUEST_MODE_GUEST_HV
Paul Mackerrasde56a942011-06-29 00:21:34 +0000932 stb r9, HSTATE_IN_GUEST(r13)
933
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100934#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
935 /* Accumulate timing */
936 addi r3, r4, VCPU_TB_GUEST
937 bl kvmhv_accumulate_time
938#endif
939
Paul Mackerrasde56a942011-06-29 00:21:34 +0000940 /* Enter guest */
941
Paul Mackerras0acb9112013-02-04 18:10:51 +0000942BEGIN_FTR_SECTION
943 ld r5, VCPU_CFAR(r4)
944 mtspr SPRN_CFAR, r5
945END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000946BEGIN_FTR_SECTION
947 ld r0, VCPU_PPR(r4)
948END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerras0acb9112013-02-04 18:10:51 +0000949
Paul Mackerrasde56a942011-06-29 00:21:34 +0000950 ld r5, VCPU_LR(r4)
951 lwz r6, VCPU_CR(r4)
952 mtlr r5
953 mtcr r6
954
Michael Neulingc75df6f2012-06-25 13:33:10 +0000955 ld r1, VCPU_GPR(R1)(r4)
956 ld r2, VCPU_GPR(R2)(r4)
957 ld r3, VCPU_GPR(R3)(r4)
958 ld r5, VCPU_GPR(R5)(r4)
959 ld r6, VCPU_GPR(R6)(r4)
960 ld r7, VCPU_GPR(R7)(r4)
961 ld r8, VCPU_GPR(R8)(r4)
962 ld r9, VCPU_GPR(R9)(r4)
963 ld r10, VCPU_GPR(R10)(r4)
964 ld r11, VCPU_GPR(R11)(r4)
965 ld r12, VCPU_GPR(R12)(r4)
966 ld r13, VCPU_GPR(R13)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000967
Paul Mackerras4b8473c2013-09-20 14:52:39 +1000968BEGIN_FTR_SECTION
969 mtspr SPRN_PPR, r0
970END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
971 ld r0, VCPU_GPR(R0)(r4)
Michael Neulingc75df6f2012-06-25 13:33:10 +0000972 ld r4, VCPU_GPR(R4)(r4)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000973
974 hrfid
975 b .
976
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100977secondary_too_late:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100978 li r12, 0
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100979 cmpdi r4, 0
980 beq 11f
Paul Mackerras6af27c82015-03-28 14:21:10 +1100981 stw r12, VCPU_TRAP(r4)
982#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100983 addi r3, r4, VCPU_TB_RMEXIT
984 bl kvmhv_accumulate_time
Paul Mackerras6af27c82015-03-28 14:21:10 +1100985#endif
Paul Mackerrasb6c295d2015-03-28 14:21:02 +110098611: b kvmhv_switch_to_host
987
988hdec_soon:
Paul Mackerras6af27c82015-03-28 14:21:10 +1100989 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
990 stw r12, VCPU_TRAP(r4)
991 mr r9, r4
992#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100993 addi r3, r4, VCPU_TB_RMEXIT
994 bl kvmhv_accumulate_time
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100995#endif
Paul Mackerras6af27c82015-03-28 14:21:10 +1100996 b guest_exit_cont
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100997
Paul Mackerrasde56a942011-06-29 00:21:34 +0000998/******************************************************************************
999 * *
1000 * Exit code *
1001 * *
1002 *****************************************************************************/
1003
1004/*
1005 * We come here from the first-level interrupt handlers.
1006 */
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301007 .globl kvmppc_interrupt_hv
1008kvmppc_interrupt_hv:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001009 /*
1010 * Register contents:
1011 * R12 = interrupt vector
1012 * R13 = PACA
1013 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
1014 * guest R13 saved in SPRN_SCRATCH0
1015 */
Aneesh Kumar K.V36e7bb32013-11-11 19:29:47 +05301016 std r9, HSTATE_SCRATCH2(r13)
Paul Mackerras44a3add2013-10-04 21:45:04 +10001017
1018 lbz r9, HSTATE_IN_GUEST(r13)
1019 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1020 beq kvmppc_bad_host_intr
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301021#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1022 cmpwi r9, KVM_GUEST_MODE_GUEST
Aneesh Kumar K.V36e7bb32013-11-11 19:29:47 +05301023 ld r9, HSTATE_SCRATCH2(r13)
Aneesh Kumar K.Vdd96b2c2013-10-07 22:17:55 +05301024 beq kvmppc_interrupt_pr
1025#endif
Paul Mackerras44a3add2013-10-04 21:45:04 +10001026 /* We're now back in the host but in guest MMU context */
1027 li r9, KVM_GUEST_MODE_HOST_HV
1028 stb r9, HSTATE_IN_GUEST(r13)
1029
Paul Mackerrasde56a942011-06-29 00:21:34 +00001030 ld r9, HSTATE_KVM_VCPU(r13)
1031
1032 /* Save registers */
1033
Michael Neulingc75df6f2012-06-25 13:33:10 +00001034 std r0, VCPU_GPR(R0)(r9)
1035 std r1, VCPU_GPR(R1)(r9)
1036 std r2, VCPU_GPR(R2)(r9)
1037 std r3, VCPU_GPR(R3)(r9)
1038 std r4, VCPU_GPR(R4)(r9)
1039 std r5, VCPU_GPR(R5)(r9)
1040 std r6, VCPU_GPR(R6)(r9)
1041 std r7, VCPU_GPR(R7)(r9)
1042 std r8, VCPU_GPR(R8)(r9)
Aneesh Kumar K.V36e7bb32013-11-11 19:29:47 +05301043 ld r0, HSTATE_SCRATCH2(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001044 std r0, VCPU_GPR(R9)(r9)
1045 std r10, VCPU_GPR(R10)(r9)
1046 std r11, VCPU_GPR(R11)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001047 ld r3, HSTATE_SCRATCH0(r13)
1048 lwz r4, HSTATE_SCRATCH1(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001049 std r3, VCPU_GPR(R12)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001050 stw r4, VCPU_CR(r9)
Paul Mackerras0acb9112013-02-04 18:10:51 +00001051BEGIN_FTR_SECTION
1052 ld r3, HSTATE_CFAR(r13)
1053 std r3, VCPU_CFAR(r9)
1054END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001055BEGIN_FTR_SECTION
1056 ld r4, HSTATE_PPR(r13)
1057 std r4, VCPU_PPR(r9)
1058END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001059
1060 /* Restore R1/R2 so we can handle faults */
1061 ld r1, HSTATE_HOST_R1(r13)
1062 ld r2, PACATOC(r13)
1063
1064 mfspr r10, SPRN_SRR0
1065 mfspr r11, SPRN_SRR1
1066 std r10, VCPU_SRR0(r9)
1067 std r11, VCPU_SRR1(r9)
1068 andi. r0, r12, 2 /* need to read HSRR0/1? */
1069 beq 1f
1070 mfspr r10, SPRN_HSRR0
1071 mfspr r11, SPRN_HSRR1
1072 clrrdi r12, r12, 2
10731: std r10, VCPU_PC(r9)
1074 std r11, VCPU_MSR(r9)
1075
1076 GET_SCRATCH0(r3)
1077 mflr r4
Michael Neulingc75df6f2012-06-25 13:33:10 +00001078 std r3, VCPU_GPR(R13)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001079 std r4, VCPU_LR(r9)
1080
Paul Mackerrasde56a942011-06-29 00:21:34 +00001081 stw r12,VCPU_TRAP(r9)
1082
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001083#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1084 addi r3, r9, VCPU_TB_RMINTR
1085 mr r4, r9
1086 bl kvmhv_accumulate_time
1087 ld r5, VCPU_GPR(R5)(r9)
1088 ld r6, VCPU_GPR(R6)(r9)
1089 ld r7, VCPU_GPR(R7)(r9)
1090 ld r8, VCPU_GPR(R8)(r9)
1091#endif
1092
Paul Mackerras4a157d62014-12-03 13:30:39 +11001093 /* Save HEIR (HV emulation assist reg) in emul_inst
Paul Mackerras697d3892011-12-12 12:36:37 +00001094 if this is an HEI (HV emulation interrupt, e40) */
1095 li r3,KVM_INST_FETCH_FAILED
Paul Mackerras2bf27602015-03-20 20:39:40 +11001096 stw r3,VCPU_LAST_INST(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001097 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1098 bne 11f
1099 mfspr r3,SPRN_HEIR
Paul Mackerras4a157d62014-12-03 13:30:39 +1100110011: stw r3,VCPU_HEIR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001101
1102 /* these are volatile across C function calls */
1103 mfctr r3
1104 mfxer r4
1105 std r3, VCPU_CTR(r9)
1106 stw r4, VCPU_XER(r9)
1107
Paul Mackerras697d3892011-12-12 12:36:37 +00001108 /* If this is a page table miss then see if it's theirs or ours */
1109 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1110 beq kvmppc_hdsi
Paul Mackerras342d3db2011-12-12 12:38:05 +00001111 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1112 beq kvmppc_hisi
Paul Mackerras697d3892011-12-12 12:36:37 +00001113
Paul Mackerrasde56a942011-06-29 00:21:34 +00001114 /* See if this is a leftover HDEC interrupt */
1115 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1116 bne 2f
1117 mfspr r3,SPRN_HDEC
1118 cmpwi r3,0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001119 mr r4,r9
1120 bge fast_guest_return
Paul Mackerrasde56a942011-06-29 00:21:34 +000011212:
Paul Mackerras697d3892011-12-12 12:36:37 +00001122 /* See if this is an hcall we can handle in real mode */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001123 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1124 beq hcall_try_real_mode
Paul Mackerrasde56a942011-06-29 00:21:34 +00001125
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001126 /* External interrupt ? */
1127 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001128 bne+ guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001129
1130 /* External interrupt, first check for host_ipi. If this is
1131 * set, we know the host wants us out so let's do it now
1132 */
Paul Mackerrasc9342432013-09-06 13:24:13 +10001133 bl kvmppc_read_intr
1134 cmpdi r3, 0
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001135 bgt guest_exit_cont
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +00001136
Paul Mackerras4619ac82013-04-17 20:31:41 +00001137 /* Check if any CPU is heading out to the host, if so head out too */
1138 ld r5, HSTATE_KVM_VCORE(r13)
1139 lwz r0, VCORE_ENTRY_EXIT(r5)
1140 cmpwi r0, 0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11001141 mr r4, r9
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001142 blt deliver_guest_interrupt
Paul Mackerrasde56a942011-06-29 00:21:34 +00001143
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001144guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001145 /* Save more register state */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001146 mfdar r6
1147 mfdsisr r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001148 std r6, VCPU_DAR(r9)
1149 stw r7, VCPU_DSISR(r9)
Paul Mackerras697d3892011-12-12 12:36:37 +00001150 /* don't overwrite fault_dar/fault_dsisr if HDSI */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001151 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
Paul Mackerras6af27c82015-03-28 14:21:10 +11001152 beq mc_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001153 std r6, VCPU_FAULT_DAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001154 stw r7, VCPU_FAULT_DSISR(r9)
1155
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001156 /* See if it is a machine check */
1157 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1158 beq machine_check_realmode
1159mc_cont:
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001160#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1161 addi r3, r9, VCPU_TB_RMEXIT
1162 mr r4, r9
1163 bl kvmhv_accumulate_time
1164#endif
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001165
Paul Mackerras6af27c82015-03-28 14:21:10 +11001166 /* Increment exit count, poke other threads to exit */
1167 bl kvmhv_commence_exit
Paul Mackerraseddb60f2015-03-28 14:21:11 +11001168 nop
1169 ld r9, HSTATE_KVM_VCPU(r13)
1170 lwz r12, VCPU_TRAP(r9)
Paul Mackerras6af27c82015-03-28 14:21:10 +11001171
Paul Mackerrasde56a942011-06-29 00:21:34 +00001172 /* Save guest CTRL register, set runlatch to 1 */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001173 mfspr r6,SPRN_CTRLF
Paul Mackerrasde56a942011-06-29 00:21:34 +00001174 stw r6,VCPU_CTRL(r9)
1175 andi. r0,r6,1
1176 bne 4f
1177 ori r6,r6,1
1178 mtspr SPRN_CTRLT,r6
11794:
1180 /* Read the guest SLB and save it away */
1181 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1182 mtctr r0
1183 li r6,0
1184 addi r7,r9,VCPU_SLB
1185 li r5,0
11861: slbmfee r8,r6
1187 andis. r0,r8,SLB_ESID_V@h
1188 beq 2f
1189 add r8,r8,r6 /* put index in */
1190 slbmfev r3,r6
1191 std r8,VCPU_SLB_E(r7)
1192 std r3,VCPU_SLB_V(r7)
1193 addi r7,r7,VCPU_SLB_SIZE
1194 addi r5,r5,1
11952: addi r6,r6,1
1196 bdnz 1b
1197 stw r5,VCPU_SLB_MAX(r9)
1198
1199 /*
1200 * Save the guest PURR/SPURR
1201 */
1202 mfspr r5,SPRN_PURR
1203 mfspr r6,SPRN_SPURR
1204 ld r7,VCPU_PURR(r9)
1205 ld r8,VCPU_SPURR(r9)
1206 std r5,VCPU_PURR(r9)
1207 std r6,VCPU_SPURR(r9)
1208 subf r5,r7,r5
1209 subf r6,r8,r6
1210
1211 /*
1212 * Restore host PURR/SPURR and add guest times
1213 * so that the time in the guest gets accounted.
1214 */
1215 ld r3,HSTATE_PURR(r13)
1216 ld r4,HSTATE_SPURR(r13)
1217 add r3,r3,r5
1218 add r4,r4,r6
1219 mtspr SPRN_PURR,r3
1220 mtspr SPRN_SPURR,r4
1221
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001222 /* Save DEC */
1223 mfspr r5,SPRN_DEC
1224 mftb r6
1225 extsw r5,r5
1226 add r5,r5,r6
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001227 /* r5 is a guest timebase value here, convert to host TB */
1228 ld r3,HSTATE_KVM_VCORE(r13)
1229 ld r4,VCORE_TB_OFFSET(r3)
1230 subf r5,r4,r5
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001231 std r5,VCPU_DEC_EXPIRES(r9)
1232
Michael Neulingb005255e2014-01-08 21:25:21 +11001233BEGIN_FTR_SECTION
1234 b 8f
1235END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
Michael Neulingb005255e2014-01-08 21:25:21 +11001236 /* Save POWER8-specific registers */
1237 mfspr r5, SPRN_IAMR
1238 mfspr r6, SPRN_PSPB
1239 mfspr r7, SPRN_FSCR
1240 std r5, VCPU_IAMR(r9)
1241 stw r6, VCPU_PSPB(r9)
1242 std r7, VCPU_FSCR(r9)
1243 mfspr r5, SPRN_IC
1244 mfspr r6, SPRN_VTB
1245 mfspr r7, SPRN_TAR
1246 std r5, VCPU_IC(r9)
1247 std r6, VCPU_VTB(r9)
1248 std r7, VCPU_TAR(r9)
Michael Neuling7b490412014-01-08 21:25:32 +11001249 mfspr r8, SPRN_EBBHR
Michael Neulingb005255e2014-01-08 21:25:21 +11001250 std r8, VCPU_EBBHR(r9)
1251 mfspr r5, SPRN_EBBRR
1252 mfspr r6, SPRN_BESCR
1253 mfspr r7, SPRN_CSIGR
1254 mfspr r8, SPRN_TACR
1255 std r5, VCPU_EBBRR(r9)
1256 std r6, VCPU_BESCR(r9)
1257 std r7, VCPU_CSIGR(r9)
1258 std r8, VCPU_TACR(r9)
1259 mfspr r5, SPRN_TCSCR
1260 mfspr r6, SPRN_ACOP
1261 mfspr r7, SPRN_PID
1262 mfspr r8, SPRN_WORT
1263 std r5, VCPU_TCSCR(r9)
1264 std r6, VCPU_ACOP(r9)
1265 stw r7, VCPU_GUEST_PID(r9)
1266 std r8, VCPU_WORT(r9)
12678:
1268
Paul Mackerrasde56a942011-06-29 00:21:34 +00001269 /* Save and reset AMR and UAMOR before turning on the MMU */
1270 mfspr r5,SPRN_AMR
1271 mfspr r6,SPRN_UAMOR
1272 std r5,VCPU_AMR(r9)
1273 std r6,VCPU_UAMOR(r9)
1274 li r6,0
1275 mtspr SPRN_AMR,r6
1276
Paul Mackerrasde56a942011-06-29 00:21:34 +00001277 /* Switch DSCR back to host value */
1278 mfspr r8, SPRN_DSCR
1279 ld r7, HSTATE_DSCR(r13)
Paul Mackerrascfc86022013-09-21 09:53:28 +10001280 std r8, VCPU_DSCR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001281 mtspr SPRN_DSCR, r7
1282
1283 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001284 std r14, VCPU_GPR(R14)(r9)
1285 std r15, VCPU_GPR(R15)(r9)
1286 std r16, VCPU_GPR(R16)(r9)
1287 std r17, VCPU_GPR(R17)(r9)
1288 std r18, VCPU_GPR(R18)(r9)
1289 std r19, VCPU_GPR(R19)(r9)
1290 std r20, VCPU_GPR(R20)(r9)
1291 std r21, VCPU_GPR(R21)(r9)
1292 std r22, VCPU_GPR(R22)(r9)
1293 std r23, VCPU_GPR(R23)(r9)
1294 std r24, VCPU_GPR(R24)(r9)
1295 std r25, VCPU_GPR(R25)(r9)
1296 std r26, VCPU_GPR(R26)(r9)
1297 std r27, VCPU_GPR(R27)(r9)
1298 std r28, VCPU_GPR(R28)(r9)
1299 std r29, VCPU_GPR(R29)(r9)
1300 std r30, VCPU_GPR(R30)(r9)
1301 std r31, VCPU_GPR(R31)(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001302
1303 /* Save SPRGs */
1304 mfspr r3, SPRN_SPRG0
1305 mfspr r4, SPRN_SPRG1
1306 mfspr r5, SPRN_SPRG2
1307 mfspr r6, SPRN_SPRG3
1308 std r3, VCPU_SPRG0(r9)
1309 std r4, VCPU_SPRG1(r9)
1310 std r5, VCPU_SPRG2(r9)
1311 std r6, VCPU_SPRG3(r9)
1312
Paul Mackerras89436332012-03-02 01:38:23 +00001313 /* save FP state */
1314 mr r3, r9
Paul Mackerras595e4f72013-10-15 20:43:04 +11001315 bl kvmppc_save_fp
Paul Mackerras89436332012-03-02 01:38:23 +00001316
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001317#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1318BEGIN_FTR_SECTION
1319 b 2f
1320END_FTR_SECTION_IFCLR(CPU_FTR_TM)
1321 /* Turn on TM. */
1322 mfmsr r8
1323 li r0, 1
1324 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
1325 mtmsrd r8
1326
1327 ld r5, VCPU_MSR(r9)
1328 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
1329 beq 1f /* TM not active in guest. */
1330
1331 li r3, TM_CAUSE_KVM_RESCHED
1332
1333 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
1334 li r5, 0
1335 mtmsrd r5, 1
1336
1337 /* All GPRs are volatile at this point. */
1338 TRECLAIM(R3)
1339
1340 /* Temporarily store r13 and r9 so we have some regs to play with */
1341 SET_SCRATCH0(r13)
1342 GET_PACA(r13)
1343 std r9, PACATMSCRATCH(r13)
1344 ld r9, HSTATE_KVM_VCPU(r13)
1345
1346 /* Get a few more GPRs free. */
1347 std r29, VCPU_GPRS_TM(29)(r9)
1348 std r30, VCPU_GPRS_TM(30)(r9)
1349 std r31, VCPU_GPRS_TM(31)(r9)
1350
1351 /* Save away PPR and DSCR soon so don't run with user values. */
1352 mfspr r31, SPRN_PPR
1353 HMT_MEDIUM
1354 mfspr r30, SPRN_DSCR
1355 ld r29, HSTATE_DSCR(r13)
1356 mtspr SPRN_DSCR, r29
1357
1358 /* Save all but r9, r13 & r29-r31 */
1359 reg = 0
1360 .rept 29
1361 .if (reg != 9) && (reg != 13)
1362 std reg, VCPU_GPRS_TM(reg)(r9)
1363 .endif
1364 reg = reg + 1
1365 .endr
1366 /* ... now save r13 */
1367 GET_SCRATCH0(r4)
1368 std r4, VCPU_GPRS_TM(13)(r9)
1369 /* ... and save r9 */
1370 ld r4, PACATMSCRATCH(r13)
1371 std r4, VCPU_GPRS_TM(9)(r9)
1372
1373 /* Reload stack pointer and TOC. */
1374 ld r1, HSTATE_HOST_R1(r13)
1375 ld r2, PACATOC(r13)
1376
1377 /* Set MSR RI now we have r1 and r13 back. */
1378 li r5, MSR_RI
1379 mtmsrd r5, 1
1380
1381 /* Save away checkpinted SPRs. */
1382 std r31, VCPU_PPR_TM(r9)
1383 std r30, VCPU_DSCR_TM(r9)
1384 mflr r5
1385 mfcr r6
1386 mfctr r7
1387 mfspr r8, SPRN_AMR
1388 mfspr r10, SPRN_TAR
1389 std r5, VCPU_LR_TM(r9)
1390 stw r6, VCPU_CR_TM(r9)
1391 std r7, VCPU_CTR_TM(r9)
1392 std r8, VCPU_AMR_TM(r9)
1393 std r10, VCPU_TAR_TM(r9)
1394
1395 /* Restore r12 as trap number. */
1396 lwz r12, VCPU_TRAP(r9)
1397
1398 /* Save FP/VSX. */
1399 addi r3, r9, VCPU_FPRS_TM
Alexander Graf9bf163f2014-06-16 14:41:15 +02001400 bl store_fp_state
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001401 addi r3, r9, VCPU_VRS_TM
Alexander Graf9bf163f2014-06-16 14:41:15 +02001402 bl store_vr_state
Paul Mackerras0a8ecce2014-04-14 08:56:26 +10001403 mfspr r6, SPRN_VRSAVE
1404 stw r6, VCPU_VRSAVE_TM(r9)
14051:
1406 /*
1407 * We need to save these SPRs after the treclaim so that the software
1408 * error code is recorded correctly in the TEXASR. Also the user may
1409 * change these outside of a transaction, so they must always be
1410 * context switched.
1411 */
1412 mfspr r5, SPRN_TFHAR
1413 mfspr r6, SPRN_TFIAR
1414 mfspr r7, SPRN_TEXASR
1415 std r5, VCPU_TFHAR(r9)
1416 std r6, VCPU_TFIAR(r9)
1417 std r7, VCPU_TEXASR(r9)
14182:
1419#endif
1420
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001421 /* Increment yield count if they have a VPA */
1422 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1423 cmpdi r8, 0
1424 beq 25f
Alexander Graf0865a582014-06-11 10:36:17 +02001425 li r4, LPPACA_YIELDCOUNT
1426 LWZX_BE r3, r8, r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001427 addi r3, r3, 1
Alexander Graf0865a582014-06-11 10:36:17 +02001428 STWX_BE r3, r8, r4
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001429 li r3, 1
1430 stb r3, VCPU_VPA_DIRTY(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000143125:
1432 /* Save PMU registers if requested */
1433 /* r8 and cr0.eq are live here */
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001434BEGIN_FTR_SECTION
1435 /*
1436 * POWER8 seems to have a hardware bug where setting
1437 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1438 * when some counters are already negative doesn't seem
1439 * to cause a performance monitor alert (and hence interrupt).
1440 * The effect of this is that when saving the PMU state,
1441 * if there is no PMU alert pending when we read MMCR0
1442 * before freezing the counters, but one becomes pending
1443 * before we read the counters, we lose it.
1444 * To work around this, we need a way to freeze the counters
1445 * before reading MMCR0. Normally, freezing the counters
1446 * is done by writing MMCR0 (to set MMCR0[FC]) which
1447 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1448 * we can also freeze the counters using MMCR2, by writing
1449 * 1s to all the counter freeze condition bits (there are
1450 * 9 bits each for 6 counters).
1451 */
1452 li r3, -1 /* set all freeze bits */
1453 clrrdi r3, r3, 10
1454 mfspr r10, SPRN_MMCR2
1455 mtspr SPRN_MMCR2, r3
1456 isync
1457END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001458 li r3, 1
1459 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1460 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1461 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
Paul Mackerras89436332012-03-02 01:38:23 +00001462 mfspr r6, SPRN_MMCRA
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001463 /* Clear MMCRA in order to disable SDAR updates */
Paul Mackerras89436332012-03-02 01:38:23 +00001464 li r7, 0
1465 mtspr SPRN_MMCRA, r7
Paul Mackerrasde56a942011-06-29 00:21:34 +00001466 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001467 beq 21f /* if no VPA, save PMU stuff anyway */
1468 lbz r7, LPPACA_PMCINUSE(r8)
1469 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1470 bne 21f
1471 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1472 b 22f
147321: mfspr r5, SPRN_MMCR1
Paul Mackerras14941782013-09-06 13:11:18 +10001474 mfspr r7, SPRN_SIAR
1475 mfspr r8, SPRN_SDAR
Paul Mackerrasde56a942011-06-29 00:21:34 +00001476 std r4, VCPU_MMCR(r9)
1477 std r5, VCPU_MMCR + 8(r9)
1478 std r6, VCPU_MMCR + 16(r9)
Paul Mackerras9bc01a92014-05-26 19:48:40 +10001479BEGIN_FTR_SECTION
1480 std r10, VCPU_MMCR + 24(r9)
1481END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras14941782013-09-06 13:11:18 +10001482 std r7, VCPU_SIAR(r9)
1483 std r8, VCPU_SDAR(r9)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001484 mfspr r3, SPRN_PMC1
1485 mfspr r4, SPRN_PMC2
1486 mfspr r5, SPRN_PMC3
1487 mfspr r6, SPRN_PMC4
1488 mfspr r7, SPRN_PMC5
1489 mfspr r8, SPRN_PMC6
1490 stw r3, VCPU_PMC(r9)
1491 stw r4, VCPU_PMC + 4(r9)
1492 stw r5, VCPU_PMC + 8(r9)
1493 stw r6, VCPU_PMC + 12(r9)
1494 stw r7, VCPU_PMC + 16(r9)
1495 stw r8, VCPU_PMC + 20(r9)
Paul Mackerras9e368f22011-06-29 00:40:08 +00001496BEGIN_FTR_SECTION
Michael Neulingb005255e2014-01-08 21:25:21 +11001497 mfspr r5, SPRN_SIER
1498 mfspr r6, SPRN_SPMC1
1499 mfspr r7, SPRN_SPMC2
1500 mfspr r8, SPRN_MMCRS
Michael Neulingb005255e2014-01-08 21:25:21 +11001501 std r5, VCPU_SIER(r9)
1502 stw r6, VCPU_PMC + 24(r9)
1503 stw r7, VCPU_PMC + 28(r9)
1504 std r8, VCPU_MMCR + 32(r9)
1505 lis r4, 0x8000
1506 mtspr SPRN_MMCRS, r4
1507END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000150822:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001509 /* Clear out SLB */
1510 li r5,0
1511 slbmte r5,r5
1512 slbia
1513 ptesync
1514
Paul Mackerrasde56a942011-06-29 00:21:34 +00001515 /*
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001516 * POWER7/POWER8 guest -> host partition switch code.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001517 * We don't have to lock against tlbies but we do
1518 * have to coordinate the hardware threads.
1519 */
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001520kvmhv_switch_to_host:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001521 /* Secondary threads wait for primary to do partition switch */
Paul Mackerras6af27c82015-03-28 14:21:10 +11001522 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001523 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1524 lbz r3,HSTATE_PTID(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001525 cmpwi r3,0
1526 beq 15f
1527 HMT_LOW
152813: lbz r3,VCORE_IN_GUEST(r5)
1529 cmpwi r3,0
1530 bne 13b
1531 HMT_MEDIUM
1532 b 16f
1533
1534 /* Primary thread waits for all the secondaries to exit guest */
153515: lwz r3,VCORE_ENTRY_EXIT(r5)
1536 srwi r0,r3,8
1537 clrldi r3,r3,56
1538 cmpw r3,r0
1539 bne 15b
1540 isync
1541
1542 /* Primary thread switches back to host partition */
1543 ld r6,KVM_HOST_SDR1(r4)
1544 lwz r7,KVM_HOST_LPID(r4)
1545 li r8,LPID_RSVD /* switch to reserved LPID */
1546 mtspr SPRN_LPID,r8
1547 ptesync
1548 mtspr SPRN_SDR1,r6 /* switch to partition page table */
1549 mtspr SPRN_LPID,r7
1550 isync
1551
Michael Neulingb005255e2014-01-08 21:25:21 +11001552BEGIN_FTR_SECTION
1553 /* DPDES is shared between threads */
1554 mfspr r7, SPRN_DPDES
1555 std r7, VCORE_DPDES(r5)
1556 /* clear DPDES so we don't get guest doorbells in the host */
1557 li r8, 0
1558 mtspr SPRN_DPDES, r8
1559END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1560
Paul Mackerrasde56a942011-06-29 00:21:34 +00001561 /* Subtract timebase offset from timebase */
1562 ld r8,VCORE_TB_OFFSET(r5)
1563 cmpdi r8,0
1564 beq 17f
Paul Mackerrasc5fb80d2014-03-25 10:47:07 +11001565 mftb r6 /* current guest timebase */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001566 subf r8,r8,r6
1567 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1568 mftb r7 /* check if lower 24 bits overflowed */
1569 clrldi r6,r6,40
1570 clrldi r7,r7,40
1571 cmpld r7,r6
1572 bge 17f
1573 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1574 mtspr SPRN_TBU40,r8
1575
1576 /* Reset PCR */
157717: ld r0, VCORE_PCR(r5)
1578 cmpdi r0, 0
1579 beq 18f
1580 li r0, 0
1581 mtspr SPRN_PCR, r0
158218:
1583 /* Signal secondary CPUs to continue */
1584 stb r0,VCORE_IN_GUEST(r5)
1585 lis r8,0x7fff /* MAX_INT@h */
1586 mtspr SPRN_HDEC,r8
1587
158816: ld r8,KVM_HOST_LPCR(r4)
1589 mtspr SPRN_LPCR,r8
1590 isync
Paul Mackerrasde56a942011-06-29 00:21:34 +00001591
1592 /* load host SLB entries */
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11001593 ld r8,PACA_SLBSHADOWPTR(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001594
1595 .rept SLB_NUM_BOLTED
Alexander Graf0865a582014-06-11 10:36:17 +02001596 li r3, SLBSHADOW_SAVEAREA
1597 LDX_BE r5, r8, r3
1598 addi r3, r3, 8
1599 LDX_BE r6, r8, r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00001600 andis. r7,r5,SLB_ESID_V@h
1601 beq 1f
1602 slbmte r6,r5
16031: addi r8,r8,16
1604 .endr
1605
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001606#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1607 /* Finish timing, if we have a vcpu */
1608 ld r4, HSTATE_KVM_VCPU(r13)
1609 cmpdi r4, 0
1610 li r3, 0
1611 beq 2f
1612 bl kvmhv_accumulate_time
16132:
1614#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001615 /* Unset guest mode */
1616 li r0, KVM_GUEST_MODE_NONE
1617 stb r0, HSTATE_IN_GUEST(r13)
1618
Paul Mackerras218309b2013-09-06 13:23:44 +10001619 ld r0, 112+PPC_LR_STKOFF(r1)
1620 addi r1, r1, 112
1621 mtlr r0
1622 blr
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001623
Paul Mackerras697d3892011-12-12 12:36:37 +00001624/*
1625 * Check whether an HDSI is an HPTE not found fault or something else.
1626 * If it is an HPTE not found fault that is due to the guest accessing
1627 * a page that they have mapped but which we have paged out, then
1628 * we continue on with the guest exit path. In all other cases,
1629 * reflect the HDSI to the guest as a DSI.
1630 */
1631kvmppc_hdsi:
1632 mfspr r4, SPRN_HDAR
1633 mfspr r6, SPRN_HDSISR
Paul Mackerras4cf302b2011-12-12 12:38:51 +00001634 /* HPTE not found fault or protection fault? */
1635 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
Paul Mackerras697d3892011-12-12 12:36:37 +00001636 beq 1f /* if not, send it to the guest */
1637 andi. r0, r11, MSR_DR /* data relocation enabled? */
1638 beq 3f
1639 clrrdi r0, r4, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001640 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerras697d3892011-12-12 12:36:37 +00001641 bne 1f /* if no SLB entry found */
16424: std r4, VCPU_FAULT_DAR(r9)
1643 stw r6, VCPU_FAULT_DSISR(r9)
1644
1645 /* Search the hash table. */
1646 mr r3, r9 /* vcpu pointer */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001647 li r7, 1 /* data fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001648 bl kvmppc_hpte_hv_fault
Paul Mackerras697d3892011-12-12 12:36:37 +00001649 ld r9, HSTATE_KVM_VCPU(r13)
1650 ld r10, VCPU_PC(r9)
1651 ld r11, VCPU_MSR(r9)
1652 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1653 cmpdi r3, 0 /* retry the instruction */
1654 beq 6f
1655 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001656 beq guest_exit_cont
Paul Mackerras697d3892011-12-12 12:36:37 +00001657 cmpdi r3, -2 /* MMIO emulation; need instr word */
1658 beq 2f
1659
1660 /* Synthesize a DSI for the guest */
1661 ld r4, VCPU_FAULT_DAR(r9)
1662 mr r6, r3
16631: mtspr SPRN_DAR, r4
1664 mtspr SPRN_DSISR, r6
1665 mtspr SPRN_SRR0, r10
1666 mtspr SPRN_SRR1, r11
1667 li r10, BOOK3S_INTERRUPT_DATA_STORAGE
Michael Neulinge4e38122014-03-25 10:47:02 +11001668 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001669fast_interrupt_c_return:
Paul Mackerras697d3892011-12-12 12:36:37 +000016706: ld r7, VCPU_CTR(r9)
1671 lwz r8, VCPU_XER(r9)
1672 mtctr r7
1673 mtxer r8
1674 mr r4, r9
1675 b fast_guest_return
1676
16773: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1678 ld r5, KVM_VRMA_SLB_V(r5)
1679 b 4b
1680
1681 /* If this is for emulated MMIO, load the instruction word */
16822: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1683
1684 /* Set guest mode to 'jump over instruction' so if lwz faults
1685 * we'll just continue at the next IP. */
1686 li r0, KVM_GUEST_MODE_SKIP
1687 stb r0, HSTATE_IN_GUEST(r13)
1688
1689 /* Do the access with MSR:DR enabled */
1690 mfmsr r3
1691 ori r4, r3, MSR_DR /* Enable paging for data */
1692 mtmsrd r4
1693 lwz r8, 0(r10)
1694 mtmsrd r3
1695
1696 /* Store the result */
1697 stw r8, VCPU_LAST_INST(r9)
1698
1699 /* Unset guest mode. */
Paul Mackerras44a3add2013-10-04 21:45:04 +10001700 li r0, KVM_GUEST_MODE_HOST_HV
Paul Mackerras697d3892011-12-12 12:36:37 +00001701 stb r0, HSTATE_IN_GUEST(r13)
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001702 b guest_exit_cont
Paul Mackerrasde56a942011-06-29 00:21:34 +00001703
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001704/*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001705 * Similarly for an HISI, reflect it to the guest as an ISI unless
1706 * it is an HPTE not found fault for a page that we have paged out.
1707 */
1708kvmppc_hisi:
1709 andis. r0, r11, SRR1_ISI_NOPT@h
1710 beq 1f
1711 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1712 beq 3f
1713 clrrdi r0, r10, 28
Michael Neulingc75df6f2012-06-25 13:33:10 +00001714 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
Paul Mackerras342d3db2011-12-12 12:38:05 +00001715 bne 1f /* if no SLB entry found */
17164:
1717 /* Search the hash table. */
1718 mr r3, r9 /* vcpu pointer */
1719 mr r4, r10
1720 mr r6, r11
1721 li r7, 0 /* instruction fault */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11001722 bl kvmppc_hpte_hv_fault
Paul Mackerras342d3db2011-12-12 12:38:05 +00001723 ld r9, HSTATE_KVM_VCPU(r13)
1724 ld r10, VCPU_PC(r9)
1725 ld r11, VCPU_MSR(r9)
1726 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1727 cmpdi r3, 0 /* retry the instruction */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001728 beq fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001729 cmpdi r3, -1 /* handle in kernel mode */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001730 beq guest_exit_cont
Paul Mackerras342d3db2011-12-12 12:38:05 +00001731
1732 /* Synthesize an ISI for the guest */
1733 mr r11, r3
17341: mtspr SPRN_SRR0, r10
1735 mtspr SPRN_SRR1, r11
1736 li r10, BOOK3S_INTERRUPT_INST_STORAGE
Michael Neulinge4e38122014-03-25 10:47:02 +11001737 bl kvmppc_msr_interrupt
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001738 b fast_interrupt_c_return
Paul Mackerras342d3db2011-12-12 12:38:05 +00001739
17403: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1741 ld r5, KVM_VRMA_SLB_V(r6)
1742 b 4b
1743
1744/*
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001745 * Try to handle an hcall in real mode.
1746 * Returns to the guest if we handle it, or continues on up to
1747 * the kernel if we can't (i.e. if we don't have a handler for
1748 * it, or if the handler returns H_TOO_HARD).
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11001749 *
1750 * r5 - r8 contain hcall args,
1751 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001752 */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001753hcall_try_real_mode:
Michael Neulingc75df6f2012-06-25 13:33:10 +00001754 ld r3,VCPU_GPR(R3)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001755 andi. r0,r11,MSR_PR
Liu Ping Fan27025a62013-11-19 14:12:48 +08001756 /* sc 1 from userspace - reflect to guest syscall */
1757 bne sc_1_fast_return
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001758 clrrdi r3,r3,2
1759 cmpldi r3,hcall_real_table_end - hcall_real_table
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001760 bge guest_exit_cont
Paul Mackerras699a0ea2014-06-02 11:02:59 +10001761 /* See if this hcall is enabled for in-kernel handling */
1762 ld r4, VCPU_KVM(r9)
1763 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
1764 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
1765 add r4, r4, r0
1766 ld r0, KVM_ENABLED_HCALLS(r4)
1767 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
1768 srd r0, r0, r4
1769 andi. r0, r0, 1
1770 beq guest_exit_cont
1771 /* Get pointer to handler, if any, and call it */
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001772 LOAD_REG_ADDR(r4, hcall_real_table)
Paul Mackerras4baa1d82013-07-08 20:09:53 +10001773 lwax r3,r3,r4
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001774 cmpwi r3,0
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001775 beq guest_exit_cont
Anton Blanchard05a308c2014-06-12 18:16:10 +10001776 add r12,r3,r4
1777 mtctr r12
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001778 mr r3,r9 /* get vcpu pointer */
Michael Neulingc75df6f2012-06-25 13:33:10 +00001779 ld r4,VCPU_GPR(R4)(r9)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001780 bctrl
1781 cmpdi r3,H_TOO_HARD
1782 beq hcall_real_fallback
1783 ld r4,HSTATE_KVM_VCPU(r13)
Michael Neulingc75df6f2012-06-25 13:33:10 +00001784 std r3,VCPU_GPR(R3)(r4)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001785 ld r10,VCPU_PC(r4)
1786 ld r11,VCPU_MSR(r4)
1787 b fast_guest_return
1788
Liu Ping Fan27025a62013-11-19 14:12:48 +08001789sc_1_fast_return:
1790 mtspr SPRN_SRR0,r10
1791 mtspr SPRN_SRR1,r11
1792 li r10, BOOK3S_INTERRUPT_SYSCALL
Michael Neulinge4e38122014-03-25 10:47:02 +11001793 bl kvmppc_msr_interrupt
Liu Ping Fan27025a62013-11-19 14:12:48 +08001794 mr r4,r9
1795 b fast_guest_return
1796
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001797 /* We've attempted a real mode hcall, but it's punted it back
1798 * to userspace. We need to restore some clobbered volatiles
1799 * before resuming the pass-it-to-qemu path */
1800hcall_real_fallback:
1801 li r12,BOOK3S_INTERRUPT_SYSCALL
1802 ld r9, HSTATE_KVM_VCPU(r13)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001803
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001804 b guest_exit_cont
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001805
1806 .globl hcall_real_table
1807hcall_real_table:
1808 .long 0 /* 0 - unused */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001809 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
1810 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
1811 .long DOTSYM(kvmppc_h_read) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001812 .long 0 /* 0x10 - H_CLEAR_MOD */
1813 .long 0 /* 0x14 - H_CLEAR_REF */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001814 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
1815 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
1816 .long DOTSYM(kvmppc_h_put_tce) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001817 .long 0 /* 0x24 - H_SET_SPRG0 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001818 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001819 .long 0 /* 0x2c */
1820 .long 0 /* 0x30 */
1821 .long 0 /* 0x34 */
1822 .long 0 /* 0x38 */
1823 .long 0 /* 0x3c */
1824 .long 0 /* 0x40 */
1825 .long 0 /* 0x44 */
1826 .long 0 /* 0x48 */
1827 .long 0 /* 0x4c */
1828 .long 0 /* 0x50 */
1829 .long 0 /* 0x54 */
1830 .long 0 /* 0x58 */
1831 .long 0 /* 0x5c */
1832 .long 0 /* 0x60 */
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001833#ifdef CONFIG_KVM_XICS
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001834 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
1835 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
1836 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001837 .long 0 /* 0x70 - H_IPOLL */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001838 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
Benjamin Herrenschmidte7d26f22013-04-17 20:31:15 +00001839#else
1840 .long 0 /* 0x64 - H_EOI */
1841 .long 0 /* 0x68 - H_CPPR */
1842 .long 0 /* 0x6c - H_IPI */
1843 .long 0 /* 0x70 - H_IPOLL */
1844 .long 0 /* 0x74 - H_XIRR */
1845#endif
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001846 .long 0 /* 0x78 */
1847 .long 0 /* 0x7c */
1848 .long 0 /* 0x80 */
1849 .long 0 /* 0x84 */
1850 .long 0 /* 0x88 */
1851 .long 0 /* 0x8c */
1852 .long 0 /* 0x90 */
1853 .long 0 /* 0x94 */
1854 .long 0 /* 0x98 */
1855 .long 0 /* 0x9c */
1856 .long 0 /* 0xa0 */
1857 .long 0 /* 0xa4 */
1858 .long 0 /* 0xa8 */
1859 .long 0 /* 0xac */
1860 .long 0 /* 0xb0 */
1861 .long 0 /* 0xb4 */
1862 .long 0 /* 0xb8 */
1863 .long 0 /* 0xbc */
1864 .long 0 /* 0xc0 */
1865 .long 0 /* 0xc4 */
1866 .long 0 /* 0xc8 */
1867 .long 0 /* 0xcc */
1868 .long 0 /* 0xd0 */
1869 .long 0 /* 0xd4 */
1870 .long 0 /* 0xd8 */
1871 .long 0 /* 0xdc */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001872 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001873 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001874 .long 0 /* 0xe8 */
1875 .long 0 /* 0xec */
1876 .long 0 /* 0xf0 */
1877 .long 0 /* 0xf4 */
1878 .long 0 /* 0xf8 */
1879 .long 0 /* 0xfc */
1880 .long 0 /* 0x100 */
1881 .long 0 /* 0x104 */
1882 .long 0 /* 0x108 */
1883 .long 0 /* 0x10c */
1884 .long 0 /* 0x110 */
1885 .long 0 /* 0x114 */
1886 .long 0 /* 0x118 */
1887 .long 0 /* 0x11c */
1888 .long 0 /* 0x120 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001889 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
Paul Mackerras8563bf52014-01-08 21:25:29 +11001890 .long 0 /* 0x128 */
1891 .long 0 /* 0x12c */
1892 .long 0 /* 0x130 */
Anton Blanchardc1fb0192014-02-04 16:07:01 +11001893 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
Michael Ellermane928e9c2015-03-20 20:39:41 +11001894 .long 0 /* 0x138 */
1895 .long 0 /* 0x13c */
1896 .long 0 /* 0x140 */
1897 .long 0 /* 0x144 */
1898 .long 0 /* 0x148 */
1899 .long 0 /* 0x14c */
1900 .long 0 /* 0x150 */
1901 .long 0 /* 0x154 */
1902 .long 0 /* 0x158 */
1903 .long 0 /* 0x15c */
1904 .long 0 /* 0x160 */
1905 .long 0 /* 0x164 */
1906 .long 0 /* 0x168 */
1907 .long 0 /* 0x16c */
1908 .long 0 /* 0x170 */
1909 .long 0 /* 0x174 */
1910 .long 0 /* 0x178 */
1911 .long 0 /* 0x17c */
1912 .long 0 /* 0x180 */
1913 .long 0 /* 0x184 */
1914 .long 0 /* 0x188 */
1915 .long 0 /* 0x18c */
1916 .long 0 /* 0x190 */
1917 .long 0 /* 0x194 */
1918 .long 0 /* 0x198 */
1919 .long 0 /* 0x19c */
1920 .long 0 /* 0x1a0 */
1921 .long 0 /* 0x1a4 */
1922 .long 0 /* 0x1a8 */
1923 .long 0 /* 0x1ac */
1924 .long 0 /* 0x1b0 */
1925 .long 0 /* 0x1b4 */
1926 .long 0 /* 0x1b8 */
1927 .long 0 /* 0x1bc */
1928 .long 0 /* 0x1c0 */
1929 .long 0 /* 0x1c4 */
1930 .long 0 /* 0x1c8 */
1931 .long 0 /* 0x1cc */
1932 .long 0 /* 0x1d0 */
1933 .long 0 /* 0x1d4 */
1934 .long 0 /* 0x1d8 */
1935 .long 0 /* 0x1dc */
1936 .long 0 /* 0x1e0 */
1937 .long 0 /* 0x1e4 */
1938 .long 0 /* 0x1e8 */
1939 .long 0 /* 0x1ec */
1940 .long 0 /* 0x1f0 */
1941 .long 0 /* 0x1f4 */
1942 .long 0 /* 0x1f8 */
1943 .long 0 /* 0x1fc */
1944 .long 0 /* 0x200 */
1945 .long 0 /* 0x204 */
1946 .long 0 /* 0x208 */
1947 .long 0 /* 0x20c */
1948 .long 0 /* 0x210 */
1949 .long 0 /* 0x214 */
1950 .long 0 /* 0x218 */
1951 .long 0 /* 0x21c */
1952 .long 0 /* 0x220 */
1953 .long 0 /* 0x224 */
1954 .long 0 /* 0x228 */
1955 .long 0 /* 0x22c */
1956 .long 0 /* 0x230 */
1957 .long 0 /* 0x234 */
1958 .long 0 /* 0x238 */
1959 .long 0 /* 0x23c */
1960 .long 0 /* 0x240 */
1961 .long 0 /* 0x244 */
1962 .long 0 /* 0x248 */
1963 .long 0 /* 0x24c */
1964 .long 0 /* 0x250 */
1965 .long 0 /* 0x254 */
1966 .long 0 /* 0x258 */
1967 .long 0 /* 0x25c */
1968 .long 0 /* 0x260 */
1969 .long 0 /* 0x264 */
1970 .long 0 /* 0x268 */
1971 .long 0 /* 0x26c */
1972 .long 0 /* 0x270 */
1973 .long 0 /* 0x274 */
1974 .long 0 /* 0x278 */
1975 .long 0 /* 0x27c */
1976 .long 0 /* 0x280 */
1977 .long 0 /* 0x284 */
1978 .long 0 /* 0x288 */
1979 .long 0 /* 0x28c */
1980 .long 0 /* 0x290 */
1981 .long 0 /* 0x294 */
1982 .long 0 /* 0x298 */
1983 .long 0 /* 0x29c */
1984 .long 0 /* 0x2a0 */
1985 .long 0 /* 0x2a4 */
1986 .long 0 /* 0x2a8 */
1987 .long 0 /* 0x2ac */
1988 .long 0 /* 0x2b0 */
1989 .long 0 /* 0x2b4 */
1990 .long 0 /* 0x2b8 */
1991 .long 0 /* 0x2bc */
1992 .long 0 /* 0x2c0 */
1993 .long 0 /* 0x2c4 */
1994 .long 0 /* 0x2c8 */
1995 .long 0 /* 0x2cc */
1996 .long 0 /* 0x2d0 */
1997 .long 0 /* 0x2d4 */
1998 .long 0 /* 0x2d8 */
1999 .long 0 /* 0x2dc */
2000 .long 0 /* 0x2e0 */
2001 .long 0 /* 0x2e4 */
2002 .long 0 /* 0x2e8 */
2003 .long 0 /* 0x2ec */
2004 .long 0 /* 0x2f0 */
2005 .long 0 /* 0x2f4 */
2006 .long 0 /* 0x2f8 */
2007 .long 0 /* 0x2fc */
2008 .long DOTSYM(kvmppc_h_random) - hcall_real_table
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002009 .globl hcall_real_table_end
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002010hcall_real_table_end:
2011
Paul Mackerras8563bf52014-01-08 21:25:29 +11002012_GLOBAL(kvmppc_h_set_xdabr)
2013 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2014 beq 6f
2015 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2016 andc. r0, r5, r0
2017 beq 3f
20186: li r3, H_PARAMETER
2019 blr
2020
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002021_GLOBAL(kvmppc_h_set_dabr)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002022 li r5, DABRX_USER | DABRX_KERNEL
20233:
Michael Neulingeee7ff92014-01-08 21:25:19 +11002024BEGIN_FTR_SECTION
2025 b 2f
2026END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002027 std r4,VCPU_DABR(r3)
Paul Mackerras8563bf52014-01-08 21:25:29 +11002028 stw r5, VCPU_DABRX(r3)
2029 mtspr SPRN_DABRX, r5
Paul Mackerras89436332012-03-02 01:38:23 +00002030 /* Work around P7 bug where DABR can get corrupted on mtspr */
20311: mtspr SPRN_DABR,r4
2032 mfspr r5, SPRN_DABR
2033 cmpd r4, r5
2034 bne 1b
2035 isync
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002036 li r3,0
2037 blr
2038
Paul Mackerras8563bf52014-01-08 21:25:29 +11002039 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
20402: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
2041 rlwimi r5, r4, 1, DAWRX_WT
2042 clrrdi r4, r4, 3
2043 std r4, VCPU_DAWR(r3)
2044 std r5, VCPU_DAWRX(r3)
2045 mtspr SPRN_DAWR, r4
2046 mtspr SPRN_DAWRX, r5
2047 li r3, 0
Paul Mackerrasde56a942011-06-29 00:21:34 +00002048 blr
2049
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002050_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002051 ori r11,r11,MSR_EE
2052 std r11,VCPU_MSR(r3)
2053 li r0,1
2054 stb r0,VCPU_CEDED(r3)
2055 sync /* order setting ceded vs. testing prodded */
2056 lbz r5,VCPU_PRODDED(r3)
2057 cmpwi r5,0
Paul Mackerras04f995a2012-08-06 00:03:28 +00002058 bne kvm_cede_prodded
Paul Mackerras6af27c82015-03-28 14:21:10 +11002059 li r12,0 /* set trap to 0 to say hcall is handled */
2060 stw r12,VCPU_TRAP(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002061 li r0,H_SUCCESS
Michael Neulingc75df6f2012-06-25 13:33:10 +00002062 std r0,VCPU_GPR(R3)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002063
2064 /*
2065 * Set our bit in the bitmask of napping threads unless all the
2066 * other threads are already napping, in which case we send this
2067 * up to the host.
2068 */
2069 ld r5,HSTATE_KVM_VCORE(r13)
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002070 lbz r6,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002071 lwz r8,VCORE_ENTRY_EXIT(r5)
2072 clrldi r8,r8,56
2073 li r0,1
2074 sld r0,r0,r6
2075 addi r6,r5,VCORE_NAPPING_THREADS
207631: lwarx r4,0,r6
2077 or r4,r4,r0
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002078 cmpw r4,r8
2079 beq kvm_cede_exit
Paul Mackerras19ccb762011-07-23 17:42:46 +10002080 stwcx. r4,0,r6
2081 bne 31b
Paul Mackerras7d6c40d2015-03-28 14:21:09 +11002082 /* order napping_threads update vs testing entry_exit_map */
Paul Mackerrasf019b7a2013-11-16 17:46:03 +11002083 isync
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002084 li r0,NAPPING_CEDE
Paul Mackerras19ccb762011-07-23 17:42:46 +10002085 stb r0,HSTATE_NAPPING(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002086 lwz r7,VCORE_ENTRY_EXIT(r5)
2087 cmpwi r7,0x100
2088 bge 33f /* another thread already exiting */
2089
2090/*
2091 * Although not specifically required by the architecture, POWER7
2092 * preserves the following registers in nap mode, even if an SMT mode
2093 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2094 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2095 */
2096 /* Save non-volatile GPRs */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002097 std r14, VCPU_GPR(R14)(r3)
2098 std r15, VCPU_GPR(R15)(r3)
2099 std r16, VCPU_GPR(R16)(r3)
2100 std r17, VCPU_GPR(R17)(r3)
2101 std r18, VCPU_GPR(R18)(r3)
2102 std r19, VCPU_GPR(R19)(r3)
2103 std r20, VCPU_GPR(R20)(r3)
2104 std r21, VCPU_GPR(R21)(r3)
2105 std r22, VCPU_GPR(R22)(r3)
2106 std r23, VCPU_GPR(R23)(r3)
2107 std r24, VCPU_GPR(R24)(r3)
2108 std r25, VCPU_GPR(R25)(r3)
2109 std r26, VCPU_GPR(R26)(r3)
2110 std r27, VCPU_GPR(R27)(r3)
2111 std r28, VCPU_GPR(R28)(r3)
2112 std r29, VCPU_GPR(R29)(r3)
2113 std r30, VCPU_GPR(R30)(r3)
2114 std r31, VCPU_GPR(R31)(r3)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002115
2116 /* save FP state */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002117 bl kvmppc_save_fp
Paul Mackerras19ccb762011-07-23 17:42:46 +10002118
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002119 /*
2120 * Set DEC to the smaller of DEC and HDEC, so that we wake
2121 * no later than the end of our timeslice (HDEC interrupts
2122 * don't wake us from nap).
2123 */
2124 mfspr r3, SPRN_DEC
2125 mfspr r4, SPRN_HDEC
2126 mftb r5
2127 cmpw r3, r4
2128 ble 67f
2129 mtspr SPRN_DEC, r4
213067:
2131 /* save expiry time of guest decrementer */
2132 extsw r3, r3
2133 add r3, r3, r5
2134 ld r4, HSTATE_KVM_VCPU(r13)
2135 ld r5, HSTATE_KVM_VCORE(r13)
2136 ld r6, VCORE_TB_OFFSET(r5)
2137 subf r3, r6, r3 /* convert to host TB value */
2138 std r3, VCPU_DEC_EXPIRES(r4)
2139
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002140#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2141 ld r4, HSTATE_KVM_VCPU(r13)
2142 addi r3, r4, VCPU_TB_CEDE
2143 bl kvmhv_accumulate_time
2144#endif
2145
Paul Mackerrasccc07772015-03-28 14:21:07 +11002146 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2147
Paul Mackerras19ccb762011-07-23 17:42:46 +10002148 /*
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002149 * Take a nap until a decrementer or external or doobell interrupt
Paul Mackerrasccc07772015-03-28 14:21:07 +11002150 * occurs, with PECE1 and PECE0 set in LPCR.
2151 * On POWER8, if we are ceding, also set PECEDP.
2152 * Also clear the runlatch bit before napping.
Paul Mackerras19ccb762011-07-23 17:42:46 +10002153 */
Paul Mackerras56548fc2014-12-03 14:48:40 +11002154kvm_do_nap:
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002155 mfspr r0, SPRN_CTRLF
2156 clrrdi r0, r0, 1
2157 mtspr SPRN_CTRLT, r0
Preeti U Murthy582b9102014-04-11 16:02:08 +05302158
Paul Mackerrasf0888f72012-02-03 00:54:17 +00002159 li r0,1
2160 stb r0,HSTATE_HWTHREAD_REQ(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002161 mfspr r5,SPRN_LPCR
2162 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002163BEGIN_FTR_SECTION
Paul Mackerrasccc07772015-03-28 14:21:07 +11002164 rlwimi r5, r3, 0, LPCR_PECEDP
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002165END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002166 mtspr SPRN_LPCR,r5
2167 isync
2168 li r0, 0
2169 std r0, HSTATE_SCRATCH0(r13)
2170 ptesync
2171 ld r0, HSTATE_SCRATCH0(r13)
21721: cmpd r0, r0
2173 bne 1b
2174 nap
2175 b .
2176
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100217733: mr r4, r3
2178 li r3, 0
2179 li r12, 0
2180 b 34f
2181
Paul Mackerras19ccb762011-07-23 17:42:46 +10002182kvm_end_cede:
Paul Mackerras4619ac82013-04-17 20:31:41 +00002183 /* get vcpu pointer */
2184 ld r4, HSTATE_KVM_VCPU(r13)
2185
Paul Mackerras19ccb762011-07-23 17:42:46 +10002186 /* Woken by external or decrementer interrupt */
2187 ld r1, HSTATE_HOST_R1(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002188
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002189#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2190 addi r3, r4, VCPU_TB_RMINTR
2191 bl kvmhv_accumulate_time
2192#endif
2193
Paul Mackerras19ccb762011-07-23 17:42:46 +10002194 /* load up FP state */
2195 bl kvmppc_load_fp
2196
Paul Mackerrasfd6d53b2015-03-28 14:21:08 +11002197 /* Restore guest decrementer */
2198 ld r3, VCPU_DEC_EXPIRES(r4)
2199 ld r5, HSTATE_KVM_VCORE(r13)
2200 ld r6, VCORE_TB_OFFSET(r5)
2201 add r3, r3, r6 /* convert host TB to guest TB value */
2202 mftb r7
2203 subf r3, r7, r3
2204 mtspr SPRN_DEC, r3
2205
Paul Mackerras19ccb762011-07-23 17:42:46 +10002206 /* Load NV GPRS */
Michael Neulingc75df6f2012-06-25 13:33:10 +00002207 ld r14, VCPU_GPR(R14)(r4)
2208 ld r15, VCPU_GPR(R15)(r4)
2209 ld r16, VCPU_GPR(R16)(r4)
2210 ld r17, VCPU_GPR(R17)(r4)
2211 ld r18, VCPU_GPR(R18)(r4)
2212 ld r19, VCPU_GPR(R19)(r4)
2213 ld r20, VCPU_GPR(R20)(r4)
2214 ld r21, VCPU_GPR(R21)(r4)
2215 ld r22, VCPU_GPR(R22)(r4)
2216 ld r23, VCPU_GPR(R23)(r4)
2217 ld r24, VCPU_GPR(R24)(r4)
2218 ld r25, VCPU_GPR(R25)(r4)
2219 ld r26, VCPU_GPR(R26)(r4)
2220 ld r27, VCPU_GPR(R27)(r4)
2221 ld r28, VCPU_GPR(R28)(r4)
2222 ld r29, VCPU_GPR(R29)(r4)
2223 ld r30, VCPU_GPR(R30)(r4)
2224 ld r31, VCPU_GPR(R31)(r4)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002225
2226 /* Check the wake reason in SRR1 to see why we got here */
2227 bl kvmppc_check_wake_reason
Paul Mackerras19ccb762011-07-23 17:42:46 +10002228
2229 /* clear our bit in vcore->napping_threads */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +1100223034: ld r5,HSTATE_KVM_VCORE(r13)
2231 lbz r7,HSTATE_PTID(r13)
Paul Mackerras19ccb762011-07-23 17:42:46 +10002232 li r0,1
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002233 sld r0,r0,r7
Paul Mackerras19ccb762011-07-23 17:42:46 +10002234 addi r6,r5,VCORE_NAPPING_THREADS
223532: lwarx r7,0,r6
2236 andc r7,r7,r0
2237 stwcx. r7,0,r6
2238 bne 32b
2239 li r0,0
2240 stb r0,HSTATE_NAPPING(r13)
2241
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002242 /* See if the wake reason means we need to exit */
2243 stw r12, VCPU_TRAP(r4)
Paul Mackerras4619ac82013-04-17 20:31:41 +00002244 mr r9, r4
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002245 cmpdi r3, 0
2246 bgt guest_exit_cont
Paul Mackerras4619ac82013-04-17 20:31:41 +00002247
Paul Mackerras19ccb762011-07-23 17:42:46 +10002248 /* see if any other thread is already exiting */
2249 lwz r0,VCORE_ENTRY_EXIT(r5)
2250 cmpwi r0,0x100
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002251 bge guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002252
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002253 b kvmppc_cede_reentry /* if not go back to guest */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002254
2255 /* cede when already previously prodded case */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002256kvm_cede_prodded:
2257 li r0,0
Paul Mackerras19ccb762011-07-23 17:42:46 +10002258 stb r0,VCPU_PRODDED(r3)
2259 sync /* order testing prodded vs. clearing ceded */
2260 stb r0,VCPU_CEDED(r3)
2261 li r3,H_SUCCESS
2262 blr
2263
2264 /* we've ceded but we want to give control to the host */
Paul Mackerras04f995a2012-08-06 00:03:28 +00002265kvm_cede_exit:
Paul Mackerras6af27c82015-03-28 14:21:10 +11002266 ld r9, HSTATE_KVM_VCPU(r13)
2267 b guest_exit_cont
Paul Mackerras19ccb762011-07-23 17:42:46 +10002268
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002269 /* Try to handle a machine check in real mode */
2270machine_check_realmode:
2271 mr r3, r9 /* get vcpu pointer */
Anton Blanchardb1576fe2014-02-04 16:04:35 +11002272 bl kvmppc_realmode_machine_check
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002273 nop
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302274 cmpdi r3, 0 /* Did we handle MCE ? */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002275 ld r9, HSTATE_KVM_VCPU(r13)
2276 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +05302277 /*
2278 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2279 * machine check interrupt (set HSRR0 to 0x200). And for handled
2280 * errors (no-fatal), just go back to guest execution with current
2281 * HSRR0 instead of exiting guest. This new approach will inject
2282 * machine check to guest for fatal error causing guest to crash.
2283 *
2284 * The old code used to return to host for unhandled errors which
2285 * was causing guest to hang with soft lockups inside guest and
2286 * makes it difficult to recover guest instance.
2287 */
2288 ld r10, VCPU_PC(r9)
2289 ld r11, VCPU_MSR(r9)
2290 bne 2f /* Continue guest execution. */
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002291 /* If not, deliver a machine check. SRR0/1 are already set */
2292 li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
Paul Mackerras000a25d2014-05-26 19:48:41 +10002293 ld r11, VCPU_MSR(r9)
Michael Neulinge4e38122014-03-25 10:47:02 +11002294 bl kvmppc_msr_interrupt
Mahesh Salgaonkar74845bc2014-06-11 14:18:21 +053022952: b fast_interrupt_c_return
Paul Mackerrasb4072df2012-11-23 22:37:50 +00002296
Paul Mackerrasde56a942011-06-29 00:21:34 +00002297/*
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002298 * Check the reason we woke from nap, and take appropriate action.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002299 * Returns (in r3):
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002300 * 0 if nothing needs to be done
2301 * 1 if something happened that needs to be handled by the host
2302 * -1 if there was a guest wakeup (IPI)
2303 *
2304 * Also sets r12 to the interrupt vector for any interrupt that needs
2305 * to be handled now by the host (0x500 for external interrupt), or zero.
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002306 * Modifies r0, r6, r7, r8.
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002307 */
2308kvmppc_check_wake_reason:
2309 mfspr r6, SPRN_SRR1
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002310BEGIN_FTR_SECTION
2311 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2312FTR_SECTION_ELSE
2313 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2314ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2315 cmpwi r6, 8 /* was it an external interrupt? */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002316 li r12, BOOK3S_INTERRUPT_EXTERNAL
2317 beq kvmppc_read_intr /* if so, see what it was */
2318 li r3, 0
2319 li r12, 0
2320 cmpwi r6, 6 /* was it the decrementer? */
2321 beq 0f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002322BEGIN_FTR_SECTION
2323 cmpwi r6, 5 /* privileged doorbell? */
2324 beq 0f
Paul Mackerras5d00f662014-01-08 21:25:28 +11002325 cmpwi r6, 3 /* hypervisor doorbell? */
2326 beq 3f
Paul Mackerrasaa31e842014-01-08 21:25:26 +11002327END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002328 li r3, 1 /* anything else, return 1 */
23290: blr
2330
Paul Mackerras5d00f662014-01-08 21:25:28 +11002331 /* hypervisor doorbell */
23323: li r12, BOOK3S_INTERRUPT_H_DOORBELL
2333 li r3, 1
2334 blr
2335
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002336/*
Paul Mackerrasc9342432013-09-06 13:24:13 +10002337 * Determine what sort of external interrupt is pending (if any).
2338 * Returns:
2339 * 0 if no interrupt is pending
2340 * 1 if an interrupt is pending that needs to be handled by the host
2341 * -1 if there was a guest wakeup IPI (which has now been cleared)
Paul Mackerras1f09c3e2015-03-28 14:21:04 +11002342 * Modifies r0, r6, r7, r8, returns value in r3.
Paul Mackerrasc9342432013-09-06 13:24:13 +10002343 */
2344kvmppc_read_intr:
2345 /* see if a host IPI is pending */
2346 li r3, 1
2347 lbz r0, HSTATE_HOST_IPI(r13)
2348 cmpwi r0, 0
2349 bne 1f
Paul Mackerrasde56a942011-06-29 00:21:34 +00002350
Paul Mackerrasc9342432013-09-06 13:24:13 +10002351 /* Now read the interrupt from the ICP */
2352 ld r6, HSTATE_XICS_PHYS(r13)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002353 li r7, XICS_XIRR
Paul Mackerrasc9342432013-09-06 13:24:13 +10002354 cmpdi r6, 0
2355 beq- 1f
2356 lwzcix r0, r6, r7
Alexander Graf76d072f2014-06-11 10:37:52 +02002357 /*
2358 * Save XIRR for later. Since we get in in reverse endian on LE
2359 * systems, save it byte reversed and fetch it back in host endian.
2360 */
2361 li r3, HSTATE_SAVED_XIRR
2362 STWX_BE r0, r3, r13
2363#ifdef __LITTLE_ENDIAN__
2364 lwz r3, HSTATE_SAVED_XIRR(r13)
2365#else
2366 mr r3, r0
2367#endif
2368 rlwinm. r3, r3, 0, 0xffffff
Paul Mackerrasde56a942011-06-29 00:21:34 +00002369 sync
Paul Mackerrasc9342432013-09-06 13:24:13 +10002370 beq 1f /* if nothing pending in the ICP */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002371
Paul Mackerrasc9342432013-09-06 13:24:13 +10002372 /* We found something in the ICP...
2373 *
2374 * If it's not an IPI, stash it in the PACA and return to
2375 * the host, we don't (yet) handle directing real external
2376 * interrupts directly to the guest
2377 */
2378 cmpwi r3, XICS_IPI /* if there is, is it an IPI? */
Paul Mackerrasc9342432013-09-06 13:24:13 +10002379 bne 42f
Paul Mackerrasde56a942011-06-29 00:21:34 +00002380
Paul Mackerrasc9342432013-09-06 13:24:13 +10002381 /* It's an IPI, clear the MFRR and EOI it */
2382 li r3, 0xff
2383 li r8, XICS_MFRR
2384 stbcix r3, r6, r8 /* clear the IPI */
2385 stwcix r0, r6, r7 /* EOI it */
2386 sync
Paul Mackerrasde56a942011-06-29 00:21:34 +00002387
Paul Mackerrasc9342432013-09-06 13:24:13 +10002388 /* We need to re-check host IPI now in case it got set in the
2389 * meantime. If it's clear, we bounce the interrupt to the
2390 * guest
2391 */
2392 lbz r0, HSTATE_HOST_IPI(r13)
2393 cmpwi r0, 0
2394 bne- 43f
2395
2396 /* OK, it's an IPI for us */
Paul Mackerras6af27c82015-03-28 14:21:10 +11002397 li r12, 0
Paul Mackerrasc9342432013-09-06 13:24:13 +10002398 li r3, -1
23991: blr
2400
Alexander Graf76d072f2014-06-11 10:37:52 +0200240142: /* It's not an IPI and it's for the host. We saved a copy of XIRR in
2402 * the PACA earlier, it will be picked up by the host ICP driver
Paul Mackerrasc9342432013-09-06 13:24:13 +10002403 */
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002404 li r3, 1
Paul Mackerrasc9342432013-09-06 13:24:13 +10002405 b 1b
2406
240743: /* We raced with the host, we need to resend that IPI, bummer */
2408 li r0, IPI_PRIORITY
2409 stbcix r0, r6, r8 /* set the IPI */
2410 sync
Paul Mackerrase3bbbbf2014-01-08 21:25:25 +11002411 li r3, 1
Paul Mackerrasc9342432013-09-06 13:24:13 +10002412 b 1b
Paul Mackerrasde56a942011-06-29 00:21:34 +00002413
2414/*
2415 * Save away FP, VMX and VSX registers.
2416 * r3 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002417 * N.B. r30 and r31 are volatile across this function,
2418 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002419 */
Paul Mackerras595e4f72013-10-15 20:43:04 +11002420kvmppc_save_fp:
2421 mflr r30
2422 mr r31,r3
Paul Mackerrasde56a942011-06-29 00:21:34 +00002423 mfmsr r5
2424 ori r8,r5,MSR_FP
2425#ifdef CONFIG_ALTIVEC
2426BEGIN_FTR_SECTION
2427 oris r8,r8,MSR_VEC@h
2428END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2429#endif
2430#ifdef CONFIG_VSX
2431BEGIN_FTR_SECTION
2432 oris r8,r8,MSR_VSX@h
2433END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2434#endif
2435 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002436 addi r3,r3,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002437 bl store_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002438#ifdef CONFIG_ALTIVEC
2439BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002440 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002441 bl store_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002442END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2443#endif
2444 mfspr r6,SPRN_VRSAVE
Paul Mackerrase724f082014-03-13 20:02:48 +11002445 stw r6,VCPU_VRSAVE(r31)
Paul Mackerras595e4f72013-10-15 20:43:04 +11002446 mtlr r30
Paul Mackerrasde56a942011-06-29 00:21:34 +00002447 blr
2448
2449/*
2450 * Load up FP, VMX and VSX registers
2451 * r4 = vcpu pointer
Paul Mackerras595e4f72013-10-15 20:43:04 +11002452 * N.B. r30 and r31 are volatile across this function,
2453 * thus it is not callable from C.
Paul Mackerrasde56a942011-06-29 00:21:34 +00002454 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00002455kvmppc_load_fp:
Paul Mackerras595e4f72013-10-15 20:43:04 +11002456 mflr r30
2457 mr r31,r4
Paul Mackerrasde56a942011-06-29 00:21:34 +00002458 mfmsr r9
2459 ori r8,r9,MSR_FP
2460#ifdef CONFIG_ALTIVEC
2461BEGIN_FTR_SECTION
2462 oris r8,r8,MSR_VEC@h
2463END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2464#endif
2465#ifdef CONFIG_VSX
2466BEGIN_FTR_SECTION
2467 oris r8,r8,MSR_VSX@h
2468END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2469#endif
2470 mtmsrd r8
Paul Mackerras595e4f72013-10-15 20:43:04 +11002471 addi r3,r4,VCPU_FPRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002472 bl load_fp_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002473#ifdef CONFIG_ALTIVEC
2474BEGIN_FTR_SECTION
Paul Mackerras595e4f72013-10-15 20:43:04 +11002475 addi r3,r31,VCPU_VRS
Alexander Graf9bf163f2014-06-16 14:41:15 +02002476 bl load_vr_state
Paul Mackerrasde56a942011-06-29 00:21:34 +00002477END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2478#endif
Paul Mackerrase724f082014-03-13 20:02:48 +11002479 lwz r7,VCPU_VRSAVE(r31)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002480 mtspr SPRN_VRSAVE,r7
Paul Mackerras595e4f72013-10-15 20:43:04 +11002481 mtlr r30
2482 mr r4,r31
Paul Mackerrasde56a942011-06-29 00:21:34 +00002483 blr
Paul Mackerras44a3add2013-10-04 21:45:04 +10002484
2485/*
2486 * We come here if we get any exception or interrupt while we are
2487 * executing host real mode code while in guest MMU context.
2488 * For now just spin, but we should do something better.
2489 */
2490kvmppc_bad_host_intr:
2491 b .
Michael Neulinge4e38122014-03-25 10:47:02 +11002492
2493/*
2494 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
2495 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
2496 * r11 has the guest MSR value (in/out)
2497 * r9 has a vcpu pointer (in)
2498 * r0 is used as a scratch register
2499 */
2500kvmppc_msr_interrupt:
2501 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
2502 cmpwi r0, 2 /* Check if we are in transactional state.. */
2503 ld r11, VCPU_INTR_MSR(r9)
2504 bne 1f
2505 /* ... if transactional, change to suspended */
2506 li r0, 1
25071: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
2508 blr
Paul Mackerras9bc01a92014-05-26 19:48:40 +10002509
2510/*
2511 * This works around a hardware bug on POWER8E processors, where
2512 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
2513 * performance monitor interrupt. Instead, when we need to have
2514 * an interrupt pending, we have to arrange for a counter to overflow.
2515 */
2516kvmppc_fix_pmao:
2517 li r3, 0
2518 mtspr SPRN_MMCR2, r3
2519 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
2520 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
2521 mtspr SPRN_MMCR0, r3
2522 lis r3, 0x7fff
2523 ori r3, r3, 0xffff
2524 mtspr SPRN_PMC6, r3
2525 isync
2526 blr
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002527
2528#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2529/*
2530 * Start timing an activity
2531 * r3 = pointer to time accumulation struct, r4 = vcpu
2532 */
2533kvmhv_start_timing:
2534 ld r5, HSTATE_KVM_VCORE(r13)
2535 lbz r6, VCORE_IN_GUEST(r5)
2536 cmpwi r6, 0
2537 beq 5f /* if in guest, need to */
2538 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
25395: mftb r5
2540 subf r5, r6, r5
2541 std r3, VCPU_CUR_ACTIVITY(r4)
2542 std r5, VCPU_ACTIVITY_START(r4)
2543 blr
2544
2545/*
2546 * Accumulate time to one activity and start another.
2547 * r3 = pointer to new time accumulation struct, r4 = vcpu
2548 */
2549kvmhv_accumulate_time:
2550 ld r5, HSTATE_KVM_VCORE(r13)
2551 lbz r8, VCORE_IN_GUEST(r5)
2552 cmpwi r8, 0
2553 beq 4f /* if in guest, need to */
2554 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
25554: ld r5, VCPU_CUR_ACTIVITY(r4)
2556 ld r6, VCPU_ACTIVITY_START(r4)
2557 std r3, VCPU_CUR_ACTIVITY(r4)
2558 mftb r7
2559 subf r7, r8, r7
2560 std r7, VCPU_ACTIVITY_START(r4)
2561 cmpdi r5, 0
2562 beqlr
2563 subf r3, r6, r7
2564 ld r8, TAS_SEQCOUNT(r5)
2565 cmpdi r8, 0
2566 addi r8, r8, 1
2567 std r8, TAS_SEQCOUNT(r5)
2568 lwsync
2569 ld r7, TAS_TOTAL(r5)
2570 add r7, r7, r3
2571 std r7, TAS_TOTAL(r5)
2572 ld r6, TAS_MIN(r5)
2573 ld r7, TAS_MAX(r5)
2574 beq 3f
2575 cmpd r3, r6
2576 bge 1f
25773: std r3, TAS_MIN(r5)
25781: cmpd r3, r7
2579 ble 2f
2580 std r3, TAS_MAX(r5)
25812: lwsync
2582 addi r8, r8, 1
2583 std r8, TAS_SEQCOUNT(r5)
2584 blr
2585#endif