1 /* 2 * This file is subject to the terms and conditions of the GNU General Public 3 * License. See the file "COPYING" in the main directory of this archive 4 * for more details. 5 * 6 * KVM/MIPS: MIPS specific KVM APIs 7 * 8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. 9 * Authors: Sanjay Lal <sanjayl@kymasys.com> 10 */ 11 12 #include <linux/bitops.h> 13 #include <linux/errno.h> 14 #include <linux/err.h> 15 #include <linux/kdebug.h> 16 #include <linux/module.h> 17 #include <linux/uaccess.h> 18 #include <linux/vmalloc.h> 19 #include <linux/sched/signal.h> 20 #include <linux/fs.h> 21 #include <linux/memblock.h> 22 #include <linux/pgtable.h> 23 24 #include <asm/fpu.h> 25 #include <asm/page.h> 26 #include <asm/cacheflush.h> 27 #include <asm/mmu_context.h> 28 #include <asm/pgalloc.h> 29 30 #include <linux/kvm_host.h> 31 32 #include "interrupt.h" 33 #include "commpage.h" 34 35 #define CREATE_TRACE_POINTS 36 #include "trace.h" 37 38 #ifndef VECTORSPACING 39 #define VECTORSPACING 0x100 /* for EI/VI mode */ 40 #endif 41 42 struct kvm_stats_debugfs_item debugfs_entries[] = { 43 VCPU_STAT("wait", wait_exits), 44 VCPU_STAT("cache", cache_exits), 45 VCPU_STAT("signal", signal_exits), 46 VCPU_STAT("interrupt", int_exits), 47 VCPU_STAT("cop_unusable", cop_unusable_exits), 48 VCPU_STAT("tlbmod", tlbmod_exits), 49 VCPU_STAT("tlbmiss_ld", tlbmiss_ld_exits), 50 VCPU_STAT("tlbmiss_st", tlbmiss_st_exits), 51 VCPU_STAT("addrerr_st", addrerr_st_exits), 52 VCPU_STAT("addrerr_ld", addrerr_ld_exits), 53 VCPU_STAT("syscall", syscall_exits), 54 VCPU_STAT("resvd_inst", resvd_inst_exits), 55 VCPU_STAT("break_inst", break_inst_exits), 56 VCPU_STAT("trap_inst", trap_inst_exits), 57 VCPU_STAT("msa_fpe", msa_fpe_exits), 58 VCPU_STAT("fpe", fpe_exits), 59 VCPU_STAT("msa_disabled", msa_disabled_exits), 60 VCPU_STAT("flush_dcache", flush_dcache_exits), 61 #ifdef CONFIG_KVM_MIPS_VZ 62 VCPU_STAT("vz_gpsi", vz_gpsi_exits), 63 VCPU_STAT("vz_gsfc", vz_gsfc_exits), 64 VCPU_STAT("vz_hc", vz_hc_exits), 65 VCPU_STAT("vz_grr", vz_grr_exits), 66 VCPU_STAT("vz_gva", vz_gva_exits), 67 VCPU_STAT("vz_ghfc", vz_ghfc_exits), 68 VCPU_STAT("vz_gpa", vz_gpa_exits), 69 VCPU_STAT("vz_resvd", vz_resvd_exits), 70 #ifdef CONFIG_CPU_LOONGSON64 71 VCPU_STAT("vz_cpucfg", vz_cpucfg_exits), 72 #endif 73 #endif 74 VCPU_STAT("halt_successful_poll", halt_successful_poll), 75 VCPU_STAT("halt_attempted_poll", halt_attempted_poll), 76 VCPU_STAT("halt_poll_invalid", halt_poll_invalid), 77 VCPU_STAT("halt_wakeup", halt_wakeup), 78 VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns), 79 VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns), 80 {NULL} 81 }; 82 83 bool kvm_trace_guest_mode_change; 84 85 int kvm_guest_mode_change_trace_reg(void) 86 { 87 kvm_trace_guest_mode_change = true; 88 return 0; 89 } 90 91 void kvm_guest_mode_change_trace_unreg(void) 92 { 93 kvm_trace_guest_mode_change = false; 94 } 95 96 /* 97 * XXXKYMA: We are simulatoring a processor that has the WII bit set in 98 * Config7, so we are "runnable" if interrupts are pending 99 */ 100 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 101 { 102 return !!(vcpu->arch.pending_exceptions); 103 } 104 105 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 106 { 107 return false; 108 } 109 110 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 111 { 112 return 1; 113 } 114 115 int kvm_arch_hardware_enable(void) 116 { 117 return kvm_mips_callbacks->hardware_enable(); 118 } 119 120 void kvm_arch_hardware_disable(void) 121 { 122 kvm_mips_callbacks->hardware_disable(); 123 } 124 125 int kvm_arch_hardware_setup(void *opaque) 126 { 127 return 0; 128 } 129 130 int kvm_arch_check_processor_compat(void *opaque) 131 { 132 return 0; 133 } 134 135 extern void kvm_init_loongson_ipi(struct kvm *kvm); 136 137 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 138 { 139 switch (type) { 140 #ifdef CONFIG_KVM_MIPS_VZ 141 case KVM_VM_MIPS_VZ: 142 #else 143 case KVM_VM_MIPS_TE: 144 #endif 145 break; 146 default: 147 /* Unsupported KVM type */ 148 return -EINVAL; 149 }; 150 151 /* Allocate page table to map GPA -> RPA */ 152 kvm->arch.gpa_mm.pgd = kvm_pgd_alloc(); 153 if (!kvm->arch.gpa_mm.pgd) 154 return -ENOMEM; 155 156 #ifdef CONFIG_CPU_LOONGSON64 157 kvm_init_loongson_ipi(kvm); 158 #endif 159 160 return 0; 161 } 162 163 void kvm_mips_free_vcpus(struct kvm *kvm) 164 { 165 unsigned int i; 166 struct kvm_vcpu *vcpu; 167 168 kvm_for_each_vcpu(i, vcpu, kvm) { 169 kvm_vcpu_destroy(vcpu); 170 } 171 172 mutex_lock(&kvm->lock); 173 174 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 175 kvm->vcpus[i] = NULL; 176 177 atomic_set(&kvm->online_vcpus, 0); 178 179 mutex_unlock(&kvm->lock); 180 } 181 182 static void kvm_mips_free_gpa_pt(struct kvm *kvm) 183 { 184 /* It should always be safe to remove after flushing the whole range */ 185 WARN_ON(!kvm_mips_flush_gpa_pt(kvm, 0, ~0)); 186 pgd_free(NULL, kvm->arch.gpa_mm.pgd); 187 } 188 189 void kvm_arch_destroy_vm(struct kvm *kvm) 190 { 191 kvm_mips_free_vcpus(kvm); 192 kvm_mips_free_gpa_pt(kvm); 193 } 194 195 long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, 196 unsigned long arg) 197 { 198 return -ENOIOCTLCMD; 199 } 200 201 void kvm_arch_flush_shadow_all(struct kvm *kvm) 202 { 203 /* Flush whole GPA */ 204 kvm_mips_flush_gpa_pt(kvm, 0, ~0); 205 206 /* Let implementation do the rest */ 207 kvm_mips_callbacks->flush_shadow_all(kvm); 208 } 209 210 void kvm_arch_flush_shadow_memslot(struct kvm *kvm, 211 struct kvm_memory_slot *slot) 212 { 213 /* 214 * The slot has been made invalid (ready for moving or deletion), so we 215 * need to ensure that it can no longer be accessed by any guest VCPUs. 216 */ 217 218 spin_lock(&kvm->mmu_lock); 219 /* Flush slot from GPA */ 220 kvm_mips_flush_gpa_pt(kvm, slot->base_gfn, 221 slot->base_gfn + slot->npages - 1); 222 /* Let implementation do the rest */ 223 kvm_mips_callbacks->flush_shadow_memslot(kvm, slot); 224 spin_unlock(&kvm->mmu_lock); 225 } 226 227 int kvm_arch_prepare_memory_region(struct kvm *kvm, 228 struct kvm_memory_slot *memslot, 229 const struct kvm_userspace_memory_region *mem, 230 enum kvm_mr_change change) 231 { 232 return 0; 233 } 234 235 void kvm_arch_commit_memory_region(struct kvm *kvm, 236 const struct kvm_userspace_memory_region *mem, 237 struct kvm_memory_slot *old, 238 const struct kvm_memory_slot *new, 239 enum kvm_mr_change change) 240 { 241 int needs_flush; 242 243 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n", 244 __func__, kvm, mem->slot, mem->guest_phys_addr, 245 mem->memory_size, mem->userspace_addr); 246 247 /* 248 * If dirty page logging is enabled, write protect all pages in the slot 249 * ready for dirty logging. 250 * 251 * There is no need to do this in any of the following cases: 252 * CREATE: No dirty mappings will already exist. 253 * MOVE/DELETE: The old mappings will already have been cleaned up by 254 * kvm_arch_flush_shadow_memslot() 255 */ 256 if (change == KVM_MR_FLAGS_ONLY && 257 (!(old->flags & KVM_MEM_LOG_DIRTY_PAGES) && 258 new->flags & KVM_MEM_LOG_DIRTY_PAGES)) { 259 spin_lock(&kvm->mmu_lock); 260 /* Write protect GPA page table entries */ 261 needs_flush = kvm_mips_mkclean_gpa_pt(kvm, new->base_gfn, 262 new->base_gfn + new->npages - 1); 263 /* Let implementation do the rest */ 264 if (needs_flush) 265 kvm_mips_callbacks->flush_shadow_memslot(kvm, new); 266 spin_unlock(&kvm->mmu_lock); 267 } 268 } 269 270 static inline void dump_handler(const char *symbol, void *start, void *end) 271 { 272 u32 *p; 273 274 pr_debug("LEAF(%s)\n", symbol); 275 276 pr_debug("\t.set push\n"); 277 pr_debug("\t.set noreorder\n"); 278 279 for (p = start; p < (u32 *)end; ++p) 280 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p); 281 282 pr_debug("\t.set\tpop\n"); 283 284 pr_debug("\tEND(%s)\n", symbol); 285 } 286 287 /* low level hrtimer wake routine */ 288 static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer) 289 { 290 struct kvm_vcpu *vcpu; 291 292 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer); 293 294 kvm_mips_callbacks->queue_timer_int(vcpu); 295 296 vcpu->arch.wait = 0; 297 rcuwait_wake_up(&vcpu->wait); 298 299 return kvm_mips_count_timeout(vcpu); 300 } 301 302 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 303 { 304 return 0; 305 } 306 307 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 308 { 309 int err, size; 310 void *gebase, *p, *handler, *refill_start, *refill_end; 311 int i; 312 313 kvm_debug("kvm @ %p: create cpu %d at %p\n", 314 vcpu->kvm, vcpu->vcpu_id, vcpu); 315 316 err = kvm_mips_callbacks->vcpu_init(vcpu); 317 if (err) 318 return err; 319 320 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC, 321 HRTIMER_MODE_REL); 322 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup; 323 324 /* 325 * Allocate space for host mode exception handlers that handle 326 * guest mode exits 327 */ 328 if (cpu_has_veic || cpu_has_vint) 329 size = 0x200 + VECTORSPACING * 64; 330 else 331 size = 0x4000; 332 333 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL); 334 335 if (!gebase) { 336 err = -ENOMEM; 337 goto out_uninit_vcpu; 338 } 339 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n", 340 ALIGN(size, PAGE_SIZE), gebase); 341 342 /* 343 * Check new ebase actually fits in CP0_EBase. The lack of a write gate 344 * limits us to the low 512MB of physical address space. If the memory 345 * we allocate is out of range, just give up now. 346 */ 347 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) { 348 kvm_err("CP0_EBase.WG required for guest exception base %pK\n", 349 gebase); 350 err = -ENOMEM; 351 goto out_free_gebase; 352 } 353 354 /* Save new ebase */ 355 vcpu->arch.guest_ebase = gebase; 356 357 /* Build guest exception vectors dynamically in unmapped memory */ 358 handler = gebase + 0x2000; 359 360 /* TLB refill (or XTLB refill on 64-bit VZ where KX=1) */ 361 refill_start = gebase; 362 if (IS_ENABLED(CONFIG_KVM_MIPS_VZ) && IS_ENABLED(CONFIG_64BIT)) 363 refill_start += 0x080; 364 refill_end = kvm_mips_build_tlb_refill_exception(refill_start, handler); 365 366 /* General Exception Entry point */ 367 kvm_mips_build_exception(gebase + 0x180, handler); 368 369 /* For vectored interrupts poke the exception code @ all offsets 0-7 */ 370 for (i = 0; i < 8; i++) { 371 kvm_debug("L1 Vectored handler @ %p\n", 372 gebase + 0x200 + (i * VECTORSPACING)); 373 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING, 374 handler); 375 } 376 377 /* General exit handler */ 378 p = handler; 379 p = kvm_mips_build_exit(p); 380 381 /* Guest entry routine */ 382 vcpu->arch.vcpu_run = p; 383 p = kvm_mips_build_vcpu_run(p); 384 385 /* Dump the generated code */ 386 pr_debug("#include <asm/asm.h>\n"); 387 pr_debug("#include <asm/regdef.h>\n"); 388 pr_debug("\n"); 389 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p); 390 dump_handler("kvm_tlb_refill", refill_start, refill_end); 391 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200); 392 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run); 393 394 /* Invalidate the icache for these ranges */ 395 flush_icache_range((unsigned long)gebase, 396 (unsigned long)gebase + ALIGN(size, PAGE_SIZE)); 397 398 /* 399 * Allocate comm page for guest kernel, a TLB will be reserved for 400 * mapping GVA @ 0xFFFF8000 to this page 401 */ 402 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL); 403 404 if (!vcpu->arch.kseg0_commpage) { 405 err = -ENOMEM; 406 goto out_free_gebase; 407 } 408 409 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage); 410 kvm_mips_commpage_init(vcpu); 411 412 /* Init */ 413 vcpu->arch.last_sched_cpu = -1; 414 vcpu->arch.last_exec_cpu = -1; 415 416 /* Initial guest state */ 417 err = kvm_mips_callbacks->vcpu_setup(vcpu); 418 if (err) 419 goto out_free_commpage; 420 421 return 0; 422 423 out_free_commpage: 424 kfree(vcpu->arch.kseg0_commpage); 425 out_free_gebase: 426 kfree(gebase); 427 out_uninit_vcpu: 428 kvm_mips_callbacks->vcpu_uninit(vcpu); 429 return err; 430 } 431 432 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 433 { 434 hrtimer_cancel(&vcpu->arch.comparecount_timer); 435 436 kvm_mips_dump_stats(vcpu); 437 438 kvm_mmu_free_memory_caches(vcpu); 439 kfree(vcpu->arch.guest_ebase); 440 kfree(vcpu->arch.kseg0_commpage); 441 442 kvm_mips_callbacks->vcpu_uninit(vcpu); 443 } 444 445 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 446 struct kvm_guest_debug *dbg) 447 { 448 return -ENOIOCTLCMD; 449 } 450 451 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 452 { 453 int r = -EINTR; 454 455 vcpu_load(vcpu); 456 457 kvm_sigset_activate(vcpu); 458 459 if (vcpu->mmio_needed) { 460 if (!vcpu->mmio_is_write) 461 kvm_mips_complete_mmio_load(vcpu); 462 vcpu->mmio_needed = 0; 463 } 464 465 if (vcpu->run->immediate_exit) 466 goto out; 467 468 lose_fpu(1); 469 470 local_irq_disable(); 471 guest_enter_irqoff(); 472 trace_kvm_enter(vcpu); 473 474 /* 475 * Make sure the read of VCPU requests in vcpu_run() callback is not 476 * reordered ahead of the write to vcpu->mode, or we could miss a TLB 477 * flush request while the requester sees the VCPU as outside of guest 478 * mode and not needing an IPI. 479 */ 480 smp_store_mb(vcpu->mode, IN_GUEST_MODE); 481 482 r = kvm_mips_callbacks->vcpu_run(vcpu); 483 484 trace_kvm_out(vcpu); 485 guest_exit_irqoff(); 486 local_irq_enable(); 487 488 out: 489 kvm_sigset_deactivate(vcpu); 490 491 vcpu_put(vcpu); 492 return r; 493 } 494 495 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, 496 struct kvm_mips_interrupt *irq) 497 { 498 int intr = (int)irq->irq; 499 struct kvm_vcpu *dvcpu = NULL; 500 501 if (intr == kvm_priority_to_irq[MIPS_EXC_INT_IPI_1] || 502 intr == kvm_priority_to_irq[MIPS_EXC_INT_IPI_2] || 503 intr == (-kvm_priority_to_irq[MIPS_EXC_INT_IPI_1]) || 504 intr == (-kvm_priority_to_irq[MIPS_EXC_INT_IPI_2])) 505 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu, 506 (int)intr); 507 508 if (irq->cpu == -1) 509 dvcpu = vcpu; 510 else 511 dvcpu = vcpu->kvm->vcpus[irq->cpu]; 512 513 if (intr == 2 || intr == 3 || intr == 4 || intr == 6) { 514 kvm_mips_callbacks->queue_io_int(dvcpu, irq); 515 516 } else if (intr == -2 || intr == -3 || intr == -4 || intr == -6) { 517 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq); 518 } else { 519 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__, 520 irq->cpu, irq->irq); 521 return -EINVAL; 522 } 523 524 dvcpu->arch.wait = 0; 525 526 rcuwait_wake_up(&dvcpu->wait); 527 528 return 0; 529 } 530 531 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 532 struct kvm_mp_state *mp_state) 533 { 534 return -ENOIOCTLCMD; 535 } 536 537 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 538 struct kvm_mp_state *mp_state) 539 { 540 return -ENOIOCTLCMD; 541 } 542 543 static u64 kvm_mips_get_one_regs[] = { 544 KVM_REG_MIPS_R0, 545 KVM_REG_MIPS_R1, 546 KVM_REG_MIPS_R2, 547 KVM_REG_MIPS_R3, 548 KVM_REG_MIPS_R4, 549 KVM_REG_MIPS_R5, 550 KVM_REG_MIPS_R6, 551 KVM_REG_MIPS_R7, 552 KVM_REG_MIPS_R8, 553 KVM_REG_MIPS_R9, 554 KVM_REG_MIPS_R10, 555 KVM_REG_MIPS_R11, 556 KVM_REG_MIPS_R12, 557 KVM_REG_MIPS_R13, 558 KVM_REG_MIPS_R14, 559 KVM_REG_MIPS_R15, 560 KVM_REG_MIPS_R16, 561 KVM_REG_MIPS_R17, 562 KVM_REG_MIPS_R18, 563 KVM_REG_MIPS_R19, 564 KVM_REG_MIPS_R20, 565 KVM_REG_MIPS_R21, 566 KVM_REG_MIPS_R22, 567 KVM_REG_MIPS_R23, 568 KVM_REG_MIPS_R24, 569 KVM_REG_MIPS_R25, 570 KVM_REG_MIPS_R26, 571 KVM_REG_MIPS_R27, 572 KVM_REG_MIPS_R28, 573 KVM_REG_MIPS_R29, 574 KVM_REG_MIPS_R30, 575 KVM_REG_MIPS_R31, 576 577 #ifndef CONFIG_CPU_MIPSR6 578 KVM_REG_MIPS_HI, 579 KVM_REG_MIPS_LO, 580 #endif 581 KVM_REG_MIPS_PC, 582 }; 583 584 static u64 kvm_mips_get_one_regs_fpu[] = { 585 KVM_REG_MIPS_FCR_IR, 586 KVM_REG_MIPS_FCR_CSR, 587 }; 588 589 static u64 kvm_mips_get_one_regs_msa[] = { 590 KVM_REG_MIPS_MSA_IR, 591 KVM_REG_MIPS_MSA_CSR, 592 }; 593 594 static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu) 595 { 596 unsigned long ret; 597 598 ret = ARRAY_SIZE(kvm_mips_get_one_regs); 599 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) { 600 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48; 601 /* odd doubles */ 602 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64) 603 ret += 16; 604 } 605 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) 606 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32; 607 ret += kvm_mips_callbacks->num_regs(vcpu); 608 609 return ret; 610 } 611 612 static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices) 613 { 614 u64 index; 615 unsigned int i; 616 617 if (copy_to_user(indices, kvm_mips_get_one_regs, 618 sizeof(kvm_mips_get_one_regs))) 619 return -EFAULT; 620 indices += ARRAY_SIZE(kvm_mips_get_one_regs); 621 622 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) { 623 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu, 624 sizeof(kvm_mips_get_one_regs_fpu))) 625 return -EFAULT; 626 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu); 627 628 for (i = 0; i < 32; ++i) { 629 index = KVM_REG_MIPS_FPR_32(i); 630 if (copy_to_user(indices, &index, sizeof(index))) 631 return -EFAULT; 632 ++indices; 633 634 /* skip odd doubles if no F64 */ 635 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64)) 636 continue; 637 638 index = KVM_REG_MIPS_FPR_64(i); 639 if (copy_to_user(indices, &index, sizeof(index))) 640 return -EFAULT; 641 ++indices; 642 } 643 } 644 645 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) { 646 if (copy_to_user(indices, kvm_mips_get_one_regs_msa, 647 sizeof(kvm_mips_get_one_regs_msa))) 648 return -EFAULT; 649 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa); 650 651 for (i = 0; i < 32; ++i) { 652 index = KVM_REG_MIPS_VEC_128(i); 653 if (copy_to_user(indices, &index, sizeof(index))) 654 return -EFAULT; 655 ++indices; 656 } 657 } 658 659 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices); 660 } 661 662 static int kvm_mips_get_reg(struct kvm_vcpu *vcpu, 663 const struct kvm_one_reg *reg) 664 { 665 struct mips_coproc *cop0 = vcpu->arch.cop0; 666 struct mips_fpu_struct *fpu = &vcpu->arch.fpu; 667 int ret; 668 s64 v; 669 s64 vs[2]; 670 unsigned int idx; 671 672 switch (reg->id) { 673 /* General purpose registers */ 674 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31: 675 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0]; 676 break; 677 #ifndef CONFIG_CPU_MIPSR6 678 case KVM_REG_MIPS_HI: 679 v = (long)vcpu->arch.hi; 680 break; 681 case KVM_REG_MIPS_LO: 682 v = (long)vcpu->arch.lo; 683 break; 684 #endif 685 case KVM_REG_MIPS_PC: 686 v = (long)vcpu->arch.pc; 687 break; 688 689 /* Floating point registers */ 690 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31): 691 if (!kvm_mips_guest_has_fpu(&vcpu->arch)) 692 return -EINVAL; 693 idx = reg->id - KVM_REG_MIPS_FPR_32(0); 694 /* Odd singles in top of even double when FR=0 */ 695 if (kvm_read_c0_guest_status(cop0) & ST0_FR) 696 v = get_fpr32(&fpu->fpr[idx], 0); 697 else 698 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1); 699 break; 700 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31): 701 if (!kvm_mips_guest_has_fpu(&vcpu->arch)) 702 return -EINVAL; 703 idx = reg->id - KVM_REG_MIPS_FPR_64(0); 704 /* Can't access odd doubles in FR=0 mode */ 705 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR)) 706 return -EINVAL; 707 v = get_fpr64(&fpu->fpr[idx], 0); 708 break; 709 case KVM_REG_MIPS_FCR_IR: 710 if (!kvm_mips_guest_has_fpu(&vcpu->arch)) 711 return -EINVAL; 712 v = boot_cpu_data.fpu_id; 713 break; 714 case KVM_REG_MIPS_FCR_CSR: 715 if (!kvm_mips_guest_has_fpu(&vcpu->arch)) 716 return -EINVAL; 717 v = fpu->fcr31; 718 break; 719 720 /* MIPS SIMD Architecture (MSA) registers */ 721 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31): 722 if (!kvm_mips_guest_has_msa(&vcpu->arch)) 723 return -EINVAL; 724 /* Can't access MSA registers in FR=0 mode */ 725 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR)) 726 return -EINVAL; 727 idx = reg->id - KVM_REG_MIPS_VEC_128(0); 728 #ifdef CONFIG_CPU_LITTLE_ENDIAN 729 /* least significant byte first */ 730 vs[0] = get_fpr64(&fpu->fpr[idx], 0); 731 vs[1] = get_fpr64(&fpu->fpr[idx], 1); 732 #else 733 /* most significant byte first */ 734 vs[0] = get_fpr64(&fpu->fpr[idx], 1); 735 vs[1] = get_fpr64(&fpu->fpr[idx], 0); 736 #endif 737 break; 738 case KVM_REG_MIPS_MSA_IR: 739 if (!kvm_mips_guest_has_msa(&vcpu->arch)) 740 return -EINVAL; 741 v = boot_cpu_data.msa_id; 742 break; 743 case KVM_REG_MIPS_MSA_CSR: 744 if (!kvm_mips_guest_has_msa(&vcpu->arch)) 745 return -EINVAL; 746 v = fpu->msacsr; 747 break; 748 749 /* registers to be handled specially */ 750 default: 751 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v); 752 if (ret) 753 return ret; 754 break; 755 } 756 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) { 757 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr; 758 759 return put_user(v, uaddr64); 760 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) { 761 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr; 762 u32 v32 = (u32)v; 763 764 return put_user(v32, uaddr32); 765 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) { 766 void __user *uaddr = (void __user *)(long)reg->addr; 767 768 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0; 769 } else { 770 return -EINVAL; 771 } 772 } 773 774 static int kvm_mips_set_reg(struct kvm_vcpu *vcpu, 775 const struct kvm_one_reg *reg) 776 { 777 struct mips_coproc *cop0 = vcpu->arch.cop0; 778 struct mips_fpu_struct *fpu = &vcpu->arch.fpu; 779 s64 v; 780 s64 vs[2]; 781 unsigned int idx; 782 783 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) { 784 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr; 785 786 if (get_user(v, uaddr64) != 0) 787 return -EFAULT; 788 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) { 789 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr; 790 s32 v32; 791 792 if (get_user(v32, uaddr32) != 0) 793 return -EFAULT; 794 v = (s64)v32; 795 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) { 796 void __user *uaddr = (void __user *)(long)reg->addr; 797 798 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0; 799 } else { 800 return -EINVAL; 801 } 802 803 switch (reg->id) { 804 /* General purpose registers */ 805 case KVM_REG_MIPS_R0: 806 /* Silently ignore requests to set $0 */ 807 break; 808 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31: 809 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v; 810 break; 811 #ifndef CONFIG_CPU_MIPSR6 812 case KVM_REG_MIPS_HI: 813 vcpu->arch.hi = v; 814 break; 815 case KVM_REG_MIPS_LO: 816 vcpu->arch.lo = v; 817 break; 818 #endif 819 case KVM_REG_MIPS_PC: 820 vcpu->arch.pc = v; 821 break; 822 823 /* Floating point registers */ 824 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31): 825 if (!kvm_mips_guest_has_fpu(&vcpu->arch)) 826 return -EINVAL; 827 idx = reg->id - KVM_REG_MIPS_FPR_32(0); 828 /* Odd singles in top of even double when FR=0 */ 829 if (kvm_read_c0_guest_status(cop0) & ST0_FR) 830 set_fpr32(&fpu->fpr[idx], 0, v); 831 else 832 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v); 833 break; 834 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31): 835 if (!kvm_mips_guest_has_fpu(&vcpu->arch)) 836 return -EINVAL; 837 idx = reg->id - KVM_REG_MIPS_FPR_64(0); 838 /* Can't access odd doubles in FR=0 mode */ 839 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR)) 840 return -EINVAL; 841 set_fpr64(&fpu->fpr[idx], 0, v); 842 break; 843 case KVM_REG_MIPS_FCR_IR: 844 if (!kvm_mips_guest_has_fpu(&vcpu->arch)) 845 return -EINVAL; 846 /* Read-only */ 847 break; 848 case KVM_REG_MIPS_FCR_CSR: 849 if (!kvm_mips_guest_has_fpu(&vcpu->arch)) 850 return -EINVAL; 851 fpu->fcr31 = v; 852 break; 853 854 /* MIPS SIMD Architecture (MSA) registers */ 855 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31): 856 if (!kvm_mips_guest_has_msa(&vcpu->arch)) 857 return -EINVAL; 858 idx = reg->id - KVM_REG_MIPS_VEC_128(0); 859 #ifdef CONFIG_CPU_LITTLE_ENDIAN 860 /* least significant byte first */ 861 set_fpr64(&fpu->fpr[idx], 0, vs[0]); 862 set_fpr64(&fpu->fpr[idx], 1, vs[1]); 863 #else 864 /* most significant byte first */ 865 set_fpr64(&fpu->fpr[idx], 1, vs[0]); 866 set_fpr64(&fpu->fpr[idx], 0, vs[1]); 867 #endif 868 break; 869 case KVM_REG_MIPS_MSA_IR: 870 if (!kvm_mips_guest_has_msa(&vcpu->arch)) 871 return -EINVAL; 872 /* Read-only */ 873 break; 874 case KVM_REG_MIPS_MSA_CSR: 875 if (!kvm_mips_guest_has_msa(&vcpu->arch)) 876 return -EINVAL; 877 fpu->msacsr = v; 878 break; 879 880 /* registers to be handled specially */ 881 default: 882 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v); 883 } 884 return 0; 885 } 886 887 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 888 struct kvm_enable_cap *cap) 889 { 890 int r = 0; 891 892 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap)) 893 return -EINVAL; 894 if (cap->flags) 895 return -EINVAL; 896 if (cap->args[0]) 897 return -EINVAL; 898 899 switch (cap->cap) { 900 case KVM_CAP_MIPS_FPU: 901 vcpu->arch.fpu_enabled = true; 902 break; 903 case KVM_CAP_MIPS_MSA: 904 vcpu->arch.msa_enabled = true; 905 break; 906 default: 907 r = -EINVAL; 908 break; 909 } 910 911 return r; 912 } 913 914 long kvm_arch_vcpu_async_ioctl(struct file *filp, unsigned int ioctl, 915 unsigned long arg) 916 { 917 struct kvm_vcpu *vcpu = filp->private_data; 918 void __user *argp = (void __user *)arg; 919 920 if (ioctl == KVM_INTERRUPT) { 921 struct kvm_mips_interrupt irq; 922 923 if (copy_from_user(&irq, argp, sizeof(irq))) 924 return -EFAULT; 925 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__, 926 irq.irq); 927 928 return kvm_vcpu_ioctl_interrupt(vcpu, &irq); 929 } 930 931 return -ENOIOCTLCMD; 932 } 933 934 long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, 935 unsigned long arg) 936 { 937 struct kvm_vcpu *vcpu = filp->private_data; 938 void __user *argp = (void __user *)arg; 939 long r; 940 941 vcpu_load(vcpu); 942 943 switch (ioctl) { 944 case KVM_SET_ONE_REG: 945 case KVM_GET_ONE_REG: { 946 struct kvm_one_reg reg; 947 948 r = -EFAULT; 949 if (copy_from_user(®, argp, sizeof(reg))) 950 break; 951 if (ioctl == KVM_SET_ONE_REG) 952 r = kvm_mips_set_reg(vcpu, ®); 953 else 954 r = kvm_mips_get_reg(vcpu, ®); 955 break; 956 } 957 case KVM_GET_REG_LIST: { 958 struct kvm_reg_list __user *user_list = argp; 959 struct kvm_reg_list reg_list; 960 unsigned n; 961 962 r = -EFAULT; 963 if (copy_from_user(®_list, user_list, sizeof(reg_list))) 964 break; 965 n = reg_list.n; 966 reg_list.n = kvm_mips_num_regs(vcpu); 967 if (copy_to_user(user_list, ®_list, sizeof(reg_list))) 968 break; 969 r = -E2BIG; 970 if (n < reg_list.n) 971 break; 972 r = kvm_mips_copy_reg_indices(vcpu, user_list->reg); 973 break; 974 } 975 case KVM_ENABLE_CAP: { 976 struct kvm_enable_cap cap; 977 978 r = -EFAULT; 979 if (copy_from_user(&cap, argp, sizeof(cap))) 980 break; 981 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 982 break; 983 } 984 default: 985 r = -ENOIOCTLCMD; 986 } 987 988 vcpu_put(vcpu); 989 return r; 990 } 991 992 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 993 { 994 995 } 996 997 void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm, 998 struct kvm_memory_slot *memslot) 999 { 1000 /* Let implementation handle TLB/GVA invalidation */ 1001 kvm_mips_callbacks->flush_shadow_memslot(kvm, memslot); 1002 } 1003 1004 long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) 1005 { 1006 long r; 1007 1008 switch (ioctl) { 1009 default: 1010 r = -ENOIOCTLCMD; 1011 } 1012 1013 return r; 1014 } 1015 1016 int kvm_arch_init(void *opaque) 1017 { 1018 if (kvm_mips_callbacks) { 1019 kvm_err("kvm: module already exists\n"); 1020 return -EEXIST; 1021 } 1022 1023 return kvm_mips_emulation_init(&kvm_mips_callbacks); 1024 } 1025 1026 void kvm_arch_exit(void) 1027 { 1028 kvm_mips_callbacks = NULL; 1029 } 1030 1031 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 1032 struct kvm_sregs *sregs) 1033 { 1034 return -ENOIOCTLCMD; 1035 } 1036 1037 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 1038 struct kvm_sregs *sregs) 1039 { 1040 return -ENOIOCTLCMD; 1041 } 1042 1043 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 1044 { 1045 } 1046 1047 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 1048 { 1049 return -ENOIOCTLCMD; 1050 } 1051 1052 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 1053 { 1054 return -ENOIOCTLCMD; 1055 } 1056 1057 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 1058 { 1059 return VM_FAULT_SIGBUS; 1060 } 1061 1062 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 1063 { 1064 int r; 1065 1066 switch (ext) { 1067 case KVM_CAP_ONE_REG: 1068 case KVM_CAP_ENABLE_CAP: 1069 case KVM_CAP_READONLY_MEM: 1070 case KVM_CAP_SYNC_MMU: 1071 case KVM_CAP_IMMEDIATE_EXIT: 1072 r = 1; 1073 break; 1074 case KVM_CAP_NR_VCPUS: 1075 r = num_online_cpus(); 1076 break; 1077 case KVM_CAP_MAX_VCPUS: 1078 r = KVM_MAX_VCPUS; 1079 break; 1080 case KVM_CAP_MAX_VCPU_ID: 1081 r = KVM_MAX_VCPU_ID; 1082 break; 1083 case KVM_CAP_MIPS_FPU: 1084 /* We don't handle systems with inconsistent cpu_has_fpu */ 1085 r = !!raw_cpu_has_fpu; 1086 break; 1087 case KVM_CAP_MIPS_MSA: 1088 /* 1089 * We don't support MSA vector partitioning yet: 1090 * 1) It would require explicit support which can't be tested 1091 * yet due to lack of support in current hardware. 1092 * 2) It extends the state that would need to be saved/restored 1093 * by e.g. QEMU for migration. 1094 * 1095 * When vector partitioning hardware becomes available, support 1096 * could be added by requiring a flag when enabling 1097 * KVM_CAP_MIPS_MSA capability to indicate that userland knows 1098 * to save/restore the appropriate extra state. 1099 */ 1100 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF); 1101 break; 1102 default: 1103 r = kvm_mips_callbacks->check_extension(kvm, ext); 1104 break; 1105 } 1106 return r; 1107 } 1108 1109 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) 1110 { 1111 return kvm_mips_pending_timer(vcpu) || 1112 kvm_read_c0_guest_cause(vcpu->arch.cop0) & C_TI; 1113 } 1114 1115 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu) 1116 { 1117 int i; 1118 struct mips_coproc *cop0; 1119 1120 if (!vcpu) 1121 return -1; 1122 1123 kvm_debug("VCPU Register Dump:\n"); 1124 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc); 1125 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions); 1126 1127 for (i = 0; i < 32; i += 4) { 1128 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i, 1129 vcpu->arch.gprs[i], 1130 vcpu->arch.gprs[i + 1], 1131 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]); 1132 } 1133 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi); 1134 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo); 1135 1136 cop0 = vcpu->arch.cop0; 1137 kvm_debug("\tStatus: 0x%08x, Cause: 0x%08x\n", 1138 kvm_read_c0_guest_status(cop0), 1139 kvm_read_c0_guest_cause(cop0)); 1140 1141 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0)); 1142 1143 return 0; 1144 } 1145 1146 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 1147 { 1148 int i; 1149 1150 vcpu_load(vcpu); 1151 1152 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++) 1153 vcpu->arch.gprs[i] = regs->gpr[i]; 1154 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */ 1155 vcpu->arch.hi = regs->hi; 1156 vcpu->arch.lo = regs->lo; 1157 vcpu->arch.pc = regs->pc; 1158 1159 vcpu_put(vcpu); 1160 return 0; 1161 } 1162 1163 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 1164 { 1165 int i; 1166 1167 vcpu_load(vcpu); 1168 1169 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++) 1170 regs->gpr[i] = vcpu->arch.gprs[i]; 1171 1172 regs->hi = vcpu->arch.hi; 1173 regs->lo = vcpu->arch.lo; 1174 regs->pc = vcpu->arch.pc; 1175 1176 vcpu_put(vcpu); 1177 return 0; 1178 } 1179 1180 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 1181 struct kvm_translation *tr) 1182 { 1183 return 0; 1184 } 1185 1186 static void kvm_mips_set_c0_status(void) 1187 { 1188 u32 status = read_c0_status(); 1189 1190 if (cpu_has_dsp) 1191 status |= (ST0_MX); 1192 1193 write_c0_status(status); 1194 ehb(); 1195 } 1196 1197 /* 1198 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV) 1199 */ 1200 int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) 1201 { 1202 u32 cause = vcpu->arch.host_cp0_cause; 1203 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f; 1204 u32 __user *opc = (u32 __user *) vcpu->arch.pc; 1205 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; 1206 enum emulation_result er = EMULATE_DONE; 1207 u32 inst; 1208 int ret = RESUME_GUEST; 1209 1210 vcpu->mode = OUTSIDE_GUEST_MODE; 1211 1212 /* re-enable HTW before enabling interrupts */ 1213 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) 1214 htw_start(); 1215 1216 /* Set a default exit reason */ 1217 run->exit_reason = KVM_EXIT_UNKNOWN; 1218 run->ready_for_interrupt_injection = 1; 1219 1220 /* 1221 * Set the appropriate status bits based on host CPU features, 1222 * before we hit the scheduler 1223 */ 1224 kvm_mips_set_c0_status(); 1225 1226 local_irq_enable(); 1227 1228 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n", 1229 cause, opc, run, vcpu); 1230 trace_kvm_exit(vcpu, exccode); 1231 1232 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) { 1233 /* 1234 * Do a privilege check, if in UM most of these exit conditions 1235 * end up causing an exception to be delivered to the Guest 1236 * Kernel 1237 */ 1238 er = kvm_mips_check_privilege(cause, opc, vcpu); 1239 if (er == EMULATE_PRIV_FAIL) { 1240 goto skip_emul; 1241 } else if (er == EMULATE_FAIL) { 1242 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 1243 ret = RESUME_HOST; 1244 goto skip_emul; 1245 } 1246 } 1247 1248 switch (exccode) { 1249 case EXCCODE_INT: 1250 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc); 1251 1252 ++vcpu->stat.int_exits; 1253 1254 if (need_resched()) 1255 cond_resched(); 1256 1257 ret = RESUME_GUEST; 1258 break; 1259 1260 case EXCCODE_CPU: 1261 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc); 1262 1263 ++vcpu->stat.cop_unusable_exits; 1264 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu); 1265 /* XXXKYMA: Might need to return to user space */ 1266 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN) 1267 ret = RESUME_HOST; 1268 break; 1269 1270 case EXCCODE_MOD: 1271 ++vcpu->stat.tlbmod_exits; 1272 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu); 1273 break; 1274 1275 case EXCCODE_TLBS: 1276 kvm_debug("TLB ST fault: cause %#x, status %#x, PC: %p, BadVaddr: %#lx\n", 1277 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc, 1278 badvaddr); 1279 1280 ++vcpu->stat.tlbmiss_st_exits; 1281 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu); 1282 break; 1283 1284 case EXCCODE_TLBL: 1285 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n", 1286 cause, opc, badvaddr); 1287 1288 ++vcpu->stat.tlbmiss_ld_exits; 1289 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu); 1290 break; 1291 1292 case EXCCODE_ADES: 1293 ++vcpu->stat.addrerr_st_exits; 1294 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu); 1295 break; 1296 1297 case EXCCODE_ADEL: 1298 ++vcpu->stat.addrerr_ld_exits; 1299 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu); 1300 break; 1301 1302 case EXCCODE_SYS: 1303 ++vcpu->stat.syscall_exits; 1304 ret = kvm_mips_callbacks->handle_syscall(vcpu); 1305 break; 1306 1307 case EXCCODE_RI: 1308 ++vcpu->stat.resvd_inst_exits; 1309 ret = kvm_mips_callbacks->handle_res_inst(vcpu); 1310 break; 1311 1312 case EXCCODE_BP: 1313 ++vcpu->stat.break_inst_exits; 1314 ret = kvm_mips_callbacks->handle_break(vcpu); 1315 break; 1316 1317 case EXCCODE_TR: 1318 ++vcpu->stat.trap_inst_exits; 1319 ret = kvm_mips_callbacks->handle_trap(vcpu); 1320 break; 1321 1322 case EXCCODE_MSAFPE: 1323 ++vcpu->stat.msa_fpe_exits; 1324 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu); 1325 break; 1326 1327 case EXCCODE_FPE: 1328 ++vcpu->stat.fpe_exits; 1329 ret = kvm_mips_callbacks->handle_fpe(vcpu); 1330 break; 1331 1332 case EXCCODE_MSADIS: 1333 ++vcpu->stat.msa_disabled_exits; 1334 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu); 1335 break; 1336 1337 case EXCCODE_GE: 1338 /* defer exit accounting to handler */ 1339 ret = kvm_mips_callbacks->handle_guest_exit(vcpu); 1340 break; 1341 1342 default: 1343 if (cause & CAUSEF_BD) 1344 opc += 1; 1345 inst = 0; 1346 kvm_get_badinstr(opc, vcpu, &inst); 1347 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#x\n", 1348 exccode, opc, inst, badvaddr, 1349 kvm_read_c0_guest_status(vcpu->arch.cop0)); 1350 kvm_arch_vcpu_dump_regs(vcpu); 1351 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 1352 ret = RESUME_HOST; 1353 break; 1354 1355 } 1356 1357 skip_emul: 1358 local_irq_disable(); 1359 1360 if (ret == RESUME_GUEST) 1361 kvm_vz_acquire_htimer(vcpu); 1362 1363 if (er == EMULATE_DONE && !(ret & RESUME_HOST)) 1364 kvm_mips_deliver_interrupts(vcpu, cause); 1365 1366 if (!(ret & RESUME_HOST)) { 1367 /* Only check for signals if not already exiting to userspace */ 1368 if (signal_pending(current)) { 1369 run->exit_reason = KVM_EXIT_INTR; 1370 ret = (-EINTR << 2) | RESUME_HOST; 1371 ++vcpu->stat.signal_exits; 1372 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL); 1373 } 1374 } 1375 1376 if (ret == RESUME_GUEST) { 1377 trace_kvm_reenter(vcpu); 1378 1379 /* 1380 * Make sure the read of VCPU requests in vcpu_reenter() 1381 * callback is not reordered ahead of the write to vcpu->mode, 1382 * or we could miss a TLB flush request while the requester sees 1383 * the VCPU as outside of guest mode and not needing an IPI. 1384 */ 1385 smp_store_mb(vcpu->mode, IN_GUEST_MODE); 1386 1387 kvm_mips_callbacks->vcpu_reenter(vcpu); 1388 1389 /* 1390 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context 1391 * is live), restore FCR31 / MSACSR. 1392 * 1393 * This should be before returning to the guest exception 1394 * vector, as it may well cause an [MSA] FP exception if there 1395 * are pending exception bits unmasked. (see 1396 * kvm_mips_csr_die_notifier() for how that is handled). 1397 */ 1398 if (kvm_mips_guest_has_fpu(&vcpu->arch) && 1399 read_c0_status() & ST0_CU1) 1400 __kvm_restore_fcsr(&vcpu->arch); 1401 1402 if (kvm_mips_guest_has_msa(&vcpu->arch) && 1403 read_c0_config5() & MIPS_CONF5_MSAEN) 1404 __kvm_restore_msacsr(&vcpu->arch); 1405 } 1406 1407 /* Disable HTW before returning to guest or host */ 1408 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) 1409 htw_stop(); 1410 1411 return ret; 1412 } 1413 1414 /* Enable FPU for guest and restore context */ 1415 void kvm_own_fpu(struct kvm_vcpu *vcpu) 1416 { 1417 struct mips_coproc *cop0 = vcpu->arch.cop0; 1418 unsigned int sr, cfg5; 1419 1420 preempt_disable(); 1421 1422 sr = kvm_read_c0_guest_status(cop0); 1423 1424 /* 1425 * If MSA state is already live, it is undefined how it interacts with 1426 * FR=0 FPU state, and we don't want to hit reserved instruction 1427 * exceptions trying to save the MSA state later when CU=1 && FR=1, so 1428 * play it safe and save it first. 1429 * 1430 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should 1431 * get called when guest CU1 is set, however we can't trust the guest 1432 * not to clobber the status register directly via the commpage. 1433 */ 1434 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) && 1435 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) 1436 kvm_lose_fpu(vcpu); 1437 1438 /* 1439 * Enable FPU for guest 1440 * We set FR and FRE according to guest context 1441 */ 1442 change_c0_status(ST0_CU1 | ST0_FR, sr); 1443 if (cpu_has_fre) { 1444 cfg5 = kvm_read_c0_guest_config5(cop0); 1445 change_c0_config5(MIPS_CONF5_FRE, cfg5); 1446 } 1447 enable_fpu_hazard(); 1448 1449 /* If guest FPU state not active, restore it now */ 1450 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) { 1451 __kvm_restore_fpu(&vcpu->arch); 1452 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU; 1453 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU); 1454 } else { 1455 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU); 1456 } 1457 1458 preempt_enable(); 1459 } 1460 1461 #ifdef CONFIG_CPU_HAS_MSA 1462 /* Enable MSA for guest and restore context */ 1463 void kvm_own_msa(struct kvm_vcpu *vcpu) 1464 { 1465 struct mips_coproc *cop0 = vcpu->arch.cop0; 1466 unsigned int sr, cfg5; 1467 1468 preempt_disable(); 1469 1470 /* 1471 * Enable FPU if enabled in guest, since we're restoring FPU context 1472 * anyway. We set FR and FRE according to guest context. 1473 */ 1474 if (kvm_mips_guest_has_fpu(&vcpu->arch)) { 1475 sr = kvm_read_c0_guest_status(cop0); 1476 1477 /* 1478 * If FR=0 FPU state is already live, it is undefined how it 1479 * interacts with MSA state, so play it safe and save it first. 1480 */ 1481 if (!(sr & ST0_FR) && 1482 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | 1483 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU) 1484 kvm_lose_fpu(vcpu); 1485 1486 change_c0_status(ST0_CU1 | ST0_FR, sr); 1487 if (sr & ST0_CU1 && cpu_has_fre) { 1488 cfg5 = kvm_read_c0_guest_config5(cop0); 1489 change_c0_config5(MIPS_CONF5_FRE, cfg5); 1490 } 1491 } 1492 1493 /* Enable MSA for guest */ 1494 set_c0_config5(MIPS_CONF5_MSAEN); 1495 enable_fpu_hazard(); 1496 1497 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) { 1498 case KVM_MIPS_AUX_FPU: 1499 /* 1500 * Guest FPU state already loaded, only restore upper MSA state 1501 */ 1502 __kvm_restore_msa_upper(&vcpu->arch); 1503 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA; 1504 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA); 1505 break; 1506 case 0: 1507 /* Neither FPU or MSA already active, restore full MSA state */ 1508 __kvm_restore_msa(&vcpu->arch); 1509 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA; 1510 if (kvm_mips_guest_has_fpu(&vcpu->arch)) 1511 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU; 1512 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, 1513 KVM_TRACE_AUX_FPU_MSA); 1514 break; 1515 default: 1516 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA); 1517 break; 1518 } 1519 1520 preempt_enable(); 1521 } 1522 #endif 1523 1524 /* Drop FPU & MSA without saving it */ 1525 void kvm_drop_fpu(struct kvm_vcpu *vcpu) 1526 { 1527 preempt_disable(); 1528 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) { 1529 disable_msa(); 1530 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA); 1531 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA; 1532 } 1533 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) { 1534 clear_c0_status(ST0_CU1 | ST0_FR); 1535 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU); 1536 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU; 1537 } 1538 preempt_enable(); 1539 } 1540 1541 /* Save and disable FPU & MSA */ 1542 void kvm_lose_fpu(struct kvm_vcpu *vcpu) 1543 { 1544 /* 1545 * With T&E, FPU & MSA get disabled in root context (hardware) when it 1546 * is disabled in guest context (software), but the register state in 1547 * the hardware may still be in use. 1548 * This is why we explicitly re-enable the hardware before saving. 1549 */ 1550 1551 preempt_disable(); 1552 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) { 1553 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) { 1554 set_c0_config5(MIPS_CONF5_MSAEN); 1555 enable_fpu_hazard(); 1556 } 1557 1558 __kvm_save_msa(&vcpu->arch); 1559 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA); 1560 1561 /* Disable MSA & FPU */ 1562 disable_msa(); 1563 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) { 1564 clear_c0_status(ST0_CU1 | ST0_FR); 1565 disable_fpu_hazard(); 1566 } 1567 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA); 1568 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) { 1569 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) { 1570 set_c0_status(ST0_CU1); 1571 enable_fpu_hazard(); 1572 } 1573 1574 __kvm_save_fpu(&vcpu->arch); 1575 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU; 1576 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU); 1577 1578 /* Disable FPU */ 1579 clear_c0_status(ST0_CU1 | ST0_FR); 1580 disable_fpu_hazard(); 1581 } 1582 preempt_enable(); 1583 } 1584 1585 /* 1586 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are 1587 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP 1588 * exception if cause bits are set in the value being written. 1589 */ 1590 static int kvm_mips_csr_die_notify(struct notifier_block *self, 1591 unsigned long cmd, void *ptr) 1592 { 1593 struct die_args *args = (struct die_args *)ptr; 1594 struct pt_regs *regs = args->regs; 1595 unsigned long pc; 1596 1597 /* Only interested in FPE and MSAFPE */ 1598 if (cmd != DIE_FP && cmd != DIE_MSAFP) 1599 return NOTIFY_DONE; 1600 1601 /* Return immediately if guest context isn't active */ 1602 if (!(current->flags & PF_VCPU)) 1603 return NOTIFY_DONE; 1604 1605 /* Should never get here from user mode */ 1606 BUG_ON(user_mode(regs)); 1607 1608 pc = instruction_pointer(regs); 1609 switch (cmd) { 1610 case DIE_FP: 1611 /* match 2nd instruction in __kvm_restore_fcsr */ 1612 if (pc != (unsigned long)&__kvm_restore_fcsr + 4) 1613 return NOTIFY_DONE; 1614 break; 1615 case DIE_MSAFP: 1616 /* match 2nd/3rd instruction in __kvm_restore_msacsr */ 1617 if (!cpu_has_msa || 1618 pc < (unsigned long)&__kvm_restore_msacsr + 4 || 1619 pc > (unsigned long)&__kvm_restore_msacsr + 8) 1620 return NOTIFY_DONE; 1621 break; 1622 } 1623 1624 /* Move PC forward a little and continue executing */ 1625 instruction_pointer(regs) += 4; 1626 1627 return NOTIFY_STOP; 1628 } 1629 1630 static struct notifier_block kvm_mips_csr_die_notifier = { 1631 .notifier_call = kvm_mips_csr_die_notify, 1632 }; 1633 1634 static u32 kvm_default_priority_to_irq[MIPS_EXC_MAX] = { 1635 [MIPS_EXC_INT_TIMER] = C_IRQ5, 1636 [MIPS_EXC_INT_IO_1] = C_IRQ0, 1637 [MIPS_EXC_INT_IPI_1] = C_IRQ1, 1638 [MIPS_EXC_INT_IPI_2] = C_IRQ2, 1639 }; 1640 1641 static u32 kvm_loongson3_priority_to_irq[MIPS_EXC_MAX] = { 1642 [MIPS_EXC_INT_TIMER] = C_IRQ5, 1643 [MIPS_EXC_INT_IO_1] = C_IRQ0, 1644 [MIPS_EXC_INT_IO_2] = C_IRQ1, 1645 [MIPS_EXC_INT_IPI_1] = C_IRQ4, 1646 }; 1647 1648 u32 *kvm_priority_to_irq = kvm_default_priority_to_irq; 1649 1650 u32 kvm_irq_to_priority(u32 irq) 1651 { 1652 int i; 1653 1654 for (i = MIPS_EXC_INT_TIMER; i < MIPS_EXC_MAX; i++) { 1655 if (kvm_priority_to_irq[i] == (1 << (irq + 8))) 1656 return i; 1657 } 1658 1659 return MIPS_EXC_MAX; 1660 } 1661 1662 static int __init kvm_mips_init(void) 1663 { 1664 int ret; 1665 1666 if (cpu_has_mmid) { 1667 pr_warn("KVM does not yet support MMIDs. KVM Disabled\n"); 1668 return -EOPNOTSUPP; 1669 } 1670 1671 ret = kvm_mips_entry_setup(); 1672 if (ret) 1673 return ret; 1674 1675 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 1676 1677 if (ret) 1678 return ret; 1679 1680 if (boot_cpu_type() == CPU_LOONGSON64) 1681 kvm_priority_to_irq = kvm_loongson3_priority_to_irq; 1682 1683 register_die_notifier(&kvm_mips_csr_die_notifier); 1684 1685 return 0; 1686 } 1687 1688 static void __exit kvm_mips_exit(void) 1689 { 1690 kvm_exit(); 1691 1692 unregister_die_notifier(&kvm_mips_csr_die_notifier); 1693 } 1694 1695 module_init(kvm_mips_init); 1696 module_exit(kvm_mips_exit); 1697 1698 EXPORT_TRACEPOINT_SYMBOL(kvm_exit); 1699