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