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/vmalloc.h>
19 #include <linux/bootmem.h>
22 #include <asm/cacheflush.h>
23 #include <asm/mmu_context.h>
24 #include <asm/pgtable.h>
26 #include <linux/kvm_host.h>
28 #include "interrupt.h"
31 #define CREATE_TRACE_POINTS
35 #define VECTORSPACING 0x100 /* for EI/VI mode */
38 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
39 struct kvm_stats_debugfs_item debugfs_entries
[] = {
40 { "wait", VCPU_STAT(wait_exits
), KVM_STAT_VCPU
},
41 { "cache", VCPU_STAT(cache_exits
), KVM_STAT_VCPU
},
42 { "signal", VCPU_STAT(signal_exits
), KVM_STAT_VCPU
},
43 { "interrupt", VCPU_STAT(int_exits
), KVM_STAT_VCPU
},
44 { "cop_unsuable", VCPU_STAT(cop_unusable_exits
), KVM_STAT_VCPU
},
45 { "tlbmod", VCPU_STAT(tlbmod_exits
), KVM_STAT_VCPU
},
46 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits
), KVM_STAT_VCPU
},
47 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits
), KVM_STAT_VCPU
},
48 { "addrerr_st", VCPU_STAT(addrerr_st_exits
), KVM_STAT_VCPU
},
49 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits
), KVM_STAT_VCPU
},
50 { "syscall", VCPU_STAT(syscall_exits
), KVM_STAT_VCPU
},
51 { "resvd_inst", VCPU_STAT(resvd_inst_exits
), KVM_STAT_VCPU
},
52 { "break_inst", VCPU_STAT(break_inst_exits
), KVM_STAT_VCPU
},
53 { "trap_inst", VCPU_STAT(trap_inst_exits
), KVM_STAT_VCPU
},
54 { "msa_fpe", VCPU_STAT(msa_fpe_exits
), KVM_STAT_VCPU
},
55 { "fpe", VCPU_STAT(fpe_exits
), KVM_STAT_VCPU
},
56 { "msa_disabled", VCPU_STAT(msa_disabled_exits
), KVM_STAT_VCPU
},
57 { "flush_dcache", VCPU_STAT(flush_dcache_exits
), KVM_STAT_VCPU
},
58 { "halt_successful_poll", VCPU_STAT(halt_successful_poll
), KVM_STAT_VCPU
},
59 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll
), KVM_STAT_VCPU
},
60 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid
), KVM_STAT_VCPU
},
61 { "halt_wakeup", VCPU_STAT(halt_wakeup
), KVM_STAT_VCPU
},
65 static int kvm_mips_reset_vcpu(struct kvm_vcpu
*vcpu
)
69 for_each_possible_cpu(i
) {
70 vcpu
->arch
.guest_kernel_asid
[i
] = 0;
71 vcpu
->arch
.guest_user_asid
[i
] = 0;
78 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
79 * Config7, so we are "runnable" if interrupts are pending
81 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
83 return !!(vcpu
->arch
.pending_exceptions
);
86 int kvm_arch_vcpu_should_kick(struct kvm_vcpu
*vcpu
)
91 int kvm_arch_hardware_enable(void)
96 int kvm_arch_hardware_setup(void)
101 void kvm_arch_check_processor_compat(void *rtn
)
106 static void kvm_mips_init_tlbs(struct kvm
*kvm
)
111 * Add a wired entry to the TLB, it is used to map the commpage to
114 wired
= read_c0_wired();
115 write_c0_wired(wired
+ 1);
117 kvm
->arch
.commpage_tlb
= wired
;
119 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
120 kvm
->arch
.commpage_tlb
);
123 static void kvm_mips_init_vm_percpu(void *arg
)
125 struct kvm
*kvm
= (struct kvm
*)arg
;
127 kvm_mips_init_tlbs(kvm
);
128 kvm_mips_callbacks
->vm_init(kvm
);
132 int kvm_arch_init_vm(struct kvm
*kvm
, unsigned long type
)
134 if (atomic_inc_return(&kvm_mips_instance
) == 1) {
135 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
137 on_each_cpu(kvm_mips_init_vm_percpu
, kvm
, 1);
143 void kvm_mips_free_vcpus(struct kvm
*kvm
)
146 struct kvm_vcpu
*vcpu
;
148 /* Put the pages we reserved for the guest pmap */
149 for (i
= 0; i
< kvm
->arch
.guest_pmap_npages
; i
++) {
150 if (kvm
->arch
.guest_pmap
[i
] != KVM_INVALID_PAGE
)
151 kvm_release_pfn_clean(kvm
->arch
.guest_pmap
[i
]);
153 kfree(kvm
->arch
.guest_pmap
);
155 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
156 kvm_arch_vcpu_free(vcpu
);
159 mutex_lock(&kvm
->lock
);
161 for (i
= 0; i
< atomic_read(&kvm
->online_vcpus
); i
++)
162 kvm
->vcpus
[i
] = NULL
;
164 atomic_set(&kvm
->online_vcpus
, 0);
166 mutex_unlock(&kvm
->lock
);
169 static void kvm_mips_uninit_tlbs(void *arg
)
171 /* Restore wired count */
174 /* Clear out all the TLBs */
175 kvm_local_flush_tlb_all();
178 void kvm_arch_destroy_vm(struct kvm
*kvm
)
180 kvm_mips_free_vcpus(kvm
);
182 /* If this is the last instance, restore wired count */
183 if (atomic_dec_return(&kvm_mips_instance
) == 0) {
184 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
186 on_each_cpu(kvm_mips_uninit_tlbs
, NULL
, 1);
190 long kvm_arch_dev_ioctl(struct file
*filp
, unsigned int ioctl
,
196 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
197 unsigned long npages
)
202 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
203 struct kvm_memory_slot
*memslot
,
204 const struct kvm_userspace_memory_region
*mem
,
205 enum kvm_mr_change change
)
210 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
211 const struct kvm_userspace_memory_region
*mem
,
212 const struct kvm_memory_slot
*old
,
213 const struct kvm_memory_slot
*new,
214 enum kvm_mr_change change
)
216 unsigned long npages
= 0;
219 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
220 __func__
, kvm
, mem
->slot
, mem
->guest_phys_addr
,
221 mem
->memory_size
, mem
->userspace_addr
);
223 /* Setup Guest PMAP table */
224 if (!kvm
->arch
.guest_pmap
) {
226 npages
= mem
->memory_size
>> PAGE_SHIFT
;
229 kvm
->arch
.guest_pmap_npages
= npages
;
230 kvm
->arch
.guest_pmap
=
231 kzalloc(npages
* sizeof(unsigned long), GFP_KERNEL
);
233 if (!kvm
->arch
.guest_pmap
) {
234 kvm_err("Failed to allocate guest PMAP\n");
238 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
239 npages
, kvm
->arch
.guest_pmap
);
241 /* Now setup the page table */
242 for (i
= 0; i
< npages
; i
++)
243 kvm
->arch
.guest_pmap
[i
] = KVM_INVALID_PAGE
;
248 static inline void dump_handler(const char *symbol
, void *start
, void *end
)
252 pr_debug("LEAF(%s)\n", symbol
);
254 pr_debug("\t.set push\n");
255 pr_debug("\t.set noreorder\n");
257 for (p
= start
; p
< (u32
*)end
; ++p
)
258 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p
, p
);
260 pr_debug("\t.set\tpop\n");
262 pr_debug("\tEND(%s)\n", symbol
);
265 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
, unsigned int id
)
268 void *gebase
, *p
, *handler
;
271 struct kvm_vcpu
*vcpu
= kzalloc(sizeof(struct kvm_vcpu
), GFP_KERNEL
);
278 err
= kvm_vcpu_init(vcpu
, kvm
, id
);
283 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm
, id
, vcpu
);
286 * Allocate space for host mode exception handlers that handle
289 if (cpu_has_veic
|| cpu_has_vint
)
290 size
= 0x200 + VECTORSPACING
* 64;
294 gebase
= kzalloc(ALIGN(size
, PAGE_SIZE
), GFP_KERNEL
);
300 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
301 ALIGN(size
, PAGE_SIZE
), gebase
);
304 vcpu
->arch
.guest_ebase
= gebase
;
306 /* Build guest exception vectors dynamically in unmapped memory */
307 handler
= gebase
+ 0x2000;
309 /* TLB Refill, EXL = 0 */
310 kvm_mips_build_exception(gebase
, handler
);
312 /* General Exception Entry point */
313 kvm_mips_build_exception(gebase
+ 0x180, handler
);
315 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
316 for (i
= 0; i
< 8; i
++) {
317 kvm_debug("L1 Vectored handler @ %p\n",
318 gebase
+ 0x200 + (i
* VECTORSPACING
));
319 kvm_mips_build_exception(gebase
+ 0x200 + i
* VECTORSPACING
,
323 /* General exit handler */
325 p
= kvm_mips_build_exit(p
);
327 /* Guest entry routine */
328 vcpu
->arch
.vcpu_run
= p
;
329 p
= kvm_mips_build_vcpu_run(p
);
331 /* Dump the generated code */
332 pr_debug("#include <asm/asm.h>\n");
333 pr_debug("#include <asm/regdef.h>\n");
335 dump_handler("kvm_vcpu_run", vcpu
->arch
.vcpu_run
, p
);
336 dump_handler("kvm_gen_exc", gebase
+ 0x180, gebase
+ 0x200);
337 dump_handler("kvm_exit", gebase
+ 0x2000, vcpu
->arch
.vcpu_run
);
339 /* Invalidate the icache for these ranges */
340 local_flush_icache_range((unsigned long)gebase
,
341 (unsigned long)gebase
+ ALIGN(size
, PAGE_SIZE
));
344 * Allocate comm page for guest kernel, a TLB will be reserved for
345 * mapping GVA @ 0xFFFF8000 to this page
347 vcpu
->arch
.kseg0_commpage
= kzalloc(PAGE_SIZE
<< 1, GFP_KERNEL
);
349 if (!vcpu
->arch
.kseg0_commpage
) {
351 goto out_free_gebase
;
354 kvm_debug("Allocated COMM page @ %p\n", vcpu
->arch
.kseg0_commpage
);
355 kvm_mips_commpage_init(vcpu
);
358 vcpu
->arch
.last_sched_cpu
= -1;
360 /* Start off the timer */
361 kvm_mips_init_count(vcpu
);
369 kvm_vcpu_uninit(vcpu
);
378 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
380 hrtimer_cancel(&vcpu
->arch
.comparecount_timer
);
382 kvm_vcpu_uninit(vcpu
);
384 kvm_mips_dump_stats(vcpu
);
386 kfree(vcpu
->arch
.guest_ebase
);
387 kfree(vcpu
->arch
.kseg0_commpage
);
391 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
393 kvm_arch_vcpu_free(vcpu
);
396 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
397 struct kvm_guest_debug
*dbg
)
402 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
407 if (vcpu
->sigset_active
)
408 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
410 if (vcpu
->mmio_needed
) {
411 if (!vcpu
->mmio_is_write
)
412 kvm_mips_complete_mmio_load(vcpu
, run
);
413 vcpu
->mmio_needed
= 0;
419 /* Check if we have any exceptions/interrupts pending */
420 kvm_mips_deliver_interrupts(vcpu
,
421 kvm_read_c0_guest_cause(vcpu
->arch
.cop0
));
423 guest_enter_irqoff();
425 /* Disable hardware page table walking while in guest */
428 trace_kvm_enter(vcpu
);
429 r
= vcpu
->arch
.vcpu_run(run
, vcpu
);
432 /* Re-enable HTW before enabling interrupts */
438 if (vcpu
->sigset_active
)
439 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
444 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
445 struct kvm_mips_interrupt
*irq
)
447 int intr
= (int)irq
->irq
;
448 struct kvm_vcpu
*dvcpu
= NULL
;
450 if (intr
== 3 || intr
== -3 || intr
== 4 || intr
== -4)
451 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__
, irq
->cpu
,
457 dvcpu
= vcpu
->kvm
->vcpus
[irq
->cpu
];
459 if (intr
== 2 || intr
== 3 || intr
== 4) {
460 kvm_mips_callbacks
->queue_io_int(dvcpu
, irq
);
462 } else if (intr
== -2 || intr
== -3 || intr
== -4) {
463 kvm_mips_callbacks
->dequeue_io_int(dvcpu
, irq
);
465 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__
,
470 dvcpu
->arch
.wait
= 0;
472 if (swait_active(&dvcpu
->wq
))
473 swake_up(&dvcpu
->wq
);
478 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
479 struct kvm_mp_state
*mp_state
)
484 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
485 struct kvm_mp_state
*mp_state
)
490 static u64 kvm_mips_get_one_regs
[] = {
528 KVM_REG_MIPS_CP0_INDEX
,
529 KVM_REG_MIPS_CP0_CONTEXT
,
530 KVM_REG_MIPS_CP0_USERLOCAL
,
531 KVM_REG_MIPS_CP0_PAGEMASK
,
532 KVM_REG_MIPS_CP0_WIRED
,
533 KVM_REG_MIPS_CP0_HWRENA
,
534 KVM_REG_MIPS_CP0_BADVADDR
,
535 KVM_REG_MIPS_CP0_COUNT
,
536 KVM_REG_MIPS_CP0_ENTRYHI
,
537 KVM_REG_MIPS_CP0_COMPARE
,
538 KVM_REG_MIPS_CP0_STATUS
,
539 KVM_REG_MIPS_CP0_CAUSE
,
540 KVM_REG_MIPS_CP0_EPC
,
541 KVM_REG_MIPS_CP0_PRID
,
542 KVM_REG_MIPS_CP0_CONFIG
,
543 KVM_REG_MIPS_CP0_CONFIG1
,
544 KVM_REG_MIPS_CP0_CONFIG2
,
545 KVM_REG_MIPS_CP0_CONFIG3
,
546 KVM_REG_MIPS_CP0_CONFIG4
,
547 KVM_REG_MIPS_CP0_CONFIG5
,
548 KVM_REG_MIPS_CP0_CONFIG7
,
549 KVM_REG_MIPS_CP0_ERROREPC
,
551 KVM_REG_MIPS_COUNT_CTL
,
552 KVM_REG_MIPS_COUNT_RESUME
,
553 KVM_REG_MIPS_COUNT_HZ
,
556 static u64 kvm_mips_get_one_regs_fpu
[] = {
558 KVM_REG_MIPS_FCR_CSR
,
561 static u64 kvm_mips_get_one_regs_msa
[] = {
563 KVM_REG_MIPS_MSA_CSR
,
566 static u64 kvm_mips_get_one_regs_kscratch
[] = {
567 KVM_REG_MIPS_CP0_KSCRATCH1
,
568 KVM_REG_MIPS_CP0_KSCRATCH2
,
569 KVM_REG_MIPS_CP0_KSCRATCH3
,
570 KVM_REG_MIPS_CP0_KSCRATCH4
,
571 KVM_REG_MIPS_CP0_KSCRATCH5
,
572 KVM_REG_MIPS_CP0_KSCRATCH6
,
575 static unsigned long kvm_mips_num_regs(struct kvm_vcpu
*vcpu
)
579 ret
= ARRAY_SIZE(kvm_mips_get_one_regs
);
580 if (kvm_mips_guest_can_have_fpu(&vcpu
->arch
)) {
581 ret
+= ARRAY_SIZE(kvm_mips_get_one_regs_fpu
) + 48;
583 if (boot_cpu_data
.fpu_id
& MIPS_FPIR_F64
)
586 if (kvm_mips_guest_can_have_msa(&vcpu
->arch
))
587 ret
+= ARRAY_SIZE(kvm_mips_get_one_regs_msa
) + 32;
588 ret
+= __arch_hweight8(vcpu
->arch
.kscratch_enabled
);
589 ret
+= kvm_mips_callbacks
->num_regs(vcpu
);
594 static int kvm_mips_copy_reg_indices(struct kvm_vcpu
*vcpu
, u64 __user
*indices
)
599 if (copy_to_user(indices
, kvm_mips_get_one_regs
,
600 sizeof(kvm_mips_get_one_regs
)))
602 indices
+= ARRAY_SIZE(kvm_mips_get_one_regs
);
604 if (kvm_mips_guest_can_have_fpu(&vcpu
->arch
)) {
605 if (copy_to_user(indices
, kvm_mips_get_one_regs_fpu
,
606 sizeof(kvm_mips_get_one_regs_fpu
)))
608 indices
+= ARRAY_SIZE(kvm_mips_get_one_regs_fpu
);
610 for (i
= 0; i
< 32; ++i
) {
611 index
= KVM_REG_MIPS_FPR_32(i
);
612 if (copy_to_user(indices
, &index
, sizeof(index
)))
616 /* skip odd doubles if no F64 */
617 if (i
& 1 && !(boot_cpu_data
.fpu_id
& MIPS_FPIR_F64
))
620 index
= KVM_REG_MIPS_FPR_64(i
);
621 if (copy_to_user(indices
, &index
, sizeof(index
)))
627 if (kvm_mips_guest_can_have_msa(&vcpu
->arch
)) {
628 if (copy_to_user(indices
, kvm_mips_get_one_regs_msa
,
629 sizeof(kvm_mips_get_one_regs_msa
)))
631 indices
+= ARRAY_SIZE(kvm_mips_get_one_regs_msa
);
633 for (i
= 0; i
< 32; ++i
) {
634 index
= KVM_REG_MIPS_VEC_128(i
);
635 if (copy_to_user(indices
, &index
, sizeof(index
)))
641 for (i
= 0; i
< 6; ++i
) {
642 if (!(vcpu
->arch
.kscratch_enabled
& BIT(i
+ 2)))
645 if (copy_to_user(indices
, &kvm_mips_get_one_regs_kscratch
[i
],
646 sizeof(kvm_mips_get_one_regs_kscratch
[i
])))
651 return kvm_mips_callbacks
->copy_reg_indices(vcpu
, indices
);
654 static int kvm_mips_get_reg(struct kvm_vcpu
*vcpu
,
655 const struct kvm_one_reg
*reg
)
657 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
658 struct mips_fpu_struct
*fpu
= &vcpu
->arch
.fpu
;
665 /* General purpose registers */
666 case KVM_REG_MIPS_R0
... KVM_REG_MIPS_R31
:
667 v
= (long)vcpu
->arch
.gprs
[reg
->id
- KVM_REG_MIPS_R0
];
669 case KVM_REG_MIPS_HI
:
670 v
= (long)vcpu
->arch
.hi
;
672 case KVM_REG_MIPS_LO
:
673 v
= (long)vcpu
->arch
.lo
;
675 case KVM_REG_MIPS_PC
:
676 v
= (long)vcpu
->arch
.pc
;
679 /* Floating point registers */
680 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
681 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
683 idx
= reg
->id
- KVM_REG_MIPS_FPR_32(0);
684 /* Odd singles in top of even double when FR=0 */
685 if (kvm_read_c0_guest_status(cop0
) & ST0_FR
)
686 v
= get_fpr32(&fpu
->fpr
[idx
], 0);
688 v
= get_fpr32(&fpu
->fpr
[idx
& ~1], idx
& 1);
690 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
691 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
693 idx
= reg
->id
- KVM_REG_MIPS_FPR_64(0);
694 /* Can't access odd doubles in FR=0 mode */
695 if (idx
& 1 && !(kvm_read_c0_guest_status(cop0
) & ST0_FR
))
697 v
= get_fpr64(&fpu
->fpr
[idx
], 0);
699 case KVM_REG_MIPS_FCR_IR
:
700 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
702 v
= boot_cpu_data
.fpu_id
;
704 case KVM_REG_MIPS_FCR_CSR
:
705 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
710 /* MIPS SIMD Architecture (MSA) registers */
711 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
712 if (!kvm_mips_guest_has_msa(&vcpu
->arch
))
714 /* Can't access MSA registers in FR=0 mode */
715 if (!(kvm_read_c0_guest_status(cop0
) & ST0_FR
))
717 idx
= reg
->id
- KVM_REG_MIPS_VEC_128(0);
718 #ifdef CONFIG_CPU_LITTLE_ENDIAN
719 /* least significant byte first */
720 vs
[0] = get_fpr64(&fpu
->fpr
[idx
], 0);
721 vs
[1] = get_fpr64(&fpu
->fpr
[idx
], 1);
723 /* most significant byte first */
724 vs
[0] = get_fpr64(&fpu
->fpr
[idx
], 1);
725 vs
[1] = get_fpr64(&fpu
->fpr
[idx
], 0);
728 case KVM_REG_MIPS_MSA_IR
:
729 if (!kvm_mips_guest_has_msa(&vcpu
->arch
))
731 v
= boot_cpu_data
.msa_id
;
733 case KVM_REG_MIPS_MSA_CSR
:
734 if (!kvm_mips_guest_has_msa(&vcpu
->arch
))
739 /* Co-processor 0 registers */
740 case KVM_REG_MIPS_CP0_INDEX
:
741 v
= (long)kvm_read_c0_guest_index(cop0
);
743 case KVM_REG_MIPS_CP0_CONTEXT
:
744 v
= (long)kvm_read_c0_guest_context(cop0
);
746 case KVM_REG_MIPS_CP0_USERLOCAL
:
747 v
= (long)kvm_read_c0_guest_userlocal(cop0
);
749 case KVM_REG_MIPS_CP0_PAGEMASK
:
750 v
= (long)kvm_read_c0_guest_pagemask(cop0
);
752 case KVM_REG_MIPS_CP0_WIRED
:
753 v
= (long)kvm_read_c0_guest_wired(cop0
);
755 case KVM_REG_MIPS_CP0_HWRENA
:
756 v
= (long)kvm_read_c0_guest_hwrena(cop0
);
758 case KVM_REG_MIPS_CP0_BADVADDR
:
759 v
= (long)kvm_read_c0_guest_badvaddr(cop0
);
761 case KVM_REG_MIPS_CP0_ENTRYHI
:
762 v
= (long)kvm_read_c0_guest_entryhi(cop0
);
764 case KVM_REG_MIPS_CP0_COMPARE
:
765 v
= (long)kvm_read_c0_guest_compare(cop0
);
767 case KVM_REG_MIPS_CP0_STATUS
:
768 v
= (long)kvm_read_c0_guest_status(cop0
);
770 case KVM_REG_MIPS_CP0_CAUSE
:
771 v
= (long)kvm_read_c0_guest_cause(cop0
);
773 case KVM_REG_MIPS_CP0_EPC
:
774 v
= (long)kvm_read_c0_guest_epc(cop0
);
776 case KVM_REG_MIPS_CP0_PRID
:
777 v
= (long)kvm_read_c0_guest_prid(cop0
);
779 case KVM_REG_MIPS_CP0_CONFIG
:
780 v
= (long)kvm_read_c0_guest_config(cop0
);
782 case KVM_REG_MIPS_CP0_CONFIG1
:
783 v
= (long)kvm_read_c0_guest_config1(cop0
);
785 case KVM_REG_MIPS_CP0_CONFIG2
:
786 v
= (long)kvm_read_c0_guest_config2(cop0
);
788 case KVM_REG_MIPS_CP0_CONFIG3
:
789 v
= (long)kvm_read_c0_guest_config3(cop0
);
791 case KVM_REG_MIPS_CP0_CONFIG4
:
792 v
= (long)kvm_read_c0_guest_config4(cop0
);
794 case KVM_REG_MIPS_CP0_CONFIG5
:
795 v
= (long)kvm_read_c0_guest_config5(cop0
);
797 case KVM_REG_MIPS_CP0_CONFIG7
:
798 v
= (long)kvm_read_c0_guest_config7(cop0
);
800 case KVM_REG_MIPS_CP0_ERROREPC
:
801 v
= (long)kvm_read_c0_guest_errorepc(cop0
);
803 case KVM_REG_MIPS_CP0_KSCRATCH1
... KVM_REG_MIPS_CP0_KSCRATCH6
:
804 idx
= reg
->id
- KVM_REG_MIPS_CP0_KSCRATCH1
+ 2;
805 if (!(vcpu
->arch
.kscratch_enabled
& BIT(idx
)))
809 v
= (long)kvm_read_c0_guest_kscratch1(cop0
);
812 v
= (long)kvm_read_c0_guest_kscratch2(cop0
);
815 v
= (long)kvm_read_c0_guest_kscratch3(cop0
);
818 v
= (long)kvm_read_c0_guest_kscratch4(cop0
);
821 v
= (long)kvm_read_c0_guest_kscratch5(cop0
);
824 v
= (long)kvm_read_c0_guest_kscratch6(cop0
);
828 /* registers to be handled specially */
830 ret
= kvm_mips_callbacks
->get_one_reg(vcpu
, reg
, &v
);
835 if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
) {
836 u64 __user
*uaddr64
= (u64 __user
*)(long)reg
->addr
;
838 return put_user(v
, uaddr64
);
839 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U32
) {
840 u32 __user
*uaddr32
= (u32 __user
*)(long)reg
->addr
;
843 return put_user(v32
, uaddr32
);
844 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U128
) {
845 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
847 return copy_to_user(uaddr
, vs
, 16) ? -EFAULT
: 0;
853 static int kvm_mips_set_reg(struct kvm_vcpu
*vcpu
,
854 const struct kvm_one_reg
*reg
)
856 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
857 struct mips_fpu_struct
*fpu
= &vcpu
->arch
.fpu
;
862 if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
) {
863 u64 __user
*uaddr64
= (u64 __user
*)(long)reg
->addr
;
865 if (get_user(v
, uaddr64
) != 0)
867 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U32
) {
868 u32 __user
*uaddr32
= (u32 __user
*)(long)reg
->addr
;
871 if (get_user(v32
, uaddr32
) != 0)
874 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U128
) {
875 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
877 return copy_from_user(vs
, uaddr
, 16) ? -EFAULT
: 0;
883 /* General purpose registers */
884 case KVM_REG_MIPS_R0
:
885 /* Silently ignore requests to set $0 */
887 case KVM_REG_MIPS_R1
... KVM_REG_MIPS_R31
:
888 vcpu
->arch
.gprs
[reg
->id
- KVM_REG_MIPS_R0
] = v
;
890 case KVM_REG_MIPS_HI
:
893 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
);
1088 /* Treat the NMI as a CPU reset */
1089 r
= kvm_mips_reset_vcpu(vcpu
);
1093 struct kvm_mips_interrupt irq
;
1096 if (copy_from_user(&irq
, argp
, sizeof(irq
)))
1099 kvm_debug("[%d] %s: irq: %d\n", vcpu
->vcpu_id
, __func__
,
1102 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1105 case KVM_ENABLE_CAP
: {
1106 struct kvm_enable_cap cap
;
1109 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
1111 r
= kvm_vcpu_ioctl_enable_cap(vcpu
, &cap
);
1122 /* Get (and clear) the dirty memory log for a memory slot. */
1123 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
, struct kvm_dirty_log
*log
)
1125 struct kvm_memslots
*slots
;
1126 struct kvm_memory_slot
*memslot
;
1127 unsigned long ga
, ga_end
;
1132 mutex_lock(&kvm
->slots_lock
);
1134 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1138 /* If nothing is dirty, don't bother messing with page tables. */
1140 slots
= kvm_memslots(kvm
);
1141 memslot
= id_to_memslot(slots
, log
->slot
);
1143 ga
= memslot
->base_gfn
<< PAGE_SHIFT
;
1144 ga_end
= ga
+ (memslot
->npages
<< PAGE_SHIFT
);
1146 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__
, ga
,
1149 n
= kvm_dirty_bitmap_bytes(memslot
);
1150 memset(memslot
->dirty_bitmap
, 0, n
);
1155 mutex_unlock(&kvm
->slots_lock
);
1160 long kvm_arch_vm_ioctl(struct file
*filp
, unsigned int ioctl
, unsigned long arg
)
1172 int kvm_arch_init(void *opaque
)
1174 if (kvm_mips_callbacks
) {
1175 kvm_err("kvm: module already exists\n");
1179 return kvm_mips_emulation_init(&kvm_mips_callbacks
);
1182 void kvm_arch_exit(void)
1184 kvm_mips_callbacks
= NULL
;
1187 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
1188 struct kvm_sregs
*sregs
)
1190 return -ENOIOCTLCMD
;
1193 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
1194 struct kvm_sregs
*sregs
)
1196 return -ENOIOCTLCMD
;
1199 void kvm_arch_vcpu_postcreate(struct kvm_vcpu
*vcpu
)
1203 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
1205 return -ENOIOCTLCMD
;
1208 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
1210 return -ENOIOCTLCMD
;
1213 int kvm_arch_vcpu_fault(struct kvm_vcpu
*vcpu
, struct vm_fault
*vmf
)
1215 return VM_FAULT_SIGBUS
;
1218 int kvm_vm_ioctl_check_extension(struct kvm
*kvm
, long ext
)
1223 case KVM_CAP_ONE_REG
:
1224 case KVM_CAP_ENABLE_CAP
:
1227 case KVM_CAP_COALESCED_MMIO
:
1228 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
1230 case KVM_CAP_MIPS_FPU
:
1231 /* We don't handle systems with inconsistent cpu_has_fpu */
1232 r
= !!raw_cpu_has_fpu
;
1234 case KVM_CAP_MIPS_MSA
:
1236 * We don't support MSA vector partitioning yet:
1237 * 1) It would require explicit support which can't be tested
1238 * yet due to lack of support in current hardware.
1239 * 2) It extends the state that would need to be saved/restored
1240 * by e.g. QEMU for migration.
1242 * When vector partitioning hardware becomes available, support
1243 * could be added by requiring a flag when enabling
1244 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1245 * to save/restore the appropriate extra state.
1247 r
= cpu_has_msa
&& !(boot_cpu_data
.msa_id
& MSA_IR_WRPF
);
1256 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
1258 return kvm_mips_pending_timer(vcpu
);
1261 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu
*vcpu
)
1264 struct mips_coproc
*cop0
;
1269 kvm_debug("VCPU Register Dump:\n");
1270 kvm_debug("\tpc = 0x%08lx\n", vcpu
->arch
.pc
);
1271 kvm_debug("\texceptions: %08lx\n", vcpu
->arch
.pending_exceptions
);
1273 for (i
= 0; i
< 32; i
+= 4) {
1274 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i
,
1276 vcpu
->arch
.gprs
[i
+ 1],
1277 vcpu
->arch
.gprs
[i
+ 2], vcpu
->arch
.gprs
[i
+ 3]);
1279 kvm_debug("\thi: 0x%08lx\n", vcpu
->arch
.hi
);
1280 kvm_debug("\tlo: 0x%08lx\n", vcpu
->arch
.lo
);
1282 cop0
= vcpu
->arch
.cop0
;
1283 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1284 kvm_read_c0_guest_status(cop0
),
1285 kvm_read_c0_guest_cause(cop0
));
1287 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0
));
1292 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
1296 for (i
= 1; i
< ARRAY_SIZE(vcpu
->arch
.gprs
); i
++)
1297 vcpu
->arch
.gprs
[i
] = regs
->gpr
[i
];
1298 vcpu
->arch
.gprs
[0] = 0; /* zero is special, and cannot be set. */
1299 vcpu
->arch
.hi
= regs
->hi
;
1300 vcpu
->arch
.lo
= regs
->lo
;
1301 vcpu
->arch
.pc
= regs
->pc
;
1306 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
1310 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.gprs
); i
++)
1311 regs
->gpr
[i
] = vcpu
->arch
.gprs
[i
];
1313 regs
->hi
= vcpu
->arch
.hi
;
1314 regs
->lo
= vcpu
->arch
.lo
;
1315 regs
->pc
= vcpu
->arch
.pc
;
1320 static void kvm_mips_comparecount_func(unsigned long data
)
1322 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)data
;
1324 kvm_mips_callbacks
->queue_timer_int(vcpu
);
1326 vcpu
->arch
.wait
= 0;
1327 if (swait_active(&vcpu
->wq
))
1328 swake_up(&vcpu
->wq
);
1331 /* low level hrtimer wake routine */
1332 static enum hrtimer_restart
kvm_mips_comparecount_wakeup(struct hrtimer
*timer
)
1334 struct kvm_vcpu
*vcpu
;
1336 vcpu
= container_of(timer
, struct kvm_vcpu
, arch
.comparecount_timer
);
1337 kvm_mips_comparecount_func((unsigned long) vcpu
);
1338 return kvm_mips_count_timeout(vcpu
);
1341 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
1343 kvm_mips_callbacks
->vcpu_init(vcpu
);
1344 hrtimer_init(&vcpu
->arch
.comparecount_timer
, CLOCK_MONOTONIC
,
1346 vcpu
->arch
.comparecount_timer
.function
= kvm_mips_comparecount_wakeup
;
1350 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
1351 struct kvm_translation
*tr
)
1356 /* Initial guest state */
1357 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
1359 return kvm_mips_callbacks
->vcpu_setup(vcpu
);
1362 static void kvm_mips_set_c0_status(void)
1364 u32 status
= read_c0_status();
1369 write_c0_status(status
);
1374 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1376 int kvm_mips_handle_exit(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
1378 u32 cause
= vcpu
->arch
.host_cp0_cause
;
1379 u32 exccode
= (cause
>> CAUSEB_EXCCODE
) & 0x1f;
1380 u32 __user
*opc
= (u32 __user
*) vcpu
->arch
.pc
;
1381 unsigned long badvaddr
= vcpu
->arch
.host_cp0_badvaddr
;
1382 enum emulation_result er
= EMULATE_DONE
;
1383 int ret
= RESUME_GUEST
;
1385 /* re-enable HTW before enabling interrupts */
1388 /* Set a default exit reason */
1389 run
->exit_reason
= KVM_EXIT_UNKNOWN
;
1390 run
->ready_for_interrupt_injection
= 1;
1393 * Set the appropriate status bits based on host CPU features,
1394 * before we hit the scheduler
1396 kvm_mips_set_c0_status();
1400 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1401 cause
, opc
, run
, vcpu
);
1402 trace_kvm_exit(vcpu
, exccode
);
1405 * Do a privilege check, if in UM most of these exit conditions end up
1406 * causing an exception to be delivered to the Guest Kernel
1408 er
= kvm_mips_check_privilege(cause
, opc
, run
, vcpu
);
1409 if (er
== EMULATE_PRIV_FAIL
) {
1411 } else if (er
== EMULATE_FAIL
) {
1412 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1419 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu
->vcpu_id
, opc
);
1421 ++vcpu
->stat
.int_exits
;
1430 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc
);
1432 ++vcpu
->stat
.cop_unusable_exits
;
1433 ret
= kvm_mips_callbacks
->handle_cop_unusable(vcpu
);
1434 /* XXXKYMA: Might need to return to user space */
1435 if (run
->exit_reason
== KVM_EXIT_IRQ_WINDOW_OPEN
)
1440 ++vcpu
->stat
.tlbmod_exits
;
1441 ret
= kvm_mips_callbacks
->handle_tlb_mod(vcpu
);
1445 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1446 cause
, kvm_read_c0_guest_status(vcpu
->arch
.cop0
), opc
,
1449 ++vcpu
->stat
.tlbmiss_st_exits
;
1450 ret
= kvm_mips_callbacks
->handle_tlb_st_miss(vcpu
);
1454 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1455 cause
, opc
, badvaddr
);
1457 ++vcpu
->stat
.tlbmiss_ld_exits
;
1458 ret
= kvm_mips_callbacks
->handle_tlb_ld_miss(vcpu
);
1462 ++vcpu
->stat
.addrerr_st_exits
;
1463 ret
= kvm_mips_callbacks
->handle_addr_err_st(vcpu
);
1467 ++vcpu
->stat
.addrerr_ld_exits
;
1468 ret
= kvm_mips_callbacks
->handle_addr_err_ld(vcpu
);
1472 ++vcpu
->stat
.syscall_exits
;
1473 ret
= kvm_mips_callbacks
->handle_syscall(vcpu
);
1477 ++vcpu
->stat
.resvd_inst_exits
;
1478 ret
= kvm_mips_callbacks
->handle_res_inst(vcpu
);
1482 ++vcpu
->stat
.break_inst_exits
;
1483 ret
= kvm_mips_callbacks
->handle_break(vcpu
);
1487 ++vcpu
->stat
.trap_inst_exits
;
1488 ret
= kvm_mips_callbacks
->handle_trap(vcpu
);
1491 case EXCCODE_MSAFPE
:
1492 ++vcpu
->stat
.msa_fpe_exits
;
1493 ret
= kvm_mips_callbacks
->handle_msa_fpe(vcpu
);
1497 ++vcpu
->stat
.fpe_exits
;
1498 ret
= kvm_mips_callbacks
->handle_fpe(vcpu
);
1501 case EXCCODE_MSADIS
:
1502 ++vcpu
->stat
.msa_disabled_exits
;
1503 ret
= kvm_mips_callbacks
->handle_msa_disabled(vcpu
);
1507 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1508 exccode
, opc
, kvm_get_inst(opc
, vcpu
), badvaddr
,
1509 kvm_read_c0_guest_status(vcpu
->arch
.cop0
));
1510 kvm_arch_vcpu_dump_regs(vcpu
);
1511 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1518 local_irq_disable();
1520 if (er
== EMULATE_DONE
&& !(ret
& RESUME_HOST
))
1521 kvm_mips_deliver_interrupts(vcpu
, cause
);
1523 if (!(ret
& RESUME_HOST
)) {
1524 /* Only check for signals if not already exiting to userspace */
1525 if (signal_pending(current
)) {
1526 run
->exit_reason
= KVM_EXIT_INTR
;
1527 ret
= (-EINTR
<< 2) | RESUME_HOST
;
1528 ++vcpu
->stat
.signal_exits
;
1529 trace_kvm_exit(vcpu
, KVM_TRACE_EXIT_SIGNAL
);
1533 if (ret
== RESUME_GUEST
) {
1534 trace_kvm_reenter(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
);