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