KVM: PPC: Book3S HV: Make sure we don't re-enter guest without XIVE loaded
[linux-2.6-block.git] / arch / powerpc / kvm / book3s_hv_rmhandlers.S
CommitLineData
de56a948
PM
1/*
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>
177339d7 23#include <asm/mmu.h>
de56a948 24#include <asm/page.h>
177339d7
PM
25#include <asm/ptrace.h>
26#include <asm/hvcall.h>
de56a948
PM
27#include <asm/asm-offsets.h>
28#include <asm/exception-64s.h>
f0888f70 29#include <asm/kvm_book3s_asm.h>
f64e8084 30#include <asm/book3s/64/mmu-hash.h>
e4e38121 31#include <asm/tm.h>
fd7bacbc 32#include <asm/opal.h>
5af50993 33#include <asm/xive-regs.h>
857b99e1 34#include <asm/thread_info.h>
e4e38121 35
2f272463
PM
36/* Sign-extend HDEC if not on POWER9 */
37#define EXTEND_HDEC(reg) \
38BEGIN_FTR_SECTION; \
39 extsw reg, reg; \
40END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
41
e4e38121 42#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
de56a948 43
e0b7ec05
PM
44/* Values in HSTATE_NAPPING(r13) */
45#define NAPPING_CEDE 1
46#define NAPPING_NOVCPU 2
47
7ceaa6dc 48/* Stack frame offsets for kvmppc_hv_entry */
769377f7 49#define SFS 160
7ceaa6dc
PM
50#define STACK_SLOT_TRAP (SFS-4)
51#define STACK_SLOT_TID (SFS-16)
52#define STACK_SLOT_PSSCR (SFS-24)
53#define STACK_SLOT_PID (SFS-32)
54#define STACK_SLOT_IAMR (SFS-40)
55#define STACK_SLOT_CIABR (SFS-48)
56#define STACK_SLOT_DAWR (SFS-56)
57#define STACK_SLOT_DAWRX (SFS-64)
769377f7 58#define STACK_SLOT_HFSCR (SFS-72)
7ceaa6dc 59
de56a948 60/*
19ccb76a 61 * Call kvmppc_hv_entry in real mode.
de56a948
PM
62 * Must be called with interrupts hard-disabled.
63 *
64 * Input Registers:
65 *
66 * LR = return address to continue at after eventually re-enabling MMU
67 */
6ed179b6 68_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
218309b7
PM
69 mflr r0
70 std r0, PPC_LR_STKOFF(r1)
71 stdu r1, -112(r1)
de56a948 72 mfmsr r10
8b24e69f 73 std r10, HSTATE_HOST_MSR(r13)
218309b7 74 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
de56a948
PM
75 li r0,MSR_RI
76 andc r0,r10,r0
77 li r6,MSR_IR | MSR_DR
78 andc r6,r10,r6
79 mtmsrd r0,1 /* clear RI in MSR */
80 mtsrr0 r5
81 mtsrr1 r6
82 RFI
83
218309b7 84kvmppc_call_hv_entry:
c0101509
PM
85BEGIN_FTR_SECTION
86 /* On P9, do LPCR setting, if necessary */
87 ld r3, HSTATE_SPLIT_MODE(r13)
88 cmpdi r3, 0
89 beq 46f
90 lwz r4, KVM_SPLIT_DO_SET(r3)
91 cmpwi r4, 0
92 beq 46f
93 bl kvmhv_p9_set_lpcr
94 nop
9546:
96END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
97
e0b7ec05 98 ld r4, HSTATE_KVM_VCPU(r13)
218309b7
PM
99 bl kvmppc_hv_entry
100
101 /* Back from guest - restore host state and return to caller */
102
eee7ff9d 103BEGIN_FTR_SECTION
218309b7
PM
104 /* Restore host DABR and DABRX */
105 ld r5,HSTATE_DABR(r13)
106 li r6,7
107 mtspr SPRN_DABR,r5
108 mtspr SPRN_DABRX,r6
eee7ff9d 109END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
218309b7
PM
110
111 /* Restore SPRG3 */
9d378dfa
SW
112 ld r3,PACA_SPRG_VDSO(r13)
113 mtspr SPRN_SPRG_VDSO_WRITE,r3
218309b7 114
218309b7
PM
115 /* Reload the host's PMU registers */
116 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
117 lbz r4, LPPACA_PMCINUSE(r3)
118 cmpwi r4, 0
119 beq 23f /* skip if not */
9bc01a9b 120BEGIN_FTR_SECTION
9a4fc4ea 121 ld r3, HSTATE_MMCR0(r13)
9bc01a9b
PM
122 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
123 cmpwi r4, MMCR0_PMAO
124 beql kvmppc_fix_pmao
125END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
9a4fc4ea
ME
126 lwz r3, HSTATE_PMC1(r13)
127 lwz r4, HSTATE_PMC2(r13)
128 lwz r5, HSTATE_PMC3(r13)
129 lwz r6, HSTATE_PMC4(r13)
130 lwz r8, HSTATE_PMC5(r13)
131 lwz r9, HSTATE_PMC6(r13)
218309b7
PM
132 mtspr SPRN_PMC1, r3
133 mtspr SPRN_PMC2, r4
134 mtspr SPRN_PMC3, r5
135 mtspr SPRN_PMC4, r6
136 mtspr SPRN_PMC5, r8
137 mtspr SPRN_PMC6, r9
9a4fc4ea
ME
138 ld r3, HSTATE_MMCR0(r13)
139 ld r4, HSTATE_MMCR1(r13)
140 ld r5, HSTATE_MMCRA(r13)
141 ld r6, HSTATE_SIAR(r13)
142 ld r7, HSTATE_SDAR(r13)
218309b7
PM
143 mtspr SPRN_MMCR1, r4
144 mtspr SPRN_MMCRA, r5
72cde5a8
PM
145 mtspr SPRN_SIAR, r6
146 mtspr SPRN_SDAR, r7
147BEGIN_FTR_SECTION
9a4fc4ea
ME
148 ld r8, HSTATE_MMCR2(r13)
149 ld r9, HSTATE_SIER(r13)
72cde5a8
PM
150 mtspr SPRN_MMCR2, r8
151 mtspr SPRN_SIER, r9
152END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
218309b7
PM
153 mtspr SPRN_MMCR0, r3
154 isync
15523:
156
e0b7ec05
PM
157 /*
158 * Reload DEC. HDEC interrupts were disabled when
159 * we reloaded the host's LPCR value.
160 */
161 ld r3, HSTATE_DECEXP(r13)
162 mftb r4
163 subf r4, r4, r3
164 mtspr SPRN_DEC, r4
165
b4deba5c
PM
166 /* hwthread_req may have got set by cede or no vcpu, so clear it */
167 li r0, 0
168 stb r0, HSTATE_HWTHREAD_REQ(r13)
169
218309b7 170 /*
e20bbd3d
AP
171 * For external interrupts we need to call the Linux
172 * handler to process the interrupt. We do that by jumping
173 * to absolute address 0x500 for external interrupts.
174 * The [h]rfid at the end of the handler will return to
175 * the book3s_hv_interrupts.S code. For other interrupts
176 * we do the rfid to get back to the book3s_hv_interrupts.S
177 * code here.
218309b7
PM
178 */
179 ld r8, 112+PPC_LR_STKOFF(r1)
180 addi r1, r1, 112
181 ld r7, HSTATE_HOST_MSR(r13)
182
8b24e69f
PM
183 /* Return the trap number on this thread as the return value */
184 mr r3, r12
185
53af3ba2
PM
186 /*
187 * If we came back from the guest via a relocation-on interrupt,
188 * we will be in virtual mode at this point, which makes it a
189 * little easier to get back to the caller.
190 */
191 mfmsr r0
192 andi. r0, r0, MSR_IR /* in real mode? */
193 bne .Lvirt_return
194
8b24e69f 195 /* RFI into the highmem handler */
218309b7
PM
196 mfmsr r6
197 li r0, MSR_RI
198 andc r6, r6, r0
199 mtmsrd r6, 1 /* Clear RI in MSR */
200 mtsrr0 r8
201 mtsrr1 r7
218309b7
PM
202 RFI
203
8b24e69f 204 /* Virtual-mode return */
53af3ba2 205.Lvirt_return:
8b24e69f 206 mtlr r8
53af3ba2
PM
207 blr
208
e0b7ec05
PM
209kvmppc_primary_no_guest:
210 /* We handle this much like a ceded vcpu */
fd6d53b1 211 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */
2f272463
PM
212 /* HDEC may be larger than DEC for arch >= v3.00, but since the */
213 /* HDEC value came from DEC in the first place, it will fit */
fd6d53b1
PM
214 mfspr r3, SPRN_HDEC
215 mtspr SPRN_DEC, r3
6af27c84
PM
216 /*
217 * Make sure the primary has finished the MMU switch.
218 * We should never get here on a secondary thread, but
219 * check it for robustness' sake.
220 */
221 ld r5, HSTATE_KVM_VCORE(r13)
22265: lbz r0, VCORE_IN_GUEST(r5)
223 cmpwi r0, 0
224 beq 65b
225 /* Set LPCR. */
226 ld r8,VCORE_LPCR(r5)
227 mtspr SPRN_LPCR,r8
228 isync
e0b7ec05
PM
229 /* set our bit in napping_threads */
230 ld r5, HSTATE_KVM_VCORE(r13)
231 lbz r7, HSTATE_PTID(r13)
232 li r0, 1
233 sld r0, r0, r7
234 addi r6, r5, VCORE_NAPPING_THREADS
2351: lwarx r3, 0, r6
236 or r3, r3, r0
237 stwcx. r3, 0, r6
238 bne 1b
7d6c40da 239 /* order napping_threads update vs testing entry_exit_map */
e0b7ec05
PM
240 isync
241 li r12, 0
242 lwz r7, VCORE_ENTRY_EXIT(r5)
243 cmpwi r7, 0x100
244 bge kvm_novcpu_exit /* another thread already exiting */
245 li r3, NAPPING_NOVCPU
246 stb r3, HSTATE_NAPPING(r13)
e0b7ec05 247
ccc07772 248 li r3, 0 /* Don't wake on privileged (OS) doorbell */
e0b7ec05
PM
249 b kvm_do_nap
250
37f55d30
SW
251/*
252 * kvm_novcpu_wakeup
253 * Entered from kvm_start_guest if kvm_hstate.napping is set
254 * to NAPPING_NOVCPU
255 * r2 = kernel TOC
256 * r13 = paca
257 */
e0b7ec05
PM
258kvm_novcpu_wakeup:
259 ld r1, HSTATE_HOST_R1(r13)
260 ld r5, HSTATE_KVM_VCORE(r13)
261 li r0, 0
262 stb r0, HSTATE_NAPPING(r13)
e0b7ec05 263
e3bbbbfa
PM
264 /* check the wake reason */
265 bl kvmppc_check_wake_reason
6af27c84 266
37f55d30
SW
267 /*
268 * Restore volatile registers since we could have called
269 * a C routine in kvmppc_check_wake_reason.
270 * r5 = VCORE
271 */
272 ld r5, HSTATE_KVM_VCORE(r13)
273
e0b7ec05 274 /* see if any other thread is already exiting */
e0b7ec05
PM
275 lwz r0, VCORE_ENTRY_EXIT(r5)
276 cmpwi r0, 0x100
277 bge kvm_novcpu_exit
278
279 /* clear our bit in napping_threads */
280 lbz r7, HSTATE_PTID(r13)
281 li r0, 1
282 sld r0, r0, r7
283 addi r6, r5, VCORE_NAPPING_THREADS
e3bbbbfa
PM
2844: lwarx r7, 0, r6
285 andc r7, r7, r0
286 stwcx. r7, 0, r6
e0b7ec05
PM
287 bne 4b
288
e3bbbbfa 289 /* See if the wake reason means we need to exit */
e0b7ec05
PM
290 cmpdi r3, 0
291 bge kvm_novcpu_exit
e0b7ec05 292
fd6d53b1
PM
293 /* See if our timeslice has expired (HDEC is negative) */
294 mfspr r0, SPRN_HDEC
2f272463 295 EXTEND_HDEC(r0)
fd6d53b1 296 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
2f272463 297 cmpdi r0, 0
fd6d53b1
PM
298 blt kvm_novcpu_exit
299
e0b7ec05
PM
300 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
301 ld r4, HSTATE_KVM_VCPU(r13)
302 cmpdi r4, 0
b6c295df
PM
303 beq kvmppc_primary_no_guest
304
305#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
306 addi r3, r4, VCPU_TB_RMENTRY
307 bl kvmhv_start_timing
308#endif
309 b kvmppc_got_guest
e0b7ec05
PM
310
311kvm_novcpu_exit:
6af27c84
PM
312#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
313 ld r4, HSTATE_KVM_VCPU(r13)
314 cmpdi r4, 0
315 beq 13f
316 addi r3, r4, VCPU_TB_RMEXIT
317 bl kvmhv_accumulate_time
318#endif
eddb60fb 31913: mr r3, r12
7ceaa6dc 320 stw r12, STACK_SLOT_TRAP(r1)
eddb60fb
PM
321 bl kvmhv_commence_exit
322 nop
7ceaa6dc 323 lwz r12, STACK_SLOT_TRAP(r1)
6af27c84 324 b kvmhv_switch_to_host
e0b7ec05 325
371fefd6 326/*
e0b7ec05 327 * We come in here when wakened from nap mode.
371fefd6
PM
328 * Relocation is off and most register values are lost.
329 * r13 points to the PACA.
9d292501 330 * r3 contains the SRR1 wakeup value, SRR1 is trashed.
371fefd6
PM
331 */
332 .globl kvm_start_guest
333kvm_start_guest:
fd17dc7b 334 /* Set runlatch bit the minute you wake up from nap */
1f09c3ed
PM
335 mfspr r0, SPRN_CTRLF
336 ori r0, r0, 1
337 mtspr SPRN_CTRLT, r0
fd17dc7b 338
9d292501
NP
339 /*
340 * Could avoid this and pass it through in r3. For now,
341 * code expects it to be in SRR1.
342 */
343 mtspr SPRN_SRR1,r3
344
19ccb76a
PM
345 ld r2,PACATOC(r13)
346
f0888f70
PM
347 li r0,KVM_HWTHREAD_IN_KVM
348 stb r0,HSTATE_HWTHREAD_STATE(r13)
371fefd6 349
f0888f70
PM
350 /* NV GPR values from power7_idle() will no longer be valid */
351 li r0,1
352 stb r0,PACA_NAPSTATELOST(r13)
371fefd6 353
4619ac88
PM
354 /* were we napping due to cede? */
355 lbz r0,HSTATE_NAPPING(r13)
e0b7ec05
PM
356 cmpwi r0,NAPPING_CEDE
357 beq kvm_end_cede
358 cmpwi r0,NAPPING_NOVCPU
359 beq kvm_novcpu_wakeup
360
361 ld r1,PACAEMERGSP(r13)
362 subi r1,r1,STACK_FRAME_OVERHEAD
4619ac88
PM
363
364 /*
365 * We weren't napping due to cede, so this must be a secondary
366 * thread being woken up to run a guest, or being woken up due
367 * to a stray IPI. (Or due to some machine check or hypervisor
368 * maintenance interrupt while the core is in KVM.)
369 */
f0888f70
PM
370
371 /* Check the wake reason in SRR1 to see why we got here */
e3bbbbfa 372 bl kvmppc_check_wake_reason
37f55d30
SW
373 /*
374 * kvmppc_check_wake_reason could invoke a C routine, but we
375 * have no volatile registers to restore when we return.
376 */
377
e3bbbbfa
PM
378 cmpdi r3, 0
379 bge kvm_no_guest
371fefd6 380
b4deba5c
PM
381 /* get vcore pointer, NULL if we have nothing to run */
382 ld r5,HSTATE_KVM_VCORE(r13)
383 cmpdi r5,0
384 /* if we have no vcore to run, go back to sleep */
7b444c67 385 beq kvm_no_guest
f0888f70 386
56548fc0
PM
387kvm_secondary_got_guest:
388
e0b7ec05 389 /* Set HSTATE_DSCR(r13) to something sensible */
1db36525 390 ld r6, PACA_DSCR_DEFAULT(r13)
e0b7ec05 391 std r6, HSTATE_DSCR(r13)
2fde6d20 392
b4deba5c
PM
393 /* On thread 0 of a subcore, set HDEC to max */
394 lbz r4, HSTATE_PTID(r13)
395 cmpwi r4, 0
396 bne 63f
2f272463
PM
397 LOAD_REG_ADDR(r6, decrementer_max)
398 ld r6, 0(r6)
b4deba5c
PM
399 mtspr SPRN_HDEC, r6
400 /* and set per-LPAR registers, if doing dynamic micro-threading */
401 ld r6, HSTATE_SPLIT_MODE(r13)
402 cmpdi r6, 0
403 beq 63f
c0101509 404BEGIN_FTR_SECTION
b4deba5c
PM
405 ld r0, KVM_SPLIT_RPR(r6)
406 mtspr SPRN_RPR, r0
407 ld r0, KVM_SPLIT_PMMAR(r6)
408 mtspr SPRN_PMMAR, r0
409 ld r0, KVM_SPLIT_LDBAR(r6)
410 mtspr SPRN_LDBAR, r0
411 isync
c0101509
PM
412FTR_SECTION_ELSE
413 /* On P9 we use the split_info for coordinating LPCR changes */
414 lwz r4, KVM_SPLIT_DO_SET(r6)
415 cmpwi r4, 0
416 beq 63f
417 mr r3, r6
418 bl kvmhv_p9_set_lpcr
419 nop
420ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
b4deba5c
PM
42163:
422 /* Order load of vcpu after load of vcore */
5d5b99cd 423 lwsync
b4deba5c 424 ld r4, HSTATE_KVM_VCPU(r13)
e0b7ec05 425 bl kvmppc_hv_entry
218309b7
PM
426
427 /* Back from the guest, go back to nap */
b4deba5c 428 /* Clear our vcpu and vcore pointers so we don't come back in early */
218309b7 429 li r0, 0
b4deba5c 430 std r0, HSTATE_KVM_VCPU(r13)
f019b7ad 431 /*
b4deba5c 432 * Once we clear HSTATE_KVM_VCORE(r13), the code in
5d5b99cd
PM
433 * kvmppc_run_core() is going to assume that all our vcpu
434 * state is visible in memory. This lwsync makes sure
435 * that that is true.
f019b7ad 436 */
218309b7 437 lwsync
b4deba5c 438 std r0, HSTATE_KVM_VCORE(r13)
218309b7 439
fd7bacbc
MS
440 /*
441 * All secondaries exiting guest will fall through this path.
442 * Before proceeding, just check for HMI interrupt and
443 * invoke opal hmi handler. By now we are sure that the
444 * primary thread on this core/subcore has already made partition
445 * switch/TB resync and we are good to call opal hmi handler.
446 */
447 cmpwi r12, BOOK3S_INTERRUPT_HMI
448 bne kvm_no_guest
449
450 li r3,0 /* NULL argument */
451 bl hmi_exception_realmode
56548fc0
PM
452/*
453 * At this point we have finished executing in the guest.
454 * We need to wait for hwthread_req to become zero, since
455 * we may not turn on the MMU while hwthread_req is non-zero.
456 * While waiting we also need to check if we get given a vcpu to run.
457 */
218309b7 458kvm_no_guest:
56548fc0
PM
459 lbz r3, HSTATE_HWTHREAD_REQ(r13)
460 cmpwi r3, 0
461 bne 53f
462 HMT_MEDIUM
463 li r0, KVM_HWTHREAD_IN_KERNEL
218309b7 464 stb r0, HSTATE_HWTHREAD_STATE(r13)
56548fc0
PM
465 /* need to recheck hwthread_req after a barrier, to avoid race */
466 sync
467 lbz r3, HSTATE_HWTHREAD_REQ(r13)
468 cmpwi r3, 0
469 bne 54f
470/*
5fa6b6bd 471 * We jump to pnv_wakeup_loss, which will return to the caller
56548fc0 472 * of power7_nap in the powernv cpu offline loop. The value we
9d292501
NP
473 * put in r3 becomes the return value for power7_nap. pnv_wakeup_loss
474 * requires SRR1 in r12.
56548fc0 475 */
218309b7
PM
476 li r3, LPCR_PECE0
477 mfspr r4, SPRN_LPCR
478 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
479 mtspr SPRN_LPCR, r4
56548fc0 480 li r3, 0
9d292501 481 mfspr r12,SPRN_SRR1
5fa6b6bd 482 b pnv_wakeup_loss
56548fc0
PM
483
48453: HMT_LOW
b4deba5c
PM
485 ld r5, HSTATE_KVM_VCORE(r13)
486 cmpdi r5, 0
487 bne 60f
488 ld r3, HSTATE_SPLIT_MODE(r13)
489 cmpdi r3, 0
490 beq kvm_no_guest
c0101509
PM
491 lwz r0, KVM_SPLIT_DO_SET(r3)
492 cmpwi r0, 0
493 bne kvmhv_do_set
494 lwz r0, KVM_SPLIT_DO_RESTORE(r3)
495 cmpwi r0, 0
496 bne kvmhv_do_restore
b4deba5c
PM
497 lbz r0, KVM_SPLIT_DO_NAP(r3)
498 cmpwi r0, 0
56548fc0
PM
499 beq kvm_no_guest
500 HMT_MEDIUM
b4deba5c
PM
501 b kvm_unsplit_nap
50260: HMT_MEDIUM
56548fc0
PM
503 b kvm_secondary_got_guest
504
50554: li r0, KVM_HWTHREAD_IN_KVM
506 stb r0, HSTATE_HWTHREAD_STATE(r13)
507 b kvm_no_guest
218309b7 508
c0101509
PM
509kvmhv_do_set:
510 /* Set LPCR, LPIDR etc. on P9 */
511 HMT_MEDIUM
512 bl kvmhv_p9_set_lpcr
513 nop
514 b kvm_no_guest
515
516kvmhv_do_restore:
517 HMT_MEDIUM
518 bl kvmhv_p9_restore_lpcr
519 nop
520 b kvm_no_guest
521
b4deba5c
PM
522/*
523 * Here the primary thread is trying to return the core to
524 * whole-core mode, so we need to nap.
525 */
526kvm_unsplit_nap:
fd7bacbc
MS
527 /*
528 * When secondaries are napping in kvm_unsplit_nap() with
529 * hwthread_req = 1, HMI goes ignored even though subcores are
530 * already exited the guest. Hence HMI keeps waking up secondaries
531 * from nap in a loop and secondaries always go back to nap since
532 * no vcore is assigned to them. This makes impossible for primary
533 * thread to get hold of secondary threads resulting into a soft
534 * lockup in KVM path.
535 *
536 * Let us check if HMI is pending and handle it before we go to nap.
537 */
538 cmpwi r12, BOOK3S_INTERRUPT_HMI
539 bne 55f
540 li r3, 0 /* NULL argument */
541 bl hmi_exception_realmode
54255:
7f235328
GS
543 /*
544 * Ensure that secondary doesn't nap when it has
545 * its vcore pointer set.
546 */
547 sync /* matches smp_mb() before setting split_info.do_nap */
548 ld r0, HSTATE_KVM_VCORE(r13)
549 cmpdi r0, 0
550 bne kvm_no_guest
b4deba5c
PM
551 /* clear any pending message */
552BEGIN_FTR_SECTION
553 lis r6, (PPC_DBELL_SERVER << (63-36))@h
554 PPC_MSGCLR(6)
555END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
556 /* Set kvm_split_mode.napped[tid] = 1 */
557 ld r3, HSTATE_SPLIT_MODE(r13)
558 li r0, 1
c0101509 559 lbz r4, HSTATE_TID(r13)
b4deba5c
PM
560 addi r4, r4, KVM_SPLIT_NAPPED
561 stbx r0, r3, r4
562 /* Check the do_nap flag again after setting napped[] */
563 sync
564 lbz r0, KVM_SPLIT_DO_NAP(r3)
565 cmpwi r0, 0
566 beq 57f
567 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4
bf53c88e
PM
568 mfspr r5, SPRN_LPCR
569 rlwimi r5, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1)
570 b kvm_nap_sequence
b4deba5c
PM
571
57257: li r0, 0
573 stbx r0, r3, r4
574 b kvm_no_guest
575
218309b7
PM
576/******************************************************************************
577 * *
578 * Entry code *
579 * *
580 *****************************************************************************/
581
de56a948
PM
582.global kvmppc_hv_entry
583kvmppc_hv_entry:
584
585 /* Required state:
586 *
e0b7ec05 587 * R4 = vcpu pointer (or NULL)
de56a948
PM
588 * MSR = ~IR|DR
589 * R13 = PACA
590 * R1 = host R1
06a29e42 591 * R2 = TOC
de56a948 592 * all other volatile GPRS = free
f4c51f84 593 * Does not preserve non-volatile GPRs or CR fields
de56a948
PM
594 */
595 mflr r0
218309b7 596 std r0, PPC_LR_STKOFF(r1)
7ceaa6dc 597 stdu r1, -SFS(r1)
de56a948 598
de56a948
PM
599 /* Save R1 in the PACA */
600 std r1, HSTATE_HOST_R1(r13)
601
44a3add8
PM
602 li r6, KVM_GUEST_MODE_HOST_HV
603 stb r6, HSTATE_IN_GUEST(r13)
604
b6c295df
PM
605#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
606 /* Store initial timestamp */
607 cmpdi r4, 0
608 beq 1f
609 addi r3, r4, VCPU_TB_RMENTRY
610 bl kvmhv_start_timing
6111:
612#endif
f4c51f84
PM
613
614 /* Use cr7 as an indication of radix mode */
615 ld r5, HSTATE_KVM_VCORE(r13)
616 ld r9, VCORE_KVM(r5) /* pointer to struct kvm */
617 lbz r0, KVM_RADIX(r9)
618 cmpwi cr7, r0, 0
619
620 /* Clear out SLB if hash */
621 bne cr7, 2f
de56a948
PM
622 li r6,0
623 slbmte r6,r6
624 slbia
625 ptesync
f4c51f84 6262:
9e368f29 627 /*
c17b98cf 628 * POWER7/POWER8 host -> guest partition switch code.
9e368f29
PM
629 * We don't have to lock against concurrent tlbies,
630 * but we do have to coordinate across hardware threads.
631 */
7d6c40da 632 /* Set bit in entry map iff exit map is zero. */
7d6c40da
PM
633 li r7, 1
634 lbz r6, HSTATE_PTID(r13)
635 sld r7, r7, r6
f4c51f84
PM
636 addi r8, r5, VCORE_ENTRY_EXIT
63721: lwarx r3, 0, r8
7d6c40da 638 cmpwi r3, 0x100 /* any threads starting to exit? */
371fefd6 639 bge secondary_too_late /* if so we're too late to the party */
7d6c40da 640 or r3, r3, r7
f4c51f84 641 stwcx. r3, 0, r8
371fefd6
PM
642 bne 21b
643
644 /* Primary thread switches to guest partition. */
371fefd6 645 cmpwi r6,0
6af27c84 646 bne 10f
de56a948 647 lwz r7,KVM_LPID(r9)
7a84084c
PM
648BEGIN_FTR_SECTION
649 ld r6,KVM_SDR1(r9)
de56a948
PM
650 li r0,LPID_RSVD /* switch to reserved LPID */
651 mtspr SPRN_LPID,r0
652 ptesync
653 mtspr SPRN_SDR1,r6 /* switch to partition page table */
7a84084c 654END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
de56a948
PM
655 mtspr SPRN_LPID,r7
656 isync
1b400ba0
PM
657
658 /* See if we need to flush the TLB */
659 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
a29ebeaf
PM
660BEGIN_FTR_SECTION
661 /*
662 * On POWER9, individual threads can come in here, but the
663 * TLB is shared between the 4 threads in a core, hence
664 * invalidating on one thread invalidates for all.
665 * Thus we make all 4 threads use the same bit here.
666 */
667 clrrdi r6,r6,2
668END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1b400ba0
PM
669 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
670 srdi r6,r6,6 /* doubleword number */
671 sldi r6,r6,3 /* address offset */
672 add r6,r6,r9
673 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
a29ebeaf
PM
674 li r8,1
675 sld r8,r8,r7
1b400ba0 676 ld r7,0(r6)
a29ebeaf 677 and. r7,r7,r8
1b400ba0 678 beq 22f
ca252055 679 /* Flush the TLB of any entries for this LPID */
a29ebeaf
PM
680 lwz r0,KVM_TLB_SETS(r9)
681 mtctr r0
1b400ba0
PM
682 li r7,0x800 /* IS field = 0b10 */
683 ptesync
a29ebeaf
PM
684 li r0,0 /* RS for P9 version of tlbiel */
685 bne cr7, 29f
68628: tlbiel r7 /* On P9, rs=0, RIC=0, PRS=0, R=0 */
1b400ba0
PM
687 addi r7,r7,0x1000
688 bdnz 28b
a29ebeaf
PM
689 b 30f
69029: PPC_TLBIEL(7,0,2,1,1) /* for radix, RIC=2, PRS=1, R=1 */
691 addi r7,r7,0x1000
692 bdnz 29b
69330: ptesync
69423: ldarx r7,0,r6 /* clear the bit after TLB flushed */
695 andc r7,r7,r8
696 stdcx. r7,0,r6
697 bne 23b
1b400ba0 698
93b0f4dc
PM
699 /* Add timebase offset onto timebase */
70022: ld r8,VCORE_TB_OFFSET(r5)
701 cmpdi r8,0
702 beq 37f
703 mftb r6 /* current host timebase */
704 add r8,r8,r6
705 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
706 mftb r7 /* check if lower 24 bits overflowed */
707 clrldi r6,r6,40
708 clrldi r7,r7,40
709 cmpld r7,r6
710 bge 37f
711 addis r8,r8,0x100 /* if so, increment upper 40 bits */
712 mtspr SPRN_TBU40,r8
713
388cc6e1
PM
714 /* Load guest PCR value to select appropriate compat mode */
71537: ld r7, VCORE_PCR(r5)
716 cmpdi r7, 0
717 beq 38f
718 mtspr SPRN_PCR, r7
71938:
b005255e
MN
720
721BEGIN_FTR_SECTION
88b02cf9 722 /* DPDES and VTB are shared between threads */
b005255e 723 ld r8, VCORE_DPDES(r5)
88b02cf9 724 ld r7, VCORE_VTB(r5)
b005255e 725 mtspr SPRN_DPDES, r8
88b02cf9 726 mtspr SPRN_VTB, r7
b005255e
MN
727END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
728
fd7bacbc
MS
729 /* Mark the subcore state as inside guest */
730 bl kvmppc_subcore_enter_guest
731 nop
732 ld r5, HSTATE_KVM_VCORE(r13)
733 ld r4, HSTATE_KVM_VCPU(r13)
388cc6e1 734 li r0,1
371fefd6 735 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
9e368f29 736
e0b7ec05 737 /* Do we have a guest vcpu to run? */
6af27c84 73810: cmpdi r4, 0
e0b7ec05
PM
739 beq kvmppc_primary_no_guest
740kvmppc_got_guest:
de56a948 741
f4c51f84 742 /* Load up guest SLB entries (N.B. slb_max will be 0 for radix) */
e0b7ec05 743 lwz r5,VCPU_SLB_MAX(r4)
de56a948
PM
744 cmpwi r5,0
745 beq 9f
746 mtctr r5
747 addi r6,r4,VCPU_SLB
7481: ld r8,VCPU_SLB_E(r6)
749 ld r9,VCPU_SLB_V(r6)
750 slbmte r9,r8
751 addi r6,r6,VCPU_SLB_SIZE
752 bdnz 1b
7539:
e0b7ec05
PM
754 /* Increment yield count if they have a VPA */
755 ld r3, VCPU_VPA(r4)
756 cmpdi r3, 0
757 beq 25f
0865a583
AG
758 li r6, LPPACA_YIELDCOUNT
759 LWZX_BE r5, r3, r6
e0b7ec05 760 addi r5, r5, 1
0865a583 761 STWX_BE r5, r3, r6
e0b7ec05
PM
762 li r6, 1
763 stb r6, VCPU_VPA_DIRTY(r4)
76425:
765
e0b7ec05
PM
766 /* Save purr/spurr */
767 mfspr r5,SPRN_PURR
768 mfspr r6,SPRN_SPURR
769 std r5,HSTATE_PURR(r13)
770 std r6,HSTATE_SPURR(r13)
771 ld r7,VCPU_PURR(r4)
772 ld r8,VCPU_SPURR(r4)
773 mtspr SPRN_PURR,r7
774 mtspr SPRN_SPURR,r8
e0b7ec05 775
e9cf1e08
PM
776 /* Save host values of some registers */
777BEGIN_FTR_SECTION
778 mfspr r5, SPRN_TIDR
779 mfspr r6, SPRN_PSSCR
f4c51f84 780 mfspr r7, SPRN_PID
4c3bb4cc 781 mfspr r8, SPRN_IAMR
e9cf1e08
PM
782 std r5, STACK_SLOT_TID(r1)
783 std r6, STACK_SLOT_PSSCR(r1)
f4c51f84 784 std r7, STACK_SLOT_PID(r1)
4c3bb4cc 785 std r8, STACK_SLOT_IAMR(r1)
769377f7
PM
786 mfspr r5, SPRN_HFSCR
787 std r5, STACK_SLOT_HFSCR(r1)
e9cf1e08 788END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
7ceaa6dc
PM
789BEGIN_FTR_SECTION
790 mfspr r5, SPRN_CIABR
791 mfspr r6, SPRN_DAWR
792 mfspr r7, SPRN_DAWRX
793 std r5, STACK_SLOT_CIABR(r1)
794 std r6, STACK_SLOT_DAWR(r1)
795 std r7, STACK_SLOT_DAWRX(r1)
796END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e9cf1e08 797
e0b7ec05
PM
798BEGIN_FTR_SECTION
799 /* Set partition DABR */
800 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
8563bf52 801 lwz r5,VCPU_DABRX(r4)
e0b7ec05
PM
802 ld r6,VCPU_DABR(r4)
803 mtspr SPRN_DABRX,r5
804 mtspr SPRN_DABR,r6
e0b7ec05 805 isync
e0b7ec05
PM
806END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
807
e4e38121
MN
808#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
809BEGIN_FTR_SECTION
67f8a8c1
PM
810 /*
811 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
812 */
f024ee09
PM
813 bl kvmppc_restore_tm
814END_FTR_SECTION_IFSET(CPU_FTR_TM)
e4e38121
MN
815#endif
816
e0b7ec05
PM
817 /* Load guest PMU registers */
818 /* R4 is live here (vcpu pointer) */
819 li r3, 1
820 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
821 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
822 isync
9bc01a9b
PM
823BEGIN_FTR_SECTION
824 ld r3, VCPU_MMCR(r4)
825 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
826 cmpwi r5, MMCR0_PMAO
827 beql kvmppc_fix_pmao
828END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
e0b7ec05
PM
829 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
830 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
831 lwz r6, VCPU_PMC + 8(r4)
832 lwz r7, VCPU_PMC + 12(r4)
833 lwz r8, VCPU_PMC + 16(r4)
834 lwz r9, VCPU_PMC + 20(r4)
e0b7ec05
PM
835 mtspr SPRN_PMC1, r3
836 mtspr SPRN_PMC2, r5
837 mtspr SPRN_PMC3, r6
838 mtspr SPRN_PMC4, r7
839 mtspr SPRN_PMC5, r8
840 mtspr SPRN_PMC6, r9
e0b7ec05
PM
841 ld r3, VCPU_MMCR(r4)
842 ld r5, VCPU_MMCR + 8(r4)
843 ld r6, VCPU_MMCR + 16(r4)
844 ld r7, VCPU_SIAR(r4)
845 ld r8, VCPU_SDAR(r4)
846 mtspr SPRN_MMCR1, r5
847 mtspr SPRN_MMCRA, r6
848 mtspr SPRN_SIAR, r7
849 mtspr SPRN_SDAR, r8
b005255e
MN
850BEGIN_FTR_SECTION
851 ld r5, VCPU_MMCR + 24(r4)
852 ld r6, VCPU_SIER(r4)
83677f55
PM
853 mtspr SPRN_MMCR2, r5
854 mtspr SPRN_SIER, r6
855BEGIN_FTR_SECTION_NESTED(96)
b005255e
MN
856 lwz r7, VCPU_PMC + 24(r4)
857 lwz r8, VCPU_PMC + 28(r4)
858 ld r9, VCPU_MMCR + 32(r4)
b005255e
MN
859 mtspr SPRN_SPMC1, r7
860 mtspr SPRN_SPMC2, r8
861 mtspr SPRN_MMCRS, r9
83677f55 862END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
b005255e 863END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05
PM
864 mtspr SPRN_MMCR0, r3
865 isync
866
867 /* Load up FP, VMX and VSX registers */
868 bl kvmppc_load_fp
869
870 ld r14, VCPU_GPR(R14)(r4)
871 ld r15, VCPU_GPR(R15)(r4)
872 ld r16, VCPU_GPR(R16)(r4)
873 ld r17, VCPU_GPR(R17)(r4)
874 ld r18, VCPU_GPR(R18)(r4)
875 ld r19, VCPU_GPR(R19)(r4)
876 ld r20, VCPU_GPR(R20)(r4)
877 ld r21, VCPU_GPR(R21)(r4)
878 ld r22, VCPU_GPR(R22)(r4)
879 ld r23, VCPU_GPR(R23)(r4)
880 ld r24, VCPU_GPR(R24)(r4)
881 ld r25, VCPU_GPR(R25)(r4)
882 ld r26, VCPU_GPR(R26)(r4)
883 ld r27, VCPU_GPR(R27)(r4)
884 ld r28, VCPU_GPR(R28)(r4)
885 ld r29, VCPU_GPR(R29)(r4)
886 ld r30, VCPU_GPR(R30)(r4)
887 ld r31, VCPU_GPR(R31)(r4)
888
e0b7ec05
PM
889 /* Switch DSCR to guest value */
890 ld r5, VCPU_DSCR(r4)
891 mtspr SPRN_DSCR, r5
e0b7ec05 892
b005255e 893BEGIN_FTR_SECTION
c17b98cf 894 /* Skip next section on POWER7 */
b005255e
MN
895 b 8f
896END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
b005255e
MN
897 /* Load up POWER8-specific registers */
898 ld r5, VCPU_IAMR(r4)
899 lwz r6, VCPU_PSPB(r4)
900 ld r7, VCPU_FSCR(r4)
901 mtspr SPRN_IAMR, r5
902 mtspr SPRN_PSPB, r6
903 mtspr SPRN_FSCR, r7
904 ld r5, VCPU_DAWR(r4)
905 ld r6, VCPU_DAWRX(r4)
906 ld r7, VCPU_CIABR(r4)
907 ld r8, VCPU_TAR(r4)
908 mtspr SPRN_DAWR, r5
909 mtspr SPRN_DAWRX, r6
910 mtspr SPRN_CIABR, r7
911 mtspr SPRN_TAR, r8
912 ld r5, VCPU_IC(r4)
7b490411 913 ld r8, VCPU_EBBHR(r4)
88b02cf9 914 mtspr SPRN_IC, r5
b005255e
MN
915 mtspr SPRN_EBBHR, r8
916 ld r5, VCPU_EBBRR(r4)
917 ld r6, VCPU_BESCR(r4)
83677f55
PM
918 lwz r7, VCPU_GUEST_PID(r4)
919 ld r8, VCPU_WORT(r4)
b005255e
MN
920 mtspr SPRN_EBBRR, r5
921 mtspr SPRN_BESCR, r6
83677f55
PM
922 mtspr SPRN_PID, r7
923 mtspr SPRN_WORT, r8
f11f6f79
PM
924BEGIN_FTR_SECTION
925 PPC_INVALIDATE_ERAT
926END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
83677f55 927BEGIN_FTR_SECTION
e9cf1e08 928 /* POWER8-only registers */
b005255e
MN
929 ld r5, VCPU_TCSCR(r4)
930 ld r6, VCPU_ACOP(r4)
83677f55
PM
931 ld r7, VCPU_CSIGR(r4)
932 ld r8, VCPU_TACR(r4)
b005255e
MN
933 mtspr SPRN_TCSCR, r5
934 mtspr SPRN_ACOP, r6
83677f55
PM
935 mtspr SPRN_CSIGR, r7
936 mtspr SPRN_TACR, r8
e9cf1e08
PM
937FTR_SECTION_ELSE
938 /* POWER9-only registers */
939 ld r5, VCPU_TID(r4)
940 ld r6, VCPU_PSSCR(r4)
941 oris r6, r6, PSSCR_EC@h /* This makes stop trap to HV */
769377f7 942 ld r7, VCPU_HFSCR(r4)
e9cf1e08
PM
943 mtspr SPRN_TIDR, r5
944 mtspr SPRN_PSSCR, r6
769377f7 945 mtspr SPRN_HFSCR, r7
e9cf1e08 946ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
b005255e
MN
9478:
948
e0b7ec05
PM
949 /*
950 * Set the decrementer to the guest decrementer.
951 */
952 ld r8,VCPU_DEC_EXPIRES(r4)
c5fb80d3
PM
953 /* r8 is a host timebase value here, convert to guest TB */
954 ld r5,HSTATE_KVM_VCORE(r13)
955 ld r6,VCORE_TB_OFFSET(r5)
956 add r8,r8,r6
e0b7ec05
PM
957 mftb r7
958 subf r3,r7,r8
959 mtspr SPRN_DEC,r3
e0b7ec05
PM
960
961 ld r5, VCPU_SPRG0(r4)
962 ld r6, VCPU_SPRG1(r4)
963 ld r7, VCPU_SPRG2(r4)
964 ld r8, VCPU_SPRG3(r4)
965 mtspr SPRN_SPRG0, r5
966 mtspr SPRN_SPRG1, r6
967 mtspr SPRN_SPRG2, r7
968 mtspr SPRN_SPRG3, r8
969
970 /* Load up DAR and DSISR */
971 ld r5, VCPU_DAR(r4)
972 lwz r6, VCPU_DSISR(r4)
973 mtspr SPRN_DAR, r5
974 mtspr SPRN_DSISR, r6
975
e0b7ec05
PM
976 /* Restore AMR and UAMOR, set AMOR to all 1s */
977 ld r5,VCPU_AMR(r4)
978 ld r6,VCPU_UAMOR(r4)
979 li r7,-1
980 mtspr SPRN_AMR,r5
981 mtspr SPRN_UAMOR,r6
982 mtspr SPRN_AMOR,r7
de56a948
PM
983
984 /* Restore state of CTRL run bit; assume 1 on entry */
985 lwz r5,VCPU_CTRL(r4)
986 andi. r5,r5,1
987 bne 4f
988 mfspr r6,SPRN_CTRLF
989 clrrdi r6,r6,1
990 mtspr SPRN_CTRLT,r6
9914:
6af27c84
PM
992 /* Secondary threads wait for primary to have done partition switch */
993 ld r5, HSTATE_KVM_VCORE(r13)
994 lbz r6, HSTATE_PTID(r13)
995 cmpwi r6, 0
996 beq 21f
997 lbz r0, VCORE_IN_GUEST(r5)
998 cmpwi r0, 0
999 bne 21f
1000 HMT_LOW
b4deba5c
PM
100120: lwz r3, VCORE_ENTRY_EXIT(r5)
1002 cmpwi r3, 0x100
1003 bge no_switch_exit
1004 lbz r0, VCORE_IN_GUEST(r5)
6af27c84
PM
1005 cmpwi r0, 0
1006 beq 20b
1007 HMT_MEDIUM
100821:
1009 /* Set LPCR. */
1010 ld r8,VCORE_LPCR(r5)
1011 mtspr SPRN_LPCR,r8
1012 isync
1013
1014 /* Check if HDEC expires soon */
1015 mfspr r3, SPRN_HDEC
2f272463
PM
1016 EXTEND_HDEC(r3)
1017 cmpdi r3, 512 /* 1 microsecond */
6af27c84
PM
1018 blt hdec_soon
1019
5af50993
BH
1020#ifdef CONFIG_KVM_XICS
1021 /* We are entering the guest on that thread, push VCPU to XIVE */
1022 ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
0bfa33c7 1023 cmpldi cr0, r10, 0
5af50993
BH
1024 beq no_xive
1025 ld r11, VCPU_XIVE_SAVED_STATE(r4)
1026 li r9, TM_QW1_OS
5af50993 1027 eieio
ad98dd1a 1028 stdcix r11,r9,r10
5af50993
BH
1029 lwz r11, VCPU_XIVE_CAM_WORD(r4)
1030 li r9, TM_QW1_OS + TM_WORD2
1031 stwcix r11,r9,r10
1032 li r9, 1
1033 stw r9, VCPU_XIVE_PUSHED(r4)
ad98dd1a 1034 eieio
5af50993
BH
1035no_xive:
1036#endif /* CONFIG_KVM_XICS */
1037
37f55d30 1038deliver_guest_interrupt:
de56a948 1039 ld r6, VCPU_CTR(r4)
c63517c2 1040 ld r7, VCPU_XER(r4)
de56a948
PM
1041
1042 mtctr r6
1043 mtxer r7
1044
e3bbbbfa 1045kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
4619ac88
PM
1046 ld r10, VCPU_PC(r4)
1047 ld r11, VCPU_MSR(r4)
de56a948
PM
1048 ld r6, VCPU_SRR0(r4)
1049 ld r7, VCPU_SRR1(r4)
e3bbbbfa
PM
1050 mtspr SPRN_SRR0, r6
1051 mtspr SPRN_SRR1, r7
de56a948 1052
4619ac88 1053 /* r11 = vcpu->arch.msr & ~MSR_HV */
de56a948
PM
1054 rldicl r11, r11, 63 - MSR_HV_LG, 1
1055 rotldi r11, r11, 1 + MSR_HV_LG
1056 ori r11, r11, MSR_ME
1057
19ccb76a 1058 /* Check if we can deliver an external or decrementer interrupt now */
e3bbbbfa
PM
1059 ld r0, VCPU_PENDING_EXC(r4)
1060 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
1061 cmpdi cr1, r0, 0
1062 andi. r8, r11, MSR_EE
e3bbbbfa
PM
1063 mfspr r8, SPRN_LPCR
1064 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
1065 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
1066 mtspr SPRN_LPCR, r8
19ccb76a 1067 isync
19ccb76a 1068 beq 5f
e3bbbbfa
PM
1069 li r0, BOOK3S_INTERRUPT_EXTERNAL
1070 bne cr1, 12f
1071 mfspr r0, SPRN_DEC
1bc3fe81
PM
1072BEGIN_FTR_SECTION
1073 /* On POWER9 check whether the guest has large decrementer enabled */
1074 andis. r8, r8, LPCR_LD@h
1075 bne 15f
1076END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1077 extsw r0, r0
107815: cmpdi r0, 0
e3bbbbfa
PM
1079 li r0, BOOK3S_INTERRUPT_DECREMENTER
1080 bge 5f
19ccb76a 1081
e3bbbbfa 108212: mtspr SPRN_SRR0, r10
19ccb76a 1083 mr r10,r0
e3bbbbfa 1084 mtspr SPRN_SRR1, r11
e4e38121
MN
1085 mr r9, r4
1086 bl kvmppc_msr_interrupt
e3bbbbfa 10875:
57900694
PM
1088BEGIN_FTR_SECTION
1089 b fast_guest_return
1090END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
1091 /* On POWER9, check for pending doorbell requests */
1092 lbz r0, VCPU_DBELL_REQ(r4)
1093 cmpwi r0, 0
1094 beq fast_guest_return
1095 ld r5, HSTATE_KVM_VCORE(r13)
1096 /* Set DPDES register so the CPU will take a doorbell interrupt */
1097 li r0, 1
1098 mtspr SPRN_DPDES, r0
1099 std r0, VCORE_DPDES(r5)
1100 /* Make sure other cpus see vcore->dpdes set before dbell req clear */
1101 lwsync
1102 /* Clear the pending doorbell request */
1103 li r0, 0
1104 stb r0, VCPU_DBELL_REQ(r4)
19ccb76a 1105
27025a60
LPF
1106/*
1107 * Required state:
1108 * R4 = vcpu
1109 * R10: value for HSRR0
1110 * R11: value for HSRR1
1111 * R13 = PACA
1112 */
de56a948 1113fast_guest_return:
4619ac88
PM
1114 li r0,0
1115 stb r0,VCPU_CEDED(r4) /* cancel cede */
de56a948
PM
1116 mtspr SPRN_HSRR0,r10
1117 mtspr SPRN_HSRR1,r11
1118
1119 /* Activate guest mode, so faults get handled by KVM */
44a3add8 1120 li r9, KVM_GUEST_MODE_GUEST_HV
de56a948
PM
1121 stb r9, HSTATE_IN_GUEST(r13)
1122
b6c295df
PM
1123#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1124 /* Accumulate timing */
1125 addi r3, r4, VCPU_TB_GUEST
1126 bl kvmhv_accumulate_time
1127#endif
1128
de56a948
PM
1129 /* Enter guest */
1130
0acb9111
PM
1131BEGIN_FTR_SECTION
1132 ld r5, VCPU_CFAR(r4)
1133 mtspr SPRN_CFAR, r5
1134END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
4b8473c9
PM
1135BEGIN_FTR_SECTION
1136 ld r0, VCPU_PPR(r4)
1137END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
0acb9111 1138
de56a948
PM
1139 ld r5, VCPU_LR(r4)
1140 lwz r6, VCPU_CR(r4)
1141 mtlr r5
1142 mtcr r6
1143
c75df6f9
MN
1144 ld r1, VCPU_GPR(R1)(r4)
1145 ld r2, VCPU_GPR(R2)(r4)
1146 ld r3, VCPU_GPR(R3)(r4)
1147 ld r5, VCPU_GPR(R5)(r4)
1148 ld r6, VCPU_GPR(R6)(r4)
1149 ld r7, VCPU_GPR(R7)(r4)
1150 ld r8, VCPU_GPR(R8)(r4)
1151 ld r9, VCPU_GPR(R9)(r4)
1152 ld r10, VCPU_GPR(R10)(r4)
1153 ld r11, VCPU_GPR(R11)(r4)
1154 ld r12, VCPU_GPR(R12)(r4)
1155 ld r13, VCPU_GPR(R13)(r4)
1156
4b8473c9
PM
1157BEGIN_FTR_SECTION
1158 mtspr SPRN_PPR, r0
1159END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
e001fa78
MN
1160
1161/* Move canary into DSISR to check for later */
1162BEGIN_FTR_SECTION
1163 li r0, 0x7fff
1164 mtspr SPRN_HDSISR, r0
1165END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1166
4b8473c9 1167 ld r0, VCPU_GPR(R0)(r4)
c75df6f9 1168 ld r4, VCPU_GPR(R4)(r4)
de56a948
PM
1169
1170 hrfid
1171 b .
1172
b6c295df 1173secondary_too_late:
6af27c84 1174 li r12, 0
b6c295df
PM
1175 cmpdi r4, 0
1176 beq 11f
6af27c84
PM
1177 stw r12, VCPU_TRAP(r4)
1178#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
b6c295df
PM
1179 addi r3, r4, VCPU_TB_RMEXIT
1180 bl kvmhv_accumulate_time
6af27c84 1181#endif
b6c295df
PM
118211: b kvmhv_switch_to_host
1183
b4deba5c
PM
1184no_switch_exit:
1185 HMT_MEDIUM
1186 li r12, 0
1187 b 12f
b6c295df 1188hdec_soon:
6af27c84 1189 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER
b4deba5c 119012: stw r12, VCPU_TRAP(r4)
6af27c84
PM
1191 mr r9, r4
1192#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
b6c295df
PM
1193 addi r3, r4, VCPU_TB_RMEXIT
1194 bl kvmhv_accumulate_time
b6c295df 1195#endif
6af27c84 1196 b guest_exit_cont
b6c295df 1197
de56a948
PM
1198/******************************************************************************
1199 * *
1200 * Exit code *
1201 * *
1202 *****************************************************************************/
1203
1204/*
1205 * We come here from the first-level interrupt handlers.
1206 */
dd96b2c2
AK
1207 .globl kvmppc_interrupt_hv
1208kvmppc_interrupt_hv:
de56a948
PM
1209 /*
1210 * Register contents:
d3918e7f 1211 * R12 = (guest CR << 32) | interrupt vector
de56a948 1212 * R13 = PACA
d3918e7f 1213 * guest R12 saved in shadow VCPU SCRATCH0
a97a65d5 1214 * guest CTR saved in shadow VCPU SCRATCH1 if RELOCATABLE
de56a948
PM
1215 * guest R13 saved in SPRN_SCRATCH0
1216 */
a97a65d5 1217 std r9, HSTATE_SCRATCH2(r13)
44a3add8
PM
1218 lbz r9, HSTATE_IN_GUEST(r13)
1219 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1220 beq kvmppc_bad_host_intr
dd96b2c2
AK
1221#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1222 cmpwi r9, KVM_GUEST_MODE_GUEST
a97a65d5 1223 ld r9, HSTATE_SCRATCH2(r13)
dd96b2c2
AK
1224 beq kvmppc_interrupt_pr
1225#endif
44a3add8
PM
1226 /* We're now back in the host but in guest MMU context */
1227 li r9, KVM_GUEST_MODE_HOST_HV
1228 stb r9, HSTATE_IN_GUEST(r13)
1229
de56a948
PM
1230 ld r9, HSTATE_KVM_VCPU(r13)
1231
1232 /* Save registers */
1233
c75df6f9
MN
1234 std r0, VCPU_GPR(R0)(r9)
1235 std r1, VCPU_GPR(R1)(r9)
1236 std r2, VCPU_GPR(R2)(r9)
1237 std r3, VCPU_GPR(R3)(r9)
1238 std r4, VCPU_GPR(R4)(r9)
1239 std r5, VCPU_GPR(R5)(r9)
1240 std r6, VCPU_GPR(R6)(r9)
1241 std r7, VCPU_GPR(R7)(r9)
1242 std r8, VCPU_GPR(R8)(r9)
a97a65d5 1243 ld r0, HSTATE_SCRATCH2(r13)
c75df6f9
MN
1244 std r0, VCPU_GPR(R9)(r9)
1245 std r10, VCPU_GPR(R10)(r9)
1246 std r11, VCPU_GPR(R11)(r9)
de56a948 1247 ld r3, HSTATE_SCRATCH0(r13)
c75df6f9 1248 std r3, VCPU_GPR(R12)(r9)
d3918e7f
NP
1249 /* CR is in the high half of r12 */
1250 srdi r4, r12, 32
de56a948 1251 stw r4, VCPU_CR(r9)
0acb9111
PM
1252BEGIN_FTR_SECTION
1253 ld r3, HSTATE_CFAR(r13)
1254 std r3, VCPU_CFAR(r9)
1255END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
4b8473c9
PM
1256BEGIN_FTR_SECTION
1257 ld r4, HSTATE_PPR(r13)
1258 std r4, VCPU_PPR(r9)
1259END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
de56a948
PM
1260
1261 /* Restore R1/R2 so we can handle faults */
1262 ld r1, HSTATE_HOST_R1(r13)
1263 ld r2, PACATOC(r13)
1264
1265 mfspr r10, SPRN_SRR0
1266 mfspr r11, SPRN_SRR1
1267 std r10, VCPU_SRR0(r9)
1268 std r11, VCPU_SRR1(r9)
d3918e7f
NP
1269 /* trap is in the low half of r12, clear CR from the high half */
1270 clrldi r12, r12, 32
de56a948
PM
1271 andi. r0, r12, 2 /* need to read HSRR0/1? */
1272 beq 1f
1273 mfspr r10, SPRN_HSRR0
1274 mfspr r11, SPRN_HSRR1
1275 clrrdi r12, r12, 2
12761: std r10, VCPU_PC(r9)
1277 std r11, VCPU_MSR(r9)
1278
1279 GET_SCRATCH0(r3)
1280 mflr r4
c75df6f9 1281 std r3, VCPU_GPR(R13)(r9)
de56a948
PM
1282 std r4, VCPU_LR(r9)
1283
de56a948
PM
1284 stw r12,VCPU_TRAP(r9)
1285
8b24e69f
PM
1286 /*
1287 * Now that we have saved away SRR0/1 and HSRR0/1,
1288 * interrupts are recoverable in principle, so set MSR_RI.
1289 * This becomes important for relocation-on interrupts from
1290 * the guest, which we can get in radix mode on POWER9.
1291 */
1292 li r0, MSR_RI
1293 mtmsrd r0, 1
1294
b6c295df
PM
1295#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1296 addi r3, r9, VCPU_TB_RMINTR
1297 mr r4, r9
1298 bl kvmhv_accumulate_time
1299 ld r5, VCPU_GPR(R5)(r9)
1300 ld r6, VCPU_GPR(R6)(r9)
1301 ld r7, VCPU_GPR(R7)(r9)
1302 ld r8, VCPU_GPR(R8)(r9)
1303#endif
1304
4a157d61 1305 /* Save HEIR (HV emulation assist reg) in emul_inst
697d3899
PM
1306 if this is an HEI (HV emulation interrupt, e40) */
1307 li r3,KVM_INST_FETCH_FAILED
2bf27601 1308 stw r3,VCPU_LAST_INST(r9)
697d3899
PM
1309 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1310 bne 11f
1311 mfspr r3,SPRN_HEIR
4a157d61 131211: stw r3,VCPU_HEIR(r9)
697d3899
PM
1313
1314 /* these are volatile across C function calls */
a97a65d5
NP
1315#ifdef CONFIG_RELOCATABLE
1316 ld r3, HSTATE_SCRATCH1(r13)
1317 mtctr r3
1318#else
697d3899 1319 mfctr r3
a97a65d5 1320#endif
697d3899
PM
1321 mfxer r4
1322 std r3, VCPU_CTR(r9)
c63517c2 1323 std r4, VCPU_XER(r9)
697d3899 1324
697d3899
PM
1325 /* If this is a page table miss then see if it's theirs or ours */
1326 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1327 beq kvmppc_hdsi
342d3db7
PM
1328 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1329 beq kvmppc_hisi
697d3899 1330
de56a948
PM
1331 /* See if this is a leftover HDEC interrupt */
1332 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1333 bne 2f
1334 mfspr r3,SPRN_HDEC
a4faf2e7
PM
1335 EXTEND_HDEC(r3)
1336 cmpdi r3,0
1f09c3ed
PM
1337 mr r4,r9
1338 bge fast_guest_return
de56a948 13392:
697d3899 1340 /* See if this is an hcall we can handle in real mode */
a8606e20
PM
1341 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1342 beq hcall_try_real_mode
de56a948 1343
66feed61
PM
1344 /* Hypervisor doorbell - exit only if host IPI flag set */
1345 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL
1346 bne 3f
bd0fdb19
NP
1347BEGIN_FTR_SECTION
1348 PPC_MSGSYNC
2cde3716 1349 lwsync
bd0fdb19 1350END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
66feed61 1351 lbz r0, HSTATE_HOST_IPI(r13)
06554d9f 1352 cmpwi r0, 0
66feed61
PM
1353 beq 4f
1354 b guest_exit_cont
13553:
769377f7
PM
1356 /* If it's a hypervisor facility unavailable interrupt, save HFSCR */
1357 cmpwi r12, BOOK3S_INTERRUPT_H_FAC_UNAVAIL
1358 bne 14f
1359 mfspr r3, SPRN_HFSCR
1360 std r3, VCPU_HFSCR(r9)
1361 b guest_exit_cont
136214:
54695c30
BH
1363 /* External interrupt ? */
1364 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
1f09c3ed 1365 bne+ guest_exit_cont
54695c30
BH
1366
1367 /* External interrupt, first check for host_ipi. If this is
1368 * set, we know the host wants us out so let's do it now
1369 */
c934243c 1370 bl kvmppc_read_intr
37f55d30
SW
1371
1372 /*
1373 * Restore the active volatile registers after returning from
1374 * a C function.
1375 */
1376 ld r9, HSTATE_KVM_VCPU(r13)
1377 li r12, BOOK3S_INTERRUPT_EXTERNAL
1378
1379 /*
1380 * kvmppc_read_intr return codes:
1381 *
1382 * Exit to host (r3 > 0)
1383 * 1 An interrupt is pending that needs to be handled by the host
1384 * Exit guest and return to host by branching to guest_exit_cont
1385 *
f7af5209
SW
1386 * 2 Passthrough that needs completion in the host
1387 * Exit guest and return to host by branching to guest_exit_cont
1388 * However, we also set r12 to BOOK3S_INTERRUPT_HV_RM_HARD
1389 * to indicate to the host to complete handling the interrupt
1390 *
37f55d30
SW
1391 * Before returning to guest, we check if any CPU is heading out
1392 * to the host and if so, we head out also. If no CPUs are heading
1393 * check return values <= 0.
1394 *
1395 * Return to guest (r3 <= 0)
1396 * 0 No external interrupt is pending
1397 * -1 A guest wakeup IPI (which has now been cleared)
1398 * In either case, we return to guest to deliver any pending
1399 * guest interrupts.
e3c13e56
SW
1400 *
1401 * -2 A PCI passthrough external interrupt was handled
1402 * (interrupt was delivered directly to guest)
1403 * Return to guest to deliver any pending guest interrupts.
37f55d30
SW
1404 */
1405
f7af5209
SW
1406 cmpdi r3, 1
1407 ble 1f
1408
1409 /* Return code = 2 */
1410 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
1411 stw r12, VCPU_TRAP(r9)
1412 b guest_exit_cont
1413
14141: /* Return code <= 1 */
c934243c 1415 cmpdi r3, 0
1f09c3ed 1416 bgt guest_exit_cont
54695c30 1417
37f55d30 1418 /* Return code <= 0 */
66feed61 14194: ld r5, HSTATE_KVM_VCORE(r13)
4619ac88
PM
1420 lwz r0, VCORE_ENTRY_EXIT(r5)
1421 cmpwi r0, 0x100
e3bbbbfa 1422 mr r4, r9
1f09c3ed 1423 blt deliver_guest_interrupt
de56a948 1424
b4072df4 1425guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
43ff3f65
PM
1426 /* Save more register state */
1427 mfdar r6
1428 mfdsisr r7
1429 std r6, VCPU_DAR(r9)
1430 stw r7, VCPU_DSISR(r9)
1431 /* don't overwrite fault_dar/fault_dsisr if HDSI */
1432 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1433 beq mc_cont
1434 std r6, VCPU_FAULT_DAR(r9)
1435 stw r7, VCPU_FAULT_DSISR(r9)
1436
1437 /* See if it is a machine check */
1438 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1439 beq machine_check_realmode
1440mc_cont:
1441#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1442 addi r3, r9, VCPU_TB_RMEXIT
1443 mr r4, r9
1444 bl kvmhv_accumulate_time
1445#endif
5af50993
BH
1446#ifdef CONFIG_KVM_XICS
1447 /* We are exiting, pull the VP from the XIVE */
1448 lwz r0, VCPU_XIVE_PUSHED(r9)
1449 cmpwi cr0, r0, 0
1450 beq 1f
1451 li r7, TM_SPC_PULL_OS_CTX
1452 li r6, TM_QW1_OS
1453 mfmsr r0
1454 andi. r0, r0, MSR_IR /* in real mode? */
1455 beq 2f
1456 ld r10, HSTATE_XIVE_TIMA_VIRT(r13)
1457 cmpldi cr0, r10, 0
1458 beq 1f
1459 /* First load to pull the context, we ignore the value */
5af50993 1460 eieio
ad98dd1a 1461 lwzx r11, r7, r10
5af50993
BH
1462 /* Second load to recover the context state (Words 0 and 1) */
1463 ldx r11, r6, r10
1464 b 3f
14652: ld r10, HSTATE_XIVE_TIMA_PHYS(r13)
1466 cmpldi cr0, r10, 0
1467 beq 1f
1468 /* First load to pull the context, we ignore the value */
5af50993 1469 eieio
ad98dd1a 1470 lwzcix r11, r7, r10
5af50993
BH
1471 /* Second load to recover the context state (Words 0 and 1) */
1472 ldcix r11, r6, r10
14733: std r11, VCPU_XIVE_SAVED_STATE(r9)
1474 /* Fixup some of the state for the next load */
1475 li r10, 0
1476 li r0, 0xff
1477 stw r10, VCPU_XIVE_PUSHED(r9)
1478 stb r10, (VCPU_XIVE_SAVED_STATE+3)(r9)
1479 stb r0, (VCPU_XIVE_SAVED_STATE+4)(r9)
ad98dd1a 1480 eieio
5af50993
BH
14811:
1482#endif /* CONFIG_KVM_XICS */
b4072df4 1483
7e022e71 1484 mr r3, r12
6af27c84
PM
1485 /* Increment exit count, poke other threads to exit */
1486 bl kvmhv_commence_exit
eddb60fb
PM
1487 nop
1488 ld r9, HSTATE_KVM_VCPU(r13)
1489 lwz r12, VCPU_TRAP(r9)
6af27c84 1490
ec257165
PM
1491 /* Stop others sending VCPU interrupts to this physical CPU */
1492 li r0, -1
1493 stw r0, VCPU_CPU(r9)
1494 stw r0, VCPU_THREAD_CPU(r9)
1495
de56a948 1496 /* Save guest CTRL register, set runlatch to 1 */
6af27c84 1497 mfspr r6,SPRN_CTRLF
de56a948
PM
1498 stw r6,VCPU_CTRL(r9)
1499 andi. r0,r6,1
1500 bne 4f
1501 ori r6,r6,1
1502 mtspr SPRN_CTRLT,r6
15034:
a25bd72b 1504 /* Check if we are running hash or radix and store it in cr2 */
f4c51f84
PM
1505 ld r5, VCPU_KVM(r9)
1506 lbz r0, KVM_RADIX(r5)
a25bd72b
BH
1507 cmpwi cr2,r0,0
1508
1509 /* Read the guest SLB and save it away */
f4c51f84 1510 li r5, 0
a25bd72b 1511 bne cr2, 3f /* for radix, save 0 entries */
de56a948
PM
1512 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1513 mtctr r0
1514 li r6,0
1515 addi r7,r9,VCPU_SLB
de56a948
PM
15161: slbmfee r8,r6
1517 andis. r0,r8,SLB_ESID_V@h
1518 beq 2f
1519 add r8,r8,r6 /* put index in */
1520 slbmfev r3,r6
1521 std r8,VCPU_SLB_E(r7)
1522 std r3,VCPU_SLB_V(r7)
1523 addi r7,r7,VCPU_SLB_SIZE
1524 addi r5,r5,1
15252: addi r6,r6,1
1526 bdnz 1b
f4c51f84 15273: stw r5,VCPU_SLB_MAX(r9)
de56a948
PM
1528
1529 /*
1530 * Save the guest PURR/SPURR
1531 */
1532 mfspr r5,SPRN_PURR
1533 mfspr r6,SPRN_SPURR
1534 ld r7,VCPU_PURR(r9)
1535 ld r8,VCPU_SPURR(r9)
1536 std r5,VCPU_PURR(r9)
1537 std r6,VCPU_SPURR(r9)
1538 subf r5,r7,r5
1539 subf r6,r8,r6
1540
1541 /*
1542 * Restore host PURR/SPURR and add guest times
1543 * so that the time in the guest gets accounted.
1544 */
1545 ld r3,HSTATE_PURR(r13)
1546 ld r4,HSTATE_SPURR(r13)
1547 add r3,r3,r5
1548 add r4,r4,r6
1549 mtspr SPRN_PURR,r3
1550 mtspr SPRN_SPURR,r4
1551
e0b7ec05 1552 /* Save DEC */
1bc3fe81 1553 ld r3, HSTATE_KVM_VCORE(r13)
e0b7ec05
PM
1554 mfspr r5,SPRN_DEC
1555 mftb r6
1bc3fe81
PM
1556 /* On P9, if the guest has large decr enabled, don't sign extend */
1557BEGIN_FTR_SECTION
1558 ld r4, VCORE_LPCR(r3)
1559 andis. r4, r4, LPCR_LD@h
1560 bne 16f
1561END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
e0b7ec05 1562 extsw r5,r5
1bc3fe81 156316: add r5,r5,r6
c5fb80d3 1564 /* r5 is a guest timebase value here, convert to host TB */
c5fb80d3
PM
1565 ld r4,VCORE_TB_OFFSET(r3)
1566 subf r5,r4,r5
e0b7ec05
PM
1567 std r5,VCPU_DEC_EXPIRES(r9)
1568
b005255e
MN
1569BEGIN_FTR_SECTION
1570 b 8f
1571END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
b005255e
MN
1572 /* Save POWER8-specific registers */
1573 mfspr r5, SPRN_IAMR
1574 mfspr r6, SPRN_PSPB
1575 mfspr r7, SPRN_FSCR
1576 std r5, VCPU_IAMR(r9)
1577 stw r6, VCPU_PSPB(r9)
1578 std r7, VCPU_FSCR(r9)
1579 mfspr r5, SPRN_IC
b005255e
MN
1580 mfspr r7, SPRN_TAR
1581 std r5, VCPU_IC(r9)
b005255e 1582 std r7, VCPU_TAR(r9)
7b490411 1583 mfspr r8, SPRN_EBBHR
b005255e
MN
1584 std r8, VCPU_EBBHR(r9)
1585 mfspr r5, SPRN_EBBRR
1586 mfspr r6, SPRN_BESCR
83677f55
PM
1587 mfspr r7, SPRN_PID
1588 mfspr r8, SPRN_WORT
b005255e
MN
1589 std r5, VCPU_EBBRR(r9)
1590 std r6, VCPU_BESCR(r9)
83677f55
PM
1591 stw r7, VCPU_GUEST_PID(r9)
1592 std r8, VCPU_WORT(r9)
1593BEGIN_FTR_SECTION
b005255e
MN
1594 mfspr r5, SPRN_TCSCR
1595 mfspr r6, SPRN_ACOP
83677f55
PM
1596 mfspr r7, SPRN_CSIGR
1597 mfspr r8, SPRN_TACR
b005255e
MN
1598 std r5, VCPU_TCSCR(r9)
1599 std r6, VCPU_ACOP(r9)
83677f55
PM
1600 std r7, VCPU_CSIGR(r9)
1601 std r8, VCPU_TACR(r9)
e9cf1e08
PM
1602FTR_SECTION_ELSE
1603 mfspr r5, SPRN_TIDR
1604 mfspr r6, SPRN_PSSCR
1605 std r5, VCPU_TID(r9)
1606 rldicl r6, r6, 4, 50 /* r6 &= PSSCR_GUEST_VIS */
1607 rotldi r6, r6, 60
1608 std r6, VCPU_PSSCR(r9)
769377f7
PM
1609 /* Restore host HFSCR value */
1610 ld r7, STACK_SLOT_HFSCR(r1)
1611 mtspr SPRN_HFSCR, r7
e9cf1e08 1612ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
ccec4456
PM
1613 /*
1614 * Restore various registers to 0, where non-zero values
1615 * set by the guest could disrupt the host.
1616 */
1617 li r0, 0
4c3bb4cc 1618 mtspr SPRN_PSPB, r0
ccec4456 1619 mtspr SPRN_WORT, r0
83677f55 1620BEGIN_FTR_SECTION
4c3bb4cc 1621 mtspr SPRN_IAMR, r0
83677f55 1622 mtspr SPRN_TCSCR, r0
ccec4456
PM
1623 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */
1624 li r0, 1
1625 sldi r0, r0, 31
1626 mtspr SPRN_MMCRS, r0
83677f55 1627END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
b005255e
MN
16288:
1629
e0b7ec05 1630 /* Save and reset AMR and UAMOR before turning on the MMU */
e0b7ec05
PM
1631 mfspr r5,SPRN_AMR
1632 mfspr r6,SPRN_UAMOR
1633 std r5,VCPU_AMR(r9)
1634 std r6,VCPU_UAMOR(r9)
1635 li r6,0
1636 mtspr SPRN_AMR,r6
4c3bb4cc 1637 mtspr SPRN_UAMOR, r6
e0b7ec05
PM
1638
1639 /* Switch DSCR back to host value */
e0b7ec05
PM
1640 mfspr r8, SPRN_DSCR
1641 ld r7, HSTATE_DSCR(r13)
1642 std r8, VCPU_DSCR(r9)
1643 mtspr SPRN_DSCR, r7
e0b7ec05
PM
1644
1645 /* Save non-volatile GPRs */
1646 std r14, VCPU_GPR(R14)(r9)
1647 std r15, VCPU_GPR(R15)(r9)
1648 std r16, VCPU_GPR(R16)(r9)
1649 std r17, VCPU_GPR(R17)(r9)
1650 std r18, VCPU_GPR(R18)(r9)
1651 std r19, VCPU_GPR(R19)(r9)
1652 std r20, VCPU_GPR(R20)(r9)
1653 std r21, VCPU_GPR(R21)(r9)
1654 std r22, VCPU_GPR(R22)(r9)
1655 std r23, VCPU_GPR(R23)(r9)
1656 std r24, VCPU_GPR(R24)(r9)
1657 std r25, VCPU_GPR(R25)(r9)
1658 std r26, VCPU_GPR(R26)(r9)
1659 std r27, VCPU_GPR(R27)(r9)
1660 std r28, VCPU_GPR(R28)(r9)
1661 std r29, VCPU_GPR(R29)(r9)
1662 std r30, VCPU_GPR(R30)(r9)
1663 std r31, VCPU_GPR(R31)(r9)
1664
1665 /* Save SPRGs */
1666 mfspr r3, SPRN_SPRG0
1667 mfspr r4, SPRN_SPRG1
1668 mfspr r5, SPRN_SPRG2
1669 mfspr r6, SPRN_SPRG3
1670 std r3, VCPU_SPRG0(r9)
1671 std r4, VCPU_SPRG1(r9)
1672 std r5, VCPU_SPRG2(r9)
1673 std r6, VCPU_SPRG3(r9)
1674
1675 /* save FP state */
1676 mr r3, r9
1677 bl kvmppc_save_fp
de56a948 1678
0a8eccef
PM
1679#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1680BEGIN_FTR_SECTION
67f8a8c1
PM
1681 /*
1682 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
1683 */
f024ee09
PM
1684 bl kvmppc_save_tm
1685END_FTR_SECTION_IFSET(CPU_FTR_TM)
0a8eccef
PM
1686#endif
1687
e0b7ec05
PM
1688 /* Increment yield count if they have a VPA */
1689 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1690 cmpdi r8, 0
1691 beq 25f
0865a583
AG
1692 li r4, LPPACA_YIELDCOUNT
1693 LWZX_BE r3, r8, r4
e0b7ec05 1694 addi r3, r3, 1
0865a583 1695 STWX_BE r3, r8, r4
e0b7ec05
PM
1696 li r3, 1
1697 stb r3, VCPU_VPA_DIRTY(r9)
169825:
1699 /* Save PMU registers if requested */
1700 /* r8 and cr0.eq are live here */
9bc01a9b
PM
1701BEGIN_FTR_SECTION
1702 /*
1703 * POWER8 seems to have a hardware bug where setting
1704 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1705 * when some counters are already negative doesn't seem
1706 * to cause a performance monitor alert (and hence interrupt).
1707 * The effect of this is that when saving the PMU state,
1708 * if there is no PMU alert pending when we read MMCR0
1709 * before freezing the counters, but one becomes pending
1710 * before we read the counters, we lose it.
1711 * To work around this, we need a way to freeze the counters
1712 * before reading MMCR0. Normally, freezing the counters
1713 * is done by writing MMCR0 (to set MMCR0[FC]) which
1714 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1715 * we can also freeze the counters using MMCR2, by writing
1716 * 1s to all the counter freeze condition bits (there are
1717 * 9 bits each for 6 counters).
1718 */
1719 li r3, -1 /* set all freeze bits */
1720 clrrdi r3, r3, 10
1721 mfspr r10, SPRN_MMCR2
1722 mtspr SPRN_MMCR2, r3
1723 isync
1724END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05
PM
1725 li r3, 1
1726 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1727 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1728 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
1729 mfspr r6, SPRN_MMCRA
c17b98cf 1730 /* Clear MMCRA in order to disable SDAR updates */
e0b7ec05
PM
1731 li r7, 0
1732 mtspr SPRN_MMCRA, r7
e0b7ec05
PM
1733 isync
1734 beq 21f /* if no VPA, save PMU stuff anyway */
1735 lbz r7, LPPACA_PMCINUSE(r8)
1736 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1737 bne 21f
1738 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1739 b 22f
174021: mfspr r5, SPRN_MMCR1
1741 mfspr r7, SPRN_SIAR
1742 mfspr r8, SPRN_SDAR
1743 std r4, VCPU_MMCR(r9)
1744 std r5, VCPU_MMCR + 8(r9)
1745 std r6, VCPU_MMCR + 16(r9)
9bc01a9b
PM
1746BEGIN_FTR_SECTION
1747 std r10, VCPU_MMCR + 24(r9)
1748END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05
PM
1749 std r7, VCPU_SIAR(r9)
1750 std r8, VCPU_SDAR(r9)
1751 mfspr r3, SPRN_PMC1
1752 mfspr r4, SPRN_PMC2
1753 mfspr r5, SPRN_PMC3
1754 mfspr r6, SPRN_PMC4
1755 mfspr r7, SPRN_PMC5
1756 mfspr r8, SPRN_PMC6
e0b7ec05
PM
1757 stw r3, VCPU_PMC(r9)
1758 stw r4, VCPU_PMC + 4(r9)
1759 stw r5, VCPU_PMC + 8(r9)
1760 stw r6, VCPU_PMC + 12(r9)
1761 stw r7, VCPU_PMC + 16(r9)
1762 stw r8, VCPU_PMC + 20(r9)
b005255e 1763BEGIN_FTR_SECTION
b005255e 1764 mfspr r5, SPRN_SIER
83677f55
PM
1765 std r5, VCPU_SIER(r9)
1766BEGIN_FTR_SECTION_NESTED(96)
b005255e
MN
1767 mfspr r6, SPRN_SPMC1
1768 mfspr r7, SPRN_SPMC2
1769 mfspr r8, SPRN_MMCRS
b005255e
MN
1770 stw r6, VCPU_PMC + 24(r9)
1771 stw r7, VCPU_PMC + 28(r9)
1772 std r8, VCPU_MMCR + 32(r9)
1773 lis r4, 0x8000
1774 mtspr SPRN_MMCRS, r4
83677f55 1775END_FTR_SECTION_NESTED(CPU_FTR_ARCH_300, 0, 96)
b005255e 1776END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05 177722:
de56a948 1778
e9cf1e08 1779 /* Restore host values of some registers */
7ceaa6dc
PM
1780BEGIN_FTR_SECTION
1781 ld r5, STACK_SLOT_CIABR(r1)
1782 ld r6, STACK_SLOT_DAWR(r1)
1783 ld r7, STACK_SLOT_DAWRX(r1)
1784 mtspr SPRN_CIABR, r5
1785 mtspr SPRN_DAWR, r6
1786 mtspr SPRN_DAWRX, r7
1787END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e9cf1e08
PM
1788BEGIN_FTR_SECTION
1789 ld r5, STACK_SLOT_TID(r1)
1790 ld r6, STACK_SLOT_PSSCR(r1)
f4c51f84 1791 ld r7, STACK_SLOT_PID(r1)
4c3bb4cc 1792 ld r8, STACK_SLOT_IAMR(r1)
e9cf1e08
PM
1793 mtspr SPRN_TIDR, r5
1794 mtspr SPRN_PSSCR, r6
f4c51f84 1795 mtspr SPRN_PID, r7
4c3bb4cc 1796 mtspr SPRN_IAMR, r8
e9cf1e08 1797END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
a25bd72b
BH
1798
1799#ifdef CONFIG_PPC_RADIX_MMU
1800 /*
1801 * Are we running hash or radix ?
1802 */
67f8a8c1
PM
1803 ld r5, VCPU_KVM(r9)
1804 lbz r0, KVM_RADIX(r5)
1805 cmpwi cr2, r0, 0
1806 beq cr2, 3f
a25bd72b
BH
1807
1808 /* Radix: Handle the case where the guest used an illegal PID */
1809 LOAD_REG_ADDR(r4, mmu_base_pid)
1810 lwz r3, VCPU_GUEST_PID(r9)
1811 lwz r5, 0(r4)
1812 cmpw cr0,r3,r5
1813 blt 2f
1814
1815 /*
1816 * Illegal PID, the HW might have prefetched and cached in the TLB
1817 * some translations for the LPID 0 / guest PID combination which
1818 * Linux doesn't know about, so we need to flush that PID out of
1819 * the TLB. First we need to set LPIDR to 0 so tlbiel applies to
1820 * the right context.
1821 */
1822 li r0,0
1823 mtspr SPRN_LPID,r0
1824 isync
1825
1826 /* Then do a congruence class local flush */
1827 ld r6,VCPU_KVM(r9)
1828 lwz r0,KVM_TLB_SETS(r6)
1829 mtctr r0
1830 li r7,0x400 /* IS field = 0b01 */
1831 ptesync
1832 sldi r0,r3,32 /* RS has PID */
18331: PPC_TLBIEL(7,0,2,1,1) /* RIC=2, PRS=1, R=1 */
1834 addi r7,r7,0x1000
1835 bdnz 1b
1836 ptesync
1837
18382: /* Flush the ERAT on radix P9 DD1 guest exit */
f11f6f79
PM
1839BEGIN_FTR_SECTION
1840 PPC_INVALIDATE_ERAT
1841END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
a25bd72b
BH
1842 b 4f
1843#endif /* CONFIG_PPC_RADIX_MMU */
e9cf1e08 1844
a25bd72b
BH
1845 /* Hash: clear out SLB */
18463: li r5,0
1847 slbmte r5,r5
1848 slbia
1849 ptesync
18504:
9e368f29 1851 /*
c17b98cf 1852 * POWER7/POWER8 guest -> host partition switch code.
9e368f29
PM
1853 * We don't have to lock against tlbies but we do
1854 * have to coordinate the hardware threads.
1855 */
b6c295df 1856kvmhv_switch_to_host:
371fefd6 1857 /* Secondary threads wait for primary to do partition switch */
6af27c84 1858 ld r5,HSTATE_KVM_VCORE(r13)
e0b7ec05
PM
1859 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1860 lbz r3,HSTATE_PTID(r13)
371fefd6
PM
1861 cmpwi r3,0
1862 beq 15f
1863 HMT_LOW
186413: lbz r3,VCORE_IN_GUEST(r5)
1865 cmpwi r3,0
1866 bne 13b
1867 HMT_MEDIUM
1868 b 16f
1869
1870 /* Primary thread waits for all the secondaries to exit guest */
187115: lwz r3,VCORE_ENTRY_EXIT(r5)
b4deba5c 1872 rlwinm r0,r3,32-8,0xff
371fefd6
PM
1873 clrldi r3,r3,56
1874 cmpw r3,r0
1875 bne 15b
1876 isync
1877
b4deba5c
PM
1878 /* Did we actually switch to the guest at all? */
1879 lbz r6, VCORE_IN_GUEST(r5)
1880 cmpwi r6, 0
1881 beq 19f
1882
371fefd6 1883 /* Primary thread switches back to host partition */
de56a948 1884 lwz r7,KVM_HOST_LPID(r4)
7a84084c
PM
1885BEGIN_FTR_SECTION
1886 ld r6,KVM_HOST_SDR1(r4)
de56a948
PM
1887 li r8,LPID_RSVD /* switch to reserved LPID */
1888 mtspr SPRN_LPID,r8
1889 ptesync
7a84084c
PM
1890 mtspr SPRN_SDR1,r6 /* switch to host page table */
1891END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
de56a948
PM
1892 mtspr SPRN_LPID,r7
1893 isync
93b0f4dc 1894
b005255e 1895BEGIN_FTR_SECTION
88b02cf9 1896 /* DPDES and VTB are shared between threads */
b005255e 1897 mfspr r7, SPRN_DPDES
88b02cf9 1898 mfspr r8, SPRN_VTB
b005255e 1899 std r7, VCORE_DPDES(r5)
88b02cf9 1900 std r8, VCORE_VTB(r5)
b005255e
MN
1901 /* clear DPDES so we don't get guest doorbells in the host */
1902 li r8, 0
1903 mtspr SPRN_DPDES, r8
1904END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1905
fd7bacbc
MS
1906 /* If HMI, call kvmppc_realmode_hmi_handler() */
1907 cmpwi r12, BOOK3S_INTERRUPT_HMI
1908 bne 27f
1909 bl kvmppc_realmode_hmi_handler
1910 nop
1911 li r12, BOOK3S_INTERRUPT_HMI
1912 /*
1913 * At this point kvmppc_realmode_hmi_handler would have resync-ed
1914 * the TB. Hence it is not required to subtract guest timebase
1915 * offset from timebase. So, skip it.
1916 *
1917 * Also, do not call kvmppc_subcore_exit_guest() because it has
1918 * been invoked as part of kvmppc_realmode_hmi_handler().
1919 */
1920 b 30f
1921
192227:
93b0f4dc
PM
1923 /* Subtract timebase offset from timebase */
1924 ld r8,VCORE_TB_OFFSET(r5)
1925 cmpdi r8,0
1926 beq 17f
c5fb80d3 1927 mftb r6 /* current guest timebase */
93b0f4dc
PM
1928 subf r8,r8,r6
1929 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1930 mftb r7 /* check if lower 24 bits overflowed */
1931 clrldi r6,r6,40
1932 clrldi r7,r7,40
1933 cmpld r7,r6
1934 bge 17f
1935 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1936 mtspr SPRN_TBU40,r8
1937
fd7bacbc
MS
193817: bl kvmppc_subcore_exit_guest
1939 nop
194030: ld r5,HSTATE_KVM_VCORE(r13)
1941 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1942
388cc6e1 1943 /* Reset PCR */
fd7bacbc 1944 ld r0, VCORE_PCR(r5)
388cc6e1
PM
1945 cmpdi r0, 0
1946 beq 18f
1947 li r0, 0
1948 mtspr SPRN_PCR, r0
194918:
93b0f4dc 1950 /* Signal secondary CPUs to continue */
371fefd6 1951 stb r0,VCORE_IN_GUEST(r5)
b4deba5c 195219: lis r8,0x7fff /* MAX_INT@h */
de56a948
PM
1953 mtspr SPRN_HDEC,r8
1954
c0101509
PM
195516:
1956BEGIN_FTR_SECTION
1957 /* On POWER9 with HPT-on-radix we need to wait for all other threads */
1958 ld r3, HSTATE_SPLIT_MODE(r13)
1959 cmpdi r3, 0
1960 beq 47f
1961 lwz r8, KVM_SPLIT_DO_RESTORE(r3)
1962 cmpwi r8, 0
1963 beq 47f
1964 stw r12, STACK_SLOT_TRAP(r1)
1965 bl kvmhv_p9_restore_lpcr
1966 nop
1967 lwz r12, STACK_SLOT_TRAP(r1)
1968 b 48f
196947:
1970END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
1971 ld r8,KVM_HOST_LPCR(r4)
de56a948
PM
1972 mtspr SPRN_LPCR,r8
1973 isync
c0101509 197448:
de56a948 1975 /* load host SLB entries */
f4c51f84
PM
1976BEGIN_MMU_FTR_SECTION
1977 b 0f
1978END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
c17b98cf 1979 ld r8,PACA_SLBSHADOWPTR(r13)
de56a948
PM
1980
1981 .rept SLB_NUM_BOLTED
0865a583
AG
1982 li r3, SLBSHADOW_SAVEAREA
1983 LDX_BE r5, r8, r3
1984 addi r3, r3, 8
1985 LDX_BE r6, r8, r3
de56a948
PM
1986 andis. r7,r5,SLB_ESID_V@h
1987 beq 1f
1988 slbmte r6,r5
19891: addi r8,r8,16
1990 .endr
f4c51f84 19910:
b6c295df
PM
1992#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1993 /* Finish timing, if we have a vcpu */
1994 ld r4, HSTATE_KVM_VCPU(r13)
1995 cmpdi r4, 0
1996 li r3, 0
1997 beq 2f
1998 bl kvmhv_accumulate_time
19992:
2000#endif
44a3add8
PM
2001 /* Unset guest mode */
2002 li r0, KVM_GUEST_MODE_NONE
2003 stb r0, HSTATE_IN_GUEST(r13)
2004
7ceaa6dc
PM
2005 ld r0, SFS+PPC_LR_STKOFF(r1)
2006 addi r1, r1, SFS
218309b7
PM
2007 mtlr r0
2008 blr
b4072df4 2009
697d3899
PM
2010/*
2011 * Check whether an HDSI is an HPTE not found fault or something else.
2012 * If it is an HPTE not found fault that is due to the guest accessing
2013 * a page that they have mapped but which we have paged out, then
2014 * we continue on with the guest exit path. In all other cases,
2015 * reflect the HDSI to the guest as a DSI.
2016 */
2017kvmppc_hdsi:
f4c51f84
PM
2018 ld r3, VCPU_KVM(r9)
2019 lbz r0, KVM_RADIX(r3)
697d3899
PM
2020 mfspr r4, SPRN_HDAR
2021 mfspr r6, SPRN_HDSISR
e001fa78
MN
2022BEGIN_FTR_SECTION
2023 /* Look for DSISR canary. If we find it, retry instruction */
2024 cmpdi r6, 0x7fff
2025 beq 6f
2026END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2027 cmpwi r0, 0
f4c51f84 2028 bne .Lradix_hdsi /* on radix, just save DAR/DSISR/ASDR */
4cf302bc
PM
2029 /* HPTE not found fault or protection fault? */
2030 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
697d3899 2031 beq 1f /* if not, send it to the guest */
4e5acdc2
PM
2032 andi. r0, r11, MSR_DR /* data relocation enabled? */
2033 beq 3f
ef8c640c
PM
2034BEGIN_FTR_SECTION
2035 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2036 b 4f
2037END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
697d3899 2038 clrrdi r0, r4, 28
c75df6f9 2039 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
cf29b215
PM
2040 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT
2041 bne 7f /* if no SLB entry found */
697d3899
PM
20424: std r4, VCPU_FAULT_DAR(r9)
2043 stw r6, VCPU_FAULT_DSISR(r9)
2044
2045 /* Search the hash table. */
2046 mr r3, r9 /* vcpu pointer */
342d3db7 2047 li r7, 1 /* data fault */
b1576fec 2048 bl kvmppc_hpte_hv_fault
697d3899
PM
2049 ld r9, HSTATE_KVM_VCPU(r13)
2050 ld r10, VCPU_PC(r9)
2051 ld r11, VCPU_MSR(r9)
2052 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
2053 cmpdi r3, 0 /* retry the instruction */
2054 beq 6f
2055 cmpdi r3, -1 /* handle in kernel mode */
b4072df4 2056 beq guest_exit_cont
697d3899
PM
2057 cmpdi r3, -2 /* MMIO emulation; need instr word */
2058 beq 2f
2059
cf29b215 2060 /* Synthesize a DSI (or DSegI) for the guest */
697d3899
PM
2061 ld r4, VCPU_FAULT_DAR(r9)
2062 mr r6, r3
cf29b215 20631: li r0, BOOK3S_INTERRUPT_DATA_STORAGE
697d3899 2064 mtspr SPRN_DSISR, r6
cf29b215 20657: mtspr SPRN_DAR, r4
697d3899
PM
2066 mtspr SPRN_SRR0, r10
2067 mtspr SPRN_SRR1, r11
cf29b215 2068 mr r10, r0
e4e38121 2069 bl kvmppc_msr_interrupt
b4072df4 2070fast_interrupt_c_return:
697d3899 20716: ld r7, VCPU_CTR(r9)
c63517c2 2072 ld r8, VCPU_XER(r9)
697d3899
PM
2073 mtctr r7
2074 mtxer r8
2075 mr r4, r9
2076 b fast_guest_return
2077
20783: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
2079 ld r5, KVM_VRMA_SLB_V(r5)
2080 b 4b
2081
2082 /* If this is for emulated MMIO, load the instruction word */
20832: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
2084
2085 /* Set guest mode to 'jump over instruction' so if lwz faults
2086 * we'll just continue at the next IP. */
2087 li r0, KVM_GUEST_MODE_SKIP
2088 stb r0, HSTATE_IN_GUEST(r13)
2089
2090 /* Do the access with MSR:DR enabled */
2091 mfmsr r3
2092 ori r4, r3, MSR_DR /* Enable paging for data */
2093 mtmsrd r4
2094 lwz r8, 0(r10)
2095 mtmsrd r3
2096
2097 /* Store the result */
2098 stw r8, VCPU_LAST_INST(r9)
2099
2100 /* Unset guest mode. */
44a3add8 2101 li r0, KVM_GUEST_MODE_HOST_HV
697d3899 2102 stb r0, HSTATE_IN_GUEST(r13)
b4072df4 2103 b guest_exit_cont
de56a948 2104
f4c51f84
PM
2105.Lradix_hdsi:
2106 std r4, VCPU_FAULT_DAR(r9)
2107 stw r6, VCPU_FAULT_DSISR(r9)
2108.Lradix_hisi:
2109 mfspr r5, SPRN_ASDR
2110 std r5, VCPU_FAULT_GPA(r9)
2111 b guest_exit_cont
2112
342d3db7
PM
2113/*
2114 * Similarly for an HISI, reflect it to the guest as an ISI unless
2115 * it is an HPTE not found fault for a page that we have paged out.
2116 */
2117kvmppc_hisi:
f4c51f84
PM
2118 ld r3, VCPU_KVM(r9)
2119 lbz r0, KVM_RADIX(r3)
2120 cmpwi r0, 0
2121 bne .Lradix_hisi /* for radix, just save ASDR */
342d3db7
PM
2122 andis. r0, r11, SRR1_ISI_NOPT@h
2123 beq 1f
4e5acdc2
PM
2124 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
2125 beq 3f
ef8c640c
PM
2126BEGIN_FTR_SECTION
2127 mfspr r5, SPRN_ASDR /* on POWER9, use ASDR to get VSID */
2128 b 4f
2129END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
342d3db7 2130 clrrdi r0, r10, 28
c75df6f9 2131 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
cf29b215
PM
2132 li r0, BOOK3S_INTERRUPT_INST_SEGMENT
2133 bne 7f /* if no SLB entry found */
342d3db7
PM
21344:
2135 /* Search the hash table. */
2136 mr r3, r9 /* vcpu pointer */
2137 mr r4, r10
2138 mr r6, r11
2139 li r7, 0 /* instruction fault */
b1576fec 2140 bl kvmppc_hpte_hv_fault
342d3db7
PM
2141 ld r9, HSTATE_KVM_VCPU(r13)
2142 ld r10, VCPU_PC(r9)
2143 ld r11, VCPU_MSR(r9)
2144 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
2145 cmpdi r3, 0 /* retry the instruction */
b4072df4 2146 beq fast_interrupt_c_return
342d3db7 2147 cmpdi r3, -1 /* handle in kernel mode */
b4072df4 2148 beq guest_exit_cont
342d3db7 2149
cf29b215 2150 /* Synthesize an ISI (or ISegI) for the guest */
342d3db7 2151 mr r11, r3
cf29b215
PM
21521: li r0, BOOK3S_INTERRUPT_INST_STORAGE
21537: mtspr SPRN_SRR0, r10
342d3db7 2154 mtspr SPRN_SRR1, r11
cf29b215 2155 mr r10, r0
e4e38121 2156 bl kvmppc_msr_interrupt
b4072df4 2157 b fast_interrupt_c_return
342d3db7
PM
2158
21593: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
2160 ld r5, KVM_VRMA_SLB_V(r6)
2161 b 4b
2162
a8606e20
PM
2163/*
2164 * Try to handle an hcall in real mode.
2165 * Returns to the guest if we handle it, or continues on up to
2166 * the kernel if we can't (i.e. if we don't have a handler for
2167 * it, or if the handler returns H_TOO_HARD).
1f09c3ed
PM
2168 *
2169 * r5 - r8 contain hcall args,
2170 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca
a8606e20 2171 */
a8606e20 2172hcall_try_real_mode:
c75df6f9 2173 ld r3,VCPU_GPR(R3)(r9)
a8606e20 2174 andi. r0,r11,MSR_PR
27025a60
LPF
2175 /* sc 1 from userspace - reflect to guest syscall */
2176 bne sc_1_fast_return
a8606e20
PM
2177 clrrdi r3,r3,2
2178 cmpldi r3,hcall_real_table_end - hcall_real_table
b4072df4 2179 bge guest_exit_cont
699a0ea0
PM
2180 /* See if this hcall is enabled for in-kernel handling */
2181 ld r4, VCPU_KVM(r9)
2182 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
2183 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
2184 add r4, r4, r0
2185 ld r0, KVM_ENABLED_HCALLS(r4)
2186 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
2187 srd r0, r0, r4
2188 andi. r0, r0, 1
2189 beq guest_exit_cont
2190 /* Get pointer to handler, if any, and call it */
a8606e20 2191 LOAD_REG_ADDR(r4, hcall_real_table)
4baa1d87 2192 lwax r3,r3,r4
a8606e20 2193 cmpwi r3,0
b4072df4 2194 beq guest_exit_cont
05a308c7
AB
2195 add r12,r3,r4
2196 mtctr r12
a8606e20 2197 mr r3,r9 /* get vcpu pointer */
c75df6f9 2198 ld r4,VCPU_GPR(R4)(r9)
a8606e20
PM
2199 bctrl
2200 cmpdi r3,H_TOO_HARD
2201 beq hcall_real_fallback
2202 ld r4,HSTATE_KVM_VCPU(r13)
c75df6f9 2203 std r3,VCPU_GPR(R3)(r4)
a8606e20
PM
2204 ld r10,VCPU_PC(r4)
2205 ld r11,VCPU_MSR(r4)
2206 b fast_guest_return
2207
27025a60
LPF
2208sc_1_fast_return:
2209 mtspr SPRN_SRR0,r10
2210 mtspr SPRN_SRR1,r11
2211 li r10, BOOK3S_INTERRUPT_SYSCALL
e4e38121 2212 bl kvmppc_msr_interrupt
27025a60
LPF
2213 mr r4,r9
2214 b fast_guest_return
2215
a8606e20
PM
2216 /* We've attempted a real mode hcall, but it's punted it back
2217 * to userspace. We need to restore some clobbered volatiles
2218 * before resuming the pass-it-to-qemu path */
2219hcall_real_fallback:
2220 li r12,BOOK3S_INTERRUPT_SYSCALL
2221 ld r9, HSTATE_KVM_VCPU(r13)
a8606e20 2222
b4072df4 2223 b guest_exit_cont
a8606e20
PM
2224
2225 .globl hcall_real_table
2226hcall_real_table:
2227 .long 0 /* 0 - unused */
c1fb0194
AB
2228 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
2229 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
2230 .long DOTSYM(kvmppc_h_read) - hcall_real_table
cdeee518
PM
2231 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table
2232 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table
c1fb0194
AB
2233 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
2234 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
31217db7 2235 .long DOTSYM(kvmppc_rm_h_put_tce) - hcall_real_table
a8606e20 2236 .long 0 /* 0x24 - H_SET_SPRG0 */
c1fb0194 2237 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
a8606e20
PM
2238 .long 0 /* 0x2c */
2239 .long 0 /* 0x30 */
2240 .long 0 /* 0x34 */
2241 .long 0 /* 0x38 */
2242 .long 0 /* 0x3c */
2243 .long 0 /* 0x40 */
2244 .long 0 /* 0x44 */
2245 .long 0 /* 0x48 */
2246 .long 0 /* 0x4c */
2247 .long 0 /* 0x50 */
2248 .long 0 /* 0x54 */
2249 .long 0 /* 0x58 */
2250 .long 0 /* 0x5c */
2251 .long 0 /* 0x60 */
e7d26f28 2252#ifdef CONFIG_KVM_XICS
c1fb0194
AB
2253 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
2254 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
2255 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
5af50993 2256 .long DOTSYM(kvmppc_rm_h_ipoll) - hcall_real_table
c1fb0194 2257 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
e7d26f28
BH
2258#else
2259 .long 0 /* 0x64 - H_EOI */
2260 .long 0 /* 0x68 - H_CPPR */
2261 .long 0 /* 0x6c - H_IPI */
2262 .long 0 /* 0x70 - H_IPOLL */
2263 .long 0 /* 0x74 - H_XIRR */
2264#endif
a8606e20
PM
2265 .long 0 /* 0x78 */
2266 .long 0 /* 0x7c */
2267 .long 0 /* 0x80 */
2268 .long 0 /* 0x84 */
2269 .long 0 /* 0x88 */
2270 .long 0 /* 0x8c */
2271 .long 0 /* 0x90 */
2272 .long 0 /* 0x94 */
2273 .long 0 /* 0x98 */
2274 .long 0 /* 0x9c */
2275 .long 0 /* 0xa0 */
2276 .long 0 /* 0xa4 */
2277 .long 0 /* 0xa8 */
2278 .long 0 /* 0xac */
2279 .long 0 /* 0xb0 */
2280 .long 0 /* 0xb4 */
2281 .long 0 /* 0xb8 */
2282 .long 0 /* 0xbc */
2283 .long 0 /* 0xc0 */
2284 .long 0 /* 0xc4 */
2285 .long 0 /* 0xc8 */
2286 .long 0 /* 0xcc */
2287 .long 0 /* 0xd0 */
2288 .long 0 /* 0xd4 */
2289 .long 0 /* 0xd8 */
2290 .long 0 /* 0xdc */
c1fb0194 2291 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
90fd09f8 2292 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table
a8606e20
PM
2293 .long 0 /* 0xe8 */
2294 .long 0 /* 0xec */
2295 .long 0 /* 0xf0 */
2296 .long 0 /* 0xf4 */
2297 .long 0 /* 0xf8 */
2298 .long 0 /* 0xfc */
2299 .long 0 /* 0x100 */
2300 .long 0 /* 0x104 */
2301 .long 0 /* 0x108 */
2302 .long 0 /* 0x10c */
2303 .long 0 /* 0x110 */
2304 .long 0 /* 0x114 */
2305 .long 0 /* 0x118 */
2306 .long 0 /* 0x11c */
2307 .long 0 /* 0x120 */
c1fb0194 2308 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
8563bf52
PM
2309 .long 0 /* 0x128 */
2310 .long 0 /* 0x12c */
2311 .long 0 /* 0x130 */
c1fb0194 2312 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
31217db7 2313 .long DOTSYM(kvmppc_rm_h_stuff_tce) - hcall_real_table
d3695aa4 2314 .long DOTSYM(kvmppc_rm_h_put_tce_indirect) - hcall_real_table
e928e9cb
ME
2315 .long 0 /* 0x140 */
2316 .long 0 /* 0x144 */
2317 .long 0 /* 0x148 */
2318 .long 0 /* 0x14c */
2319 .long 0 /* 0x150 */
2320 .long 0 /* 0x154 */
2321 .long 0 /* 0x158 */
2322 .long 0 /* 0x15c */
2323 .long 0 /* 0x160 */
2324 .long 0 /* 0x164 */
2325 .long 0 /* 0x168 */
2326 .long 0 /* 0x16c */
2327 .long 0 /* 0x170 */
2328 .long 0 /* 0x174 */
2329 .long 0 /* 0x178 */
2330 .long 0 /* 0x17c */
2331 .long 0 /* 0x180 */
2332 .long 0 /* 0x184 */
2333 .long 0 /* 0x188 */
2334 .long 0 /* 0x18c */
2335 .long 0 /* 0x190 */
2336 .long 0 /* 0x194 */
2337 .long 0 /* 0x198 */
2338 .long 0 /* 0x19c */
2339 .long 0 /* 0x1a0 */
2340 .long 0 /* 0x1a4 */
2341 .long 0 /* 0x1a8 */
2342 .long 0 /* 0x1ac */
2343 .long 0 /* 0x1b0 */
2344 .long 0 /* 0x1b4 */
2345 .long 0 /* 0x1b8 */
2346 .long 0 /* 0x1bc */
2347 .long 0 /* 0x1c0 */
2348 .long 0 /* 0x1c4 */
2349 .long 0 /* 0x1c8 */
2350 .long 0 /* 0x1cc */
2351 .long 0 /* 0x1d0 */
2352 .long 0 /* 0x1d4 */
2353 .long 0 /* 0x1d8 */
2354 .long 0 /* 0x1dc */
2355 .long 0 /* 0x1e0 */
2356 .long 0 /* 0x1e4 */
2357 .long 0 /* 0x1e8 */
2358 .long 0 /* 0x1ec */
2359 .long 0 /* 0x1f0 */
2360 .long 0 /* 0x1f4 */
2361 .long 0 /* 0x1f8 */
2362 .long 0 /* 0x1fc */
2363 .long 0 /* 0x200 */
2364 .long 0 /* 0x204 */
2365 .long 0 /* 0x208 */
2366 .long 0 /* 0x20c */
2367 .long 0 /* 0x210 */
2368 .long 0 /* 0x214 */
2369 .long 0 /* 0x218 */
2370 .long 0 /* 0x21c */
2371 .long 0 /* 0x220 */
2372 .long 0 /* 0x224 */
2373 .long 0 /* 0x228 */
2374 .long 0 /* 0x22c */
2375 .long 0 /* 0x230 */
2376 .long 0 /* 0x234 */
2377 .long 0 /* 0x238 */
2378 .long 0 /* 0x23c */
2379 .long 0 /* 0x240 */
2380 .long 0 /* 0x244 */
2381 .long 0 /* 0x248 */
2382 .long 0 /* 0x24c */
2383 .long 0 /* 0x250 */
2384 .long 0 /* 0x254 */
2385 .long 0 /* 0x258 */
2386 .long 0 /* 0x25c */
2387 .long 0 /* 0x260 */
2388 .long 0 /* 0x264 */
2389 .long 0 /* 0x268 */
2390 .long 0 /* 0x26c */
2391 .long 0 /* 0x270 */
2392 .long 0 /* 0x274 */
2393 .long 0 /* 0x278 */
2394 .long 0 /* 0x27c */
2395 .long 0 /* 0x280 */
2396 .long 0 /* 0x284 */
2397 .long 0 /* 0x288 */
2398 .long 0 /* 0x28c */
2399 .long 0 /* 0x290 */
2400 .long 0 /* 0x294 */
2401 .long 0 /* 0x298 */
2402 .long 0 /* 0x29c */
2403 .long 0 /* 0x2a0 */
2404 .long 0 /* 0x2a4 */
2405 .long 0 /* 0x2a8 */
2406 .long 0 /* 0x2ac */
2407 .long 0 /* 0x2b0 */
2408 .long 0 /* 0x2b4 */
2409 .long 0 /* 0x2b8 */
2410 .long 0 /* 0x2bc */
2411 .long 0 /* 0x2c0 */
2412 .long 0 /* 0x2c4 */
2413 .long 0 /* 0x2c8 */
2414 .long 0 /* 0x2cc */
2415 .long 0 /* 0x2d0 */
2416 .long 0 /* 0x2d4 */
2417 .long 0 /* 0x2d8 */
2418 .long 0 /* 0x2dc */
2419 .long 0 /* 0x2e0 */
2420 .long 0 /* 0x2e4 */
2421 .long 0 /* 0x2e8 */
2422 .long 0 /* 0x2ec */
2423 .long 0 /* 0x2f0 */
2424 .long 0 /* 0x2f4 */
2425 .long 0 /* 0x2f8 */
5af50993
BH
2426#ifdef CONFIG_KVM_XICS
2427 .long DOTSYM(kvmppc_rm_h_xirr_x) - hcall_real_table
2428#else
2429 .long 0 /* 0x2fc - H_XIRR_X*/
2430#endif
e928e9cb 2431 .long DOTSYM(kvmppc_h_random) - hcall_real_table
ae2113a4 2432 .globl hcall_real_table_end
a8606e20
PM
2433hcall_real_table_end:
2434
8563bf52
PM
2435_GLOBAL(kvmppc_h_set_xdabr)
2436 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2437 beq 6f
2438 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2439 andc. r0, r5, r0
2440 beq 3f
24416: li r3, H_PARAMETER
2442 blr
2443
a8606e20 2444_GLOBAL(kvmppc_h_set_dabr)
8563bf52
PM
2445 li r5, DABRX_USER | DABRX_KERNEL
24463:
eee7ff9d
MN
2447BEGIN_FTR_SECTION
2448 b 2f
2449END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
a8606e20 2450 std r4,VCPU_DABR(r3)
8563bf52
PM
2451 stw r5, VCPU_DABRX(r3)
2452 mtspr SPRN_DABRX, r5
8943633c
PM
2453 /* Work around P7 bug where DABR can get corrupted on mtspr */
24541: mtspr SPRN_DABR,r4
2455 mfspr r5, SPRN_DABR
2456 cmpd r4, r5
2457 bne 1b
2458 isync
a8606e20
PM
2459 li r3,0
2460 blr
2461
8563bf52
PM
2462 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
24632: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
760a7364 2464 rlwimi r5, r4, 2, DAWRX_WT
8563bf52
PM
2465 clrrdi r4, r4, 3
2466 std r4, VCPU_DAWR(r3)
2467 std r5, VCPU_DAWRX(r3)
2468 mtspr SPRN_DAWR, r4
2469 mtspr SPRN_DAWRX, r5
2470 li r3, 0
a8606e20
PM
2471 blr
2472
1f09c3ed 2473_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */
19ccb76a
PM
2474 ori r11,r11,MSR_EE
2475 std r11,VCPU_MSR(r3)
2476 li r0,1
2477 stb r0,VCPU_CEDED(r3)
2478 sync /* order setting ceded vs. testing prodded */
2479 lbz r5,VCPU_PRODDED(r3)
2480 cmpwi r5,0
04f995a5 2481 bne kvm_cede_prodded
6af27c84
PM
2482 li r12,0 /* set trap to 0 to say hcall is handled */
2483 stw r12,VCPU_TRAP(r3)
19ccb76a 2484 li r0,H_SUCCESS
c75df6f9 2485 std r0,VCPU_GPR(R3)(r3)
19ccb76a
PM
2486
2487 /*
2488 * Set our bit in the bitmask of napping threads unless all the
2489 * other threads are already napping, in which case we send this
2490 * up to the host.
2491 */
2492 ld r5,HSTATE_KVM_VCORE(r13)
e0b7ec05 2493 lbz r6,HSTATE_PTID(r13)
19ccb76a
PM
2494 lwz r8,VCORE_ENTRY_EXIT(r5)
2495 clrldi r8,r8,56
2496 li r0,1
2497 sld r0,r0,r6
2498 addi r6,r5,VCORE_NAPPING_THREADS
249931: lwarx r4,0,r6
2500 or r4,r4,r0
7d6c40da
PM
2501 cmpw r4,r8
2502 beq kvm_cede_exit
19ccb76a
PM
2503 stwcx. r4,0,r6
2504 bne 31b
7d6c40da 2505 /* order napping_threads update vs testing entry_exit_map */
f019b7ad 2506 isync
e0b7ec05 2507 li r0,NAPPING_CEDE
19ccb76a 2508 stb r0,HSTATE_NAPPING(r13)
19ccb76a
PM
2509 lwz r7,VCORE_ENTRY_EXIT(r5)
2510 cmpwi r7,0x100
2511 bge 33f /* another thread already exiting */
2512
2513/*
2514 * Although not specifically required by the architecture, POWER7
2515 * preserves the following registers in nap mode, even if an SMT mode
2516 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2517 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2518 */
2519 /* Save non-volatile GPRs */
c75df6f9
MN
2520 std r14, VCPU_GPR(R14)(r3)
2521 std r15, VCPU_GPR(R15)(r3)
2522 std r16, VCPU_GPR(R16)(r3)
2523 std r17, VCPU_GPR(R17)(r3)
2524 std r18, VCPU_GPR(R18)(r3)
2525 std r19, VCPU_GPR(R19)(r3)
2526 std r20, VCPU_GPR(R20)(r3)
2527 std r21, VCPU_GPR(R21)(r3)
2528 std r22, VCPU_GPR(R22)(r3)
2529 std r23, VCPU_GPR(R23)(r3)
2530 std r24, VCPU_GPR(R24)(r3)
2531 std r25, VCPU_GPR(R25)(r3)
2532 std r26, VCPU_GPR(R26)(r3)
2533 std r27, VCPU_GPR(R27)(r3)
2534 std r28, VCPU_GPR(R28)(r3)
2535 std r29, VCPU_GPR(R29)(r3)
2536 std r30, VCPU_GPR(R30)(r3)
2537 std r31, VCPU_GPR(R31)(r3)
19ccb76a
PM
2538
2539 /* save FP state */
595e4f7e 2540 bl kvmppc_save_fp
19ccb76a 2541
93d17397
PM
2542#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2543BEGIN_FTR_SECTION
67f8a8c1
PM
2544 /*
2545 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
2546 */
93d17397
PM
2547 ld r9, HSTATE_KVM_VCPU(r13)
2548 bl kvmppc_save_tm
2549END_FTR_SECTION_IFSET(CPU_FTR_TM)
2550#endif
2551
fd6d53b1
PM
2552 /*
2553 * Set DEC to the smaller of DEC and HDEC, so that we wake
2554 * no later than the end of our timeslice (HDEC interrupts
2555 * don't wake us from nap).
2556 */
2557 mfspr r3, SPRN_DEC
2558 mfspr r4, SPRN_HDEC
2559 mftb r5
1bc3fe81
PM
2560BEGIN_FTR_SECTION
2561 /* On P9 check whether the guest has large decrementer mode enabled */
2562 ld r6, HSTATE_KVM_VCORE(r13)
2563 ld r6, VCORE_LPCR(r6)
2564 andis. r6, r6, LPCR_LD@h
2565 bne 68f
2566END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
2f272463 2567 extsw r3, r3
1bc3fe81 256868: EXTEND_HDEC(r4)
2f272463 2569 cmpd r3, r4
fd6d53b1
PM
2570 ble 67f
2571 mtspr SPRN_DEC, r4
257267:
2573 /* save expiry time of guest decrementer */
fd6d53b1
PM
2574 add r3, r3, r5
2575 ld r4, HSTATE_KVM_VCPU(r13)
2576 ld r5, HSTATE_KVM_VCORE(r13)
2577 ld r6, VCORE_TB_OFFSET(r5)
2578 subf r3, r6, r3 /* convert to host TB value */
2579 std r3, VCPU_DEC_EXPIRES(r4)
2580
b6c295df
PM
2581#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2582 ld r4, HSTATE_KVM_VCPU(r13)
2583 addi r3, r4, VCPU_TB_CEDE
2584 bl kvmhv_accumulate_time
2585#endif
2586
ccc07772
PM
2587 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */
2588
19ccb76a 2589 /*
aa31e843 2590 * Take a nap until a decrementer or external or doobell interrupt
ccc07772 2591 * occurs, with PECE1 and PECE0 set in LPCR.
66feed61 2592 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP.
ccc07772 2593 * Also clear the runlatch bit before napping.
19ccb76a 2594 */
56548fc0 2595kvm_do_nap:
1f09c3ed
PM
2596 mfspr r0, SPRN_CTRLF
2597 clrrdi r0, r0, 1
2598 mtspr SPRN_CTRLT, r0
582b910e 2599
f0888f70
PM
2600 li r0,1
2601 stb r0,HSTATE_HWTHREAD_REQ(r13)
19ccb76a
PM
2602 mfspr r5,SPRN_LPCR
2603 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
aa31e843 2604BEGIN_FTR_SECTION
66feed61 2605 ori r5, r5, LPCR_PECEDH
ccc07772 2606 rlwimi r5, r3, 0, LPCR_PECEDP
aa31e843 2607END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
bf53c88e
PM
2608
2609kvm_nap_sequence: /* desired LPCR value in r5 */
2610BEGIN_FTR_SECTION
2611 /*
2612 * PSSCR bits: exit criterion = 1 (wakeup based on LPCR at sreset)
2613 * enable state loss = 1 (allow SMT mode switch)
2614 * requested level = 0 (just stop dispatching)
2615 */
2616 lis r3, (PSSCR_EC | PSSCR_ESL)@h
2617 mtspr SPRN_PSSCR, r3
2618 /* Set LPCR_PECE_HVEE bit to enable wakeup by HV interrupts */
2619 li r4, LPCR_PECE_HVEE@higher
2620 sldi r4, r4, 32
2621 or r5, r5, r4
2622END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
19ccb76a
PM
2623 mtspr SPRN_LPCR,r5
2624 isync
2625 li r0, 0
2626 std r0, HSTATE_SCRATCH0(r13)
2627 ptesync
2628 ld r0, HSTATE_SCRATCH0(r13)
26291: cmpd r0, r0
2630 bne 1b
bf53c88e 2631BEGIN_FTR_SECTION
19ccb76a 2632 nap
bf53c88e
PM
2633FTR_SECTION_ELSE
2634 PPC_STOP
2635ALT_FTR_SECTION_END_IFCLR(CPU_FTR_ARCH_300)
19ccb76a
PM
2636 b .
2637
e3bbbbfa
PM
263833: mr r4, r3
2639 li r3, 0
2640 li r12, 0
2641 b 34f
2642
19ccb76a 2643kvm_end_cede:
4619ac88
PM
2644 /* get vcpu pointer */
2645 ld r4, HSTATE_KVM_VCPU(r13)
2646
19ccb76a
PM
2647 /* Woken by external or decrementer interrupt */
2648 ld r1, HSTATE_HOST_R1(r13)
19ccb76a 2649
b6c295df
PM
2650#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2651 addi r3, r4, VCPU_TB_RMINTR
2652 bl kvmhv_accumulate_time
2653#endif
2654
93d17397
PM
2655#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2656BEGIN_FTR_SECTION
67f8a8c1
PM
2657 /*
2658 * NOTE THAT THIS TRASHES ALL NON-VOLATILE REGISTERS INCLUDING CR
2659 */
93d17397
PM
2660 bl kvmppc_restore_tm
2661END_FTR_SECTION_IFSET(CPU_FTR_TM)
2662#endif
2663
19ccb76a
PM
2664 /* load up FP state */
2665 bl kvmppc_load_fp
2666
fd6d53b1
PM
2667 /* Restore guest decrementer */
2668 ld r3, VCPU_DEC_EXPIRES(r4)
2669 ld r5, HSTATE_KVM_VCORE(r13)
2670 ld r6, VCORE_TB_OFFSET(r5)
2671 add r3, r3, r6 /* convert host TB to guest TB value */
2672 mftb r7
2673 subf r3, r7, r3
2674 mtspr SPRN_DEC, r3
2675
19ccb76a 2676 /* Load NV GPRS */
c75df6f9
MN
2677 ld r14, VCPU_GPR(R14)(r4)
2678 ld r15, VCPU_GPR(R15)(r4)
2679 ld r16, VCPU_GPR(R16)(r4)
2680 ld r17, VCPU_GPR(R17)(r4)
2681 ld r18, VCPU_GPR(R18)(r4)
2682 ld r19, VCPU_GPR(R19)(r4)
2683 ld r20, VCPU_GPR(R20)(r4)
2684 ld r21, VCPU_GPR(R21)(r4)
2685 ld r22, VCPU_GPR(R22)(r4)
2686 ld r23, VCPU_GPR(R23)(r4)
2687 ld r24, VCPU_GPR(R24)(r4)
2688 ld r25, VCPU_GPR(R25)(r4)
2689 ld r26, VCPU_GPR(R26)(r4)
2690 ld r27, VCPU_GPR(R27)(r4)
2691 ld r28, VCPU_GPR(R28)(r4)
2692 ld r29, VCPU_GPR(R29)(r4)
2693 ld r30, VCPU_GPR(R30)(r4)
2694 ld r31, VCPU_GPR(R31)(r4)
37f55d30 2695
e3bbbbfa
PM
2696 /* Check the wake reason in SRR1 to see why we got here */
2697 bl kvmppc_check_wake_reason
19ccb76a 2698
37f55d30
SW
2699 /*
2700 * Restore volatile registers since we could have called a
2701 * C routine in kvmppc_check_wake_reason
2702 * r4 = VCPU
2703 * r3 tells us whether we need to return to host or not
2704 * WARNING: it gets checked further down:
2705 * should not modify r3 until this check is done.
2706 */
2707 ld r4, HSTATE_KVM_VCPU(r13)
2708
19ccb76a 2709 /* clear our bit in vcore->napping_threads */
e3bbbbfa
PM
271034: ld r5,HSTATE_KVM_VCORE(r13)
2711 lbz r7,HSTATE_PTID(r13)
19ccb76a 2712 li r0,1
e3bbbbfa 2713 sld r0,r0,r7
19ccb76a
PM
2714 addi r6,r5,VCORE_NAPPING_THREADS
271532: lwarx r7,0,r6
2716 andc r7,r7,r0
2717 stwcx. r7,0,r6
2718 bne 32b
2719 li r0,0
2720 stb r0,HSTATE_NAPPING(r13)
2721
37f55d30 2722 /* See if the wake reason saved in r3 means we need to exit */
e3bbbbfa 2723 stw r12, VCPU_TRAP(r4)
4619ac88 2724 mr r9, r4
e3bbbbfa
PM
2725 cmpdi r3, 0
2726 bgt guest_exit_cont
4619ac88 2727
19ccb76a
PM
2728 /* see if any other thread is already exiting */
2729 lwz r0,VCORE_ENTRY_EXIT(r5)
2730 cmpwi r0,0x100
e3bbbbfa 2731 bge guest_exit_cont
19ccb76a 2732
e3bbbbfa 2733 b kvmppc_cede_reentry /* if not go back to guest */
19ccb76a
PM
2734
2735 /* cede when already previously prodded case */
04f995a5
PM
2736kvm_cede_prodded:
2737 li r0,0
19ccb76a
PM
2738 stb r0,VCPU_PRODDED(r3)
2739 sync /* order testing prodded vs. clearing ceded */
2740 stb r0,VCPU_CEDED(r3)
2741 li r3,H_SUCCESS
2742 blr
2743
2744 /* we've ceded but we want to give control to the host */
04f995a5 2745kvm_cede_exit:
6af27c84
PM
2746 ld r9, HSTATE_KVM_VCPU(r13)
2747 b guest_exit_cont
19ccb76a 2748
b4072df4
PM
2749 /* Try to handle a machine check in real mode */
2750machine_check_realmode:
2751 mr r3, r9 /* get vcpu pointer */
b1576fec 2752 bl kvmppc_realmode_machine_check
b4072df4 2753 nop
b4072df4
PM
2754 ld r9, HSTATE_KVM_VCPU(r13)
2755 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
74845bc2 2756 /*
e20bbd3d
AP
2757 * For the guest that is FWNMI capable, deliver all the MCE errors
2758 * (handled/unhandled) by exiting the guest with KVM_EXIT_NMI exit
2759 * reason. This new approach injects machine check errors in guest
2760 * address space to guest with additional information in the form
2761 * of RTAS event, thus enabling guest kernel to suitably handle
2762 * such errors.
966d713e 2763 *
e20bbd3d
AP
2764 * For the guest that is not FWNMI capable (old QEMU) fallback
2765 * to old behaviour for backward compatibility:
2766 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest either
2767 * through machine check interrupt (set HSRR0 to 0x200).
2768 * For handled errors (no-fatal), just go back to guest execution
2769 * with current HSRR0.
966d713e
MS
2770 * if we receive machine check with MSR(RI=0) then deliver it to
2771 * guest as machine check causing guest to crash.
74845bc2 2772 */
74845bc2 2773 ld r11, VCPU_MSR(r9)
1c9e3d51
PM
2774 rldicl. r0, r11, 64-MSR_HV_LG, 63 /* check if it happened in HV mode */
2775 bne mc_cont /* if so, exit to host */
e20bbd3d
AP
2776 /* Check if guest is capable of handling NMI exit */
2777 ld r10, VCPU_KVM(r9)
2778 lbz r10, KVM_FWNMI(r10)
2779 cmpdi r10, 1 /* FWNMI capable? */
2780 beq mc_cont /* if so, exit with KVM_EXIT_NMI. */
2781
2782 /* if not, fall through for backward compatibility. */
966d713e
MS
2783 andi. r10, r11, MSR_RI /* check for unrecoverable exception */
2784 beq 1f /* Deliver a machine check to guest */
2785 ld r10, VCPU_PC(r9)
2786 cmpdi r3, 0 /* Did we handle MCE ? */
74845bc2 2787 bne 2f /* Continue guest execution. */
b4072df4 2788 /* If not, deliver a machine check. SRR0/1 are already set */
966d713e 27891: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
e4e38121 2790 bl kvmppc_msr_interrupt
74845bc2 27912: b fast_interrupt_c_return
b4072df4 2792
e3bbbbfa
PM
2793/*
2794 * Check the reason we woke from nap, and take appropriate action.
1f09c3ed 2795 * Returns (in r3):
e3bbbbfa
PM
2796 * 0 if nothing needs to be done
2797 * 1 if something happened that needs to be handled by the host
66feed61 2798 * -1 if there was a guest wakeup (IPI or msgsnd)
e3c13e56
SW
2799 * -2 if we handled a PCI passthrough interrupt (returned by
2800 * kvmppc_read_intr only)
e3bbbbfa
PM
2801 *
2802 * Also sets r12 to the interrupt vector for any interrupt that needs
2803 * to be handled now by the host (0x500 for external interrupt), or zero.
37f55d30
SW
2804 * Modifies all volatile registers (since it may call a C function).
2805 * This routine calls kvmppc_read_intr, a C function, if an external
2806 * interrupt is pending.
e3bbbbfa
PM
2807 */
2808kvmppc_check_wake_reason:
2809 mfspr r6, SPRN_SRR1
aa31e843
PM
2810BEGIN_FTR_SECTION
2811 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2812FTR_SECTION_ELSE
2813 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2814ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2815 cmpwi r6, 8 /* was it an external interrupt? */
37f55d30 2816 beq 7f /* if so, see what it was */
e3bbbbfa
PM
2817 li r3, 0
2818 li r12, 0
2819 cmpwi r6, 6 /* was it the decrementer? */
2820 beq 0f
aa31e843
PM
2821BEGIN_FTR_SECTION
2822 cmpwi r6, 5 /* privileged doorbell? */
2823 beq 0f
5d00f66b
PM
2824 cmpwi r6, 3 /* hypervisor doorbell? */
2825 beq 3f
aa31e843 2826END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
fd7bacbc
MS
2827 cmpwi r6, 0xa /* Hypervisor maintenance ? */
2828 beq 4f
e3bbbbfa
PM
2829 li r3, 1 /* anything else, return 1 */
28300: blr
2831
5d00f66b
PM
2832 /* hypervisor doorbell */
28333: li r12, BOOK3S_INTERRUPT_H_DOORBELL
70aa3961
GS
2834
2835 /*
2836 * Clear the doorbell as we will invoke the handler
2837 * explicitly in the guest exit path.
2838 */
2839 lis r6, (PPC_DBELL_SERVER << (63-36))@h
2840 PPC_MSGCLR(6)
66feed61 2841 /* see if it's a host IPI */
5d00f66b 2842 li r3, 1
2cde3716
NP
2843BEGIN_FTR_SECTION
2844 PPC_MSGSYNC
2845 lwsync
2846END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
66feed61
PM
2847 lbz r0, HSTATE_HOST_IPI(r13)
2848 cmpwi r0, 0
2849 bnelr
70aa3961 2850 /* if not, return -1 */
66feed61 2851 li r3, -1
5d00f66b
PM
2852 blr
2853
fd7bacbc
MS
2854 /* Woken up due to Hypervisor maintenance interrupt */
28554: li r12, BOOK3S_INTERRUPT_HMI
2856 li r3, 1
2857 blr
2858
37f55d30
SW
2859 /* external interrupt - create a stack frame so we can call C */
28607: mflr r0
2861 std r0, PPC_LR_STKOFF(r1)
2862 stdu r1, -PPC_MIN_STKFRM(r1)
2863 bl kvmppc_read_intr
2864 nop
2865 li r12, BOOK3S_INTERRUPT_EXTERNAL
f7af5209
SW
2866 cmpdi r3, 1
2867 ble 1f
2868
2869 /*
2870 * Return code of 2 means PCI passthrough interrupt, but
2871 * we need to return back to host to complete handling the
2872 * interrupt. Trap reason is expected in r12 by guest
2873 * exit code.
2874 */
2875 li r12, BOOK3S_INTERRUPT_HV_RM_HARD
28761:
37f55d30
SW
2877 ld r0, PPC_MIN_STKFRM+PPC_LR_STKOFF(r1)
2878 addi r1, r1, PPC_MIN_STKFRM
2879 mtlr r0
2880 blr
371fefd6 2881
de56a948
PM
2882/*
2883 * Save away FP, VMX and VSX registers.
2884 * r3 = vcpu pointer
595e4f7e
PM
2885 * N.B. r30 and r31 are volatile across this function,
2886 * thus it is not callable from C.
a8606e20 2887 */
595e4f7e
PM
2888kvmppc_save_fp:
2889 mflr r30
2890 mr r31,r3
8943633c
PM
2891 mfmsr r5
2892 ori r8,r5,MSR_FP
de56a948
PM
2893#ifdef CONFIG_ALTIVEC
2894BEGIN_FTR_SECTION
2895 oris r8,r8,MSR_VEC@h
2896END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2897#endif
2898#ifdef CONFIG_VSX
2899BEGIN_FTR_SECTION
2900 oris r8,r8,MSR_VSX@h
2901END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2902#endif
2903 mtmsrd r8
595e4f7e 2904 addi r3,r3,VCPU_FPRS
9bf163f8 2905 bl store_fp_state
de56a948
PM
2906#ifdef CONFIG_ALTIVEC
2907BEGIN_FTR_SECTION
595e4f7e 2908 addi r3,r31,VCPU_VRS
9bf163f8 2909 bl store_vr_state
de56a948
PM
2910END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2911#endif
2912 mfspr r6,SPRN_VRSAVE
e724f080 2913 stw r6,VCPU_VRSAVE(r31)
595e4f7e 2914 mtlr r30
de56a948
PM
2915 blr
2916
2917/*
2918 * Load up FP, VMX and VSX registers
2919 * r4 = vcpu pointer
595e4f7e
PM
2920 * N.B. r30 and r31 are volatile across this function,
2921 * thus it is not callable from C.
de56a948 2922 */
de56a948 2923kvmppc_load_fp:
595e4f7e
PM
2924 mflr r30
2925 mr r31,r4
de56a948
PM
2926 mfmsr r9
2927 ori r8,r9,MSR_FP
2928#ifdef CONFIG_ALTIVEC
2929BEGIN_FTR_SECTION
2930 oris r8,r8,MSR_VEC@h
2931END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2932#endif
2933#ifdef CONFIG_VSX
2934BEGIN_FTR_SECTION
2935 oris r8,r8,MSR_VSX@h
2936END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2937#endif
2938 mtmsrd r8
595e4f7e 2939 addi r3,r4,VCPU_FPRS
9bf163f8 2940 bl load_fp_state
de56a948
PM
2941#ifdef CONFIG_ALTIVEC
2942BEGIN_FTR_SECTION
595e4f7e 2943 addi r3,r31,VCPU_VRS
9bf163f8 2944 bl load_vr_state
de56a948
PM
2945END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2946#endif
e724f080 2947 lwz r7,VCPU_VRSAVE(r31)
de56a948 2948 mtspr SPRN_VRSAVE,r7
595e4f7e
PM
2949 mtlr r30
2950 mr r4,r31
de56a948 2951 blr
44a3add8 2952
f024ee09
PM
2953#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2954/*
2955 * Save transactional state and TM-related registers.
2956 * Called with r9 pointing to the vcpu struct.
2957 * This can modify all checkpointed registers, but
2958 * restores r1, r2 and r9 (vcpu pointer) before exit.
2959 */
2960kvmppc_save_tm:
2961 mflr r0
2962 std r0, PPC_LR_STKOFF(r1)
2963
2964 /* Turn on TM. */
2965 mfmsr r8
2966 li r0, 1
2967 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
2968 mtmsrd r8
2969
2970 ld r5, VCPU_MSR(r9)
2971 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
2972 beq 1f /* TM not active in guest. */
2973
2974 std r1, HSTATE_HOST_R1(r13)
2975 li r3, TM_CAUSE_KVM_RESCHED
2976
2977 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
2978 li r5, 0
2979 mtmsrd r5, 1
2980
2981 /* All GPRs are volatile at this point. */
2982 TRECLAIM(R3)
2983
2984 /* Temporarily store r13 and r9 so we have some regs to play with */
2985 SET_SCRATCH0(r13)
2986 GET_PACA(r13)
2987 std r9, PACATMSCRATCH(r13)
2988 ld r9, HSTATE_KVM_VCPU(r13)
2989
2990 /* Get a few more GPRs free. */
2991 std r29, VCPU_GPRS_TM(29)(r9)
2992 std r30, VCPU_GPRS_TM(30)(r9)
2993 std r31, VCPU_GPRS_TM(31)(r9)
2994
2995 /* Save away PPR and DSCR soon so don't run with user values. */
2996 mfspr r31, SPRN_PPR
2997 HMT_MEDIUM
2998 mfspr r30, SPRN_DSCR
2999 ld r29, HSTATE_DSCR(r13)
3000 mtspr SPRN_DSCR, r29
3001
3002 /* Save all but r9, r13 & r29-r31 */
3003 reg = 0
3004 .rept 29
3005 .if (reg != 9) && (reg != 13)
3006 std reg, VCPU_GPRS_TM(reg)(r9)
3007 .endif
3008 reg = reg + 1
3009 .endr
3010 /* ... now save r13 */
3011 GET_SCRATCH0(r4)
3012 std r4, VCPU_GPRS_TM(13)(r9)
3013 /* ... and save r9 */
3014 ld r4, PACATMSCRATCH(r13)
3015 std r4, VCPU_GPRS_TM(9)(r9)
3016
3017 /* Reload stack pointer and TOC. */
3018 ld r1, HSTATE_HOST_R1(r13)
3019 ld r2, PACATOC(r13)
3020
3021 /* Set MSR RI now we have r1 and r13 back. */
3022 li r5, MSR_RI
3023 mtmsrd r5, 1
3024
3025 /* Save away checkpinted SPRs. */
3026 std r31, VCPU_PPR_TM(r9)
3027 std r30, VCPU_DSCR_TM(r9)
3028 mflr r5
3029 mfcr r6
3030 mfctr r7
3031 mfspr r8, SPRN_AMR
3032 mfspr r10, SPRN_TAR
0d808df0 3033 mfxer r11
f024ee09
PM
3034 std r5, VCPU_LR_TM(r9)
3035 stw r6, VCPU_CR_TM(r9)
3036 std r7, VCPU_CTR_TM(r9)
3037 std r8, VCPU_AMR_TM(r9)
3038 std r10, VCPU_TAR_TM(r9)
0d808df0 3039 std r11, VCPU_XER_TM(r9)
f024ee09
PM
3040
3041 /* Restore r12 as trap number. */
3042 lwz r12, VCPU_TRAP(r9)
3043
3044 /* Save FP/VSX. */
3045 addi r3, r9, VCPU_FPRS_TM
3046 bl store_fp_state
3047 addi r3, r9, VCPU_VRS_TM
3048 bl store_vr_state
3049 mfspr r6, SPRN_VRSAVE
3050 stw r6, VCPU_VRSAVE_TM(r9)
30511:
3052 /*
3053 * We need to save these SPRs after the treclaim so that the software
3054 * error code is recorded correctly in the TEXASR. Also the user may
3055 * change these outside of a transaction, so they must always be
3056 * context switched.
3057 */
3058 mfspr r5, SPRN_TFHAR
3059 mfspr r6, SPRN_TFIAR
3060 mfspr r7, SPRN_TEXASR
3061 std r5, VCPU_TFHAR(r9)
3062 std r6, VCPU_TFIAR(r9)
3063 std r7, VCPU_TEXASR(r9)
3064
3065 ld r0, PPC_LR_STKOFF(r1)
3066 mtlr r0
3067 blr
3068
3069/*
3070 * Restore transactional state and TM-related registers.
3071 * Called with r4 pointing to the vcpu struct.
3072 * This potentially modifies all checkpointed registers.
3073 * It restores r1, r2, r4 from the PACA.
3074 */
3075kvmppc_restore_tm:
3076 mflr r0
3077 std r0, PPC_LR_STKOFF(r1)
3078
3079 /* Turn on TM/FP/VSX/VMX so we can restore them. */
3080 mfmsr r5
3081 li r6, MSR_TM >> 32
3082 sldi r6, r6, 32
3083 or r5, r5, r6
3084 ori r5, r5, MSR_FP
3085 oris r5, r5, (MSR_VEC | MSR_VSX)@h
3086 mtmsrd r5
3087
3088 /*
3089 * The user may change these outside of a transaction, so they must
3090 * always be context switched.
3091 */
3092 ld r5, VCPU_TFHAR(r4)
3093 ld r6, VCPU_TFIAR(r4)
3094 ld r7, VCPU_TEXASR(r4)
3095 mtspr SPRN_TFHAR, r5
3096 mtspr SPRN_TFIAR, r6
3097 mtspr SPRN_TEXASR, r7
3098
3099 ld r5, VCPU_MSR(r4)
3100 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
3101 beqlr /* TM not active in guest */
3102 std r1, HSTATE_HOST_R1(r13)
3103
3104 /* Make sure the failure summary is set, otherwise we'll program check
3105 * when we trechkpt. It's possible that this might have been not set
3106 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
3107 * host.
3108 */
3109 oris r7, r7, (TEXASR_FS)@h
3110 mtspr SPRN_TEXASR, r7
3111
3112 /*
3113 * We need to load up the checkpointed state for the guest.
3114 * We need to do this early as it will blow away any GPRs, VSRs and
3115 * some SPRs.
3116 */
3117
3118 mr r31, r4
3119 addi r3, r31, VCPU_FPRS_TM
3120 bl load_fp_state
3121 addi r3, r31, VCPU_VRS_TM
3122 bl load_vr_state
3123 mr r4, r31
3124 lwz r7, VCPU_VRSAVE_TM(r4)
3125 mtspr SPRN_VRSAVE, r7
3126
3127 ld r5, VCPU_LR_TM(r4)
3128 lwz r6, VCPU_CR_TM(r4)
3129 ld r7, VCPU_CTR_TM(r4)
3130 ld r8, VCPU_AMR_TM(r4)
3131 ld r9, VCPU_TAR_TM(r4)
0d808df0 3132 ld r10, VCPU_XER_TM(r4)
f024ee09
PM
3133 mtlr r5
3134 mtcr r6
3135 mtctr r7
3136 mtspr SPRN_AMR, r8
3137 mtspr SPRN_TAR, r9
0d808df0 3138 mtxer r10
f024ee09
PM
3139
3140 /*
3141 * Load up PPR and DSCR values but don't put them in the actual SPRs
3142 * till the last moment to avoid running with userspace PPR and DSCR for
3143 * too long.
3144 */
3145 ld r29, VCPU_DSCR_TM(r4)
3146 ld r30, VCPU_PPR_TM(r4)
3147
3148 std r2, PACATMSCRATCH(r13) /* Save TOC */
3149
3150 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
3151 li r5, 0
3152 mtmsrd r5, 1
3153
3154 /* Load GPRs r0-r28 */
3155 reg = 0
3156 .rept 29
3157 ld reg, VCPU_GPRS_TM(reg)(r31)
3158 reg = reg + 1
3159 .endr
3160
3161 mtspr SPRN_DSCR, r29
3162 mtspr SPRN_PPR, r30
3163
3164 /* Load final GPRs */
3165 ld 29, VCPU_GPRS_TM(29)(r31)
3166 ld 30, VCPU_GPRS_TM(30)(r31)
3167 ld 31, VCPU_GPRS_TM(31)(r31)
3168
3169 /* TM checkpointed state is now setup. All GPRs are now volatile. */
3170 TRECHKPT
3171
3172 /* Now let's get back the state we need. */
3173 HMT_MEDIUM
3174 GET_PACA(r13)
3175 ld r29, HSTATE_DSCR(r13)
3176 mtspr SPRN_DSCR, r29
3177 ld r4, HSTATE_KVM_VCPU(r13)
3178 ld r1, HSTATE_HOST_R1(r13)
3179 ld r2, PACATMSCRATCH(r13)
3180
3181 /* Set the MSR RI since we have our registers back. */
3182 li r5, MSR_RI
3183 mtmsrd r5, 1
3184
3185 ld r0, PPC_LR_STKOFF(r1)
3186 mtlr r0
3187 blr
3188#endif
3189
44a3add8
PM
3190/*
3191 * We come here if we get any exception or interrupt while we are
3192 * executing host real mode code while in guest MMU context.
857b99e1
PM
3193 * r12 is (CR << 32) | vector
3194 * r13 points to our PACA
3195 * r12 is saved in HSTATE_SCRATCH0(r13)
3196 * ctr is saved in HSTATE_SCRATCH1(r13) if RELOCATABLE
3197 * r9 is saved in HSTATE_SCRATCH2(r13)
3198 * r13 is saved in HSPRG1
3199 * cfar is saved in HSTATE_CFAR(r13)
3200 * ppr is saved in HSTATE_PPR(r13)
44a3add8
PM
3201 */
3202kvmppc_bad_host_intr:
857b99e1
PM
3203 /*
3204 * Switch to the emergency stack, but start half-way down in
3205 * case we were already on it.
3206 */
3207 mr r9, r1
3208 std r1, PACAR1(r13)
3209 ld r1, PACAEMERGSP(r13)
3210 subi r1, r1, THREAD_SIZE/2 + INT_FRAME_SIZE
3211 std r9, 0(r1)
3212 std r0, GPR0(r1)
3213 std r9, GPR1(r1)
3214 std r2, GPR2(r1)
3215 SAVE_4GPRS(3, r1)
3216 SAVE_2GPRS(7, r1)
3217 srdi r0, r12, 32
3218 clrldi r12, r12, 32
3219 std r0, _CCR(r1)
3220 std r12, _TRAP(r1)
3221 andi. r0, r12, 2
3222 beq 1f
3223 mfspr r3, SPRN_HSRR0
3224 mfspr r4, SPRN_HSRR1
3225 mfspr r5, SPRN_HDAR
3226 mfspr r6, SPRN_HDSISR
3227 b 2f
32281: mfspr r3, SPRN_SRR0
3229 mfspr r4, SPRN_SRR1
3230 mfspr r5, SPRN_DAR
3231 mfspr r6, SPRN_DSISR
32322: std r3, _NIP(r1)
3233 std r4, _MSR(r1)
3234 std r5, _DAR(r1)
3235 std r6, _DSISR(r1)
3236 ld r9, HSTATE_SCRATCH2(r13)
3237 ld r12, HSTATE_SCRATCH0(r13)
3238 GET_SCRATCH0(r0)
3239 SAVE_4GPRS(9, r1)
3240 std r0, GPR13(r1)
3241 SAVE_NVGPRS(r1)
3242 ld r5, HSTATE_CFAR(r13)
3243 std r5, ORIG_GPR3(r1)
3244 mflr r3
3245#ifdef CONFIG_RELOCATABLE
3246 ld r4, HSTATE_SCRATCH1(r13)
3247#else
3248 mfctr r4
3249#endif
3250 mfxer r5
3251 lbz r6, PACASOFTIRQEN(r13)
3252 std r3, _LINK(r1)
3253 std r4, _CTR(r1)
3254 std r5, _XER(r1)
3255 std r6, SOFTE(r1)
3256 ld r2, PACATOC(r13)
3257 LOAD_REG_IMMEDIATE(3, 0x7265677368657265)
3258 std r3, STACK_FRAME_OVERHEAD-16(r1)
3259
3260 /*
3261 * On POWER9 do a minimal restore of the MMU and call C code,
3262 * which will print a message and panic.
3263 * XXX On POWER7 and POWER8, we just spin here since we don't
3264 * know what the other threads are doing (and we don't want to
3265 * coordinate with them) - but at least we now have register state
3266 * in memory that we might be able to look at from another CPU.
3267 */
3268BEGIN_FTR_SECTION
44a3add8 3269 b .
857b99e1
PM
3270END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
3271 ld r9, HSTATE_KVM_VCPU(r13)
3272 ld r10, VCPU_KVM(r9)
3273
3274 li r0, 0
3275 mtspr SPRN_AMR, r0
3276 mtspr SPRN_IAMR, r0
3277 mtspr SPRN_CIABR, r0
3278 mtspr SPRN_DAWRX, r0
3279
3280 /* Flush the ERAT on radix P9 DD1 guest exit */
3281BEGIN_FTR_SECTION
3282 PPC_INVALIDATE_ERAT
3283END_FTR_SECTION_IFSET(CPU_FTR_POWER9_DD1)
3284
3285BEGIN_MMU_FTR_SECTION
3286 b 4f
3287END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
3288
3289 slbmte r0, r0
3290 slbia
3291 ptesync
3292 ld r8, PACA_SLBSHADOWPTR(r13)
3293 .rept SLB_NUM_BOLTED
3294 li r3, SLBSHADOW_SAVEAREA
3295 LDX_BE r5, r8, r3
3296 addi r3, r3, 8
3297 LDX_BE r6, r8, r3
3298 andis. r7, r5, SLB_ESID_V@h
3299 beq 3f
3300 slbmte r6, r5
33013: addi r8, r8, 16
3302 .endr
3303
33044: lwz r7, KVM_HOST_LPID(r10)
3305 mtspr SPRN_LPID, r7
3306 mtspr SPRN_PID, r0
3307 ld r8, KVM_HOST_LPCR(r10)
3308 mtspr SPRN_LPCR, r8
3309 isync
3310 li r0, KVM_GUEST_MODE_NONE
3311 stb r0, HSTATE_IN_GUEST(r13)
3312
3313 /*
3314 * Turn on the MMU and jump to C code
3315 */
3316 bcl 20, 31, .+4
33175: mflr r3
3318 addi r3, r3, 9f - 5b
3319 ld r4, PACAKMSR(r13)
3320 mtspr SPRN_SRR0, r3
3321 mtspr SPRN_SRR1, r4
3322 rfid
33239: addi r3, r1, STACK_FRAME_OVERHEAD
3324 bl kvmppc_bad_interrupt
3325 b 9b
e4e38121
MN
3326
3327/*
3328 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
3329 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
3330 * r11 has the guest MSR value (in/out)
3331 * r9 has a vcpu pointer (in)
3332 * r0 is used as a scratch register
3333 */
3334kvmppc_msr_interrupt:
3335 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
3336 cmpwi r0, 2 /* Check if we are in transactional state.. */
3337 ld r11, VCPU_INTR_MSR(r9)
3338 bne 1f
3339 /* ... if transactional, change to suspended */
3340 li r0, 1
33411: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
3342 blr
9bc01a9b
PM
3343
3344/*
3345 * This works around a hardware bug on POWER8E processors, where
3346 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
3347 * performance monitor interrupt. Instead, when we need to have
3348 * an interrupt pending, we have to arrange for a counter to overflow.
3349 */
3350kvmppc_fix_pmao:
3351 li r3, 0
3352 mtspr SPRN_MMCR2, r3
3353 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
3354 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
3355 mtspr SPRN_MMCR0, r3
3356 lis r3, 0x7fff
3357 ori r3, r3, 0xffff
3358 mtspr SPRN_PMC6, r3
3359 isync
3360 blr
b6c295df
PM
3361
3362#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
3363/*
3364 * Start timing an activity
3365 * r3 = pointer to time accumulation struct, r4 = vcpu
3366 */
3367kvmhv_start_timing:
3368 ld r5, HSTATE_KVM_VCORE(r13)
3369 lbz r6, VCORE_IN_GUEST(r5)
3370 cmpwi r6, 0
3371 beq 5f /* if in guest, need to */
3372 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
33735: mftb r5
3374 subf r5, r6, r5
3375 std r3, VCPU_CUR_ACTIVITY(r4)
3376 std r5, VCPU_ACTIVITY_START(r4)
3377 blr
3378
3379/*
3380 * Accumulate time to one activity and start another.
3381 * r3 = pointer to new time accumulation struct, r4 = vcpu
3382 */
3383kvmhv_accumulate_time:
3384 ld r5, HSTATE_KVM_VCORE(r13)
3385 lbz r8, VCORE_IN_GUEST(r5)
3386 cmpwi r8, 0
3387 beq 4f /* if in guest, need to */
3388 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */
33894: ld r5, VCPU_CUR_ACTIVITY(r4)
3390 ld r6, VCPU_ACTIVITY_START(r4)
3391 std r3, VCPU_CUR_ACTIVITY(r4)
3392 mftb r7
3393 subf r7, r8, r7
3394 std r7, VCPU_ACTIVITY_START(r4)
3395 cmpdi r5, 0
3396 beqlr
3397 subf r3, r6, r7
3398 ld r8, TAS_SEQCOUNT(r5)
3399 cmpdi r8, 0
3400 addi r8, r8, 1
3401 std r8, TAS_SEQCOUNT(r5)
3402 lwsync
3403 ld r7, TAS_TOTAL(r5)
3404 add r7, r7, r3
3405 std r7, TAS_TOTAL(r5)
3406 ld r6, TAS_MIN(r5)
3407 ld r7, TAS_MAX(r5)
3408 beq 3f
3409 cmpd r3, r6
3410 bge 1f
34113: std r3, TAS_MIN(r5)
34121: cmpd r3, r7
3413 ble 2f
3414 std r3, TAS_MAX(r5)
34152: lwsync
3416 addi r8, r8, 1
3417 std r8, TAS_SEQCOUNT(r5)
3418 blr
3419#endif