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/code-patching-asm.h> 22#include <asm/kvm_asm.h> 23#include <asm/reg.h> 24#include <asm/mmu.h> 25#include <asm/page.h> 26#include <asm/ptrace.h> 27#include <asm/hvcall.h> 28#include <asm/asm-offsets.h> 29#include <asm/exception-64s.h> 30#include <asm/kvm_book3s_asm.h> 31#include <asm/mmu-hash64.h> 32#include <asm/tm.h> 33 34#define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM) 35 36/* Values in HSTATE_NAPPING(r13) */ 37#define NAPPING_CEDE 1 38#define NAPPING_NOVCPU 2 39 40/* Stack frame offsets for kvmppc_hv_entry */ 41#define SFS 112 42#define STACK_SLOT_TRAP (SFS-4) 43#define STACK_SLOT_CIABR (SFS-16) 44#define STACK_SLOT_DAWR (SFS-24) 45#define STACK_SLOT_DAWRX (SFS-32) 46 47/* 48 * Call kvmppc_hv_entry in real mode. 49 * Must be called with interrupts hard-disabled. 50 * 51 * Input Registers: 52 * 53 * LR = return address to continue at after eventually re-enabling MMU 54 */ 55_GLOBAL_TOC(kvmppc_hv_entry_trampoline) 56 mflr r0 57 std r0, PPC_LR_STKOFF(r1) 58 stdu r1, -112(r1) 59 mfmsr r10 60 LOAD_REG_ADDR(r5, kvmppc_call_hv_entry) 61 li r0,MSR_RI 62 andc r0,r10,r0 63 li r6,MSR_IR | MSR_DR 64 andc r6,r10,r6 65 mtmsrd r0,1 /* clear RI in MSR */ 66 mtsrr0 r5 67 mtsrr1 r6 68 RFI_TO_KERNEL 69 70kvmppc_call_hv_entry: 71 ld r4, HSTATE_KVM_VCPU(r13) 72 bl kvmppc_hv_entry 73 74 /* Back from guest - restore host state and return to caller */ 75 76BEGIN_FTR_SECTION 77 /* Restore host DABR and DABRX */ 78 ld r5,HSTATE_DABR(r13) 79 li r6,7 80 mtspr SPRN_DABR,r5 81 mtspr SPRN_DABRX,r6 82END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) 83 84 /* Restore SPRG3 */ 85 ld r3,PACA_SPRG_VDSO(r13) 86 mtspr SPRN_SPRG_VDSO_WRITE,r3 87 88 /* Reload the host's PMU registers */ 89 ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */ 90 lbz r4, LPPACA_PMCINUSE(r3) 91 cmpwi r4, 0 92 beq 23f /* skip if not */ 93BEGIN_FTR_SECTION 94 ld r3, HSTATE_MMCR0(r13) 95 andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO 96 cmpwi r4, MMCR0_PMAO 97 beql kvmppc_fix_pmao 98END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG) 99 lwz r3, HSTATE_PMC1(r13) 100 lwz r4, HSTATE_PMC2(r13) 101 lwz r5, HSTATE_PMC3(r13) 102 lwz r6, HSTATE_PMC4(r13) 103 lwz r8, HSTATE_PMC5(r13) 104 lwz r9, HSTATE_PMC6(r13) 105 mtspr SPRN_PMC1, r3 106 mtspr SPRN_PMC2, r4 107 mtspr SPRN_PMC3, r5 108 mtspr SPRN_PMC4, r6 109 mtspr SPRN_PMC5, r8 110 mtspr SPRN_PMC6, r9 111 ld r3, HSTATE_MMCR0(r13) 112 ld r4, HSTATE_MMCR1(r13) 113 ld r5, HSTATE_MMCRA(r13) 114 ld r6, HSTATE_SIAR(r13) 115 ld r7, HSTATE_SDAR(r13) 116 mtspr SPRN_MMCR1, r4 117 mtspr SPRN_MMCRA, r5 118 mtspr SPRN_SIAR, r6 119 mtspr SPRN_SDAR, r7 120BEGIN_FTR_SECTION 121 ld r8, HSTATE_MMCR2(r13) 122 ld r9, HSTATE_SIER(r13) 123 mtspr SPRN_MMCR2, r8 124 mtspr SPRN_SIER, r9 125END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 126 mtspr SPRN_MMCR0, r3 127 isync 12823: 129 130 /* 131 * Reload DEC. HDEC interrupts were disabled when 132 * we reloaded the host's LPCR value. 133 */ 134 ld r3, HSTATE_DECEXP(r13) 135 mftb r4 136 subf r4, r4, r3 137 mtspr SPRN_DEC, r4 138 139 /* hwthread_req may have got set by cede or no vcpu, so clear it */ 140 li r0, 0 141 stb r0, HSTATE_HWTHREAD_REQ(r13) 142 143 /* 144 * For external and machine check interrupts, we need 145 * to call the Linux handler to process the interrupt. 146 * We do that by jumping to absolute address 0x500 for 147 * external interrupts, or the machine_check_fwnmi label 148 * for machine checks (since firmware might have patched 149 * the vector area at 0x200). The [h]rfid at the end of the 150 * handler will return to the book3s_hv_interrupts.S code. 151 * For other interrupts we do the rfid to get back 152 * to the book3s_hv_interrupts.S code here. 153 */ 154 ld r8, 112+PPC_LR_STKOFF(r1) 155 addi r1, r1, 112 156 ld r7, HSTATE_HOST_MSR(r13) 157 158 cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK 159 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL 160 beq 11f 161 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL 162 beq 15f /* Invoke the H_DOORBELL handler */ 163 cmpwi cr2, r12, BOOK3S_INTERRUPT_HMI 164 beq cr2, 14f /* HMI check */ 165 166 /* RFI into the highmem handler, or branch to interrupt handler */ 167 mfmsr r6 168 li r0, MSR_RI 169 andc r6, r6, r0 170 mtmsrd r6, 1 /* Clear RI in MSR */ 171 mtsrr0 r8 172 mtsrr1 r7 173 beq cr1, 13f /* machine check */ 174 RFI_TO_KERNEL 175 176 /* On POWER7, we have external interrupts set to use HSRR0/1 */ 17711: mtspr SPRN_HSRR0, r8 178 mtspr SPRN_HSRR1, r7 179 ba 0x500 180 18113: b machine_check_fwnmi 182 18314: mtspr SPRN_HSRR0, r8 184 mtspr SPRN_HSRR1, r7 185 b hmi_exception_after_realmode 186 18715: mtspr SPRN_HSRR0, r8 188 mtspr SPRN_HSRR1, r7 189 ba 0xe80 190 191kvmppc_primary_no_guest: 192 /* We handle this much like a ceded vcpu */ 193 /* put the HDEC into the DEC, since HDEC interrupts don't wake us */ 194 mfspr r3, SPRN_HDEC 195 mtspr SPRN_DEC, r3 196 /* 197 * Make sure the primary has finished the MMU switch. 198 * We should never get here on a secondary thread, but 199 * check it for robustness' sake. 200 */ 201 ld r5, HSTATE_KVM_VCORE(r13) 20265: lbz r0, VCORE_IN_GUEST(r5) 203 cmpwi r0, 0 204 beq 65b 205 /* Set LPCR. */ 206 ld r8,VCORE_LPCR(r5) 207 mtspr SPRN_LPCR,r8 208 isync 209 /* set our bit in napping_threads */ 210 ld r5, HSTATE_KVM_VCORE(r13) 211 lbz r7, HSTATE_PTID(r13) 212 li r0, 1 213 sld r0, r0, r7 214 addi r6, r5, VCORE_NAPPING_THREADS 2151: lwarx r3, 0, r6 216 or r3, r3, r0 217 stwcx. r3, 0, r6 218 bne 1b 219 /* order napping_threads update vs testing entry_exit_map */ 220 isync 221 li r12, 0 222 lwz r7, VCORE_ENTRY_EXIT(r5) 223 cmpwi r7, 0x100 224 bge kvm_novcpu_exit /* another thread already exiting */ 225 li r3, NAPPING_NOVCPU 226 stb r3, HSTATE_NAPPING(r13) 227 228 li r3, 0 /* Don't wake on privileged (OS) doorbell */ 229 b kvm_do_nap 230 231kvm_novcpu_wakeup: 232 ld r1, HSTATE_HOST_R1(r13) 233 ld r5, HSTATE_KVM_VCORE(r13) 234 li r0, 0 235 stb r0, HSTATE_NAPPING(r13) 236 237 /* check the wake reason */ 238 bl kvmppc_check_wake_reason 239 240 /* see if any other thread is already exiting */ 241 lwz r0, VCORE_ENTRY_EXIT(r5) 242 cmpwi r0, 0x100 243 bge kvm_novcpu_exit 244 245 /* clear our bit in napping_threads */ 246 lbz r7, HSTATE_PTID(r13) 247 li r0, 1 248 sld r0, r0, r7 249 addi r6, r5, VCORE_NAPPING_THREADS 2504: lwarx r7, 0, r6 251 andc r7, r7, r0 252 stwcx. r7, 0, r6 253 bne 4b 254 255 /* See if the wake reason means we need to exit */ 256 cmpdi r3, 0 257 bge kvm_novcpu_exit 258 259 /* See if our timeslice has expired (HDEC is negative) */ 260 mfspr r0, SPRN_HDEC 261 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER 262 cmpwi r0, 0 263 blt kvm_novcpu_exit 264 265 /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */ 266 ld r4, HSTATE_KVM_VCPU(r13) 267 cmpdi r4, 0 268 beq kvmppc_primary_no_guest 269 270#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 271 addi r3, r4, VCPU_TB_RMENTRY 272 bl kvmhv_start_timing 273#endif 274 b kvmppc_got_guest 275 276kvm_novcpu_exit: 277#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 278 ld r4, HSTATE_KVM_VCPU(r13) 279 cmpdi r4, 0 280 beq 13f 281 addi r3, r4, VCPU_TB_RMEXIT 282 bl kvmhv_accumulate_time 283#endif 28413: mr r3, r12 285 stw r12, STACK_SLOT_TRAP(r1) 286 bl kvmhv_commence_exit 287 nop 288 lwz r12, STACK_SLOT_TRAP(r1) 289 b kvmhv_switch_to_host 290 291/* 292 * We come in here when wakened from nap mode. 293 * Relocation is off and most register values are lost. 294 * r13 points to the PACA. 295 */ 296 .globl kvm_start_guest 297kvm_start_guest: 298 299 /* Set runlatch bit the minute you wake up from nap */ 300 mfspr r0, SPRN_CTRLF 301 ori r0, r0, 1 302 mtspr SPRN_CTRLT, r0 303 304 ld r2,PACATOC(r13) 305 306 li r0,KVM_HWTHREAD_IN_KVM 307 stb r0,HSTATE_HWTHREAD_STATE(r13) 308 309 /* NV GPR values from power7_idle() will no longer be valid */ 310 li r0,1 311 stb r0,PACA_NAPSTATELOST(r13) 312 313 /* were we napping due to cede? */ 314 lbz r0,HSTATE_NAPPING(r13) 315 cmpwi r0,NAPPING_CEDE 316 beq kvm_end_cede 317 cmpwi r0,NAPPING_NOVCPU 318 beq kvm_novcpu_wakeup 319 320 ld r1,PACAEMERGSP(r13) 321 subi r1,r1,STACK_FRAME_OVERHEAD 322 323 /* 324 * We weren't napping due to cede, so this must be a secondary 325 * thread being woken up to run a guest, or being woken up due 326 * to a stray IPI. (Or due to some machine check or hypervisor 327 * maintenance interrupt while the core is in KVM.) 328 */ 329 330 /* Check the wake reason in SRR1 to see why we got here */ 331 bl kvmppc_check_wake_reason 332 cmpdi r3, 0 333 bge kvm_no_guest 334 335 /* get vcore pointer, NULL if we have nothing to run */ 336 ld r5,HSTATE_KVM_VCORE(r13) 337 cmpdi r5,0 338 /* if we have no vcore to run, go back to sleep */ 339 beq kvm_no_guest 340 341kvm_secondary_got_guest: 342 343 /* Set HSTATE_DSCR(r13) to something sensible */ 344 ld r6, PACA_DSCR_DEFAULT(r13) 345 std r6, HSTATE_DSCR(r13) 346 347 /* On thread 0 of a subcore, set HDEC to max */ 348 lbz r4, HSTATE_PTID(r13) 349 cmpwi r4, 0 350 bne 63f 351 lis r6, 0x7fff 352 ori r6, r6, 0xffff 353 mtspr SPRN_HDEC, r6 354 /* and set per-LPAR registers, if doing dynamic micro-threading */ 355 ld r6, HSTATE_SPLIT_MODE(r13) 356 cmpdi r6, 0 357 beq 63f 358 ld r0, KVM_SPLIT_RPR(r6) 359 mtspr SPRN_RPR, r0 360 ld r0, KVM_SPLIT_PMMAR(r6) 361 mtspr SPRN_PMMAR, r0 362 ld r0, KVM_SPLIT_LDBAR(r6) 363 mtspr SPRN_LDBAR, r0 364 isync 36563: 366 /* Order load of vcpu after load of vcore */ 367 lwsync 368 ld r4, HSTATE_KVM_VCPU(r13) 369 bl kvmppc_hv_entry 370 371 /* Back from the guest, go back to nap */ 372 /* Clear our vcpu and vcore pointers so we don't come back in early */ 373 li r0, 0 374 std r0, HSTATE_KVM_VCPU(r13) 375 /* 376 * Once we clear HSTATE_KVM_VCORE(r13), the code in 377 * kvmppc_run_core() is going to assume that all our vcpu 378 * state is visible in memory. This lwsync makes sure 379 * that that is true. 380 */ 381 lwsync 382 std r0, HSTATE_KVM_VCORE(r13) 383 384/* 385 * At this point we have finished executing in the guest. 386 * We need to wait for hwthread_req to become zero, since 387 * we may not turn on the MMU while hwthread_req is non-zero. 388 * While waiting we also need to check if we get given a vcpu to run. 389 */ 390kvm_no_guest: 391 lbz r3, HSTATE_HWTHREAD_REQ(r13) 392 cmpwi r3, 0 393 bne 53f 394 HMT_MEDIUM 395 li r0, KVM_HWTHREAD_IN_KERNEL 396 stb r0, HSTATE_HWTHREAD_STATE(r13) 397 /* need to recheck hwthread_req after a barrier, to avoid race */ 398 sync 399 lbz r3, HSTATE_HWTHREAD_REQ(r13) 400 cmpwi r3, 0 401 bne 54f 402/* 403 * We jump to power7_wakeup_loss, which will return to the caller 404 * of power7_nap in the powernv cpu offline loop. The value we 405 * put in r3 becomes the return value for power7_nap. 406 */ 407 li r3, LPCR_PECE0 408 mfspr r4, SPRN_LPCR 409 rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1 410 mtspr SPRN_LPCR, r4 411 li r3, 0 412 b power7_wakeup_loss 413 41453: HMT_LOW 415 ld r5, HSTATE_KVM_VCORE(r13) 416 cmpdi r5, 0 417 bne 60f 418 ld r3, HSTATE_SPLIT_MODE(r13) 419 cmpdi r3, 0 420 beq kvm_no_guest 421 lbz r0, KVM_SPLIT_DO_NAP(r3) 422 cmpwi r0, 0 423 beq kvm_no_guest 424 HMT_MEDIUM 425 b kvm_unsplit_nap 42660: HMT_MEDIUM 427 b kvm_secondary_got_guest 428 42954: li r0, KVM_HWTHREAD_IN_KVM 430 stb r0, HSTATE_HWTHREAD_STATE(r13) 431 b kvm_no_guest 432 433/* 434 * Here the primary thread is trying to return the core to 435 * whole-core mode, so we need to nap. 436 */ 437kvm_unsplit_nap: 438 /* 439 * Ensure that secondary doesn't nap when it has 440 * its vcore pointer set. 441 */ 442 sync /* matches smp_mb() before setting split_info.do_nap */ 443 ld r0, HSTATE_KVM_VCORE(r13) 444 cmpdi r0, 0 445 bne kvm_no_guest 446 /* clear any pending message */ 447BEGIN_FTR_SECTION 448 lis r6, (PPC_DBELL_SERVER << (63-36))@h 449 PPC_MSGCLR(6) 450END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 451 /* Set kvm_split_mode.napped[tid] = 1 */ 452 ld r3, HSTATE_SPLIT_MODE(r13) 453 li r0, 1 454 lhz r4, PACAPACAINDEX(r13) 455 clrldi r4, r4, 61 /* micro-threading => P8 => 8 threads/core */ 456 addi r4, r4, KVM_SPLIT_NAPPED 457 stbx r0, r3, r4 458 /* Check the do_nap flag again after setting napped[] */ 459 sync 460 lbz r0, KVM_SPLIT_DO_NAP(r3) 461 cmpwi r0, 0 462 beq 57f 463 li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4 464 mfspr r4, SPRN_LPCR 465 rlwimi r4, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1) 466 mtspr SPRN_LPCR, r4 467 isync 468 std r0, HSTATE_SCRATCH0(r13) 469 ptesync 470 ld r0, HSTATE_SCRATCH0(r13) 4711: cmpd r0, r0 472 bne 1b 473 nap 474 b . 475 47657: li r0, 0 477 stbx r0, r3, r4 478 b kvm_no_guest 479 480/****************************************************************************** 481 * * 482 * Entry code * 483 * * 484 *****************************************************************************/ 485 486.global kvmppc_hv_entry 487kvmppc_hv_entry: 488 489 /* Required state: 490 * 491 * R4 = vcpu pointer (or NULL) 492 * MSR = ~IR|DR 493 * R13 = PACA 494 * R1 = host R1 495 * R2 = TOC 496 * all other volatile GPRS = free 497 */ 498 mflr r0 499 std r0, PPC_LR_STKOFF(r1) 500 stdu r1, -SFS(r1) 501 502 /* Save R1 in the PACA */ 503 std r1, HSTATE_HOST_R1(r13) 504 505 li r6, KVM_GUEST_MODE_HOST_HV 506 stb r6, HSTATE_IN_GUEST(r13) 507 508#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 509 /* Store initial timestamp */ 510 cmpdi r4, 0 511 beq 1f 512 addi r3, r4, VCPU_TB_RMENTRY 513 bl kvmhv_start_timing 5141: 515#endif 516 /* Clear out SLB */ 517 li r6,0 518 slbmte r6,r6 519 slbia 520 ptesync 521 522 /* 523 * POWER7/POWER8 host -> guest partition switch code. 524 * We don't have to lock against concurrent tlbies, 525 * but we do have to coordinate across hardware threads. 526 */ 527 /* Set bit in entry map iff exit map is zero. */ 528 ld r5, HSTATE_KVM_VCORE(r13) 529 li r7, 1 530 lbz r6, HSTATE_PTID(r13) 531 sld r7, r7, r6 532 addi r9, r5, VCORE_ENTRY_EXIT 53321: lwarx r3, 0, r9 534 cmpwi r3, 0x100 /* any threads starting to exit? */ 535 bge secondary_too_late /* if so we're too late to the party */ 536 or r3, r3, r7 537 stwcx. r3, 0, r9 538 bne 21b 539 540 /* Primary thread switches to guest partition. */ 541 ld r9,VCORE_KVM(r5) /* pointer to struct kvm */ 542 cmpwi r6,0 543 bne 10f 544 ld r6,KVM_SDR1(r9) 545 lwz r7,KVM_LPID(r9) 546 li r0,LPID_RSVD /* switch to reserved LPID */ 547 mtspr SPRN_LPID,r0 548 ptesync 549 mtspr SPRN_SDR1,r6 /* switch to partition page table */ 550 mtspr SPRN_LPID,r7 551 isync 552 553 /* See if we need to flush the TLB */ 554 lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */ 555 clrldi r7,r6,64-6 /* extract bit number (6 bits) */ 556 srdi r6,r6,6 /* doubleword number */ 557 sldi r6,r6,3 /* address offset */ 558 add r6,r6,r9 559 addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */ 560 li r0,1 561 sld r0,r0,r7 562 ld r7,0(r6) 563 and. r7,r7,r0 564 beq 22f 56523: ldarx r7,0,r6 /* if set, clear the bit */ 566 andc r7,r7,r0 567 stdcx. r7,0,r6 568 bne 23b 569 /* Flush the TLB of any entries for this LPID */ 570 /* use arch 2.07S as a proxy for POWER8 */ 571BEGIN_FTR_SECTION 572 li r6,512 /* POWER8 has 512 sets */ 573FTR_SECTION_ELSE 574 li r6,128 /* POWER7 has 128 sets */ 575ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S) 576 mtctr r6 577 li r7,0x800 /* IS field = 0b10 */ 578 ptesync 57928: tlbiel r7 580 addi r7,r7,0x1000 581 bdnz 28b 582 ptesync 583 584 /* Add timebase offset onto timebase */ 58522: ld r8,VCORE_TB_OFFSET(r5) 586 cmpdi r8,0 587 beq 37f 588 mftb r6 /* current host timebase */ 589 add r8,r8,r6 590 mtspr SPRN_TBU40,r8 /* update upper 40 bits */ 591 mftb r7 /* check if lower 24 bits overflowed */ 592 clrldi r6,r6,40 593 clrldi r7,r7,40 594 cmpld r7,r6 595 bge 37f 596 addis r8,r8,0x100 /* if so, increment upper 40 bits */ 597 mtspr SPRN_TBU40,r8 598 599 /* Load guest PCR value to select appropriate compat mode */ 60037: ld r7, VCORE_PCR(r5) 601 cmpdi r7, 0 602 beq 38f 603 mtspr SPRN_PCR, r7 60438: 605 606BEGIN_FTR_SECTION 607 /* DPDES is shared between threads */ 608 ld r8, VCORE_DPDES(r5) 609 mtspr SPRN_DPDES, r8 610END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 611 612 li r0,1 613 stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */ 614 615 /* Do we have a guest vcpu to run? */ 61610: cmpdi r4, 0 617 beq kvmppc_primary_no_guest 618kvmppc_got_guest: 619 620 /* Load up guest SLB entries */ 621 lwz r5,VCPU_SLB_MAX(r4) 622 cmpwi r5,0 623 beq 9f 624 mtctr r5 625 addi r6,r4,VCPU_SLB 6261: ld r8,VCPU_SLB_E(r6) 627 ld r9,VCPU_SLB_V(r6) 628 slbmte r9,r8 629 addi r6,r6,VCPU_SLB_SIZE 630 bdnz 1b 6319: 632 /* Increment yield count if they have a VPA */ 633 ld r3, VCPU_VPA(r4) 634 cmpdi r3, 0 635 beq 25f 636 li r6, LPPACA_YIELDCOUNT 637 LWZX_BE r5, r3, r6 638 addi r5, r5, 1 639 STWX_BE r5, r3, r6 640 li r6, 1 641 stb r6, VCPU_VPA_DIRTY(r4) 64225: 643 644 /* Save purr/spurr */ 645 mfspr r5,SPRN_PURR 646 mfspr r6,SPRN_SPURR 647 std r5,HSTATE_PURR(r13) 648 std r6,HSTATE_SPURR(r13) 649 ld r7,VCPU_PURR(r4) 650 ld r8,VCPU_SPURR(r4) 651 mtspr SPRN_PURR,r7 652 mtspr SPRN_SPURR,r8 653 654 /* Save host values of some registers */ 655BEGIN_FTR_SECTION 656 mfspr r5, SPRN_CIABR 657 mfspr r6, SPRN_DAWR 658 mfspr r7, SPRN_DAWRX 659 std r5, STACK_SLOT_CIABR(r1) 660 std r6, STACK_SLOT_DAWR(r1) 661 std r7, STACK_SLOT_DAWRX(r1) 662END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 663 664BEGIN_FTR_SECTION 665 /* Set partition DABR */ 666 /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */ 667 lwz r5,VCPU_DABRX(r4) 668 ld r6,VCPU_DABR(r4) 669 mtspr SPRN_DABRX,r5 670 mtspr SPRN_DABR,r6 671 isync 672END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) 673 674#ifdef CONFIG_PPC_TRANSACTIONAL_MEM 675BEGIN_FTR_SECTION 676 bl kvmppc_restore_tm 677END_FTR_SECTION_IFSET(CPU_FTR_TM) 678#endif 679 680 /* Load guest PMU registers */ 681 /* R4 is live here (vcpu pointer) */ 682 li r3, 1 683 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */ 684 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */ 685 isync 686BEGIN_FTR_SECTION 687 ld r3, VCPU_MMCR(r4) 688 andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO 689 cmpwi r5, MMCR0_PMAO 690 beql kvmppc_fix_pmao 691END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG) 692 lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */ 693 lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */ 694 lwz r6, VCPU_PMC + 8(r4) 695 lwz r7, VCPU_PMC + 12(r4) 696 lwz r8, VCPU_PMC + 16(r4) 697 lwz r9, VCPU_PMC + 20(r4) 698 mtspr SPRN_PMC1, r3 699 mtspr SPRN_PMC2, r5 700 mtspr SPRN_PMC3, r6 701 mtspr SPRN_PMC4, r7 702 mtspr SPRN_PMC5, r8 703 mtspr SPRN_PMC6, r9 704 ld r3, VCPU_MMCR(r4) 705 ld r5, VCPU_MMCR + 8(r4) 706 ld r6, VCPU_MMCR + 16(r4) 707 ld r7, VCPU_SIAR(r4) 708 ld r8, VCPU_SDAR(r4) 709 mtspr SPRN_MMCR1, r5 710 mtspr SPRN_MMCRA, r6 711 mtspr SPRN_SIAR, r7 712 mtspr SPRN_SDAR, r8 713BEGIN_FTR_SECTION 714 ld r5, VCPU_MMCR + 24(r4) 715 ld r6, VCPU_SIER(r4) 716 lwz r7, VCPU_PMC + 24(r4) 717 lwz r8, VCPU_PMC + 28(r4) 718 ld r9, VCPU_MMCR + 32(r4) 719 mtspr SPRN_MMCR2, r5 720 mtspr SPRN_SIER, r6 721 mtspr SPRN_SPMC1, r7 722 mtspr SPRN_SPMC2, r8 723 mtspr SPRN_MMCRS, r9 724END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 725 mtspr SPRN_MMCR0, r3 726 isync 727 728 /* Load up FP, VMX and VSX registers */ 729 bl kvmppc_load_fp 730 731 ld r14, VCPU_GPR(R14)(r4) 732 ld r15, VCPU_GPR(R15)(r4) 733 ld r16, VCPU_GPR(R16)(r4) 734 ld r17, VCPU_GPR(R17)(r4) 735 ld r18, VCPU_GPR(R18)(r4) 736 ld r19, VCPU_GPR(R19)(r4) 737 ld r20, VCPU_GPR(R20)(r4) 738 ld r21, VCPU_GPR(R21)(r4) 739 ld r22, VCPU_GPR(R22)(r4) 740 ld r23, VCPU_GPR(R23)(r4) 741 ld r24, VCPU_GPR(R24)(r4) 742 ld r25, VCPU_GPR(R25)(r4) 743 ld r26, VCPU_GPR(R26)(r4) 744 ld r27, VCPU_GPR(R27)(r4) 745 ld r28, VCPU_GPR(R28)(r4) 746 ld r29, VCPU_GPR(R29)(r4) 747 ld r30, VCPU_GPR(R30)(r4) 748 ld r31, VCPU_GPR(R31)(r4) 749 750 /* Switch DSCR to guest value */ 751 ld r5, VCPU_DSCR(r4) 752 mtspr SPRN_DSCR, r5 753 754BEGIN_FTR_SECTION 755 /* Skip next section on POWER7 */ 756 b 8f 757END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) 758 /* Load up POWER8-specific registers */ 759 ld r5, VCPU_IAMR(r4) 760 lwz r6, VCPU_PSPB(r4) 761 ld r7, VCPU_FSCR(r4) 762 mtspr SPRN_IAMR, r5 763 mtspr SPRN_PSPB, r6 764 mtspr SPRN_FSCR, r7 765 ld r5, VCPU_DAWR(r4) 766 ld r6, VCPU_DAWRX(r4) 767 ld r7, VCPU_CIABR(r4) 768 ld r8, VCPU_TAR(r4) 769 mtspr SPRN_DAWR, r5 770 mtspr SPRN_DAWRX, r6 771 mtspr SPRN_CIABR, r7 772 mtspr SPRN_TAR, r8 773 ld r5, VCPU_IC(r4) 774 ld r6, VCPU_VTB(r4) 775 mtspr SPRN_IC, r5 776 mtspr SPRN_VTB, r6 777 ld r8, VCPU_EBBHR(r4) 778 mtspr SPRN_EBBHR, r8 779 ld r5, VCPU_EBBRR(r4) 780 ld r6, VCPU_BESCR(r4) 781 ld r7, VCPU_CSIGR(r4) 782 ld r8, VCPU_TACR(r4) 783 mtspr SPRN_EBBRR, r5 784 mtspr SPRN_BESCR, r6 785 mtspr SPRN_CSIGR, r7 786 mtspr SPRN_TACR, r8 787 ld r5, VCPU_TCSCR(r4) 788 ld r6, VCPU_ACOP(r4) 789 lwz r7, VCPU_GUEST_PID(r4) 790 ld r8, VCPU_WORT(r4) 791 mtspr SPRN_TCSCR, r5 792 mtspr SPRN_ACOP, r6 793 mtspr SPRN_PID, r7 794 mtspr SPRN_WORT, r8 7958: 796 797 /* 798 * Set the decrementer to the guest decrementer. 799 */ 800 ld r8,VCPU_DEC_EXPIRES(r4) 801 /* r8 is a host timebase value here, convert to guest TB */ 802 ld r5,HSTATE_KVM_VCORE(r13) 803 ld r6,VCORE_TB_OFFSET(r5) 804 add r8,r8,r6 805 mftb r7 806 subf r3,r7,r8 807 mtspr SPRN_DEC,r3 808 stw r3,VCPU_DEC(r4) 809 810 ld r5, VCPU_SPRG0(r4) 811 ld r6, VCPU_SPRG1(r4) 812 ld r7, VCPU_SPRG2(r4) 813 ld r8, VCPU_SPRG3(r4) 814 mtspr SPRN_SPRG0, r5 815 mtspr SPRN_SPRG1, r6 816 mtspr SPRN_SPRG2, r7 817 mtspr SPRN_SPRG3, r8 818 819 /* Load up DAR and DSISR */ 820 ld r5, VCPU_DAR(r4) 821 lwz r6, VCPU_DSISR(r4) 822 mtspr SPRN_DAR, r5 823 mtspr SPRN_DSISR, r6 824 825 /* Restore AMR and UAMOR, set AMOR to all 1s */ 826 ld r5,VCPU_AMR(r4) 827 ld r6,VCPU_UAMOR(r4) 828 li r7,-1 829 mtspr SPRN_AMR,r5 830 mtspr SPRN_UAMOR,r6 831 mtspr SPRN_AMOR,r7 832 833 /* Restore state of CTRL run bit; assume 1 on entry */ 834 lwz r5,VCPU_CTRL(r4) 835 andi. r5,r5,1 836 bne 4f 837 mfspr r6,SPRN_CTRLF 838 clrrdi r6,r6,1 839 mtspr SPRN_CTRLT,r6 8404: 841 /* Secondary threads wait for primary to have done partition switch */ 842 ld r5, HSTATE_KVM_VCORE(r13) 843 lbz r6, HSTATE_PTID(r13) 844 cmpwi r6, 0 845 beq 21f 846 lbz r0, VCORE_IN_GUEST(r5) 847 cmpwi r0, 0 848 bne 21f 849 HMT_LOW 85020: lwz r3, VCORE_ENTRY_EXIT(r5) 851 cmpwi r3, 0x100 852 bge no_switch_exit 853 lbz r0, VCORE_IN_GUEST(r5) 854 cmpwi r0, 0 855 beq 20b 856 HMT_MEDIUM 85721: 858 /* Set LPCR. */ 859 ld r8,VCORE_LPCR(r5) 860 mtspr SPRN_LPCR,r8 861 isync 862 863 /* Check if HDEC expires soon */ 864 mfspr r3, SPRN_HDEC 865 cmpwi r3, 512 /* 1 microsecond */ 866 blt hdec_soon 867 868 ld r6, VCPU_CTR(r4) 869 ld r7, VCPU_XER(r4) 870 871 mtctr r6 872 mtxer r7 873 874kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */ 875 ld r10, VCPU_PC(r4) 876 ld r11, VCPU_MSR(r4) 877 ld r6, VCPU_SRR0(r4) 878 ld r7, VCPU_SRR1(r4) 879 mtspr SPRN_SRR0, r6 880 mtspr SPRN_SRR1, r7 881 882deliver_guest_interrupt: 883 /* r11 = vcpu->arch.msr & ~MSR_HV */ 884 rldicl r11, r11, 63 - MSR_HV_LG, 1 885 rotldi r11, r11, 1 + MSR_HV_LG 886 ori r11, r11, MSR_ME 887 888 /* Check if we can deliver an external or decrementer interrupt now */ 889 ld r0, VCPU_PENDING_EXC(r4) 890 rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63 891 cmpdi cr1, r0, 0 892 andi. r8, r11, MSR_EE 893 mfspr r8, SPRN_LPCR 894 /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */ 895 rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH 896 mtspr SPRN_LPCR, r8 897 isync 898 beq 5f 899 li r0, BOOK3S_INTERRUPT_EXTERNAL 900 bne cr1, 12f 901 mfspr r0, SPRN_DEC 902 cmpwi r0, 0 903 li r0, BOOK3S_INTERRUPT_DECREMENTER 904 bge 5f 905 90612: mtspr SPRN_SRR0, r10 907 mr r10,r0 908 mtspr SPRN_SRR1, r11 909 mr r9, r4 910 bl kvmppc_msr_interrupt 9115: 912 913/* 914 * Required state: 915 * R4 = vcpu 916 * R10: value for HSRR0 917 * R11: value for HSRR1 918 * R13 = PACA 919 */ 920fast_guest_return: 921 li r0,0 922 stb r0,VCPU_CEDED(r4) /* cancel cede */ 923 mtspr SPRN_HSRR0,r10 924 mtspr SPRN_HSRR1,r11 925 926 /* Activate guest mode, so faults get handled by KVM */ 927 li r9, KVM_GUEST_MODE_GUEST_HV 928 stb r9, HSTATE_IN_GUEST(r13) 929 930#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 931 /* Accumulate timing */ 932 addi r3, r4, VCPU_TB_GUEST 933 bl kvmhv_accumulate_time 934#endif 935 936 /* Enter guest */ 937 938BEGIN_FTR_SECTION 939 ld r5, VCPU_CFAR(r4) 940 mtspr SPRN_CFAR, r5 941END_FTR_SECTION_IFSET(CPU_FTR_CFAR) 942BEGIN_FTR_SECTION 943 ld r0, VCPU_PPR(r4) 944END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) 945 946 ld r5, VCPU_LR(r4) 947 lwz r6, VCPU_CR(r4) 948 mtlr r5 949 mtcr r6 950 951 ld r1, VCPU_GPR(R1)(r4) 952 ld r2, VCPU_GPR(R2)(r4) 953 ld r3, VCPU_GPR(R3)(r4) 954 ld r5, VCPU_GPR(R5)(r4) 955 ld r6, VCPU_GPR(R6)(r4) 956 ld r7, VCPU_GPR(R7)(r4) 957 ld r8, VCPU_GPR(R8)(r4) 958 ld r9, VCPU_GPR(R9)(r4) 959 ld r10, VCPU_GPR(R10)(r4) 960 ld r11, VCPU_GPR(R11)(r4) 961 ld r12, VCPU_GPR(R12)(r4) 962 ld r13, VCPU_GPR(R13)(r4) 963 964BEGIN_FTR_SECTION 965 mtspr SPRN_PPR, r0 966END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) 967 ld r0, VCPU_GPR(R0)(r4) 968 ld r4, VCPU_GPR(R4)(r4) 969 HRFI_TO_GUEST 970 b . 971 972secondary_too_late: 973 li r12, 0 974 cmpdi r4, 0 975 beq 11f 976 stw r12, VCPU_TRAP(r4) 977#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 978 addi r3, r4, VCPU_TB_RMEXIT 979 bl kvmhv_accumulate_time 980#endif 98111: b kvmhv_switch_to_host 982 983no_switch_exit: 984 HMT_MEDIUM 985 li r12, 0 986 b 12f 987hdec_soon: 988 li r12, BOOK3S_INTERRUPT_HV_DECREMENTER 98912: stw r12, VCPU_TRAP(r4) 990 mr r9, r4 991#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 992 addi r3, r4, VCPU_TB_RMEXIT 993 bl kvmhv_accumulate_time 994#endif 995 b guest_exit_cont 996 997/****************************************************************************** 998 * * 999 * Exit code * 1000 * * 1001 *****************************************************************************/ 1002 1003/* 1004 * We come here from the first-level interrupt handlers. 1005 */ 1006 .globl kvmppc_interrupt_hv 1007kvmppc_interrupt_hv: 1008 /* 1009 * Register contents: 1010 * R12 = interrupt vector 1011 * R13 = PACA 1012 * guest CR, R12 saved in shadow VCPU SCRATCH1/0 1013 * guest R13 saved in SPRN_SCRATCH0 1014 */ 1015 std r9, HSTATE_SCRATCH2(r13) 1016 1017 lbz r9, HSTATE_IN_GUEST(r13) 1018 cmpwi r9, KVM_GUEST_MODE_HOST_HV 1019 beq kvmppc_bad_host_intr 1020#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE 1021 cmpwi r9, KVM_GUEST_MODE_GUEST 1022 ld r9, HSTATE_SCRATCH2(r13) 1023 beq kvmppc_interrupt_pr 1024#endif 1025 /* We're now back in the host but in guest MMU context */ 1026 li r9, KVM_GUEST_MODE_HOST_HV 1027 stb r9, HSTATE_IN_GUEST(r13) 1028 1029 ld r9, HSTATE_KVM_VCPU(r13) 1030 1031 /* Save registers */ 1032 1033 std r0, VCPU_GPR(R0)(r9) 1034 std r1, VCPU_GPR(R1)(r9) 1035 std r2, VCPU_GPR(R2)(r9) 1036 std r3, VCPU_GPR(R3)(r9) 1037 std r4, VCPU_GPR(R4)(r9) 1038 std r5, VCPU_GPR(R5)(r9) 1039 std r6, VCPU_GPR(R6)(r9) 1040 std r7, VCPU_GPR(R7)(r9) 1041 std r8, VCPU_GPR(R8)(r9) 1042 ld r0, HSTATE_SCRATCH2(r13) 1043 std r0, VCPU_GPR(R9)(r9) 1044 std r10, VCPU_GPR(R10)(r9) 1045 std r11, VCPU_GPR(R11)(r9) 1046 ld r3, HSTATE_SCRATCH0(r13) 1047 lwz r4, HSTATE_SCRATCH1(r13) 1048 std r3, VCPU_GPR(R12)(r9) 1049 stw r4, VCPU_CR(r9) 1050BEGIN_FTR_SECTION 1051 ld r3, HSTATE_CFAR(r13) 1052 std r3, VCPU_CFAR(r9) 1053END_FTR_SECTION_IFSET(CPU_FTR_CFAR) 1054BEGIN_FTR_SECTION 1055 ld r4, HSTATE_PPR(r13) 1056 std r4, VCPU_PPR(r9) 1057END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) 1058 1059 /* Restore R1/R2 so we can handle faults */ 1060 ld r1, HSTATE_HOST_R1(r13) 1061 ld r2, PACATOC(r13) 1062 1063 mfspr r10, SPRN_SRR0 1064 mfspr r11, SPRN_SRR1 1065 std r10, VCPU_SRR0(r9) 1066 std r11, VCPU_SRR1(r9) 1067 andi. r0, r12, 2 /* need to read HSRR0/1? */ 1068 beq 1f 1069 mfspr r10, SPRN_HSRR0 1070 mfspr r11, SPRN_HSRR1 1071 clrrdi r12, r12, 2 10721: std r10, VCPU_PC(r9) 1073 std r11, VCPU_MSR(r9) 1074 1075 GET_SCRATCH0(r3) 1076 mflr r4 1077 std r3, VCPU_GPR(R13)(r9) 1078 std r4, VCPU_LR(r9) 1079 1080 stw r12,VCPU_TRAP(r9) 1081 1082#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 1083 addi r3, r9, VCPU_TB_RMINTR 1084 mr r4, r9 1085 bl kvmhv_accumulate_time 1086 ld r5, VCPU_GPR(R5)(r9) 1087 ld r6, VCPU_GPR(R6)(r9) 1088 ld r7, VCPU_GPR(R7)(r9) 1089 ld r8, VCPU_GPR(R8)(r9) 1090#endif 1091 1092 /* Save HEIR (HV emulation assist reg) in emul_inst 1093 if this is an HEI (HV emulation interrupt, e40) */ 1094 li r3,KVM_INST_FETCH_FAILED 1095 stw r3,VCPU_LAST_INST(r9) 1096 cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST 1097 bne 11f 1098 mfspr r3,SPRN_HEIR 109911: stw r3,VCPU_HEIR(r9) 1100 1101 /* these are volatile across C function calls */ 1102 mfctr r3 1103 mfxer r4 1104 std r3, VCPU_CTR(r9) 1105 std r4, VCPU_XER(r9) 1106 1107 /* If this is a page table miss then see if it's theirs or ours */ 1108 cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE 1109 beq kvmppc_hdsi 1110 cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE 1111 beq kvmppc_hisi 1112 1113 /* See if this is a leftover HDEC interrupt */ 1114 cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER 1115 bne 2f 1116 mfspr r3,SPRN_HDEC 1117 cmpwi r3,0 1118 mr r4,r9 1119 bge fast_guest_return 11202: 1121 /* See if this is an hcall we can handle in real mode */ 1122 cmpwi r12,BOOK3S_INTERRUPT_SYSCALL 1123 beq hcall_try_real_mode 1124 1125 /* Hypervisor doorbell - exit only if host IPI flag set */ 1126 cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL 1127 bne 3f 1128 lbz r0, HSTATE_HOST_IPI(r13) 1129 cmpwi r0, 0 1130 beq 4f 1131 b guest_exit_cont 11323: 1133 /* External interrupt ? */ 1134 cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL 1135 bne+ guest_exit_cont 1136 1137 /* External interrupt, first check for host_ipi. If this is 1138 * set, we know the host wants us out so let's do it now 1139 */ 1140 bl kvmppc_read_intr 1141 cmpdi r3, 0 1142 bgt guest_exit_cont 1143 1144 /* Check if any CPU is heading out to the host, if so head out too */ 11454: ld r5, HSTATE_KVM_VCORE(r13) 1146 lwz r0, VCORE_ENTRY_EXIT(r5) 1147 cmpwi r0, 0x100 1148 mr r4, r9 1149 blt deliver_guest_interrupt 1150 1151guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */ 1152 /* Save more register state */ 1153 mfdar r6 1154 mfdsisr r7 1155 std r6, VCPU_DAR(r9) 1156 stw r7, VCPU_DSISR(r9) 1157 /* don't overwrite fault_dar/fault_dsisr if HDSI */ 1158 cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE 1159 beq mc_cont 1160 std r6, VCPU_FAULT_DAR(r9) 1161 stw r7, VCPU_FAULT_DSISR(r9) 1162 1163 /* See if it is a machine check */ 1164 cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK 1165 beq machine_check_realmode 1166mc_cont: 1167#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 1168 addi r3, r9, VCPU_TB_RMEXIT 1169 mr r4, r9 1170 bl kvmhv_accumulate_time 1171#endif 1172 1173 /* Possibly flush the link stack here. */ 11741: nop 1175 patch_site 1b patch__call_kvm_flush_link_stack 1176 1177 mr r3, r12 1178 /* Increment exit count, poke other threads to exit */ 1179 bl kvmhv_commence_exit 1180 nop 1181 ld r9, HSTATE_KVM_VCPU(r13) 1182 lwz r12, VCPU_TRAP(r9) 1183 1184 /* Stop others sending VCPU interrupts to this physical CPU */ 1185 li r0, -1 1186 stw r0, VCPU_CPU(r9) 1187 stw r0, VCPU_THREAD_CPU(r9) 1188 1189 /* Save guest CTRL register, set runlatch to 1 */ 1190 mfspr r6,SPRN_CTRLF 1191 stw r6,VCPU_CTRL(r9) 1192 andi. r0,r6,1 1193 bne 4f 1194 ori r6,r6,1 1195 mtspr SPRN_CTRLT,r6 11964: 1197 /* Read the guest SLB and save it away */ 1198 lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */ 1199 mtctr r0 1200 li r6,0 1201 addi r7,r9,VCPU_SLB 1202 li r5,0 12031: slbmfee r8,r6 1204 andis. r0,r8,SLB_ESID_V@h 1205 beq 2f 1206 add r8,r8,r6 /* put index in */ 1207 slbmfev r3,r6 1208 std r8,VCPU_SLB_E(r7) 1209 std r3,VCPU_SLB_V(r7) 1210 addi r7,r7,VCPU_SLB_SIZE 1211 addi r5,r5,1 12122: addi r6,r6,1 1213 bdnz 1b 1214 stw r5,VCPU_SLB_MAX(r9) 1215 1216 /* 1217 * Save the guest PURR/SPURR 1218 */ 1219 mfspr r5,SPRN_PURR 1220 mfspr r6,SPRN_SPURR 1221 ld r7,VCPU_PURR(r9) 1222 ld r8,VCPU_SPURR(r9) 1223 std r5,VCPU_PURR(r9) 1224 std r6,VCPU_SPURR(r9) 1225 subf r5,r7,r5 1226 subf r6,r8,r6 1227 1228 /* 1229 * Restore host PURR/SPURR and add guest times 1230 * so that the time in the guest gets accounted. 1231 */ 1232 ld r3,HSTATE_PURR(r13) 1233 ld r4,HSTATE_SPURR(r13) 1234 add r3,r3,r5 1235 add r4,r4,r6 1236 mtspr SPRN_PURR,r3 1237 mtspr SPRN_SPURR,r4 1238 1239 /* Save DEC */ 1240 mfspr r5,SPRN_DEC 1241 mftb r6 1242 extsw r5,r5 1243 add r5,r5,r6 1244 /* r5 is a guest timebase value here, convert to host TB */ 1245 ld r3,HSTATE_KVM_VCORE(r13) 1246 ld r4,VCORE_TB_OFFSET(r3) 1247 subf r5,r4,r5 1248 std r5,VCPU_DEC_EXPIRES(r9) 1249 1250BEGIN_FTR_SECTION 1251 b 8f 1252END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) 1253 /* Save POWER8-specific registers */ 1254 mfspr r5, SPRN_IAMR 1255 mfspr r6, SPRN_PSPB 1256 mfspr r7, SPRN_FSCR 1257 std r5, VCPU_IAMR(r9) 1258 stw r6, VCPU_PSPB(r9) 1259 std r7, VCPU_FSCR(r9) 1260 mfspr r5, SPRN_IC 1261 mfspr r6, SPRN_VTB 1262 mfspr r7, SPRN_TAR 1263 std r5, VCPU_IC(r9) 1264 std r6, VCPU_VTB(r9) 1265 std r7, VCPU_TAR(r9) 1266 mfspr r8, SPRN_EBBHR 1267 std r8, VCPU_EBBHR(r9) 1268 mfspr r5, SPRN_EBBRR 1269 mfspr r6, SPRN_BESCR 1270 mfspr r7, SPRN_CSIGR 1271 mfspr r8, SPRN_TACR 1272 std r5, VCPU_EBBRR(r9) 1273 std r6, VCPU_BESCR(r9) 1274 std r7, VCPU_CSIGR(r9) 1275 std r8, VCPU_TACR(r9) 1276 mfspr r5, SPRN_TCSCR 1277 mfspr r6, SPRN_ACOP 1278 mfspr r7, SPRN_PID 1279 mfspr r8, SPRN_WORT 1280 std r5, VCPU_TCSCR(r9) 1281 std r6, VCPU_ACOP(r9) 1282 stw r7, VCPU_GUEST_PID(r9) 1283 std r8, VCPU_WORT(r9) 1284 /* 1285 * Restore various registers to 0, where non-zero values 1286 * set by the guest could disrupt the host. 1287 */ 1288 li r0, 0 1289 mtspr SPRN_IAMR, r0 1290 mtspr SPRN_PSPB, r0 1291 mtspr SPRN_TCSCR, r0 1292 mtspr SPRN_WORT, r0 1293 /* Set MMCRS to 1<<31 to freeze and disable the SPMC counters */ 1294 li r0, 1 1295 sldi r0, r0, 31 1296 mtspr SPRN_MMCRS, r0 12978: 1298 1299 /* Save and reset AMR and UAMOR before turning on the MMU */ 1300 mfspr r5,SPRN_AMR 1301 mfspr r6,SPRN_UAMOR 1302 std r5,VCPU_AMR(r9) 1303 std r6,VCPU_UAMOR(r9) 1304 li r6,0 1305 mtspr SPRN_AMR,r6 1306 mtspr SPRN_UAMOR, r6 1307 1308 /* Switch DSCR back to host value */ 1309 mfspr r8, SPRN_DSCR 1310 ld r7, HSTATE_DSCR(r13) 1311 std r8, VCPU_DSCR(r9) 1312 mtspr SPRN_DSCR, r7 1313 1314 /* Save non-volatile GPRs */ 1315 std r14, VCPU_GPR(R14)(r9) 1316 std r15, VCPU_GPR(R15)(r9) 1317 std r16, VCPU_GPR(R16)(r9) 1318 std r17, VCPU_GPR(R17)(r9) 1319 std r18, VCPU_GPR(R18)(r9) 1320 std r19, VCPU_GPR(R19)(r9) 1321 std r20, VCPU_GPR(R20)(r9) 1322 std r21, VCPU_GPR(R21)(r9) 1323 std r22, VCPU_GPR(R22)(r9) 1324 std r23, VCPU_GPR(R23)(r9) 1325 std r24, VCPU_GPR(R24)(r9) 1326 std r25, VCPU_GPR(R25)(r9) 1327 std r26, VCPU_GPR(R26)(r9) 1328 std r27, VCPU_GPR(R27)(r9) 1329 std r28, VCPU_GPR(R28)(r9) 1330 std r29, VCPU_GPR(R29)(r9) 1331 std r30, VCPU_GPR(R30)(r9) 1332 std r31, VCPU_GPR(R31)(r9) 1333 1334 /* Save SPRGs */ 1335 mfspr r3, SPRN_SPRG0 1336 mfspr r4, SPRN_SPRG1 1337 mfspr r5, SPRN_SPRG2 1338 mfspr r6, SPRN_SPRG3 1339 std r3, VCPU_SPRG0(r9) 1340 std r4, VCPU_SPRG1(r9) 1341 std r5, VCPU_SPRG2(r9) 1342 std r6, VCPU_SPRG3(r9) 1343 1344 /* save FP state */ 1345 mr r3, r9 1346 bl kvmppc_save_fp 1347 1348#ifdef CONFIG_PPC_TRANSACTIONAL_MEM 1349BEGIN_FTR_SECTION 1350 bl kvmppc_save_tm 1351END_FTR_SECTION_IFSET(CPU_FTR_TM) 1352#endif 1353 1354 /* Increment yield count if they have a VPA */ 1355 ld r8, VCPU_VPA(r9) /* do they have a VPA? */ 1356 cmpdi r8, 0 1357 beq 25f 1358 li r4, LPPACA_YIELDCOUNT 1359 LWZX_BE r3, r8, r4 1360 addi r3, r3, 1 1361 STWX_BE r3, r8, r4 1362 li r3, 1 1363 stb r3, VCPU_VPA_DIRTY(r9) 136425: 1365 /* Save PMU registers if requested */ 1366 /* r8 and cr0.eq are live here */ 1367BEGIN_FTR_SECTION 1368 /* 1369 * POWER8 seems to have a hardware bug where setting 1370 * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE] 1371 * when some counters are already negative doesn't seem 1372 * to cause a performance monitor alert (and hence interrupt). 1373 * The effect of this is that when saving the PMU state, 1374 * if there is no PMU alert pending when we read MMCR0 1375 * before freezing the counters, but one becomes pending 1376 * before we read the counters, we lose it. 1377 * To work around this, we need a way to freeze the counters 1378 * before reading MMCR0. Normally, freezing the counters 1379 * is done by writing MMCR0 (to set MMCR0[FC]) which 1380 * unavoidably writes MMCR0[PMA0] as well. On POWER8, 1381 * we can also freeze the counters using MMCR2, by writing 1382 * 1s to all the counter freeze condition bits (there are 1383 * 9 bits each for 6 counters). 1384 */ 1385 li r3, -1 /* set all freeze bits */ 1386 clrrdi r3, r3, 10 1387 mfspr r10, SPRN_MMCR2 1388 mtspr SPRN_MMCR2, r3 1389 isync 1390END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 1391 li r3, 1 1392 sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */ 1393 mfspr r4, SPRN_MMCR0 /* save MMCR0 */ 1394 mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */ 1395 mfspr r6, SPRN_MMCRA 1396 /* Clear MMCRA in order to disable SDAR updates */ 1397 li r7, 0 1398 mtspr SPRN_MMCRA, r7 1399 isync 1400 beq 21f /* if no VPA, save PMU stuff anyway */ 1401 lbz r7, LPPACA_PMCINUSE(r8) 1402 cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */ 1403 bne 21f 1404 std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */ 1405 b 22f 140621: mfspr r5, SPRN_MMCR1 1407 mfspr r7, SPRN_SIAR 1408 mfspr r8, SPRN_SDAR 1409 std r4, VCPU_MMCR(r9) 1410 std r5, VCPU_MMCR + 8(r9) 1411 std r6, VCPU_MMCR + 16(r9) 1412BEGIN_FTR_SECTION 1413 std r10, VCPU_MMCR + 24(r9) 1414END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 1415 std r7, VCPU_SIAR(r9) 1416 std r8, VCPU_SDAR(r9) 1417 mfspr r3, SPRN_PMC1 1418 mfspr r4, SPRN_PMC2 1419 mfspr r5, SPRN_PMC3 1420 mfspr r6, SPRN_PMC4 1421 mfspr r7, SPRN_PMC5 1422 mfspr r8, SPRN_PMC6 1423 stw r3, VCPU_PMC(r9) 1424 stw r4, VCPU_PMC + 4(r9) 1425 stw r5, VCPU_PMC + 8(r9) 1426 stw r6, VCPU_PMC + 12(r9) 1427 stw r7, VCPU_PMC + 16(r9) 1428 stw r8, VCPU_PMC + 20(r9) 1429BEGIN_FTR_SECTION 1430 mfspr r5, SPRN_SIER 1431 mfspr r6, SPRN_SPMC1 1432 mfspr r7, SPRN_SPMC2 1433 mfspr r8, SPRN_MMCRS 1434 std r5, VCPU_SIER(r9) 1435 stw r6, VCPU_PMC + 24(r9) 1436 stw r7, VCPU_PMC + 28(r9) 1437 std r8, VCPU_MMCR + 32(r9) 1438 lis r4, 0x8000 1439 mtspr SPRN_MMCRS, r4 1440END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 144122: 1442 /* Clear out SLB */ 1443 li r5,0 1444 slbmte r5,r5 1445 slbia 1446 ptesync 1447 1448 /* Restore host values of some registers */ 1449BEGIN_FTR_SECTION 1450 ld r5, STACK_SLOT_CIABR(r1) 1451 ld r6, STACK_SLOT_DAWR(r1) 1452 ld r7, STACK_SLOT_DAWRX(r1) 1453 mtspr SPRN_CIABR, r5 1454 mtspr SPRN_DAWR, r6 1455 mtspr SPRN_DAWRX, r7 1456END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 1457 1458 /* 1459 * POWER7/POWER8 guest -> host partition switch code. 1460 * We don't have to lock against tlbies but we do 1461 * have to coordinate the hardware threads. 1462 */ 1463kvmhv_switch_to_host: 1464 /* Secondary threads wait for primary to do partition switch */ 1465 ld r5,HSTATE_KVM_VCORE(r13) 1466 ld r4,VCORE_KVM(r5) /* pointer to struct kvm */ 1467 lbz r3,HSTATE_PTID(r13) 1468 cmpwi r3,0 1469 beq 15f 1470 HMT_LOW 147113: lbz r3,VCORE_IN_GUEST(r5) 1472 cmpwi r3,0 1473 bne 13b 1474 HMT_MEDIUM 1475 b 16f 1476 1477 /* Primary thread waits for all the secondaries to exit guest */ 147815: lwz r3,VCORE_ENTRY_EXIT(r5) 1479 rlwinm r0,r3,32-8,0xff 1480 clrldi r3,r3,56 1481 cmpw r3,r0 1482 bne 15b 1483 isync 1484 1485 /* Did we actually switch to the guest at all? */ 1486 lbz r6, VCORE_IN_GUEST(r5) 1487 cmpwi r6, 0 1488 beq 19f 1489 1490 /* Primary thread switches back to host partition */ 1491 ld r6,KVM_HOST_SDR1(r4) 1492 lwz r7,KVM_HOST_LPID(r4) 1493 li r8,LPID_RSVD /* switch to reserved LPID */ 1494 mtspr SPRN_LPID,r8 1495 ptesync 1496 mtspr SPRN_SDR1,r6 /* switch to partition page table */ 1497 mtspr SPRN_LPID,r7 1498 isync 1499 1500BEGIN_FTR_SECTION 1501 /* DPDES is shared between threads */ 1502 mfspr r7, SPRN_DPDES 1503 std r7, VCORE_DPDES(r5) 1504 /* clear DPDES so we don't get guest doorbells in the host */ 1505 li r8, 0 1506 mtspr SPRN_DPDES, r8 1507END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 1508 1509 /* Subtract timebase offset from timebase */ 1510 ld r8,VCORE_TB_OFFSET(r5) 1511 cmpdi r8,0 1512 beq 17f 1513 mftb r6 /* current guest timebase */ 1514 subf r8,r8,r6 1515 mtspr SPRN_TBU40,r8 /* update upper 40 bits */ 1516 mftb r7 /* check if lower 24 bits overflowed */ 1517 clrldi r6,r6,40 1518 clrldi r7,r7,40 1519 cmpld r7,r6 1520 bge 17f 1521 addis r8,r8,0x100 /* if so, increment upper 40 bits */ 1522 mtspr SPRN_TBU40,r8 1523 1524 /* Reset PCR */ 152517: ld r0, VCORE_PCR(r5) 1526 cmpdi r0, 0 1527 beq 18f 1528 li r0, 0 1529 mtspr SPRN_PCR, r0 153018: 1531 /* Signal secondary CPUs to continue */ 1532 stb r0,VCORE_IN_GUEST(r5) 153319: lis r8,0x7fff /* MAX_INT@h */ 1534 mtspr SPRN_HDEC,r8 1535 153616: ld r8,KVM_HOST_LPCR(r4) 1537 mtspr SPRN_LPCR,r8 1538 isync 1539 1540 /* load host SLB entries */ 1541 ld r8,PACA_SLBSHADOWPTR(r13) 1542 1543 .rept SLB_NUM_BOLTED 1544 li r3, SLBSHADOW_SAVEAREA 1545 LDX_BE r5, r8, r3 1546 addi r3, r3, 8 1547 LDX_BE r6, r8, r3 1548 andis. r7,r5,SLB_ESID_V@h 1549 beq 1f 1550 slbmte r6,r5 15511: addi r8,r8,16 1552 .endr 1553 1554#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 1555 /* Finish timing, if we have a vcpu */ 1556 ld r4, HSTATE_KVM_VCPU(r13) 1557 cmpdi r4, 0 1558 li r3, 0 1559 beq 2f 1560 bl kvmhv_accumulate_time 15612: 1562#endif 1563 /* Unset guest mode */ 1564 li r0, KVM_GUEST_MODE_NONE 1565 stb r0, HSTATE_IN_GUEST(r13) 1566 1567 ld r0, SFS+PPC_LR_STKOFF(r1) 1568 addi r1, r1, SFS 1569 mtlr r0 1570 blr 1571 1572.balign 32 1573.global kvm_flush_link_stack 1574kvm_flush_link_stack: 1575 /* Save LR into r0 */ 1576 mflr r0 1577 1578 /* Flush the link stack. On Power8 it's up to 32 entries in size. */ 1579 .rept 32 1580 bl .+4 1581 .endr 1582 1583 /* Restore LR */ 1584 mtlr r0 1585 blr 1586 1587/* 1588 * Check whether an HDSI is an HPTE not found fault or something else. 1589 * If it is an HPTE not found fault that is due to the guest accessing 1590 * a page that they have mapped but which we have paged out, then 1591 * we continue on with the guest exit path. In all other cases, 1592 * reflect the HDSI to the guest as a DSI. 1593 */ 1594kvmppc_hdsi: 1595 mfspr r4, SPRN_HDAR 1596 mfspr r6, SPRN_HDSISR 1597 /* HPTE not found fault or protection fault? */ 1598 andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h 1599 beq 1f /* if not, send it to the guest */ 1600 andi. r0, r11, MSR_DR /* data relocation enabled? */ 1601 beq 3f 1602 clrrdi r0, r4, 28 1603 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */ 1604 li r0, BOOK3S_INTERRUPT_DATA_SEGMENT 1605 bne 7f /* if no SLB entry found */ 16064: std r4, VCPU_FAULT_DAR(r9) 1607 stw r6, VCPU_FAULT_DSISR(r9) 1608 1609 /* Search the hash table. */ 1610 mr r3, r9 /* vcpu pointer */ 1611 li r7, 1 /* data fault */ 1612 bl kvmppc_hpte_hv_fault 1613 ld r9, HSTATE_KVM_VCPU(r13) 1614 ld r10, VCPU_PC(r9) 1615 ld r11, VCPU_MSR(r9) 1616 li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE 1617 cmpdi r3, 0 /* retry the instruction */ 1618 beq 6f 1619 cmpdi r3, -1 /* handle in kernel mode */ 1620 beq guest_exit_cont 1621 cmpdi r3, -2 /* MMIO emulation; need instr word */ 1622 beq 2f 1623 1624 /* Synthesize a DSI (or DSegI) for the guest */ 1625 ld r4, VCPU_FAULT_DAR(r9) 1626 mr r6, r3 16271: li r0, BOOK3S_INTERRUPT_DATA_STORAGE 1628 mtspr SPRN_DSISR, r6 16297: mtspr SPRN_DAR, r4 1630 mtspr SPRN_SRR0, r10 1631 mtspr SPRN_SRR1, r11 1632 mr r10, r0 1633 bl kvmppc_msr_interrupt 1634fast_interrupt_c_return: 16356: ld r7, VCPU_CTR(r9) 1636 ld r8, VCPU_XER(r9) 1637 mtctr r7 1638 mtxer r8 1639 mr r4, r9 1640 b fast_guest_return 1641 16423: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */ 1643 ld r5, KVM_VRMA_SLB_V(r5) 1644 b 4b 1645 1646 /* If this is for emulated MMIO, load the instruction word */ 16472: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */ 1648 1649 /* Set guest mode to 'jump over instruction' so if lwz faults 1650 * we'll just continue at the next IP. */ 1651 li r0, KVM_GUEST_MODE_SKIP 1652 stb r0, HSTATE_IN_GUEST(r13) 1653 1654 /* Do the access with MSR:DR enabled */ 1655 mfmsr r3 1656 ori r4, r3, MSR_DR /* Enable paging for data */ 1657 mtmsrd r4 1658 lwz r8, 0(r10) 1659 mtmsrd r3 1660 1661 /* Store the result */ 1662 stw r8, VCPU_LAST_INST(r9) 1663 1664 /* Unset guest mode. */ 1665 li r0, KVM_GUEST_MODE_HOST_HV 1666 stb r0, HSTATE_IN_GUEST(r13) 1667 b guest_exit_cont 1668 1669/* 1670 * Similarly for an HISI, reflect it to the guest as an ISI unless 1671 * it is an HPTE not found fault for a page that we have paged out. 1672 */ 1673kvmppc_hisi: 1674 andis. r0, r11, SRR1_ISI_NOPT@h 1675 beq 1f 1676 andi. r0, r11, MSR_IR /* instruction relocation enabled? */ 1677 beq 3f 1678 clrrdi r0, r10, 28 1679 PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */ 1680 li r0, BOOK3S_INTERRUPT_INST_SEGMENT 1681 bne 7f /* if no SLB entry found */ 16824: 1683 /* Search the hash table. */ 1684 mr r3, r9 /* vcpu pointer */ 1685 mr r4, r10 1686 mr r6, r11 1687 li r7, 0 /* instruction fault */ 1688 bl kvmppc_hpte_hv_fault 1689 ld r9, HSTATE_KVM_VCPU(r13) 1690 ld r10, VCPU_PC(r9) 1691 ld r11, VCPU_MSR(r9) 1692 li r12, BOOK3S_INTERRUPT_H_INST_STORAGE 1693 cmpdi r3, 0 /* retry the instruction */ 1694 beq fast_interrupt_c_return 1695 cmpdi r3, -1 /* handle in kernel mode */ 1696 beq guest_exit_cont 1697 1698 /* Synthesize an ISI (or ISegI) for the guest */ 1699 mr r11, r3 17001: li r0, BOOK3S_INTERRUPT_INST_STORAGE 17017: mtspr SPRN_SRR0, r10 1702 mtspr SPRN_SRR1, r11 1703 mr r10, r0 1704 bl kvmppc_msr_interrupt 1705 b fast_interrupt_c_return 1706 17073: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */ 1708 ld r5, KVM_VRMA_SLB_V(r6) 1709 b 4b 1710 1711/* 1712 * Try to handle an hcall in real mode. 1713 * Returns to the guest if we handle it, or continues on up to 1714 * the kernel if we can't (i.e. if we don't have a handler for 1715 * it, or if the handler returns H_TOO_HARD). 1716 * 1717 * r5 - r8 contain hcall args, 1718 * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca 1719 */ 1720hcall_try_real_mode: 1721 ld r3,VCPU_GPR(R3)(r9) 1722 andi. r0,r11,MSR_PR 1723 /* sc 1 from userspace - reflect to guest syscall */ 1724 bne sc_1_fast_return 1725 clrrdi r3,r3,2 1726 cmpldi r3,hcall_real_table_end - hcall_real_table 1727 bge guest_exit_cont 1728 /* See if this hcall is enabled for in-kernel handling */ 1729 ld r4, VCPU_KVM(r9) 1730 srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */ 1731 sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */ 1732 add r4, r4, r0 1733 ld r0, KVM_ENABLED_HCALLS(r4) 1734 rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */ 1735 srd r0, r0, r4 1736 andi. r0, r0, 1 1737 beq guest_exit_cont 1738 /* Get pointer to handler, if any, and call it */ 1739 LOAD_REG_ADDR(r4, hcall_real_table) 1740 lwax r3,r3,r4 1741 cmpwi r3,0 1742 beq guest_exit_cont 1743 add r12,r3,r4 1744 mtctr r12 1745 mr r3,r9 /* get vcpu pointer */ 1746 ld r4,VCPU_GPR(R4)(r9) 1747 bctrl 1748 cmpdi r3,H_TOO_HARD 1749 beq hcall_real_fallback 1750 ld r4,HSTATE_KVM_VCPU(r13) 1751 std r3,VCPU_GPR(R3)(r4) 1752 ld r10,VCPU_PC(r4) 1753 ld r11,VCPU_MSR(r4) 1754 b fast_guest_return 1755 1756sc_1_fast_return: 1757 mtspr SPRN_SRR0,r10 1758 mtspr SPRN_SRR1,r11 1759 li r10, BOOK3S_INTERRUPT_SYSCALL 1760 bl kvmppc_msr_interrupt 1761 mr r4,r9 1762 b fast_guest_return 1763 1764 /* We've attempted a real mode hcall, but it's punted it back 1765 * to userspace. We need to restore some clobbered volatiles 1766 * before resuming the pass-it-to-qemu path */ 1767hcall_real_fallback: 1768 li r12,BOOK3S_INTERRUPT_SYSCALL 1769 ld r9, HSTATE_KVM_VCPU(r13) 1770 1771 b guest_exit_cont 1772 1773 .globl hcall_real_table 1774hcall_real_table: 1775 .long 0 /* 0 - unused */ 1776 .long DOTSYM(kvmppc_h_remove) - hcall_real_table 1777 .long DOTSYM(kvmppc_h_enter) - hcall_real_table 1778 .long DOTSYM(kvmppc_h_read) - hcall_real_table 1779 .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table 1780 .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table 1781 .long DOTSYM(kvmppc_h_protect) - hcall_real_table 1782 .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table 1783 .long DOTSYM(kvmppc_h_put_tce) - hcall_real_table 1784 .long 0 /* 0x24 - H_SET_SPRG0 */ 1785 .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table 1786 .long 0 /* 0x2c */ 1787 .long 0 /* 0x30 */ 1788 .long 0 /* 0x34 */ 1789 .long 0 /* 0x38 */ 1790 .long 0 /* 0x3c */ 1791 .long 0 /* 0x40 */ 1792 .long 0 /* 0x44 */ 1793 .long 0 /* 0x48 */ 1794 .long 0 /* 0x4c */ 1795 .long 0 /* 0x50 */ 1796 .long 0 /* 0x54 */ 1797 .long 0 /* 0x58 */ 1798 .long 0 /* 0x5c */ 1799 .long 0 /* 0x60 */ 1800#ifdef CONFIG_KVM_XICS 1801 .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table 1802 .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table 1803 .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table 1804 .long 0 /* 0x70 - H_IPOLL */ 1805 .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table 1806#else 1807 .long 0 /* 0x64 - H_EOI */ 1808 .long 0 /* 0x68 - H_CPPR */ 1809 .long 0 /* 0x6c - H_IPI */ 1810 .long 0 /* 0x70 - H_IPOLL */ 1811 .long 0 /* 0x74 - H_XIRR */ 1812#endif 1813 .long 0 /* 0x78 */ 1814 .long 0 /* 0x7c */ 1815 .long 0 /* 0x80 */ 1816 .long 0 /* 0x84 */ 1817 .long 0 /* 0x88 */ 1818 .long 0 /* 0x8c */ 1819 .long 0 /* 0x90 */ 1820 .long 0 /* 0x94 */ 1821 .long 0 /* 0x98 */ 1822 .long 0 /* 0x9c */ 1823 .long 0 /* 0xa0 */ 1824 .long 0 /* 0xa4 */ 1825 .long 0 /* 0xa8 */ 1826 .long 0 /* 0xac */ 1827 .long 0 /* 0xb0 */ 1828 .long 0 /* 0xb4 */ 1829 .long 0 /* 0xb8 */ 1830 .long 0 /* 0xbc */ 1831 .long 0 /* 0xc0 */ 1832 .long 0 /* 0xc4 */ 1833 .long 0 /* 0xc8 */ 1834 .long 0 /* 0xcc */ 1835 .long 0 /* 0xd0 */ 1836 .long 0 /* 0xd4 */ 1837 .long 0 /* 0xd8 */ 1838 .long 0 /* 0xdc */ 1839 .long DOTSYM(kvmppc_h_cede) - hcall_real_table 1840 .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table 1841 .long 0 /* 0xe8 */ 1842 .long 0 /* 0xec */ 1843 .long 0 /* 0xf0 */ 1844 .long 0 /* 0xf4 */ 1845 .long 0 /* 0xf8 */ 1846 .long 0 /* 0xfc */ 1847 .long 0 /* 0x100 */ 1848 .long 0 /* 0x104 */ 1849 .long 0 /* 0x108 */ 1850 .long 0 /* 0x10c */ 1851 .long 0 /* 0x110 */ 1852 .long 0 /* 0x114 */ 1853 .long 0 /* 0x118 */ 1854 .long 0 /* 0x11c */ 1855 .long 0 /* 0x120 */ 1856 .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table 1857 .long 0 /* 0x128 */ 1858 .long 0 /* 0x12c */ 1859 .long 0 /* 0x130 */ 1860 .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table 1861 .long 0 /* 0x138 */ 1862 .long 0 /* 0x13c */ 1863 .long 0 /* 0x140 */ 1864 .long 0 /* 0x144 */ 1865 .long 0 /* 0x148 */ 1866 .long 0 /* 0x14c */ 1867 .long 0 /* 0x150 */ 1868 .long 0 /* 0x154 */ 1869 .long 0 /* 0x158 */ 1870 .long 0 /* 0x15c */ 1871 .long 0 /* 0x160 */ 1872 .long 0 /* 0x164 */ 1873 .long 0 /* 0x168 */ 1874 .long 0 /* 0x16c */ 1875 .long 0 /* 0x170 */ 1876 .long 0 /* 0x174 */ 1877 .long 0 /* 0x178 */ 1878 .long 0 /* 0x17c */ 1879 .long 0 /* 0x180 */ 1880 .long 0 /* 0x184 */ 1881 .long 0 /* 0x188 */ 1882 .long 0 /* 0x18c */ 1883 .long 0 /* 0x190 */ 1884 .long 0 /* 0x194 */ 1885 .long 0 /* 0x198 */ 1886 .long 0 /* 0x19c */ 1887 .long 0 /* 0x1a0 */ 1888 .long 0 /* 0x1a4 */ 1889 .long 0 /* 0x1a8 */ 1890 .long 0 /* 0x1ac */ 1891 .long 0 /* 0x1b0 */ 1892 .long 0 /* 0x1b4 */ 1893 .long 0 /* 0x1b8 */ 1894 .long 0 /* 0x1bc */ 1895 .long 0 /* 0x1c0 */ 1896 .long 0 /* 0x1c4 */ 1897 .long 0 /* 0x1c8 */ 1898 .long 0 /* 0x1cc */ 1899 .long 0 /* 0x1d0 */ 1900 .long 0 /* 0x1d4 */ 1901 .long 0 /* 0x1d8 */ 1902 .long 0 /* 0x1dc */ 1903 .long 0 /* 0x1e0 */ 1904 .long 0 /* 0x1e4 */ 1905 .long 0 /* 0x1e8 */ 1906 .long 0 /* 0x1ec */ 1907 .long 0 /* 0x1f0 */ 1908 .long 0 /* 0x1f4 */ 1909 .long 0 /* 0x1f8 */ 1910 .long 0 /* 0x1fc */ 1911 .long 0 /* 0x200 */ 1912 .long 0 /* 0x204 */ 1913 .long 0 /* 0x208 */ 1914 .long 0 /* 0x20c */ 1915 .long 0 /* 0x210 */ 1916 .long 0 /* 0x214 */ 1917 .long 0 /* 0x218 */ 1918 .long 0 /* 0x21c */ 1919 .long 0 /* 0x220 */ 1920 .long 0 /* 0x224 */ 1921 .long 0 /* 0x228 */ 1922 .long 0 /* 0x22c */ 1923 .long 0 /* 0x230 */ 1924 .long 0 /* 0x234 */ 1925 .long 0 /* 0x238 */ 1926 .long 0 /* 0x23c */ 1927 .long 0 /* 0x240 */ 1928 .long 0 /* 0x244 */ 1929 .long 0 /* 0x248 */ 1930 .long 0 /* 0x24c */ 1931 .long 0 /* 0x250 */ 1932 .long 0 /* 0x254 */ 1933 .long 0 /* 0x258 */ 1934 .long 0 /* 0x25c */ 1935 .long 0 /* 0x260 */ 1936 .long 0 /* 0x264 */ 1937 .long 0 /* 0x268 */ 1938 .long 0 /* 0x26c */ 1939 .long 0 /* 0x270 */ 1940 .long 0 /* 0x274 */ 1941 .long 0 /* 0x278 */ 1942 .long 0 /* 0x27c */ 1943 .long 0 /* 0x280 */ 1944 .long 0 /* 0x284 */ 1945 .long 0 /* 0x288 */ 1946 .long 0 /* 0x28c */ 1947 .long 0 /* 0x290 */ 1948 .long 0 /* 0x294 */ 1949 .long 0 /* 0x298 */ 1950 .long 0 /* 0x29c */ 1951 .long 0 /* 0x2a0 */ 1952 .long 0 /* 0x2a4 */ 1953 .long 0 /* 0x2a8 */ 1954 .long 0 /* 0x2ac */ 1955 .long 0 /* 0x2b0 */ 1956 .long 0 /* 0x2b4 */ 1957 .long 0 /* 0x2b8 */ 1958 .long 0 /* 0x2bc */ 1959 .long 0 /* 0x2c0 */ 1960 .long 0 /* 0x2c4 */ 1961 .long 0 /* 0x2c8 */ 1962 .long 0 /* 0x2cc */ 1963 .long 0 /* 0x2d0 */ 1964 .long 0 /* 0x2d4 */ 1965 .long 0 /* 0x2d8 */ 1966 .long 0 /* 0x2dc */ 1967 .long 0 /* 0x2e0 */ 1968 .long 0 /* 0x2e4 */ 1969 .long 0 /* 0x2e8 */ 1970 .long 0 /* 0x2ec */ 1971 .long 0 /* 0x2f0 */ 1972 .long 0 /* 0x2f4 */ 1973 .long 0 /* 0x2f8 */ 1974 .long 0 /* 0x2fc */ 1975 .long DOTSYM(kvmppc_h_random) - hcall_real_table 1976 .globl hcall_real_table_end 1977hcall_real_table_end: 1978 1979_GLOBAL(kvmppc_h_set_xdabr) 1980 andi. r0, r5, DABRX_USER | DABRX_KERNEL 1981 beq 6f 1982 li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI 1983 andc. r0, r5, r0 1984 beq 3f 19856: li r3, H_PARAMETER 1986 blr 1987 1988_GLOBAL(kvmppc_h_set_dabr) 1989 li r5, DABRX_USER | DABRX_KERNEL 19903: 1991BEGIN_FTR_SECTION 1992 b 2f 1993END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 1994 std r4,VCPU_DABR(r3) 1995 stw r5, VCPU_DABRX(r3) 1996 mtspr SPRN_DABRX, r5 1997 /* Work around P7 bug where DABR can get corrupted on mtspr */ 19981: mtspr SPRN_DABR,r4 1999 mfspr r5, SPRN_DABR 2000 cmpd r4, r5 2001 bne 1b 2002 isync 2003 li r3,0 2004 blr 2005 2006 /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */ 20072: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW 2008 rlwimi r5, r4, 2, DAWRX_WT 2009 clrrdi r4, r4, 3 2010 std r4, VCPU_DAWR(r3) 2011 std r5, VCPU_DAWRX(r3) 2012 mtspr SPRN_DAWR, r4 2013 mtspr SPRN_DAWRX, r5 2014 li r3, 0 2015 blr 2016 2017_GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */ 2018 ori r11,r11,MSR_EE 2019 std r11,VCPU_MSR(r3) 2020 li r0,1 2021 stb r0,VCPU_CEDED(r3) 2022 sync /* order setting ceded vs. testing prodded */ 2023 lbz r5,VCPU_PRODDED(r3) 2024 cmpwi r5,0 2025 bne kvm_cede_prodded 2026 li r12,0 /* set trap to 0 to say hcall is handled */ 2027 stw r12,VCPU_TRAP(r3) 2028 li r0,H_SUCCESS 2029 std r0,VCPU_GPR(R3)(r3) 2030 2031 /* 2032 * Set our bit in the bitmask of napping threads unless all the 2033 * other threads are already napping, in which case we send this 2034 * up to the host. 2035 */ 2036 ld r5,HSTATE_KVM_VCORE(r13) 2037 lbz r6,HSTATE_PTID(r13) 2038 lwz r8,VCORE_ENTRY_EXIT(r5) 2039 clrldi r8,r8,56 2040 li r0,1 2041 sld r0,r0,r6 2042 addi r6,r5,VCORE_NAPPING_THREADS 204331: lwarx r4,0,r6 2044 or r4,r4,r0 2045 cmpw r4,r8 2046 beq kvm_cede_exit 2047 stwcx. r4,0,r6 2048 bne 31b 2049 /* order napping_threads update vs testing entry_exit_map */ 2050 isync 2051 li r0,NAPPING_CEDE 2052 stb r0,HSTATE_NAPPING(r13) 2053 lwz r7,VCORE_ENTRY_EXIT(r5) 2054 cmpwi r7,0x100 2055 bge 33f /* another thread already exiting */ 2056 2057/* 2058 * Although not specifically required by the architecture, POWER7 2059 * preserves the following registers in nap mode, even if an SMT mode 2060 * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3, 2061 * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR. 2062 */ 2063 /* Save non-volatile GPRs */ 2064 std r14, VCPU_GPR(R14)(r3) 2065 std r15, VCPU_GPR(R15)(r3) 2066 std r16, VCPU_GPR(R16)(r3) 2067 std r17, VCPU_GPR(R17)(r3) 2068 std r18, VCPU_GPR(R18)(r3) 2069 std r19, VCPU_GPR(R19)(r3) 2070 std r20, VCPU_GPR(R20)(r3) 2071 std r21, VCPU_GPR(R21)(r3) 2072 std r22, VCPU_GPR(R22)(r3) 2073 std r23, VCPU_GPR(R23)(r3) 2074 std r24, VCPU_GPR(R24)(r3) 2075 std r25, VCPU_GPR(R25)(r3) 2076 std r26, VCPU_GPR(R26)(r3) 2077 std r27, VCPU_GPR(R27)(r3) 2078 std r28, VCPU_GPR(R28)(r3) 2079 std r29, VCPU_GPR(R29)(r3) 2080 std r30, VCPU_GPR(R30)(r3) 2081 std r31, VCPU_GPR(R31)(r3) 2082 2083 /* save FP state */ 2084 bl kvmppc_save_fp 2085 2086#ifdef CONFIG_PPC_TRANSACTIONAL_MEM 2087BEGIN_FTR_SECTION 2088 ld r9, HSTATE_KVM_VCPU(r13) 2089 bl kvmppc_save_tm 2090END_FTR_SECTION_IFSET(CPU_FTR_TM) 2091#endif 2092 2093 /* 2094 * Set DEC to the smaller of DEC and HDEC, so that we wake 2095 * no later than the end of our timeslice (HDEC interrupts 2096 * don't wake us from nap). 2097 */ 2098 mfspr r3, SPRN_DEC 2099 mfspr r4, SPRN_HDEC 2100 mftb r5 2101 cmpw r3, r4 2102 ble 67f 2103 mtspr SPRN_DEC, r4 210467: 2105 /* save expiry time of guest decrementer */ 2106 extsw r3, r3 2107 add r3, r3, r5 2108 ld r4, HSTATE_KVM_VCPU(r13) 2109 ld r5, HSTATE_KVM_VCORE(r13) 2110 ld r6, VCORE_TB_OFFSET(r5) 2111 subf r3, r6, r3 /* convert to host TB value */ 2112 std r3, VCPU_DEC_EXPIRES(r4) 2113 2114#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 2115 ld r4, HSTATE_KVM_VCPU(r13) 2116 addi r3, r4, VCPU_TB_CEDE 2117 bl kvmhv_accumulate_time 2118#endif 2119 2120 lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */ 2121 2122 /* 2123 * Take a nap until a decrementer or external or doobell interrupt 2124 * occurs, with PECE1 and PECE0 set in LPCR. 2125 * On POWER8, set PECEDH, and if we are ceding, also set PECEDP. 2126 * Also clear the runlatch bit before napping. 2127 */ 2128kvm_do_nap: 2129 mfspr r0, SPRN_CTRLF 2130 clrrdi r0, r0, 1 2131 mtspr SPRN_CTRLT, r0 2132 2133 li r0,1 2134 stb r0,HSTATE_HWTHREAD_REQ(r13) 2135 mfspr r5,SPRN_LPCR 2136 ori r5,r5,LPCR_PECE0 | LPCR_PECE1 2137BEGIN_FTR_SECTION 2138 ori r5, r5, LPCR_PECEDH 2139 rlwimi r5, r3, 0, LPCR_PECEDP 2140END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 2141 mtspr SPRN_LPCR,r5 2142 isync 2143 li r0, 0 2144 std r0, HSTATE_SCRATCH0(r13) 2145 ptesync 2146 ld r0, HSTATE_SCRATCH0(r13) 21471: cmpd r0, r0 2148 bne 1b 2149 nap 2150 b . 2151 215233: mr r4, r3 2153 li r3, 0 2154 li r12, 0 2155 b 34f 2156 2157kvm_end_cede: 2158 /* get vcpu pointer */ 2159 ld r4, HSTATE_KVM_VCPU(r13) 2160 2161 /* Woken by external or decrementer interrupt */ 2162 ld r1, HSTATE_HOST_R1(r13) 2163 2164#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 2165 addi r3, r4, VCPU_TB_RMINTR 2166 bl kvmhv_accumulate_time 2167#endif 2168 2169#ifdef CONFIG_PPC_TRANSACTIONAL_MEM 2170BEGIN_FTR_SECTION 2171 bl kvmppc_restore_tm 2172END_FTR_SECTION_IFSET(CPU_FTR_TM) 2173#endif 2174 2175 /* load up FP state */ 2176 bl kvmppc_load_fp 2177 2178 /* Restore guest decrementer */ 2179 ld r3, VCPU_DEC_EXPIRES(r4) 2180 ld r5, HSTATE_KVM_VCORE(r13) 2181 ld r6, VCORE_TB_OFFSET(r5) 2182 add r3, r3, r6 /* convert host TB to guest TB value */ 2183 mftb r7 2184 subf r3, r7, r3 2185 mtspr SPRN_DEC, r3 2186 2187 /* Load NV GPRS */ 2188 ld r14, VCPU_GPR(R14)(r4) 2189 ld r15, VCPU_GPR(R15)(r4) 2190 ld r16, VCPU_GPR(R16)(r4) 2191 ld r17, VCPU_GPR(R17)(r4) 2192 ld r18, VCPU_GPR(R18)(r4) 2193 ld r19, VCPU_GPR(R19)(r4) 2194 ld r20, VCPU_GPR(R20)(r4) 2195 ld r21, VCPU_GPR(R21)(r4) 2196 ld r22, VCPU_GPR(R22)(r4) 2197 ld r23, VCPU_GPR(R23)(r4) 2198 ld r24, VCPU_GPR(R24)(r4) 2199 ld r25, VCPU_GPR(R25)(r4) 2200 ld r26, VCPU_GPR(R26)(r4) 2201 ld r27, VCPU_GPR(R27)(r4) 2202 ld r28, VCPU_GPR(R28)(r4) 2203 ld r29, VCPU_GPR(R29)(r4) 2204 ld r30, VCPU_GPR(R30)(r4) 2205 ld r31, VCPU_GPR(R31)(r4) 2206 2207 /* Check the wake reason in SRR1 to see why we got here */ 2208 bl kvmppc_check_wake_reason 2209 2210 /* clear our bit in vcore->napping_threads */ 221134: ld r5,HSTATE_KVM_VCORE(r13) 2212 lbz r7,HSTATE_PTID(r13) 2213 li r0,1 2214 sld r0,r0,r7 2215 addi r6,r5,VCORE_NAPPING_THREADS 221632: lwarx r7,0,r6 2217 andc r7,r7,r0 2218 stwcx. r7,0,r6 2219 bne 32b 2220 li r0,0 2221 stb r0,HSTATE_NAPPING(r13) 2222 2223 /* See if the wake reason means we need to exit */ 2224 stw r12, VCPU_TRAP(r4) 2225 mr r9, r4 2226 cmpdi r3, 0 2227 bgt guest_exit_cont 2228 2229 /* see if any other thread is already exiting */ 2230 lwz r0,VCORE_ENTRY_EXIT(r5) 2231 cmpwi r0,0x100 2232 bge guest_exit_cont 2233 2234 b kvmppc_cede_reentry /* if not go back to guest */ 2235 2236 /* cede when already previously prodded case */ 2237kvm_cede_prodded: 2238 li r0,0 2239 stb r0,VCPU_PRODDED(r3) 2240 sync /* order testing prodded vs. clearing ceded */ 2241 stb r0,VCPU_CEDED(r3) 2242 li r3,H_SUCCESS 2243 blr 2244 2245 /* we've ceded but we want to give control to the host */ 2246kvm_cede_exit: 2247 ld r9, HSTATE_KVM_VCPU(r13) 2248 b guest_exit_cont 2249 2250 /* Try to handle a machine check in real mode */ 2251machine_check_realmode: 2252 mr r3, r9 /* get vcpu pointer */ 2253 bl kvmppc_realmode_machine_check 2254 nop 2255 ld r9, HSTATE_KVM_VCPU(r13) 2256 li r12, BOOK3S_INTERRUPT_MACHINE_CHECK 2257 /* 2258 * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through 2259 * machine check interrupt (set HSRR0 to 0x200). And for handled 2260 * errors (no-fatal), just go back to guest execution with current 2261 * HSRR0 instead of exiting guest. This new approach will inject 2262 * machine check to guest for fatal error causing guest to crash. 2263 * 2264 * The old code used to return to host for unhandled errors which 2265 * was causing guest to hang with soft lockups inside guest and 2266 * makes it difficult to recover guest instance. 2267 * 2268 * if we receive machine check with MSR(RI=0) then deliver it to 2269 * guest as machine check causing guest to crash. 2270 */ 2271 ld r11, VCPU_MSR(r9) 2272 andi. r10, r11, MSR_RI /* check for unrecoverable exception */ 2273 beq 1f /* Deliver a machine check to guest */ 2274 ld r10, VCPU_PC(r9) 2275 cmpdi r3, 0 /* Did we handle MCE ? */ 2276 bne 2f /* Continue guest execution. */ 2277 /* If not, deliver a machine check. SRR0/1 are already set */ 22781: li r10, BOOK3S_INTERRUPT_MACHINE_CHECK 2279 bl kvmppc_msr_interrupt 22802: b fast_interrupt_c_return 2281 2282/* 2283 * Check the reason we woke from nap, and take appropriate action. 2284 * Returns (in r3): 2285 * 0 if nothing needs to be done 2286 * 1 if something happened that needs to be handled by the host 2287 * -1 if there was a guest wakeup (IPI or msgsnd) 2288 * 2289 * Also sets r12 to the interrupt vector for any interrupt that needs 2290 * to be handled now by the host (0x500 for external interrupt), or zero. 2291 * Modifies r0, r6, r7, r8. 2292 */ 2293kvmppc_check_wake_reason: 2294 mfspr r6, SPRN_SRR1 2295BEGIN_FTR_SECTION 2296 rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */ 2297FTR_SECTION_ELSE 2298 rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */ 2299ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S) 2300 cmpwi r6, 8 /* was it an external interrupt? */ 2301 li r12, BOOK3S_INTERRUPT_EXTERNAL 2302 beq kvmppc_read_intr /* if so, see what it was */ 2303 li r3, 0 2304 li r12, 0 2305 cmpwi r6, 6 /* was it the decrementer? */ 2306 beq 0f 2307BEGIN_FTR_SECTION 2308 cmpwi r6, 5 /* privileged doorbell? */ 2309 beq 0f 2310 cmpwi r6, 3 /* hypervisor doorbell? */ 2311 beq 3f 2312END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) 2313 li r3, 1 /* anything else, return 1 */ 23140: blr 2315 2316 /* hypervisor doorbell */ 23173: li r12, BOOK3S_INTERRUPT_H_DOORBELL 2318 2319 /* 2320 * Clear the doorbell as we will invoke the handler 2321 * explicitly in the guest exit path. 2322 */ 2323 lis r6, (PPC_DBELL_SERVER << (63-36))@h 2324 PPC_MSGCLR(6) 2325 /* see if it's a host IPI */ 2326 li r3, 1 2327 lbz r0, HSTATE_HOST_IPI(r13) 2328 cmpwi r0, 0 2329 bnelr 2330 /* if not, return -1 */ 2331 li r3, -1 2332 blr 2333 2334/* 2335 * Determine what sort of external interrupt is pending (if any). 2336 * Returns: 2337 * 0 if no interrupt is pending 2338 * 1 if an interrupt is pending that needs to be handled by the host 2339 * -1 if there was a guest wakeup IPI (which has now been cleared) 2340 * Modifies r0, r6, r7, r8, returns value in r3. 2341 */ 2342kvmppc_read_intr: 2343 /* see if a host IPI is pending */ 2344 li r3, 1 2345 lbz r0, HSTATE_HOST_IPI(r13) 2346 cmpwi r0, 0 2347 bne 1f 2348 2349 /* Now read the interrupt from the ICP */ 2350 ld r6, HSTATE_XICS_PHYS(r13) 2351 li r7, XICS_XIRR 2352 cmpdi r6, 0 2353 beq- 1f 2354 lwzcix r0, r6, r7 2355 /* 2356 * Save XIRR for later. Since we get in in reverse endian on LE 2357 * systems, save it byte reversed and fetch it back in host endian. 2358 */ 2359 li r3, HSTATE_SAVED_XIRR 2360 STWX_BE r0, r3, r13 2361#ifdef __LITTLE_ENDIAN__ 2362 lwz r3, HSTATE_SAVED_XIRR(r13) 2363#else 2364 mr r3, r0 2365#endif 2366 rlwinm. r3, r3, 0, 0xffffff 2367 sync 2368 beq 1f /* if nothing pending in the ICP */ 2369 2370 /* We found something in the ICP... 2371 * 2372 * If it's not an IPI, stash it in the PACA and return to 2373 * the host, we don't (yet) handle directing real external 2374 * interrupts directly to the guest 2375 */ 2376 cmpwi r3, XICS_IPI /* if there is, is it an IPI? */ 2377 bne 42f 2378 2379 /* It's an IPI, clear the MFRR and EOI it */ 2380 li r3, 0xff 2381 li r8, XICS_MFRR 2382 stbcix r3, r6, r8 /* clear the IPI */ 2383 stwcix r0, r6, r7 /* EOI it */ 2384 sync 2385 2386 /* We need to re-check host IPI now in case it got set in the 2387 * meantime. If it's clear, we bounce the interrupt to the 2388 * guest 2389 */ 2390 lbz r0, HSTATE_HOST_IPI(r13) 2391 cmpwi r0, 0 2392 bne- 43f 2393 2394 /* OK, it's an IPI for us */ 2395 li r12, 0 2396 li r3, -1 23971: blr 2398 239942: /* It's not an IPI and it's for the host. We saved a copy of XIRR in 2400 * the PACA earlier, it will be picked up by the host ICP driver 2401 */ 2402 li r3, 1 2403 b 1b 2404 240543: /* We raced with the host, we need to resend that IPI, bummer */ 2406 li r0, IPI_PRIORITY 2407 stbcix r0, r6, r8 /* set the IPI */ 2408 sync 2409 li r3, 1 2410 b 1b 2411 2412/* 2413 * Save away FP, VMX and VSX registers. 2414 * r3 = vcpu pointer 2415 * N.B. r30 and r31 are volatile across this function, 2416 * thus it is not callable from C. 2417 */ 2418kvmppc_save_fp: 2419 mflr r30 2420 mr r31,r3 2421 mfmsr r5 2422 ori r8,r5,MSR_FP 2423#ifdef CONFIG_ALTIVEC 2424BEGIN_FTR_SECTION 2425 oris r8,r8,MSR_VEC@h 2426END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) 2427#endif 2428#ifdef CONFIG_VSX 2429BEGIN_FTR_SECTION 2430 oris r8,r8,MSR_VSX@h 2431END_FTR_SECTION_IFSET(CPU_FTR_VSX) 2432#endif 2433 mtmsrd r8 2434 addi r3,r3,VCPU_FPRS 2435 bl store_fp_state 2436#ifdef CONFIG_ALTIVEC 2437BEGIN_FTR_SECTION 2438 addi r3,r31,VCPU_VRS 2439 bl store_vr_state 2440END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) 2441#endif 2442 mfspr r6,SPRN_VRSAVE 2443 stw r6,VCPU_VRSAVE(r31) 2444 mtlr r30 2445 blr 2446 2447/* 2448 * Load up FP, VMX and VSX registers 2449 * r4 = vcpu pointer 2450 * N.B. r30 and r31 are volatile across this function, 2451 * thus it is not callable from C. 2452 */ 2453kvmppc_load_fp: 2454 mflr r30 2455 mr r31,r4 2456 mfmsr r9 2457 ori r8,r9,MSR_FP 2458#ifdef CONFIG_ALTIVEC 2459BEGIN_FTR_SECTION 2460 oris r8,r8,MSR_VEC@h 2461END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) 2462#endif 2463#ifdef CONFIG_VSX 2464BEGIN_FTR_SECTION 2465 oris r8,r8,MSR_VSX@h 2466END_FTR_SECTION_IFSET(CPU_FTR_VSX) 2467#endif 2468 mtmsrd r8 2469 addi r3,r4,VCPU_FPRS 2470 bl load_fp_state 2471#ifdef CONFIG_ALTIVEC 2472BEGIN_FTR_SECTION 2473 addi r3,r31,VCPU_VRS 2474 bl load_vr_state 2475END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) 2476#endif 2477 lwz r7,VCPU_VRSAVE(r31) 2478 mtspr SPRN_VRSAVE,r7 2479 mtlr r30 2480 mr r4,r31 2481 blr 2482 2483#ifdef CONFIG_PPC_TRANSACTIONAL_MEM 2484/* 2485 * Save transactional state and TM-related registers. 2486 * Called with r9 pointing to the vcpu struct. 2487 * This can modify all checkpointed registers, but 2488 * restores r1, r2 and r9 (vcpu pointer) before exit. 2489 */ 2490kvmppc_save_tm: 2491 mflr r0 2492 std r0, PPC_LR_STKOFF(r1) 2493 2494 /* Turn on TM. */ 2495 mfmsr r8 2496 li r0, 1 2497 rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG 2498 mtmsrd r8 2499 2500 ld r5, VCPU_MSR(r9) 2501 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62 2502 beq 1f /* TM not active in guest. */ 2503 2504 std r1, HSTATE_HOST_R1(r13) 2505 li r3, TM_CAUSE_KVM_RESCHED 2506 2507 /* Clear the MSR RI since r1, r13 are all going to be foobar. */ 2508 li r5, 0 2509 mtmsrd r5, 1 2510 2511 /* All GPRs are volatile at this point. */ 2512 TRECLAIM(R3) 2513 2514 /* Temporarily store r13 and r9 so we have some regs to play with */ 2515 SET_SCRATCH0(r13) 2516 GET_PACA(r13) 2517 std r9, PACATMSCRATCH(r13) 2518 ld r9, HSTATE_KVM_VCPU(r13) 2519 2520 /* Get a few more GPRs free. */ 2521 std r29, VCPU_GPRS_TM(29)(r9) 2522 std r30, VCPU_GPRS_TM(30)(r9) 2523 std r31, VCPU_GPRS_TM(31)(r9) 2524 2525 /* Save away PPR and DSCR soon so don't run with user values. */ 2526 mfspr r31, SPRN_PPR 2527 HMT_MEDIUM 2528 mfspr r30, SPRN_DSCR 2529 ld r29, HSTATE_DSCR(r13) 2530 mtspr SPRN_DSCR, r29 2531 2532 /* Save all but r9, r13 & r29-r31 */ 2533 reg = 0 2534 .rept 29 2535 .if (reg != 9) && (reg != 13) 2536 std reg, VCPU_GPRS_TM(reg)(r9) 2537 .endif 2538 reg = reg + 1 2539 .endr 2540 /* ... now save r13 */ 2541 GET_SCRATCH0(r4) 2542 std r4, VCPU_GPRS_TM(13)(r9) 2543 /* ... and save r9 */ 2544 ld r4, PACATMSCRATCH(r13) 2545 std r4, VCPU_GPRS_TM(9)(r9) 2546 2547 /* Reload stack pointer and TOC. */ 2548 ld r1, HSTATE_HOST_R1(r13) 2549 ld r2, PACATOC(r13) 2550 2551 /* Set MSR RI now we have r1 and r13 back. */ 2552 li r5, MSR_RI 2553 mtmsrd r5, 1 2554 2555 /* Save away checkpinted SPRs. */ 2556 std r31, VCPU_PPR_TM(r9) 2557 std r30, VCPU_DSCR_TM(r9) 2558 mflr r5 2559 mfcr r6 2560 mfctr r7 2561 mfspr r8, SPRN_AMR 2562 mfspr r10, SPRN_TAR 2563 mfxer r11 2564 std r5, VCPU_LR_TM(r9) 2565 stw r6, VCPU_CR_TM(r9) 2566 std r7, VCPU_CTR_TM(r9) 2567 std r8, VCPU_AMR_TM(r9) 2568 std r10, VCPU_TAR_TM(r9) 2569 std r11, VCPU_XER_TM(r9) 2570 2571 /* Restore r12 as trap number. */ 2572 lwz r12, VCPU_TRAP(r9) 2573 2574 /* Save FP/VSX. */ 2575 addi r3, r9, VCPU_FPRS_TM 2576 bl store_fp_state 2577 addi r3, r9, VCPU_VRS_TM 2578 bl store_vr_state 2579 mfspr r6, SPRN_VRSAVE 2580 stw r6, VCPU_VRSAVE_TM(r9) 25811: 2582 /* 2583 * We need to save these SPRs after the treclaim so that the software 2584 * error code is recorded correctly in the TEXASR. Also the user may 2585 * change these outside of a transaction, so they must always be 2586 * context switched. 2587 */ 2588 mfspr r5, SPRN_TFHAR 2589 mfspr r6, SPRN_TFIAR 2590 mfspr r7, SPRN_TEXASR 2591 std r5, VCPU_TFHAR(r9) 2592 std r6, VCPU_TFIAR(r9) 2593 std r7, VCPU_TEXASR(r9) 2594 2595 ld r0, PPC_LR_STKOFF(r1) 2596 mtlr r0 2597 blr 2598 2599/* 2600 * Restore transactional state and TM-related registers. 2601 * Called with r4 pointing to the vcpu struct. 2602 * This potentially modifies all checkpointed registers. 2603 * It restores r1, r2, r4 from the PACA. 2604 */ 2605kvmppc_restore_tm: 2606 mflr r0 2607 std r0, PPC_LR_STKOFF(r1) 2608 2609 /* Turn on TM/FP/VSX/VMX so we can restore them. */ 2610 mfmsr r5 2611 li r6, MSR_TM >> 32 2612 sldi r6, r6, 32 2613 or r5, r5, r6 2614 ori r5, r5, MSR_FP 2615 oris r5, r5, (MSR_VEC | MSR_VSX)@h 2616 mtmsrd r5 2617 2618 /* 2619 * The user may change these outside of a transaction, so they must 2620 * always be context switched. 2621 */ 2622 ld r5, VCPU_TFHAR(r4) 2623 ld r6, VCPU_TFIAR(r4) 2624 ld r7, VCPU_TEXASR(r4) 2625 mtspr SPRN_TFHAR, r5 2626 mtspr SPRN_TFIAR, r6 2627 mtspr SPRN_TEXASR, r7 2628 2629 ld r5, VCPU_MSR(r4) 2630 rldicl. r5, r5, 64 - MSR_TS_S_LG, 62 2631 beqlr /* TM not active in guest */ 2632 std r1, HSTATE_HOST_R1(r13) 2633 2634 /* Make sure the failure summary is set, otherwise we'll program check 2635 * when we trechkpt. It's possible that this might have been not set 2636 * on a kvmppc_set_one_reg() call but we shouldn't let this crash the 2637 * host. 2638 */ 2639 oris r7, r7, (TEXASR_FS)@h 2640 mtspr SPRN_TEXASR, r7 2641 2642 /* 2643 * We need to load up the checkpointed state for the guest. 2644 * We need to do this early as it will blow away any GPRs, VSRs and 2645 * some SPRs. 2646 */ 2647 2648 mr r31, r4 2649 addi r3, r31, VCPU_FPRS_TM 2650 bl load_fp_state 2651 addi r3, r31, VCPU_VRS_TM 2652 bl load_vr_state 2653 mr r4, r31 2654 lwz r7, VCPU_VRSAVE_TM(r4) 2655 mtspr SPRN_VRSAVE, r7 2656 2657 ld r5, VCPU_LR_TM(r4) 2658 lwz r6, VCPU_CR_TM(r4) 2659 ld r7, VCPU_CTR_TM(r4) 2660 ld r8, VCPU_AMR_TM(r4) 2661 ld r9, VCPU_TAR_TM(r4) 2662 ld r10, VCPU_XER_TM(r4) 2663 mtlr r5 2664 mtcr r6 2665 mtctr r7 2666 mtspr SPRN_AMR, r8 2667 mtspr SPRN_TAR, r9 2668 mtxer r10 2669 2670 /* 2671 * Load up PPR and DSCR values but don't put them in the actual SPRs 2672 * till the last moment to avoid running with userspace PPR and DSCR for 2673 * too long. 2674 */ 2675 ld r29, VCPU_DSCR_TM(r4) 2676 ld r30, VCPU_PPR_TM(r4) 2677 2678 std r2, PACATMSCRATCH(r13) /* Save TOC */ 2679 2680 /* Clear the MSR RI since r1, r13 are all going to be foobar. */ 2681 li r5, 0 2682 mtmsrd r5, 1 2683 2684 /* Load GPRs r0-r28 */ 2685 reg = 0 2686 .rept 29 2687 ld reg, VCPU_GPRS_TM(reg)(r31) 2688 reg = reg + 1 2689 .endr 2690 2691 mtspr SPRN_DSCR, r29 2692 mtspr SPRN_PPR, r30 2693 2694 /* Load final GPRs */ 2695 ld 29, VCPU_GPRS_TM(29)(r31) 2696 ld 30, VCPU_GPRS_TM(30)(r31) 2697 ld 31, VCPU_GPRS_TM(31)(r31) 2698 2699 /* TM checkpointed state is now setup. All GPRs are now volatile. */ 2700 TRECHKPT 2701 2702 /* Now let's get back the state we need. */ 2703 HMT_MEDIUM 2704 GET_PACA(r13) 2705 ld r29, HSTATE_DSCR(r13) 2706 mtspr SPRN_DSCR, r29 2707 ld r4, HSTATE_KVM_VCPU(r13) 2708 ld r1, HSTATE_HOST_R1(r13) 2709 ld r2, PACATMSCRATCH(r13) 2710 2711 /* Set the MSR RI since we have our registers back. */ 2712 li r5, MSR_RI 2713 mtmsrd r5, 1 2714 2715 ld r0, PPC_LR_STKOFF(r1) 2716 mtlr r0 2717 blr 2718#endif 2719 2720/* 2721 * We come here if we get any exception or interrupt while we are 2722 * executing host real mode code while in guest MMU context. 2723 * For now just spin, but we should do something better. 2724 */ 2725kvmppc_bad_host_intr: 2726 b . 2727 2728/* 2729 * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken 2730 * from VCPU_INTR_MSR and is modified based on the required TM state changes. 2731 * r11 has the guest MSR value (in/out) 2732 * r9 has a vcpu pointer (in) 2733 * r0 is used as a scratch register 2734 */ 2735kvmppc_msr_interrupt: 2736 rldicl r0, r11, 64 - MSR_TS_S_LG, 62 2737 cmpwi r0, 2 /* Check if we are in transactional state.. */ 2738 ld r11, VCPU_INTR_MSR(r9) 2739 bne 1f 2740 /* ... if transactional, change to suspended */ 2741 li r0, 1 27421: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG 2743 blr 2744 2745/* 2746 * This works around a hardware bug on POWER8E processors, where 2747 * writing a 1 to the MMCR0[PMAO] bit doesn't generate a 2748 * performance monitor interrupt. Instead, when we need to have 2749 * an interrupt pending, we have to arrange for a counter to overflow. 2750 */ 2751kvmppc_fix_pmao: 2752 li r3, 0 2753 mtspr SPRN_MMCR2, r3 2754 lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h 2755 ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN 2756 mtspr SPRN_MMCR0, r3 2757 lis r3, 0x7fff 2758 ori r3, r3, 0xffff 2759 mtspr SPRN_PMC6, r3 2760 isync 2761 blr 2762 2763#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 2764/* 2765 * Start timing an activity 2766 * r3 = pointer to time accumulation struct, r4 = vcpu 2767 */ 2768kvmhv_start_timing: 2769 ld r5, HSTATE_KVM_VCORE(r13) 2770 lbz r6, VCORE_IN_GUEST(r5) 2771 cmpwi r6, 0 2772 beq 5f /* if in guest, need to */ 2773 ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */ 27745: mftb r5 2775 subf r5, r6, r5 2776 std r3, VCPU_CUR_ACTIVITY(r4) 2777 std r5, VCPU_ACTIVITY_START(r4) 2778 blr 2779 2780/* 2781 * Accumulate time to one activity and start another. 2782 * r3 = pointer to new time accumulation struct, r4 = vcpu 2783 */ 2784kvmhv_accumulate_time: 2785 ld r5, HSTATE_KVM_VCORE(r13) 2786 lbz r8, VCORE_IN_GUEST(r5) 2787 cmpwi r8, 0 2788 beq 4f /* if in guest, need to */ 2789 ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */ 27904: ld r5, VCPU_CUR_ACTIVITY(r4) 2791 ld r6, VCPU_ACTIVITY_START(r4) 2792 std r3, VCPU_CUR_ACTIVITY(r4) 2793 mftb r7 2794 subf r7, r8, r7 2795 std r7, VCPU_ACTIVITY_START(r4) 2796 cmpdi r5, 0 2797 beqlr 2798 subf r3, r6, r7 2799 ld r8, TAS_SEQCOUNT(r5) 2800 cmpdi r8, 0 2801 addi r8, r8, 1 2802 std r8, TAS_SEQCOUNT(r5) 2803 lwsync 2804 ld r7, TAS_TOTAL(r5) 2805 add r7, r7, r3 2806 std r7, TAS_TOTAL(r5) 2807 ld r6, TAS_MIN(r5) 2808 ld r7, TAS_MAX(r5) 2809 beq 3f 2810 cmpd r3, r6 2811 bge 1f 28123: std r3, TAS_MIN(r5) 28131: cmpd r3, r7 2814 ble 2f 2815 std r3, TAS_MAX(r5) 28162: lwsync 2817 addi r8, r8, 1 2818 std r8, TAS_SEQCOUNT(r5) 2819 blr 2820#endif 2821