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 * You should have received a copy of the GNU General Public License 12 * along with this program; if not, write to the Free Software 13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 14 * 15 * Copyright IBM Corp. 2007 16 * 17 * Authors: Hollis Blanchard <hollisb@us.ibm.com> 18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> 19 */ 20 21 #include <linux/errno.h> 22 #include <linux/err.h> 23 #include <linux/kvm_host.h> 24 #include <linux/module.h> 25 #include <linux/vmalloc.h> 26 #include <linux/hrtimer.h> 27 #include <linux/fs.h> 28 #include <linux/slab.h> 29 #include <asm/cputable.h> 30 #include <asm/uaccess.h> 31 #include <asm/kvm_ppc.h> 32 #include <asm/tlbflush.h> 33 #include <asm/cputhreads.h> 34 #include "timing.h" 35 #include "../mm/mmu_decl.h" 36 37 #define CREATE_TRACE_POINTS 38 #include "trace.h" 39 40 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) 41 { 42 #ifndef CONFIG_KVM_BOOK3S_64_HV 43 return !(v->arch.shared->msr & MSR_WE) || 44 !!(v->arch.pending_exceptions); 45 #else 46 return !(v->arch.ceded) || !!(v->arch.pending_exceptions); 47 #endif 48 } 49 50 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu) 51 { 52 int nr = kvmppc_get_gpr(vcpu, 11); 53 int r; 54 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3); 55 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4); 56 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5); 57 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6); 58 unsigned long r2 = 0; 59 60 if (!(vcpu->arch.shared->msr & MSR_SF)) { 61 /* 32 bit mode */ 62 param1 &= 0xffffffff; 63 param2 &= 0xffffffff; 64 param3 &= 0xffffffff; 65 param4 &= 0xffffffff; 66 } 67 68 switch (nr) { 69 case HC_VENDOR_KVM | KVM_HC_PPC_MAP_MAGIC_PAGE: 70 { 71 vcpu->arch.magic_page_pa = param1; 72 vcpu->arch.magic_page_ea = param2; 73 74 r2 = KVM_MAGIC_FEAT_SR; 75 76 r = HC_EV_SUCCESS; 77 break; 78 } 79 case HC_VENDOR_KVM | KVM_HC_FEATURES: 80 r = HC_EV_SUCCESS; 81 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500) 82 /* XXX Missing magic page on 44x */ 83 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE); 84 #endif 85 86 /* Second return value is in r4 */ 87 break; 88 default: 89 r = HC_EV_UNIMPLEMENTED; 90 break; 91 } 92 93 kvmppc_set_gpr(vcpu, 4, r2); 94 95 return r; 96 } 97 98 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu) 99 { 100 enum emulation_result er; 101 int r; 102 103 er = kvmppc_emulate_instruction(run, vcpu); 104 switch (er) { 105 case EMULATE_DONE: 106 /* Future optimization: only reload non-volatiles if they were 107 * actually modified. */ 108 r = RESUME_GUEST_NV; 109 break; 110 case EMULATE_DO_MMIO: 111 run->exit_reason = KVM_EXIT_MMIO; 112 /* We must reload nonvolatiles because "update" load/store 113 * instructions modify register state. */ 114 /* Future optimization: only reload non-volatiles if they were 115 * actually modified. */ 116 r = RESUME_HOST_NV; 117 break; 118 case EMULATE_FAIL: 119 /* XXX Deliver Program interrupt to guest. */ 120 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__, 121 kvmppc_get_last_inst(vcpu)); 122 r = RESUME_HOST; 123 break; 124 default: 125 BUG(); 126 } 127 128 return r; 129 } 130 131 int kvm_arch_hardware_enable(void *garbage) 132 { 133 return 0; 134 } 135 136 void kvm_arch_hardware_disable(void *garbage) 137 { 138 } 139 140 int kvm_arch_hardware_setup(void) 141 { 142 return 0; 143 } 144 145 void kvm_arch_hardware_unsetup(void) 146 { 147 } 148 149 void kvm_arch_check_processor_compat(void *rtn) 150 { 151 *(int *)rtn = kvmppc_core_check_processor_compat(); 152 } 153 154 int kvm_arch_init_vm(struct kvm *kvm) 155 { 156 return kvmppc_core_init_vm(kvm); 157 } 158 159 void kvm_arch_destroy_vm(struct kvm *kvm) 160 { 161 unsigned int i; 162 struct kvm_vcpu *vcpu; 163 164 kvm_for_each_vcpu(i, vcpu, kvm) 165 kvm_arch_vcpu_free(vcpu); 166 167 mutex_lock(&kvm->lock); 168 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 169 kvm->vcpus[i] = NULL; 170 171 atomic_set(&kvm->online_vcpus, 0); 172 173 kvmppc_core_destroy_vm(kvm); 174 175 mutex_unlock(&kvm->lock); 176 } 177 178 void kvm_arch_sync_events(struct kvm *kvm) 179 { 180 } 181 182 int kvm_dev_ioctl_check_extension(long ext) 183 { 184 int r; 185 186 switch (ext) { 187 #ifdef CONFIG_BOOKE 188 case KVM_CAP_PPC_BOOKE_SREGS: 189 #else 190 case KVM_CAP_PPC_SEGSTATE: 191 #endif 192 case KVM_CAP_PPC_UNSET_IRQ: 193 case KVM_CAP_PPC_IRQ_LEVEL: 194 case KVM_CAP_ENABLE_CAP: 195 r = 1; 196 break; 197 #ifndef CONFIG_KVM_BOOK3S_64_HV 198 case KVM_CAP_PPC_PAIRED_SINGLES: 199 case KVM_CAP_PPC_OSI: 200 case KVM_CAP_PPC_GET_PVINFO: 201 r = 1; 202 break; 203 case KVM_CAP_COALESCED_MMIO: 204 r = KVM_COALESCED_MMIO_PAGE_OFFSET; 205 break; 206 #endif 207 #ifdef CONFIG_KVM_BOOK3S_64_HV 208 case KVM_CAP_SPAPR_TCE: 209 r = 1; 210 break; 211 case KVM_CAP_PPC_SMT: 212 r = threads_per_core; 213 break; 214 case KVM_CAP_PPC_RMA: 215 r = 1; 216 break; 217 #endif 218 default: 219 r = 0; 220 break; 221 } 222 return r; 223 224 } 225 226 long kvm_arch_dev_ioctl(struct file *filp, 227 unsigned int ioctl, unsigned long arg) 228 { 229 return -EINVAL; 230 } 231 232 int kvm_arch_prepare_memory_region(struct kvm *kvm, 233 struct kvm_memory_slot *memslot, 234 struct kvm_memory_slot old, 235 struct kvm_userspace_memory_region *mem, 236 int user_alloc) 237 { 238 return kvmppc_core_prepare_memory_region(kvm, mem); 239 } 240 241 void kvm_arch_commit_memory_region(struct kvm *kvm, 242 struct kvm_userspace_memory_region *mem, 243 struct kvm_memory_slot old, 244 int user_alloc) 245 { 246 kvmppc_core_commit_memory_region(kvm, mem); 247 } 248 249 250 void kvm_arch_flush_shadow(struct kvm *kvm) 251 { 252 } 253 254 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) 255 { 256 struct kvm_vcpu *vcpu; 257 vcpu = kvmppc_core_vcpu_create(kvm, id); 258 if (!IS_ERR(vcpu)) 259 kvmppc_create_vcpu_debugfs(vcpu, id); 260 return vcpu; 261 } 262 263 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) 264 { 265 /* Make sure we're not using the vcpu anymore */ 266 hrtimer_cancel(&vcpu->arch.dec_timer); 267 tasklet_kill(&vcpu->arch.tasklet); 268 269 kvmppc_remove_vcpu_debugfs(vcpu); 270 kvmppc_core_vcpu_free(vcpu); 271 } 272 273 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 274 { 275 kvm_arch_vcpu_free(vcpu); 276 } 277 278 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) 279 { 280 return kvmppc_core_pending_dec(vcpu); 281 } 282 283 static void kvmppc_decrementer_func(unsigned long data) 284 { 285 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; 286 287 kvmppc_core_queue_dec(vcpu); 288 289 if (waitqueue_active(&vcpu->wq)) { 290 wake_up_interruptible(&vcpu->wq); 291 vcpu->stat.halt_wakeup++; 292 } 293 } 294 295 /* 296 * low level hrtimer wake routine. Because this runs in hardirq context 297 * we schedule a tasklet to do the real work. 298 */ 299 enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer) 300 { 301 struct kvm_vcpu *vcpu; 302 303 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer); 304 tasklet_schedule(&vcpu->arch.tasklet); 305 306 return HRTIMER_NORESTART; 307 } 308 309 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) 310 { 311 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); 312 tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu); 313 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup; 314 vcpu->arch.dec_expires = ~(u64)0; 315 316 #ifdef CONFIG_KVM_EXIT_TIMING 317 mutex_init(&vcpu->arch.exit_timing_lock); 318 #endif 319 320 return 0; 321 } 322 323 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) 324 { 325 kvmppc_mmu_destroy(vcpu); 326 } 327 328 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 329 { 330 #ifdef CONFIG_BOOKE 331 /* 332 * vrsave (formerly usprg0) isn't used by Linux, but may 333 * be used by the guest. 334 * 335 * On non-booke this is associated with Altivec and 336 * is handled by code in book3s.c. 337 */ 338 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave); 339 #endif 340 kvmppc_core_vcpu_load(vcpu, cpu); 341 vcpu->cpu = smp_processor_id(); 342 } 343 344 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 345 { 346 kvmppc_core_vcpu_put(vcpu); 347 #ifdef CONFIG_BOOKE 348 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE); 349 #endif 350 vcpu->cpu = -1; 351 } 352 353 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 354 struct kvm_guest_debug *dbg) 355 { 356 return -EINVAL; 357 } 358 359 static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu, 360 struct kvm_run *run) 361 { 362 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data); 363 } 364 365 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, 366 struct kvm_run *run) 367 { 368 u64 uninitialized_var(gpr); 369 370 if (run->mmio.len > sizeof(gpr)) { 371 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len); 372 return; 373 } 374 375 if (vcpu->arch.mmio_is_bigendian) { 376 switch (run->mmio.len) { 377 case 8: gpr = *(u64 *)run->mmio.data; break; 378 case 4: gpr = *(u32 *)run->mmio.data; break; 379 case 2: gpr = *(u16 *)run->mmio.data; break; 380 case 1: gpr = *(u8 *)run->mmio.data; break; 381 } 382 } else { 383 /* Convert BE data from userland back to LE. */ 384 switch (run->mmio.len) { 385 case 4: gpr = ld_le32((u32 *)run->mmio.data); break; 386 case 2: gpr = ld_le16((u16 *)run->mmio.data); break; 387 case 1: gpr = *(u8 *)run->mmio.data; break; 388 } 389 } 390 391 if (vcpu->arch.mmio_sign_extend) { 392 switch (run->mmio.len) { 393 #ifdef CONFIG_PPC64 394 case 4: 395 gpr = (s64)(s32)gpr; 396 break; 397 #endif 398 case 2: 399 gpr = (s64)(s16)gpr; 400 break; 401 case 1: 402 gpr = (s64)(s8)gpr; 403 break; 404 } 405 } 406 407 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); 408 409 switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) { 410 case KVM_REG_GPR: 411 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); 412 break; 413 case KVM_REG_FPR: 414 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; 415 break; 416 #ifdef CONFIG_PPC_BOOK3S 417 case KVM_REG_QPR: 418 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; 419 break; 420 case KVM_REG_FQPR: 421 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; 422 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; 423 break; 424 #endif 425 default: 426 BUG(); 427 } 428 } 429 430 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, 431 unsigned int rt, unsigned int bytes, int is_bigendian) 432 { 433 if (bytes > sizeof(run->mmio.data)) { 434 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, 435 run->mmio.len); 436 } 437 438 run->mmio.phys_addr = vcpu->arch.paddr_accessed; 439 run->mmio.len = bytes; 440 run->mmio.is_write = 0; 441 442 vcpu->arch.io_gpr = rt; 443 vcpu->arch.mmio_is_bigendian = is_bigendian; 444 vcpu->mmio_needed = 1; 445 vcpu->mmio_is_write = 0; 446 vcpu->arch.mmio_sign_extend = 0; 447 448 return EMULATE_DO_MMIO; 449 } 450 451 /* Same as above, but sign extends */ 452 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu, 453 unsigned int rt, unsigned int bytes, int is_bigendian) 454 { 455 int r; 456 457 r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian); 458 vcpu->arch.mmio_sign_extend = 1; 459 460 return r; 461 } 462 463 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, 464 u64 val, unsigned int bytes, int is_bigendian) 465 { 466 void *data = run->mmio.data; 467 468 if (bytes > sizeof(run->mmio.data)) { 469 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, 470 run->mmio.len); 471 } 472 473 run->mmio.phys_addr = vcpu->arch.paddr_accessed; 474 run->mmio.len = bytes; 475 run->mmio.is_write = 1; 476 vcpu->mmio_needed = 1; 477 vcpu->mmio_is_write = 1; 478 479 /* Store the value at the lowest bytes in 'data'. */ 480 if (is_bigendian) { 481 switch (bytes) { 482 case 8: *(u64 *)data = val; break; 483 case 4: *(u32 *)data = val; break; 484 case 2: *(u16 *)data = val; break; 485 case 1: *(u8 *)data = val; break; 486 } 487 } else { 488 /* Store LE value into 'data'. */ 489 switch (bytes) { 490 case 4: st_le32(data, val); break; 491 case 2: st_le16(data, val); break; 492 case 1: *(u8 *)data = val; break; 493 } 494 } 495 496 return EMULATE_DO_MMIO; 497 } 498 499 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) 500 { 501 int r; 502 sigset_t sigsaved; 503 504 if (vcpu->sigset_active) 505 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); 506 507 if (vcpu->mmio_needed) { 508 if (!vcpu->mmio_is_write) 509 kvmppc_complete_mmio_load(vcpu, run); 510 vcpu->mmio_needed = 0; 511 } else if (vcpu->arch.dcr_needed) { 512 if (!vcpu->arch.dcr_is_write) 513 kvmppc_complete_dcr_load(vcpu, run); 514 vcpu->arch.dcr_needed = 0; 515 } else if (vcpu->arch.osi_needed) { 516 u64 *gprs = run->osi.gprs; 517 int i; 518 519 for (i = 0; i < 32; i++) 520 kvmppc_set_gpr(vcpu, i, gprs[i]); 521 vcpu->arch.osi_needed = 0; 522 } else if (vcpu->arch.hcall_needed) { 523 int i; 524 525 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret); 526 for (i = 0; i < 9; ++i) 527 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]); 528 vcpu->arch.hcall_needed = 0; 529 } 530 531 kvmppc_core_deliver_interrupts(vcpu); 532 533 r = kvmppc_vcpu_run(run, vcpu); 534 535 if (vcpu->sigset_active) 536 sigprocmask(SIG_SETMASK, &sigsaved, NULL); 537 538 return r; 539 } 540 541 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) 542 { 543 if (irq->irq == KVM_INTERRUPT_UNSET) 544 kvmppc_core_dequeue_external(vcpu, irq); 545 else 546 kvmppc_core_queue_external(vcpu, irq); 547 548 if (waitqueue_active(&vcpu->wq)) { 549 wake_up_interruptible(&vcpu->wq); 550 vcpu->stat.halt_wakeup++; 551 } else if (vcpu->cpu != -1) { 552 smp_send_reschedule(vcpu->cpu); 553 } 554 555 return 0; 556 } 557 558 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 559 struct kvm_enable_cap *cap) 560 { 561 int r; 562 563 if (cap->flags) 564 return -EINVAL; 565 566 switch (cap->cap) { 567 case KVM_CAP_PPC_OSI: 568 r = 0; 569 vcpu->arch.osi_enabled = true; 570 break; 571 default: 572 r = -EINVAL; 573 break; 574 } 575 576 return r; 577 } 578 579 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 580 struct kvm_mp_state *mp_state) 581 { 582 return -EINVAL; 583 } 584 585 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 586 struct kvm_mp_state *mp_state) 587 { 588 return -EINVAL; 589 } 590 591 long kvm_arch_vcpu_ioctl(struct file *filp, 592 unsigned int ioctl, unsigned long arg) 593 { 594 struct kvm_vcpu *vcpu = filp->private_data; 595 void __user *argp = (void __user *)arg; 596 long r; 597 598 switch (ioctl) { 599 case KVM_INTERRUPT: { 600 struct kvm_interrupt irq; 601 r = -EFAULT; 602 if (copy_from_user(&irq, argp, sizeof(irq))) 603 goto out; 604 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); 605 goto out; 606 } 607 608 case KVM_ENABLE_CAP: 609 { 610 struct kvm_enable_cap cap; 611 r = -EFAULT; 612 if (copy_from_user(&cap, argp, sizeof(cap))) 613 goto out; 614 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 615 break; 616 } 617 default: 618 r = -EINVAL; 619 } 620 621 out: 622 return r; 623 } 624 625 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo) 626 { 627 u32 inst_lis = 0x3c000000; 628 u32 inst_ori = 0x60000000; 629 u32 inst_nop = 0x60000000; 630 u32 inst_sc = 0x44000002; 631 u32 inst_imm_mask = 0xffff; 632 633 /* 634 * The hypercall to get into KVM from within guest context is as 635 * follows: 636 * 637 * lis r0, r0, KVM_SC_MAGIC_R0@h 638 * ori r0, KVM_SC_MAGIC_R0@l 639 * sc 640 * nop 641 */ 642 pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask); 643 pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask); 644 pvinfo->hcall[2] = inst_sc; 645 pvinfo->hcall[3] = inst_nop; 646 647 return 0; 648 } 649 650 long kvm_arch_vm_ioctl(struct file *filp, 651 unsigned int ioctl, unsigned long arg) 652 { 653 void __user *argp = (void __user *)arg; 654 long r; 655 656 switch (ioctl) { 657 case KVM_PPC_GET_PVINFO: { 658 struct kvm_ppc_pvinfo pvinfo; 659 memset(&pvinfo, 0, sizeof(pvinfo)); 660 r = kvm_vm_ioctl_get_pvinfo(&pvinfo); 661 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) { 662 r = -EFAULT; 663 goto out; 664 } 665 666 break; 667 } 668 #ifdef CONFIG_KVM_BOOK3S_64_HV 669 case KVM_CREATE_SPAPR_TCE: { 670 struct kvm_create_spapr_tce create_tce; 671 struct kvm *kvm = filp->private_data; 672 673 r = -EFAULT; 674 if (copy_from_user(&create_tce, argp, sizeof(create_tce))) 675 goto out; 676 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce); 677 goto out; 678 } 679 680 case KVM_ALLOCATE_RMA: { 681 struct kvm *kvm = filp->private_data; 682 struct kvm_allocate_rma rma; 683 684 r = kvm_vm_ioctl_allocate_rma(kvm, &rma); 685 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma))) 686 r = -EFAULT; 687 break; 688 } 689 #endif /* CONFIG_KVM_BOOK3S_64_HV */ 690 691 default: 692 r = -ENOTTY; 693 } 694 695 out: 696 return r; 697 } 698 699 int kvm_arch_init(void *opaque) 700 { 701 return 0; 702 } 703 704 void kvm_arch_exit(void) 705 { 706 } 707