1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Copyright IBM Corp. 2007 5 * 6 * Authors: Hollis Blanchard <hollisb@us.ibm.com> 7 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> 8 */ 9 10 #include <linux/errno.h> 11 #include <linux/err.h> 12 #include <linux/kvm_host.h> 13 #include <linux/vmalloc.h> 14 #include <linux/hrtimer.h> 15 #include <linux/sched/signal.h> 16 #include <linux/fs.h> 17 #include <linux/slab.h> 18 #include <linux/file.h> 19 #include <linux/module.h> 20 #include <linux/irqbypass.h> 21 #include <linux/kvm_irqfd.h> 22 #include <linux/of.h> 23 #include <asm/cputable.h> 24 #include <linux/uaccess.h> 25 #include <asm/kvm_ppc.h> 26 #include <asm/cputhreads.h> 27 #include <asm/irqflags.h> 28 #include <asm/iommu.h> 29 #include <asm/switch_to.h> 30 #include <asm/xive.h> 31 #ifdef CONFIG_PPC_PSERIES 32 #include <asm/hvcall.h> 33 #include <asm/plpar_wrappers.h> 34 #endif 35 #include <asm/ultravisor.h> 36 #include <asm/setup.h> 37 38 #include "timing.h" 39 #include "../mm/mmu_decl.h" 40 41 #define CREATE_TRACE_POINTS 42 #include "trace.h" 43 44 struct kvmppc_ops *kvmppc_hv_ops; 45 EXPORT_SYMBOL_GPL(kvmppc_hv_ops); 46 struct kvmppc_ops *kvmppc_pr_ops; 47 EXPORT_SYMBOL_GPL(kvmppc_pr_ops); 48 49 50 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) 51 { 52 return !!(v->arch.pending_exceptions) || kvm_request_pending(v); 53 } 54 55 bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) 56 { 57 return kvm_arch_vcpu_runnable(vcpu); 58 } 59 60 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 61 { 62 return false; 63 } 64 65 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 66 { 67 return 1; 68 } 69 70 /* 71 * Common checks before entering the guest world. Call with interrupts 72 * enabled. 73 * 74 * returns: 75 * 76 * == 1 if we're ready to go into guest state 77 * <= 0 if we need to go back to the host with return value 78 */ 79 int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu) 80 { 81 int r; 82 83 WARN_ON(irqs_disabled()); 84 hard_irq_disable(); 85 86 while (true) { 87 if (need_resched()) { 88 local_irq_enable(); 89 cond_resched(); 90 hard_irq_disable(); 91 continue; 92 } 93 94 if (signal_pending(current)) { 95 kvmppc_account_exit(vcpu, SIGNAL_EXITS); 96 vcpu->run->exit_reason = KVM_EXIT_INTR; 97 r = -EINTR; 98 break; 99 } 100 101 vcpu->mode = IN_GUEST_MODE; 102 103 /* 104 * Reading vcpu->requests must happen after setting vcpu->mode, 105 * so we don't miss a request because the requester sees 106 * OUTSIDE_GUEST_MODE and assumes we'll be checking requests 107 * before next entering the guest (and thus doesn't IPI). 108 * This also orders the write to mode from any reads 109 * to the page tables done while the VCPU is running. 110 * Please see the comment in kvm_flush_remote_tlbs. 111 */ 112 smp_mb(); 113 114 if (kvm_request_pending(vcpu)) { 115 /* Make sure we process requests preemptable */ 116 local_irq_enable(); 117 trace_kvm_check_requests(vcpu); 118 r = kvmppc_core_check_requests(vcpu); 119 hard_irq_disable(); 120 if (r > 0) 121 continue; 122 break; 123 } 124 125 if (kvmppc_core_prepare_to_enter(vcpu)) { 126 /* interrupts got enabled in between, so we 127 are back at square 1 */ 128 continue; 129 } 130 131 guest_enter_irqoff(); 132 return 1; 133 } 134 135 /* return to host */ 136 local_irq_enable(); 137 return r; 138 } 139 EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter); 140 141 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE) 142 static void kvmppc_swab_shared(struct kvm_vcpu *vcpu) 143 { 144 struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared; 145 int i; 146 147 shared->sprg0 = swab64(shared->sprg0); 148 shared->sprg1 = swab64(shared->sprg1); 149 shared->sprg2 = swab64(shared->sprg2); 150 shared->sprg3 = swab64(shared->sprg3); 151 shared->srr0 = swab64(shared->srr0); 152 shared->srr1 = swab64(shared->srr1); 153 shared->dar = swab64(shared->dar); 154 shared->msr = swab64(shared->msr); 155 shared->dsisr = swab32(shared->dsisr); 156 shared->int_pending = swab32(shared->int_pending); 157 for (i = 0; i < ARRAY_SIZE(shared->sr); i++) 158 shared->sr[i] = swab32(shared->sr[i]); 159 } 160 #endif 161 162 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu) 163 { 164 int nr = kvmppc_get_gpr(vcpu, 11); 165 int r; 166 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3); 167 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4); 168 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5); 169 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6); 170 unsigned long r2 = 0; 171 172 if (!(kvmppc_get_msr(vcpu) & MSR_SF)) { 173 /* 32 bit mode */ 174 param1 &= 0xffffffff; 175 param2 &= 0xffffffff; 176 param3 &= 0xffffffff; 177 param4 &= 0xffffffff; 178 } 179 180 switch (nr) { 181 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE): 182 { 183 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE) 184 /* Book3S can be little endian, find it out here */ 185 int shared_big_endian = true; 186 if (vcpu->arch.intr_msr & MSR_LE) 187 shared_big_endian = false; 188 if (shared_big_endian != vcpu->arch.shared_big_endian) 189 kvmppc_swab_shared(vcpu); 190 vcpu->arch.shared_big_endian = shared_big_endian; 191 #endif 192 193 if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) { 194 /* 195 * Older versions of the Linux magic page code had 196 * a bug where they would map their trampoline code 197 * NX. If that's the case, remove !PR NX capability. 198 */ 199 vcpu->arch.disable_kernel_nx = true; 200 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 201 } 202 203 vcpu->arch.magic_page_pa = param1 & ~0xfffULL; 204 vcpu->arch.magic_page_ea = param2 & ~0xfffULL; 205 206 #ifdef CONFIG_PPC_64K_PAGES 207 /* 208 * Make sure our 4k magic page is in the same window of a 64k 209 * page within the guest and within the host's page. 210 */ 211 if ((vcpu->arch.magic_page_pa & 0xf000) != 212 ((ulong)vcpu->arch.shared & 0xf000)) { 213 void *old_shared = vcpu->arch.shared; 214 ulong shared = (ulong)vcpu->arch.shared; 215 void *new_shared; 216 217 shared &= PAGE_MASK; 218 shared |= vcpu->arch.magic_page_pa & 0xf000; 219 new_shared = (void*)shared; 220 memcpy(new_shared, old_shared, 0x1000); 221 vcpu->arch.shared = new_shared; 222 } 223 #endif 224 225 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7; 226 227 r = EV_SUCCESS; 228 break; 229 } 230 case KVM_HCALL_TOKEN(KVM_HC_FEATURES): 231 r = EV_SUCCESS; 232 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2) 233 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE); 234 #endif 235 236 /* Second return value is in r4 */ 237 break; 238 case EV_HCALL_TOKEN(EV_IDLE): 239 r = EV_SUCCESS; 240 kvm_vcpu_halt(vcpu); 241 break; 242 default: 243 r = EV_UNIMPLEMENTED; 244 break; 245 } 246 247 kvmppc_set_gpr(vcpu, 4, r2); 248 249 return r; 250 } 251 EXPORT_SYMBOL_GPL(kvmppc_kvm_pv); 252 253 int kvmppc_sanity_check(struct kvm_vcpu *vcpu) 254 { 255 int r = false; 256 257 /* We have to know what CPU to virtualize */ 258 if (!vcpu->arch.pvr) 259 goto out; 260 261 /* PAPR only works with book3s_64 */ 262 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled) 263 goto out; 264 265 /* HV KVM can only do PAPR mode for now */ 266 if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm)) 267 goto out; 268 269 #ifdef CONFIG_KVM_BOOKE_HV 270 if (!cpu_has_feature(CPU_FTR_EMB_HV)) 271 goto out; 272 #endif 273 274 r = true; 275 276 out: 277 vcpu->arch.sane = r; 278 return r ? 0 : -EINVAL; 279 } 280 EXPORT_SYMBOL_GPL(kvmppc_sanity_check); 281 282 int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu) 283 { 284 enum emulation_result er; 285 int r; 286 287 er = kvmppc_emulate_loadstore(vcpu); 288 switch (er) { 289 case EMULATE_DONE: 290 /* Future optimization: only reload non-volatiles if they were 291 * actually modified. */ 292 r = RESUME_GUEST_NV; 293 break; 294 case EMULATE_AGAIN: 295 r = RESUME_GUEST; 296 break; 297 case EMULATE_DO_MMIO: 298 vcpu->run->exit_reason = KVM_EXIT_MMIO; 299 /* We must reload nonvolatiles because "update" load/store 300 * instructions modify register state. */ 301 /* Future optimization: only reload non-volatiles if they were 302 * actually modified. */ 303 r = RESUME_HOST_NV; 304 break; 305 case EMULATE_FAIL: 306 { 307 ppc_inst_t last_inst; 308 309 kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst); 310 kvm_debug_ratelimited("Guest access to device memory using unsupported instruction (opcode: %#08x)\n", 311 ppc_inst_val(last_inst)); 312 313 /* 314 * Injecting a Data Storage here is a bit more 315 * accurate since the instruction that caused the 316 * access could still be a valid one. 317 */ 318 if (!IS_ENABLED(CONFIG_BOOKE)) { 319 ulong dsisr = DSISR_BADACCESS; 320 321 if (vcpu->mmio_is_write) 322 dsisr |= DSISR_ISSTORE; 323 324 kvmppc_core_queue_data_storage(vcpu, 325 kvmppc_get_msr(vcpu) & SRR1_PREFIXED, 326 vcpu->arch.vaddr_accessed, dsisr); 327 } else { 328 /* 329 * BookE does not send a SIGBUS on a bad 330 * fault, so use a Program interrupt instead 331 * to avoid a fault loop. 332 */ 333 kvmppc_core_queue_program(vcpu, 0); 334 } 335 336 r = RESUME_GUEST; 337 break; 338 } 339 default: 340 WARN_ON(1); 341 r = RESUME_GUEST; 342 } 343 344 return r; 345 } 346 EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio); 347 348 int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, 349 bool data) 350 { 351 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK; 352 struct kvmppc_pte pte; 353 int r = -EINVAL; 354 355 vcpu->stat.st++; 356 357 if (vcpu->kvm->arch.kvm_ops && vcpu->kvm->arch.kvm_ops->store_to_eaddr) 358 r = vcpu->kvm->arch.kvm_ops->store_to_eaddr(vcpu, eaddr, ptr, 359 size); 360 361 if ((!r) || (r == -EAGAIN)) 362 return r; 363 364 r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST, 365 XLATE_WRITE, &pte); 366 if (r < 0) 367 return r; 368 369 *eaddr = pte.raddr; 370 371 if (!pte.may_write) 372 return -EPERM; 373 374 /* Magic page override */ 375 if (kvmppc_supports_magic_page(vcpu) && mp_pa && 376 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) && 377 !(kvmppc_get_msr(vcpu) & MSR_PR)) { 378 void *magic = vcpu->arch.shared; 379 magic += pte.eaddr & 0xfff; 380 memcpy(magic, ptr, size); 381 return EMULATE_DONE; 382 } 383 384 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size)) 385 return EMULATE_DO_MMIO; 386 387 return EMULATE_DONE; 388 } 389 EXPORT_SYMBOL_GPL(kvmppc_st); 390 391 int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, 392 bool data) 393 { 394 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK; 395 struct kvmppc_pte pte; 396 int rc = -EINVAL; 397 398 vcpu->stat.ld++; 399 400 if (vcpu->kvm->arch.kvm_ops && vcpu->kvm->arch.kvm_ops->load_from_eaddr) 401 rc = vcpu->kvm->arch.kvm_ops->load_from_eaddr(vcpu, eaddr, ptr, 402 size); 403 404 if ((!rc) || (rc == -EAGAIN)) 405 return rc; 406 407 rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST, 408 XLATE_READ, &pte); 409 if (rc) 410 return rc; 411 412 *eaddr = pte.raddr; 413 414 if (!pte.may_read) 415 return -EPERM; 416 417 if (!data && !pte.may_execute) 418 return -ENOEXEC; 419 420 /* Magic page override */ 421 if (kvmppc_supports_magic_page(vcpu) && mp_pa && 422 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) && 423 !(kvmppc_get_msr(vcpu) & MSR_PR)) { 424 void *magic = vcpu->arch.shared; 425 magic += pte.eaddr & 0xfff; 426 memcpy(ptr, magic, size); 427 return EMULATE_DONE; 428 } 429 430 kvm_vcpu_srcu_read_lock(vcpu); 431 rc = kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size); 432 kvm_vcpu_srcu_read_unlock(vcpu); 433 if (rc) 434 return EMULATE_DO_MMIO; 435 436 return EMULATE_DONE; 437 } 438 EXPORT_SYMBOL_GPL(kvmppc_ld); 439 440 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 441 { 442 struct kvmppc_ops *kvm_ops = NULL; 443 int r; 444 445 /* 446 * if we have both HV and PR enabled, default is HV 447 */ 448 if (type == 0) { 449 if (kvmppc_hv_ops) 450 kvm_ops = kvmppc_hv_ops; 451 else 452 kvm_ops = kvmppc_pr_ops; 453 if (!kvm_ops) 454 goto err_out; 455 } else if (type == KVM_VM_PPC_HV) { 456 if (!kvmppc_hv_ops) 457 goto err_out; 458 kvm_ops = kvmppc_hv_ops; 459 } else if (type == KVM_VM_PPC_PR) { 460 if (!kvmppc_pr_ops) 461 goto err_out; 462 kvm_ops = kvmppc_pr_ops; 463 } else 464 goto err_out; 465 466 if (!try_module_get(kvm_ops->owner)) 467 return -ENOENT; 468 469 kvm->arch.kvm_ops = kvm_ops; 470 r = kvmppc_core_init_vm(kvm); 471 if (r) 472 module_put(kvm_ops->owner); 473 return r; 474 err_out: 475 return -EINVAL; 476 } 477 478 void kvm_arch_destroy_vm(struct kvm *kvm) 479 { 480 #ifdef CONFIG_KVM_XICS 481 /* 482 * We call kick_all_cpus_sync() to ensure that all 483 * CPUs have executed any pending IPIs before we 484 * continue and free VCPUs structures below. 485 */ 486 if (is_kvmppc_hv_enabled(kvm)) 487 kick_all_cpus_sync(); 488 #endif 489 490 kvm_destroy_vcpus(kvm); 491 492 mutex_lock(&kvm->lock); 493 494 kvmppc_core_destroy_vm(kvm); 495 496 mutex_unlock(&kvm->lock); 497 498 /* drop the module reference */ 499 module_put(kvm->arch.kvm_ops->owner); 500 } 501 502 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 503 { 504 int r; 505 /* Assume we're using HV mode when the HV module is loaded */ 506 int hv_enabled = kvmppc_hv_ops ? 1 : 0; 507 508 if (kvm) { 509 /* 510 * Hooray - we know which VM type we're running on. Depend on 511 * that rather than the guess above. 512 */ 513 hv_enabled = is_kvmppc_hv_enabled(kvm); 514 } 515 516 switch (ext) { 517 #ifdef CONFIG_BOOKE 518 case KVM_CAP_PPC_BOOKE_SREGS: 519 case KVM_CAP_PPC_BOOKE_WATCHDOG: 520 case KVM_CAP_PPC_EPR: 521 #else 522 case KVM_CAP_PPC_SEGSTATE: 523 case KVM_CAP_PPC_HIOR: 524 case KVM_CAP_PPC_PAPR: 525 #endif 526 case KVM_CAP_PPC_UNSET_IRQ: 527 case KVM_CAP_PPC_IRQ_LEVEL: 528 case KVM_CAP_ENABLE_CAP: 529 case KVM_CAP_ONE_REG: 530 case KVM_CAP_IOEVENTFD: 531 case KVM_CAP_IMMEDIATE_EXIT: 532 case KVM_CAP_SET_GUEST_DEBUG: 533 r = 1; 534 break; 535 case KVM_CAP_PPC_GUEST_DEBUG_SSTEP: 536 case KVM_CAP_PPC_PAIRED_SINGLES: 537 case KVM_CAP_PPC_OSI: 538 case KVM_CAP_PPC_GET_PVINFO: 539 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) 540 case KVM_CAP_SW_TLB: 541 #endif 542 /* We support this only for PR */ 543 r = !hv_enabled; 544 break; 545 #ifdef CONFIG_KVM_MPIC 546 case KVM_CAP_IRQ_MPIC: 547 r = 1; 548 break; 549 #endif 550 551 #ifdef CONFIG_PPC_BOOK3S_64 552 case KVM_CAP_SPAPR_TCE: 553 fallthrough; 554 case KVM_CAP_SPAPR_TCE_64: 555 case KVM_CAP_SPAPR_TCE_VFIO: 556 case KVM_CAP_PPC_RTAS: 557 case KVM_CAP_PPC_FIXUP_HCALL: 558 case KVM_CAP_PPC_ENABLE_HCALL: 559 #ifdef CONFIG_KVM_XICS 560 case KVM_CAP_IRQ_XICS: 561 #endif 562 case KVM_CAP_PPC_GET_CPU_CHAR: 563 r = 1; 564 break; 565 #ifdef CONFIG_KVM_XIVE 566 case KVM_CAP_PPC_IRQ_XIVE: 567 /* 568 * We need XIVE to be enabled on the platform (implies 569 * a POWER9 processor) and the PowerNV platform, as 570 * nested is not yet supported. 571 */ 572 r = xive_enabled() && !!cpu_has_feature(CPU_FTR_HVMODE) && 573 kvmppc_xive_native_supported(); 574 break; 575 #endif 576 577 #ifdef CONFIG_HAVE_KVM_IRQCHIP 578 case KVM_CAP_IRQFD_RESAMPLE: 579 r = !xive_enabled(); 580 break; 581 #endif 582 583 case KVM_CAP_PPC_ALLOC_HTAB: 584 r = hv_enabled; 585 break; 586 #endif /* CONFIG_PPC_BOOK3S_64 */ 587 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE 588 case KVM_CAP_PPC_SMT: 589 r = 0; 590 if (kvm) { 591 if (kvm->arch.emul_smt_mode > 1) 592 r = kvm->arch.emul_smt_mode; 593 else 594 r = kvm->arch.smt_mode; 595 } else if (hv_enabled) { 596 if (cpu_has_feature(CPU_FTR_ARCH_300)) 597 r = 1; 598 else 599 r = threads_per_subcore; 600 } 601 break; 602 case KVM_CAP_PPC_SMT_POSSIBLE: 603 r = 1; 604 if (hv_enabled) { 605 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 606 r = ((threads_per_subcore << 1) - 1); 607 else 608 /* P9 can emulate dbells, so allow any mode */ 609 r = 8 | 4 | 2 | 1; 610 } 611 break; 612 case KVM_CAP_PPC_HWRNG: 613 r = kvmppc_hwrng_present(); 614 break; 615 case KVM_CAP_PPC_MMU_RADIX: 616 r = !!(hv_enabled && radix_enabled()); 617 break; 618 case KVM_CAP_PPC_MMU_HASH_V3: 619 r = !!(hv_enabled && kvmppc_hv_ops->hash_v3_possible && 620 kvmppc_hv_ops->hash_v3_possible()); 621 break; 622 case KVM_CAP_PPC_NESTED_HV: 623 r = !!(hv_enabled && kvmppc_hv_ops->enable_nested && 624 !kvmppc_hv_ops->enable_nested(NULL)); 625 break; 626 case KVM_CAP_PPC_HTAB_FD: 627 r = hv_enabled; 628 break; 629 #endif 630 case KVM_CAP_NR_VCPUS: 631 /* 632 * Recommending a number of CPUs is somewhat arbitrary; we 633 * return the number of present CPUs for -HV (since a host 634 * will have secondary threads "offline"), and for other KVM 635 * implementations just count online CPUs. 636 */ 637 if (hv_enabled) 638 r = min_t(unsigned int, num_present_cpus(), KVM_MAX_VCPUS); 639 else 640 r = min_t(unsigned int, num_online_cpus(), KVM_MAX_VCPUS); 641 break; 642 case KVM_CAP_MAX_VCPUS: 643 r = KVM_MAX_VCPUS; 644 break; 645 case KVM_CAP_MAX_VCPU_ID: 646 r = KVM_MAX_VCPU_IDS; 647 break; 648 #ifdef CONFIG_PPC_BOOK3S_64 649 case KVM_CAP_PPC_GET_SMMU_INFO: 650 r = 1; 651 break; 652 case KVM_CAP_SPAPR_MULTITCE: 653 r = 1; 654 break; 655 case KVM_CAP_SPAPR_RESIZE_HPT: 656 r = !!hv_enabled; 657 break; 658 #endif 659 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE 660 case KVM_CAP_PPC_FWNMI: 661 r = hv_enabled; 662 break; 663 #endif 664 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 665 case KVM_CAP_PPC_HTM: 666 r = !!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_HTM) || 667 (hv_enabled && cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)); 668 break; 669 #endif 670 #if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE) 671 case KVM_CAP_PPC_SECURE_GUEST: 672 r = hv_enabled && kvmppc_hv_ops->enable_svm && 673 !kvmppc_hv_ops->enable_svm(NULL); 674 break; 675 case KVM_CAP_PPC_DAWR1: 676 r = !!(hv_enabled && kvmppc_hv_ops->enable_dawr1 && 677 !kvmppc_hv_ops->enable_dawr1(NULL)); 678 break; 679 case KVM_CAP_PPC_RPT_INVALIDATE: 680 r = 1; 681 break; 682 #endif 683 case KVM_CAP_PPC_AIL_MODE_3: 684 r = 0; 685 /* 686 * KVM PR, POWER7, and some POWER9s don't support AIL=3 mode. 687 * The POWER9s can support it if the guest runs in hash mode, 688 * but QEMU doesn't necessarily query the capability in time. 689 */ 690 if (hv_enabled) { 691 if (kvmhv_on_pseries()) { 692 if (pseries_reloc_on_exception()) 693 r = 1; 694 } else if (cpu_has_feature(CPU_FTR_ARCH_207S) && 695 !cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG)) { 696 r = 1; 697 } 698 } 699 break; 700 default: 701 r = 0; 702 break; 703 } 704 return r; 705 706 } 707 708 long kvm_arch_dev_ioctl(struct file *filp, 709 unsigned int ioctl, unsigned long arg) 710 { 711 return -EINVAL; 712 } 713 714 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) 715 { 716 kvmppc_core_free_memslot(kvm, slot); 717 } 718 719 int kvm_arch_prepare_memory_region(struct kvm *kvm, 720 const struct kvm_memory_slot *old, 721 struct kvm_memory_slot *new, 722 enum kvm_mr_change change) 723 { 724 return kvmppc_core_prepare_memory_region(kvm, old, new, change); 725 } 726 727 void kvm_arch_commit_memory_region(struct kvm *kvm, 728 struct kvm_memory_slot *old, 729 const struct kvm_memory_slot *new, 730 enum kvm_mr_change change) 731 { 732 kvmppc_core_commit_memory_region(kvm, old, new, change); 733 } 734 735 void kvm_arch_flush_shadow_memslot(struct kvm *kvm, 736 struct kvm_memory_slot *slot) 737 { 738 kvmppc_core_flush_memslot(kvm, slot); 739 } 740 741 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 742 { 743 return 0; 744 } 745 746 static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer) 747 { 748 struct kvm_vcpu *vcpu; 749 750 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer); 751 kvmppc_decrementer_func(vcpu); 752 753 return HRTIMER_NORESTART; 754 } 755 756 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 757 { 758 int err; 759 760 hrtimer_setup(&vcpu->arch.dec_timer, kvmppc_decrementer_wakeup, CLOCK_REALTIME, 761 HRTIMER_MODE_ABS); 762 763 #ifdef CONFIG_KVM_EXIT_TIMING 764 mutex_init(&vcpu->arch.exit_timing_lock); 765 #endif 766 err = kvmppc_subarch_vcpu_init(vcpu); 767 if (err) 768 return err; 769 770 err = kvmppc_core_vcpu_create(vcpu); 771 if (err) 772 goto out_vcpu_uninit; 773 774 rcuwait_init(&vcpu->arch.wait); 775 vcpu->arch.waitp = &vcpu->arch.wait; 776 return 0; 777 778 out_vcpu_uninit: 779 kvmppc_subarch_vcpu_uninit(vcpu); 780 return err; 781 } 782 783 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 784 { 785 } 786 787 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 788 { 789 /* Make sure we're not using the vcpu anymore */ 790 hrtimer_cancel(&vcpu->arch.dec_timer); 791 792 switch (vcpu->arch.irq_type) { 793 case KVMPPC_IRQ_MPIC: 794 kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu); 795 break; 796 case KVMPPC_IRQ_XICS: 797 if (xics_on_xive()) 798 kvmppc_xive_cleanup_vcpu(vcpu); 799 else 800 kvmppc_xics_free_icp(vcpu); 801 break; 802 case KVMPPC_IRQ_XIVE: 803 kvmppc_xive_native_cleanup_vcpu(vcpu); 804 break; 805 } 806 807 kvmppc_core_vcpu_free(vcpu); 808 809 kvmppc_subarch_vcpu_uninit(vcpu); 810 } 811 812 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) 813 { 814 return kvmppc_core_pending_dec(vcpu); 815 } 816 817 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 818 { 819 #ifdef CONFIG_BOOKE 820 /* 821 * vrsave (formerly usprg0) isn't used by Linux, but may 822 * be used by the guest. 823 * 824 * On non-booke this is associated with Altivec and 825 * is handled by code in book3s.c. 826 */ 827 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave); 828 #endif 829 kvmppc_core_vcpu_load(vcpu, cpu); 830 } 831 832 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 833 { 834 kvmppc_core_vcpu_put(vcpu); 835 #ifdef CONFIG_BOOKE 836 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE); 837 #endif 838 } 839 840 /* 841 * irq_bypass_add_producer and irq_bypass_del_producer are only 842 * useful if the architecture supports PCI passthrough. 843 * irq_bypass_stop and irq_bypass_start are not needed and so 844 * kvm_ops are not defined for them. 845 */ 846 bool kvm_arch_has_irq_bypass(void) 847 { 848 return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) || 849 (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer)); 850 } 851 852 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, 853 struct irq_bypass_producer *prod) 854 { 855 struct kvm_kernel_irqfd *irqfd = 856 container_of(cons, struct kvm_kernel_irqfd, consumer); 857 struct kvm *kvm = irqfd->kvm; 858 859 if (kvm->arch.kvm_ops->irq_bypass_add_producer) 860 return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod); 861 862 return 0; 863 } 864 865 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, 866 struct irq_bypass_producer *prod) 867 { 868 struct kvm_kernel_irqfd *irqfd = 869 container_of(cons, struct kvm_kernel_irqfd, consumer); 870 struct kvm *kvm = irqfd->kvm; 871 872 if (kvm->arch.kvm_ops->irq_bypass_del_producer) 873 kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod); 874 } 875 876 #ifdef CONFIG_VSX 877 static inline int kvmppc_get_vsr_dword_offset(int index) 878 { 879 int offset; 880 881 if ((index != 0) && (index != 1)) 882 return -1; 883 884 #ifdef __BIG_ENDIAN 885 offset = index; 886 #else 887 offset = 1 - index; 888 #endif 889 890 return offset; 891 } 892 893 static inline int kvmppc_get_vsr_word_offset(int index) 894 { 895 int offset; 896 897 if ((index > 3) || (index < 0)) 898 return -1; 899 900 #ifdef __BIG_ENDIAN 901 offset = index; 902 #else 903 offset = 3 - index; 904 #endif 905 return offset; 906 } 907 908 static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu, 909 u64 gpr) 910 { 911 union kvmppc_one_reg val; 912 int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset); 913 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; 914 915 if (offset == -1) 916 return; 917 918 if (index >= 32) { 919 kvmppc_get_vsx_vr(vcpu, index - 32, &val.vval); 920 val.vsxval[offset] = gpr; 921 kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval); 922 } else { 923 kvmppc_set_vsx_fpr(vcpu, index, offset, gpr); 924 } 925 } 926 927 static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu, 928 u64 gpr) 929 { 930 union kvmppc_one_reg val; 931 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; 932 933 if (index >= 32) { 934 kvmppc_get_vsx_vr(vcpu, index - 32, &val.vval); 935 val.vsxval[0] = gpr; 936 val.vsxval[1] = gpr; 937 kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval); 938 } else { 939 kvmppc_set_vsx_fpr(vcpu, index, 0, gpr); 940 kvmppc_set_vsx_fpr(vcpu, index, 1, gpr); 941 } 942 } 943 944 static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu *vcpu, 945 u32 gpr) 946 { 947 union kvmppc_one_reg val; 948 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; 949 950 if (index >= 32) { 951 val.vsx32val[0] = gpr; 952 val.vsx32val[1] = gpr; 953 val.vsx32val[2] = gpr; 954 val.vsx32val[3] = gpr; 955 kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval); 956 } else { 957 val.vsx32val[0] = gpr; 958 val.vsx32val[1] = gpr; 959 kvmppc_set_vsx_fpr(vcpu, index, 0, val.vsxval[0]); 960 kvmppc_set_vsx_fpr(vcpu, index, 1, val.vsxval[0]); 961 } 962 } 963 964 static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu, 965 u32 gpr32) 966 { 967 union kvmppc_one_reg val; 968 int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset); 969 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; 970 int dword_offset, word_offset; 971 972 if (offset == -1) 973 return; 974 975 if (index >= 32) { 976 kvmppc_get_vsx_vr(vcpu, index - 32, &val.vval); 977 val.vsx32val[offset] = gpr32; 978 kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval); 979 } else { 980 dword_offset = offset / 2; 981 word_offset = offset % 2; 982 val.vsxval[0] = kvmppc_get_vsx_fpr(vcpu, index, dword_offset); 983 val.vsx32val[word_offset] = gpr32; 984 kvmppc_set_vsx_fpr(vcpu, index, dword_offset, val.vsxval[0]); 985 } 986 } 987 #endif /* CONFIG_VSX */ 988 989 #ifdef CONFIG_ALTIVEC 990 static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu *vcpu, 991 int index, int element_size) 992 { 993 int offset; 994 int elts = sizeof(vector128)/element_size; 995 996 if ((index < 0) || (index >= elts)) 997 return -1; 998 999 if (kvmppc_need_byteswap(vcpu)) 1000 offset = elts - index - 1; 1001 else 1002 offset = index; 1003 1004 return offset; 1005 } 1006 1007 static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu *vcpu, 1008 int index) 1009 { 1010 return kvmppc_get_vmx_offset_generic(vcpu, index, 8); 1011 } 1012 1013 static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu *vcpu, 1014 int index) 1015 { 1016 return kvmppc_get_vmx_offset_generic(vcpu, index, 4); 1017 } 1018 1019 static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu *vcpu, 1020 int index) 1021 { 1022 return kvmppc_get_vmx_offset_generic(vcpu, index, 2); 1023 } 1024 1025 static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu *vcpu, 1026 int index) 1027 { 1028 return kvmppc_get_vmx_offset_generic(vcpu, index, 1); 1029 } 1030 1031 1032 static inline void kvmppc_set_vmx_dword(struct kvm_vcpu *vcpu, 1033 u64 gpr) 1034 { 1035 union kvmppc_one_reg val; 1036 int offset = kvmppc_get_vmx_dword_offset(vcpu, 1037 vcpu->arch.mmio_vmx_offset); 1038 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; 1039 1040 if (offset == -1) 1041 return; 1042 1043 kvmppc_get_vsx_vr(vcpu, index, &val.vval); 1044 val.vsxval[offset] = gpr; 1045 kvmppc_set_vsx_vr(vcpu, index, &val.vval); 1046 } 1047 1048 static inline void kvmppc_set_vmx_word(struct kvm_vcpu *vcpu, 1049 u32 gpr32) 1050 { 1051 union kvmppc_one_reg val; 1052 int offset = kvmppc_get_vmx_word_offset(vcpu, 1053 vcpu->arch.mmio_vmx_offset); 1054 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; 1055 1056 if (offset == -1) 1057 return; 1058 1059 kvmppc_get_vsx_vr(vcpu, index, &val.vval); 1060 val.vsx32val[offset] = gpr32; 1061 kvmppc_set_vsx_vr(vcpu, index, &val.vval); 1062 } 1063 1064 static inline void kvmppc_set_vmx_hword(struct kvm_vcpu *vcpu, 1065 u16 gpr16) 1066 { 1067 union kvmppc_one_reg val; 1068 int offset = kvmppc_get_vmx_hword_offset(vcpu, 1069 vcpu->arch.mmio_vmx_offset); 1070 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; 1071 1072 if (offset == -1) 1073 return; 1074 1075 kvmppc_get_vsx_vr(vcpu, index, &val.vval); 1076 val.vsx16val[offset] = gpr16; 1077 kvmppc_set_vsx_vr(vcpu, index, &val.vval); 1078 } 1079 1080 static inline void kvmppc_set_vmx_byte(struct kvm_vcpu *vcpu, 1081 u8 gpr8) 1082 { 1083 union kvmppc_one_reg val; 1084 int offset = kvmppc_get_vmx_byte_offset(vcpu, 1085 vcpu->arch.mmio_vmx_offset); 1086 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK; 1087 1088 if (offset == -1) 1089 return; 1090 1091 kvmppc_get_vsx_vr(vcpu, index, &val.vval); 1092 val.vsx8val[offset] = gpr8; 1093 kvmppc_set_vsx_vr(vcpu, index, &val.vval); 1094 } 1095 #endif /* CONFIG_ALTIVEC */ 1096 1097 #ifdef CONFIG_PPC_FPU 1098 static inline u64 sp_to_dp(u32 fprs) 1099 { 1100 u64 fprd; 1101 1102 preempt_disable(); 1103 enable_kernel_fp(); 1104 asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m<>" (fprd) : "m<>" (fprs) 1105 : "fr0"); 1106 preempt_enable(); 1107 return fprd; 1108 } 1109 1110 static inline u32 dp_to_sp(u64 fprd) 1111 { 1112 u32 fprs; 1113 1114 preempt_disable(); 1115 enable_kernel_fp(); 1116 asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m<>" (fprs) : "m<>" (fprd) 1117 : "fr0"); 1118 preempt_enable(); 1119 return fprs; 1120 } 1121 1122 #else 1123 #define sp_to_dp(x) (x) 1124 #define dp_to_sp(x) (x) 1125 #endif /* CONFIG_PPC_FPU */ 1126 1127 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu) 1128 { 1129 struct kvm_run *run = vcpu->run; 1130 u64 gpr; 1131 1132 if (run->mmio.len > sizeof(gpr)) 1133 return; 1134 1135 if (!vcpu->arch.mmio_host_swabbed) { 1136 switch (run->mmio.len) { 1137 case 8: gpr = *(u64 *)run->mmio.data; break; 1138 case 4: gpr = *(u32 *)run->mmio.data; break; 1139 case 2: gpr = *(u16 *)run->mmio.data; break; 1140 case 1: gpr = *(u8 *)run->mmio.data; break; 1141 } 1142 } else { 1143 switch (run->mmio.len) { 1144 case 8: gpr = swab64(*(u64 *)run->mmio.data); break; 1145 case 4: gpr = swab32(*(u32 *)run->mmio.data); break; 1146 case 2: gpr = swab16(*(u16 *)run->mmio.data); break; 1147 case 1: gpr = *(u8 *)run->mmio.data; break; 1148 } 1149 } 1150 1151 /* conversion between single and double precision */ 1152 if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4)) 1153 gpr = sp_to_dp(gpr); 1154 1155 if (vcpu->arch.mmio_sign_extend) { 1156 switch (run->mmio.len) { 1157 #ifdef CONFIG_PPC64 1158 case 4: 1159 gpr = (s64)(s32)gpr; 1160 break; 1161 #endif 1162 case 2: 1163 gpr = (s64)(s16)gpr; 1164 break; 1165 case 1: 1166 gpr = (s64)(s8)gpr; 1167 break; 1168 } 1169 } 1170 1171 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) { 1172 case KVM_MMIO_REG_GPR: 1173 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); 1174 break; 1175 case KVM_MMIO_REG_FPR: 1176 if (vcpu->kvm->arch.kvm_ops->giveup_ext) 1177 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_FP); 1178 1179 kvmppc_set_fpr(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK, gpr); 1180 break; 1181 #ifdef CONFIG_PPC_BOOK3S 1182 case KVM_MMIO_REG_QPR: 1183 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; 1184 break; 1185 case KVM_MMIO_REG_FQPR: 1186 kvmppc_set_fpr(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK, gpr); 1187 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; 1188 break; 1189 #endif 1190 #ifdef CONFIG_VSX 1191 case KVM_MMIO_REG_VSX: 1192 if (vcpu->kvm->arch.kvm_ops->giveup_ext) 1193 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VSX); 1194 1195 if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_DWORD) 1196 kvmppc_set_vsr_dword(vcpu, gpr); 1197 else if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_WORD) 1198 kvmppc_set_vsr_word(vcpu, gpr); 1199 else if (vcpu->arch.mmio_copy_type == 1200 KVMPPC_VSX_COPY_DWORD_LOAD_DUMP) 1201 kvmppc_set_vsr_dword_dump(vcpu, gpr); 1202 else if (vcpu->arch.mmio_copy_type == 1203 KVMPPC_VSX_COPY_WORD_LOAD_DUMP) 1204 kvmppc_set_vsr_word_dump(vcpu, gpr); 1205 break; 1206 #endif 1207 #ifdef CONFIG_ALTIVEC 1208 case KVM_MMIO_REG_VMX: 1209 if (vcpu->kvm->arch.kvm_ops->giveup_ext) 1210 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VEC); 1211 1212 if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_DWORD) 1213 kvmppc_set_vmx_dword(vcpu, gpr); 1214 else if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_WORD) 1215 kvmppc_set_vmx_word(vcpu, gpr); 1216 else if (vcpu->arch.mmio_copy_type == 1217 KVMPPC_VMX_COPY_HWORD) 1218 kvmppc_set_vmx_hword(vcpu, gpr); 1219 else if (vcpu->arch.mmio_copy_type == 1220 KVMPPC_VMX_COPY_BYTE) 1221 kvmppc_set_vmx_byte(vcpu, gpr); 1222 break; 1223 #endif 1224 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE 1225 case KVM_MMIO_REG_NESTED_GPR: 1226 if (kvmppc_need_byteswap(vcpu)) 1227 gpr = swab64(gpr); 1228 kvm_vcpu_write_guest(vcpu, vcpu->arch.nested_io_gpr, &gpr, 1229 sizeof(gpr)); 1230 break; 1231 #endif 1232 default: 1233 BUG(); 1234 } 1235 } 1236 1237 static int __kvmppc_handle_load(struct kvm_vcpu *vcpu, 1238 unsigned int rt, unsigned int bytes, 1239 int is_default_endian, int sign_extend) 1240 { 1241 struct kvm_run *run = vcpu->run; 1242 int idx, ret; 1243 bool host_swabbed; 1244 1245 /* Pity C doesn't have a logical XOR operator */ 1246 if (kvmppc_need_byteswap(vcpu)) { 1247 host_swabbed = is_default_endian; 1248 } else { 1249 host_swabbed = !is_default_endian; 1250 } 1251 1252 if (bytes > sizeof(run->mmio.data)) 1253 return EMULATE_FAIL; 1254 1255 run->mmio.phys_addr = vcpu->arch.paddr_accessed; 1256 run->mmio.len = bytes; 1257 run->mmio.is_write = 0; 1258 1259 vcpu->arch.io_gpr = rt; 1260 vcpu->arch.mmio_host_swabbed = host_swabbed; 1261 vcpu->mmio_needed = 1; 1262 vcpu->mmio_is_write = 0; 1263 vcpu->arch.mmio_sign_extend = sign_extend; 1264 1265 idx = srcu_read_lock(&vcpu->kvm->srcu); 1266 1267 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr, 1268 bytes, &run->mmio.data); 1269 1270 srcu_read_unlock(&vcpu->kvm->srcu, idx); 1271 1272 if (!ret) { 1273 kvmppc_complete_mmio_load(vcpu); 1274 vcpu->mmio_needed = 0; 1275 return EMULATE_DONE; 1276 } 1277 1278 return EMULATE_DO_MMIO; 1279 } 1280 1281 int kvmppc_handle_load(struct kvm_vcpu *vcpu, 1282 unsigned int rt, unsigned int bytes, 1283 int is_default_endian) 1284 { 1285 return __kvmppc_handle_load(vcpu, rt, bytes, is_default_endian, 0); 1286 } 1287 EXPORT_SYMBOL_GPL(kvmppc_handle_load); 1288 1289 /* Same as above, but sign extends */ 1290 int kvmppc_handle_loads(struct kvm_vcpu *vcpu, 1291 unsigned int rt, unsigned int bytes, 1292 int is_default_endian) 1293 { 1294 return __kvmppc_handle_load(vcpu, rt, bytes, is_default_endian, 1); 1295 } 1296 1297 #ifdef CONFIG_VSX 1298 int kvmppc_handle_vsx_load(struct kvm_vcpu *vcpu, 1299 unsigned int rt, unsigned int bytes, 1300 int is_default_endian, int mmio_sign_extend) 1301 { 1302 enum emulation_result emulated = EMULATE_DONE; 1303 1304 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */ 1305 if (vcpu->arch.mmio_vsx_copy_nums > 4) 1306 return EMULATE_FAIL; 1307 1308 while (vcpu->arch.mmio_vsx_copy_nums) { 1309 emulated = __kvmppc_handle_load(vcpu, rt, bytes, 1310 is_default_endian, mmio_sign_extend); 1311 1312 if (emulated != EMULATE_DONE) 1313 break; 1314 1315 vcpu->arch.paddr_accessed += vcpu->run->mmio.len; 1316 1317 vcpu->arch.mmio_vsx_copy_nums--; 1318 vcpu->arch.mmio_vsx_offset++; 1319 } 1320 return emulated; 1321 } 1322 #endif /* CONFIG_VSX */ 1323 1324 int kvmppc_handle_store(struct kvm_vcpu *vcpu, 1325 u64 val, unsigned int bytes, int is_default_endian) 1326 { 1327 struct kvm_run *run = vcpu->run; 1328 void *data = run->mmio.data; 1329 int idx, ret; 1330 bool host_swabbed; 1331 1332 /* Pity C doesn't have a logical XOR operator */ 1333 if (kvmppc_need_byteswap(vcpu)) { 1334 host_swabbed = is_default_endian; 1335 } else { 1336 host_swabbed = !is_default_endian; 1337 } 1338 1339 if (bytes > sizeof(run->mmio.data)) 1340 return EMULATE_FAIL; 1341 1342 run->mmio.phys_addr = vcpu->arch.paddr_accessed; 1343 run->mmio.len = bytes; 1344 run->mmio.is_write = 1; 1345 vcpu->mmio_needed = 1; 1346 vcpu->mmio_is_write = 1; 1347 1348 if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4)) 1349 val = dp_to_sp(val); 1350 1351 /* Store the value at the lowest bytes in 'data'. */ 1352 if (!host_swabbed) { 1353 switch (bytes) { 1354 case 8: *(u64 *)data = val; break; 1355 case 4: *(u32 *)data = val; break; 1356 case 2: *(u16 *)data = val; break; 1357 case 1: *(u8 *)data = val; break; 1358 } 1359 } else { 1360 switch (bytes) { 1361 case 8: *(u64 *)data = swab64(val); break; 1362 case 4: *(u32 *)data = swab32(val); break; 1363 case 2: *(u16 *)data = swab16(val); break; 1364 case 1: *(u8 *)data = val; break; 1365 } 1366 } 1367 1368 idx = srcu_read_lock(&vcpu->kvm->srcu); 1369 1370 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr, 1371 bytes, &run->mmio.data); 1372 1373 srcu_read_unlock(&vcpu->kvm->srcu, idx); 1374 1375 if (!ret) { 1376 vcpu->mmio_needed = 0; 1377 return EMULATE_DONE; 1378 } 1379 1380 return EMULATE_DO_MMIO; 1381 } 1382 EXPORT_SYMBOL_GPL(kvmppc_handle_store); 1383 1384 #ifdef CONFIG_VSX 1385 static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val) 1386 { 1387 u32 dword_offset, word_offset; 1388 union kvmppc_one_reg reg; 1389 int vsx_offset = 0; 1390 int copy_type = vcpu->arch.mmio_copy_type; 1391 int result = 0; 1392 1393 switch (copy_type) { 1394 case KVMPPC_VSX_COPY_DWORD: 1395 vsx_offset = 1396 kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset); 1397 1398 if (vsx_offset == -1) { 1399 result = -1; 1400 break; 1401 } 1402 1403 if (rs < 32) { 1404 *val = kvmppc_get_vsx_fpr(vcpu, rs, vsx_offset); 1405 } else { 1406 kvmppc_get_vsx_vr(vcpu, rs - 32, ®.vval); 1407 *val = reg.vsxval[vsx_offset]; 1408 } 1409 break; 1410 1411 case KVMPPC_VSX_COPY_WORD: 1412 vsx_offset = 1413 kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset); 1414 1415 if (vsx_offset == -1) { 1416 result = -1; 1417 break; 1418 } 1419 1420 if (rs < 32) { 1421 dword_offset = vsx_offset / 2; 1422 word_offset = vsx_offset % 2; 1423 reg.vsxval[0] = kvmppc_get_vsx_fpr(vcpu, rs, dword_offset); 1424 *val = reg.vsx32val[word_offset]; 1425 } else { 1426 kvmppc_get_vsx_vr(vcpu, rs - 32, ®.vval); 1427 *val = reg.vsx32val[vsx_offset]; 1428 } 1429 break; 1430 1431 default: 1432 result = -1; 1433 break; 1434 } 1435 1436 return result; 1437 } 1438 1439 int kvmppc_handle_vsx_store(struct kvm_vcpu *vcpu, 1440 int rs, unsigned int bytes, int is_default_endian) 1441 { 1442 u64 val; 1443 enum emulation_result emulated = EMULATE_DONE; 1444 1445 vcpu->arch.io_gpr = rs; 1446 1447 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */ 1448 if (vcpu->arch.mmio_vsx_copy_nums > 4) 1449 return EMULATE_FAIL; 1450 1451 while (vcpu->arch.mmio_vsx_copy_nums) { 1452 if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1) 1453 return EMULATE_FAIL; 1454 1455 emulated = kvmppc_handle_store(vcpu, 1456 val, bytes, is_default_endian); 1457 1458 if (emulated != EMULATE_DONE) 1459 break; 1460 1461 vcpu->arch.paddr_accessed += vcpu->run->mmio.len; 1462 1463 vcpu->arch.mmio_vsx_copy_nums--; 1464 vcpu->arch.mmio_vsx_offset++; 1465 } 1466 1467 return emulated; 1468 } 1469 1470 static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu) 1471 { 1472 struct kvm_run *run = vcpu->run; 1473 enum emulation_result emulated = EMULATE_FAIL; 1474 int r; 1475 1476 vcpu->arch.paddr_accessed += run->mmio.len; 1477 1478 if (!vcpu->mmio_is_write) { 1479 emulated = kvmppc_handle_vsx_load(vcpu, vcpu->arch.io_gpr, 1480 run->mmio.len, 1, vcpu->arch.mmio_sign_extend); 1481 } else { 1482 emulated = kvmppc_handle_vsx_store(vcpu, 1483 vcpu->arch.io_gpr, run->mmio.len, 1); 1484 } 1485 1486 switch (emulated) { 1487 case EMULATE_DO_MMIO: 1488 run->exit_reason = KVM_EXIT_MMIO; 1489 r = RESUME_HOST; 1490 break; 1491 case EMULATE_FAIL: 1492 pr_info("KVM: MMIO emulation failed (VSX repeat)\n"); 1493 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 1494 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; 1495 r = RESUME_HOST; 1496 break; 1497 default: 1498 r = RESUME_GUEST; 1499 break; 1500 } 1501 return r; 1502 } 1503 #endif /* CONFIG_VSX */ 1504 1505 #ifdef CONFIG_ALTIVEC 1506 int kvmppc_handle_vmx_load(struct kvm_vcpu *vcpu, 1507 unsigned int rt, unsigned int bytes, int is_default_endian) 1508 { 1509 enum emulation_result emulated = EMULATE_DONE; 1510 1511 if (vcpu->arch.mmio_vmx_copy_nums > 2) 1512 return EMULATE_FAIL; 1513 1514 while (vcpu->arch.mmio_vmx_copy_nums) { 1515 emulated = __kvmppc_handle_load(vcpu, rt, bytes, 1516 is_default_endian, 0); 1517 1518 if (emulated != EMULATE_DONE) 1519 break; 1520 1521 vcpu->arch.paddr_accessed += vcpu->run->mmio.len; 1522 vcpu->arch.mmio_vmx_copy_nums--; 1523 vcpu->arch.mmio_vmx_offset++; 1524 } 1525 1526 return emulated; 1527 } 1528 1529 static int kvmppc_get_vmx_dword(struct kvm_vcpu *vcpu, int index, u64 *val) 1530 { 1531 union kvmppc_one_reg reg; 1532 int vmx_offset = 0; 1533 int result = 0; 1534 1535 vmx_offset = 1536 kvmppc_get_vmx_dword_offset(vcpu, vcpu->arch.mmio_vmx_offset); 1537 1538 if (vmx_offset == -1) 1539 return -1; 1540 1541 kvmppc_get_vsx_vr(vcpu, index, ®.vval); 1542 *val = reg.vsxval[vmx_offset]; 1543 1544 return result; 1545 } 1546 1547 static int kvmppc_get_vmx_word(struct kvm_vcpu *vcpu, int index, u64 *val) 1548 { 1549 union kvmppc_one_reg reg; 1550 int vmx_offset = 0; 1551 int result = 0; 1552 1553 vmx_offset = 1554 kvmppc_get_vmx_word_offset(vcpu, vcpu->arch.mmio_vmx_offset); 1555 1556 if (vmx_offset == -1) 1557 return -1; 1558 1559 kvmppc_get_vsx_vr(vcpu, index, ®.vval); 1560 *val = reg.vsx32val[vmx_offset]; 1561 1562 return result; 1563 } 1564 1565 static int kvmppc_get_vmx_hword(struct kvm_vcpu *vcpu, int index, u64 *val) 1566 { 1567 union kvmppc_one_reg reg; 1568 int vmx_offset = 0; 1569 int result = 0; 1570 1571 vmx_offset = 1572 kvmppc_get_vmx_hword_offset(vcpu, vcpu->arch.mmio_vmx_offset); 1573 1574 if (vmx_offset == -1) 1575 return -1; 1576 1577 kvmppc_get_vsx_vr(vcpu, index, ®.vval); 1578 *val = reg.vsx16val[vmx_offset]; 1579 1580 return result; 1581 } 1582 1583 static int kvmppc_get_vmx_byte(struct kvm_vcpu *vcpu, int index, u64 *val) 1584 { 1585 union kvmppc_one_reg reg; 1586 int vmx_offset = 0; 1587 int result = 0; 1588 1589 vmx_offset = 1590 kvmppc_get_vmx_byte_offset(vcpu, vcpu->arch.mmio_vmx_offset); 1591 1592 if (vmx_offset == -1) 1593 return -1; 1594 1595 kvmppc_get_vsx_vr(vcpu, index, ®.vval); 1596 *val = reg.vsx8val[vmx_offset]; 1597 1598 return result; 1599 } 1600 1601 int kvmppc_handle_vmx_store(struct kvm_vcpu *vcpu, 1602 unsigned int rs, unsigned int bytes, int is_default_endian) 1603 { 1604 u64 val = 0; 1605 unsigned int index = rs & KVM_MMIO_REG_MASK; 1606 enum emulation_result emulated = EMULATE_DONE; 1607 1608 if (vcpu->arch.mmio_vmx_copy_nums > 2) 1609 return EMULATE_FAIL; 1610 1611 vcpu->arch.io_gpr = rs; 1612 1613 while (vcpu->arch.mmio_vmx_copy_nums) { 1614 switch (vcpu->arch.mmio_copy_type) { 1615 case KVMPPC_VMX_COPY_DWORD: 1616 if (kvmppc_get_vmx_dword(vcpu, index, &val) == -1) 1617 return EMULATE_FAIL; 1618 1619 break; 1620 case KVMPPC_VMX_COPY_WORD: 1621 if (kvmppc_get_vmx_word(vcpu, index, &val) == -1) 1622 return EMULATE_FAIL; 1623 break; 1624 case KVMPPC_VMX_COPY_HWORD: 1625 if (kvmppc_get_vmx_hword(vcpu, index, &val) == -1) 1626 return EMULATE_FAIL; 1627 break; 1628 case KVMPPC_VMX_COPY_BYTE: 1629 if (kvmppc_get_vmx_byte(vcpu, index, &val) == -1) 1630 return EMULATE_FAIL; 1631 break; 1632 default: 1633 return EMULATE_FAIL; 1634 } 1635 1636 emulated = kvmppc_handle_store(vcpu, val, bytes, 1637 is_default_endian); 1638 if (emulated != EMULATE_DONE) 1639 break; 1640 1641 vcpu->arch.paddr_accessed += vcpu->run->mmio.len; 1642 vcpu->arch.mmio_vmx_copy_nums--; 1643 vcpu->arch.mmio_vmx_offset++; 1644 } 1645 1646 return emulated; 1647 } 1648 1649 static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu *vcpu) 1650 { 1651 struct kvm_run *run = vcpu->run; 1652 enum emulation_result emulated = EMULATE_FAIL; 1653 int r; 1654 1655 vcpu->arch.paddr_accessed += run->mmio.len; 1656 1657 if (!vcpu->mmio_is_write) { 1658 emulated = kvmppc_handle_vmx_load(vcpu, 1659 vcpu->arch.io_gpr, run->mmio.len, 1); 1660 } else { 1661 emulated = kvmppc_handle_vmx_store(vcpu, 1662 vcpu->arch.io_gpr, run->mmio.len, 1); 1663 } 1664 1665 switch (emulated) { 1666 case EMULATE_DO_MMIO: 1667 run->exit_reason = KVM_EXIT_MMIO; 1668 r = RESUME_HOST; 1669 break; 1670 case EMULATE_FAIL: 1671 pr_info("KVM: MMIO emulation failed (VMX repeat)\n"); 1672 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 1673 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; 1674 r = RESUME_HOST; 1675 break; 1676 default: 1677 r = RESUME_GUEST; 1678 break; 1679 } 1680 return r; 1681 } 1682 #endif /* CONFIG_ALTIVEC */ 1683 1684 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) 1685 { 1686 int r = 0; 1687 union kvmppc_one_reg val; 1688 int size; 1689 1690 size = one_reg_size(reg->id); 1691 if (size > sizeof(val)) 1692 return -EINVAL; 1693 1694 r = kvmppc_get_one_reg(vcpu, reg->id, &val); 1695 if (r == -EINVAL) { 1696 r = 0; 1697 switch (reg->id) { 1698 #ifdef CONFIG_ALTIVEC 1699 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31: 1700 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { 1701 r = -ENXIO; 1702 break; 1703 } 1704 kvmppc_get_vsx_vr(vcpu, reg->id - KVM_REG_PPC_VR0, &val.vval); 1705 break; 1706 case KVM_REG_PPC_VSCR: 1707 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { 1708 r = -ENXIO; 1709 break; 1710 } 1711 val = get_reg_val(reg->id, kvmppc_get_vscr(vcpu)); 1712 break; 1713 case KVM_REG_PPC_VRSAVE: 1714 val = get_reg_val(reg->id, kvmppc_get_vrsave(vcpu)); 1715 break; 1716 #endif /* CONFIG_ALTIVEC */ 1717 default: 1718 r = -EINVAL; 1719 break; 1720 } 1721 } 1722 1723 if (r) 1724 return r; 1725 1726 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size)) 1727 r = -EFAULT; 1728 1729 return r; 1730 } 1731 1732 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) 1733 { 1734 int r; 1735 union kvmppc_one_reg val; 1736 int size; 1737 1738 size = one_reg_size(reg->id); 1739 if (size > sizeof(val)) 1740 return -EINVAL; 1741 1742 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size)) 1743 return -EFAULT; 1744 1745 r = kvmppc_set_one_reg(vcpu, reg->id, &val); 1746 if (r == -EINVAL) { 1747 r = 0; 1748 switch (reg->id) { 1749 #ifdef CONFIG_ALTIVEC 1750 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31: 1751 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { 1752 r = -ENXIO; 1753 break; 1754 } 1755 kvmppc_set_vsx_vr(vcpu, reg->id - KVM_REG_PPC_VR0, &val.vval); 1756 break; 1757 case KVM_REG_PPC_VSCR: 1758 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { 1759 r = -ENXIO; 1760 break; 1761 } 1762 kvmppc_set_vscr(vcpu, set_reg_val(reg->id, val)); 1763 break; 1764 case KVM_REG_PPC_VRSAVE: 1765 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { 1766 r = -ENXIO; 1767 break; 1768 } 1769 kvmppc_set_vrsave(vcpu, set_reg_val(reg->id, val)); 1770 break; 1771 #endif /* CONFIG_ALTIVEC */ 1772 default: 1773 r = -EINVAL; 1774 break; 1775 } 1776 } 1777 1778 return r; 1779 } 1780 1781 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 1782 { 1783 struct kvm_run *run = vcpu->run; 1784 int r; 1785 1786 vcpu_load(vcpu); 1787 1788 if (vcpu->mmio_needed) { 1789 vcpu->mmio_needed = 0; 1790 if (!vcpu->mmio_is_write) 1791 kvmppc_complete_mmio_load(vcpu); 1792 #ifdef CONFIG_VSX 1793 if (vcpu->arch.mmio_vsx_copy_nums > 0) { 1794 vcpu->arch.mmio_vsx_copy_nums--; 1795 vcpu->arch.mmio_vsx_offset++; 1796 } 1797 1798 if (vcpu->arch.mmio_vsx_copy_nums > 0) { 1799 r = kvmppc_emulate_mmio_vsx_loadstore(vcpu); 1800 if (r == RESUME_HOST) { 1801 vcpu->mmio_needed = 1; 1802 goto out; 1803 } 1804 } 1805 #endif 1806 #ifdef CONFIG_ALTIVEC 1807 if (vcpu->arch.mmio_vmx_copy_nums > 0) { 1808 vcpu->arch.mmio_vmx_copy_nums--; 1809 vcpu->arch.mmio_vmx_offset++; 1810 } 1811 1812 if (vcpu->arch.mmio_vmx_copy_nums > 0) { 1813 r = kvmppc_emulate_mmio_vmx_loadstore(vcpu); 1814 if (r == RESUME_HOST) { 1815 vcpu->mmio_needed = 1; 1816 goto out; 1817 } 1818 } 1819 #endif 1820 } else if (vcpu->arch.osi_needed) { 1821 u64 *gprs = run->osi.gprs; 1822 int i; 1823 1824 for (i = 0; i < 32; i++) 1825 kvmppc_set_gpr(vcpu, i, gprs[i]); 1826 vcpu->arch.osi_needed = 0; 1827 } else if (vcpu->arch.hcall_needed) { 1828 int i; 1829 1830 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret); 1831 for (i = 0; i < 9; ++i) 1832 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]); 1833 vcpu->arch.hcall_needed = 0; 1834 #ifdef CONFIG_BOOKE 1835 } else if (vcpu->arch.epr_needed) { 1836 kvmppc_set_epr(vcpu, run->epr.epr); 1837 vcpu->arch.epr_needed = 0; 1838 #endif 1839 } 1840 1841 kvm_sigset_activate(vcpu); 1842 1843 if (!vcpu->wants_to_run) 1844 r = -EINTR; 1845 else 1846 r = kvmppc_vcpu_run(vcpu); 1847 1848 kvm_sigset_deactivate(vcpu); 1849 1850 #ifdef CONFIG_ALTIVEC 1851 out: 1852 #endif 1853 1854 /* 1855 * We're already returning to userspace, don't pass the 1856 * RESUME_HOST flags along. 1857 */ 1858 if (r > 0) 1859 r = 0; 1860 1861 vcpu_put(vcpu); 1862 return r; 1863 } 1864 1865 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) 1866 { 1867 if (irq->irq == KVM_INTERRUPT_UNSET) { 1868 kvmppc_core_dequeue_external(vcpu); 1869 return 0; 1870 } 1871 1872 kvmppc_core_queue_external(vcpu, irq); 1873 1874 kvm_vcpu_kick(vcpu); 1875 1876 return 0; 1877 } 1878 1879 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 1880 struct kvm_enable_cap *cap) 1881 { 1882 int r; 1883 1884 if (cap->flags) 1885 return -EINVAL; 1886 1887 switch (cap->cap) { 1888 case KVM_CAP_PPC_OSI: 1889 r = 0; 1890 vcpu->arch.osi_enabled = true; 1891 break; 1892 case KVM_CAP_PPC_PAPR: 1893 r = 0; 1894 vcpu->arch.papr_enabled = true; 1895 break; 1896 case KVM_CAP_PPC_EPR: 1897 r = 0; 1898 if (cap->args[0]) 1899 vcpu->arch.epr_flags |= KVMPPC_EPR_USER; 1900 else 1901 vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER; 1902 break; 1903 #ifdef CONFIG_BOOKE 1904 case KVM_CAP_PPC_BOOKE_WATCHDOG: 1905 r = 0; 1906 vcpu->arch.watchdog_enabled = true; 1907 break; 1908 #endif 1909 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) 1910 case KVM_CAP_SW_TLB: { 1911 struct kvm_config_tlb cfg; 1912 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0]; 1913 1914 r = -EFAULT; 1915 if (copy_from_user(&cfg, user_ptr, sizeof(cfg))) 1916 break; 1917 1918 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg); 1919 break; 1920 } 1921 #endif 1922 #ifdef CONFIG_KVM_MPIC 1923 case KVM_CAP_IRQ_MPIC: { 1924 CLASS(fd, f)(cap->args[0]); 1925 struct kvm_device *dev; 1926 1927 r = -EBADF; 1928 if (fd_empty(f)) 1929 break; 1930 1931 r = -EPERM; 1932 dev = kvm_device_from_filp(fd_file(f)); 1933 if (dev) 1934 r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]); 1935 1936 break; 1937 } 1938 #endif 1939 #ifdef CONFIG_KVM_XICS 1940 case KVM_CAP_IRQ_XICS: { 1941 CLASS(fd, f)(cap->args[0]); 1942 struct kvm_device *dev; 1943 1944 r = -EBADF; 1945 if (fd_empty(f)) 1946 break; 1947 1948 r = -EPERM; 1949 dev = kvm_device_from_filp(fd_file(f)); 1950 if (dev) { 1951 if (xics_on_xive()) 1952 r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]); 1953 else 1954 r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]); 1955 } 1956 break; 1957 } 1958 #endif /* CONFIG_KVM_XICS */ 1959 #ifdef CONFIG_KVM_XIVE 1960 case KVM_CAP_PPC_IRQ_XIVE: { 1961 CLASS(fd, f)(cap->args[0]); 1962 struct kvm_device *dev; 1963 1964 r = -EBADF; 1965 if (fd_empty(f)) 1966 break; 1967 1968 r = -ENXIO; 1969 if (!xive_enabled()) 1970 break; 1971 1972 r = -EPERM; 1973 dev = kvm_device_from_filp(fd_file(f)); 1974 if (dev) 1975 r = kvmppc_xive_native_connect_vcpu(dev, vcpu, 1976 cap->args[1]); 1977 break; 1978 } 1979 #endif /* CONFIG_KVM_XIVE */ 1980 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE 1981 case KVM_CAP_PPC_FWNMI: 1982 r = -EINVAL; 1983 if (!is_kvmppc_hv_enabled(vcpu->kvm)) 1984 break; 1985 r = 0; 1986 vcpu->kvm->arch.fwnmi_enabled = true; 1987 break; 1988 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */ 1989 default: 1990 r = -EINVAL; 1991 break; 1992 } 1993 1994 if (!r) 1995 r = kvmppc_sanity_check(vcpu); 1996 1997 return r; 1998 } 1999 2000 bool kvm_arch_intc_initialized(struct kvm *kvm) 2001 { 2002 #ifdef CONFIG_KVM_MPIC 2003 if (kvm->arch.mpic) 2004 return true; 2005 #endif 2006 #ifdef CONFIG_KVM_XICS 2007 if (kvm->arch.xics || kvm->arch.xive) 2008 return true; 2009 #endif 2010 return false; 2011 } 2012 2013 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 2014 struct kvm_mp_state *mp_state) 2015 { 2016 return -EINVAL; 2017 } 2018 2019 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 2020 struct kvm_mp_state *mp_state) 2021 { 2022 return -EINVAL; 2023 } 2024 2025 long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl, 2026 unsigned long arg) 2027 { 2028 struct kvm_vcpu *vcpu = filp->private_data; 2029 void __user *argp = (void __user *)arg; 2030 2031 if (ioctl == KVM_INTERRUPT) { 2032 struct kvm_interrupt irq; 2033 if (copy_from_user(&irq, argp, sizeof(irq))) 2034 return -EFAULT; 2035 return kvm_vcpu_ioctl_interrupt(vcpu, &irq); 2036 } 2037 return -ENOIOCTLCMD; 2038 } 2039 2040 long kvm_arch_vcpu_ioctl(struct file *filp, 2041 unsigned int ioctl, unsigned long arg) 2042 { 2043 struct kvm_vcpu *vcpu = filp->private_data; 2044 void __user *argp = (void __user *)arg; 2045 long r; 2046 2047 switch (ioctl) { 2048 case KVM_ENABLE_CAP: 2049 { 2050 struct kvm_enable_cap cap; 2051 r = -EFAULT; 2052 if (copy_from_user(&cap, argp, sizeof(cap))) 2053 goto out; 2054 vcpu_load(vcpu); 2055 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 2056 vcpu_put(vcpu); 2057 break; 2058 } 2059 2060 case KVM_SET_ONE_REG: 2061 case KVM_GET_ONE_REG: 2062 { 2063 struct kvm_one_reg reg; 2064 r = -EFAULT; 2065 if (copy_from_user(®, argp, sizeof(reg))) 2066 goto out; 2067 if (ioctl == KVM_SET_ONE_REG) 2068 r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®); 2069 else 2070 r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®); 2071 break; 2072 } 2073 2074 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) 2075 case KVM_DIRTY_TLB: { 2076 struct kvm_dirty_tlb dirty; 2077 r = -EFAULT; 2078 if (copy_from_user(&dirty, argp, sizeof(dirty))) 2079 goto out; 2080 vcpu_load(vcpu); 2081 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty); 2082 vcpu_put(vcpu); 2083 break; 2084 } 2085 #endif 2086 default: 2087 r = -EINVAL; 2088 } 2089 2090 out: 2091 return r; 2092 } 2093 2094 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 2095 { 2096 return VM_FAULT_SIGBUS; 2097 } 2098 2099 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo) 2100 { 2101 u32 inst_nop = 0x60000000; 2102 #ifdef CONFIG_KVM_BOOKE_HV 2103 u32 inst_sc1 = 0x44000022; 2104 pvinfo->hcall[0] = cpu_to_be32(inst_sc1); 2105 pvinfo->hcall[1] = cpu_to_be32(inst_nop); 2106 pvinfo->hcall[2] = cpu_to_be32(inst_nop); 2107 pvinfo->hcall[3] = cpu_to_be32(inst_nop); 2108 #else 2109 u32 inst_lis = 0x3c000000; 2110 u32 inst_ori = 0x60000000; 2111 u32 inst_sc = 0x44000002; 2112 u32 inst_imm_mask = 0xffff; 2113 2114 /* 2115 * The hypercall to get into KVM from within guest context is as 2116 * follows: 2117 * 2118 * lis r0, r0, KVM_SC_MAGIC_R0@h 2119 * ori r0, KVM_SC_MAGIC_R0@l 2120 * sc 2121 * nop 2122 */ 2123 pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask)); 2124 pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask)); 2125 pvinfo->hcall[2] = cpu_to_be32(inst_sc); 2126 pvinfo->hcall[3] = cpu_to_be32(inst_nop); 2127 #endif 2128 2129 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE; 2130 2131 return 0; 2132 } 2133 2134 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm) 2135 { 2136 int ret = 0; 2137 2138 #ifdef CONFIG_KVM_MPIC 2139 ret = ret || (kvm->arch.mpic != NULL); 2140 #endif 2141 #ifdef CONFIG_KVM_XICS 2142 ret = ret || (kvm->arch.xics != NULL); 2143 ret = ret || (kvm->arch.xive != NULL); 2144 #endif 2145 smp_rmb(); 2146 return ret; 2147 } 2148 2149 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, 2150 bool line_status) 2151 { 2152 if (!kvm_arch_irqchip_in_kernel(kvm)) 2153 return -ENXIO; 2154 2155 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 2156 irq_event->irq, irq_event->level, 2157 line_status); 2158 return 0; 2159 } 2160 2161 2162 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, 2163 struct kvm_enable_cap *cap) 2164 { 2165 int r; 2166 2167 if (cap->flags) 2168 return -EINVAL; 2169 2170 switch (cap->cap) { 2171 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER 2172 case KVM_CAP_PPC_ENABLE_HCALL: { 2173 unsigned long hcall = cap->args[0]; 2174 2175 r = -EINVAL; 2176 if (hcall > MAX_HCALL_OPCODE || (hcall & 3) || 2177 cap->args[1] > 1) 2178 break; 2179 if (!kvmppc_book3s_hcall_implemented(kvm, hcall)) 2180 break; 2181 if (cap->args[1]) 2182 set_bit(hcall / 4, kvm->arch.enabled_hcalls); 2183 else 2184 clear_bit(hcall / 4, kvm->arch.enabled_hcalls); 2185 r = 0; 2186 break; 2187 } 2188 case KVM_CAP_PPC_SMT: { 2189 unsigned long mode = cap->args[0]; 2190 unsigned long flags = cap->args[1]; 2191 2192 r = -EINVAL; 2193 if (kvm->arch.kvm_ops->set_smt_mode) 2194 r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags); 2195 break; 2196 } 2197 2198 case KVM_CAP_PPC_NESTED_HV: 2199 r = -EINVAL; 2200 if (!is_kvmppc_hv_enabled(kvm) || 2201 !kvm->arch.kvm_ops->enable_nested) 2202 break; 2203 r = kvm->arch.kvm_ops->enable_nested(kvm); 2204 break; 2205 #endif 2206 #if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE) 2207 case KVM_CAP_PPC_SECURE_GUEST: 2208 r = -EINVAL; 2209 if (!is_kvmppc_hv_enabled(kvm) || !kvm->arch.kvm_ops->enable_svm) 2210 break; 2211 r = kvm->arch.kvm_ops->enable_svm(kvm); 2212 break; 2213 case KVM_CAP_PPC_DAWR1: 2214 r = -EINVAL; 2215 if (!is_kvmppc_hv_enabled(kvm) || !kvm->arch.kvm_ops->enable_dawr1) 2216 break; 2217 r = kvm->arch.kvm_ops->enable_dawr1(kvm); 2218 break; 2219 #endif 2220 default: 2221 r = -EINVAL; 2222 break; 2223 } 2224 2225 return r; 2226 } 2227 2228 #ifdef CONFIG_PPC_BOOK3S_64 2229 /* 2230 * These functions check whether the underlying hardware is safe 2231 * against attacks based on observing the effects of speculatively 2232 * executed instructions, and whether it supplies instructions for 2233 * use in workarounds. The information comes from firmware, either 2234 * via the device tree on powernv platforms or from an hcall on 2235 * pseries platforms. 2236 */ 2237 #ifdef CONFIG_PPC_PSERIES 2238 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp) 2239 { 2240 struct h_cpu_char_result c; 2241 unsigned long rc; 2242 2243 if (!machine_is(pseries)) 2244 return -ENOTTY; 2245 2246 rc = plpar_get_cpu_characteristics(&c); 2247 if (rc == H_SUCCESS) { 2248 cp->character = c.character; 2249 cp->behaviour = c.behaviour; 2250 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 | 2251 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED | 2252 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 | 2253 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 | 2254 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV | 2255 KVM_PPC_CPU_CHAR_BR_HINT_HONOURED | 2256 KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF | 2257 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS | 2258 KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST; 2259 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY | 2260 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR | 2261 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR | 2262 KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE; 2263 } 2264 return 0; 2265 } 2266 #else 2267 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp) 2268 { 2269 return -ENOTTY; 2270 } 2271 #endif 2272 2273 static inline bool have_fw_feat(struct device_node *fw_features, 2274 const char *state, const char *name) 2275 { 2276 struct device_node *np; 2277 bool r = false; 2278 2279 np = of_get_child_by_name(fw_features, name); 2280 if (np) { 2281 r = of_property_read_bool(np, state); 2282 of_node_put(np); 2283 } 2284 return r; 2285 } 2286 2287 static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char *cp) 2288 { 2289 struct device_node *np, *fw_features; 2290 int r; 2291 2292 memset(cp, 0, sizeof(*cp)); 2293 r = pseries_get_cpu_char(cp); 2294 if (r != -ENOTTY) 2295 return r; 2296 2297 np = of_find_node_by_name(NULL, "ibm,opal"); 2298 if (np) { 2299 fw_features = of_get_child_by_name(np, "fw-features"); 2300 of_node_put(np); 2301 if (!fw_features) 2302 return 0; 2303 if (have_fw_feat(fw_features, "enabled", 2304 "inst-spec-barrier-ori31,31,0")) 2305 cp->character |= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31; 2306 if (have_fw_feat(fw_features, "enabled", 2307 "fw-bcctrl-serialized")) 2308 cp->character |= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED; 2309 if (have_fw_feat(fw_features, "enabled", 2310 "inst-l1d-flush-ori30,30,0")) 2311 cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30; 2312 if (have_fw_feat(fw_features, "enabled", 2313 "inst-l1d-flush-trig2")) 2314 cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2; 2315 if (have_fw_feat(fw_features, "enabled", 2316 "fw-l1d-thread-split")) 2317 cp->character |= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV; 2318 if (have_fw_feat(fw_features, "enabled", 2319 "fw-count-cache-disabled")) 2320 cp->character |= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS; 2321 if (have_fw_feat(fw_features, "enabled", 2322 "fw-count-cache-flush-bcctr2,0,0")) 2323 cp->character |= KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST; 2324 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 | 2325 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED | 2326 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 | 2327 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 | 2328 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV | 2329 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS | 2330 KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST; 2331 2332 if (have_fw_feat(fw_features, "enabled", 2333 "speculation-policy-favor-security")) 2334 cp->behaviour |= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY; 2335 if (!have_fw_feat(fw_features, "disabled", 2336 "needs-l1d-flush-msr-pr-0-to-1")) 2337 cp->behaviour |= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR; 2338 if (!have_fw_feat(fw_features, "disabled", 2339 "needs-spec-barrier-for-bound-checks")) 2340 cp->behaviour |= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR; 2341 if (have_fw_feat(fw_features, "enabled", 2342 "needs-count-cache-flush-on-context-switch")) 2343 cp->behaviour |= KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE; 2344 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY | 2345 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR | 2346 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR | 2347 KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE; 2348 2349 of_node_put(fw_features); 2350 } 2351 2352 return 0; 2353 } 2354 #endif 2355 2356 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) 2357 { 2358 struct kvm *kvm __maybe_unused = filp->private_data; 2359 void __user *argp = (void __user *)arg; 2360 int r; 2361 2362 switch (ioctl) { 2363 case KVM_PPC_GET_PVINFO: { 2364 struct kvm_ppc_pvinfo pvinfo; 2365 memset(&pvinfo, 0, sizeof(pvinfo)); 2366 r = kvm_vm_ioctl_get_pvinfo(&pvinfo); 2367 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) { 2368 r = -EFAULT; 2369 goto out; 2370 } 2371 2372 break; 2373 } 2374 #ifdef CONFIG_SPAPR_TCE_IOMMU 2375 case KVM_CREATE_SPAPR_TCE_64: { 2376 struct kvm_create_spapr_tce_64 create_tce_64; 2377 2378 r = -EFAULT; 2379 if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64))) 2380 goto out; 2381 if (create_tce_64.flags) { 2382 r = -EINVAL; 2383 goto out; 2384 } 2385 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64); 2386 goto out; 2387 } 2388 case KVM_CREATE_SPAPR_TCE: { 2389 struct kvm_create_spapr_tce create_tce; 2390 struct kvm_create_spapr_tce_64 create_tce_64; 2391 2392 r = -EFAULT; 2393 if (copy_from_user(&create_tce, argp, sizeof(create_tce))) 2394 goto out; 2395 2396 create_tce_64.liobn = create_tce.liobn; 2397 create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K; 2398 create_tce_64.offset = 0; 2399 create_tce_64.size = create_tce.window_size >> 2400 IOMMU_PAGE_SHIFT_4K; 2401 create_tce_64.flags = 0; 2402 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64); 2403 goto out; 2404 } 2405 #endif 2406 #ifdef CONFIG_PPC_BOOK3S_64 2407 case KVM_PPC_GET_SMMU_INFO: { 2408 struct kvm_ppc_smmu_info info; 2409 struct kvm *kvm = filp->private_data; 2410 2411 memset(&info, 0, sizeof(info)); 2412 r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info); 2413 if (r >= 0 && copy_to_user(argp, &info, sizeof(info))) 2414 r = -EFAULT; 2415 break; 2416 } 2417 case KVM_PPC_RTAS_DEFINE_TOKEN: { 2418 struct kvm *kvm = filp->private_data; 2419 2420 r = kvm_vm_ioctl_rtas_define_token(kvm, argp); 2421 break; 2422 } 2423 case KVM_PPC_CONFIGURE_V3_MMU: { 2424 struct kvm *kvm = filp->private_data; 2425 struct kvm_ppc_mmuv3_cfg cfg; 2426 2427 r = -EINVAL; 2428 if (!kvm->arch.kvm_ops->configure_mmu) 2429 goto out; 2430 r = -EFAULT; 2431 if (copy_from_user(&cfg, argp, sizeof(cfg))) 2432 goto out; 2433 r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg); 2434 break; 2435 } 2436 case KVM_PPC_GET_RMMU_INFO: { 2437 struct kvm *kvm = filp->private_data; 2438 struct kvm_ppc_rmmu_info info; 2439 2440 r = -EINVAL; 2441 if (!kvm->arch.kvm_ops->get_rmmu_info) 2442 goto out; 2443 r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info); 2444 if (r >= 0 && copy_to_user(argp, &info, sizeof(info))) 2445 r = -EFAULT; 2446 break; 2447 } 2448 case KVM_PPC_GET_CPU_CHAR: { 2449 struct kvm_ppc_cpu_char cpuchar; 2450 2451 r = kvmppc_get_cpu_char(&cpuchar); 2452 if (r >= 0 && copy_to_user(argp, &cpuchar, sizeof(cpuchar))) 2453 r = -EFAULT; 2454 break; 2455 } 2456 case KVM_PPC_SVM_OFF: { 2457 struct kvm *kvm = filp->private_data; 2458 2459 r = 0; 2460 if (!kvm->arch.kvm_ops->svm_off) 2461 goto out; 2462 2463 r = kvm->arch.kvm_ops->svm_off(kvm); 2464 break; 2465 } 2466 default: { 2467 struct kvm *kvm = filp->private_data; 2468 r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg); 2469 } 2470 #else /* CONFIG_PPC_BOOK3S_64 */ 2471 default: 2472 r = -ENOTTY; 2473 #endif 2474 } 2475 out: 2476 return r; 2477 } 2478 2479 static DEFINE_IDA(lpid_inuse); 2480 static unsigned long nr_lpids; 2481 2482 long kvmppc_alloc_lpid(void) 2483 { 2484 int lpid; 2485 2486 /* The host LPID must always be 0 (allocation starts at 1) */ 2487 lpid = ida_alloc_range(&lpid_inuse, 1, nr_lpids - 1, GFP_KERNEL); 2488 if (lpid < 0) { 2489 if (lpid == -ENOMEM) 2490 pr_err("%s: Out of memory\n", __func__); 2491 else 2492 pr_err("%s: No LPIDs free\n", __func__); 2493 return -ENOMEM; 2494 } 2495 2496 return lpid; 2497 } 2498 EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid); 2499 2500 void kvmppc_free_lpid(long lpid) 2501 { 2502 ida_free(&lpid_inuse, lpid); 2503 } 2504 EXPORT_SYMBOL_GPL(kvmppc_free_lpid); 2505 2506 /* nr_lpids_param includes the host LPID */ 2507 void kvmppc_init_lpid(unsigned long nr_lpids_param) 2508 { 2509 nr_lpids = nr_lpids_param; 2510 } 2511 EXPORT_SYMBOL_GPL(kvmppc_init_lpid); 2512 2513 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr); 2514 2515 void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry) 2516 { 2517 if (vcpu->kvm->arch.kvm_ops->create_vcpu_debugfs) 2518 vcpu->kvm->arch.kvm_ops->create_vcpu_debugfs(vcpu, debugfs_dentry); 2519 } 2520 2521 void kvm_arch_create_vm_debugfs(struct kvm *kvm) 2522 { 2523 if (kvm->arch.kvm_ops->create_vm_debugfs) 2524 kvm->arch.kvm_ops->create_vm_debugfs(kvm); 2525 } 2526