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/pgtable.h>
27 #include <linux/kvm_host.h>
29 #include "interrupt.h"
32 #define CREATE_TRACE_POINTS
36 #define VECTORSPACING 0x100 /* for EI/VI mode */
39 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
40 struct kvm_stats_debugfs_item debugfs_entries[] = {
41 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
42 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
43 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
44 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
45 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
46 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
47 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
48 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
49 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
50 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
51 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
52 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
53 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
54 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
55 { "msa_fpe", VCPU_STAT(msa_fpe_exits), KVM_STAT_VCPU },
56 { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU },
57 { "msa_disabled", VCPU_STAT(msa_disabled_exits), KVM_STAT_VCPU },
58 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
59 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
60 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), KVM_STAT_VCPU },
61 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid), KVM_STAT_VCPU },
62 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
67 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
68 * Config7, so we are "runnable" if interrupts are pending
70 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
72 return !!(vcpu->arch.pending_exceptions);
75 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
80 int kvm_arch_hardware_enable(void)
85 int kvm_arch_hardware_setup(void)
90 void kvm_arch_check_processor_compat(void *rtn)
95 static void kvm_mips_init_tlbs(struct kvm *kvm)
100 * Add a wired entry to the TLB, it is used to map the commpage to
103 wired = read_c0_wired();
104 write_c0_wired(wired + 1);
106 kvm->arch.commpage_tlb = wired;
108 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
109 kvm->arch.commpage_tlb);
112 static void kvm_mips_init_vm_percpu(void *arg)
114 struct kvm *kvm = (struct kvm *)arg;
116 kvm_mips_init_tlbs(kvm);
117 kvm_mips_callbacks->vm_init(kvm);
121 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
123 if (atomic_inc_return(&kvm_mips_instance) == 1) {
124 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
126 on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
132 bool kvm_arch_has_vcpu_debugfs(void)
137 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
142 void kvm_mips_free_vcpus(struct kvm *kvm)
145 struct kvm_vcpu *vcpu;
147 /* Put the pages we reserved for the guest pmap */
148 for (i = 0; i < kvm->arch.guest_pmap_npages; i++) {
149 if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE)
150 kvm_release_pfn_clean(kvm->arch.guest_pmap[i]);
152 kfree(kvm->arch.guest_pmap);
154 kvm_for_each_vcpu(i, vcpu, kvm) {
155 kvm_arch_vcpu_free(vcpu);
158 mutex_lock(&kvm->lock);
160 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
161 kvm->vcpus[i] = NULL;
163 atomic_set(&kvm->online_vcpus, 0);
165 mutex_unlock(&kvm->lock);
168 static void kvm_mips_uninit_tlbs(void *arg)
170 /* Restore wired count */
173 /* Clear out all the TLBs */
174 kvm_local_flush_tlb_all();
177 void kvm_arch_destroy_vm(struct kvm *kvm)
179 kvm_mips_free_vcpus(kvm);
181 /* If this is the last instance, restore wired count */
182 if (atomic_dec_return(&kvm_mips_instance) == 0) {
183 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
185 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
189 long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
195 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
196 unsigned long npages)
201 int kvm_arch_prepare_memory_region(struct kvm *kvm,
202 struct kvm_memory_slot *memslot,
203 const struct kvm_userspace_memory_region *mem,
204 enum kvm_mr_change change)
209 void kvm_arch_commit_memory_region(struct kvm *kvm,
210 const struct kvm_userspace_memory_region *mem,
211 const struct kvm_memory_slot *old,
212 const struct kvm_memory_slot *new,
213 enum kvm_mr_change change)
215 unsigned long npages = 0;
218 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
219 __func__, kvm, mem->slot, mem->guest_phys_addr,
220 mem->memory_size, mem->userspace_addr);
222 /* Setup Guest PMAP table */
223 if (!kvm->arch.guest_pmap) {
225 npages = mem->memory_size >> PAGE_SHIFT;
228 kvm->arch.guest_pmap_npages = npages;
229 kvm->arch.guest_pmap =
230 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
232 if (!kvm->arch.guest_pmap) {
233 kvm_err("Failed to allocate guest PMAP\n");
237 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
238 npages, kvm->arch.guest_pmap);
240 /* Now setup the page table */
241 for (i = 0; i < npages; i++)
242 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
247 static inline void dump_handler(const char *symbol, void *start, void *end)
251 pr_debug("LEAF(%s)\n", symbol);
253 pr_debug("\t.set push\n");
254 pr_debug("\t.set noreorder\n");
256 for (p = start; p < (u32 *)end; ++p)
257 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p);
259 pr_debug("\t.set\tpop\n");
261 pr_debug("\tEND(%s)\n", symbol);
264 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
267 void *gebase, *p, *handler;
270 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
277 err = kvm_vcpu_init(vcpu, kvm, id);
282 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
285 * Allocate space for host mode exception handlers that handle
288 if (cpu_has_veic || cpu_has_vint)
289 size = 0x200 + VECTORSPACING * 64;
293 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
299 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
300 ALIGN(size, PAGE_SIZE), gebase);
303 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
304 * limits us to the low 512MB of physical address space. If the memory
305 * we allocate is out of range, just give up now.
307 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) {
308 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
311 goto out_free_gebase;
315 vcpu->arch.guest_ebase = gebase;
317 /* Build guest exception vectors dynamically in unmapped memory */
318 handler = gebase + 0x2000;
320 /* TLB Refill, EXL = 0 */
321 kvm_mips_build_exception(gebase, handler);
323 /* General Exception Entry point */
324 kvm_mips_build_exception(gebase + 0x180, handler);
326 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
327 for (i = 0; i < 8; i++) {
328 kvm_debug("L1 Vectored handler @ %p\n",
329 gebase + 0x200 + (i * VECTORSPACING));
330 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
334 /* General exit handler */
336 p = kvm_mips_build_exit(p);
338 /* Guest entry routine */
339 vcpu->arch.vcpu_run = p;
340 p = kvm_mips_build_vcpu_run(p);
342 /* Dump the generated code */
343 pr_debug("#include <asm/asm.h>\n");
344 pr_debug("#include <asm/regdef.h>\n");
346 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
347 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
348 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
350 /* Invalidate the icache for these ranges */
351 flush_icache_range((unsigned long)gebase,
352 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
355 * Allocate comm page for guest kernel, a TLB will be reserved for
356 * mapping GVA @ 0xFFFF8000 to this page
358 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
360 if (!vcpu->arch.kseg0_commpage) {
362 goto out_free_gebase;
365 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
366 kvm_mips_commpage_init(vcpu);
369 vcpu->arch.last_sched_cpu = -1;
371 /* Start off the timer */
372 kvm_mips_init_count(vcpu);
380 kvm_vcpu_uninit(vcpu);
389 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
391 hrtimer_cancel(&vcpu->arch.comparecount_timer);
393 kvm_vcpu_uninit(vcpu);
395 kvm_mips_dump_stats(vcpu);
397 kfree(vcpu->arch.guest_ebase);
398 kfree(vcpu->arch.kseg0_commpage);
402 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
404 kvm_arch_vcpu_free(vcpu);
407 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
408 struct kvm_guest_debug *dbg)
413 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
418 if (vcpu->sigset_active)
419 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
421 if (vcpu->mmio_needed) {
422 if (!vcpu->mmio_is_write)
423 kvm_mips_complete_mmio_load(vcpu, run);
424 vcpu->mmio_needed = 0;
430 guest_enter_irqoff();
431 trace_kvm_enter(vcpu);
433 r = kvm_mips_callbacks->vcpu_run(run, vcpu);
439 if (vcpu->sigset_active)
440 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
445 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
446 struct kvm_mips_interrupt *irq)
448 int intr = (int)irq->irq;
449 struct kvm_vcpu *dvcpu = NULL;
451 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
452 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
458 dvcpu = vcpu->kvm->vcpus[irq->cpu];
460 if (intr == 2 || intr == 3 || intr == 4) {
461 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
463 } else if (intr == -2 || intr == -3 || intr == -4) {
464 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
466 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
471 dvcpu->arch.wait = 0;
473 if (swait_active(&dvcpu->wq))
474 swake_up(&dvcpu->wq);
479 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
480 struct kvm_mp_state *mp_state)
485 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
486 struct kvm_mp_state *mp_state)
491 static u64 kvm_mips_get_one_regs[] = {
525 #ifndef CONFIG_CPU_MIPSR6
531 KVM_REG_MIPS_CP0_INDEX,
532 KVM_REG_MIPS_CP0_CONTEXT,
533 KVM_REG_MIPS_CP0_USERLOCAL,
534 KVM_REG_MIPS_CP0_PAGEMASK,
535 KVM_REG_MIPS_CP0_WIRED,
536 KVM_REG_MIPS_CP0_HWRENA,
537 KVM_REG_MIPS_CP0_BADVADDR,
538 KVM_REG_MIPS_CP0_COUNT,
539 KVM_REG_MIPS_CP0_ENTRYHI,
540 KVM_REG_MIPS_CP0_COMPARE,
541 KVM_REG_MIPS_CP0_STATUS,
542 KVM_REG_MIPS_CP0_CAUSE,
543 KVM_REG_MIPS_CP0_EPC,
544 KVM_REG_MIPS_CP0_PRID,
545 KVM_REG_MIPS_CP0_CONFIG,
546 KVM_REG_MIPS_CP0_CONFIG1,
547 KVM_REG_MIPS_CP0_CONFIG2,
548 KVM_REG_MIPS_CP0_CONFIG3,
549 KVM_REG_MIPS_CP0_CONFIG4,
550 KVM_REG_MIPS_CP0_CONFIG5,
551 KVM_REG_MIPS_CP0_CONFIG7,
552 KVM_REG_MIPS_CP0_ERROREPC,
554 KVM_REG_MIPS_COUNT_CTL,
555 KVM_REG_MIPS_COUNT_RESUME,
556 KVM_REG_MIPS_COUNT_HZ,
559 static u64 kvm_mips_get_one_regs_fpu[] = {
561 KVM_REG_MIPS_FCR_CSR,
564 static u64 kvm_mips_get_one_regs_msa[] = {
566 KVM_REG_MIPS_MSA_CSR,
569 static u64 kvm_mips_get_one_regs_kscratch[] = {
570 KVM_REG_MIPS_CP0_KSCRATCH1,
571 KVM_REG_MIPS_CP0_KSCRATCH2,
572 KVM_REG_MIPS_CP0_KSCRATCH3,
573 KVM_REG_MIPS_CP0_KSCRATCH4,
574 KVM_REG_MIPS_CP0_KSCRATCH5,
575 KVM_REG_MIPS_CP0_KSCRATCH6,
578 static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
582 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
583 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
584 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
586 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
589 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
590 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
591 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
592 ret += kvm_mips_callbacks->num_regs(vcpu);
597 static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
602 if (copy_to_user(indices, kvm_mips_get_one_regs,
603 sizeof(kvm_mips_get_one_regs)))
605 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
607 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
608 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
609 sizeof(kvm_mips_get_one_regs_fpu)))
611 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
613 for (i = 0; i < 32; ++i) {
614 index = KVM_REG_MIPS_FPR_32(i);
615 if (copy_to_user(indices, &index, sizeof(index)))
619 /* skip odd doubles if no F64 */
620 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
623 index = KVM_REG_MIPS_FPR_64(i);
624 if (copy_to_user(indices, &index, sizeof(index)))
630 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
631 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
632 sizeof(kvm_mips_get_one_regs_msa)))
634 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
636 for (i = 0; i < 32; ++i) {
637 index = KVM_REG_MIPS_VEC_128(i);
638 if (copy_to_user(indices, &index, sizeof(index)))
644 for (i = 0; i < 6; ++i) {
645 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
648 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
649 sizeof(kvm_mips_get_one_regs_kscratch[i])))
654 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
657 static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
658 const struct kvm_one_reg *reg)
660 struct mips_coproc *cop0 = vcpu->arch.cop0;
661 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
668 /* General purpose registers */
669 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
670 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
672 #ifndef CONFIG_CPU_MIPSR6
673 case KVM_REG_MIPS_HI:
674 v = (long)vcpu->arch.hi;
676 case KVM_REG_MIPS_LO:
677 v = (long)vcpu->arch.lo;
680 case KVM_REG_MIPS_PC:
681 v = (long)vcpu->arch.pc;
684 /* Floating point registers */
685 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
686 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
688 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
689 /* Odd singles in top of even double when FR=0 */
690 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
691 v = get_fpr32(&fpu->fpr[idx], 0);
693 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
695 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
696 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
698 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
699 /* Can't access odd doubles in FR=0 mode */
700 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
702 v = get_fpr64(&fpu->fpr[idx], 0);
704 case KVM_REG_MIPS_FCR_IR:
705 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
707 v = boot_cpu_data.fpu_id;
709 case KVM_REG_MIPS_FCR_CSR:
710 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
715 /* MIPS SIMD Architecture (MSA) registers */
716 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
717 if (!kvm_mips_guest_has_msa(&vcpu->arch))
719 /* Can't access MSA registers in FR=0 mode */
720 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
722 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
723 #ifdef CONFIG_CPU_LITTLE_ENDIAN
724 /* least significant byte first */
725 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
726 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
728 /* most significant byte first */
729 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
730 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
733 case KVM_REG_MIPS_MSA_IR:
734 if (!kvm_mips_guest_has_msa(&vcpu->arch))
736 v = boot_cpu_data.msa_id;
738 case KVM_REG_MIPS_MSA_CSR:
739 if (!kvm_mips_guest_has_msa(&vcpu->arch))
744 /* Co-processor 0 registers */
745 case KVM_REG_MIPS_CP0_INDEX:
746 v = (long)kvm_read_c0_guest_index(cop0);
748 case KVM_REG_MIPS_CP0_CONTEXT:
749 v = (long)kvm_read_c0_guest_context(cop0);
751 case KVM_REG_MIPS_CP0_USERLOCAL:
752 v = (long)kvm_read_c0_guest_userlocal(cop0);
754 case KVM_REG_MIPS_CP0_PAGEMASK:
755 v = (long)kvm_read_c0_guest_pagemask(cop0);
757 case KVM_REG_MIPS_CP0_WIRED:
758 v = (long)kvm_read_c0_guest_wired(cop0);
760 case KVM_REG_MIPS_CP0_HWRENA:
761 v = (long)kvm_read_c0_guest_hwrena(cop0);
763 case KVM_REG_MIPS_CP0_BADVADDR:
764 v = (long)kvm_read_c0_guest_badvaddr(cop0);
766 case KVM_REG_MIPS_CP0_ENTRYHI:
767 v = (long)kvm_read_c0_guest_entryhi(cop0);
769 case KVM_REG_MIPS_CP0_COMPARE:
770 v = (long)kvm_read_c0_guest_compare(cop0);
772 case KVM_REG_MIPS_CP0_STATUS:
773 v = (long)kvm_read_c0_guest_status(cop0);
775 case KVM_REG_MIPS_CP0_CAUSE:
776 v = (long)kvm_read_c0_guest_cause(cop0);
778 case KVM_REG_MIPS_CP0_EPC:
779 v = (long)kvm_read_c0_guest_epc(cop0);
781 case KVM_REG_MIPS_CP0_PRID:
782 v = (long)kvm_read_c0_guest_prid(cop0);
784 case KVM_REG_MIPS_CP0_CONFIG:
785 v = (long)kvm_read_c0_guest_config(cop0);
787 case KVM_REG_MIPS_CP0_CONFIG1:
788 v = (long)kvm_read_c0_guest_config1(cop0);
790 case KVM_REG_MIPS_CP0_CONFIG2:
791 v = (long)kvm_read_c0_guest_config2(cop0);
793 case KVM_REG_MIPS_CP0_CONFIG3:
794 v = (long)kvm_read_c0_guest_config3(cop0);
796 case KVM_REG_MIPS_CP0_CONFIG4:
797 v = (long)kvm_read_c0_guest_config4(cop0);
799 case KVM_REG_MIPS_CP0_CONFIG5:
800 v = (long)kvm_read_c0_guest_config5(cop0);
802 case KVM_REG_MIPS_CP0_CONFIG7:
803 v = (long)kvm_read_c0_guest_config7(cop0);
805 case KVM_REG_MIPS_CP0_ERROREPC:
806 v = (long)kvm_read_c0_guest_errorepc(cop0);
808 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
809 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
810 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
814 v = (long)kvm_read_c0_guest_kscratch1(cop0);
817 v = (long)kvm_read_c0_guest_kscratch2(cop0);
820 v = (long)kvm_read_c0_guest_kscratch3(cop0);
823 v = (long)kvm_read_c0_guest_kscratch4(cop0);
826 v = (long)kvm_read_c0_guest_kscratch5(cop0);
829 v = (long)kvm_read_c0_guest_kscratch6(cop0);
833 /* registers to be handled specially */
835 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
840 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
841 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
843 return put_user(v, uaddr64);
844 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
845 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
848 return put_user(v32, uaddr32);
849 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
850 void __user *uaddr = (void __user *)(long)reg->addr;
852 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
858 static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
859 const struct kvm_one_reg *reg)
861 struct mips_coproc *cop0 = vcpu->arch.cop0;
862 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
867 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
868 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
870 if (get_user(v, uaddr64) != 0)
872 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
873 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
876 if (get_user(v32, uaddr32) != 0)
879 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
880 void __user *uaddr = (void __user *)(long)reg->addr;
882 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
888 /* General purpose registers */
889 case KVM_REG_MIPS_R0:
890 /* Silently ignore requests to set $0 */
892 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
893 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
895 #ifndef CONFIG_CPU_MIPSR6
896 case KVM_REG_MIPS_HI:
899 case KVM_REG_MIPS_LO:
903 case KVM_REG_MIPS_PC:
907 /* Floating point registers */
908 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
909 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
911 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
912 /* Odd singles in top of even double when FR=0 */
913 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
914 set_fpr32(&fpu->fpr[idx], 0, v);
916 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
918 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
919 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
921 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
922 /* Can't access odd doubles in FR=0 mode */
923 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
925 set_fpr64(&fpu->fpr[idx], 0, v);
927 case KVM_REG_MIPS_FCR_IR:
928 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
932 case KVM_REG_MIPS_FCR_CSR:
933 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
938 /* MIPS SIMD Architecture (MSA) registers */
939 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
940 if (!kvm_mips_guest_has_msa(&vcpu->arch))
942 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
943 #ifdef CONFIG_CPU_LITTLE_ENDIAN
944 /* least significant byte first */
945 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
946 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
948 /* most significant byte first */
949 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
950 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
953 case KVM_REG_MIPS_MSA_IR:
954 if (!kvm_mips_guest_has_msa(&vcpu->arch))
958 case KVM_REG_MIPS_MSA_CSR:
959 if (!kvm_mips_guest_has_msa(&vcpu->arch))
964 /* Co-processor 0 registers */
965 case KVM_REG_MIPS_CP0_INDEX:
966 kvm_write_c0_guest_index(cop0, v);
968 case KVM_REG_MIPS_CP0_CONTEXT:
969 kvm_write_c0_guest_context(cop0, v);
971 case KVM_REG_MIPS_CP0_USERLOCAL:
972 kvm_write_c0_guest_userlocal(cop0, v);
974 case KVM_REG_MIPS_CP0_PAGEMASK:
975 kvm_write_c0_guest_pagemask(cop0, v);
977 case KVM_REG_MIPS_CP0_WIRED:
978 kvm_write_c0_guest_wired(cop0, v);
980 case KVM_REG_MIPS_CP0_HWRENA:
981 kvm_write_c0_guest_hwrena(cop0, v);
983 case KVM_REG_MIPS_CP0_BADVADDR:
984 kvm_write_c0_guest_badvaddr(cop0, v);
986 case KVM_REG_MIPS_CP0_ENTRYHI:
987 kvm_write_c0_guest_entryhi(cop0, v);
989 case KVM_REG_MIPS_CP0_STATUS:
990 kvm_write_c0_guest_status(cop0, v);
992 case KVM_REG_MIPS_CP0_EPC:
993 kvm_write_c0_guest_epc(cop0, v);
995 case KVM_REG_MIPS_CP0_PRID:
996 kvm_write_c0_guest_prid(cop0, v);
998 case KVM_REG_MIPS_CP0_ERROREPC:
999 kvm_write_c0_guest_errorepc(cop0, v);
1001 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
1002 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
1003 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
1007 kvm_write_c0_guest_kscratch1(cop0, v);
1010 kvm_write_c0_guest_kscratch2(cop0, v);
1013 kvm_write_c0_guest_kscratch3(cop0, v);
1016 kvm_write_c0_guest_kscratch4(cop0, v);
1019 kvm_write_c0_guest_kscratch5(cop0, v);
1022 kvm_write_c0_guest_kscratch6(cop0, v);
1026 /* registers to be handled specially */
1028 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
1033 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1034 struct kvm_enable_cap *cap)
1038 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1046 case KVM_CAP_MIPS_FPU:
1047 vcpu->arch.fpu_enabled = true;
1049 case KVM_CAP_MIPS_MSA:
1050 vcpu->arch.msa_enabled = true;
1060 long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1063 struct kvm_vcpu *vcpu = filp->private_data;
1064 void __user *argp = (void __user *)arg;
1068 case KVM_SET_ONE_REG:
1069 case KVM_GET_ONE_REG: {
1070 struct kvm_one_reg reg;
1072 if (copy_from_user(®, argp, sizeof(reg)))
1074 if (ioctl == KVM_SET_ONE_REG)
1075 return kvm_mips_set_reg(vcpu, ®);
1077 return kvm_mips_get_reg(vcpu, ®);
1079 case KVM_GET_REG_LIST: {
1080 struct kvm_reg_list __user *user_list = argp;
1081 struct kvm_reg_list reg_list;
1084 if (copy_from_user(®_list, user_list, sizeof(reg_list)))
1087 reg_list.n = kvm_mips_num_regs(vcpu);
1088 if (copy_to_user(user_list, ®_list, sizeof(reg_list)))
1092 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
1096 struct kvm_mips_interrupt irq;
1098 if (copy_from_user(&irq, argp, sizeof(irq)))
1100 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1103 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1106 case KVM_ENABLE_CAP: {
1107 struct kvm_enable_cap cap;
1109 if (copy_from_user(&cap, argp, sizeof(cap)))
1111 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1120 /* Get (and clear) the dirty memory log for a memory slot. */
1121 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1123 struct kvm_memslots *slots;
1124 struct kvm_memory_slot *memslot;
1125 unsigned long ga, ga_end;
1130 mutex_lock(&kvm->slots_lock);
1132 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1136 /* If nothing is dirty, don't bother messing with page tables. */
1138 slots = kvm_memslots(kvm);
1139 memslot = id_to_memslot(slots, log->slot);
1141 ga = memslot->base_gfn << PAGE_SHIFT;
1142 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1144 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
1147 n = kvm_dirty_bitmap_bytes(memslot);
1148 memset(memslot->dirty_bitmap, 0, n);
1153 mutex_unlock(&kvm->slots_lock);
1158 long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1170 int kvm_arch_init(void *opaque)
1172 if (kvm_mips_callbacks) {
1173 kvm_err("kvm: module already exists\n");
1177 return kvm_mips_emulation_init(&kvm_mips_callbacks);
1180 void kvm_arch_exit(void)
1182 kvm_mips_callbacks = NULL;
1185 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1186 struct kvm_sregs *sregs)
1188 return -ENOIOCTLCMD;
1191 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1192 struct kvm_sregs *sregs)
1194 return -ENOIOCTLCMD;
1197 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
1201 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1203 return -ENOIOCTLCMD;
1206 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1208 return -ENOIOCTLCMD;
1211 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1213 return VM_FAULT_SIGBUS;
1216 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
1221 case KVM_CAP_ONE_REG:
1222 case KVM_CAP_ENABLE_CAP:
1225 case KVM_CAP_COALESCED_MMIO:
1226 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1228 case KVM_CAP_MIPS_FPU:
1229 /* We don't handle systems with inconsistent cpu_has_fpu */
1230 r = !!raw_cpu_has_fpu;
1232 case KVM_CAP_MIPS_MSA:
1234 * We don't support MSA vector partitioning yet:
1235 * 1) It would require explicit support which can't be tested
1236 * yet due to lack of support in current hardware.
1237 * 2) It extends the state that would need to be saved/restored
1238 * by e.g. QEMU for migration.
1240 * When vector partitioning hardware becomes available, support
1241 * could be added by requiring a flag when enabling
1242 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1243 * to save/restore the appropriate extra state.
1245 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1254 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1256 return kvm_mips_pending_timer(vcpu);
1259 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1262 struct mips_coproc *cop0;
1267 kvm_debug("VCPU Register Dump:\n");
1268 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1269 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
1271 for (i = 0; i < 32; i += 4) {
1272 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
1274 vcpu->arch.gprs[i + 1],
1275 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1277 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1278 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
1280 cop0 = vcpu->arch.cop0;
1281 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1282 kvm_read_c0_guest_status(cop0),
1283 kvm_read_c0_guest_cause(cop0));
1285 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
1290 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1294 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
1295 vcpu->arch.gprs[i] = regs->gpr[i];
1296 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
1297 vcpu->arch.hi = regs->hi;
1298 vcpu->arch.lo = regs->lo;
1299 vcpu->arch.pc = regs->pc;
1304 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1308 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
1309 regs->gpr[i] = vcpu->arch.gprs[i];
1311 regs->hi = vcpu->arch.hi;
1312 regs->lo = vcpu->arch.lo;
1313 regs->pc = vcpu->arch.pc;
1318 static void kvm_mips_comparecount_func(unsigned long data)
1320 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1322 kvm_mips_callbacks->queue_timer_int(vcpu);
1324 vcpu->arch.wait = 0;
1325 if (swait_active(&vcpu->wq))
1326 swake_up(&vcpu->wq);
1329 /* low level hrtimer wake routine */
1330 static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
1332 struct kvm_vcpu *vcpu;
1334 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1335 kvm_mips_comparecount_func((unsigned long) vcpu);
1336 return kvm_mips_count_timeout(vcpu);
1339 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1341 kvm_mips_callbacks->vcpu_init(vcpu);
1342 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1344 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
1348 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1349 struct kvm_translation *tr)
1354 /* Initial guest state */
1355 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1357 return kvm_mips_callbacks->vcpu_setup(vcpu);
1360 static void kvm_mips_set_c0_status(void)
1362 u32 status = read_c0_status();
1367 write_c0_status(status);
1372 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1374 int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1376 u32 cause = vcpu->arch.host_cp0_cause;
1377 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1378 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
1379 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1380 enum emulation_result er = EMULATE_DONE;
1381 int ret = RESUME_GUEST;
1383 /* re-enable HTW before enabling interrupts */
1386 /* Set a default exit reason */
1387 run->exit_reason = KVM_EXIT_UNKNOWN;
1388 run->ready_for_interrupt_injection = 1;
1391 * Set the appropriate status bits based on host CPU features,
1392 * before we hit the scheduler
1394 kvm_mips_set_c0_status();
1398 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1399 cause, opc, run, vcpu);
1400 trace_kvm_exit(vcpu, exccode);
1403 * Do a privilege check, if in UM most of these exit conditions end up
1404 * causing an exception to be delivered to the Guest Kernel
1406 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1407 if (er == EMULATE_PRIV_FAIL) {
1409 } else if (er == EMULATE_FAIL) {
1410 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1417 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
1419 ++vcpu->stat.int_exits;
1428 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
1430 ++vcpu->stat.cop_unusable_exits;
1431 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1432 /* XXXKYMA: Might need to return to user space */
1433 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
1438 ++vcpu->stat.tlbmod_exits;
1439 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1443 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1444 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1447 ++vcpu->stat.tlbmiss_st_exits;
1448 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1452 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1453 cause, opc, badvaddr);
1455 ++vcpu->stat.tlbmiss_ld_exits;
1456 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1460 ++vcpu->stat.addrerr_st_exits;
1461 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1465 ++vcpu->stat.addrerr_ld_exits;
1466 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1470 ++vcpu->stat.syscall_exits;
1471 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1475 ++vcpu->stat.resvd_inst_exits;
1476 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1480 ++vcpu->stat.break_inst_exits;
1481 ret = kvm_mips_callbacks->handle_break(vcpu);
1485 ++vcpu->stat.trap_inst_exits;
1486 ret = kvm_mips_callbacks->handle_trap(vcpu);
1489 case EXCCODE_MSAFPE:
1490 ++vcpu->stat.msa_fpe_exits;
1491 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1495 ++vcpu->stat.fpe_exits;
1496 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1499 case EXCCODE_MSADIS:
1500 ++vcpu->stat.msa_disabled_exits;
1501 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1505 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1506 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1507 kvm_read_c0_guest_status(vcpu->arch.cop0));
1508 kvm_arch_vcpu_dump_regs(vcpu);
1509 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1516 local_irq_disable();
1518 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1519 kvm_mips_deliver_interrupts(vcpu, cause);
1521 if (!(ret & RESUME_HOST)) {
1522 /* Only check for signals if not already exiting to userspace */
1523 if (signal_pending(current)) {
1524 run->exit_reason = KVM_EXIT_INTR;
1525 ret = (-EINTR << 2) | RESUME_HOST;
1526 ++vcpu->stat.signal_exits;
1527 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
1531 if (ret == RESUME_GUEST) {
1532 trace_kvm_reenter(vcpu);
1534 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
1537 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1538 * is live), restore FCR31 / MSACSR.
1540 * This should be before returning to the guest exception
1541 * vector, as it may well cause an [MSA] FP exception if there
1542 * are pending exception bits unmasked. (see
1543 * kvm_mips_csr_die_notifier() for how that is handled).
1545 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1546 read_c0_status() & ST0_CU1)
1547 __kvm_restore_fcsr(&vcpu->arch);
1549 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1550 read_c0_config5() & MIPS_CONF5_MSAEN)
1551 __kvm_restore_msacsr(&vcpu->arch);
1554 /* Disable HTW before returning to guest or host */
1560 /* Enable FPU for guest and restore context */
1561 void kvm_own_fpu(struct kvm_vcpu *vcpu)
1563 struct mips_coproc *cop0 = vcpu->arch.cop0;
1564 unsigned int sr, cfg5;
1568 sr = kvm_read_c0_guest_status(cop0);
1571 * If MSA state is already live, it is undefined how it interacts with
1572 * FR=0 FPU state, and we don't want to hit reserved instruction
1573 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1574 * play it safe and save it first.
1576 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1577 * get called when guest CU1 is set, however we can't trust the guest
1578 * not to clobber the status register directly via the commpage.
1580 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
1581 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
1585 * Enable FPU for guest
1586 * We set FR and FRE according to guest context
1588 change_c0_status(ST0_CU1 | ST0_FR, sr);
1590 cfg5 = kvm_read_c0_guest_config5(cop0);
1591 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1593 enable_fpu_hazard();
1595 /* If guest FPU state not active, restore it now */
1596 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
1597 __kvm_restore_fpu(&vcpu->arch);
1598 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
1599 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1601 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
1607 #ifdef CONFIG_CPU_HAS_MSA
1608 /* Enable MSA for guest and restore context */
1609 void kvm_own_msa(struct kvm_vcpu *vcpu)
1611 struct mips_coproc *cop0 = vcpu->arch.cop0;
1612 unsigned int sr, cfg5;
1617 * Enable FPU if enabled in guest, since we're restoring FPU context
1618 * anyway. We set FR and FRE according to guest context.
1620 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1621 sr = kvm_read_c0_guest_status(cop0);
1624 * If FR=0 FPU state is already live, it is undefined how it
1625 * interacts with MSA state, so play it safe and save it first.
1627 if (!(sr & ST0_FR) &&
1628 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1629 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
1632 change_c0_status(ST0_CU1 | ST0_FR, sr);
1633 if (sr & ST0_CU1 && cpu_has_fre) {
1634 cfg5 = kvm_read_c0_guest_config5(cop0);
1635 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1639 /* Enable MSA for guest */
1640 set_c0_config5(MIPS_CONF5_MSAEN);
1641 enable_fpu_hazard();
1643 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1644 case KVM_MIPS_AUX_FPU:
1646 * Guest FPU state already loaded, only restore upper MSA state
1648 __kvm_restore_msa_upper(&vcpu->arch);
1649 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
1650 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
1653 /* Neither FPU or MSA already active, restore full MSA state */
1654 __kvm_restore_msa(&vcpu->arch);
1655 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
1656 if (kvm_mips_guest_has_fpu(&vcpu->arch))
1657 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
1658 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1659 KVM_TRACE_AUX_FPU_MSA);
1662 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
1670 /* Drop FPU & MSA without saving it */
1671 void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1674 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
1676 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
1677 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
1679 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
1680 clear_c0_status(ST0_CU1 | ST0_FR);
1681 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
1682 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
1687 /* Save and disable FPU & MSA */
1688 void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1691 * FPU & MSA get disabled in root context (hardware) when it is disabled
1692 * in guest context (software), but the register state in the hardware
1693 * may still be in use. This is why we explicitly re-enable the hardware
1698 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
1699 set_c0_config5(MIPS_CONF5_MSAEN);
1700 enable_fpu_hazard();
1702 __kvm_save_msa(&vcpu->arch);
1703 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
1705 /* Disable MSA & FPU */
1707 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
1708 clear_c0_status(ST0_CU1 | ST0_FR);
1709 disable_fpu_hazard();
1711 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1712 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
1713 set_c0_status(ST0_CU1);
1714 enable_fpu_hazard();
1716 __kvm_save_fpu(&vcpu->arch);
1717 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
1718 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
1721 clear_c0_status(ST0_CU1 | ST0_FR);
1722 disable_fpu_hazard();
1728 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1729 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1730 * exception if cause bits are set in the value being written.
1732 static int kvm_mips_csr_die_notify(struct notifier_block *self,
1733 unsigned long cmd, void *ptr)
1735 struct die_args *args = (struct die_args *)ptr;
1736 struct pt_regs *regs = args->regs;
1739 /* Only interested in FPE and MSAFPE */
1740 if (cmd != DIE_FP && cmd != DIE_MSAFP)
1743 /* Return immediately if guest context isn't active */
1744 if (!(current->flags & PF_VCPU))
1747 /* Should never get here from user mode */
1748 BUG_ON(user_mode(regs));
1750 pc = instruction_pointer(regs);
1753 /* match 2nd instruction in __kvm_restore_fcsr */
1754 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1758 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1760 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1761 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1766 /* Move PC forward a little and continue executing */
1767 instruction_pointer(regs) += 4;
1772 static struct notifier_block kvm_mips_csr_die_notifier = {
1773 .notifier_call = kvm_mips_csr_die_notify,
1776 static int __init kvm_mips_init(void)
1780 ret = kvm_mips_entry_setup();
1784 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1789 register_die_notifier(&kvm_mips_csr_die_notifier);
1794 static void __exit kvm_mips_exit(void)
1798 unregister_die_notifier(&kvm_mips_csr_die_notifier);
1801 module_init(kvm_mips_init);
1802 module_exit(kvm_mips_exit);
1804 EXPORT_TRACEPOINT_SYMBOL(kvm_exit);