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/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>
20 #include <linux/bootmem.h>
23 #include <asm/cacheflush.h>
24 #include <asm/mmu_context.h>
25 #include <asm/pgalloc.h>
26 #include <asm/pgtable.h>
28 #include <linux/kvm_host.h>
30 #include "interrupt.h"
33 #define CREATE_TRACE_POINTS
37 #define VECTORSPACING 0x100 /* for EI/VI mode */
40 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
41 struct kvm_stats_debugfs_item debugfs_entries[] = {
42 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
43 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
44 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
45 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
46 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
47 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
48 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
49 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
50 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
51 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
52 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
53 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
54 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
55 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
56 { "msa_fpe", VCPU_STAT(msa_fpe_exits), KVM_STAT_VCPU },
57 { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU },
58 { "msa_disabled", VCPU_STAT(msa_disabled_exits), KVM_STAT_VCPU },
59 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
60 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
61 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), KVM_STAT_VCPU },
62 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid), KVM_STAT_VCPU },
63 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
68 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
69 * Config7, so we are "runnable" if interrupts are pending
71 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
73 return !!(vcpu->arch.pending_exceptions);
76 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
81 int kvm_arch_hardware_enable(void)
86 int kvm_arch_hardware_setup(void)
91 void kvm_arch_check_processor_compat(void *rtn)
96 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
98 /* Allocate page table to map GPA -> RPA */
99 kvm->arch.gpa_mm.pgd = kvm_pgd_alloc();
100 if (!kvm->arch.gpa_mm.pgd)
106 bool kvm_arch_has_vcpu_debugfs(void)
111 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
116 void kvm_mips_free_vcpus(struct kvm *kvm)
119 struct kvm_vcpu *vcpu;
121 kvm_for_each_vcpu(i, vcpu, kvm) {
122 kvm_arch_vcpu_free(vcpu);
125 mutex_lock(&kvm->lock);
127 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
128 kvm->vcpus[i] = NULL;
130 atomic_set(&kvm->online_vcpus, 0);
132 mutex_unlock(&kvm->lock);
135 static void kvm_mips_free_gpa_pt(struct kvm *kvm)
137 /* It should always be safe to remove after flushing the whole range */
138 WARN_ON(!kvm_mips_flush_gpa_pt(kvm, 0, ~0));
139 pgd_free(NULL, kvm->arch.gpa_mm.pgd);
142 void kvm_arch_destroy_vm(struct kvm *kvm)
144 kvm_mips_free_vcpus(kvm);
145 kvm_mips_free_gpa_pt(kvm);
148 long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
154 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
155 unsigned long npages)
160 void kvm_arch_flush_shadow_all(struct kvm *kvm)
162 /* Flush whole GPA */
163 kvm_mips_flush_gpa_pt(kvm, 0, ~0);
165 /* Let implementation do the rest */
166 kvm_mips_callbacks->flush_shadow_all(kvm);
169 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
170 struct kvm_memory_slot *slot)
173 * The slot has been made invalid (ready for moving or deletion), so we
174 * need to ensure that it can no longer be accessed by any guest VCPUs.
177 spin_lock(&kvm->mmu_lock);
178 /* Flush slot from GPA */
179 kvm_mips_flush_gpa_pt(kvm, slot->base_gfn,
180 slot->base_gfn + slot->npages - 1);
181 /* Let implementation do the rest */
182 kvm_mips_callbacks->flush_shadow_memslot(kvm, slot);
183 spin_unlock(&kvm->mmu_lock);
186 int kvm_arch_prepare_memory_region(struct kvm *kvm,
187 struct kvm_memory_slot *memslot,
188 const struct kvm_userspace_memory_region *mem,
189 enum kvm_mr_change change)
194 void kvm_arch_commit_memory_region(struct kvm *kvm,
195 const struct kvm_userspace_memory_region *mem,
196 const struct kvm_memory_slot *old,
197 const struct kvm_memory_slot *new,
198 enum kvm_mr_change change)
202 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
203 __func__, kvm, mem->slot, mem->guest_phys_addr,
204 mem->memory_size, mem->userspace_addr);
207 * If dirty page logging is enabled, write protect all pages in the slot
208 * ready for dirty logging.
210 * There is no need to do this in any of the following cases:
211 * CREATE: No dirty mappings will already exist.
212 * MOVE/DELETE: The old mappings will already have been cleaned up by
213 * kvm_arch_flush_shadow_memslot()
215 if (change == KVM_MR_FLAGS_ONLY &&
216 (!(old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
217 new->flags & KVM_MEM_LOG_DIRTY_PAGES)) {
218 spin_lock(&kvm->mmu_lock);
219 /* Write protect GPA page table entries */
220 needs_flush = kvm_mips_mkclean_gpa_pt(kvm, new->base_gfn,
221 new->base_gfn + new->npages - 1);
222 /* Let implementation do the rest */
224 kvm_mips_callbacks->flush_shadow_memslot(kvm, new);
225 spin_unlock(&kvm->mmu_lock);
229 static inline void dump_handler(const char *symbol, void *start, void *end)
233 pr_debug("LEAF(%s)\n", symbol);
235 pr_debug("\t.set push\n");
236 pr_debug("\t.set noreorder\n");
238 for (p = start; p < (u32 *)end; ++p)
239 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p);
241 pr_debug("\t.set\tpop\n");
243 pr_debug("\tEND(%s)\n", symbol);
246 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
249 void *gebase, *p, *handler, *refill_start, *refill_end;
252 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
259 err = kvm_vcpu_init(vcpu, kvm, id);
264 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
267 * Allocate space for host mode exception handlers that handle
270 if (cpu_has_veic || cpu_has_vint)
271 size = 0x200 + VECTORSPACING * 64;
275 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
281 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
282 ALIGN(size, PAGE_SIZE), gebase);
285 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
286 * limits us to the low 512MB of physical address space. If the memory
287 * we allocate is out of range, just give up now.
289 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) {
290 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
293 goto out_free_gebase;
297 vcpu->arch.guest_ebase = gebase;
299 /* Build guest exception vectors dynamically in unmapped memory */
300 handler = gebase + 0x2000;
303 refill_start = gebase;
304 refill_end = kvm_mips_build_tlb_refill_exception(refill_start, handler);
306 /* General Exception Entry point */
307 kvm_mips_build_exception(gebase + 0x180, handler);
309 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
310 for (i = 0; i < 8; i++) {
311 kvm_debug("L1 Vectored handler @ %p\n",
312 gebase + 0x200 + (i * VECTORSPACING));
313 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
317 /* General exit handler */
319 p = kvm_mips_build_exit(p);
321 /* Guest entry routine */
322 vcpu->arch.vcpu_run = p;
323 p = kvm_mips_build_vcpu_run(p);
325 /* Dump the generated code */
326 pr_debug("#include <asm/asm.h>\n");
327 pr_debug("#include <asm/regdef.h>\n");
329 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
330 dump_handler("kvm_tlb_refill", refill_start, refill_end);
331 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
332 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
334 /* Invalidate the icache for these ranges */
335 flush_icache_range((unsigned long)gebase,
336 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
339 * Allocate comm page for guest kernel, a TLB will be reserved for
340 * mapping GVA @ 0xFFFF8000 to this page
342 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
344 if (!vcpu->arch.kseg0_commpage) {
346 goto out_free_gebase;
349 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
350 kvm_mips_commpage_init(vcpu);
353 vcpu->arch.last_sched_cpu = -1;
355 /* Start off the timer */
356 kvm_mips_init_count(vcpu);
364 kvm_vcpu_uninit(vcpu);
373 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
375 hrtimer_cancel(&vcpu->arch.comparecount_timer);
377 kvm_vcpu_uninit(vcpu);
379 kvm_mips_dump_stats(vcpu);
381 kvm_mmu_free_memory_caches(vcpu);
382 kfree(vcpu->arch.guest_ebase);
383 kfree(vcpu->arch.kseg0_commpage);
387 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
389 kvm_arch_vcpu_free(vcpu);
392 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
393 struct kvm_guest_debug *dbg)
398 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
403 if (vcpu->sigset_active)
404 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
406 if (vcpu->mmio_needed) {
407 if (!vcpu->mmio_is_write)
408 kvm_mips_complete_mmio_load(vcpu, run);
409 vcpu->mmio_needed = 0;
415 guest_enter_irqoff();
416 trace_kvm_enter(vcpu);
419 * Make sure the read of VCPU requests in vcpu_run() callback is not
420 * reordered ahead of the write to vcpu->mode, or we could miss a TLB
421 * flush request while the requester sees the VCPU as outside of guest
422 * mode and not needing an IPI.
424 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
426 r = kvm_mips_callbacks->vcpu_run(run, vcpu);
432 if (vcpu->sigset_active)
433 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
438 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
439 struct kvm_mips_interrupt *irq)
441 int intr = (int)irq->irq;
442 struct kvm_vcpu *dvcpu = NULL;
444 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
445 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
451 dvcpu = vcpu->kvm->vcpus[irq->cpu];
453 if (intr == 2 || intr == 3 || intr == 4) {
454 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
456 } else if (intr == -2 || intr == -3 || intr == -4) {
457 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
459 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
464 dvcpu->arch.wait = 0;
466 if (swait_active(&dvcpu->wq))
467 swake_up(&dvcpu->wq);
472 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
473 struct kvm_mp_state *mp_state)
478 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
479 struct kvm_mp_state *mp_state)
484 static u64 kvm_mips_get_one_regs[] = {
518 #ifndef CONFIG_CPU_MIPSR6
524 KVM_REG_MIPS_CP0_INDEX,
525 KVM_REG_MIPS_CP0_CONTEXT,
526 KVM_REG_MIPS_CP0_USERLOCAL,
527 KVM_REG_MIPS_CP0_PAGEMASK,
528 KVM_REG_MIPS_CP0_WIRED,
529 KVM_REG_MIPS_CP0_HWRENA,
530 KVM_REG_MIPS_CP0_BADVADDR,
531 KVM_REG_MIPS_CP0_COUNT,
532 KVM_REG_MIPS_CP0_ENTRYHI,
533 KVM_REG_MIPS_CP0_COMPARE,
534 KVM_REG_MIPS_CP0_STATUS,
535 KVM_REG_MIPS_CP0_CAUSE,
536 KVM_REG_MIPS_CP0_EPC,
537 KVM_REG_MIPS_CP0_PRID,
538 KVM_REG_MIPS_CP0_CONFIG,
539 KVM_REG_MIPS_CP0_CONFIG1,
540 KVM_REG_MIPS_CP0_CONFIG2,
541 KVM_REG_MIPS_CP0_CONFIG3,
542 KVM_REG_MIPS_CP0_CONFIG4,
543 KVM_REG_MIPS_CP0_CONFIG5,
544 KVM_REG_MIPS_CP0_CONFIG7,
545 KVM_REG_MIPS_CP0_ERROREPC,
547 KVM_REG_MIPS_COUNT_CTL,
548 KVM_REG_MIPS_COUNT_RESUME,
549 KVM_REG_MIPS_COUNT_HZ,
552 static u64 kvm_mips_get_one_regs_fpu[] = {
554 KVM_REG_MIPS_FCR_CSR,
557 static u64 kvm_mips_get_one_regs_msa[] = {
559 KVM_REG_MIPS_MSA_CSR,
562 static u64 kvm_mips_get_one_regs_kscratch[] = {
563 KVM_REG_MIPS_CP0_KSCRATCH1,
564 KVM_REG_MIPS_CP0_KSCRATCH2,
565 KVM_REG_MIPS_CP0_KSCRATCH3,
566 KVM_REG_MIPS_CP0_KSCRATCH4,
567 KVM_REG_MIPS_CP0_KSCRATCH5,
568 KVM_REG_MIPS_CP0_KSCRATCH6,
571 static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
575 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
576 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
577 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
579 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
582 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
583 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
584 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
585 ret += kvm_mips_callbacks->num_regs(vcpu);
590 static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
595 if (copy_to_user(indices, kvm_mips_get_one_regs,
596 sizeof(kvm_mips_get_one_regs)))
598 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
600 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
601 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
602 sizeof(kvm_mips_get_one_regs_fpu)))
604 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
606 for (i = 0; i < 32; ++i) {
607 index = KVM_REG_MIPS_FPR_32(i);
608 if (copy_to_user(indices, &index, sizeof(index)))
612 /* skip odd doubles if no F64 */
613 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
616 index = KVM_REG_MIPS_FPR_64(i);
617 if (copy_to_user(indices, &index, sizeof(index)))
623 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
624 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
625 sizeof(kvm_mips_get_one_regs_msa)))
627 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
629 for (i = 0; i < 32; ++i) {
630 index = KVM_REG_MIPS_VEC_128(i);
631 if (copy_to_user(indices, &index, sizeof(index)))
637 for (i = 0; i < 6; ++i) {
638 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
641 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
642 sizeof(kvm_mips_get_one_regs_kscratch[i])))
647 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
650 static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
651 const struct kvm_one_reg *reg)
653 struct mips_coproc *cop0 = vcpu->arch.cop0;
654 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
661 /* General purpose registers */
662 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
663 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
665 #ifndef CONFIG_CPU_MIPSR6
666 case KVM_REG_MIPS_HI:
667 v = (long)vcpu->arch.hi;
669 case KVM_REG_MIPS_LO:
670 v = (long)vcpu->arch.lo;
673 case KVM_REG_MIPS_PC:
674 v = (long)vcpu->arch.pc;
677 /* Floating point registers */
678 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
679 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
681 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
682 /* Odd singles in top of even double when FR=0 */
683 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
684 v = get_fpr32(&fpu->fpr[idx], 0);
686 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
688 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
689 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
691 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
692 /* Can't access odd doubles in FR=0 mode */
693 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
695 v = get_fpr64(&fpu->fpr[idx], 0);
697 case KVM_REG_MIPS_FCR_IR:
698 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
700 v = boot_cpu_data.fpu_id;
702 case KVM_REG_MIPS_FCR_CSR:
703 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
708 /* MIPS SIMD Architecture (MSA) registers */
709 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
710 if (!kvm_mips_guest_has_msa(&vcpu->arch))
712 /* Can't access MSA registers in FR=0 mode */
713 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
715 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
716 #ifdef CONFIG_CPU_LITTLE_ENDIAN
717 /* least significant byte first */
718 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
719 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
721 /* most significant byte first */
722 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
723 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
726 case KVM_REG_MIPS_MSA_IR:
727 if (!kvm_mips_guest_has_msa(&vcpu->arch))
729 v = boot_cpu_data.msa_id;
731 case KVM_REG_MIPS_MSA_CSR:
732 if (!kvm_mips_guest_has_msa(&vcpu->arch))
737 /* Co-processor 0 registers */
738 case KVM_REG_MIPS_CP0_INDEX:
739 v = (long)kvm_read_c0_guest_index(cop0);
741 case KVM_REG_MIPS_CP0_CONTEXT:
742 v = (long)kvm_read_c0_guest_context(cop0);
744 case KVM_REG_MIPS_CP0_USERLOCAL:
745 v = (long)kvm_read_c0_guest_userlocal(cop0);
747 case KVM_REG_MIPS_CP0_PAGEMASK:
748 v = (long)kvm_read_c0_guest_pagemask(cop0);
750 case KVM_REG_MIPS_CP0_WIRED:
751 v = (long)kvm_read_c0_guest_wired(cop0);
753 case KVM_REG_MIPS_CP0_HWRENA:
754 v = (long)kvm_read_c0_guest_hwrena(cop0);
756 case KVM_REG_MIPS_CP0_BADVADDR:
757 v = (long)kvm_read_c0_guest_badvaddr(cop0);
759 case KVM_REG_MIPS_CP0_ENTRYHI:
760 v = (long)kvm_read_c0_guest_entryhi(cop0);
762 case KVM_REG_MIPS_CP0_COMPARE:
763 v = (long)kvm_read_c0_guest_compare(cop0);
765 case KVM_REG_MIPS_CP0_STATUS:
766 v = (long)kvm_read_c0_guest_status(cop0);
768 case KVM_REG_MIPS_CP0_CAUSE:
769 v = (long)kvm_read_c0_guest_cause(cop0);
771 case KVM_REG_MIPS_CP0_EPC:
772 v = (long)kvm_read_c0_guest_epc(cop0);
774 case KVM_REG_MIPS_CP0_PRID:
775 v = (long)kvm_read_c0_guest_prid(cop0);
777 case KVM_REG_MIPS_CP0_CONFIG:
778 v = (long)kvm_read_c0_guest_config(cop0);
780 case KVM_REG_MIPS_CP0_CONFIG1:
781 v = (long)kvm_read_c0_guest_config1(cop0);
783 case KVM_REG_MIPS_CP0_CONFIG2:
784 v = (long)kvm_read_c0_guest_config2(cop0);
786 case KVM_REG_MIPS_CP0_CONFIG3:
787 v = (long)kvm_read_c0_guest_config3(cop0);
789 case KVM_REG_MIPS_CP0_CONFIG4:
790 v = (long)kvm_read_c0_guest_config4(cop0);
792 case KVM_REG_MIPS_CP0_CONFIG5:
793 v = (long)kvm_read_c0_guest_config5(cop0);
795 case KVM_REG_MIPS_CP0_CONFIG7:
796 v = (long)kvm_read_c0_guest_config7(cop0);
798 case KVM_REG_MIPS_CP0_ERROREPC:
799 v = (long)kvm_read_c0_guest_errorepc(cop0);
801 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
802 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
803 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
807 v = (long)kvm_read_c0_guest_kscratch1(cop0);
810 v = (long)kvm_read_c0_guest_kscratch2(cop0);
813 v = (long)kvm_read_c0_guest_kscratch3(cop0);
816 v = (long)kvm_read_c0_guest_kscratch4(cop0);
819 v = (long)kvm_read_c0_guest_kscratch5(cop0);
822 v = (long)kvm_read_c0_guest_kscratch6(cop0);
826 /* registers to be handled specially */
828 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
833 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
834 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
836 return put_user(v, uaddr64);
837 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
838 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
841 return put_user(v32, uaddr32);
842 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
843 void __user *uaddr = (void __user *)(long)reg->addr;
845 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
851 static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
852 const struct kvm_one_reg *reg)
854 struct mips_coproc *cop0 = vcpu->arch.cop0;
855 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
860 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
861 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
863 if (get_user(v, uaddr64) != 0)
865 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
866 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
869 if (get_user(v32, uaddr32) != 0)
872 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
873 void __user *uaddr = (void __user *)(long)reg->addr;
875 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
881 /* General purpose registers */
882 case KVM_REG_MIPS_R0:
883 /* Silently ignore requests to set $0 */
885 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
886 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
888 #ifndef CONFIG_CPU_MIPSR6
889 case KVM_REG_MIPS_HI:
892 case KVM_REG_MIPS_LO:
896 case KVM_REG_MIPS_PC:
900 /* Floating point registers */
901 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
902 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
904 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
905 /* Odd singles in top of even double when FR=0 */
906 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
907 set_fpr32(&fpu->fpr[idx], 0, v);
909 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
911 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
912 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
914 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
915 /* Can't access odd doubles in FR=0 mode */
916 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
918 set_fpr64(&fpu->fpr[idx], 0, v);
920 case KVM_REG_MIPS_FCR_IR:
921 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
925 case KVM_REG_MIPS_FCR_CSR:
926 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
931 /* MIPS SIMD Architecture (MSA) registers */
932 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
933 if (!kvm_mips_guest_has_msa(&vcpu->arch))
935 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
936 #ifdef CONFIG_CPU_LITTLE_ENDIAN
937 /* least significant byte first */
938 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
939 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
941 /* most significant byte first */
942 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
943 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
946 case KVM_REG_MIPS_MSA_IR:
947 if (!kvm_mips_guest_has_msa(&vcpu->arch))
951 case KVM_REG_MIPS_MSA_CSR:
952 if (!kvm_mips_guest_has_msa(&vcpu->arch))
957 /* Co-processor 0 registers */
958 case KVM_REG_MIPS_CP0_INDEX:
959 kvm_write_c0_guest_index(cop0, v);
961 case KVM_REG_MIPS_CP0_CONTEXT:
962 kvm_write_c0_guest_context(cop0, v);
964 case KVM_REG_MIPS_CP0_USERLOCAL:
965 kvm_write_c0_guest_userlocal(cop0, v);
967 case KVM_REG_MIPS_CP0_PAGEMASK:
968 kvm_write_c0_guest_pagemask(cop0, v);
970 case KVM_REG_MIPS_CP0_WIRED:
971 kvm_write_c0_guest_wired(cop0, v);
973 case KVM_REG_MIPS_CP0_HWRENA:
974 kvm_write_c0_guest_hwrena(cop0, v);
976 case KVM_REG_MIPS_CP0_BADVADDR:
977 kvm_write_c0_guest_badvaddr(cop0, v);
979 case KVM_REG_MIPS_CP0_ENTRYHI:
980 kvm_write_c0_guest_entryhi(cop0, v);
982 case KVM_REG_MIPS_CP0_STATUS:
983 kvm_write_c0_guest_status(cop0, v);
985 case KVM_REG_MIPS_CP0_EPC:
986 kvm_write_c0_guest_epc(cop0, v);
988 case KVM_REG_MIPS_CP0_PRID:
989 kvm_write_c0_guest_prid(cop0, v);
991 case KVM_REG_MIPS_CP0_ERROREPC:
992 kvm_write_c0_guest_errorepc(cop0, v);
994 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
995 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
996 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
1000 kvm_write_c0_guest_kscratch1(cop0, v);
1003 kvm_write_c0_guest_kscratch2(cop0, v);
1006 kvm_write_c0_guest_kscratch3(cop0, v);
1009 kvm_write_c0_guest_kscratch4(cop0, v);
1012 kvm_write_c0_guest_kscratch5(cop0, v);
1015 kvm_write_c0_guest_kscratch6(cop0, v);
1019 /* registers to be handled specially */
1021 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
1026 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1027 struct kvm_enable_cap *cap)
1031 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1039 case KVM_CAP_MIPS_FPU:
1040 vcpu->arch.fpu_enabled = true;
1042 case KVM_CAP_MIPS_MSA:
1043 vcpu->arch.msa_enabled = true;
1053 long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1056 struct kvm_vcpu *vcpu = filp->private_data;
1057 void __user *argp = (void __user *)arg;
1061 case KVM_SET_ONE_REG:
1062 case KVM_GET_ONE_REG: {
1063 struct kvm_one_reg reg;
1065 if (copy_from_user(®, argp, sizeof(reg)))
1067 if (ioctl == KVM_SET_ONE_REG)
1068 return kvm_mips_set_reg(vcpu, ®);
1070 return kvm_mips_get_reg(vcpu, ®);
1072 case KVM_GET_REG_LIST: {
1073 struct kvm_reg_list __user *user_list = argp;
1074 struct kvm_reg_list reg_list;
1077 if (copy_from_user(®_list, user_list, sizeof(reg_list)))
1080 reg_list.n = kvm_mips_num_regs(vcpu);
1081 if (copy_to_user(user_list, ®_list, sizeof(reg_list)))
1085 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
1089 struct kvm_mips_interrupt irq;
1091 if (copy_from_user(&irq, argp, sizeof(irq)))
1093 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1096 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1099 case KVM_ENABLE_CAP: {
1100 struct kvm_enable_cap cap;
1102 if (copy_from_user(&cap, argp, sizeof(cap)))
1104 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1114 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
1115 * @kvm: kvm instance
1116 * @log: slot id and address to which we copy the log
1118 * Steps 1-4 below provide general overview of dirty page logging. See
1119 * kvm_get_dirty_log_protect() function description for additional details.
1121 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1122 * always flush the TLB (step 4) even if previous step failed and the dirty
1123 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1124 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1125 * writes will be marked dirty for next log read.
1127 * 1. Take a snapshot of the bit and clear it if needed.
1128 * 2. Write protect the corresponding page.
1129 * 3. Copy the snapshot to the userspace.
1130 * 4. Flush TLB's if needed.
1132 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1134 struct kvm_memslots *slots;
1135 struct kvm_memory_slot *memslot;
1136 bool is_dirty = false;
1139 mutex_lock(&kvm->slots_lock);
1141 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
1144 slots = kvm_memslots(kvm);
1145 memslot = id_to_memslot(slots, log->slot);
1147 /* Let implementation handle TLB/GVA invalidation */
1148 kvm_mips_callbacks->flush_shadow_memslot(kvm, memslot);
1151 mutex_unlock(&kvm->slots_lock);
1155 long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1167 int kvm_arch_init(void *opaque)
1169 if (kvm_mips_callbacks) {
1170 kvm_err("kvm: module already exists\n");
1174 return kvm_mips_emulation_init(&kvm_mips_callbacks);
1177 void kvm_arch_exit(void)
1179 kvm_mips_callbacks = NULL;
1182 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1183 struct kvm_sregs *sregs)
1185 return -ENOIOCTLCMD;
1188 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1189 struct kvm_sregs *sregs)
1191 return -ENOIOCTLCMD;
1194 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
1198 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1200 return -ENOIOCTLCMD;
1203 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1205 return -ENOIOCTLCMD;
1208 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1210 return VM_FAULT_SIGBUS;
1213 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
1218 case KVM_CAP_ONE_REG:
1219 case KVM_CAP_ENABLE_CAP:
1220 case KVM_CAP_SYNC_MMU:
1223 case KVM_CAP_COALESCED_MMIO:
1224 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1226 case KVM_CAP_MIPS_FPU:
1227 /* We don't handle systems with inconsistent cpu_has_fpu */
1228 r = !!raw_cpu_has_fpu;
1230 case KVM_CAP_MIPS_MSA:
1232 * We don't support MSA vector partitioning yet:
1233 * 1) It would require explicit support which can't be tested
1234 * yet due to lack of support in current hardware.
1235 * 2) It extends the state that would need to be saved/restored
1236 * by e.g. QEMU for migration.
1238 * When vector partitioning hardware becomes available, support
1239 * could be added by requiring a flag when enabling
1240 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1241 * to save/restore the appropriate extra state.
1243 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1252 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1254 return kvm_mips_pending_timer(vcpu);
1257 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1260 struct mips_coproc *cop0;
1265 kvm_debug("VCPU Register Dump:\n");
1266 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1267 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
1269 for (i = 0; i < 32; i += 4) {
1270 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
1272 vcpu->arch.gprs[i + 1],
1273 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1275 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1276 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
1278 cop0 = vcpu->arch.cop0;
1279 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1280 kvm_read_c0_guest_status(cop0),
1281 kvm_read_c0_guest_cause(cop0));
1283 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
1288 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1292 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
1293 vcpu->arch.gprs[i] = regs->gpr[i];
1294 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
1295 vcpu->arch.hi = regs->hi;
1296 vcpu->arch.lo = regs->lo;
1297 vcpu->arch.pc = regs->pc;
1302 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1306 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
1307 regs->gpr[i] = vcpu->arch.gprs[i];
1309 regs->hi = vcpu->arch.hi;
1310 regs->lo = vcpu->arch.lo;
1311 regs->pc = vcpu->arch.pc;
1316 static void kvm_mips_comparecount_func(unsigned long data)
1318 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1320 kvm_mips_callbacks->queue_timer_int(vcpu);
1322 vcpu->arch.wait = 0;
1323 if (swait_active(&vcpu->wq))
1324 swake_up(&vcpu->wq);
1327 /* low level hrtimer wake routine */
1328 static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
1330 struct kvm_vcpu *vcpu;
1332 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1333 kvm_mips_comparecount_func((unsigned long) vcpu);
1334 return kvm_mips_count_timeout(vcpu);
1337 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1341 err = kvm_mips_callbacks->vcpu_init(vcpu);
1345 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1347 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
1351 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1353 kvm_mips_callbacks->vcpu_uninit(vcpu);
1356 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1357 struct kvm_translation *tr)
1362 /* Initial guest state */
1363 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1365 return kvm_mips_callbacks->vcpu_setup(vcpu);
1368 static void kvm_mips_set_c0_status(void)
1370 u32 status = read_c0_status();
1375 write_c0_status(status);
1380 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1382 int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1384 u32 cause = vcpu->arch.host_cp0_cause;
1385 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1386 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
1387 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1388 enum emulation_result er = EMULATE_DONE;
1390 int ret = RESUME_GUEST;
1392 vcpu->mode = OUTSIDE_GUEST_MODE;
1394 /* re-enable HTW before enabling interrupts */
1397 /* Set a default exit reason */
1398 run->exit_reason = KVM_EXIT_UNKNOWN;
1399 run->ready_for_interrupt_injection = 1;
1402 * Set the appropriate status bits based on host CPU features,
1403 * before we hit the scheduler
1405 kvm_mips_set_c0_status();
1409 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1410 cause, opc, run, vcpu);
1411 trace_kvm_exit(vcpu, exccode);
1414 * Do a privilege check, if in UM most of these exit conditions end up
1415 * causing an exception to be delivered to the Guest Kernel
1417 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1418 if (er == EMULATE_PRIV_FAIL) {
1420 } else if (er == EMULATE_FAIL) {
1421 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1428 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
1430 ++vcpu->stat.int_exits;
1439 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
1441 ++vcpu->stat.cop_unusable_exits;
1442 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1443 /* XXXKYMA: Might need to return to user space */
1444 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
1449 ++vcpu->stat.tlbmod_exits;
1450 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1454 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1455 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1458 ++vcpu->stat.tlbmiss_st_exits;
1459 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1463 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1464 cause, opc, badvaddr);
1466 ++vcpu->stat.tlbmiss_ld_exits;
1467 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1471 ++vcpu->stat.addrerr_st_exits;
1472 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1476 ++vcpu->stat.addrerr_ld_exits;
1477 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1481 ++vcpu->stat.syscall_exits;
1482 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1486 ++vcpu->stat.resvd_inst_exits;
1487 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1491 ++vcpu->stat.break_inst_exits;
1492 ret = kvm_mips_callbacks->handle_break(vcpu);
1496 ++vcpu->stat.trap_inst_exits;
1497 ret = kvm_mips_callbacks->handle_trap(vcpu);
1500 case EXCCODE_MSAFPE:
1501 ++vcpu->stat.msa_fpe_exits;
1502 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1506 ++vcpu->stat.fpe_exits;
1507 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1510 case EXCCODE_MSADIS:
1511 ++vcpu->stat.msa_disabled_exits;
1512 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1516 if (cause & CAUSEF_BD)
1519 kvm_get_badinstr(opc, vcpu, &inst);
1520 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1521 exccode, opc, inst, badvaddr,
1522 kvm_read_c0_guest_status(vcpu->arch.cop0));
1523 kvm_arch_vcpu_dump_regs(vcpu);
1524 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1531 local_irq_disable();
1533 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1534 kvm_mips_deliver_interrupts(vcpu, cause);
1536 if (!(ret & RESUME_HOST)) {
1537 /* Only check for signals if not already exiting to userspace */
1538 if (signal_pending(current)) {
1539 run->exit_reason = KVM_EXIT_INTR;
1540 ret = (-EINTR << 2) | RESUME_HOST;
1541 ++vcpu->stat.signal_exits;
1542 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
1546 if (ret == RESUME_GUEST) {
1547 trace_kvm_reenter(vcpu);
1550 * Make sure the read of VCPU requests in vcpu_reenter()
1551 * callback is not reordered ahead of the write to vcpu->mode,
1552 * or we could miss a TLB flush request while the requester sees
1553 * the VCPU as outside of guest mode and not needing an IPI.
1555 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
1557 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
1560 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1561 * is live), restore FCR31 / MSACSR.
1563 * This should be before returning to the guest exception
1564 * vector, as it may well cause an [MSA] FP exception if there
1565 * are pending exception bits unmasked. (see
1566 * kvm_mips_csr_die_notifier() for how that is handled).
1568 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1569 read_c0_status() & ST0_CU1)
1570 __kvm_restore_fcsr(&vcpu->arch);
1572 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1573 read_c0_config5() & MIPS_CONF5_MSAEN)
1574 __kvm_restore_msacsr(&vcpu->arch);
1577 /* Disable HTW before returning to guest or host */
1583 /* Enable FPU for guest and restore context */
1584 void kvm_own_fpu(struct kvm_vcpu *vcpu)
1586 struct mips_coproc *cop0 = vcpu->arch.cop0;
1587 unsigned int sr, cfg5;
1591 sr = kvm_read_c0_guest_status(cop0);
1594 * If MSA state is already live, it is undefined how it interacts with
1595 * FR=0 FPU state, and we don't want to hit reserved instruction
1596 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1597 * play it safe and save it first.
1599 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1600 * get called when guest CU1 is set, however we can't trust the guest
1601 * not to clobber the status register directly via the commpage.
1603 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
1604 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
1608 * Enable FPU for guest
1609 * We set FR and FRE according to guest context
1611 change_c0_status(ST0_CU1 | ST0_FR, sr);
1613 cfg5 = kvm_read_c0_guest_config5(cop0);
1614 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1616 enable_fpu_hazard();
1618 /* If guest FPU state not active, restore it now */
1619 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
1620 __kvm_restore_fpu(&vcpu->arch);
1621 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
1622 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1624 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
1630 #ifdef CONFIG_CPU_HAS_MSA
1631 /* Enable MSA for guest and restore context */
1632 void kvm_own_msa(struct kvm_vcpu *vcpu)
1634 struct mips_coproc *cop0 = vcpu->arch.cop0;
1635 unsigned int sr, cfg5;
1640 * Enable FPU if enabled in guest, since we're restoring FPU context
1641 * anyway. We set FR and FRE according to guest context.
1643 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1644 sr = kvm_read_c0_guest_status(cop0);
1647 * If FR=0 FPU state is already live, it is undefined how it
1648 * interacts with MSA state, so play it safe and save it first.
1650 if (!(sr & ST0_FR) &&
1651 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1652 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
1655 change_c0_status(ST0_CU1 | ST0_FR, sr);
1656 if (sr & ST0_CU1 && cpu_has_fre) {
1657 cfg5 = kvm_read_c0_guest_config5(cop0);
1658 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1662 /* Enable MSA for guest */
1663 set_c0_config5(MIPS_CONF5_MSAEN);
1664 enable_fpu_hazard();
1666 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1667 case KVM_MIPS_AUX_FPU:
1669 * Guest FPU state already loaded, only restore upper MSA state
1671 __kvm_restore_msa_upper(&vcpu->arch);
1672 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
1673 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
1676 /* Neither FPU or MSA already active, restore full MSA state */
1677 __kvm_restore_msa(&vcpu->arch);
1678 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
1679 if (kvm_mips_guest_has_fpu(&vcpu->arch))
1680 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
1681 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1682 KVM_TRACE_AUX_FPU_MSA);
1685 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
1693 /* Drop FPU & MSA without saving it */
1694 void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1697 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
1699 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
1700 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
1702 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
1703 clear_c0_status(ST0_CU1 | ST0_FR);
1704 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
1705 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
1710 /* Save and disable FPU & MSA */
1711 void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1714 * FPU & MSA get disabled in root context (hardware) when it is disabled
1715 * in guest context (software), but the register state in the hardware
1716 * may still be in use. This is why we explicitly re-enable the hardware
1721 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
1722 set_c0_config5(MIPS_CONF5_MSAEN);
1723 enable_fpu_hazard();
1725 __kvm_save_msa(&vcpu->arch);
1726 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
1728 /* Disable MSA & FPU */
1730 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
1731 clear_c0_status(ST0_CU1 | ST0_FR);
1732 disable_fpu_hazard();
1734 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1735 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
1736 set_c0_status(ST0_CU1);
1737 enable_fpu_hazard();
1739 __kvm_save_fpu(&vcpu->arch);
1740 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
1741 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
1744 clear_c0_status(ST0_CU1 | ST0_FR);
1745 disable_fpu_hazard();
1751 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1752 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1753 * exception if cause bits are set in the value being written.
1755 static int kvm_mips_csr_die_notify(struct notifier_block *self,
1756 unsigned long cmd, void *ptr)
1758 struct die_args *args = (struct die_args *)ptr;
1759 struct pt_regs *regs = args->regs;
1762 /* Only interested in FPE and MSAFPE */
1763 if (cmd != DIE_FP && cmd != DIE_MSAFP)
1766 /* Return immediately if guest context isn't active */
1767 if (!(current->flags & PF_VCPU))
1770 /* Should never get here from user mode */
1771 BUG_ON(user_mode(regs));
1773 pc = instruction_pointer(regs);
1776 /* match 2nd instruction in __kvm_restore_fcsr */
1777 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1781 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1783 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1784 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1789 /* Move PC forward a little and continue executing */
1790 instruction_pointer(regs) += 4;
1795 static struct notifier_block kvm_mips_csr_die_notifier = {
1796 .notifier_call = kvm_mips_csr_die_notify,
1799 static int __init kvm_mips_init(void)
1803 ret = kvm_mips_entry_setup();
1807 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1812 register_die_notifier(&kvm_mips_csr_die_notifier);
1817 static void __exit kvm_mips_exit(void)
1821 unregister_die_notifier(&kvm_mips_csr_die_notifier);
1824 module_init(kvm_mips_init);
1825 module_exit(kvm_mips_exit);
1827 EXPORT_TRACEPOINT_SYMBOL(kvm_exit);