KVM: PPC: Book3S HV: Align physical and virtual CPU thread numbers
[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>
de56a948 31
7ffcf8ec
AB
32#ifdef __LITTLE_ENDIAN__
33#error Need to fix lppaca and SLB shadow accesses in little endian mode
34#endif
35
e0b7ec05
PM
36/* Values in HSTATE_NAPPING(r13) */
37#define NAPPING_CEDE 1
38#define NAPPING_NOVCPU 2
39
de56a948 40/*
19ccb76a 41 * Call kvmppc_hv_entry in real mode.
de56a948
PM
42 * Must be called with interrupts hard-disabled.
43 *
44 * Input Registers:
45 *
46 * LR = return address to continue at after eventually re-enabling MMU
47 */
48_GLOBAL(kvmppc_hv_entry_trampoline)
218309b7
PM
49 mflr r0
50 std r0, PPC_LR_STKOFF(r1)
51 stdu r1, -112(r1)
de56a948 52 mfmsr r10
218309b7 53 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry)
de56a948
PM
54 li r0,MSR_RI
55 andc r0,r10,r0
56 li r6,MSR_IR | MSR_DR
57 andc r6,r10,r6
58 mtmsrd r0,1 /* clear RI in MSR */
59 mtsrr0 r5
60 mtsrr1 r6
61 RFI
62
218309b7 63kvmppc_call_hv_entry:
e0b7ec05 64 ld r4, HSTATE_KVM_VCPU(r13)
218309b7
PM
65 bl kvmppc_hv_entry
66
67 /* Back from guest - restore host state and return to caller */
68
eee7ff9d 69BEGIN_FTR_SECTION
218309b7
PM
70 /* Restore host DABR and DABRX */
71 ld r5,HSTATE_DABR(r13)
72 li r6,7
73 mtspr SPRN_DABR,r5
74 mtspr SPRN_DABRX,r6
eee7ff9d 75END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
218309b7
PM
76
77 /* Restore SPRG3 */
78 ld r3,PACA_SPRG3(r13)
79 mtspr SPRN_SPRG3,r3
80
218309b7
PM
81 /* Reload the host's PMU registers */
82 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */
83 lbz r4, LPPACA_PMCINUSE(r3)
84 cmpwi r4, 0
85 beq 23f /* skip if not */
86 lwz r3, HSTATE_PMC(r13)
87 lwz r4, HSTATE_PMC + 4(r13)
88 lwz r5, HSTATE_PMC + 8(r13)
89 lwz r6, HSTATE_PMC + 12(r13)
90 lwz r8, HSTATE_PMC + 16(r13)
91 lwz r9, HSTATE_PMC + 20(r13)
92BEGIN_FTR_SECTION
93 lwz r10, HSTATE_PMC + 24(r13)
94 lwz r11, HSTATE_PMC + 28(r13)
95END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
96 mtspr SPRN_PMC1, r3
97 mtspr SPRN_PMC2, r4
98 mtspr SPRN_PMC3, r5
99 mtspr SPRN_PMC4, r6
100 mtspr SPRN_PMC5, r8
101 mtspr SPRN_PMC6, r9
102BEGIN_FTR_SECTION
103 mtspr SPRN_PMC7, r10
104 mtspr SPRN_PMC8, r11
105END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
106 ld r3, HSTATE_MMCR(r13)
107 ld r4, HSTATE_MMCR + 8(r13)
108 ld r5, HSTATE_MMCR + 16(r13)
109 mtspr SPRN_MMCR1, r4
110 mtspr SPRN_MMCRA, r5
111 mtspr SPRN_MMCR0, r3
112 isync
11323:
114
e0b7ec05
PM
115 /*
116 * Reload DEC. HDEC interrupts were disabled when
117 * we reloaded the host's LPCR value.
118 */
119 ld r3, HSTATE_DECEXP(r13)
120 mftb r4
121 subf r4, r4, r3
122 mtspr SPRN_DEC, r4
123
218309b7
PM
124 /*
125 * For external and machine check interrupts, we need
126 * to call the Linux handler to process the interrupt.
127 * We do that by jumping to absolute address 0x500 for
128 * external interrupts, or the machine_check_fwnmi label
129 * for machine checks (since firmware might have patched
130 * the vector area at 0x200). The [h]rfid at the end of the
131 * handler will return to the book3s_hv_interrupts.S code.
132 * For other interrupts we do the rfid to get back
133 * to the book3s_hv_interrupts.S code here.
134 */
135 ld r8, 112+PPC_LR_STKOFF(r1)
136 addi r1, r1, 112
137 ld r7, HSTATE_HOST_MSR(r13)
138
139 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK
140 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
141BEGIN_FTR_SECTION
142 beq 11f
143END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
144
145 /* RFI into the highmem handler, or branch to interrupt handler */
146 mfmsr r6
147 li r0, MSR_RI
148 andc r6, r6, r0
149 mtmsrd r6, 1 /* Clear RI in MSR */
150 mtsrr0 r8
151 mtsrr1 r7
152 beqa 0x500 /* external interrupt (PPC970) */
153 beq cr1, 13f /* machine check */
154 RFI
155
156 /* On POWER7, we have external interrupts set to use HSRR0/1 */
15711: mtspr SPRN_HSRR0, r8
158 mtspr SPRN_HSRR1, r7
159 ba 0x500
160
16113: b machine_check_fwnmi
162
de56a948 163
e0b7ec05
PM
164kvmppc_primary_no_guest:
165 /* We handle this much like a ceded vcpu */
166 /* set our bit in napping_threads */
167 ld r5, HSTATE_KVM_VCORE(r13)
168 lbz r7, HSTATE_PTID(r13)
169 li r0, 1
170 sld r0, r0, r7
171 addi r6, r5, VCORE_NAPPING_THREADS
1721: lwarx r3, 0, r6
173 or r3, r3, r0
174 stwcx. r3, 0, r6
175 bne 1b
176 /* order napping_threads update vs testing entry_exit_count */
177 isync
178 li r12, 0
179 lwz r7, VCORE_ENTRY_EXIT(r5)
180 cmpwi r7, 0x100
181 bge kvm_novcpu_exit /* another thread already exiting */
182 li r3, NAPPING_NOVCPU
183 stb r3, HSTATE_NAPPING(r13)
184 li r3, 1
185 stb r3, HSTATE_HWTHREAD_REQ(r13)
186
187 b kvm_do_nap
188
189kvm_novcpu_wakeup:
190 ld r1, HSTATE_HOST_R1(r13)
191 ld r5, HSTATE_KVM_VCORE(r13)
192 li r0, 0
193 stb r0, HSTATE_NAPPING(r13)
194 stb r0, HSTATE_HWTHREAD_REQ(r13)
195
196 /* see if any other thread is already exiting */
197 li r12, 0
198 lwz r0, VCORE_ENTRY_EXIT(r5)
199 cmpwi r0, 0x100
200 bge kvm_novcpu_exit
201
202 /* clear our bit in napping_threads */
203 lbz r7, HSTATE_PTID(r13)
204 li r0, 1
205 sld r0, r0, r7
206 addi r6, r5, VCORE_NAPPING_THREADS
2074: lwarx r3, 0, r6
208 andc r3, r3, r0
209 stwcx. r3, 0, r6
210 bne 4b
211
212 /* Check the wake reason in SRR1 to see why we got here */
213 mfspr r3, SPRN_SRR1
214 rlwinm r3, r3, 44-31, 0x7 /* extract wake reason field */
215 cmpwi r3, 4 /* was it an external interrupt? */
216 bne kvm_novcpu_exit /* if not, exit the guest */
217
218 /* extern interrupt - read and handle it */
219 li r12, BOOK3S_INTERRUPT_EXTERNAL
220 bl kvmppc_read_intr
221 cmpdi r3, 0
222 bge kvm_novcpu_exit
223 li r12, 0
224
225 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
226 ld r4, HSTATE_KVM_VCPU(r13)
227 cmpdi r4, 0
228 bne kvmppc_got_guest
229
230kvm_novcpu_exit:
231 b hdec_soon
232
371fefd6 233/*
e0b7ec05 234 * We come in here when wakened from nap mode.
371fefd6
PM
235 * Relocation is off and most register values are lost.
236 * r13 points to the PACA.
237 */
238 .globl kvm_start_guest
239kvm_start_guest:
19ccb76a
PM
240 ld r2,PACATOC(r13)
241
f0888f70
PM
242 li r0,KVM_HWTHREAD_IN_KVM
243 stb r0,HSTATE_HWTHREAD_STATE(r13)
371fefd6 244
f0888f70
PM
245 /* NV GPR values from power7_idle() will no longer be valid */
246 li r0,1
247 stb r0,PACA_NAPSTATELOST(r13)
371fefd6 248
4619ac88
PM
249 /* were we napping due to cede? */
250 lbz r0,HSTATE_NAPPING(r13)
e0b7ec05
PM
251 cmpwi r0,NAPPING_CEDE
252 beq kvm_end_cede
253 cmpwi r0,NAPPING_NOVCPU
254 beq kvm_novcpu_wakeup
255
256 ld r1,PACAEMERGSP(r13)
257 subi r1,r1,STACK_FRAME_OVERHEAD
4619ac88
PM
258
259 /*
260 * We weren't napping due to cede, so this must be a secondary
261 * thread being woken up to run a guest, or being woken up due
262 * to a stray IPI. (Or due to some machine check or hypervisor
263 * maintenance interrupt while the core is in KVM.)
264 */
f0888f70
PM
265
266 /* Check the wake reason in SRR1 to see why we got here */
267 mfspr r3,SPRN_SRR1
268 rlwinm r3,r3,44-31,0x7 /* extract wake reason field */
269 cmpwi r3,4 /* was it an external interrupt? */
4619ac88
PM
270 bne 27f /* if not */
271 ld r5,HSTATE_XICS_PHYS(r13)
272 li r7,XICS_XIRR /* if it was an external interrupt, */
f0888f70 273 lwzcix r8,r5,r7 /* get and ack the interrupt */
371fefd6 274 sync
f0888f70 275 clrldi. r9,r8,40 /* get interrupt source ID. */
4619ac88
PM
276 beq 28f /* none there? */
277 cmpwi r9,XICS_IPI /* was it an IPI? */
278 bne 29f
279 li r0,0xff
280 li r6,XICS_MFRR
f0888f70 281 stbcix r0,r5,r6 /* clear IPI */
4619ac88
PM
282 stwcix r8,r5,r7 /* EOI the interrupt */
283 sync /* order loading of vcpu after that */
371fefd6 284
4619ac88 285 /* get vcpu pointer, NULL if we have no vcpu to run */
7b444c67
PM
286 ld r4,HSTATE_KVM_VCPU(r13)
287 cmpdi r4,0
f0888f70 288 /* if we have no vcpu to run, go back to sleep */
7b444c67 289 beq kvm_no_guest
218309b7 290 b 30f
f0888f70 291
4619ac88
PM
29227: /* XXX should handle hypervisor maintenance interrupts etc. here */
293 b kvm_no_guest
29428: /* SRR1 said external but ICP said nope?? */
295 b kvm_no_guest
29629: /* External non-IPI interrupt to offline secondary thread? help?? */
297 stw r8,HSTATE_SAVED_XIRR(r13)
298 b kvm_no_guest
2fde6d20 299
e0b7ec05
PM
30030:
301 /* Set HSTATE_DSCR(r13) to something sensible */
302 LOAD_REG_ADDR(r6, dscr_default)
303 ld r6, 0(r6)
304 std r6, HSTATE_DSCR(r13)
305
306 bl kvmppc_hv_entry
218309b7
PM
307
308 /* Back from the guest, go back to nap */
309 /* Clear our vcpu pointer so we don't come back in early */
310 li r0, 0
311 std r0, HSTATE_KVM_VCPU(r13)
312 lwsync
313 /* Clear any pending IPI - we're an offline thread */
314 ld r5, HSTATE_XICS_PHYS(r13)
315 li r7, XICS_XIRR
316 lwzcix r3, r5, r7 /* ack any pending interrupt */
317 rlwinm. r0, r3, 0, 0xffffff /* any pending? */
318 beq 37f
319 sync
320 li r0, 0xff
321 li r6, XICS_MFRR
322 stbcix r0, r5, r6 /* clear the IPI */
323 stwcix r3, r5, r7 /* EOI it */
32437: sync
325
326 /* increment the nap count and then go to nap mode */
327 ld r4, HSTATE_KVM_VCORE(r13)
328 addi r4, r4, VCORE_NAP_COUNT
329 lwsync /* make previous updates visible */
33051: lwarx r3, 0, r4
331 addi r3, r3, 1
332 stwcx. r3, 0, r4
333 bne 51b
334
335kvm_no_guest:
336 li r0, KVM_HWTHREAD_IN_NAP
337 stb r0, HSTATE_HWTHREAD_STATE(r13)
e0b7ec05 338kvm_do_nap:
218309b7
PM
339 li r3, LPCR_PECE0
340 mfspr r4, SPRN_LPCR
341 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1
342 mtspr SPRN_LPCR, r4
343 isync
344 std r0, HSTATE_SCRATCH0(r13)
345 ptesync
346 ld r0, HSTATE_SCRATCH0(r13)
3471: cmpd r0, r0
348 bne 1b
349 nap
350 b .
351
352/******************************************************************************
353 * *
354 * Entry code *
355 * *
356 *****************************************************************************/
357
de56a948
PM
358.global kvmppc_hv_entry
359kvmppc_hv_entry:
360
361 /* Required state:
362 *
e0b7ec05 363 * R4 = vcpu pointer (or NULL)
de56a948
PM
364 * MSR = ~IR|DR
365 * R13 = PACA
366 * R1 = host R1
367 * all other volatile GPRS = free
368 */
369 mflr r0
218309b7
PM
370 std r0, PPC_LR_STKOFF(r1)
371 stdu r1, -112(r1)
de56a948 372
de56a948
PM
373 /* Save R1 in the PACA */
374 std r1, HSTATE_HOST_R1(r13)
375
44a3add8
PM
376 li r6, KVM_GUEST_MODE_HOST_HV
377 stb r6, HSTATE_IN_GUEST(r13)
378
de56a948
PM
379 /* Clear out SLB */
380 li r6,0
381 slbmte r6,r6
382 slbia
383 ptesync
384
9e368f29
PM
385BEGIN_FTR_SECTION
386 b 30f
387END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
388 /*
389 * POWER7 host -> guest partition switch code.
390 * We don't have to lock against concurrent tlbies,
391 * but we do have to coordinate across hardware threads.
392 */
371fefd6
PM
393 /* Increment entry count iff exit count is zero. */
394 ld r5,HSTATE_KVM_VCORE(r13)
395 addi r9,r5,VCORE_ENTRY_EXIT
39621: lwarx r3,0,r9
397 cmpwi r3,0x100 /* any threads starting to exit? */
398 bge secondary_too_late /* if so we're too late to the party */
399 addi r3,r3,1
400 stwcx. r3,0,r9
401 bne 21b
402
403 /* Primary thread switches to guest partition. */
e0b7ec05
PM
404 ld r9,VCORE_KVM(r5) /* pointer to struct kvm */
405 lbz r6,HSTATE_PTID(r13)
371fefd6
PM
406 cmpwi r6,0
407 bne 20f
de56a948
PM
408 ld r6,KVM_SDR1(r9)
409 lwz r7,KVM_LPID(r9)
410 li r0,LPID_RSVD /* switch to reserved LPID */
411 mtspr SPRN_LPID,r0
412 ptesync
413 mtspr SPRN_SDR1,r6 /* switch to partition page table */
414 mtspr SPRN_LPID,r7
415 isync
1b400ba0
PM
416
417 /* See if we need to flush the TLB */
418 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */
419 clrldi r7,r6,64-6 /* extract bit number (6 bits) */
420 srdi r6,r6,6 /* doubleword number */
421 sldi r6,r6,3 /* address offset */
422 add r6,r6,r9
423 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */
371fefd6 424 li r0,1
1b400ba0
PM
425 sld r0,r0,r7
426 ld r7,0(r6)
427 and. r7,r7,r0
428 beq 22f
42923: ldarx r7,0,r6 /* if set, clear the bit */
430 andc r7,r7,r0
431 stdcx. r7,0,r6
432 bne 23b
433 li r6,128 /* and flush the TLB */
434 mtctr r6
435 li r7,0x800 /* IS field = 0b10 */
436 ptesync
43728: tlbiel r7
438 addi r7,r7,0x1000
439 bdnz 28b
440 ptesync
441
93b0f4dc
PM
442 /* Add timebase offset onto timebase */
44322: ld r8,VCORE_TB_OFFSET(r5)
444 cmpdi r8,0
445 beq 37f
446 mftb r6 /* current host timebase */
447 add r8,r8,r6
448 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
449 mftb r7 /* check if lower 24 bits overflowed */
450 clrldi r6,r6,40
451 clrldi r7,r7,40
452 cmpld r7,r6
453 bge 37f
454 addis r8,r8,0x100 /* if so, increment upper 40 bits */
455 mtspr SPRN_TBU40,r8
456
388cc6e1
PM
457 /* Load guest PCR value to select appropriate compat mode */
45837: ld r7, VCORE_PCR(r5)
459 cmpdi r7, 0
460 beq 38f
461 mtspr SPRN_PCR, r7
46238:
463 li r0,1
371fefd6
PM
464 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */
465 b 10f
466
467 /* Secondary threads wait for primary to have done partition switch */
46820: lbz r0,VCORE_IN_GUEST(r5)
469 cmpwi r0,0
470 beq 20b
aa04b4cc 471
19ccb76a 472 /* Set LPCR and RMOR. */
a0144e2a 47310: ld r8,VCORE_LPCR(r5)
19ccb76a 474 mtspr SPRN_LPCR,r8
aa04b4cc
PM
475 ld r8,KVM_RMOR(r9)
476 mtspr SPRN_RMOR,r8
de56a948
PM
477 isync
478
479 /* Check if HDEC expires soon */
480 mfspr r3,SPRN_HDEC
e0b7ec05 481 cmpwi r3,512 /* 1 microsecond */
de56a948 482 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
de56a948 483 blt hdec_soon
9e368f29
PM
484 b 31f
485
486 /*
487 * PPC970 host -> guest partition switch code.
488 * We have to lock against concurrent tlbies,
489 * using native_tlbie_lock to lock against host tlbies
490 * and kvm->arch.tlbie_lock to lock against guest tlbies.
491 * We also have to invalidate the TLB since its
492 * entries aren't tagged with the LPID.
493 */
e0b7ec05
PM
49430: ld r5,HSTATE_KVM_VCORE(r13)
495 ld r9,VCORE_KVM(r5) /* pointer to struct kvm */
9e368f29
PM
496
497 /* first take native_tlbie_lock */
498 .section ".toc","aw"
499toc_tlbie_lock:
500 .tc native_tlbie_lock[TC],native_tlbie_lock
501 .previous
502 ld r3,toc_tlbie_lock@toc(2)
54bb7f4b 503#ifdef __BIG_ENDIAN__
9e368f29 504 lwz r8,PACA_LOCK_TOKEN(r13)
54bb7f4b
AB
505#else
506 lwz r8,PACAPACAINDEX(r13)
507#endif
9e368f29
PM
50824: lwarx r0,0,r3
509 cmpwi r0,0
510 bne 24b
511 stwcx. r8,0,r3
512 bne 24b
513 isync
514
a0144e2a
PM
515 ld r5,HSTATE_KVM_VCORE(r13)
516 ld r7,VCORE_LPCR(r5) /* use vcore->lpcr to store HID4 */
9e368f29
PM
517 li r0,0x18f
518 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
519 or r0,r7,r0
520 ptesync
521 sync
522 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
523 isync
524 li r0,0
525 stw r0,0(r3) /* drop native_tlbie_lock */
526
527 /* invalidate the whole TLB */
528 li r0,256
529 mtctr r0
530 li r6,0
53125: tlbiel r6
532 addi r6,r6,0x1000
533 bdnz 25b
534 ptesync
535
536 /* Take the guest's tlbie_lock */
537 addi r3,r9,KVM_TLBIE_LOCK
53824: lwarx r0,0,r3
539 cmpwi r0,0
540 bne 24b
541 stwcx. r8,0,r3
542 bne 24b
543 isync
544 ld r6,KVM_SDR1(r9)
545 mtspr SPRN_SDR1,r6 /* switch to partition page table */
546
547 /* Set up HID4 with the guest's LPID etc. */
548 sync
549 mtspr SPRN_HID4,r7
550 isync
551
552 /* drop the guest's tlbie_lock */
553 li r0,0
554 stw r0,0(r3)
555
556 /* Check if HDEC expires soon */
557 mfspr r3,SPRN_HDEC
558 cmpwi r3,10
559 li r12,BOOK3S_INTERRUPT_HV_DECREMENTER
9e368f29
PM
560 blt hdec_soon
561
562 /* Enable HDEC interrupts */
563 mfspr r0,SPRN_HID0
564 li r3,1
565 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
566 sync
567 mtspr SPRN_HID0,r0
568 mfspr r0,SPRN_HID0
569 mfspr r0,SPRN_HID0
570 mfspr r0,SPRN_HID0
571 mfspr r0,SPRN_HID0
572 mfspr r0,SPRN_HID0
573 mfspr r0,SPRN_HID0
e0b7ec05
PM
57431:
575 /* Do we have a guest vcpu to run? */
576 cmpdi r4, 0
577 beq kvmppc_primary_no_guest
578kvmppc_got_guest:
de56a948
PM
579
580 /* Load up guest SLB entries */
e0b7ec05 581 lwz r5,VCPU_SLB_MAX(r4)
de56a948
PM
582 cmpwi r5,0
583 beq 9f
584 mtctr r5
585 addi r6,r4,VCPU_SLB
5861: ld r8,VCPU_SLB_E(r6)
587 ld r9,VCPU_SLB_V(r6)
588 slbmte r9,r8
589 addi r6,r6,VCPU_SLB_SIZE
590 bdnz 1b
5919:
e0b7ec05
PM
592 /* Increment yield count if they have a VPA */
593 ld r3, VCPU_VPA(r4)
594 cmpdi r3, 0
595 beq 25f
596 lwz r5, LPPACA_YIELDCOUNT(r3)
597 addi r5, r5, 1
598 stw r5, LPPACA_YIELDCOUNT(r3)
599 li r6, 1
600 stb r6, VCPU_VPA_DIRTY(r4)
60125:
602
603BEGIN_FTR_SECTION
604 /* Save purr/spurr */
605 mfspr r5,SPRN_PURR
606 mfspr r6,SPRN_SPURR
607 std r5,HSTATE_PURR(r13)
608 std r6,HSTATE_SPURR(r13)
609 ld r7,VCPU_PURR(r4)
610 ld r8,VCPU_SPURR(r4)
611 mtspr SPRN_PURR,r7
612 mtspr SPRN_SPURR,r8
613END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
614
615BEGIN_FTR_SECTION
616 /* Set partition DABR */
617 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */
618 li r5,3
619 ld r6,VCPU_DABR(r4)
620 mtspr SPRN_DABRX,r5
621 mtspr SPRN_DABR,r6
622 BEGIN_FTR_SECTION_NESTED(89)
623 isync
624 END_FTR_SECTION_NESTED(CPU_FTR_ARCH_206, CPU_FTR_ARCH_206, 89)
625END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S)
626
627 /* Load guest PMU registers */
628 /* R4 is live here (vcpu pointer) */
629 li r3, 1
630 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
631 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
632 isync
633 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */
634 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */
635 lwz r6, VCPU_PMC + 8(r4)
636 lwz r7, VCPU_PMC + 12(r4)
637 lwz r8, VCPU_PMC + 16(r4)
638 lwz r9, VCPU_PMC + 20(r4)
639BEGIN_FTR_SECTION
640 lwz r10, VCPU_PMC + 24(r4)
641 lwz r11, VCPU_PMC + 28(r4)
642END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
643 mtspr SPRN_PMC1, r3
644 mtspr SPRN_PMC2, r5
645 mtspr SPRN_PMC3, r6
646 mtspr SPRN_PMC4, r7
647 mtspr SPRN_PMC5, r8
648 mtspr SPRN_PMC6, r9
649BEGIN_FTR_SECTION
650 mtspr SPRN_PMC7, r10
651 mtspr SPRN_PMC8, r11
652END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
653 ld r3, VCPU_MMCR(r4)
654 ld r5, VCPU_MMCR + 8(r4)
655 ld r6, VCPU_MMCR + 16(r4)
656 ld r7, VCPU_SIAR(r4)
657 ld r8, VCPU_SDAR(r4)
658 mtspr SPRN_MMCR1, r5
659 mtspr SPRN_MMCRA, r6
660 mtspr SPRN_SIAR, r7
661 mtspr SPRN_SDAR, r8
662 mtspr SPRN_MMCR0, r3
663 isync
664
665 /* Load up FP, VMX and VSX registers */
666 bl kvmppc_load_fp
667
668 ld r14, VCPU_GPR(R14)(r4)
669 ld r15, VCPU_GPR(R15)(r4)
670 ld r16, VCPU_GPR(R16)(r4)
671 ld r17, VCPU_GPR(R17)(r4)
672 ld r18, VCPU_GPR(R18)(r4)
673 ld r19, VCPU_GPR(R19)(r4)
674 ld r20, VCPU_GPR(R20)(r4)
675 ld r21, VCPU_GPR(R21)(r4)
676 ld r22, VCPU_GPR(R22)(r4)
677 ld r23, VCPU_GPR(R23)(r4)
678 ld r24, VCPU_GPR(R24)(r4)
679 ld r25, VCPU_GPR(R25)(r4)
680 ld r26, VCPU_GPR(R26)(r4)
681 ld r27, VCPU_GPR(R27)(r4)
682 ld r28, VCPU_GPR(R28)(r4)
683 ld r29, VCPU_GPR(R29)(r4)
684 ld r30, VCPU_GPR(R30)(r4)
685 ld r31, VCPU_GPR(R31)(r4)
686
687BEGIN_FTR_SECTION
688 /* Switch DSCR to guest value */
689 ld r5, VCPU_DSCR(r4)
690 mtspr SPRN_DSCR, r5
691END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
692
693 /*
694 * Set the decrementer to the guest decrementer.
695 */
696 ld r8,VCPU_DEC_EXPIRES(r4)
697 mftb r7
698 subf r3,r7,r8
699 mtspr SPRN_DEC,r3
700 stw r3,VCPU_DEC(r4)
701
702 ld r5, VCPU_SPRG0(r4)
703 ld r6, VCPU_SPRG1(r4)
704 ld r7, VCPU_SPRG2(r4)
705 ld r8, VCPU_SPRG3(r4)
706 mtspr SPRN_SPRG0, r5
707 mtspr SPRN_SPRG1, r6
708 mtspr SPRN_SPRG2, r7
709 mtspr SPRN_SPRG3, r8
710
711 /* Load up DAR and DSISR */
712 ld r5, VCPU_DAR(r4)
713 lwz r6, VCPU_DSISR(r4)
714 mtspr SPRN_DAR, r5
715 mtspr SPRN_DSISR, r6
716
717BEGIN_FTR_SECTION
718 /* Restore AMR and UAMOR, set AMOR to all 1s */
719 ld r5,VCPU_AMR(r4)
720 ld r6,VCPU_UAMOR(r4)
721 li r7,-1
722 mtspr SPRN_AMR,r5
723 mtspr SPRN_UAMOR,r6
724 mtspr SPRN_AMOR,r7
725END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
de56a948
PM
726
727 /* Restore state of CTRL run bit; assume 1 on entry */
728 lwz r5,VCPU_CTRL(r4)
729 andi. r5,r5,1
730 bne 4f
731 mfspr r6,SPRN_CTRLF
732 clrrdi r6,r6,1
733 mtspr SPRN_CTRLT,r6
7344:
735 ld r6, VCPU_CTR(r4)
736 lwz r7, VCPU_XER(r4)
737
738 mtctr r6
739 mtxer r7
740
4619ac88
PM
741 ld r10, VCPU_PC(r4)
742 ld r11, VCPU_MSR(r4)
19ccb76a 743kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */
de56a948
PM
744 ld r6, VCPU_SRR0(r4)
745 ld r7, VCPU_SRR1(r4)
de56a948 746
4619ac88 747 /* r11 = vcpu->arch.msr & ~MSR_HV */
de56a948
PM
748 rldicl r11, r11, 63 - MSR_HV_LG, 1
749 rotldi r11, r11, 1 + MSR_HV_LG
750 ori r11, r11, MSR_ME
751
19ccb76a
PM
752 /* Check if we can deliver an external or decrementer interrupt now */
753 ld r0,VCPU_PENDING_EXC(r4)
4619ac88 754 lis r8,(1 << BOOK3S_IRQPRIO_EXTERNAL_LEVEL)@h
19ccb76a
PM
755 and r0,r0,r8
756 cmpdi cr1,r0,0
757 andi. r0,r11,MSR_EE
758 beq cr1,11f
759BEGIN_FTR_SECTION
760 mfspr r8,SPRN_LPCR
761 ori r8,r8,LPCR_MER
762 mtspr SPRN_LPCR,r8
763 isync
764END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
765 beq 5f
766 li r0,BOOK3S_INTERRUPT_EXTERNAL
76712: mr r6,r10
768 mr r10,r0
769 mr r7,r11
770 li r11,(MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
771 rotldi r11,r11,63
772 b 5f
77311: beq 5f
774 mfspr r0,SPRN_DEC
775 cmpwi r0,0
776 li r0,BOOK3S_INTERRUPT_DECREMENTER
777 blt 12b
778
779 /* Move SRR0 and SRR1 into the respective regs */
7805: mtspr SPRN_SRR0, r6
781 mtspr SPRN_SRR1, r7
19ccb76a 782
27025a60
LPF
783/*
784 * Required state:
785 * R4 = vcpu
786 * R10: value for HSRR0
787 * R11: value for HSRR1
788 * R13 = PACA
789 */
de56a948 790fast_guest_return:
4619ac88
PM
791 li r0,0
792 stb r0,VCPU_CEDED(r4) /* cancel cede */
de56a948
PM
793 mtspr SPRN_HSRR0,r10
794 mtspr SPRN_HSRR1,r11
795
796 /* Activate guest mode, so faults get handled by KVM */
44a3add8 797 li r9, KVM_GUEST_MODE_GUEST_HV
de56a948
PM
798 stb r9, HSTATE_IN_GUEST(r13)
799
800 /* Enter guest */
801
0acb9111
PM
802BEGIN_FTR_SECTION
803 ld r5, VCPU_CFAR(r4)
804 mtspr SPRN_CFAR, r5
805END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
4b8473c9
PM
806BEGIN_FTR_SECTION
807 ld r0, VCPU_PPR(r4)
808END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
0acb9111 809
de56a948
PM
810 ld r5, VCPU_LR(r4)
811 lwz r6, VCPU_CR(r4)
812 mtlr r5
813 mtcr r6
814
c75df6f9
MN
815 ld r1, VCPU_GPR(R1)(r4)
816 ld r2, VCPU_GPR(R2)(r4)
817 ld r3, VCPU_GPR(R3)(r4)
818 ld r5, VCPU_GPR(R5)(r4)
819 ld r6, VCPU_GPR(R6)(r4)
820 ld r7, VCPU_GPR(R7)(r4)
821 ld r8, VCPU_GPR(R8)(r4)
822 ld r9, VCPU_GPR(R9)(r4)
823 ld r10, VCPU_GPR(R10)(r4)
824 ld r11, VCPU_GPR(R11)(r4)
825 ld r12, VCPU_GPR(R12)(r4)
826 ld r13, VCPU_GPR(R13)(r4)
827
4b8473c9
PM
828BEGIN_FTR_SECTION
829 mtspr SPRN_PPR, r0
830END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
831 ld r0, VCPU_GPR(R0)(r4)
c75df6f9 832 ld r4, VCPU_GPR(R4)(r4)
de56a948
PM
833
834 hrfid
835 b .
836
837/******************************************************************************
838 * *
839 * Exit code *
840 * *
841 *****************************************************************************/
842
843/*
844 * We come here from the first-level interrupt handlers.
845 */
dd96b2c2
AK
846 .globl kvmppc_interrupt_hv
847kvmppc_interrupt_hv:
de56a948
PM
848 /*
849 * Register contents:
850 * R12 = interrupt vector
851 * R13 = PACA
852 * guest CR, R12 saved in shadow VCPU SCRATCH1/0
853 * guest R13 saved in SPRN_SCRATCH0
854 */
855 /* abuse host_r2 as third scratch area; we get r2 from PACATOC(r13) */
856 std r9, HSTATE_HOST_R2(r13)
44a3add8
PM
857
858 lbz r9, HSTATE_IN_GUEST(r13)
859 cmpwi r9, KVM_GUEST_MODE_HOST_HV
860 beq kvmppc_bad_host_intr
dd96b2c2
AK
861#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
862 cmpwi r9, KVM_GUEST_MODE_GUEST
863 ld r9, HSTATE_HOST_R2(r13)
864 beq kvmppc_interrupt_pr
865#endif
44a3add8
PM
866 /* We're now back in the host but in guest MMU context */
867 li r9, KVM_GUEST_MODE_HOST_HV
868 stb r9, HSTATE_IN_GUEST(r13)
869
de56a948
PM
870 ld r9, HSTATE_KVM_VCPU(r13)
871
872 /* Save registers */
873
c75df6f9
MN
874 std r0, VCPU_GPR(R0)(r9)
875 std r1, VCPU_GPR(R1)(r9)
876 std r2, VCPU_GPR(R2)(r9)
877 std r3, VCPU_GPR(R3)(r9)
878 std r4, VCPU_GPR(R4)(r9)
879 std r5, VCPU_GPR(R5)(r9)
880 std r6, VCPU_GPR(R6)(r9)
881 std r7, VCPU_GPR(R7)(r9)
882 std r8, VCPU_GPR(R8)(r9)
de56a948 883 ld r0, HSTATE_HOST_R2(r13)
c75df6f9
MN
884 std r0, VCPU_GPR(R9)(r9)
885 std r10, VCPU_GPR(R10)(r9)
886 std r11, VCPU_GPR(R11)(r9)
de56a948
PM
887 ld r3, HSTATE_SCRATCH0(r13)
888 lwz r4, HSTATE_SCRATCH1(r13)
c75df6f9 889 std r3, VCPU_GPR(R12)(r9)
de56a948 890 stw r4, VCPU_CR(r9)
0acb9111
PM
891BEGIN_FTR_SECTION
892 ld r3, HSTATE_CFAR(r13)
893 std r3, VCPU_CFAR(r9)
894END_FTR_SECTION_IFSET(CPU_FTR_CFAR)
4b8473c9
PM
895BEGIN_FTR_SECTION
896 ld r4, HSTATE_PPR(r13)
897 std r4, VCPU_PPR(r9)
898END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
de56a948
PM
899
900 /* Restore R1/R2 so we can handle faults */
901 ld r1, HSTATE_HOST_R1(r13)
902 ld r2, PACATOC(r13)
903
904 mfspr r10, SPRN_SRR0
905 mfspr r11, SPRN_SRR1
906 std r10, VCPU_SRR0(r9)
907 std r11, VCPU_SRR1(r9)
908 andi. r0, r12, 2 /* need to read HSRR0/1? */
909 beq 1f
910 mfspr r10, SPRN_HSRR0
911 mfspr r11, SPRN_HSRR1
912 clrrdi r12, r12, 2
9131: std r10, VCPU_PC(r9)
914 std r11, VCPU_MSR(r9)
915
916 GET_SCRATCH0(r3)
917 mflr r4
c75df6f9 918 std r3, VCPU_GPR(R13)(r9)
de56a948
PM
919 std r4, VCPU_LR(r9)
920
de56a948
PM
921 stw r12,VCPU_TRAP(r9)
922
697d3899
PM
923 /* Save HEIR (HV emulation assist reg) in last_inst
924 if this is an HEI (HV emulation interrupt, e40) */
925 li r3,KVM_INST_FETCH_FAILED
926BEGIN_FTR_SECTION
927 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST
928 bne 11f
929 mfspr r3,SPRN_HEIR
930END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
93111: stw r3,VCPU_LAST_INST(r9)
932
933 /* these are volatile across C function calls */
934 mfctr r3
935 mfxer r4
936 std r3, VCPU_CTR(r9)
937 stw r4, VCPU_XER(r9)
938
939BEGIN_FTR_SECTION
940 /* If this is a page table miss then see if it's theirs or ours */
941 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
942 beq kvmppc_hdsi
342d3db7
PM
943 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE
944 beq kvmppc_hisi
697d3899
PM
945END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
946
de56a948
PM
947 /* See if this is a leftover HDEC interrupt */
948 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
949 bne 2f
950 mfspr r3,SPRN_HDEC
951 cmpwi r3,0
952 bge ignore_hdec
9532:
697d3899 954 /* See if this is an hcall we can handle in real mode */
a8606e20
PM
955 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL
956 beq hcall_try_real_mode
de56a948 957
54695c30 958 /* Only handle external interrupts here on arch 206 and later */
9e368f29 959BEGIN_FTR_SECTION
54695c30
BH
960 b ext_interrupt_to_host
961END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
962
963 /* External interrupt ? */
964 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
965 bne+ ext_interrupt_to_host
966
967 /* External interrupt, first check for host_ipi. If this is
968 * set, we know the host wants us out so let's do it now
969 */
4619ac88 970do_ext_interrupt:
c934243c
PM
971 bl kvmppc_read_intr
972 cmpdi r3, 0
973 bgt ext_interrupt_to_host
54695c30
BH
974
975 /* Allright, looks like an IPI for the guest, we need to set MER */
4619ac88
PM
976 /* Check if any CPU is heading out to the host, if so head out too */
977 ld r5, HSTATE_KVM_VCORE(r13)
978 lwz r0, VCORE_ENTRY_EXIT(r5)
979 cmpwi r0, 0x100
980 bge ext_interrupt_to_host
981
982 /* See if there is a pending interrupt for the guest */
983 mfspr r8, SPRN_LPCR
984 ld r0, VCPU_PENDING_EXC(r9)
985 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */
986 rldicl. r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63
987 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH
988 beq 2f
54695c30
BH
989
990 /* And if the guest EE is set, we can deliver immediately, else
991 * we return to the guest with MER set
992 */
993 andi. r0, r11, MSR_EE
4619ac88
PM
994 beq 2f
995 mtspr SPRN_SRR0, r10
996 mtspr SPRN_SRR1, r11
997 li r10, BOOK3S_INTERRUPT_EXTERNAL
998 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
999 rotldi r11, r11, 63
10002: mr r4, r9
1001 mtspr SPRN_LPCR, r8
54695c30
BH
1002 b fast_guest_return
1003
54695c30 1004ext_interrupt_to_host:
de56a948 1005
b4072df4 1006guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */
de56a948 1007 /* Save more register state */
de56a948
PM
1008 mfdar r6
1009 mfdsisr r7
de56a948
PM
1010 std r6, VCPU_DAR(r9)
1011 stw r7, VCPU_DSISR(r9)
9e368f29 1012BEGIN_FTR_SECTION
697d3899 1013 /* don't overwrite fault_dar/fault_dsisr if HDSI */
de56a948
PM
1014 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE
1015 beq 6f
9e368f29 1016END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
697d3899 1017 std r6, VCPU_FAULT_DAR(r9)
de56a948
PM
1018 stw r7, VCPU_FAULT_DSISR(r9)
1019
b4072df4
PM
1020 /* See if it is a machine check */
1021 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1022 beq machine_check_realmode
1023mc_cont:
1024
de56a948 1025 /* Save guest CTRL register, set runlatch to 1 */
697d3899 10266: mfspr r6,SPRN_CTRLF
de56a948
PM
1027 stw r6,VCPU_CTRL(r9)
1028 andi. r0,r6,1
1029 bne 4f
1030 ori r6,r6,1
1031 mtspr SPRN_CTRLT,r6
10324:
1033 /* Read the guest SLB and save it away */
1034 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */
1035 mtctr r0
1036 li r6,0
1037 addi r7,r9,VCPU_SLB
1038 li r5,0
10391: slbmfee r8,r6
1040 andis. r0,r8,SLB_ESID_V@h
1041 beq 2f
1042 add r8,r8,r6 /* put index in */
1043 slbmfev r3,r6
1044 std r8,VCPU_SLB_E(r7)
1045 std r3,VCPU_SLB_V(r7)
1046 addi r7,r7,VCPU_SLB_SIZE
1047 addi r5,r5,1
10482: addi r6,r6,1
1049 bdnz 1b
1050 stw r5,VCPU_SLB_MAX(r9)
1051
1052 /*
1053 * Save the guest PURR/SPURR
1054 */
9e368f29 1055BEGIN_FTR_SECTION
de56a948
PM
1056 mfspr r5,SPRN_PURR
1057 mfspr r6,SPRN_SPURR
1058 ld r7,VCPU_PURR(r9)
1059 ld r8,VCPU_SPURR(r9)
1060 std r5,VCPU_PURR(r9)
1061 std r6,VCPU_SPURR(r9)
1062 subf r5,r7,r5
1063 subf r6,r8,r6
1064
1065 /*
1066 * Restore host PURR/SPURR and add guest times
1067 * so that the time in the guest gets accounted.
1068 */
1069 ld r3,HSTATE_PURR(r13)
1070 ld r4,HSTATE_SPURR(r13)
1071 add r3,r3,r5
1072 add r4,r4,r6
1073 mtspr SPRN_PURR,r3
1074 mtspr SPRN_SPURR,r4
9e368f29 1075END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_201)
de56a948 1076
e0b7ec05
PM
1077 /* Save DEC */
1078 mfspr r5,SPRN_DEC
1079 mftb r6
1080 extsw r5,r5
1081 add r5,r5,r6
1082 std r5,VCPU_DEC_EXPIRES(r9)
1083
1084 /* Save and reset AMR and UAMOR before turning on the MMU */
1085BEGIN_FTR_SECTION
1086 mfspr r5,SPRN_AMR
1087 mfspr r6,SPRN_UAMOR
1088 std r5,VCPU_AMR(r9)
1089 std r6,VCPU_UAMOR(r9)
1090 li r6,0
1091 mtspr SPRN_AMR,r6
1092END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1093
1094 /* Switch DSCR back to host value */
1095BEGIN_FTR_SECTION
1096 mfspr r8, SPRN_DSCR
1097 ld r7, HSTATE_DSCR(r13)
1098 std r8, VCPU_DSCR(r9)
1099 mtspr SPRN_DSCR, r7
1100END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1101
1102 /* Save non-volatile GPRs */
1103 std r14, VCPU_GPR(R14)(r9)
1104 std r15, VCPU_GPR(R15)(r9)
1105 std r16, VCPU_GPR(R16)(r9)
1106 std r17, VCPU_GPR(R17)(r9)
1107 std r18, VCPU_GPR(R18)(r9)
1108 std r19, VCPU_GPR(R19)(r9)
1109 std r20, VCPU_GPR(R20)(r9)
1110 std r21, VCPU_GPR(R21)(r9)
1111 std r22, VCPU_GPR(R22)(r9)
1112 std r23, VCPU_GPR(R23)(r9)
1113 std r24, VCPU_GPR(R24)(r9)
1114 std r25, VCPU_GPR(R25)(r9)
1115 std r26, VCPU_GPR(R26)(r9)
1116 std r27, VCPU_GPR(R27)(r9)
1117 std r28, VCPU_GPR(R28)(r9)
1118 std r29, VCPU_GPR(R29)(r9)
1119 std r30, VCPU_GPR(R30)(r9)
1120 std r31, VCPU_GPR(R31)(r9)
1121
1122 /* Save SPRGs */
1123 mfspr r3, SPRN_SPRG0
1124 mfspr r4, SPRN_SPRG1
1125 mfspr r5, SPRN_SPRG2
1126 mfspr r6, SPRN_SPRG3
1127 std r3, VCPU_SPRG0(r9)
1128 std r4, VCPU_SPRG1(r9)
1129 std r5, VCPU_SPRG2(r9)
1130 std r6, VCPU_SPRG3(r9)
1131
1132 /* save FP state */
1133 mr r3, r9
1134 bl kvmppc_save_fp
1135
1136 /* Increment yield count if they have a VPA */
1137 ld r8, VCPU_VPA(r9) /* do they have a VPA? */
1138 cmpdi r8, 0
1139 beq 25f
1140 lwz r3, LPPACA_YIELDCOUNT(r8)
1141 addi r3, r3, 1
1142 stw r3, LPPACA_YIELDCOUNT(r8)
1143 li r3, 1
1144 stb r3, VCPU_VPA_DIRTY(r9)
114525:
1146 /* Save PMU registers if requested */
1147 /* r8 and cr0.eq are live here */
1148 li r3, 1
1149 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */
1150 mfspr r4, SPRN_MMCR0 /* save MMCR0 */
1151 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */
1152 mfspr r6, SPRN_MMCRA
1153BEGIN_FTR_SECTION
1154 /* On P7, clear MMCRA in order to disable SDAR updates */
1155 li r7, 0
1156 mtspr SPRN_MMCRA, r7
1157END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
1158 isync
1159 beq 21f /* if no VPA, save PMU stuff anyway */
1160 lbz r7, LPPACA_PMCINUSE(r8)
1161 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */
1162 bne 21f
1163 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */
1164 b 22f
116521: mfspr r5, SPRN_MMCR1
1166 mfspr r7, SPRN_SIAR
1167 mfspr r8, SPRN_SDAR
1168 std r4, VCPU_MMCR(r9)
1169 std r5, VCPU_MMCR + 8(r9)
1170 std r6, VCPU_MMCR + 16(r9)
1171 std r7, VCPU_SIAR(r9)
1172 std r8, VCPU_SDAR(r9)
1173 mfspr r3, SPRN_PMC1
1174 mfspr r4, SPRN_PMC2
1175 mfspr r5, SPRN_PMC3
1176 mfspr r6, SPRN_PMC4
1177 mfspr r7, SPRN_PMC5
1178 mfspr r8, SPRN_PMC6
1179BEGIN_FTR_SECTION
1180 mfspr r10, SPRN_PMC7
1181 mfspr r11, SPRN_PMC8
1182END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1183 stw r3, VCPU_PMC(r9)
1184 stw r4, VCPU_PMC + 4(r9)
1185 stw r5, VCPU_PMC + 8(r9)
1186 stw r6, VCPU_PMC + 12(r9)
1187 stw r7, VCPU_PMC + 16(r9)
1188 stw r8, VCPU_PMC + 20(r9)
1189BEGIN_FTR_SECTION
1190 stw r10, VCPU_PMC + 24(r9)
1191 stw r11, VCPU_PMC + 28(r9)
1192END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
119322:
de56a948
PM
1194 /* Clear out SLB */
1195 li r5,0
1196 slbmte r5,r5
1197 slbia
1198 ptesync
1199
e0b7ec05 1200hdec_soon: /* r12 = trap, r13 = paca */
9e368f29
PM
1201BEGIN_FTR_SECTION
1202 b 32f
1203END_FTR_SECTION_IFSET(CPU_FTR_ARCH_201)
1204 /*
1205 * POWER7 guest -> host partition switch code.
1206 * We don't have to lock against tlbies but we do
1207 * have to coordinate the hardware threads.
1208 */
371fefd6
PM
1209 /* Increment the threads-exiting-guest count in the 0xff00
1210 bits of vcore->entry_exit_count */
1211 lwsync
1212 ld r5,HSTATE_KVM_VCORE(r13)
1213 addi r6,r5,VCORE_ENTRY_EXIT
121441: lwarx r3,0,r6
1215 addi r0,r3,0x100
1216 stwcx. r0,0,r6
1217 bne 41b
19ccb76a 1218 lwsync
371fefd6
PM
1219
1220 /*
1221 * At this point we have an interrupt that we have to pass
1222 * up to the kernel or qemu; we can't handle it in real mode.
1223 * Thus we have to do a partition switch, so we have to
1224 * collect the other threads, if we are the first thread
1225 * to take an interrupt. To do this, we set the HDEC to 0,
1226 * which causes an HDEC interrupt in all threads within 2ns
1227 * because the HDEC register is shared between all 4 threads.
1228 * However, we don't need to bother if this is an HDEC
1229 * interrupt, since the other threads will already be on their
1230 * way here in that case.
1231 */
19ccb76a
PM
1232 cmpwi r3,0x100 /* Are we the first here? */
1233 bge 43f
371fefd6
PM
1234 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER
1235 beq 40f
371fefd6
PM
1236 li r0,0
1237 mtspr SPRN_HDEC,r0
123840:
19ccb76a
PM
1239 /*
1240 * Send an IPI to any napping threads, since an HDEC interrupt
1241 * doesn't wake CPUs up from nap.
1242 */
1243 lwz r3,VCORE_NAPPING_THREADS(r5)
e0b7ec05 1244 lbz r4,HSTATE_PTID(r13)
19ccb76a 1245 li r0,1
2f584a14 1246 sld r0,r0,r4
19ccb76a
PM
1247 andc. r3,r3,r0 /* no sense IPI'ing ourselves */
1248 beq 43f
1249 mulli r4,r4,PACA_SIZE /* get paca for thread 0 */
1250 subf r6,r4,r13
125142: andi. r0,r3,1
1252 beq 44f
1253 ld r8,HSTATE_XICS_PHYS(r6) /* get thread's XICS reg addr */
1254 li r0,IPI_PRIORITY
54695c30 1255 li r7,XICS_MFRR
19ccb76a
PM
1256 stbcix r0,r7,r8 /* trigger the IPI */
125744: srdi. r3,r3,1
1258 addi r6,r6,PACA_SIZE
1259 bne 42b
371fefd6 1260
e0b7ec05 1261secondary_too_late:
371fefd6 1262 /* Secondary threads wait for primary to do partition switch */
e0b7ec05
PM
126343: ld r5,HSTATE_KVM_VCORE(r13)
1264 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
1265 lbz r3,HSTATE_PTID(r13)
371fefd6
PM
1266 cmpwi r3,0
1267 beq 15f
1268 HMT_LOW
126913: lbz r3,VCORE_IN_GUEST(r5)
1270 cmpwi r3,0
1271 bne 13b
1272 HMT_MEDIUM
1273 b 16f
1274
1275 /* Primary thread waits for all the secondaries to exit guest */
127615: lwz r3,VCORE_ENTRY_EXIT(r5)
1277 srwi r0,r3,8
1278 clrldi r3,r3,56
1279 cmpw r3,r0
1280 bne 15b
1281 isync
1282
1283 /* Primary thread switches back to host partition */
de56a948
PM
1284 ld r6,KVM_HOST_SDR1(r4)
1285 lwz r7,KVM_HOST_LPID(r4)
1286 li r8,LPID_RSVD /* switch to reserved LPID */
1287 mtspr SPRN_LPID,r8
1288 ptesync
1289 mtspr SPRN_SDR1,r6 /* switch to partition page table */
1290 mtspr SPRN_LPID,r7
1291 isync
93b0f4dc
PM
1292
1293 /* Subtract timebase offset from timebase */
1294 ld r8,VCORE_TB_OFFSET(r5)
1295 cmpdi r8,0
1296 beq 17f
1297 mftb r6 /* current host timebase */
1298 subf r8,r8,r6
1299 mtspr SPRN_TBU40,r8 /* update upper 40 bits */
1300 mftb r7 /* check if lower 24 bits overflowed */
1301 clrldi r6,r6,40
1302 clrldi r7,r7,40
1303 cmpld r7,r6
1304 bge 17f
1305 addis r8,r8,0x100 /* if so, increment upper 40 bits */
1306 mtspr SPRN_TBU40,r8
1307
388cc6e1
PM
1308 /* Reset PCR */
130917: ld r0, VCORE_PCR(r5)
1310 cmpdi r0, 0
1311 beq 18f
1312 li r0, 0
1313 mtspr SPRN_PCR, r0
131418:
93b0f4dc 1315 /* Signal secondary CPUs to continue */
371fefd6 1316 stb r0,VCORE_IN_GUEST(r5)
de56a948
PM
1317 lis r8,0x7fff /* MAX_INT@h */
1318 mtspr SPRN_HDEC,r8
1319
371fefd6 132016: ld r8,KVM_HOST_LPCR(r4)
de56a948
PM
1321 mtspr SPRN_LPCR,r8
1322 isync
9e368f29
PM
1323 b 33f
1324
1325 /*
1326 * PPC970 guest -> host partition switch code.
1327 * We have to lock against concurrent tlbies, and
1328 * we have to flush the whole TLB.
1329 */
e0b7ec05
PM
133032: ld r5,HSTATE_KVM_VCORE(r13)
1331 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */
9e368f29
PM
1332
1333 /* Take the guest's tlbie_lock */
54bb7f4b 1334#ifdef __BIG_ENDIAN__
9e368f29 1335 lwz r8,PACA_LOCK_TOKEN(r13)
54bb7f4b
AB
1336#else
1337 lwz r8,PACAPACAINDEX(r13)
1338#endif
9e368f29
PM
1339 addi r3,r4,KVM_TLBIE_LOCK
134024: lwarx r0,0,r3
1341 cmpwi r0,0
1342 bne 24b
1343 stwcx. r8,0,r3
1344 bne 24b
1345 isync
1346
1347 ld r7,KVM_HOST_LPCR(r4) /* use kvm->arch.host_lpcr for HID4 */
1348 li r0,0x18f
1349 rotldi r0,r0,HID4_LPID5_SH /* all lpid bits in HID4 = 1 */
1350 or r0,r7,r0
1351 ptesync
1352 sync
1353 mtspr SPRN_HID4,r0 /* switch to reserved LPID */
1354 isync
1355 li r0,0
1356 stw r0,0(r3) /* drop guest tlbie_lock */
1357
1358 /* invalidate the whole TLB */
1359 li r0,256
1360 mtctr r0
1361 li r6,0
136225: tlbiel r6
1363 addi r6,r6,0x1000
1364 bdnz 25b
1365 ptesync
1366
1367 /* take native_tlbie_lock */
1368 ld r3,toc_tlbie_lock@toc(2)
136924: lwarx r0,0,r3
1370 cmpwi r0,0
1371 bne 24b
1372 stwcx. r8,0,r3
1373 bne 24b
1374 isync
1375
1376 ld r6,KVM_HOST_SDR1(r4)
1377 mtspr SPRN_SDR1,r6 /* switch to host page table */
1378
1379 /* Set up host HID4 value */
1380 sync
1381 mtspr SPRN_HID4,r7
1382 isync
1383 li r0,0
1384 stw r0,0(r3) /* drop native_tlbie_lock */
1385
1386 lis r8,0x7fff /* MAX_INT@h */
1387 mtspr SPRN_HDEC,r8
1388
1389 /* Disable HDEC interrupts */
1390 mfspr r0,SPRN_HID0
1391 li r3,0
1392 rldimi r0,r3, HID0_HDICE_SH, 64-HID0_HDICE_SH-1
1393 sync
1394 mtspr SPRN_HID0,r0
1395 mfspr r0,SPRN_HID0
1396 mfspr r0,SPRN_HID0
1397 mfspr r0,SPRN_HID0
1398 mfspr r0,SPRN_HID0
1399 mfspr r0,SPRN_HID0
1400 mfspr r0,SPRN_HID0
de56a948
PM
1401
1402 /* load host SLB entries */
9e368f29 140333: ld r8,PACA_SLBSHADOWPTR(r13)
de56a948
PM
1404
1405 .rept SLB_NUM_BOLTED
1406 ld r5,SLBSHADOW_SAVEAREA(r8)
1407 ld r6,SLBSHADOW_SAVEAREA+8(r8)
1408 andis. r7,r5,SLB_ESID_V@h
1409 beq 1f
1410 slbmte r6,r5
14111: addi r8,r8,16
1412 .endr
1413
44a3add8
PM
1414 /* Unset guest mode */
1415 li r0, KVM_GUEST_MODE_NONE
1416 stb r0, HSTATE_IN_GUEST(r13)
1417
218309b7
PM
1418 ld r0, 112+PPC_LR_STKOFF(r1)
1419 addi r1, r1, 112
1420 mtlr r0
1421 blr
b4072df4 1422
697d3899
PM
1423/*
1424 * Check whether an HDSI is an HPTE not found fault or something else.
1425 * If it is an HPTE not found fault that is due to the guest accessing
1426 * a page that they have mapped but which we have paged out, then
1427 * we continue on with the guest exit path. In all other cases,
1428 * reflect the HDSI to the guest as a DSI.
1429 */
1430kvmppc_hdsi:
1431 mfspr r4, SPRN_HDAR
1432 mfspr r6, SPRN_HDSISR
4cf302bc
PM
1433 /* HPTE not found fault or protection fault? */
1434 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h
697d3899
PM
1435 beq 1f /* if not, send it to the guest */
1436 andi. r0, r11, MSR_DR /* data relocation enabled? */
1437 beq 3f
1438 clrrdi r0, r4, 28
c75df6f9 1439 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
697d3899
PM
1440 bne 1f /* if no SLB entry found */
14414: std r4, VCPU_FAULT_DAR(r9)
1442 stw r6, VCPU_FAULT_DSISR(r9)
1443
1444 /* Search the hash table. */
1445 mr r3, r9 /* vcpu pointer */
342d3db7 1446 li r7, 1 /* data fault */
697d3899
PM
1447 bl .kvmppc_hpte_hv_fault
1448 ld r9, HSTATE_KVM_VCPU(r13)
1449 ld r10, VCPU_PC(r9)
1450 ld r11, VCPU_MSR(r9)
1451 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE
1452 cmpdi r3, 0 /* retry the instruction */
1453 beq 6f
1454 cmpdi r3, -1 /* handle in kernel mode */
b4072df4 1455 beq guest_exit_cont
697d3899
PM
1456 cmpdi r3, -2 /* MMIO emulation; need instr word */
1457 beq 2f
1458
1459 /* Synthesize a DSI for the guest */
1460 ld r4, VCPU_FAULT_DAR(r9)
1461 mr r6, r3
14621: mtspr SPRN_DAR, r4
1463 mtspr SPRN_DSISR, r6
1464 mtspr SPRN_SRR0, r10
1465 mtspr SPRN_SRR1, r11
1466 li r10, BOOK3S_INTERRUPT_DATA_STORAGE
1467 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1468 rotldi r11, r11, 63
b4072df4 1469fast_interrupt_c_return:
697d3899
PM
14706: ld r7, VCPU_CTR(r9)
1471 lwz r8, VCPU_XER(r9)
1472 mtctr r7
1473 mtxer r8
1474 mr r4, r9
1475 b fast_guest_return
1476
14773: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */
1478 ld r5, KVM_VRMA_SLB_V(r5)
1479 b 4b
1480
1481 /* If this is for emulated MMIO, load the instruction word */
14822: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */
1483
1484 /* Set guest mode to 'jump over instruction' so if lwz faults
1485 * we'll just continue at the next IP. */
1486 li r0, KVM_GUEST_MODE_SKIP
1487 stb r0, HSTATE_IN_GUEST(r13)
1488
1489 /* Do the access with MSR:DR enabled */
1490 mfmsr r3
1491 ori r4, r3, MSR_DR /* Enable paging for data */
1492 mtmsrd r4
1493 lwz r8, 0(r10)
1494 mtmsrd r3
1495
1496 /* Store the result */
1497 stw r8, VCPU_LAST_INST(r9)
1498
1499 /* Unset guest mode. */
44a3add8 1500 li r0, KVM_GUEST_MODE_HOST_HV
697d3899 1501 stb r0, HSTATE_IN_GUEST(r13)
b4072df4 1502 b guest_exit_cont
de56a948 1503
342d3db7
PM
1504/*
1505 * Similarly for an HISI, reflect it to the guest as an ISI unless
1506 * it is an HPTE not found fault for a page that we have paged out.
1507 */
1508kvmppc_hisi:
1509 andis. r0, r11, SRR1_ISI_NOPT@h
1510 beq 1f
1511 andi. r0, r11, MSR_IR /* instruction relocation enabled? */
1512 beq 3f
1513 clrrdi r0, r10, 28
c75df6f9 1514 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */
342d3db7
PM
1515 bne 1f /* if no SLB entry found */
15164:
1517 /* Search the hash table. */
1518 mr r3, r9 /* vcpu pointer */
1519 mr r4, r10
1520 mr r6, r11
1521 li r7, 0 /* instruction fault */
1522 bl .kvmppc_hpte_hv_fault
1523 ld r9, HSTATE_KVM_VCPU(r13)
1524 ld r10, VCPU_PC(r9)
1525 ld r11, VCPU_MSR(r9)
1526 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE
1527 cmpdi r3, 0 /* retry the instruction */
b4072df4 1528 beq fast_interrupt_c_return
342d3db7 1529 cmpdi r3, -1 /* handle in kernel mode */
b4072df4 1530 beq guest_exit_cont
342d3db7
PM
1531
1532 /* Synthesize an ISI for the guest */
1533 mr r11, r3
15341: mtspr SPRN_SRR0, r10
1535 mtspr SPRN_SRR1, r11
1536 li r10, BOOK3S_INTERRUPT_INST_STORAGE
1537 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1538 rotldi r11, r11, 63
b4072df4 1539 b fast_interrupt_c_return
342d3db7
PM
1540
15413: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */
1542 ld r5, KVM_VRMA_SLB_V(r6)
1543 b 4b
1544
a8606e20
PM
1545/*
1546 * Try to handle an hcall in real mode.
1547 * Returns to the guest if we handle it, or continues on up to
1548 * the kernel if we can't (i.e. if we don't have a handler for
1549 * it, or if the handler returns H_TOO_HARD).
1550 */
1551 .globl hcall_try_real_mode
1552hcall_try_real_mode:
c75df6f9 1553 ld r3,VCPU_GPR(R3)(r9)
a8606e20 1554 andi. r0,r11,MSR_PR
27025a60
LPF
1555 /* sc 1 from userspace - reflect to guest syscall */
1556 bne sc_1_fast_return
a8606e20
PM
1557 clrrdi r3,r3,2
1558 cmpldi r3,hcall_real_table_end - hcall_real_table
b4072df4 1559 bge guest_exit_cont
a8606e20 1560 LOAD_REG_ADDR(r4, hcall_real_table)
4baa1d87 1561 lwax r3,r3,r4
a8606e20 1562 cmpwi r3,0
b4072df4 1563 beq guest_exit_cont
a8606e20
PM
1564 add r3,r3,r4
1565 mtctr r3
1566 mr r3,r9 /* get vcpu pointer */
c75df6f9 1567 ld r4,VCPU_GPR(R4)(r9)
a8606e20
PM
1568 bctrl
1569 cmpdi r3,H_TOO_HARD
1570 beq hcall_real_fallback
1571 ld r4,HSTATE_KVM_VCPU(r13)
c75df6f9 1572 std r3,VCPU_GPR(R3)(r4)
a8606e20
PM
1573 ld r10,VCPU_PC(r4)
1574 ld r11,VCPU_MSR(r4)
1575 b fast_guest_return
1576
27025a60
LPF
1577sc_1_fast_return:
1578 mtspr SPRN_SRR0,r10
1579 mtspr SPRN_SRR1,r11
1580 li r10, BOOK3S_INTERRUPT_SYSCALL
1581 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1582 rotldi r11, r11, 63
1583 mr r4,r9
1584 b fast_guest_return
1585
a8606e20
PM
1586 /* We've attempted a real mode hcall, but it's punted it back
1587 * to userspace. We need to restore some clobbered volatiles
1588 * before resuming the pass-it-to-qemu path */
1589hcall_real_fallback:
1590 li r12,BOOK3S_INTERRUPT_SYSCALL
1591 ld r9, HSTATE_KVM_VCPU(r13)
a8606e20 1592
b4072df4 1593 b guest_exit_cont
a8606e20
PM
1594
1595 .globl hcall_real_table
1596hcall_real_table:
1597 .long 0 /* 0 - unused */
1598 .long .kvmppc_h_remove - hcall_real_table
1599 .long .kvmppc_h_enter - hcall_real_table
1600 .long .kvmppc_h_read - hcall_real_table
1601 .long 0 /* 0x10 - H_CLEAR_MOD */
1602 .long 0 /* 0x14 - H_CLEAR_REF */
1603 .long .kvmppc_h_protect - hcall_real_table
1604 .long 0 /* 0x1c - H_GET_TCE */
54738c09 1605 .long .kvmppc_h_put_tce - hcall_real_table
a8606e20
PM
1606 .long 0 /* 0x24 - H_SET_SPRG0 */
1607 .long .kvmppc_h_set_dabr - hcall_real_table
1608 .long 0 /* 0x2c */
1609 .long 0 /* 0x30 */
1610 .long 0 /* 0x34 */
1611 .long 0 /* 0x38 */
1612 .long 0 /* 0x3c */
1613 .long 0 /* 0x40 */
1614 .long 0 /* 0x44 */
1615 .long 0 /* 0x48 */
1616 .long 0 /* 0x4c */
1617 .long 0 /* 0x50 */
1618 .long 0 /* 0x54 */
1619 .long 0 /* 0x58 */
1620 .long 0 /* 0x5c */
1621 .long 0 /* 0x60 */
e7d26f28
BH
1622#ifdef CONFIG_KVM_XICS
1623 .long .kvmppc_rm_h_eoi - hcall_real_table
1624 .long .kvmppc_rm_h_cppr - hcall_real_table
1625 .long .kvmppc_rm_h_ipi - hcall_real_table
1626 .long 0 /* 0x70 - H_IPOLL */
1627 .long .kvmppc_rm_h_xirr - hcall_real_table
1628#else
1629 .long 0 /* 0x64 - H_EOI */
1630 .long 0 /* 0x68 - H_CPPR */
1631 .long 0 /* 0x6c - H_IPI */
1632 .long 0 /* 0x70 - H_IPOLL */
1633 .long 0 /* 0x74 - H_XIRR */
1634#endif
a8606e20
PM
1635 .long 0 /* 0x78 */
1636 .long 0 /* 0x7c */
1637 .long 0 /* 0x80 */
1638 .long 0 /* 0x84 */
1639 .long 0 /* 0x88 */
1640 .long 0 /* 0x8c */
1641 .long 0 /* 0x90 */
1642 .long 0 /* 0x94 */
1643 .long 0 /* 0x98 */
1644 .long 0 /* 0x9c */
1645 .long 0 /* 0xa0 */
1646 .long 0 /* 0xa4 */
1647 .long 0 /* 0xa8 */
1648 .long 0 /* 0xac */
1649 .long 0 /* 0xb0 */
1650 .long 0 /* 0xb4 */
1651 .long 0 /* 0xb8 */
1652 .long 0 /* 0xbc */
1653 .long 0 /* 0xc0 */
1654 .long 0 /* 0xc4 */
1655 .long 0 /* 0xc8 */
1656 .long 0 /* 0xcc */
1657 .long 0 /* 0xd0 */
1658 .long 0 /* 0xd4 */
1659 .long 0 /* 0xd8 */
1660 .long 0 /* 0xdc */
19ccb76a 1661 .long .kvmppc_h_cede - hcall_real_table
a8606e20
PM
1662 .long 0 /* 0xe4 */
1663 .long 0 /* 0xe8 */
1664 .long 0 /* 0xec */
1665 .long 0 /* 0xf0 */
1666 .long 0 /* 0xf4 */
1667 .long 0 /* 0xf8 */
1668 .long 0 /* 0xfc */
1669 .long 0 /* 0x100 */
1670 .long 0 /* 0x104 */
1671 .long 0 /* 0x108 */
1672 .long 0 /* 0x10c */
1673 .long 0 /* 0x110 */
1674 .long 0 /* 0x114 */
1675 .long 0 /* 0x118 */
1676 .long 0 /* 0x11c */
1677 .long 0 /* 0x120 */
1678 .long .kvmppc_h_bulk_remove - hcall_real_table
1679hcall_real_table_end:
1680
de56a948
PM
1681ignore_hdec:
1682 mr r4,r9
1683 b fast_guest_return
1684
a8606e20 1685_GLOBAL(kvmppc_h_set_dabr)
eee7ff9d
MN
1686BEGIN_FTR_SECTION
1687 b 2f
1688END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
a8606e20 1689 std r4,VCPU_DABR(r3)
8943633c
PM
1690 /* Work around P7 bug where DABR can get corrupted on mtspr */
16911: mtspr SPRN_DABR,r4
1692 mfspr r5, SPRN_DABR
1693 cmpd r4, r5
1694 bne 1b
1695 isync
eee7ff9d 16962: li r3,0
a8606e20
PM
1697 blr
1698
19ccb76a
PM
1699_GLOBAL(kvmppc_h_cede)
1700 ori r11,r11,MSR_EE
1701 std r11,VCPU_MSR(r3)
1702 li r0,1
1703 stb r0,VCPU_CEDED(r3)
1704 sync /* order setting ceded vs. testing prodded */
1705 lbz r5,VCPU_PRODDED(r3)
1706 cmpwi r5,0
04f995a5 1707 bne kvm_cede_prodded
19ccb76a
PM
1708 li r0,0 /* set trap to 0 to say hcall is handled */
1709 stw r0,VCPU_TRAP(r3)
1710 li r0,H_SUCCESS
c75df6f9 1711 std r0,VCPU_GPR(R3)(r3)
19ccb76a 1712BEGIN_FTR_SECTION
04f995a5 1713 b kvm_cede_exit /* just send it up to host on 970 */
19ccb76a
PM
1714END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_206)
1715
1716 /*
1717 * Set our bit in the bitmask of napping threads unless all the
1718 * other threads are already napping, in which case we send this
1719 * up to the host.
1720 */
1721 ld r5,HSTATE_KVM_VCORE(r13)
e0b7ec05 1722 lbz r6,HSTATE_PTID(r13)
19ccb76a
PM
1723 lwz r8,VCORE_ENTRY_EXIT(r5)
1724 clrldi r8,r8,56
1725 li r0,1
1726 sld r0,r0,r6
1727 addi r6,r5,VCORE_NAPPING_THREADS
172831: lwarx r4,0,r6
1729 or r4,r4,r0
c75df6f9 1730 PPC_POPCNTW(R7,R4)
19ccb76a 1731 cmpw r7,r8
04f995a5 1732 bge kvm_cede_exit
19ccb76a
PM
1733 stwcx. r4,0,r6
1734 bne 31b
e0b7ec05 1735 li r0,NAPPING_CEDE
19ccb76a
PM
1736 stb r0,HSTATE_NAPPING(r13)
1737 /* order napping_threads update vs testing entry_exit_count */
1738 lwsync
1739 mr r4,r3
1740 lwz r7,VCORE_ENTRY_EXIT(r5)
1741 cmpwi r7,0x100
1742 bge 33f /* another thread already exiting */
1743
1744/*
1745 * Although not specifically required by the architecture, POWER7
1746 * preserves the following registers in nap mode, even if an SMT mode
1747 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3,
1748 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR.
1749 */
1750 /* Save non-volatile GPRs */
c75df6f9
MN
1751 std r14, VCPU_GPR(R14)(r3)
1752 std r15, VCPU_GPR(R15)(r3)
1753 std r16, VCPU_GPR(R16)(r3)
1754 std r17, VCPU_GPR(R17)(r3)
1755 std r18, VCPU_GPR(R18)(r3)
1756 std r19, VCPU_GPR(R19)(r3)
1757 std r20, VCPU_GPR(R20)(r3)
1758 std r21, VCPU_GPR(R21)(r3)
1759 std r22, VCPU_GPR(R22)(r3)
1760 std r23, VCPU_GPR(R23)(r3)
1761 std r24, VCPU_GPR(R24)(r3)
1762 std r25, VCPU_GPR(R25)(r3)
1763 std r26, VCPU_GPR(R26)(r3)
1764 std r27, VCPU_GPR(R27)(r3)
1765 std r28, VCPU_GPR(R28)(r3)
1766 std r29, VCPU_GPR(R29)(r3)
1767 std r30, VCPU_GPR(R30)(r3)
1768 std r31, VCPU_GPR(R31)(r3)
19ccb76a
PM
1769
1770 /* save FP state */
595e4f7e 1771 bl kvmppc_save_fp
19ccb76a
PM
1772
1773 /*
1774 * Take a nap until a decrementer or external interrupt occurs,
1775 * with PECE1 (wake on decr) and PECE0 (wake on external) set in LPCR
1776 */
f0888f70
PM
1777 li r0,1
1778 stb r0,HSTATE_HWTHREAD_REQ(r13)
19ccb76a
PM
1779 mfspr r5,SPRN_LPCR
1780 ori r5,r5,LPCR_PECE0 | LPCR_PECE1
1781 mtspr SPRN_LPCR,r5
1782 isync
1783 li r0, 0
1784 std r0, HSTATE_SCRATCH0(r13)
1785 ptesync
1786 ld r0, HSTATE_SCRATCH0(r13)
17871: cmpd r0, r0
1788 bne 1b
1789 nap
1790 b .
1791
1792kvm_end_cede:
4619ac88
PM
1793 /* get vcpu pointer */
1794 ld r4, HSTATE_KVM_VCPU(r13)
1795
19ccb76a
PM
1796 /* Woken by external or decrementer interrupt */
1797 ld r1, HSTATE_HOST_R1(r13)
19ccb76a 1798
19ccb76a
PM
1799 /* load up FP state */
1800 bl kvmppc_load_fp
1801
1802 /* Load NV GPRS */
c75df6f9
MN
1803 ld r14, VCPU_GPR(R14)(r4)
1804 ld r15, VCPU_GPR(R15)(r4)
1805 ld r16, VCPU_GPR(R16)(r4)
1806 ld r17, VCPU_GPR(R17)(r4)
1807 ld r18, VCPU_GPR(R18)(r4)
1808 ld r19, VCPU_GPR(R19)(r4)
1809 ld r20, VCPU_GPR(R20)(r4)
1810 ld r21, VCPU_GPR(R21)(r4)
1811 ld r22, VCPU_GPR(R22)(r4)
1812 ld r23, VCPU_GPR(R23)(r4)
1813 ld r24, VCPU_GPR(R24)(r4)
1814 ld r25, VCPU_GPR(R25)(r4)
1815 ld r26, VCPU_GPR(R26)(r4)
1816 ld r27, VCPU_GPR(R27)(r4)
1817 ld r28, VCPU_GPR(R28)(r4)
1818 ld r29, VCPU_GPR(R29)(r4)
1819 ld r30, VCPU_GPR(R30)(r4)
1820 ld r31, VCPU_GPR(R31)(r4)
19ccb76a
PM
1821
1822 /* clear our bit in vcore->napping_threads */
182333: ld r5,HSTATE_KVM_VCORE(r13)
e0b7ec05 1824 lbz r3,HSTATE_PTID(r13)
19ccb76a
PM
1825 li r0,1
1826 sld r0,r0,r3
1827 addi r6,r5,VCORE_NAPPING_THREADS
182832: lwarx r7,0,r6
1829 andc r7,r7,r0
1830 stwcx. r7,0,r6
1831 bne 32b
1832 li r0,0
1833 stb r0,HSTATE_NAPPING(r13)
1834
4619ac88
PM
1835 /* Check the wake reason in SRR1 to see why we got here */
1836 mfspr r3, SPRN_SRR1
1837 rlwinm r3, r3, 44-31, 0x7 /* extract wake reason field */
1838 cmpwi r3, 4 /* was it an external interrupt? */
1839 li r12, BOOK3S_INTERRUPT_EXTERNAL
1840 mr r9, r4
1841 ld r10, VCPU_PC(r9)
1842 ld r11, VCPU_MSR(r9)
1843 beq do_ext_interrupt /* if so */
1844
19ccb76a
PM
1845 /* see if any other thread is already exiting */
1846 lwz r0,VCORE_ENTRY_EXIT(r5)
1847 cmpwi r0,0x100
1848 blt kvmppc_cede_reentry /* if not go back to guest */
1849
1850 /* some threads are exiting, so go to the guest exit path */
1851 b hcall_real_fallback
1852
1853 /* cede when already previously prodded case */
04f995a5
PM
1854kvm_cede_prodded:
1855 li r0,0
19ccb76a
PM
1856 stb r0,VCPU_PRODDED(r3)
1857 sync /* order testing prodded vs. clearing ceded */
1858 stb r0,VCPU_CEDED(r3)
1859 li r3,H_SUCCESS
1860 blr
1861
1862 /* we've ceded but we want to give control to the host */
04f995a5 1863kvm_cede_exit:
4619ac88 1864 b hcall_real_fallback
19ccb76a 1865
b4072df4
PM
1866 /* Try to handle a machine check in real mode */
1867machine_check_realmode:
1868 mr r3, r9 /* get vcpu pointer */
1869 bl .kvmppc_realmode_machine_check
1870 nop
1871 cmpdi r3, 0 /* continue exiting from guest? */
1872 ld r9, HSTATE_KVM_VCPU(r13)
1873 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK
1874 beq mc_cont
1875 /* If not, deliver a machine check. SRR0/1 are already set */
1876 li r10, BOOK3S_INTERRUPT_MACHINE_CHECK
1877 li r11, (MSR_ME << 1) | 1 /* synthesize MSR_SF | MSR_ME */
1878 rotldi r11, r11, 63
1879 b fast_interrupt_c_return
1880
c934243c
PM
1881/*
1882 * Determine what sort of external interrupt is pending (if any).
1883 * Returns:
1884 * 0 if no interrupt is pending
1885 * 1 if an interrupt is pending that needs to be handled by the host
1886 * -1 if there was a guest wakeup IPI (which has now been cleared)
1887 */
1888kvmppc_read_intr:
1889 /* see if a host IPI is pending */
1890 li r3, 1
1891 lbz r0, HSTATE_HOST_IPI(r13)
1892 cmpwi r0, 0
1893 bne 1f
371fefd6 1894
c934243c
PM
1895 /* Now read the interrupt from the ICP */
1896 ld r6, HSTATE_XICS_PHYS(r13)
19ccb76a 1897 li r7, XICS_XIRR
c934243c
PM
1898 cmpdi r6, 0
1899 beq- 1f
1900 lwzcix r0, r6, r7
1901 rlwinm. r3, r0, 0, 0xffffff
19ccb76a 1902 sync
c934243c 1903 beq 1f /* if nothing pending in the ICP */
371fefd6 1904
c934243c
PM
1905 /* We found something in the ICP...
1906 *
1907 * If it's not an IPI, stash it in the PACA and return to
1908 * the host, we don't (yet) handle directing real external
1909 * interrupts directly to the guest
1910 */
1911 cmpwi r3, XICS_IPI /* if there is, is it an IPI? */
1912 li r3, 1
1913 bne 42f
371fefd6 1914
c934243c
PM
1915 /* It's an IPI, clear the MFRR and EOI it */
1916 li r3, 0xff
1917 li r8, XICS_MFRR
1918 stbcix r3, r6, r8 /* clear the IPI */
1919 stwcix r0, r6, r7 /* EOI it */
1920 sync
f0888f70 1921
c934243c
PM
1922 /* We need to re-check host IPI now in case it got set in the
1923 * meantime. If it's clear, we bounce the interrupt to the
1924 * guest
1925 */
1926 lbz r0, HSTATE_HOST_IPI(r13)
1927 cmpwi r0, 0
1928 bne- 43f
1929
1930 /* OK, it's an IPI for us */
1931 li r3, -1
19321: blr
1933
193442: /* It's not an IPI and it's for the host, stash it in the PACA
1935 * before exit, it will be picked up by the host ICP driver
1936 */
1937 stw r0, HSTATE_SAVED_XIRR(r13)
1938 b 1b
1939
194043: /* We raced with the host, we need to resend that IPI, bummer */
1941 li r0, IPI_PRIORITY
1942 stbcix r0, r6, r8 /* set the IPI */
1943 sync
1944 b 1b
371fefd6 1945
de56a948
PM
1946/*
1947 * Save away FP, VMX and VSX registers.
1948 * r3 = vcpu pointer
595e4f7e
PM
1949 * N.B. r30 and r31 are volatile across this function,
1950 * thus it is not callable from C.
a8606e20 1951 */
595e4f7e
PM
1952kvmppc_save_fp:
1953 mflr r30
1954 mr r31,r3
8943633c
PM
1955 mfmsr r5
1956 ori r8,r5,MSR_FP
de56a948
PM
1957#ifdef CONFIG_ALTIVEC
1958BEGIN_FTR_SECTION
1959 oris r8,r8,MSR_VEC@h
1960END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1961#endif
1962#ifdef CONFIG_VSX
1963BEGIN_FTR_SECTION
1964 oris r8,r8,MSR_VSX@h
1965END_FTR_SECTION_IFSET(CPU_FTR_VSX)
1966#endif
1967 mtmsrd r8
1968 isync
595e4f7e
PM
1969 addi r3,r3,VCPU_FPRS
1970 bl .store_fp_state
de56a948
PM
1971#ifdef CONFIG_ALTIVEC
1972BEGIN_FTR_SECTION
595e4f7e
PM
1973 addi r3,r31,VCPU_VRS
1974 bl .store_vr_state
de56a948
PM
1975END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1976#endif
1977 mfspr r6,SPRN_VRSAVE
1978 stw r6,VCPU_VRSAVE(r3)
595e4f7e 1979 mtlr r30
8943633c 1980 mtmsrd r5
de56a948
PM
1981 isync
1982 blr
1983
1984/*
1985 * Load up FP, VMX and VSX registers
1986 * r4 = vcpu pointer
595e4f7e
PM
1987 * N.B. r30 and r31 are volatile across this function,
1988 * thus it is not callable from C.
de56a948 1989 */
de56a948 1990kvmppc_load_fp:
595e4f7e
PM
1991 mflr r30
1992 mr r31,r4
de56a948
PM
1993 mfmsr r9
1994 ori r8,r9,MSR_FP
1995#ifdef CONFIG_ALTIVEC
1996BEGIN_FTR_SECTION
1997 oris r8,r8,MSR_VEC@h
1998END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
1999#endif
2000#ifdef CONFIG_VSX
2001BEGIN_FTR_SECTION
2002 oris r8,r8,MSR_VSX@h
2003END_FTR_SECTION_IFSET(CPU_FTR_VSX)
2004#endif
2005 mtmsrd r8
2006 isync
595e4f7e
PM
2007 addi r3,r4,VCPU_FPRS
2008 bl .load_fp_state
de56a948
PM
2009#ifdef CONFIG_ALTIVEC
2010BEGIN_FTR_SECTION
595e4f7e
PM
2011 addi r3,r31,VCPU_VRS
2012 bl .load_vr_state
de56a948
PM
2013END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
2014#endif
2015 lwz r7,VCPU_VRSAVE(r4)
2016 mtspr SPRN_VRSAVE,r7
595e4f7e
PM
2017 mtlr r30
2018 mr r4,r31
de56a948 2019 blr
44a3add8
PM
2020
2021/*
2022 * We come here if we get any exception or interrupt while we are
2023 * executing host real mode code while in guest MMU context.
2024 * For now just spin, but we should do something better.
2025 */
2026kvmppc_bad_host_intr:
2027 b .
This page took 0.196248 seconds and 5 git commands to generate.