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