KVM: PPC: Book3S HV: Access host lppaca and shadow slb in BE
[deliverable/linux.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>
b4072df4 30#include <asm/mmu-hash64.h>
e4e38121
MN
31#include <asm/tm.h>
32
33#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
de56a948 34
e0b7ec05
PM
35/* Values in HSTATE_NAPPING(r13) */
36#define NAPPING_CEDE 1
37#define NAPPING_NOVCPU 2
38
de56a948 39/*
19ccb76a 40 * Call kvmppc_hv_entry in real mode.
de56a948
PM
41 * Must be called with interrupts hard-disabled.
42 *
43 * Input Registers:
44 *
45 * LR = return address to continue at after eventually re-enabling MMU
46 */
ad7d4584 47_GLOBAL_TOC(kvmppc_hv_entry_trampoline)
218309b7
PM
48 mflr r0
49 std r0, PPC_LR_STKOFF(r1)
50 stdu r1, -112(r1)
de56a948 51 mfmsr r10
218309b7 52 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
de56a948
PM
53 li r0,MSR_RI
54 andc r0,r10,r0
55 li r6,MSR_IR | MSR_DR
56 andc r6,r10,r6
57 mtmsrd r0,1 /* clear RI in MSR */
58 mtsrr0 r5
59 mtsrr1 r6
60 RFI
61
218309b7 62kvmppc_call_hv_entry:
e0b7ec05 63 ld r4, HSTATE_KVM_VCPU(r13)
218309b7
PM
64 bl kvmppc_hv_entry
65
66 /* Back from guest - restore host state and return to caller */
67
eee7ff9d 68BEGIN_FTR_SECTION
218309b7
PM
69 /* Restore host DABR and DABRX */
70 ld r5,HSTATE_DABR(r13)
71 li r6,7
72 mtspr SPRN_DABR,r5
73 mtspr SPRN_DABRX,r6
eee7ff9d 74END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
218309b7
PM
75
76 /* Restore SPRG3 */
9d378dfa
SW
77 ld r3,PACA_SPRG_VDSO(r13)
78 mtspr SPRN_SPRG_VDSO_WRITE,r3
218309b7 79
218309b7
PM
80 /* Reload the host's PMU registers */
81 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
82 lbz r4, LPPACA_PMCINUSE(r3)
83 cmpwi r4, 0
84 beq 23f /* skip if not */
9bc01a9b
PM
85BEGIN_FTR_SECTION
86 ld r3, HSTATE_MMCR(r13)
87 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
88 cmpwi r4, MMCR0_PMAO
89 beql kvmppc_fix_pmao
90END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
218309b7
PM
91 lwz r3, HSTATE_PMC(r13)
92 lwz r4, HSTATE_PMC + 4(r13)
93 lwz r5, HSTATE_PMC + 8(r13)
94 lwz r6, HSTATE_PMC + 12(r13)
95 lwz r8, HSTATE_PMC + 16(r13)
96 lwz r9, HSTATE_PMC + 20(r13)
97BEGIN_FTR_SECTION
98 lwz r10, HSTATE_PMC + 24(r13)
99 lwz r11, HSTATE_PMC + 28(r13)
100END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
101 mtspr SPRN_PMC1, r3
102 mtspr SPRN_PMC2, r4
103 mtspr SPRN_PMC3, r5
104 mtspr SPRN_PMC4, r6
105 mtspr SPRN_PMC5, r8
106 mtspr SPRN_PMC6, r9
107BEGIN_FTR_SECTION
108 mtspr SPRN_PMC7, r10
109 mtspr SPRN_PMC8, r11
110END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
111 ld r3, HSTATE_MMCR(r13)
112 ld r4, HSTATE_MMCR + 8(r13)
113 ld r5, HSTATE_MMCR + 16(r13)
72cde5a8
PM
114 ld r6, HSTATE_MMCR + 24(r13)
115 ld r7, HSTATE_MMCR + 32(r13)
218309b7
PM
116 mtspr SPRN_MMCR1, r4
117 mtspr SPRN_MMCRA, r5
72cde5a8
PM
118 mtspr SPRN_SIAR, r6
119 mtspr SPRN_SDAR, r7
120BEGIN_FTR_SECTION
121 ld r8, HSTATE_MMCR + 40(r13)
122 ld r9, HSTATE_MMCR + 48(r13)
123 mtspr SPRN_MMCR2, r8
124 mtspr SPRN_SIER, r9
125END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
218309b7
PM
126 mtspr SPRN_MMCR0, r3
127 isync
12823:
129
e0b7ec05
PM
130 /*
131 * Reload DEC. HDEC interrupts were disabled when
132 * we reloaded the host's LPCR value.
133 */
134 ld r3, HSTATE_DECEXP(r13)
135 mftb r4
136 subf r4, r4, r3
137 mtspr SPRN_DEC, r4
138
218309b7
PM
139 /*
140 * For external and machine check interrupts, we need
141 * to call the Linux handler to process the interrupt.
142 * We do that by jumping to absolute address 0x500 for
143 * external interrupts, or the machine_check_fwnmi label
144 * for machine checks (since firmware might have patched
145 * the vector area at 0x200). The [h]rfid at the end of the
146 * handler will return to the book3s_hv_interrupts.S code.
147 * For other interrupts we do the rfid to get back
148 * to the book3s_hv_interrupts.S code here.
149 */
150 ld r8, 112+PPC_LR_STKOFF(r1)
151 addi r1, r1, 112
152 ld r7, HSTATE_HOST_MSR(r13)
153
154 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
155 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
156BEGIN_FTR_SECTION
157 beq 11f
158END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
159
160 /* RFI into the highmem handler, or branch to interrupt handler */
161 mfmsr r6
162 li r0, MSR_RI
163 andc r6, r6, r0
164 mtmsrd r6, 1 /* Clear RI in MSR */
165 mtsrr0 r8
166 mtsrr1 r7
167 beqa 0x500 /* external interrupt (PPC970) */
168 beq cr1, 13f /* machine check */
169 RFI
170
171 /* On POWER7, we have external interrupts set to use HSRR0/1 */
17211: mtspr SPRN_HSRR0, r8
173 mtspr SPRN_HSRR1, r7
174 ba 0x500
175
17613: b machine_check_fwnmi
177
e0b7ec05
PM
178kvmppc_primary_no_guest:
179 /* We handle this much like a ceded vcpu */
180 /* set our bit in napping_threads */
181 ld r5, HSTATE_KVM_VCORE(r13)
182 lbz r7, HSTATE_PTID(r13)
183 li r0, 1
184 sld r0, r0, r7
185 addi r6, r5, VCORE_NAPPING_THREADS
1861: lwarx r3, 0, r6
187 or r3, r3, r0
188 stwcx. r3, 0, r6
189 bne 1b
190 /* order napping_threads update vs testing entry_exit_count */
191 isync
192 li r12, 0
193 lwz r7, VCORE_ENTRY_EXIT(r5)
194 cmpwi r7, 0x100
195 bge kvm_novcpu_exit /* another thread already exiting */
196 li r3, NAPPING_NOVCPU
197 stb r3, HSTATE_NAPPING(r13)
198 li r3, 1
199 stb r3, HSTATE_HWTHREAD_REQ(r13)
200
201 b kvm_do_nap
202
203kvm_novcpu_wakeup:
204 ld r1, HSTATE_HOST_R1(r13)
205 ld r5, HSTATE_KVM_VCORE(r13)
206 li r0, 0
207 stb r0, HSTATE_NAPPING(r13)
208 stb r0, HSTATE_HWTHREAD_REQ(r13)
209
e3bbbbfa
PM
210 /* check the wake reason */
211 bl kvmppc_check_wake_reason
212
e0b7ec05 213 /* see if any other thread is already exiting */
e0b7ec05
PM
214 lwz r0, VCORE_ENTRY_EXIT(r5)
215 cmpwi r0, 0x100
216 bge kvm_novcpu_exit
217
218 /* clear our bit in napping_threads */
219 lbz r7, HSTATE_PTID(r13)
220 li r0, 1
221 sld r0, r0, r7
222 addi r6, r5, VCORE_NAPPING_THREADS
e3bbbbfa
PM
2234: lwarx r7, 0, r6
224 andc r7, r7, r0
225 stwcx. r7, 0, r6
e0b7ec05
PM
226 bne 4b
227
e3bbbbfa 228 /* See if the wake reason means we need to exit */
e0b7ec05
PM
229 cmpdi r3, 0
230 bge kvm_novcpu_exit
e0b7ec05
PM
231
232 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
233 ld r4, HSTATE_KVM_VCPU(r13)
234 cmpdi r4, 0
235 bne kvmppc_got_guest
236
237kvm_novcpu_exit:
238 b hdec_soon
239
371fefd6 240/*
e0b7ec05 241 * We come in here when wakened from nap mode.
371fefd6
PM
242 * Relocation is off and most register values are lost.
243 * r13 points to the PACA.
244 */
245 .globl kvm_start_guest
246kvm_start_guest:
fd17dc7b
PM
247
248 /* Set runlatch bit the minute you wake up from nap */
249 mfspr r1, SPRN_CTRLF
250 ori r1, r1, 1
251 mtspr SPRN_CTRLT, r1
252
19ccb76a
PM
253 ld r2,PACATOC(r13)
254
f0888f70
PM
255 li r0,KVM_HWTHREAD_IN_KVM
256 stb r0,HSTATE_HWTHREAD_STATE(r13)
371fefd6 257
f0888f70
PM
258 /* NV GPR values from power7_idle() will no longer be valid */
259 li r0,1
260 stb r0,PACA_NAPSTATELOST(r13)
371fefd6 261
4619ac88
PM
262 /* were we napping due to cede? */
263 lbz r0,HSTATE_NAPPING(r13)
e0b7ec05
PM
264 cmpwi r0,NAPPING_CEDE
265 beq kvm_end_cede
266 cmpwi r0,NAPPING_NOVCPU
267 beq kvm_novcpu_wakeup
268
269 ld r1,PACAEMERGSP(r13)
270 subi r1,r1,STACK_FRAME_OVERHEAD
4619ac88
PM
271
272 /*
273 * We weren't napping due to cede, so this must be a secondary
274 * thread being woken up to run a guest, or being woken up due
275 * to a stray IPI. (Or due to some machine check or hypervisor
276 * maintenance interrupt while the core is in KVM.)
277 */
f0888f70
PM
278
279 /* Check the wake reason in SRR1 to see why we got here */
e3bbbbfa
PM
280 bl kvmppc_check_wake_reason
281 cmpdi r3, 0
282 bge kvm_no_guest
371fefd6 283
4619ac88 284 /* get vcpu pointer, NULL if we have no vcpu to run */
7b444c67
PM
285 ld r4,HSTATE_KVM_VCPU(r13)
286 cmpdi r4,0
f0888f70 287 /* if we have no vcpu to run, go back to sleep */
7b444c67 288 beq kvm_no_guest
f0888f70 289
e0b7ec05 290 /* Set HSTATE_DSCR(r13) to something sensible */
1739ea9e 291 ld r6, PACA_DSCR(r13)
e0b7ec05 292 std r6, HSTATE_DSCR(r13)
2fde6d20 293
e0b7ec05 294 bl kvmppc_hv_entry
218309b7
PM
295
296 /* Back from the guest, go back to nap */
297 /* Clear our vcpu pointer so we don't come back in early */
298 li r0, 0
299 std r0, HSTATE_KVM_VCPU(r13)
f019b7ad
PM
300 /*
301 * Make sure we clear HSTATE_KVM_VCPU(r13) before incrementing
302 * the nap_count, because once the increment to nap_count is
303 * visible we could be given another vcpu.
304 */
218309b7 305 lwsync
218309b7
PM
306
307 /* increment the nap count and then go to nap mode */
308 ld r4, HSTATE_KVM_VCORE(r13)
309 addi r4, r4, VCORE_NAP_COUNT
218309b7
PM
31051: lwarx r3, 0, r4
311 addi r3, r3, 1
312 stwcx. r3, 0, r4
313 bne 51b
314
315kvm_no_guest:
316 li r0, KVM_HWTHREAD_IN_NAP
317 stb r0, HSTATE_HWTHREAD_STATE(r13)
e0b7ec05 318kvm_do_nap:
582b910e
PM
319 /* Clear the runlatch bit before napping */
320 mfspr r2, SPRN_CTRLF
321 clrrdi r2, r2, 1
322 mtspr SPRN_CTRLT, r2
323
218309b7
PM
324 li r3, LPCR_PECE0
325 mfspr r4, SPRN_LPCR
326 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
327 mtspr SPRN_LPCR, r4
328 isync
329 std r0, HSTATE_SCRATCH0(r13)
330 ptesync
331 ld r0, HSTATE_SCRATCH0(r13)
3321: cmpd r0, r0
333 bne 1b
334 nap
335 b .
336
337/******************************************************************************
338 * *
339 * Entry code *
340 * *
341 *****************************************************************************/
342
de56a948
PM
343.global kvmppc_hv_entry
344kvmppc_hv_entry:
345
346 /* Required state:
347 *
e0b7ec05 348 * R4 = vcpu pointer (or NULL)
de56a948
PM
349 * MSR = ~IR|DR
350 * R13 = PACA
351 * R1 = host R1
352 * all other volatile GPRS = free
353 */
354 mflr r0
218309b7
PM
355 std r0, PPC_LR_STKOFF(r1)
356 stdu r1, -112(r1)
de56a948 357
de56a948
PM
358 /* Save R1 in the PACA */
359 std r1, HSTATE_HOST_R1(r13)
360
44a3add8
PM
361 li r6, KVM_GUEST_MODE_HOST_HV
362 stb r6, HSTATE_IN_GUEST(r13)
363
de56a948
PM
364 /* Clear out SLB */
365 li r6,0
366 slbmte r6,r6
367 slbia
368 ptesync
369
9e368f29
PM
370BEGIN_FTR_SECTION
371 b 30f
372END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
373 /*
374 * POWER7 host -> guest partition switch code.
375 * We don't have to lock against concurrent tlbies,
376 * but we do have to coordinate across hardware threads.
377 */
371fefd6
PM
378 /* Increment entry count iff exit count is zero. */
379 ld r5,HSTATE_KVM_VCORE(r13)
380 addi r9,r5,VCORE_ENTRY_EXIT
38121: lwarx r3,0,r9
382 cmpwi r3,0x100 /* any threads starting to exit? */
383 bge secondary_too_late /* if so we're too late to the party */
384 addi r3,r3,1
385 stwcx. r3,0,r9
386 bne 21b
387
388 /* Primary thread switches to guest partition. */
e0b7ec05
PM
389 ld r9,VCORE_KVM(r5) /* pointer to struct kvm */
390 lbz r6,HSTATE_PTID(r13)
371fefd6
PM
391 cmpwi r6,0
392 bne 20f
de56a948
PM
393 ld r6,KVM_SDR1(r9)
394 lwz r7,KVM_LPID(r9)
395 li r0,LPID_RSVD /* switch to reserved LPID */
396 mtspr SPRN_LPID,r0
397 ptesync
398 mtspr SPRN_SDR1,r6 /* switch to partition page table */
399 mtspr SPRN_LPID,r7
400 isync
1b400ba0
PM
401
402 /* See if we need to flush the TLB */
403 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
404 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
405 srdi r6,r6,6 /* doubleword number */
406 sldi r6,r6,3 /* address offset */
407 add r6,r6,r9
408 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
371fefd6 409 li r0,1
1b400ba0
PM
410 sld r0,r0,r7
411 ld r7,0(r6)
412 and. r7,r7,r0
413 beq 22f
41423: ldarx r7,0,r6 /* if set, clear the bit */
415 andc r7,r7,r0
416 stdcx. r7,0,r6
417 bne 23b
ca252055
PM
418 /* Flush the TLB of any entries for this LPID */
419 /* use arch 2.07S as a proxy for POWER8 */
420BEGIN_FTR_SECTION
421 li r6,512 /* POWER8 has 512 sets */
422FTR_SECTION_ELSE
423 li r6,128 /* POWER7 has 128 sets */
424ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
1b400ba0
PM
425 mtctr r6
426 li r7,0x800 /* IS field = 0b10 */
427 ptesync
42828: tlbiel r7
429 addi r7,r7,0x1000
430 bdnz 28b
431 ptesync
432
93b0f4dc
PM
433 /* Add timebase offset onto timebase */
43422: ld r8,VCORE_TB_OFFSET(r5)
435 cmpdi r8,0
436 beq 37f
437 mftb r6 /* current host timebase */
438 add r8,r8,r6
439 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
440 mftb r7 /* check if lower 24 bits overflowed */
441 clrldi r6,r6,40
442 clrldi r7,r7,40
443 cmpld r7,r6
444 bge 37f
445 addis r8,r8,0x100 /* if so, increment upper 40 bits */
446 mtspr SPRN_TBU40,r8
447
388cc6e1
PM
448 /* Load guest PCR value to select appropriate compat mode */
44937: ld r7, VCORE_PCR(r5)
450 cmpdi r7, 0
451 beq 38f
452 mtspr SPRN_PCR, r7
45338:
b005255e
MN
454
455BEGIN_FTR_SECTION
456 /* DPDES is shared between threads */
457 ld r8, VCORE_DPDES(r5)
458 mtspr SPRN_DPDES, r8
459END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
460
388cc6e1 461 li r0,1
371fefd6
PM
462 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
463 b 10f
464
465 /* Secondary threads wait for primary to have done partition switch */
46620: lbz r0,VCORE_IN_GUEST(r5)
467 cmpwi r0,0
468 beq 20b
aa04b4cc 469
19ccb76a 470 /* Set LPCR and RMOR. */
a0144e2a 47110: ld r8,VCORE_LPCR(r5)
19ccb76a 472 mtspr SPRN_LPCR,r8
aa04b4cc
PM
473 ld r8,KVM_RMOR(r9)
474 mtspr SPRN_RMOR,r8
de56a948
PM
475 isync
476
477 /* Check if HDEC expires soon */
478 mfspr r3,SPRN_HDEC
e0b7ec05 479 cmpwi r3,512 /* 1 microsecond */
de56a948 480 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
de56a948 481 blt hdec_soon
9e368f29
PM
482 b 31f
483
484 /*
485 * PPC970 host -> guest partition switch code.
486 * We have to lock against concurrent tlbies,
487 * using native_tlbie_lock to lock against host tlbies
488 * and kvm->arch.tlbie_lock to lock against guest tlbies.
489 * We also have to invalidate the TLB since its
490 * entries aren't tagged with the LPID.
491 */
e0b7ec05
PM
49230: ld r5,HSTATE_KVM_VCORE(r13)
493 ld r9,VCORE_KVM(r5) /* pointer to struct kvm */
9e368f29
PM
494
495 /* first take native_tlbie_lock */
496 .section ".toc","aw"
497toc_tlbie_lock:
498 .tc native_tlbie_lock[TC],native_tlbie_lock
499 .previous
500 ld r3,toc_tlbie_lock@toc(2)
54bb7f4b 501#ifdef __BIG_ENDIAN__
9e368f29 502 lwz r8,PACA_LOCK_TOKEN(r13)
54bb7f4b
AB
503#else
504 lwz r8,PACAPACAINDEX(r13)
505#endif
9e368f29
PM
50624: lwarx r0,0,r3
507 cmpwi r0,0
508 bne 24b
509 stwcx. r8,0,r3
510 bne 24b
511 isync
512
a0144e2a
PM
513 ld r5,HSTATE_KVM_VCORE(r13)
514 ld r7,VCORE_LPCR(r5) /* use vcore->lpcr to store HID4 */
9e368f29
PM
515 li r0,0x18f
516 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
517 or r0,r7,r0
518 ptesync
519 sync
520 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
521 isync
522 li r0,0
523 stw r0,0(r3) /* drop native_tlbie_lock */
524
525 /* invalidate the whole TLB */
526 li r0,256
527 mtctr r0
528 li r6,0
52925: tlbiel r6
530 addi r6,r6,0x1000
531 bdnz 25b
532 ptesync
533
534 /* Take the guest's tlbie_lock */
535 addi r3,r9,KVM_TLBIE_LOCK
53624: lwarx r0,0,r3
537 cmpwi r0,0
538 bne 24b
539 stwcx. r8,0,r3
540 bne 24b
541 isync
542 ld r6,KVM_SDR1(r9)
543 mtspr SPRN_SDR1,r6 /* switch to partition page table */
544
545 /* Set up HID4 with the guest's LPID etc. */
546 sync
547 mtspr SPRN_HID4,r7
548 isync
549
550 /* drop the guest's tlbie_lock */
551 li r0,0
552 stw r0,0(r3)
553
554 /* Check if HDEC expires soon */
555 mfspr r3,SPRN_HDEC
556 cmpwi r3,10
557 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
9e368f29
PM
558 blt hdec_soon
559
560 /* Enable HDEC interrupts */
561 mfspr r0,SPRN_HID0
562 li r3,1
563 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
564 sync
565 mtspr SPRN_HID0,r0
566 mfspr r0,SPRN_HID0
567 mfspr r0,SPRN_HID0
568 mfspr r0,SPRN_HID0
569 mfspr r0,SPRN_HID0
570 mfspr r0,SPRN_HID0
571 mfspr r0,SPRN_HID0
e0b7ec05
PM
57231:
573 /* Do we have a guest vcpu to run? */
574 cmpdi r4, 0
575 beq kvmppc_primary_no_guest
576kvmppc_got_guest:
de56a948
PM
577
578 /* Load up guest SLB entries */
e0b7ec05 579 lwz r5,VCPU_SLB_MAX(r4)
de56a948
PM
580 cmpwi r5,0
581 beq 9f
582 mtctr r5
583 addi r6,r4,VCPU_SLB
5841: ld r8,VCPU_SLB_E(r6)
585 ld r9,VCPU_SLB_V(r6)
586 slbmte r9,r8
587 addi r6,r6,VCPU_SLB_SIZE
588 bdnz 1b
5899:
e0b7ec05
PM
590 /* Increment yield count if they have a VPA */
591 ld r3, VCPU_VPA(r4)
592 cmpdi r3, 0
593 beq 25f
0865a583
AG
594 li r6, LPPACA_YIELDCOUNT
595 LWZX_BE r5, r3, r6
e0b7ec05 596 addi r5, r5, 1
0865a583 597 STWX_BE r5, r3, r6
e0b7ec05
PM
598 li r6, 1
599 stb r6, VCPU_VPA_DIRTY(r4)
60025:
601
602BEGIN_FTR_SECTION
603 /* Save purr/spurr */
604 mfspr r5,SPRN_PURR
605 mfspr r6,SPRN_SPURR
606 std r5,HSTATE_PURR(r13)
607 std r6,HSTATE_SPURR(r13)
608 ld r7,VCPU_PURR(r4)
609 ld r8,VCPU_SPURR(r4)
610 mtspr SPRN_PURR,r7
611 mtspr SPRN_SPURR,r8
612END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
613
614BEGIN_FTR_SECTION
615 /* Set partition DABR */
616 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
8563bf52 617 lwz r5,VCPU_DABRX(r4)
e0b7ec05
PM
618 ld r6,VCPU_DABR(r4)
619 mtspr SPRN_DABRX,r5
620 mtspr SPRN_DABR,r6
621 BEGIN_FTR_SECTION_NESTED(89)
622 isync
623 END_FTR_SECTION_NESTED(CPU_FTR_ARCH_206, CPU_FTR_ARCH_206, 89)
624END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
625
e4e38121
MN
626#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
627BEGIN_FTR_SECTION
628 b skip_tm
629END_FTR_SECTION_IFCLR(CPU_FTR_TM)
630
631 /* Turn on TM/FP/VSX/VMX so we can restore them. */
632 mfmsr r5
633 li r6, MSR_TM >> 32
634 sldi r6, r6, 32
635 or r5, r5, r6
636 ori r5, r5, MSR_FP
637 oris r5, r5, (MSR_VEC | MSR_VSX)@h
638 mtmsrd r5
639
640 /*
641 * The user may change these outside of a transaction, so they must
642 * always be context switched.
643 */
644 ld r5, VCPU_TFHAR(r4)
645 ld r6, VCPU_TFIAR(r4)
646 ld r7, VCPU_TEXASR(r4)
647 mtspr SPRN_TFHAR, r5
648 mtspr SPRN_TFIAR, r6
649 mtspr SPRN_TEXASR, r7
650
651 ld r5, VCPU_MSR(r4)
652 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
653 beq skip_tm /* TM not active in guest */
654
655 /* Make sure the failure summary is set, otherwise we'll program check
656 * when we trechkpt. It's possible that this might have been not set
657 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the
658 * host.
659 */
660 oris r7, r7, (TEXASR_FS)@h
661 mtspr SPRN_TEXASR, r7
662
663 /*
664 * We need to load up the checkpointed state for the guest.
665 * We need to do this early as it will blow away any GPRs, VSRs and
666 * some SPRs.
667 */
668
669 mr r31, r4
670 addi r3, r31, VCPU_FPRS_TM
671 bl .load_fp_state
672 addi r3, r31, VCPU_VRS_TM
673 bl .load_vr_state
674 mr r4, r31
675 lwz r7, VCPU_VRSAVE_TM(r4)
676 mtspr SPRN_VRSAVE, r7
677
678 ld r5, VCPU_LR_TM(r4)
679 lwz r6, VCPU_CR_TM(r4)
680 ld r7, VCPU_CTR_TM(r4)
681 ld r8, VCPU_AMR_TM(r4)
682 ld r9, VCPU_TAR_TM(r4)
683 mtlr r5
684 mtcr r6
685 mtctr r7
686 mtspr SPRN_AMR, r8
687 mtspr SPRN_TAR, r9
688
689 /*
690 * Load up PPR and DSCR values but don't put them in the actual SPRs
691 * till the last moment to avoid running with userspace PPR and DSCR for
692 * too long.
693 */
694 ld r29, VCPU_DSCR_TM(r4)
695 ld r30, VCPU_PPR_TM(r4)
696
697 std r2, PACATMSCRATCH(r13) /* Save TOC */
698
699 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
700 li r5, 0
701 mtmsrd r5, 1
702
703 /* Load GPRs r0-r28 */
704 reg = 0
705 .rept 29
706 ld reg, VCPU_GPRS_TM(reg)(r31)
707 reg = reg + 1
708 .endr
709
710 mtspr SPRN_DSCR, r29
711 mtspr SPRN_PPR, r30
712
713 /* Load final GPRs */
714 ld 29, VCPU_GPRS_TM(29)(r31)
715 ld 30, VCPU_GPRS_TM(30)(r31)
716 ld 31, VCPU_GPRS_TM(31)(r31)
717
718 /* TM checkpointed state is now setup. All GPRs are now volatile. */
719 TRECHKPT
720
721 /* Now let's get back the state we need. */
722 HMT_MEDIUM
723 GET_PACA(r13)
724 ld r29, HSTATE_DSCR(r13)
725 mtspr SPRN_DSCR, r29
726 ld r4, HSTATE_KVM_VCPU(r13)
727 ld r1, HSTATE_HOST_R1(r13)
728 ld r2, PACATMSCRATCH(r13)
729
730 /* Set the MSR RI since we have our registers back. */
731 li r5, MSR_RI
732 mtmsrd r5, 1
733skip_tm:
734#endif
735
e0b7ec05
PM
736 /* Load guest PMU registers */
737 /* R4 is live here (vcpu pointer) */
738 li r3, 1
739 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
740 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
741 isync
9bc01a9b
PM
742BEGIN_FTR_SECTION
743 ld r3, VCPU_MMCR(r4)
744 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO
745 cmpwi r5, MMCR0_PMAO
746 beql kvmppc_fix_pmao
747END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG)
e0b7ec05
PM
748 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
749 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
750 lwz r6, VCPU_PMC + 8(r4)
751 lwz r7, VCPU_PMC + 12(r4)
752 lwz r8, VCPU_PMC + 16(r4)
753 lwz r9, VCPU_PMC + 20(r4)
754BEGIN_FTR_SECTION
755 lwz r10, VCPU_PMC + 24(r4)
756 lwz r11, VCPU_PMC + 28(r4)
757END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
758 mtspr SPRN_PMC1, r3
759 mtspr SPRN_PMC2, r5
760 mtspr SPRN_PMC3, r6
761 mtspr SPRN_PMC4, r7
762 mtspr SPRN_PMC5, r8
763 mtspr SPRN_PMC6, r9
764BEGIN_FTR_SECTION
765 mtspr SPRN_PMC7, r10
766 mtspr SPRN_PMC8, r11
767END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
768 ld r3, VCPU_MMCR(r4)
769 ld r5, VCPU_MMCR + 8(r4)
770 ld r6, VCPU_MMCR + 16(r4)
771 ld r7, VCPU_SIAR(r4)
772 ld r8, VCPU_SDAR(r4)
773 mtspr SPRN_MMCR1, r5
774 mtspr SPRN_MMCRA, r6
775 mtspr SPRN_SIAR, r7
776 mtspr SPRN_SDAR, r8
b005255e
MN
777BEGIN_FTR_SECTION
778 ld r5, VCPU_MMCR + 24(r4)
779 ld r6, VCPU_SIER(r4)
780 lwz r7, VCPU_PMC + 24(r4)
781 lwz r8, VCPU_PMC + 28(r4)
782 ld r9, VCPU_MMCR + 32(r4)
783 mtspr SPRN_MMCR2, r5
784 mtspr SPRN_SIER, r6
785 mtspr SPRN_SPMC1, r7
786 mtspr SPRN_SPMC2, r8
787 mtspr SPRN_MMCRS, r9
788END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05
PM
789 mtspr SPRN_MMCR0, r3
790 isync
791
792 /* Load up FP, VMX and VSX registers */
793 bl kvmppc_load_fp
794
795 ld r14, VCPU_GPR(R14)(r4)
796 ld r15, VCPU_GPR(R15)(r4)
797 ld r16, VCPU_GPR(R16)(r4)
798 ld r17, VCPU_GPR(R17)(r4)
799 ld r18, VCPU_GPR(R18)(r4)
800 ld r19, VCPU_GPR(R19)(r4)
801 ld r20, VCPU_GPR(R20)(r4)
802 ld r21, VCPU_GPR(R21)(r4)
803 ld r22, VCPU_GPR(R22)(r4)
804 ld r23, VCPU_GPR(R23)(r4)
805 ld r24, VCPU_GPR(R24)(r4)
806 ld r25, VCPU_GPR(R25)(r4)
807 ld r26, VCPU_GPR(R26)(r4)
808 ld r27, VCPU_GPR(R27)(r4)
809 ld r28, VCPU_GPR(R28)(r4)
810 ld r29, VCPU_GPR(R29)(r4)
811 ld r30, VCPU_GPR(R30)(r4)
812 ld r31, VCPU_GPR(R31)(r4)
813
814BEGIN_FTR_SECTION
815 /* Switch DSCR to guest value */
816 ld r5, VCPU_DSCR(r4)
817 mtspr SPRN_DSCR, r5
818END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
819
b005255e
MN
820BEGIN_FTR_SECTION
821 /* Skip next section on POWER7 or PPC970 */
822 b 8f
823END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
824 /* Turn on TM so we can access TFHAR/TFIAR/TEXASR */
825 mfmsr r8
826 li r0, 1
827 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
828 mtmsrd r8
829
830 /* Load up POWER8-specific registers */
831 ld r5, VCPU_IAMR(r4)
832 lwz r6, VCPU_PSPB(r4)
833 ld r7, VCPU_FSCR(r4)
834 mtspr SPRN_IAMR, r5
835 mtspr SPRN_PSPB, r6
836 mtspr SPRN_FSCR, r7
837 ld r5, VCPU_DAWR(r4)
838 ld r6, VCPU_DAWRX(r4)
839 ld r7, VCPU_CIABR(r4)
840 ld r8, VCPU_TAR(r4)
841 mtspr SPRN_DAWR, r5
842 mtspr SPRN_DAWRX, r6
843 mtspr SPRN_CIABR, r7
844 mtspr SPRN_TAR, r8
845 ld r5, VCPU_IC(r4)
846 ld r6, VCPU_VTB(r4)
847 mtspr SPRN_IC, r5
848 mtspr SPRN_VTB, r6
7b490411 849 ld r8, VCPU_EBBHR(r4)
b005255e
MN
850 mtspr SPRN_EBBHR, r8
851 ld r5, VCPU_EBBRR(r4)
852 ld r6, VCPU_BESCR(r4)
853 ld r7, VCPU_CSIGR(r4)
854 ld r8, VCPU_TACR(r4)
855 mtspr SPRN_EBBRR, r5
856 mtspr SPRN_BESCR, r6
857 mtspr SPRN_CSIGR, r7
858 mtspr SPRN_TACR, r8
859 ld r5, VCPU_TCSCR(r4)
860 ld r6, VCPU_ACOP(r4)
861 lwz r7, VCPU_GUEST_PID(r4)
862 ld r8, VCPU_WORT(r4)
863 mtspr SPRN_TCSCR, r5
864 mtspr SPRN_ACOP, r6
865 mtspr SPRN_PID, r7
866 mtspr SPRN_WORT, r8
8678:
868
e0b7ec05
PM
869 /*
870 * Set the decrementer to the guest decrementer.
871 */
872 ld r8,VCPU_DEC_EXPIRES(r4)
c5fb80d3
PM
873 /* r8 is a host timebase value here, convert to guest TB */
874 ld r5,HSTATE_KVM_VCORE(r13)
875 ld r6,VCORE_TB_OFFSET(r5)
876 add r8,r8,r6
e0b7ec05
PM
877 mftb r7
878 subf r3,r7,r8
879 mtspr SPRN_DEC,r3
880 stw r3,VCPU_DEC(r4)
881
882 ld r5, VCPU_SPRG0(r4)
883 ld r6, VCPU_SPRG1(r4)
884 ld r7, VCPU_SPRG2(r4)
885 ld r8, VCPU_SPRG3(r4)
886 mtspr SPRN_SPRG0, r5
887 mtspr SPRN_SPRG1, r6
888 mtspr SPRN_SPRG2, r7
889 mtspr SPRN_SPRG3, r8
890
891 /* Load up DAR and DSISR */
892 ld r5, VCPU_DAR(r4)
893 lwz r6, VCPU_DSISR(r4)
894 mtspr SPRN_DAR, r5
895 mtspr SPRN_DSISR, r6
896
897BEGIN_FTR_SECTION
898 /* Restore AMR and UAMOR, set AMOR to all 1s */
899 ld r5,VCPU_AMR(r4)
900 ld r6,VCPU_UAMOR(r4)
901 li r7,-1
902 mtspr SPRN_AMR,r5
903 mtspr SPRN_UAMOR,r6
904 mtspr SPRN_AMOR,r7
905END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
de56a948
PM
906
907 /* Restore state of CTRL run bit; assume 1 on entry */
908 lwz r5,VCPU_CTRL(r4)
909 andi. r5,r5,1
910 bne 4f
911 mfspr r6,SPRN_CTRLF
912 clrrdi r6,r6,1
913 mtspr SPRN_CTRLT,r6
9144:
915 ld r6, VCPU_CTR(r4)
916 lwz r7, VCPU_XER(r4)
917
918 mtctr r6
919 mtxer r7
920
e3bbbbfa 921kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
4619ac88
PM
922 ld r10, VCPU_PC(r4)
923 ld r11, VCPU_MSR(r4)
de56a948
PM
924 ld r6, VCPU_SRR0(r4)
925 ld r7, VCPU_SRR1(r4)
e3bbbbfa
PM
926 mtspr SPRN_SRR0, r6
927 mtspr SPRN_SRR1, r7
de56a948 928
e3bbbbfa 929deliver_guest_interrupt:
4619ac88 930 /* r11 = vcpu->arch.msr & ~MSR_HV */
de56a948
PM
931 rldicl r11, r11, 63 - MSR_HV_LG, 1
932 rotldi r11, r11, 1 + MSR_HV_LG
933 ori r11, r11, MSR_ME
934
19ccb76a 935 /* Check if we can deliver an external or decrementer interrupt now */
e3bbbbfa
PM
936 ld r0, VCPU_PENDING_EXC(r4)
937 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
938 cmpdi cr1, r0, 0
939 andi. r8, r11, MSR_EE
19ccb76a 940BEGIN_FTR_SECTION
e3bbbbfa
PM
941 mfspr r8, SPRN_LPCR
942 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
943 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
944 mtspr SPRN_LPCR, r8
19ccb76a
PM
945 isync
946END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
947 beq 5f
e3bbbbfa
PM
948 li r0, BOOK3S_INTERRUPT_EXTERNAL
949 bne cr1, 12f
950 mfspr r0, SPRN_DEC
951 cmpwi r0, 0
952 li r0, BOOK3S_INTERRUPT_DECREMENTER
953 bge 5f
19ccb76a 954
e3bbbbfa 95512: mtspr SPRN_SRR0, r10
19ccb76a 956 mr r10,r0
e3bbbbfa 957 mtspr SPRN_SRR1, r11
e4e38121
MN
958 mr r9, r4
959 bl kvmppc_msr_interrupt
e3bbbbfa 9605:
19ccb76a 961
27025a60
LPF
962/*
963 * Required state:
964 * R4 = vcpu
965 * R10: value for HSRR0
966 * R11: value for HSRR1
967 * R13 = PACA
968 */
de56a948 969fast_guest_return:
4619ac88
PM
970 li r0,0
971 stb r0,VCPU_CEDED(r4) /* cancel cede */
de56a948
PM
972 mtspr SPRN_HSRR0,r10
973 mtspr SPRN_HSRR1,r11
974
975 /* Activate guest mode, so faults get handled by KVM */
44a3add8 976 li r9, KVM_GUEST_MODE_GUEST_HV
de56a948
PM
977 stb r9, HSTATE_IN_GUEST(r13)
978
979 /* Enter guest */
980
0acb9111
PM
981BEGIN_FTR_SECTION
982 ld r5, VCPU_CFAR(r4)
983 mtspr SPRN_CFAR, r5
984END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
4b8473c9
PM
985BEGIN_FTR_SECTION
986 ld r0, VCPU_PPR(r4)
987END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
0acb9111 988
de56a948
PM
989 ld r5, VCPU_LR(r4)
990 lwz r6, VCPU_CR(r4)
991 mtlr r5
992 mtcr r6
993
c75df6f9
MN
994 ld r1, VCPU_GPR(R1)(r4)
995 ld r2, VCPU_GPR(R2)(r4)
996 ld r3, VCPU_GPR(R3)(r4)
997 ld r5, VCPU_GPR(R5)(r4)
998 ld r6, VCPU_GPR(R6)(r4)
999 ld r7, VCPU_GPR(R7)(r4)
1000 ld r8, VCPU_GPR(R8)(r4)
1001 ld r9, VCPU_GPR(R9)(r4)
1002 ld r10, VCPU_GPR(R10)(r4)
1003 ld r11, VCPU_GPR(R11)(r4)
1004 ld r12, VCPU_GPR(R12)(r4)
1005 ld r13, VCPU_GPR(R13)(r4)
1006
4b8473c9
PM
1007BEGIN_FTR_SECTION
1008 mtspr SPRN_PPR, r0
1009END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
1010 ld r0, VCPU_GPR(R0)(r4)
c75df6f9 1011 ld r4, VCPU_GPR(R4)(r4)
de56a948
PM
1012
1013 hrfid
1014 b .
1015
1016/******************************************************************************
1017 * *
1018 * Exit code *
1019 * *
1020 *****************************************************************************/
1021
1022/*
1023 * We come here from the first-level interrupt handlers.
1024 */
dd96b2c2
AK
1025 .globl kvmppc_interrupt_hv
1026kvmppc_interrupt_hv:
de56a948
PM
1027 /*
1028 * Register contents:
1029 * R12 = interrupt vector
1030 * R13 = PACA
1031 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
1032 * guest R13 saved in SPRN_SCRATCH0
1033 */
36e7bb38 1034 std r9, HSTATE_SCRATCH2(r13)
44a3add8
PM
1035
1036 lbz r9, HSTATE_IN_GUEST(r13)
1037 cmpwi r9, KVM_GUEST_MODE_HOST_HV
1038 beq kvmppc_bad_host_intr
dd96b2c2
AK
1039#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1040 cmpwi r9, KVM_GUEST_MODE_GUEST
36e7bb38 1041 ld r9, HSTATE_SCRATCH2(r13)
dd96b2c2
AK
1042 beq kvmppc_interrupt_pr
1043#endif
44a3add8
PM
1044 /* We're now back in the host but in guest MMU context */
1045 li r9, KVM_GUEST_MODE_HOST_HV
1046 stb r9, HSTATE_IN_GUEST(r13)
1047
de56a948
PM
1048 ld r9, HSTATE_KVM_VCPU(r13)
1049
1050 /* Save registers */
1051
c75df6f9
MN
1052 std r0, VCPU_GPR(R0)(r9)
1053 std r1, VCPU_GPR(R1)(r9)
1054 std r2, VCPU_GPR(R2)(r9)
1055 std r3, VCPU_GPR(R3)(r9)
1056 std r4, VCPU_GPR(R4)(r9)
1057 std r5, VCPU_GPR(R5)(r9)
1058 std r6, VCPU_GPR(R6)(r9)
1059 std r7, VCPU_GPR(R7)(r9)
1060 std r8, VCPU_GPR(R8)(r9)
36e7bb38 1061 ld r0, HSTATE_SCRATCH2(r13)
c75df6f9
MN
1062 std r0, VCPU_GPR(R9)(r9)
1063 std r10, VCPU_GPR(R10)(r9)
1064 std r11, VCPU_GPR(R11)(r9)
de56a948
PM
1065 ld r3, HSTATE_SCRATCH0(r13)
1066 lwz r4, HSTATE_SCRATCH1(r13)
c75df6f9 1067 std r3, VCPU_GPR(R12)(r9)
de56a948 1068 stw r4, VCPU_CR(r9)
0acb9111
PM
1069BEGIN_FTR_SECTION
1070 ld r3, HSTATE_CFAR(r13)
1071 std r3, VCPU_CFAR(r9)
1072END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
4b8473c9
PM
1073BEGIN_FTR_SECTION
1074 ld r4, HSTATE_PPR(r13)
1075 std r4, VCPU_PPR(r9)
1076END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
de56a948
PM
1077
1078 /* Restore R1/R2 so we can handle faults */
1079 ld r1, HSTATE_HOST_R1(r13)
1080 ld r2, PACATOC(r13)
1081
1082 mfspr r10, SPRN_SRR0
1083 mfspr r11, SPRN_SRR1
1084 std r10, VCPU_SRR0(r9)
1085 std r11, VCPU_SRR1(r9)
1086 andi. r0, r12, 2 /* need to read HSRR0/1? */
1087 beq 1f
1088 mfspr r10, SPRN_HSRR0
1089 mfspr r11, SPRN_HSRR1
1090 clrrdi r12, r12, 2
10911: std r10, VCPU_PC(r9)
1092 std r11, VCPU_MSR(r9)
1093
1094 GET_SCRATCH0(r3)
1095 mflr r4
c75df6f9 1096 std r3, VCPU_GPR(R13)(r9)
de56a948
PM
1097 std r4, VCPU_LR(r9)
1098
de56a948
PM
1099 stw r12,VCPU_TRAP(r9)
1100
697d3899
PM
1101 /* Save HEIR (HV emulation assist reg) in last_inst
1102 if this is an HEI (HV emulation interrupt, e40) */
1103 li r3,KVM_INST_FETCH_FAILED
1104BEGIN_FTR_SECTION
1105 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
1106 bne 11f
1107 mfspr r3,SPRN_HEIR
1108END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
110911: stw r3,VCPU_LAST_INST(r9)
1110
1111 /* these are volatile across C function calls */
1112 mfctr r3
1113 mfxer r4
1114 std r3, VCPU_CTR(r9)
1115 stw r4, VCPU_XER(r9)
1116
1117BEGIN_FTR_SECTION
1118 /* If this is a page table miss then see if it's theirs or ours */
1119 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1120 beq kvmppc_hdsi
342d3db7
PM
1121 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1122 beq kvmppc_hisi
697d3899
PM
1123END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1124
de56a948
PM
1125 /* See if this is a leftover HDEC interrupt */
1126 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1127 bne 2f
1128 mfspr r3,SPRN_HDEC
1129 cmpwi r3,0
1130 bge ignore_hdec
11312:
697d3899 1132 /* See if this is an hcall we can handle in real mode */
a8606e20
PM
1133 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
1134 beq hcall_try_real_mode
de56a948 1135
54695c30 1136 /* Only handle external interrupts here on arch 206 and later */
9e368f29 1137BEGIN_FTR_SECTION
54695c30
BH
1138 b ext_interrupt_to_host
1139END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1140
1141 /* External interrupt ? */
1142 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
1143 bne+ ext_interrupt_to_host
1144
1145 /* External interrupt, first check for host_ipi. If this is
1146 * set, we know the host wants us out so let's do it now
1147 */
c934243c
PM
1148 bl kvmppc_read_intr
1149 cmpdi r3, 0
1150 bgt ext_interrupt_to_host
54695c30 1151
4619ac88
PM
1152 /* Check if any CPU is heading out to the host, if so head out too */
1153 ld r5, HSTATE_KVM_VCORE(r13)
1154 lwz r0, VCORE_ENTRY_EXIT(r5)
1155 cmpwi r0, 0x100
1156 bge ext_interrupt_to_host
1157
e3bbbbfa
PM
1158 /* Return to guest after delivering any pending interrupt */
1159 mr r4, r9
1160 b deliver_guest_interrupt
54695c30 1161
54695c30 1162ext_interrupt_to_host:
de56a948 1163
b4072df4 1164guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
de56a948 1165 /* Save more register state */
de56a948
PM
1166 mfdar r6
1167 mfdsisr r7
de56a948
PM
1168 std r6, VCPU_DAR(r9)
1169 stw r7, VCPU_DSISR(r9)
9e368f29 1170BEGIN_FTR_SECTION
697d3899 1171 /* don't overwrite fault_dar/fault_dsisr if HDSI */
de56a948
PM
1172 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1173 beq 6f
9e368f29 1174END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
697d3899 1175 std r6, VCPU_FAULT_DAR(r9)
de56a948
PM
1176 stw r7, VCPU_FAULT_DSISR(r9)
1177
b4072df4
PM
1178 /* See if it is a machine check */
1179 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1180 beq machine_check_realmode
1181mc_cont:
1182
de56a948 1183 /* Save guest CTRL register, set runlatch to 1 */
697d3899 11846: mfspr r6,SPRN_CTRLF
de56a948
PM
1185 stw r6,VCPU_CTRL(r9)
1186 andi. r0,r6,1
1187 bne 4f
1188 ori r6,r6,1
1189 mtspr SPRN_CTRLT,r6
11904:
1191 /* Read the guest SLB and save it away */
1192 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1193 mtctr r0
1194 li r6,0
1195 addi r7,r9,VCPU_SLB
1196 li r5,0
11971: slbmfee r8,r6
1198 andis. r0,r8,SLB_ESID_V@h
1199 beq 2f
1200 add r8,r8,r6 /* put index in */
1201 slbmfev r3,r6
1202 std r8,VCPU_SLB_E(r7)
1203 std r3,VCPU_SLB_V(r7)
1204 addi r7,r7,VCPU_SLB_SIZE
1205 addi r5,r5,1
12062: addi r6,r6,1
1207 bdnz 1b
1208 stw r5,VCPU_SLB_MAX(r9)
1209
1210 /*
1211 * Save the guest PURR/SPURR
1212 */
9e368f29 1213BEGIN_FTR_SECTION
de56a948
PM
1214 mfspr r5,SPRN_PURR
1215 mfspr r6,SPRN_SPURR
1216 ld r7,VCPU_PURR(r9)
1217 ld r8,VCPU_SPURR(r9)
1218 std r5,VCPU_PURR(r9)
1219 std r6,VCPU_SPURR(r9)
1220 subf r5,r7,r5
1221 subf r6,r8,r6
1222
1223 /*
1224 * Restore host PURR/SPURR and add guest times
1225 * so that the time in the guest gets accounted.
1226 */
1227 ld r3,HSTATE_PURR(r13)
1228 ld r4,HSTATE_SPURR(r13)
1229 add r3,r3,r5
1230 add r4,r4,r6
1231 mtspr SPRN_PURR,r3
1232 mtspr SPRN_SPURR,r4
9e368f29 1233END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
de56a948 1234
e0b7ec05
PM
1235 /* Save DEC */
1236 mfspr r5,SPRN_DEC
1237 mftb r6
1238 extsw r5,r5
1239 add r5,r5,r6
c5fb80d3
PM
1240 /* r5 is a guest timebase value here, convert to host TB */
1241 ld r3,HSTATE_KVM_VCORE(r13)
1242 ld r4,VCORE_TB_OFFSET(r3)
1243 subf r5,r4,r5
e0b7ec05
PM
1244 std r5,VCPU_DEC_EXPIRES(r9)
1245
b005255e
MN
1246BEGIN_FTR_SECTION
1247 b 8f
1248END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
b005255e
MN
1249 /* Save POWER8-specific registers */
1250 mfspr r5, SPRN_IAMR
1251 mfspr r6, SPRN_PSPB
1252 mfspr r7, SPRN_FSCR
1253 std r5, VCPU_IAMR(r9)
1254 stw r6, VCPU_PSPB(r9)
1255 std r7, VCPU_FSCR(r9)
1256 mfspr r5, SPRN_IC
1257 mfspr r6, SPRN_VTB
1258 mfspr r7, SPRN_TAR
1259 std r5, VCPU_IC(r9)
1260 std r6, VCPU_VTB(r9)
1261 std r7, VCPU_TAR(r9)
7b490411 1262 mfspr r8, SPRN_EBBHR
b005255e
MN
1263 std r8, VCPU_EBBHR(r9)
1264 mfspr r5, SPRN_EBBRR
1265 mfspr r6, SPRN_BESCR
1266 mfspr r7, SPRN_CSIGR
1267 mfspr r8, SPRN_TACR
1268 std r5, VCPU_EBBRR(r9)
1269 std r6, VCPU_BESCR(r9)
1270 std r7, VCPU_CSIGR(r9)
1271 std r8, VCPU_TACR(r9)
1272 mfspr r5, SPRN_TCSCR
1273 mfspr r6, SPRN_ACOP
1274 mfspr r7, SPRN_PID
1275 mfspr r8, SPRN_WORT
1276 std r5, VCPU_TCSCR(r9)
1277 std r6, VCPU_ACOP(r9)
1278 stw r7, VCPU_GUEST_PID(r9)
1279 std r8, VCPU_WORT(r9)
12808:
1281
e0b7ec05
PM
1282 /* Save and reset AMR and UAMOR before turning on the MMU */
1283BEGIN_FTR_SECTION
1284 mfspr r5,SPRN_AMR
1285 mfspr r6,SPRN_UAMOR
1286 std r5,VCPU_AMR(r9)
1287 std r6,VCPU_UAMOR(r9)
1288 li r6,0
1289 mtspr SPRN_AMR,r6
1290END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1291
1292 /* Switch DSCR back to host value */
1293BEGIN_FTR_SECTION
1294 mfspr r8, SPRN_DSCR
1295 ld r7, HSTATE_DSCR(r13)
1296 std r8, VCPU_DSCR(r9)
1297 mtspr SPRN_DSCR, r7
1298END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1299
1300 /* Save non-volatile GPRs */
1301 std r14, VCPU_GPR(R14)(r9)
1302 std r15, VCPU_GPR(R15)(r9)
1303 std r16, VCPU_GPR(R16)(r9)
1304 std r17, VCPU_GPR(R17)(r9)
1305 std r18, VCPU_GPR(R18)(r9)
1306 std r19, VCPU_GPR(R19)(r9)
1307 std r20, VCPU_GPR(R20)(r9)
1308 std r21, VCPU_GPR(R21)(r9)
1309 std r22, VCPU_GPR(R22)(r9)
1310 std r23, VCPU_GPR(R23)(r9)
1311 std r24, VCPU_GPR(R24)(r9)
1312 std r25, VCPU_GPR(R25)(r9)
1313 std r26, VCPU_GPR(R26)(r9)
1314 std r27, VCPU_GPR(R27)(r9)
1315 std r28, VCPU_GPR(R28)(r9)
1316 std r29, VCPU_GPR(R29)(r9)
1317 std r30, VCPU_GPR(R30)(r9)
1318 std r31, VCPU_GPR(R31)(r9)
1319
1320 /* Save SPRGs */
1321 mfspr r3, SPRN_SPRG0
1322 mfspr r4, SPRN_SPRG1
1323 mfspr r5, SPRN_SPRG2
1324 mfspr r6, SPRN_SPRG3
1325 std r3, VCPU_SPRG0(r9)
1326 std r4, VCPU_SPRG1(r9)
1327 std r5, VCPU_SPRG2(r9)
1328 std r6, VCPU_SPRG3(r9)
1329
1330 /* save FP state */
1331 mr r3, r9
1332 bl kvmppc_save_fp
de56a948 1333
0a8eccef
PM
1334#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1335BEGIN_FTR_SECTION
1336 b 2f
1337END_FTR_SECTION_IFCLR(CPU_FTR_TM)
1338 /* Turn on TM. */
1339 mfmsr r8
1340 li r0, 1
1341 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG
1342 mtmsrd r8
1343
1344 ld r5, VCPU_MSR(r9)
1345 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62
1346 beq 1f /* TM not active in guest. */
1347
1348 li r3, TM_CAUSE_KVM_RESCHED
1349
1350 /* Clear the MSR RI since r1, r13 are all going to be foobar. */
1351 li r5, 0
1352 mtmsrd r5, 1
1353
1354 /* All GPRs are volatile at this point. */
1355 TRECLAIM(R3)
1356
1357 /* Temporarily store r13 and r9 so we have some regs to play with */
1358 SET_SCRATCH0(r13)
1359 GET_PACA(r13)
1360 std r9, PACATMSCRATCH(r13)
1361 ld r9, HSTATE_KVM_VCPU(r13)
1362
1363 /* Get a few more GPRs free. */
1364 std r29, VCPU_GPRS_TM(29)(r9)
1365 std r30, VCPU_GPRS_TM(30)(r9)
1366 std r31, VCPU_GPRS_TM(31)(r9)
1367
1368 /* Save away PPR and DSCR soon so don't run with user values. */
1369 mfspr r31, SPRN_PPR
1370 HMT_MEDIUM
1371 mfspr r30, SPRN_DSCR
1372 ld r29, HSTATE_DSCR(r13)
1373 mtspr SPRN_DSCR, r29
1374
1375 /* Save all but r9, r13 & r29-r31 */
1376 reg = 0
1377 .rept 29
1378 .if (reg != 9) && (reg != 13)
1379 std reg, VCPU_GPRS_TM(reg)(r9)
1380 .endif
1381 reg = reg + 1
1382 .endr
1383 /* ... now save r13 */
1384 GET_SCRATCH0(r4)
1385 std r4, VCPU_GPRS_TM(13)(r9)
1386 /* ... and save r9 */
1387 ld r4, PACATMSCRATCH(r13)
1388 std r4, VCPU_GPRS_TM(9)(r9)
1389
1390 /* Reload stack pointer and TOC. */
1391 ld r1, HSTATE_HOST_R1(r13)
1392 ld r2, PACATOC(r13)
1393
1394 /* Set MSR RI now we have r1 and r13 back. */
1395 li r5, MSR_RI
1396 mtmsrd r5, 1
1397
1398 /* Save away checkpinted SPRs. */
1399 std r31, VCPU_PPR_TM(r9)
1400 std r30, VCPU_DSCR_TM(r9)
1401 mflr r5
1402 mfcr r6
1403 mfctr r7
1404 mfspr r8, SPRN_AMR
1405 mfspr r10, SPRN_TAR
1406 std r5, VCPU_LR_TM(r9)
1407 stw r6, VCPU_CR_TM(r9)
1408 std r7, VCPU_CTR_TM(r9)
1409 std r8, VCPU_AMR_TM(r9)
1410 std r10, VCPU_TAR_TM(r9)
1411
1412 /* Restore r12 as trap number. */
1413 lwz r12, VCPU_TRAP(r9)
1414
1415 /* Save FP/VSX. */
1416 addi r3, r9, VCPU_FPRS_TM
1417 bl .store_fp_state
1418 addi r3, r9, VCPU_VRS_TM
1419 bl .store_vr_state
1420 mfspr r6, SPRN_VRSAVE
1421 stw r6, VCPU_VRSAVE_TM(r9)
14221:
1423 /*
1424 * We need to save these SPRs after the treclaim so that the software
1425 * error code is recorded correctly in the TEXASR. Also the user may
1426 * change these outside of a transaction, so they must always be
1427 * context switched.
1428 */
1429 mfspr r5, SPRN_TFHAR
1430 mfspr r6, SPRN_TFIAR
1431 mfspr r7, SPRN_TEXASR
1432 std r5, VCPU_TFHAR(r9)
1433 std r6, VCPU_TFIAR(r9)
1434 std r7, VCPU_TEXASR(r9)
14352:
1436#endif
1437
e0b7ec05
PM
1438 /* Increment yield count if they have a VPA */
1439 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1440 cmpdi r8, 0
1441 beq 25f
0865a583
AG
1442 li r4, LPPACA_YIELDCOUNT
1443 LWZX_BE r3, r8, r4
e0b7ec05 1444 addi r3, r3, 1
0865a583 1445 STWX_BE r3, r8, r4
e0b7ec05
PM
1446 li r3, 1
1447 stb r3, VCPU_VPA_DIRTY(r9)
144825:
1449 /* Save PMU registers if requested */
1450 /* r8 and cr0.eq are live here */
9bc01a9b
PM
1451BEGIN_FTR_SECTION
1452 /*
1453 * POWER8 seems to have a hardware bug where setting
1454 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE]
1455 * when some counters are already negative doesn't seem
1456 * to cause a performance monitor alert (and hence interrupt).
1457 * The effect of this is that when saving the PMU state,
1458 * if there is no PMU alert pending when we read MMCR0
1459 * before freezing the counters, but one becomes pending
1460 * before we read the counters, we lose it.
1461 * To work around this, we need a way to freeze the counters
1462 * before reading MMCR0. Normally, freezing the counters
1463 * is done by writing MMCR0 (to set MMCR0[FC]) which
1464 * unavoidably writes MMCR0[PMA0] as well. On POWER8,
1465 * we can also freeze the counters using MMCR2, by writing
1466 * 1s to all the counter freeze condition bits (there are
1467 * 9 bits each for 6 counters).
1468 */
1469 li r3, -1 /* set all freeze bits */
1470 clrrdi r3, r3, 10
1471 mfspr r10, SPRN_MMCR2
1472 mtspr SPRN_MMCR2, r3
1473 isync
1474END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05
PM
1475 li r3, 1
1476 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1477 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1478 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
1479 mfspr r6, SPRN_MMCRA
1480BEGIN_FTR_SECTION
1481 /* On P7, clear MMCRA in order to disable SDAR updates */
1482 li r7, 0
1483 mtspr SPRN_MMCRA, r7
1484END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1485 isync
1486 beq 21f /* if no VPA, save PMU stuff anyway */
1487 lbz r7, LPPACA_PMCINUSE(r8)
1488 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1489 bne 21f
1490 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1491 b 22f
149221: mfspr r5, SPRN_MMCR1
1493 mfspr r7, SPRN_SIAR
1494 mfspr r8, SPRN_SDAR
1495 std r4, VCPU_MMCR(r9)
1496 std r5, VCPU_MMCR + 8(r9)
1497 std r6, VCPU_MMCR + 16(r9)
9bc01a9b
PM
1498BEGIN_FTR_SECTION
1499 std r10, VCPU_MMCR + 24(r9)
1500END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05
PM
1501 std r7, VCPU_SIAR(r9)
1502 std r8, VCPU_SDAR(r9)
1503 mfspr r3, SPRN_PMC1
1504 mfspr r4, SPRN_PMC2
1505 mfspr r5, SPRN_PMC3
1506 mfspr r6, SPRN_PMC4
1507 mfspr r7, SPRN_PMC5
1508 mfspr r8, SPRN_PMC6
1509BEGIN_FTR_SECTION
1510 mfspr r10, SPRN_PMC7
1511 mfspr r11, SPRN_PMC8
1512END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1513 stw r3, VCPU_PMC(r9)
1514 stw r4, VCPU_PMC + 4(r9)
1515 stw r5, VCPU_PMC + 8(r9)
1516 stw r6, VCPU_PMC + 12(r9)
1517 stw r7, VCPU_PMC + 16(r9)
1518 stw r8, VCPU_PMC + 20(r9)
1519BEGIN_FTR_SECTION
1520 stw r10, VCPU_PMC + 24(r9)
1521 stw r11, VCPU_PMC + 28(r9)
1522END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
b005255e 1523BEGIN_FTR_SECTION
b005255e
MN
1524 mfspr r5, SPRN_SIER
1525 mfspr r6, SPRN_SPMC1
1526 mfspr r7, SPRN_SPMC2
1527 mfspr r8, SPRN_MMCRS
b005255e
MN
1528 std r5, VCPU_SIER(r9)
1529 stw r6, VCPU_PMC + 24(r9)
1530 stw r7, VCPU_PMC + 28(r9)
1531 std r8, VCPU_MMCR + 32(r9)
1532 lis r4, 0x8000
1533 mtspr SPRN_MMCRS, r4
1534END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e0b7ec05 153522:
de56a948
PM
1536 /* Clear out SLB */
1537 li r5,0
1538 slbmte r5,r5
1539 slbia
1540 ptesync
1541
e0b7ec05 1542hdec_soon: /* r12 = trap, r13 = paca */
9e368f29
PM
1543BEGIN_FTR_SECTION
1544 b 32f
1545END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1546 /*
1547 * POWER7 guest -> host partition switch code.
1548 * We don't have to lock against tlbies but we do
1549 * have to coordinate the hardware threads.
1550 */
371fefd6
PM
1551 /* Increment the threads-exiting-guest count in the 0xff00
1552 bits of vcore->entry_exit_count */
371fefd6
PM
1553 ld r5,HSTATE_KVM_VCORE(r13)
1554 addi r6,r5,VCORE_ENTRY_EXIT
155541: lwarx r3,0,r6
1556 addi r0,r3,0x100
1557 stwcx. r0,0,r6
1558 bne 41b
f019b7ad 1559 isync /* order stwcx. vs. reading napping_threads */
371fefd6
PM
1560
1561 /*
1562 * At this point we have an interrupt that we have to pass
1563 * up to the kernel or qemu; we can't handle it in real mode.
1564 * Thus we have to do a partition switch, so we have to
1565 * collect the other threads, if we are the first thread
1566 * to take an interrupt. To do this, we set the HDEC to 0,
1567 * which causes an HDEC interrupt in all threads within 2ns
1568 * because the HDEC register is shared between all 4 threads.
1569 * However, we don't need to bother if this is an HDEC
1570 * interrupt, since the other threads will already be on their
1571 * way here in that case.
1572 */
19ccb76a
PM
1573 cmpwi r3,0x100 /* Are we the first here? */
1574 bge 43f
371fefd6
PM
1575 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1576 beq 40f
371fefd6
PM
1577 li r0,0
1578 mtspr SPRN_HDEC,r0
157940:
19ccb76a
PM
1580 /*
1581 * Send an IPI to any napping threads, since an HDEC interrupt
1582 * doesn't wake CPUs up from nap.
1583 */
1584 lwz r3,VCORE_NAPPING_THREADS(r5)
e0b7ec05 1585 lbz r4,HSTATE_PTID(r13)
19ccb76a 1586 li r0,1
2f584a14 1587 sld r0,r0,r4
19ccb76a
PM
1588 andc. r3,r3,r0 /* no sense IPI'ing ourselves */
1589 beq 43f
f019b7ad
PM
1590 /* Order entry/exit update vs. IPIs */
1591 sync
19ccb76a
PM
1592 mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
1593 subf r6,r4,r13
159442: andi. r0,r3,1
1595 beq 44f
1596 ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
1597 li r0,IPI_PRIORITY
54695c30 1598 li r7,XICS_MFRR
19ccb76a
PM
1599 stbcix r0,r7,r8 /* trigger the IPI */
160044: srdi. r3,r3,1
1601 addi r6,r6,PACA_SIZE
1602 bne 42b
371fefd6 1603
e0b7ec05 1604secondary_too_late:
371fefd6 1605 /* Secondary threads wait for primary to do partition switch */
e0b7ec05
PM
160643: ld r5,HSTATE_KVM_VCORE(r13)
1607 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1608 lbz r3,HSTATE_PTID(r13)
371fefd6
PM
1609 cmpwi r3,0
1610 beq 15f
1611 HMT_LOW
161213: lbz r3,VCORE_IN_GUEST(r5)
1613 cmpwi r3,0
1614 bne 13b
1615 HMT_MEDIUM
1616 b 16f
1617
1618 /* Primary thread waits for all the secondaries to exit guest */
161915: lwz r3,VCORE_ENTRY_EXIT(r5)
1620 srwi r0,r3,8
1621 clrldi r3,r3,56
1622 cmpw r3,r0
1623 bne 15b
1624 isync
1625
1626 /* Primary thread switches back to host partition */
de56a948
PM
1627 ld r6,KVM_HOST_SDR1(r4)
1628 lwz r7,KVM_HOST_LPID(r4)
1629 li r8,LPID_RSVD /* switch to reserved LPID */
1630 mtspr SPRN_LPID,r8
1631 ptesync
1632 mtspr SPRN_SDR1,r6 /* switch to partition page table */
1633 mtspr SPRN_LPID,r7
1634 isync
93b0f4dc 1635
b005255e
MN
1636BEGIN_FTR_SECTION
1637 /* DPDES is shared between threads */
1638 mfspr r7, SPRN_DPDES
1639 std r7, VCORE_DPDES(r5)
1640 /* clear DPDES so we don't get guest doorbells in the host */
1641 li r8, 0
1642 mtspr SPRN_DPDES, r8
1643END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
1644
93b0f4dc
PM
1645 /* Subtract timebase offset from timebase */
1646 ld r8,VCORE_TB_OFFSET(r5)
1647 cmpdi r8,0
1648 beq 17f
c5fb80d3 1649 mftb r6 /* current guest timebase */
93b0f4dc
PM
1650 subf r8,r8,r6
1651 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1652 mftb r7 /* check if lower 24 bits overflowed */
1653 clrldi r6,r6,40
1654 clrldi r7,r7,40
1655 cmpld r7,r6
1656 bge 17f
1657 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1658 mtspr SPRN_TBU40,r8
1659
388cc6e1
PM
1660 /* Reset PCR */
166117: ld r0, VCORE_PCR(r5)
1662 cmpdi r0, 0
1663 beq 18f
1664 li r0, 0
1665 mtspr SPRN_PCR, r0
166618:
93b0f4dc 1667 /* Signal secondary CPUs to continue */
371fefd6 1668 stb r0,VCORE_IN_GUEST(r5)
de56a948
PM
1669 lis r8,0x7fff /* MAX_INT@h */
1670 mtspr SPRN_HDEC,r8
1671
371fefd6 167216: ld r8,KVM_HOST_LPCR(r4)
de56a948
PM
1673 mtspr SPRN_LPCR,r8
1674 isync
9e368f29
PM
1675 b 33f
1676
1677 /*
1678 * PPC970 guest -> host partition switch code.
1679 * We have to lock against concurrent tlbies, and
1680 * we have to flush the whole TLB.
1681 */
e0b7ec05
PM
168232: ld r5,HSTATE_KVM_VCORE(r13)
1683 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
9e368f29
PM
1684
1685 /* Take the guest's tlbie_lock */
54bb7f4b 1686#ifdef __BIG_ENDIAN__
9e368f29 1687 lwz r8,PACA_LOCK_TOKEN(r13)
54bb7f4b
AB
1688#else
1689 lwz r8,PACAPACAINDEX(r13)
1690#endif
9e368f29
PM
1691 addi r3,r4,KVM_TLBIE_LOCK
169224: lwarx r0,0,r3
1693 cmpwi r0,0
1694 bne 24b
1695 stwcx. r8,0,r3
1696 bne 24b
1697 isync
1698
1699 ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
1700 li r0,0x18f
1701 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
1702 or r0,r7,r0
1703 ptesync
1704 sync
1705 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
1706 isync
1707 li r0,0
1708 stw r0,0(r3) /* drop guest tlbie_lock */
1709
1710 /* invalidate the whole TLB */
1711 li r0,256
1712 mtctr r0
1713 li r6,0
171425: tlbiel r6
1715 addi r6,r6,0x1000
1716 bdnz 25b
1717 ptesync
1718
1719 /* take native_tlbie_lock */
1720 ld r3,toc_tlbie_lock@toc(2)
172124: lwarx r0,0,r3
1722 cmpwi r0,0
1723 bne 24b
1724 stwcx. r8,0,r3
1725 bne 24b
1726 isync
1727
1728 ld r6,KVM_HOST_SDR1(r4)
1729 mtspr SPRN_SDR1,r6 /* switch to host page table */
1730
1731 /* Set up host HID4 value */
1732 sync
1733 mtspr SPRN_HID4,r7
1734 isync
1735 li r0,0
1736 stw r0,0(r3) /* drop native_tlbie_lock */
1737
1738 lis r8,0x7fff /* MAX_INT@h */
1739 mtspr SPRN_HDEC,r8
1740
1741 /* Disable HDEC interrupts */
1742 mfspr r0,SPRN_HID0
1743 li r3,0
1744 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
1745 sync
1746 mtspr SPRN_HID0,r0
1747 mfspr r0,SPRN_HID0
1748 mfspr r0,SPRN_HID0
1749 mfspr r0,SPRN_HID0
1750 mfspr r0,SPRN_HID0
1751 mfspr r0,SPRN_HID0
1752 mfspr r0,SPRN_HID0
de56a948
PM
1753
1754 /* load host SLB entries */
9e368f29 175533: ld r8,PACA_SLBSHADOWPTR(r13)
de56a948
PM
1756
1757 .rept SLB_NUM_BOLTED
0865a583
AG
1758 li r3, SLBSHADOW_SAVEAREA
1759 LDX_BE r5, r8, r3
1760 addi r3, r3, 8
1761 LDX_BE r6, r8, r3
de56a948
PM
1762 andis. r7,r5,SLB_ESID_V@h
1763 beq 1f
1764 slbmte r6,r5
17651: addi r8,r8,16
1766 .endr
1767
44a3add8
PM
1768 /* Unset guest mode */
1769 li r0, KVM_GUEST_MODE_NONE
1770 stb r0, HSTATE_IN_GUEST(r13)
1771
218309b7
PM
1772 ld r0, 112+PPC_LR_STKOFF(r1)
1773 addi r1, r1, 112
1774 mtlr r0
1775 blr
b4072df4 1776
697d3899
PM
1777/*
1778 * Check whether an HDSI is an HPTE not found fault or something else.
1779 * If it is an HPTE not found fault that is due to the guest accessing
1780 * a page that they have mapped but which we have paged out, then
1781 * we continue on with the guest exit path. In all other cases,
1782 * reflect the HDSI to the guest as a DSI.
1783 */
1784kvmppc_hdsi:
1785 mfspr r4, SPRN_HDAR
1786 mfspr r6, SPRN_HDSISR
4cf302bc
PM
1787 /* HPTE not found fault or protection fault? */
1788 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
697d3899
PM
1789 beq 1f /* if not, send it to the guest */
1790 andi. r0, r11, MSR_DR /* data relocation enabled? */
1791 beq 3f
1792 clrrdi r0, r4, 28
c75df6f9 1793 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
697d3899
PM
1794 bne 1f /* if no SLB entry found */
17954: std r4, VCPU_FAULT_DAR(r9)
1796 stw r6, VCPU_FAULT_DSISR(r9)
1797
1798 /* Search the hash table. */
1799 mr r3, r9 /* vcpu pointer */
342d3db7 1800 li r7, 1 /* data fault */
b1576fec 1801 bl kvmppc_hpte_hv_fault
697d3899
PM
1802 ld r9, HSTATE_KVM_VCPU(r13)
1803 ld r10, VCPU_PC(r9)
1804 ld r11, VCPU_MSR(r9)
1805 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1806 cmpdi r3, 0 /* retry the instruction */
1807 beq 6f
1808 cmpdi r3, -1 /* handle in kernel mode */
b4072df4 1809 beq guest_exit_cont
697d3899
PM
1810 cmpdi r3, -2 /* MMIO emulation; need instr word */
1811 beq 2f
1812
1813 /* Synthesize a DSI for the guest */
1814 ld r4, VCPU_FAULT_DAR(r9)
1815 mr r6, r3
18161: mtspr SPRN_DAR, r4
1817 mtspr SPRN_DSISR, r6
1818 mtspr SPRN_SRR0, r10
1819 mtspr SPRN_SRR1, r11
1820 li r10, BOOK3S_INTERRUPT_DATA_STORAGE
e4e38121 1821 bl kvmppc_msr_interrupt
b4072df4 1822fast_interrupt_c_return:
697d3899
PM
18236: ld r7, VCPU_CTR(r9)
1824 lwz r8, VCPU_XER(r9)
1825 mtctr r7
1826 mtxer r8
1827 mr r4, r9
1828 b fast_guest_return
1829
18303: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1831 ld r5, KVM_VRMA_SLB_V(r5)
1832 b 4b
1833
1834 /* If this is for emulated MMIO, load the instruction word */
18352: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1836
1837 /* Set guest mode to 'jump over instruction' so if lwz faults
1838 * we'll just continue at the next IP. */
1839 li r0, KVM_GUEST_MODE_SKIP
1840 stb r0, HSTATE_IN_GUEST(r13)
1841
1842 /* Do the access with MSR:DR enabled */
1843 mfmsr r3
1844 ori r4, r3, MSR_DR /* Enable paging for data */
1845 mtmsrd r4
1846 lwz r8, 0(r10)
1847 mtmsrd r3
1848
1849 /* Store the result */
1850 stw r8, VCPU_LAST_INST(r9)
1851
1852 /* Unset guest mode. */
44a3add8 1853 li r0, KVM_GUEST_MODE_HOST_HV
697d3899 1854 stb r0, HSTATE_IN_GUEST(r13)
b4072df4 1855 b guest_exit_cont
de56a948 1856
342d3db7
PM
1857/*
1858 * Similarly for an HISI, reflect it to the guest as an ISI unless
1859 * it is an HPTE not found fault for a page that we have paged out.
1860 */
1861kvmppc_hisi:
1862 andis. r0, r11, SRR1_ISI_NOPT@h
1863 beq 1f
1864 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1865 beq 3f
1866 clrrdi r0, r10, 28
c75df6f9 1867 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
342d3db7
PM
1868 bne 1f /* if no SLB entry found */
18694:
1870 /* Search the hash table. */
1871 mr r3, r9 /* vcpu pointer */
1872 mr r4, r10
1873 mr r6, r11
1874 li r7, 0 /* instruction fault */
b1576fec 1875 bl kvmppc_hpte_hv_fault
342d3db7
PM
1876 ld r9, HSTATE_KVM_VCPU(r13)
1877 ld r10, VCPU_PC(r9)
1878 ld r11, VCPU_MSR(r9)
1879 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1880 cmpdi r3, 0 /* retry the instruction */
b4072df4 1881 beq fast_interrupt_c_return
342d3db7 1882 cmpdi r3, -1 /* handle in kernel mode */
b4072df4 1883 beq guest_exit_cont
342d3db7
PM
1884
1885 /* Synthesize an ISI for the guest */
1886 mr r11, r3
18871: mtspr SPRN_SRR0, r10
1888 mtspr SPRN_SRR1, r11
1889 li r10, BOOK3S_INTERRUPT_INST_STORAGE
e4e38121 1890 bl kvmppc_msr_interrupt
b4072df4 1891 b fast_interrupt_c_return
342d3db7
PM
1892
18933: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1894 ld r5, KVM_VRMA_SLB_V(r6)
1895 b 4b
1896
a8606e20
PM
1897/*
1898 * Try to handle an hcall in real mode.
1899 * Returns to the guest if we handle it, or continues on up to
1900 * the kernel if we can't (i.e. if we don't have a handler for
1901 * it, or if the handler returns H_TOO_HARD).
1902 */
1903 .globl hcall_try_real_mode
1904hcall_try_real_mode:
c75df6f9 1905 ld r3,VCPU_GPR(R3)(r9)
a8606e20 1906 andi. r0,r11,MSR_PR
27025a60
LPF
1907 /* sc 1 from userspace - reflect to guest syscall */
1908 bne sc_1_fast_return
a8606e20
PM
1909 clrrdi r3,r3,2
1910 cmpldi r3,hcall_real_table_end - hcall_real_table
b4072df4 1911 bge guest_exit_cont
699a0ea0
PM
1912 /* See if this hcall is enabled for in-kernel handling */
1913 ld r4, VCPU_KVM(r9)
1914 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */
1915 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */
1916 add r4, r4, r0
1917 ld r0, KVM_ENABLED_HCALLS(r4)
1918 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */
1919 srd r0, r0, r4
1920 andi. r0, r0, 1
1921 beq guest_exit_cont
1922 /* Get pointer to handler, if any, and call it */
a8606e20 1923 LOAD_REG_ADDR(r4, hcall_real_table)
4baa1d87 1924 lwax r3,r3,r4
a8606e20 1925 cmpwi r3,0
b4072df4 1926 beq guest_exit_cont
05a308c7
AB
1927 add r12,r3,r4
1928 mtctr r12
a8606e20 1929 mr r3,r9 /* get vcpu pointer */
c75df6f9 1930 ld r4,VCPU_GPR(R4)(r9)
a8606e20
PM
1931 bctrl
1932 cmpdi r3,H_TOO_HARD
1933 beq hcall_real_fallback
1934 ld r4,HSTATE_KVM_VCPU(r13)
c75df6f9 1935 std r3,VCPU_GPR(R3)(r4)
a8606e20
PM
1936 ld r10,VCPU_PC(r4)
1937 ld r11,VCPU_MSR(r4)
1938 b fast_guest_return
1939
27025a60
LPF
1940sc_1_fast_return:
1941 mtspr SPRN_SRR0,r10
1942 mtspr SPRN_SRR1,r11
1943 li r10, BOOK3S_INTERRUPT_SYSCALL
e4e38121 1944 bl kvmppc_msr_interrupt
27025a60
LPF
1945 mr r4,r9
1946 b fast_guest_return
1947
a8606e20
PM
1948 /* We've attempted a real mode hcall, but it's punted it back
1949 * to userspace. We need to restore some clobbered volatiles
1950 * before resuming the pass-it-to-qemu path */
1951hcall_real_fallback:
1952 li r12,BOOK3S_INTERRUPT_SYSCALL
1953 ld r9, HSTATE_KVM_VCPU(r13)
a8606e20 1954
b4072df4 1955 b guest_exit_cont
a8606e20
PM
1956
1957 .globl hcall_real_table
1958hcall_real_table:
1959 .long 0 /* 0 - unused */
c1fb0194
AB
1960 .long DOTSYM(kvmppc_h_remove) - hcall_real_table
1961 .long DOTSYM(kvmppc_h_enter) - hcall_real_table
1962 .long DOTSYM(kvmppc_h_read) - hcall_real_table
a8606e20
PM
1963 .long 0 /* 0x10 - H_CLEAR_MOD */
1964 .long 0 /* 0x14 - H_CLEAR_REF */
c1fb0194
AB
1965 .long DOTSYM(kvmppc_h_protect) - hcall_real_table
1966 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table
1967 .long DOTSYM(kvmppc_h_put_tce) - hcall_real_table
a8606e20 1968 .long 0 /* 0x24 - H_SET_SPRG0 */
c1fb0194 1969 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table
a8606e20
PM
1970 .long 0 /* 0x2c */
1971 .long 0 /* 0x30 */
1972 .long 0 /* 0x34 */
1973 .long 0 /* 0x38 */
1974 .long 0 /* 0x3c */
1975 .long 0 /* 0x40 */
1976 .long 0 /* 0x44 */
1977 .long 0 /* 0x48 */
1978 .long 0 /* 0x4c */
1979 .long 0 /* 0x50 */
1980 .long 0 /* 0x54 */
1981 .long 0 /* 0x58 */
1982 .long 0 /* 0x5c */
1983 .long 0 /* 0x60 */
e7d26f28 1984#ifdef CONFIG_KVM_XICS
c1fb0194
AB
1985 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table
1986 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table
1987 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table
e7d26f28 1988 .long 0 /* 0x70 - H_IPOLL */
c1fb0194 1989 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table
e7d26f28
BH
1990#else
1991 .long 0 /* 0x64 - H_EOI */
1992 .long 0 /* 0x68 - H_CPPR */
1993 .long 0 /* 0x6c - H_IPI */
1994 .long 0 /* 0x70 - H_IPOLL */
1995 .long 0 /* 0x74 - H_XIRR */
1996#endif
a8606e20
PM
1997 .long 0 /* 0x78 */
1998 .long 0 /* 0x7c */
1999 .long 0 /* 0x80 */
2000 .long 0 /* 0x84 */
2001 .long 0 /* 0x88 */
2002 .long 0 /* 0x8c */
2003 .long 0 /* 0x90 */
2004 .long 0 /* 0x94 */
2005 .long 0 /* 0x98 */
2006 .long 0 /* 0x9c */
2007 .long 0 /* 0xa0 */
2008 .long 0 /* 0xa4 */
2009 .long 0 /* 0xa8 */
2010 .long 0 /* 0xac */
2011 .long 0 /* 0xb0 */
2012 .long 0 /* 0xb4 */
2013 .long 0 /* 0xb8 */
2014 .long 0 /* 0xbc */
2015 .long 0 /* 0xc0 */
2016 .long 0 /* 0xc4 */
2017 .long 0 /* 0xc8 */
2018 .long 0 /* 0xcc */
2019 .long 0 /* 0xd0 */
2020 .long 0 /* 0xd4 */
2021 .long 0 /* 0xd8 */
2022 .long 0 /* 0xdc */
c1fb0194 2023 .long DOTSYM(kvmppc_h_cede) - hcall_real_table
a8606e20
PM
2024 .long 0 /* 0xe4 */
2025 .long 0 /* 0xe8 */
2026 .long 0 /* 0xec */
2027 .long 0 /* 0xf0 */
2028 .long 0 /* 0xf4 */
2029 .long 0 /* 0xf8 */
2030 .long 0 /* 0xfc */
2031 .long 0 /* 0x100 */
2032 .long 0 /* 0x104 */
2033 .long 0 /* 0x108 */
2034 .long 0 /* 0x10c */
2035 .long 0 /* 0x110 */
2036 .long 0 /* 0x114 */
2037 .long 0 /* 0x118 */
2038 .long 0 /* 0x11c */
2039 .long 0 /* 0x120 */
c1fb0194 2040 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table
8563bf52
PM
2041 .long 0 /* 0x128 */
2042 .long 0 /* 0x12c */
2043 .long 0 /* 0x130 */
c1fb0194 2044 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table
ae2113a4 2045 .globl hcall_real_table_end
a8606e20
PM
2046hcall_real_table_end:
2047
de56a948
PM
2048ignore_hdec:
2049 mr r4,r9
2050 b fast_guest_return
2051
8563bf52
PM
2052_GLOBAL(kvmppc_h_set_xdabr)
2053 andi. r0, r5, DABRX_USER | DABRX_KERNEL
2054 beq 6f
2055 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI
2056 andc. r0, r5, r0
2057 beq 3f
20586: li r3, H_PARAMETER
2059 blr
2060
a8606e20 2061_GLOBAL(kvmppc_h_set_dabr)
8563bf52
PM
2062 li r5, DABRX_USER | DABRX_KERNEL
20633:
eee7ff9d
MN
2064BEGIN_FTR_SECTION
2065 b 2f
2066END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
a8606e20 2067 std r4,VCPU_DABR(r3)
8563bf52
PM
2068 stw r5, VCPU_DABRX(r3)
2069 mtspr SPRN_DABRX, r5
8943633c
PM
2070 /* Work around P7 bug where DABR can get corrupted on mtspr */
20711: mtspr SPRN_DABR,r4
2072 mfspr r5, SPRN_DABR
2073 cmpd r4, r5
2074 bne 1b
2075 isync
a8606e20
PM
2076 li r3,0
2077 blr
2078
8563bf52
PM
2079 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */
20802: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW
2081 rlwimi r5, r4, 1, DAWRX_WT
2082 clrrdi r4, r4, 3
2083 std r4, VCPU_DAWR(r3)
2084 std r5, VCPU_DAWRX(r3)
2085 mtspr SPRN_DAWR, r4
2086 mtspr SPRN_DAWRX, r5
2087 li r3, 0
a8606e20
PM
2088 blr
2089
19ccb76a
PM
2090_GLOBAL(kvmppc_h_cede)
2091 ori r11,r11,MSR_EE
2092 std r11,VCPU_MSR(r3)
2093 li r0,1
2094 stb r0,VCPU_CEDED(r3)
2095 sync /* order setting ceded vs. testing prodded */
2096 lbz r5,VCPU_PRODDED(r3)
2097 cmpwi r5,0
04f995a5 2098 bne kvm_cede_prodded
19ccb76a
PM
2099 li r0,0 /* set trap to 0 to say hcall is handled */
2100 stw r0,VCPU_TRAP(r3)
2101 li r0,H_SUCCESS
c75df6f9 2102 std r0,VCPU_GPR(R3)(r3)
19ccb76a 2103BEGIN_FTR_SECTION
04f995a5 2104 b kvm_cede_exit /* just send it up to host on 970 */
19ccb76a
PM
2105END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
2106
2107 /*
2108 * Set our bit in the bitmask of napping threads unless all the
2109 * other threads are already napping, in which case we send this
2110 * up to the host.
2111 */
2112 ld r5,HSTATE_KVM_VCORE(r13)
e0b7ec05 2113 lbz r6,HSTATE_PTID(r13)
19ccb76a
PM
2114 lwz r8,VCORE_ENTRY_EXIT(r5)
2115 clrldi r8,r8,56
2116 li r0,1
2117 sld r0,r0,r6
2118 addi r6,r5,VCORE_NAPPING_THREADS
211931: lwarx r4,0,r6
2120 or r4,r4,r0
c75df6f9 2121 PPC_POPCNTW(R7,R4)
19ccb76a 2122 cmpw r7,r8
04f995a5 2123 bge kvm_cede_exit
19ccb76a
PM
2124 stwcx. r4,0,r6
2125 bne 31b
f019b7ad
PM
2126 /* order napping_threads update vs testing entry_exit_count */
2127 isync
e0b7ec05 2128 li r0,NAPPING_CEDE
19ccb76a 2129 stb r0,HSTATE_NAPPING(r13)
19ccb76a
PM
2130 lwz r7,VCORE_ENTRY_EXIT(r5)
2131 cmpwi r7,0x100
2132 bge 33f /* another thread already exiting */
2133
2134/*
2135 * Although not specifically required by the architecture, POWER7
2136 * preserves the following registers in nap mode, even if an SMT mode
2137 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
2138 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
2139 */
2140 /* Save non-volatile GPRs */
c75df6f9
MN
2141 std r14, VCPU_GPR(R14)(r3)
2142 std r15, VCPU_GPR(R15)(r3)
2143 std r16, VCPU_GPR(R16)(r3)
2144 std r17, VCPU_GPR(R17)(r3)
2145 std r18, VCPU_GPR(R18)(r3)
2146 std r19, VCPU_GPR(R19)(r3)
2147 std r20, VCPU_GPR(R20)(r3)
2148 std r21, VCPU_GPR(R21)(r3)
2149 std r22, VCPU_GPR(R22)(r3)
2150 std r23, VCPU_GPR(R23)(r3)
2151 std r24, VCPU_GPR(R24)(r3)
2152 std r25, VCPU_GPR(R25)(r3)
2153 std r26, VCPU_GPR(R26)(r3)
2154 std r27, VCPU_GPR(R27)(r3)
2155 std r28, VCPU_GPR(R28)(r3)
2156 std r29, VCPU_GPR(R29)(r3)
2157 std r30, VCPU_GPR(R30)(r3)
2158 std r31, VCPU_GPR(R31)(r3)
19ccb76a
PM
2159
2160 /* save FP state */
595e4f7e 2161 bl kvmppc_save_fp
19ccb76a
PM
2162
2163 /*
aa31e843 2164 * Take a nap until a decrementer or external or doobell interrupt
582b910e
PM
2165 * occurs, with PECE1, PECE0 and PECEDP set in LPCR. Also clear the
2166 * runlatch bit before napping.
19ccb76a 2167 */
582b910e
PM
2168 mfspr r2, SPRN_CTRLF
2169 clrrdi r2, r2, 1
2170 mtspr SPRN_CTRLT, r2
2171
f0888f70
PM
2172 li r0,1
2173 stb r0,HSTATE_HWTHREAD_REQ(r13)
19ccb76a
PM
2174 mfspr r5,SPRN_LPCR
2175 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
aa31e843
PM
2176BEGIN_FTR_SECTION
2177 oris r5,r5,LPCR_PECEDP@h
2178END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
19ccb76a
PM
2179 mtspr SPRN_LPCR,r5
2180 isync
2181 li r0, 0
2182 std r0, HSTATE_SCRATCH0(r13)
2183 ptesync
2184 ld r0, HSTATE_SCRATCH0(r13)
21851: cmpd r0, r0
2186 bne 1b
2187 nap
2188 b .
2189
e3bbbbfa
PM
219033: mr r4, r3
2191 li r3, 0
2192 li r12, 0
2193 b 34f
2194
19ccb76a 2195kvm_end_cede:
4619ac88
PM
2196 /* get vcpu pointer */
2197 ld r4, HSTATE_KVM_VCPU(r13)
2198
19ccb76a
PM
2199 /* Woken by external or decrementer interrupt */
2200 ld r1, HSTATE_HOST_R1(r13)
19ccb76a 2201
19ccb76a
PM
2202 /* load up FP state */
2203 bl kvmppc_load_fp
2204
2205 /* Load NV GPRS */
c75df6f9
MN
2206 ld r14, VCPU_GPR(R14)(r4)
2207 ld r15, VCPU_GPR(R15)(r4)
2208 ld r16, VCPU_GPR(R16)(r4)
2209 ld r17, VCPU_GPR(R17)(r4)
2210 ld r18, VCPU_GPR(R18)(r4)
2211 ld r19, VCPU_GPR(R19)(r4)
2212 ld r20, VCPU_GPR(R20)(r4)
2213 ld r21, VCPU_GPR(R21)(r4)
2214 ld r22, VCPU_GPR(R22)(r4)
2215 ld r23, VCPU_GPR(R23)(r4)
2216 ld r24, VCPU_GPR(R24)(r4)
2217 ld r25, VCPU_GPR(R25)(r4)
2218 ld r26, VCPU_GPR(R26)(r4)
2219 ld r27, VCPU_GPR(R27)(r4)
2220 ld r28, VCPU_GPR(R28)(r4)
2221 ld r29, VCPU_GPR(R29)(r4)
2222 ld r30, VCPU_GPR(R30)(r4)
2223 ld r31, VCPU_GPR(R31)(r4)
e3bbbbfa
PM
2224
2225 /* Check the wake reason in SRR1 to see why we got here */
2226 bl kvmppc_check_wake_reason
19ccb76a
PM
2227
2228 /* clear our bit in vcore->napping_threads */
e3bbbbfa
PM
222934: ld r5,HSTATE_KVM_VCORE(r13)
2230 lbz r7,HSTATE_PTID(r13)
19ccb76a 2231 li r0,1
e3bbbbfa 2232 sld r0,r0,r7
19ccb76a
PM
2233 addi r6,r5,VCORE_NAPPING_THREADS
223432: lwarx r7,0,r6
2235 andc r7,r7,r0
2236 stwcx. r7,0,r6
2237 bne 32b
2238 li r0,0
2239 stb r0,HSTATE_NAPPING(r13)
2240
e3bbbbfa
PM
2241 /* See if the wake reason means we need to exit */
2242 stw r12, VCPU_TRAP(r4)
4619ac88 2243 mr r9, r4
e3bbbbfa
PM
2244 cmpdi r3, 0
2245 bgt guest_exit_cont
4619ac88 2246
19ccb76a
PM
2247 /* see if any other thread is already exiting */
2248 lwz r0,VCORE_ENTRY_EXIT(r5)
2249 cmpwi r0,0x100
e3bbbbfa 2250 bge guest_exit_cont
19ccb76a 2251
e3bbbbfa 2252 b kvmppc_cede_reentry /* if not go back to guest */
19ccb76a
PM
2253
2254 /* cede when already previously prodded case */
04f995a5
PM
2255kvm_cede_prodded:
2256 li r0,0
19ccb76a
PM
2257 stb r0,VCPU_PRODDED(r3)
2258 sync /* order testing prodded vs. clearing ceded */
2259 stb r0,VCPU_CEDED(r3)
2260 li r3,H_SUCCESS
2261 blr
2262
2263 /* we've ceded but we want to give control to the host */
04f995a5 2264kvm_cede_exit:
4619ac88 2265 b hcall_real_fallback
19ccb76a 2266
b4072df4
PM
2267 /* Try to handle a machine check in real mode */
2268machine_check_realmode:
2269 mr r3, r9 /* get vcpu pointer */
b1576fec 2270 bl kvmppc_realmode_machine_check
b4072df4 2271 nop
74845bc2 2272 cmpdi r3, 0 /* Did we handle MCE ? */
b4072df4
PM
2273 ld r9, HSTATE_KVM_VCPU(r13)
2274 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
74845bc2
MS
2275 /*
2276 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through
2277 * machine check interrupt (set HSRR0 to 0x200). And for handled
2278 * errors (no-fatal), just go back to guest execution with current
2279 * HSRR0 instead of exiting guest. This new approach will inject
2280 * machine check to guest for fatal error causing guest to crash.
2281 *
2282 * The old code used to return to host for unhandled errors which
2283 * was causing guest to hang with soft lockups inside guest and
2284 * makes it difficult to recover guest instance.
2285 */
2286 ld r10, VCPU_PC(r9)
2287 ld r11, VCPU_MSR(r9)
2288 bne 2f /* Continue guest execution. */
b4072df4
PM
2289 /* If not, deliver a machine check. SRR0/1 are already set */
2290 li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
000a25dd 2291 ld r11, VCPU_MSR(r9)
e4e38121 2292 bl kvmppc_msr_interrupt
74845bc2 22932: b fast_interrupt_c_return
b4072df4 2294
e3bbbbfa
PM
2295/*
2296 * Check the reason we woke from nap, and take appropriate action.
2297 * Returns:
2298 * 0 if nothing needs to be done
2299 * 1 if something happened that needs to be handled by the host
2300 * -1 if there was a guest wakeup (IPI)
2301 *
2302 * Also sets r12 to the interrupt vector for any interrupt that needs
2303 * to be handled now by the host (0x500 for external interrupt), or zero.
2304 */
2305kvmppc_check_wake_reason:
2306 mfspr r6, SPRN_SRR1
aa31e843
PM
2307BEGIN_FTR_SECTION
2308 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */
2309FTR_SECTION_ELSE
2310 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */
2311ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S)
2312 cmpwi r6, 8 /* was it an external interrupt? */
e3bbbbfa
PM
2313 li r12, BOOK3S_INTERRUPT_EXTERNAL
2314 beq kvmppc_read_intr /* if so, see what it was */
2315 li r3, 0
2316 li r12, 0
2317 cmpwi r6, 6 /* was it the decrementer? */
2318 beq 0f
aa31e843
PM
2319BEGIN_FTR_SECTION
2320 cmpwi r6, 5 /* privileged doorbell? */
2321 beq 0f
5d00f66b
PM
2322 cmpwi r6, 3 /* hypervisor doorbell? */
2323 beq 3f
aa31e843 2324END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
e3bbbbfa
PM
2325 li r3, 1 /* anything else, return 1 */
23260: blr
2327
5d00f66b
PM
2328 /* hypervisor doorbell */
23293: li r12, BOOK3S_INTERRUPT_H_DOORBELL
2330 li r3, 1
2331 blr
2332
c934243c
PM
2333/*
2334 * Determine what sort of external interrupt is pending (if any).
2335 * Returns:
2336 * 0 if no interrupt is pending
2337 * 1 if an interrupt is pending that needs to be handled by the host
2338 * -1 if there was a guest wakeup IPI (which has now been cleared)
2339 */
2340kvmppc_read_intr:
2341 /* see if a host IPI is pending */
2342 li r3, 1
2343 lbz r0, HSTATE_HOST_IPI(r13)
2344 cmpwi r0, 0
2345 bne 1f
371fefd6 2346
c934243c
PM
2347 /* Now read the interrupt from the ICP */
2348 ld r6, HSTATE_XICS_PHYS(r13)
19ccb76a 2349 li r7, XICS_XIRR
c934243c
PM
2350 cmpdi r6, 0
2351 beq- 1f
2352 lwzcix r0, r6, r7
2353 rlwinm. r3, r0, 0, 0xffffff
19ccb76a 2354 sync
c934243c 2355 beq 1f /* if nothing pending in the ICP */
371fefd6 2356
c934243c
PM
2357 /* We found something in the ICP...
2358 *
2359 * If it's not an IPI, stash it in the PACA and return to
2360 * the host, we don't (yet) handle directing real external
2361 * interrupts directly to the guest
2362 */
2363 cmpwi r3, XICS_IPI /* if there is, is it an IPI? */
c934243c 2364 bne 42f
371fefd6 2365
c934243c
PM
2366 /* It's an IPI, clear the MFRR and EOI it */
2367 li r3, 0xff
2368 li r8, XICS_MFRR
2369 stbcix r3, r6, r8 /* clear the IPI */
2370 stwcix r0, r6, r7 /* EOI it */
2371 sync
f0888f70 2372
c934243c
PM
2373 /* We need to re-check host IPI now in case it got set in the
2374 * meantime. If it's clear, we bounce the interrupt to the
2375 * guest
2376 */
2377 lbz r0, HSTATE_HOST_IPI(r13)
2378 cmpwi r0, 0
2379 bne- 43f
2380
2381 /* OK, it's an IPI for us */
2382 li r3, -1
23831: blr
2384
238542: /* It's not an IPI and it's for the host, stash it in the PACA
2386 * before exit, it will be picked up by the host ICP driver
2387 */
2388 stw r0, HSTATE_SAVED_XIRR(r13)
e3bbbbfa 2389 li r3, 1
c934243c
PM
2390 b 1b
2391
239243: /* We raced with the host, we need to resend that IPI, bummer */
2393 li r0, IPI_PRIORITY
2394 stbcix r0, r6, r8 /* set the IPI */
2395 sync
e3bbbbfa 2396 li r3, 1
c934243c 2397 b 1b
371fefd6 2398
de56a948
PM
2399/*
2400 * Save away FP, VMX and VSX registers.
2401 * r3 = vcpu pointer
595e4f7e
PM
2402 * N.B. r30 and r31 are volatile across this function,
2403 * thus it is not callable from C.
a8606e20 2404 */
595e4f7e
PM
2405kvmppc_save_fp:
2406 mflr r30
2407 mr r31,r3
8943633c
PM
2408 mfmsr r5
2409 ori r8,r5,MSR_FP
de56a948
PM
2410#ifdef CONFIG_ALTIVEC
2411BEGIN_FTR_SECTION
2412 oris r8,r8,MSR_VEC@h
2413END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2414#endif
2415#ifdef CONFIG_VSX
2416BEGIN_FTR_SECTION
2417 oris r8,r8,MSR_VSX@h
2418END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2419#endif
2420 mtmsrd r8
2421 isync
595e4f7e
PM
2422 addi r3,r3,VCPU_FPRS
2423 bl .store_fp_state
de56a948
PM
2424#ifdef CONFIG_ALTIVEC
2425BEGIN_FTR_SECTION
595e4f7e
PM
2426 addi r3,r31,VCPU_VRS
2427 bl .store_vr_state
de56a948
PM
2428END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2429#endif
2430 mfspr r6,SPRN_VRSAVE
e724f080 2431 stw r6,VCPU_VRSAVE(r31)
595e4f7e 2432 mtlr r30
de56a948
PM
2433 blr
2434
2435/*
2436 * Load up FP, VMX and VSX registers
2437 * r4 = vcpu pointer
595e4f7e
PM
2438 * N.B. r30 and r31 are volatile across this function,
2439 * thus it is not callable from C.
de56a948 2440 */
de56a948 2441kvmppc_load_fp:
595e4f7e
PM
2442 mflr r30
2443 mr r31,r4
de56a948
PM
2444 mfmsr r9
2445 ori r8,r9,MSR_FP
2446#ifdef CONFIG_ALTIVEC
2447BEGIN_FTR_SECTION
2448 oris r8,r8,MSR_VEC@h
2449END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2450#endif
2451#ifdef CONFIG_VSX
2452BEGIN_FTR_SECTION
2453 oris r8,r8,MSR_VSX@h
2454END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2455#endif
2456 mtmsrd r8
2457 isync
595e4f7e
PM
2458 addi r3,r4,VCPU_FPRS
2459 bl .load_fp_state
de56a948
PM
2460#ifdef CONFIG_ALTIVEC
2461BEGIN_FTR_SECTION
595e4f7e
PM
2462 addi r3,r31,VCPU_VRS
2463 bl .load_vr_state
de56a948
PM
2464END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2465#endif
e724f080 2466 lwz r7,VCPU_VRSAVE(r31)
de56a948 2467 mtspr SPRN_VRSAVE,r7
595e4f7e
PM
2468 mtlr r30
2469 mr r4,r31
de56a948 2470 blr
44a3add8
PM
2471
2472/*
2473 * We come here if we get any exception or interrupt while we are
2474 * executing host real mode code while in guest MMU context.
2475 * For now just spin, but we should do something better.
2476 */
2477kvmppc_bad_host_intr:
2478 b .
e4e38121
MN
2479
2480/*
2481 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken
2482 * from VCPU_INTR_MSR and is modified based on the required TM state changes.
2483 * r11 has the guest MSR value (in/out)
2484 * r9 has a vcpu pointer (in)
2485 * r0 is used as a scratch register
2486 */
2487kvmppc_msr_interrupt:
2488 rldicl r0, r11, 64 - MSR_TS_S_LG, 62
2489 cmpwi r0, 2 /* Check if we are in transactional state.. */
2490 ld r11, VCPU_INTR_MSR(r9)
2491 bne 1f
2492 /* ... if transactional, change to suspended */
2493 li r0, 1
24941: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG
2495 blr
9bc01a9b
PM
2496
2497/*
2498 * This works around a hardware bug on POWER8E processors, where
2499 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a
2500 * performance monitor interrupt. Instead, when we need to have
2501 * an interrupt pending, we have to arrange for a counter to overflow.
2502 */
2503kvmppc_fix_pmao:
2504 li r3, 0
2505 mtspr SPRN_MMCR2, r3
2506 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h
2507 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN
2508 mtspr SPRN_MMCR0, r3
2509 lis r3, 0x7fff
2510 ori r3, r3, 0xffff
2511 mtspr SPRN_PMC6, r3
2512 isync
2513 blr
This page took 0.241096 seconds and 5 git commands to generate.