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