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
6 * KVM/MIPS: MIPS specific KVM APIs
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
12 #include <linux/errno.h>
13 #include <linux/err.h>
14 #include <linux/kdebug.h>
15 #include <linux/module.h>
16 #include <linux/vmalloc.h>
18 #include <linux/bootmem.h>
21 #include <asm/cacheflush.h>
22 #include <asm/mmu_context.h>
23 #include <asm/pgtable.h>
25 #include <linux/kvm_host.h>
27 #include "interrupt.h"
30 #define CREATE_TRACE_POINTS
34 #define VECTORSPACING 0x100 /* for EI/VI mode */
37 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
38 struct kvm_stats_debugfs_item debugfs_entries[] = {
39 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
40 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
41 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
42 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
43 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
44 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
45 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
46 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
47 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
48 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
49 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
50 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
51 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
52 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
53 { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU },
54 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
55 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
56 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
60 static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu)
64 for_each_possible_cpu(i) {
65 vcpu->arch.guest_kernel_asid[i] = 0;
66 vcpu->arch.guest_user_asid[i] = 0;
73 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
74 * Config7, so we are "runnable" if interrupts are pending
76 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
78 return !!(vcpu->arch.pending_exceptions);
81 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
86 int kvm_arch_hardware_enable(void)
91 int kvm_arch_hardware_setup(void)
96 void kvm_arch_check_processor_compat(void *rtn)
101 static void kvm_mips_init_tlbs(struct kvm *kvm)
106 * Add a wired entry to the TLB, it is used to map the commpage to
109 wired = read_c0_wired();
110 write_c0_wired(wired + 1);
112 kvm->arch.commpage_tlb = wired;
114 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
115 kvm->arch.commpage_tlb);
118 static void kvm_mips_init_vm_percpu(void *arg)
120 struct kvm *kvm = (struct kvm *)arg;
122 kvm_mips_init_tlbs(kvm);
123 kvm_mips_callbacks->vm_init(kvm);
127 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
129 if (atomic_inc_return(&kvm_mips_instance) == 1) {
130 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
132 on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
138 void kvm_mips_free_vcpus(struct kvm *kvm)
141 struct kvm_vcpu *vcpu;
143 /* Put the pages we reserved for the guest pmap */
144 for (i = 0; i < kvm->arch.guest_pmap_npages; i++) {
145 if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE)
146 kvm_mips_release_pfn_clean(kvm->arch.guest_pmap[i]);
148 kfree(kvm->arch.guest_pmap);
150 kvm_for_each_vcpu(i, vcpu, kvm) {
151 kvm_arch_vcpu_free(vcpu);
154 mutex_lock(&kvm->lock);
156 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
157 kvm->vcpus[i] = NULL;
159 atomic_set(&kvm->online_vcpus, 0);
161 mutex_unlock(&kvm->lock);
164 static void kvm_mips_uninit_tlbs(void *arg)
166 /* Restore wired count */
169 /* Clear out all the TLBs */
170 kvm_local_flush_tlb_all();
173 void kvm_arch_destroy_vm(struct kvm *kvm)
175 kvm_mips_free_vcpus(kvm);
177 /* If this is the last instance, restore wired count */
178 if (atomic_dec_return(&kvm_mips_instance) == 0) {
179 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
181 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
185 long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
191 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
192 unsigned long npages)
197 int kvm_arch_prepare_memory_region(struct kvm *kvm,
198 struct kvm_memory_slot *memslot,
199 struct kvm_userspace_memory_region *mem,
200 enum kvm_mr_change change)
205 void kvm_arch_commit_memory_region(struct kvm *kvm,
206 struct kvm_userspace_memory_region *mem,
207 const struct kvm_memory_slot *old,
208 enum kvm_mr_change change)
210 unsigned long npages = 0;
213 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
214 __func__, kvm, mem->slot, mem->guest_phys_addr,
215 mem->memory_size, mem->userspace_addr);
217 /* Setup Guest PMAP table */
218 if (!kvm->arch.guest_pmap) {
220 npages = mem->memory_size >> PAGE_SHIFT;
223 kvm->arch.guest_pmap_npages = npages;
224 kvm->arch.guest_pmap =
225 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
227 if (!kvm->arch.guest_pmap) {
228 kvm_err("Failed to allocate guest PMAP");
232 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
233 npages, kvm->arch.guest_pmap);
235 /* Now setup the page table */
236 for (i = 0; i < npages; i++)
237 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
242 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
244 int err, size, offset;
248 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
255 err = kvm_vcpu_init(vcpu, kvm, id);
260 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
263 * Allocate space for host mode exception handlers that handle
266 if (cpu_has_veic || cpu_has_vint)
267 size = 0x200 + VECTORSPACING * 64;
271 /* Save Linux EBASE */
272 vcpu->arch.host_ebase = (void *)read_c0_ebase();
274 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
280 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
281 ALIGN(size, PAGE_SIZE), gebase);
284 vcpu->arch.guest_ebase = gebase;
286 /* Copy L1 Guest Exception handler to correct offset */
288 /* TLB Refill, EXL = 0 */
289 memcpy(gebase, mips32_exception,
290 mips32_exceptionEnd - mips32_exception);
292 /* General Exception Entry point */
293 memcpy(gebase + 0x180, mips32_exception,
294 mips32_exceptionEnd - mips32_exception);
296 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
297 for (i = 0; i < 8; i++) {
298 kvm_debug("L1 Vectored handler @ %p\n",
299 gebase + 0x200 + (i * VECTORSPACING));
300 memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception,
301 mips32_exceptionEnd - mips32_exception);
304 /* General handler, relocate to unmapped space for sanity's sake */
306 kvm_debug("Installing KVM Exception handlers @ %p, %#x bytes\n",
308 mips32_GuestExceptionEnd - mips32_GuestException);
310 memcpy(gebase + offset, mips32_GuestException,
311 mips32_GuestExceptionEnd - mips32_GuestException);
313 /* Invalidate the icache for these ranges */
314 local_flush_icache_range((unsigned long)gebase,
315 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
318 * Allocate comm page for guest kernel, a TLB will be reserved for
319 * mapping GVA @ 0xFFFF8000 to this page
321 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
323 if (!vcpu->arch.kseg0_commpage) {
325 goto out_free_gebase;
328 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
329 kvm_mips_commpage_init(vcpu);
332 vcpu->arch.last_sched_cpu = -1;
334 /* Start off the timer */
335 kvm_mips_init_count(vcpu);
349 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
351 hrtimer_cancel(&vcpu->arch.comparecount_timer);
353 kvm_vcpu_uninit(vcpu);
355 kvm_mips_dump_stats(vcpu);
357 kfree(vcpu->arch.guest_ebase);
358 kfree(vcpu->arch.kseg0_commpage);
362 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
364 kvm_arch_vcpu_free(vcpu);
367 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
368 struct kvm_guest_debug *dbg)
373 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
378 if (vcpu->sigset_active)
379 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
381 if (vcpu->mmio_needed) {
382 if (!vcpu->mmio_is_write)
383 kvm_mips_complete_mmio_load(vcpu, run);
384 vcpu->mmio_needed = 0;
390 /* Check if we have any exceptions/interrupts pending */
391 kvm_mips_deliver_interrupts(vcpu,
392 kvm_read_c0_guest_cause(vcpu->arch.cop0));
396 /* Disable hardware page table walking while in guest */
399 r = __kvm_mips_vcpu_run(run, vcpu);
401 /* Re-enable HTW before enabling interrupts */
407 if (vcpu->sigset_active)
408 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
413 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
414 struct kvm_mips_interrupt *irq)
416 int intr = (int)irq->irq;
417 struct kvm_vcpu *dvcpu = NULL;
419 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
420 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
426 dvcpu = vcpu->kvm->vcpus[irq->cpu];
428 if (intr == 2 || intr == 3 || intr == 4) {
429 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
431 } else if (intr == -2 || intr == -3 || intr == -4) {
432 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
434 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
439 dvcpu->arch.wait = 0;
441 if (waitqueue_active(&dvcpu->wq))
442 wake_up_interruptible(&dvcpu->wq);
447 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
448 struct kvm_mp_state *mp_state)
453 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
454 struct kvm_mp_state *mp_state)
459 static u64 kvm_mips_get_one_regs[] = {
497 KVM_REG_MIPS_CP0_INDEX,
498 KVM_REG_MIPS_CP0_CONTEXT,
499 KVM_REG_MIPS_CP0_USERLOCAL,
500 KVM_REG_MIPS_CP0_PAGEMASK,
501 KVM_REG_MIPS_CP0_WIRED,
502 KVM_REG_MIPS_CP0_HWRENA,
503 KVM_REG_MIPS_CP0_BADVADDR,
504 KVM_REG_MIPS_CP0_COUNT,
505 KVM_REG_MIPS_CP0_ENTRYHI,
506 KVM_REG_MIPS_CP0_COMPARE,
507 KVM_REG_MIPS_CP0_STATUS,
508 KVM_REG_MIPS_CP0_CAUSE,
509 KVM_REG_MIPS_CP0_EPC,
510 KVM_REG_MIPS_CP0_PRID,
511 KVM_REG_MIPS_CP0_CONFIG,
512 KVM_REG_MIPS_CP0_CONFIG1,
513 KVM_REG_MIPS_CP0_CONFIG2,
514 KVM_REG_MIPS_CP0_CONFIG3,
515 KVM_REG_MIPS_CP0_CONFIG4,
516 KVM_REG_MIPS_CP0_CONFIG5,
517 KVM_REG_MIPS_CP0_CONFIG7,
518 KVM_REG_MIPS_CP0_ERROREPC,
520 KVM_REG_MIPS_COUNT_CTL,
521 KVM_REG_MIPS_COUNT_RESUME,
522 KVM_REG_MIPS_COUNT_HZ,
525 static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
526 const struct kvm_one_reg *reg)
528 struct mips_coproc *cop0 = vcpu->arch.cop0;
529 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
535 /* General purpose registers */
536 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
537 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
539 case KVM_REG_MIPS_HI:
540 v = (long)vcpu->arch.hi;
542 case KVM_REG_MIPS_LO:
543 v = (long)vcpu->arch.lo;
545 case KVM_REG_MIPS_PC:
546 v = (long)vcpu->arch.pc;
549 /* Floating point registers */
550 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
551 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
553 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
554 /* Odd singles in top of even double when FR=0 */
555 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
556 v = get_fpr32(&fpu->fpr[idx], 0);
558 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
560 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
561 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
563 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
564 /* Can't access odd doubles in FR=0 mode */
565 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
567 v = get_fpr64(&fpu->fpr[idx], 0);
569 case KVM_REG_MIPS_FCR_IR:
570 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
572 v = boot_cpu_data.fpu_id;
574 case KVM_REG_MIPS_FCR_CSR:
575 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
580 /* Co-processor 0 registers */
581 case KVM_REG_MIPS_CP0_INDEX:
582 v = (long)kvm_read_c0_guest_index(cop0);
584 case KVM_REG_MIPS_CP0_CONTEXT:
585 v = (long)kvm_read_c0_guest_context(cop0);
587 case KVM_REG_MIPS_CP0_USERLOCAL:
588 v = (long)kvm_read_c0_guest_userlocal(cop0);
590 case KVM_REG_MIPS_CP0_PAGEMASK:
591 v = (long)kvm_read_c0_guest_pagemask(cop0);
593 case KVM_REG_MIPS_CP0_WIRED:
594 v = (long)kvm_read_c0_guest_wired(cop0);
596 case KVM_REG_MIPS_CP0_HWRENA:
597 v = (long)kvm_read_c0_guest_hwrena(cop0);
599 case KVM_REG_MIPS_CP0_BADVADDR:
600 v = (long)kvm_read_c0_guest_badvaddr(cop0);
602 case KVM_REG_MIPS_CP0_ENTRYHI:
603 v = (long)kvm_read_c0_guest_entryhi(cop0);
605 case KVM_REG_MIPS_CP0_COMPARE:
606 v = (long)kvm_read_c0_guest_compare(cop0);
608 case KVM_REG_MIPS_CP0_STATUS:
609 v = (long)kvm_read_c0_guest_status(cop0);
611 case KVM_REG_MIPS_CP0_CAUSE:
612 v = (long)kvm_read_c0_guest_cause(cop0);
614 case KVM_REG_MIPS_CP0_EPC:
615 v = (long)kvm_read_c0_guest_epc(cop0);
617 case KVM_REG_MIPS_CP0_PRID:
618 v = (long)kvm_read_c0_guest_prid(cop0);
620 case KVM_REG_MIPS_CP0_CONFIG:
621 v = (long)kvm_read_c0_guest_config(cop0);
623 case KVM_REG_MIPS_CP0_CONFIG1:
624 v = (long)kvm_read_c0_guest_config1(cop0);
626 case KVM_REG_MIPS_CP0_CONFIG2:
627 v = (long)kvm_read_c0_guest_config2(cop0);
629 case KVM_REG_MIPS_CP0_CONFIG3:
630 v = (long)kvm_read_c0_guest_config3(cop0);
632 case KVM_REG_MIPS_CP0_CONFIG4:
633 v = (long)kvm_read_c0_guest_config4(cop0);
635 case KVM_REG_MIPS_CP0_CONFIG5:
636 v = (long)kvm_read_c0_guest_config5(cop0);
638 case KVM_REG_MIPS_CP0_CONFIG7:
639 v = (long)kvm_read_c0_guest_config7(cop0);
641 case KVM_REG_MIPS_CP0_ERROREPC:
642 v = (long)kvm_read_c0_guest_errorepc(cop0);
644 /* registers to be handled specially */
645 case KVM_REG_MIPS_CP0_COUNT:
646 case KVM_REG_MIPS_COUNT_CTL:
647 case KVM_REG_MIPS_COUNT_RESUME:
648 case KVM_REG_MIPS_COUNT_HZ:
649 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
656 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
657 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
659 return put_user(v, uaddr64);
660 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
661 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
664 return put_user(v32, uaddr32);
670 static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
671 const struct kvm_one_reg *reg)
673 struct mips_coproc *cop0 = vcpu->arch.cop0;
674 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
678 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
679 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
681 if (get_user(v, uaddr64) != 0)
683 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
684 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
687 if (get_user(v32, uaddr32) != 0)
695 /* General purpose registers */
696 case KVM_REG_MIPS_R0:
697 /* Silently ignore requests to set $0 */
699 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
700 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
702 case KVM_REG_MIPS_HI:
705 case KVM_REG_MIPS_LO:
708 case KVM_REG_MIPS_PC:
712 /* Floating point registers */
713 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
714 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
716 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
717 /* Odd singles in top of even double when FR=0 */
718 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
719 set_fpr32(&fpu->fpr[idx], 0, v);
721 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
723 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
724 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
726 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
727 /* Can't access odd doubles in FR=0 mode */
728 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
730 set_fpr64(&fpu->fpr[idx], 0, v);
732 case KVM_REG_MIPS_FCR_IR:
733 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
737 case KVM_REG_MIPS_FCR_CSR:
738 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
743 /* Co-processor 0 registers */
744 case KVM_REG_MIPS_CP0_INDEX:
745 kvm_write_c0_guest_index(cop0, v);
747 case KVM_REG_MIPS_CP0_CONTEXT:
748 kvm_write_c0_guest_context(cop0, v);
750 case KVM_REG_MIPS_CP0_USERLOCAL:
751 kvm_write_c0_guest_userlocal(cop0, v);
753 case KVM_REG_MIPS_CP0_PAGEMASK:
754 kvm_write_c0_guest_pagemask(cop0, v);
756 case KVM_REG_MIPS_CP0_WIRED:
757 kvm_write_c0_guest_wired(cop0, v);
759 case KVM_REG_MIPS_CP0_HWRENA:
760 kvm_write_c0_guest_hwrena(cop0, v);
762 case KVM_REG_MIPS_CP0_BADVADDR:
763 kvm_write_c0_guest_badvaddr(cop0, v);
765 case KVM_REG_MIPS_CP0_ENTRYHI:
766 kvm_write_c0_guest_entryhi(cop0, v);
768 case KVM_REG_MIPS_CP0_STATUS:
769 kvm_write_c0_guest_status(cop0, v);
771 case KVM_REG_MIPS_CP0_EPC:
772 kvm_write_c0_guest_epc(cop0, v);
774 case KVM_REG_MIPS_CP0_PRID:
775 kvm_write_c0_guest_prid(cop0, v);
777 case KVM_REG_MIPS_CP0_ERROREPC:
778 kvm_write_c0_guest_errorepc(cop0, v);
780 /* registers to be handled specially */
781 case KVM_REG_MIPS_CP0_COUNT:
782 case KVM_REG_MIPS_CP0_COMPARE:
783 case KVM_REG_MIPS_CP0_CAUSE:
784 case KVM_REG_MIPS_CP0_CONFIG:
785 case KVM_REG_MIPS_CP0_CONFIG1:
786 case KVM_REG_MIPS_CP0_CONFIG2:
787 case KVM_REG_MIPS_CP0_CONFIG3:
788 case KVM_REG_MIPS_CP0_CONFIG4:
789 case KVM_REG_MIPS_CP0_CONFIG5:
790 case KVM_REG_MIPS_COUNT_CTL:
791 case KVM_REG_MIPS_COUNT_RESUME:
792 case KVM_REG_MIPS_COUNT_HZ:
793 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
800 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
801 struct kvm_enable_cap *cap)
805 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
813 case KVM_CAP_MIPS_FPU:
814 vcpu->arch.fpu_enabled = true;
824 long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
827 struct kvm_vcpu *vcpu = filp->private_data;
828 void __user *argp = (void __user *)arg;
832 case KVM_SET_ONE_REG:
833 case KVM_GET_ONE_REG: {
834 struct kvm_one_reg reg;
836 if (copy_from_user(®, argp, sizeof(reg)))
838 if (ioctl == KVM_SET_ONE_REG)
839 return kvm_mips_set_reg(vcpu, ®);
841 return kvm_mips_get_reg(vcpu, ®);
843 case KVM_GET_REG_LIST: {
844 struct kvm_reg_list __user *user_list = argp;
845 u64 __user *reg_dest;
846 struct kvm_reg_list reg_list;
849 if (copy_from_user(®_list, user_list, sizeof(reg_list)))
852 reg_list.n = ARRAY_SIZE(kvm_mips_get_one_regs);
853 if (copy_to_user(user_list, ®_list, sizeof(reg_list)))
857 reg_dest = user_list->reg;
858 if (copy_to_user(reg_dest, kvm_mips_get_one_regs,
859 sizeof(kvm_mips_get_one_regs)))
864 /* Treat the NMI as a CPU reset */
865 r = kvm_mips_reset_vcpu(vcpu);
869 struct kvm_mips_interrupt irq;
872 if (copy_from_user(&irq, argp, sizeof(irq)))
875 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
878 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
881 case KVM_ENABLE_CAP: {
882 struct kvm_enable_cap cap;
885 if (copy_from_user(&cap, argp, sizeof(cap)))
887 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
898 /* Get (and clear) the dirty memory log for a memory slot. */
899 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
901 struct kvm_memory_slot *memslot;
902 unsigned long ga, ga_end;
907 mutex_lock(&kvm->slots_lock);
909 r = kvm_get_dirty_log(kvm, log, &is_dirty);
913 /* If nothing is dirty, don't bother messing with page tables. */
915 memslot = &kvm->memslots->memslots[log->slot];
917 ga = memslot->base_gfn << PAGE_SHIFT;
918 ga_end = ga + (memslot->npages << PAGE_SHIFT);
920 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
923 n = kvm_dirty_bitmap_bytes(memslot);
924 memset(memslot->dirty_bitmap, 0, n);
929 mutex_unlock(&kvm->slots_lock);
934 long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
946 int kvm_arch_init(void *opaque)
948 if (kvm_mips_callbacks) {
949 kvm_err("kvm: module already exists\n");
953 return kvm_mips_emulation_init(&kvm_mips_callbacks);
956 void kvm_arch_exit(void)
958 kvm_mips_callbacks = NULL;
961 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
962 struct kvm_sregs *sregs)
967 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
968 struct kvm_sregs *sregs)
973 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
977 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
982 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
987 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
989 return VM_FAULT_SIGBUS;
992 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
997 case KVM_CAP_ONE_REG:
998 case KVM_CAP_ENABLE_CAP:
1001 case KVM_CAP_COALESCED_MMIO:
1002 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1004 case KVM_CAP_MIPS_FPU:
1014 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1016 return kvm_mips_pending_timer(vcpu);
1019 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1022 struct mips_coproc *cop0;
1027 kvm_debug("VCPU Register Dump:\n");
1028 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1029 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
1031 for (i = 0; i < 32; i += 4) {
1032 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
1034 vcpu->arch.gprs[i + 1],
1035 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1037 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1038 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
1040 cop0 = vcpu->arch.cop0;
1041 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1042 kvm_read_c0_guest_status(cop0),
1043 kvm_read_c0_guest_cause(cop0));
1045 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
1050 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1054 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
1055 vcpu->arch.gprs[i] = regs->gpr[i];
1056 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
1057 vcpu->arch.hi = regs->hi;
1058 vcpu->arch.lo = regs->lo;
1059 vcpu->arch.pc = regs->pc;
1064 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1068 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
1069 regs->gpr[i] = vcpu->arch.gprs[i];
1071 regs->hi = vcpu->arch.hi;
1072 regs->lo = vcpu->arch.lo;
1073 regs->pc = vcpu->arch.pc;
1078 static void kvm_mips_comparecount_func(unsigned long data)
1080 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1082 kvm_mips_callbacks->queue_timer_int(vcpu);
1084 vcpu->arch.wait = 0;
1085 if (waitqueue_active(&vcpu->wq))
1086 wake_up_interruptible(&vcpu->wq);
1089 /* low level hrtimer wake routine */
1090 static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
1092 struct kvm_vcpu *vcpu;
1094 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1095 kvm_mips_comparecount_func((unsigned long) vcpu);
1096 return kvm_mips_count_timeout(vcpu);
1099 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1101 kvm_mips_callbacks->vcpu_init(vcpu);
1102 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1104 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
1108 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1109 struct kvm_translation *tr)
1114 /* Initial guest state */
1115 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1117 return kvm_mips_callbacks->vcpu_setup(vcpu);
1120 static void kvm_mips_set_c0_status(void)
1122 uint32_t status = read_c0_status();
1127 write_c0_status(status);
1132 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1134 int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1136 uint32_t cause = vcpu->arch.host_cp0_cause;
1137 uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1138 uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
1139 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1140 enum emulation_result er = EMULATE_DONE;
1141 int ret = RESUME_GUEST;
1143 /* re-enable HTW before enabling interrupts */
1146 /* Set a default exit reason */
1147 run->exit_reason = KVM_EXIT_UNKNOWN;
1148 run->ready_for_interrupt_injection = 1;
1151 * Set the appropriate status bits based on host CPU features,
1152 * before we hit the scheduler
1154 kvm_mips_set_c0_status();
1158 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1159 cause, opc, run, vcpu);
1162 * Do a privilege check, if in UM most of these exit conditions end up
1163 * causing an exception to be delivered to the Guest Kernel
1165 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1166 if (er == EMULATE_PRIV_FAIL) {
1168 } else if (er == EMULATE_FAIL) {
1169 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1176 kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc);
1178 ++vcpu->stat.int_exits;
1179 trace_kvm_exit(vcpu, INT_EXITS);
1187 case T_COP_UNUSABLE:
1188 kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc);
1190 ++vcpu->stat.cop_unusable_exits;
1191 trace_kvm_exit(vcpu, COP_UNUSABLE_EXITS);
1192 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1193 /* XXXKYMA: Might need to return to user space */
1194 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
1199 ++vcpu->stat.tlbmod_exits;
1200 trace_kvm_exit(vcpu, TLBMOD_EXITS);
1201 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1205 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1206 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1209 ++vcpu->stat.tlbmiss_st_exits;
1210 trace_kvm_exit(vcpu, TLBMISS_ST_EXITS);
1211 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1215 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1216 cause, opc, badvaddr);
1218 ++vcpu->stat.tlbmiss_ld_exits;
1219 trace_kvm_exit(vcpu, TLBMISS_LD_EXITS);
1220 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1224 ++vcpu->stat.addrerr_st_exits;
1225 trace_kvm_exit(vcpu, ADDRERR_ST_EXITS);
1226 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1230 ++vcpu->stat.addrerr_ld_exits;
1231 trace_kvm_exit(vcpu, ADDRERR_LD_EXITS);
1232 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1236 ++vcpu->stat.syscall_exits;
1237 trace_kvm_exit(vcpu, SYSCALL_EXITS);
1238 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1242 ++vcpu->stat.resvd_inst_exits;
1243 trace_kvm_exit(vcpu, RESVD_INST_EXITS);
1244 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1248 ++vcpu->stat.break_inst_exits;
1249 trace_kvm_exit(vcpu, BREAK_INST_EXITS);
1250 ret = kvm_mips_callbacks->handle_break(vcpu);
1254 ++vcpu->stat.trap_inst_exits;
1255 trace_kvm_exit(vcpu, TRAP_INST_EXITS);
1256 ret = kvm_mips_callbacks->handle_trap(vcpu);
1260 ++vcpu->stat.fpe_exits;
1261 trace_kvm_exit(vcpu, FPE_EXITS);
1262 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1266 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1270 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1271 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1272 kvm_read_c0_guest_status(vcpu->arch.cop0));
1273 kvm_arch_vcpu_dump_regs(vcpu);
1274 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1281 local_irq_disable();
1283 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1284 kvm_mips_deliver_interrupts(vcpu, cause);
1286 if (!(ret & RESUME_HOST)) {
1287 /* Only check for signals if not already exiting to userspace */
1288 if (signal_pending(current)) {
1289 run->exit_reason = KVM_EXIT_INTR;
1290 ret = (-EINTR << 2) | RESUME_HOST;
1291 ++vcpu->stat.signal_exits;
1292 trace_kvm_exit(vcpu, SIGNAL_EXITS);
1296 if (ret == RESUME_GUEST) {
1298 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1299 * is live), restore FCR31 / MSACSR.
1301 * This should be before returning to the guest exception
1302 * vector, as it may well cause an [MSA] FP exception if there
1303 * are pending exception bits unmasked. (see
1304 * kvm_mips_csr_die_notifier() for how that is handled).
1306 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1307 read_c0_status() & ST0_CU1)
1308 __kvm_restore_fcsr(&vcpu->arch);
1310 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1311 read_c0_config5() & MIPS_CONF5_MSAEN)
1312 __kvm_restore_msacsr(&vcpu->arch);
1315 /* Disable HTW before returning to guest or host */
1321 /* Enable FPU for guest and restore context */
1322 void kvm_own_fpu(struct kvm_vcpu *vcpu)
1324 struct mips_coproc *cop0 = vcpu->arch.cop0;
1325 unsigned int sr, cfg5;
1329 sr = kvm_read_c0_guest_status(cop0);
1332 * If MSA state is already live, it is undefined how it interacts with
1333 * FR=0 FPU state, and we don't want to hit reserved instruction
1334 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1335 * play it safe and save it first.
1337 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1338 * get called when guest CU1 is set, however we can't trust the guest
1339 * not to clobber the status register directly via the commpage.
1341 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
1342 vcpu->arch.fpu_inuse & KVM_MIPS_FPU_MSA)
1346 * Enable FPU for guest
1347 * We set FR and FRE according to guest context
1349 change_c0_status(ST0_CU1 | ST0_FR, sr);
1351 cfg5 = kvm_read_c0_guest_config5(cop0);
1352 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1354 enable_fpu_hazard();
1356 /* If guest FPU state not active, restore it now */
1357 if (!(vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU)) {
1358 __kvm_restore_fpu(&vcpu->arch);
1359 vcpu->arch.fpu_inuse |= KVM_MIPS_FPU_FPU;
1365 #ifdef CONFIG_CPU_HAS_MSA
1366 /* Enable MSA for guest and restore context */
1367 void kvm_own_msa(struct kvm_vcpu *vcpu)
1369 struct mips_coproc *cop0 = vcpu->arch.cop0;
1370 unsigned int sr, cfg5;
1375 * Enable FPU if enabled in guest, since we're restoring FPU context
1376 * anyway. We set FR and FRE according to guest context.
1378 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1379 sr = kvm_read_c0_guest_status(cop0);
1382 * If FR=0 FPU state is already live, it is undefined how it
1383 * interacts with MSA state, so play it safe and save it first.
1385 if (!(sr & ST0_FR) &&
1386 (vcpu->arch.fpu_inuse & (KVM_MIPS_FPU_FPU |
1387 KVM_MIPS_FPU_MSA)) == KVM_MIPS_FPU_FPU)
1390 change_c0_status(ST0_CU1 | ST0_FR, sr);
1391 if (sr & ST0_CU1 && cpu_has_fre) {
1392 cfg5 = kvm_read_c0_guest_config5(cop0);
1393 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1397 /* Enable MSA for guest */
1398 set_c0_config5(MIPS_CONF5_MSAEN);
1399 enable_fpu_hazard();
1401 switch (vcpu->arch.fpu_inuse & (KVM_MIPS_FPU_FPU | KVM_MIPS_FPU_MSA)) {
1402 case KVM_MIPS_FPU_FPU:
1404 * Guest FPU state already loaded, only restore upper MSA state
1406 __kvm_restore_msa_upper(&vcpu->arch);
1407 vcpu->arch.fpu_inuse |= KVM_MIPS_FPU_MSA;
1410 /* Neither FPU or MSA already active, restore full MSA state */
1411 __kvm_restore_msa(&vcpu->arch);
1412 vcpu->arch.fpu_inuse |= KVM_MIPS_FPU_MSA;
1413 if (kvm_mips_guest_has_fpu(&vcpu->arch))
1414 vcpu->arch.fpu_inuse |= KVM_MIPS_FPU_FPU;
1424 /* Drop FPU & MSA without saving it */
1425 void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1428 if (cpu_has_msa && vcpu->arch.fpu_inuse & KVM_MIPS_FPU_MSA) {
1430 vcpu->arch.fpu_inuse &= ~KVM_MIPS_FPU_MSA;
1432 if (vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU) {
1433 clear_c0_status(ST0_CU1 | ST0_FR);
1434 vcpu->arch.fpu_inuse &= ~KVM_MIPS_FPU_FPU;
1439 /* Save and disable FPU & MSA */
1440 void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1443 * FPU & MSA get disabled in root context (hardware) when it is disabled
1444 * in guest context (software), but the register state in the hardware
1445 * may still be in use. This is why we explicitly re-enable the hardware
1450 if (cpu_has_msa && vcpu->arch.fpu_inuse & KVM_MIPS_FPU_MSA) {
1451 set_c0_config5(MIPS_CONF5_MSAEN);
1452 enable_fpu_hazard();
1454 __kvm_save_msa(&vcpu->arch);
1456 /* Disable MSA & FPU */
1458 if (vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU)
1459 clear_c0_status(ST0_CU1 | ST0_FR);
1460 vcpu->arch.fpu_inuse &= ~(KVM_MIPS_FPU_FPU | KVM_MIPS_FPU_MSA);
1461 } else if (vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU) {
1462 set_c0_status(ST0_CU1);
1463 enable_fpu_hazard();
1465 __kvm_save_fpu(&vcpu->arch);
1466 vcpu->arch.fpu_inuse &= ~KVM_MIPS_FPU_FPU;
1469 clear_c0_status(ST0_CU1 | ST0_FR);
1475 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1476 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1477 * exception if cause bits are set in the value being written.
1479 static int kvm_mips_csr_die_notify(struct notifier_block *self,
1480 unsigned long cmd, void *ptr)
1482 struct die_args *args = (struct die_args *)ptr;
1483 struct pt_regs *regs = args->regs;
1486 /* Only interested in FPE and MSAFPE */
1487 if (cmd != DIE_FP && cmd != DIE_MSAFP)
1490 /* Return immediately if guest context isn't active */
1491 if (!(current->flags & PF_VCPU))
1494 /* Should never get here from user mode */
1495 BUG_ON(user_mode(regs));
1497 pc = instruction_pointer(regs);
1500 /* match 2nd instruction in __kvm_restore_fcsr */
1501 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1505 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1507 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1508 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1513 /* Move PC forward a little and continue executing */
1514 instruction_pointer(regs) += 4;
1519 static struct notifier_block kvm_mips_csr_die_notifier = {
1520 .notifier_call = kvm_mips_csr_die_notify,
1523 int __init kvm_mips_init(void)
1527 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1532 register_die_notifier(&kvm_mips_csr_die_notifier);
1535 * On MIPS, kernel modules are executed from "mapped space", which
1536 * requires TLBs. The TLB handling code is statically linked with
1537 * the rest of the kernel (tlb.c) to avoid the possibility of
1538 * double faulting. The issue is that the TLB code references
1539 * routines that are part of the the KVM module, which are only
1540 * available once the module is loaded.
1542 kvm_mips_gfn_to_pfn = gfn_to_pfn;
1543 kvm_mips_release_pfn_clean = kvm_release_pfn_clean;
1544 kvm_mips_is_error_pfn = is_error_pfn;
1549 void __exit kvm_mips_exit(void)
1553 kvm_mips_gfn_to_pfn = NULL;
1554 kvm_mips_release_pfn_clean = NULL;
1555 kvm_mips_is_error_pfn = NULL;
1557 unregister_die_notifier(&kvm_mips_csr_die_notifier);
1560 module_init(kvm_mips_init);
1561 module_exit(kvm_mips_exit);
1563 EXPORT_TRACEPOINT_SYMBOL(kvm_exit);