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 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
305 * limits us to the low 512MB of physical address space. If the memory
306 * we allocate is out of range, just give up now.
308 if (!cpu_has_ebase_wg
&& virt_to_phys(gebase
) >= 0x20000000) {
309 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
312 goto out_free_gebase
;
316 vcpu
->arch
.guest_ebase
= gebase
;
318 /* Build guest exception vectors dynamically in unmapped memory */
319 handler
= gebase
+ 0x2000;
321 /* TLB Refill, EXL = 0 */
322 kvm_mips_build_exception(gebase
, handler
);
324 /* General Exception Entry point */
325 kvm_mips_build_exception(gebase
+ 0x180, handler
);
327 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
328 for (i
= 0; i
< 8; i
++) {
329 kvm_debug("L1 Vectored handler @ %p\n",
330 gebase
+ 0x200 + (i
* VECTORSPACING
));
331 kvm_mips_build_exception(gebase
+ 0x200 + i
* VECTORSPACING
,
335 /* General exit handler */
337 p
= kvm_mips_build_exit(p
);
339 /* Guest entry routine */
340 vcpu
->arch
.vcpu_run
= p
;
341 p
= kvm_mips_build_vcpu_run(p
);
343 /* Dump the generated code */
344 pr_debug("#include <asm/asm.h>\n");
345 pr_debug("#include <asm/regdef.h>\n");
347 dump_handler("kvm_vcpu_run", vcpu
->arch
.vcpu_run
, p
);
348 dump_handler("kvm_gen_exc", gebase
+ 0x180, gebase
+ 0x200);
349 dump_handler("kvm_exit", gebase
+ 0x2000, vcpu
->arch
.vcpu_run
);
351 /* Invalidate the icache for these ranges */
352 local_flush_icache_range((unsigned long)gebase
,
353 (unsigned long)gebase
+ ALIGN(size
, PAGE_SIZE
));
356 * Allocate comm page for guest kernel, a TLB will be reserved for
357 * mapping GVA @ 0xFFFF8000 to this page
359 vcpu
->arch
.kseg0_commpage
= kzalloc(PAGE_SIZE
<< 1, GFP_KERNEL
);
361 if (!vcpu
->arch
.kseg0_commpage
) {
363 goto out_free_gebase
;
366 kvm_debug("Allocated COMM page @ %p\n", vcpu
->arch
.kseg0_commpage
);
367 kvm_mips_commpage_init(vcpu
);
370 vcpu
->arch
.last_sched_cpu
= -1;
372 /* Start off the timer */
373 kvm_mips_init_count(vcpu
);
381 kvm_vcpu_uninit(vcpu
);
390 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
392 hrtimer_cancel(&vcpu
->arch
.comparecount_timer
);
394 kvm_vcpu_uninit(vcpu
);
396 kvm_mips_dump_stats(vcpu
);
398 kfree(vcpu
->arch
.guest_ebase
);
399 kfree(vcpu
->arch
.kseg0_commpage
);
403 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
405 kvm_arch_vcpu_free(vcpu
);
408 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
409 struct kvm_guest_debug
*dbg
)
414 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
419 if (vcpu
->sigset_active
)
420 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
422 if (vcpu
->mmio_needed
) {
423 if (!vcpu
->mmio_is_write
)
424 kvm_mips_complete_mmio_load(vcpu
, run
);
425 vcpu
->mmio_needed
= 0;
431 /* Check if we have any exceptions/interrupts pending */
432 kvm_mips_deliver_interrupts(vcpu
,
433 kvm_read_c0_guest_cause(vcpu
->arch
.cop0
));
435 guest_enter_irqoff();
437 /* Disable hardware page table walking while in guest */
440 trace_kvm_enter(vcpu
);
441 r
= vcpu
->arch
.vcpu_run(run
, vcpu
);
444 /* Re-enable HTW before enabling interrupts */
450 if (vcpu
->sigset_active
)
451 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
456 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
457 struct kvm_mips_interrupt
*irq
)
459 int intr
= (int)irq
->irq
;
460 struct kvm_vcpu
*dvcpu
= NULL
;
462 if (intr
== 3 || intr
== -3 || intr
== 4 || intr
== -4)
463 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__
, irq
->cpu
,
469 dvcpu
= vcpu
->kvm
->vcpus
[irq
->cpu
];
471 if (intr
== 2 || intr
== 3 || intr
== 4) {
472 kvm_mips_callbacks
->queue_io_int(dvcpu
, irq
);
474 } else if (intr
== -2 || intr
== -3 || intr
== -4) {
475 kvm_mips_callbacks
->dequeue_io_int(dvcpu
, irq
);
477 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__
,
482 dvcpu
->arch
.wait
= 0;
484 if (swait_active(&dvcpu
->wq
))
485 swake_up(&dvcpu
->wq
);
490 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
491 struct kvm_mp_state
*mp_state
)
496 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
497 struct kvm_mp_state
*mp_state
)
502 static u64 kvm_mips_get_one_regs
[] = {
536 #ifndef CONFIG_CPU_MIPSR6
542 KVM_REG_MIPS_CP0_INDEX
,
543 KVM_REG_MIPS_CP0_CONTEXT
,
544 KVM_REG_MIPS_CP0_USERLOCAL
,
545 KVM_REG_MIPS_CP0_PAGEMASK
,
546 KVM_REG_MIPS_CP0_WIRED
,
547 KVM_REG_MIPS_CP0_HWRENA
,
548 KVM_REG_MIPS_CP0_BADVADDR
,
549 KVM_REG_MIPS_CP0_COUNT
,
550 KVM_REG_MIPS_CP0_ENTRYHI
,
551 KVM_REG_MIPS_CP0_COMPARE
,
552 KVM_REG_MIPS_CP0_STATUS
,
553 KVM_REG_MIPS_CP0_CAUSE
,
554 KVM_REG_MIPS_CP0_EPC
,
555 KVM_REG_MIPS_CP0_PRID
,
556 KVM_REG_MIPS_CP0_CONFIG
,
557 KVM_REG_MIPS_CP0_CONFIG1
,
558 KVM_REG_MIPS_CP0_CONFIG2
,
559 KVM_REG_MIPS_CP0_CONFIG3
,
560 KVM_REG_MIPS_CP0_CONFIG4
,
561 KVM_REG_MIPS_CP0_CONFIG5
,
562 KVM_REG_MIPS_CP0_CONFIG7
,
563 KVM_REG_MIPS_CP0_ERROREPC
,
565 KVM_REG_MIPS_COUNT_CTL
,
566 KVM_REG_MIPS_COUNT_RESUME
,
567 KVM_REG_MIPS_COUNT_HZ
,
570 static u64 kvm_mips_get_one_regs_fpu
[] = {
572 KVM_REG_MIPS_FCR_CSR
,
575 static u64 kvm_mips_get_one_regs_msa
[] = {
577 KVM_REG_MIPS_MSA_CSR
,
580 static u64 kvm_mips_get_one_regs_kscratch
[] = {
581 KVM_REG_MIPS_CP0_KSCRATCH1
,
582 KVM_REG_MIPS_CP0_KSCRATCH2
,
583 KVM_REG_MIPS_CP0_KSCRATCH3
,
584 KVM_REG_MIPS_CP0_KSCRATCH4
,
585 KVM_REG_MIPS_CP0_KSCRATCH5
,
586 KVM_REG_MIPS_CP0_KSCRATCH6
,
589 static unsigned long kvm_mips_num_regs(struct kvm_vcpu
*vcpu
)
593 ret
= ARRAY_SIZE(kvm_mips_get_one_regs
);
594 if (kvm_mips_guest_can_have_fpu(&vcpu
->arch
)) {
595 ret
+= ARRAY_SIZE(kvm_mips_get_one_regs_fpu
) + 48;
597 if (boot_cpu_data
.fpu_id
& MIPS_FPIR_F64
)
600 if (kvm_mips_guest_can_have_msa(&vcpu
->arch
))
601 ret
+= ARRAY_SIZE(kvm_mips_get_one_regs_msa
) + 32;
602 ret
+= __arch_hweight8(vcpu
->arch
.kscratch_enabled
);
603 ret
+= kvm_mips_callbacks
->num_regs(vcpu
);
608 static int kvm_mips_copy_reg_indices(struct kvm_vcpu
*vcpu
, u64 __user
*indices
)
613 if (copy_to_user(indices
, kvm_mips_get_one_regs
,
614 sizeof(kvm_mips_get_one_regs
)))
616 indices
+= ARRAY_SIZE(kvm_mips_get_one_regs
);
618 if (kvm_mips_guest_can_have_fpu(&vcpu
->arch
)) {
619 if (copy_to_user(indices
, kvm_mips_get_one_regs_fpu
,
620 sizeof(kvm_mips_get_one_regs_fpu
)))
622 indices
+= ARRAY_SIZE(kvm_mips_get_one_regs_fpu
);
624 for (i
= 0; i
< 32; ++i
) {
625 index
= KVM_REG_MIPS_FPR_32(i
);
626 if (copy_to_user(indices
, &index
, sizeof(index
)))
630 /* skip odd doubles if no F64 */
631 if (i
& 1 && !(boot_cpu_data
.fpu_id
& MIPS_FPIR_F64
))
634 index
= KVM_REG_MIPS_FPR_64(i
);
635 if (copy_to_user(indices
, &index
, sizeof(index
)))
641 if (kvm_mips_guest_can_have_msa(&vcpu
->arch
)) {
642 if (copy_to_user(indices
, kvm_mips_get_one_regs_msa
,
643 sizeof(kvm_mips_get_one_regs_msa
)))
645 indices
+= ARRAY_SIZE(kvm_mips_get_one_regs_msa
);
647 for (i
= 0; i
< 32; ++i
) {
648 index
= KVM_REG_MIPS_VEC_128(i
);
649 if (copy_to_user(indices
, &index
, sizeof(index
)))
655 for (i
= 0; i
< 6; ++i
) {
656 if (!(vcpu
->arch
.kscratch_enabled
& BIT(i
+ 2)))
659 if (copy_to_user(indices
, &kvm_mips_get_one_regs_kscratch
[i
],
660 sizeof(kvm_mips_get_one_regs_kscratch
[i
])))
665 return kvm_mips_callbacks
->copy_reg_indices(vcpu
, indices
);
668 static int kvm_mips_get_reg(struct kvm_vcpu
*vcpu
,
669 const struct kvm_one_reg
*reg
)
671 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
672 struct mips_fpu_struct
*fpu
= &vcpu
->arch
.fpu
;
679 /* General purpose registers */
680 case KVM_REG_MIPS_R0
... KVM_REG_MIPS_R31
:
681 v
= (long)vcpu
->arch
.gprs
[reg
->id
- KVM_REG_MIPS_R0
];
683 #ifndef CONFIG_CPU_MIPSR6
684 case KVM_REG_MIPS_HI
:
685 v
= (long)vcpu
->arch
.hi
;
687 case KVM_REG_MIPS_LO
:
688 v
= (long)vcpu
->arch
.lo
;
691 case KVM_REG_MIPS_PC
:
692 v
= (long)vcpu
->arch
.pc
;
695 /* Floating point registers */
696 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
697 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
699 idx
= reg
->id
- KVM_REG_MIPS_FPR_32(0);
700 /* Odd singles in top of even double when FR=0 */
701 if (kvm_read_c0_guest_status(cop0
) & ST0_FR
)
702 v
= get_fpr32(&fpu
->fpr
[idx
], 0);
704 v
= get_fpr32(&fpu
->fpr
[idx
& ~1], idx
& 1);
706 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
707 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
709 idx
= reg
->id
- KVM_REG_MIPS_FPR_64(0);
710 /* Can't access odd doubles in FR=0 mode */
711 if (idx
& 1 && !(kvm_read_c0_guest_status(cop0
) & ST0_FR
))
713 v
= get_fpr64(&fpu
->fpr
[idx
], 0);
715 case KVM_REG_MIPS_FCR_IR
:
716 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
718 v
= boot_cpu_data
.fpu_id
;
720 case KVM_REG_MIPS_FCR_CSR
:
721 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
726 /* MIPS SIMD Architecture (MSA) registers */
727 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
728 if (!kvm_mips_guest_has_msa(&vcpu
->arch
))
730 /* Can't access MSA registers in FR=0 mode */
731 if (!(kvm_read_c0_guest_status(cop0
) & ST0_FR
))
733 idx
= reg
->id
- KVM_REG_MIPS_VEC_128(0);
734 #ifdef CONFIG_CPU_LITTLE_ENDIAN
735 /* least significant byte first */
736 vs
[0] = get_fpr64(&fpu
->fpr
[idx
], 0);
737 vs
[1] = get_fpr64(&fpu
->fpr
[idx
], 1);
739 /* most significant byte first */
740 vs
[0] = get_fpr64(&fpu
->fpr
[idx
], 1);
741 vs
[1] = get_fpr64(&fpu
->fpr
[idx
], 0);
744 case KVM_REG_MIPS_MSA_IR
:
745 if (!kvm_mips_guest_has_msa(&vcpu
->arch
))
747 v
= boot_cpu_data
.msa_id
;
749 case KVM_REG_MIPS_MSA_CSR
:
750 if (!kvm_mips_guest_has_msa(&vcpu
->arch
))
755 /* Co-processor 0 registers */
756 case KVM_REG_MIPS_CP0_INDEX
:
757 v
= (long)kvm_read_c0_guest_index(cop0
);
759 case KVM_REG_MIPS_CP0_CONTEXT
:
760 v
= (long)kvm_read_c0_guest_context(cop0
);
762 case KVM_REG_MIPS_CP0_USERLOCAL
:
763 v
= (long)kvm_read_c0_guest_userlocal(cop0
);
765 case KVM_REG_MIPS_CP0_PAGEMASK
:
766 v
= (long)kvm_read_c0_guest_pagemask(cop0
);
768 case KVM_REG_MIPS_CP0_WIRED
:
769 v
= (long)kvm_read_c0_guest_wired(cop0
);
771 case KVM_REG_MIPS_CP0_HWRENA
:
772 v
= (long)kvm_read_c0_guest_hwrena(cop0
);
774 case KVM_REG_MIPS_CP0_BADVADDR
:
775 v
= (long)kvm_read_c0_guest_badvaddr(cop0
);
777 case KVM_REG_MIPS_CP0_ENTRYHI
:
778 v
= (long)kvm_read_c0_guest_entryhi(cop0
);
780 case KVM_REG_MIPS_CP0_COMPARE
:
781 v
= (long)kvm_read_c0_guest_compare(cop0
);
783 case KVM_REG_MIPS_CP0_STATUS
:
784 v
= (long)kvm_read_c0_guest_status(cop0
);
786 case KVM_REG_MIPS_CP0_CAUSE
:
787 v
= (long)kvm_read_c0_guest_cause(cop0
);
789 case KVM_REG_MIPS_CP0_EPC
:
790 v
= (long)kvm_read_c0_guest_epc(cop0
);
792 case KVM_REG_MIPS_CP0_PRID
:
793 v
= (long)kvm_read_c0_guest_prid(cop0
);
795 case KVM_REG_MIPS_CP0_CONFIG
:
796 v
= (long)kvm_read_c0_guest_config(cop0
);
798 case KVM_REG_MIPS_CP0_CONFIG1
:
799 v
= (long)kvm_read_c0_guest_config1(cop0
);
801 case KVM_REG_MIPS_CP0_CONFIG2
:
802 v
= (long)kvm_read_c0_guest_config2(cop0
);
804 case KVM_REG_MIPS_CP0_CONFIG3
:
805 v
= (long)kvm_read_c0_guest_config3(cop0
);
807 case KVM_REG_MIPS_CP0_CONFIG4
:
808 v
= (long)kvm_read_c0_guest_config4(cop0
);
810 case KVM_REG_MIPS_CP0_CONFIG5
:
811 v
= (long)kvm_read_c0_guest_config5(cop0
);
813 case KVM_REG_MIPS_CP0_CONFIG7
:
814 v
= (long)kvm_read_c0_guest_config7(cop0
);
816 case KVM_REG_MIPS_CP0_ERROREPC
:
817 v
= (long)kvm_read_c0_guest_errorepc(cop0
);
819 case KVM_REG_MIPS_CP0_KSCRATCH1
... KVM_REG_MIPS_CP0_KSCRATCH6
:
820 idx
= reg
->id
- KVM_REG_MIPS_CP0_KSCRATCH1
+ 2;
821 if (!(vcpu
->arch
.kscratch_enabled
& BIT(idx
)))
825 v
= (long)kvm_read_c0_guest_kscratch1(cop0
);
828 v
= (long)kvm_read_c0_guest_kscratch2(cop0
);
831 v
= (long)kvm_read_c0_guest_kscratch3(cop0
);
834 v
= (long)kvm_read_c0_guest_kscratch4(cop0
);
837 v
= (long)kvm_read_c0_guest_kscratch5(cop0
);
840 v
= (long)kvm_read_c0_guest_kscratch6(cop0
);
844 /* registers to be handled specially */
846 ret
= kvm_mips_callbacks
->get_one_reg(vcpu
, reg
, &v
);
851 if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
) {
852 u64 __user
*uaddr64
= (u64 __user
*)(long)reg
->addr
;
854 return put_user(v
, uaddr64
);
855 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U32
) {
856 u32 __user
*uaddr32
= (u32 __user
*)(long)reg
->addr
;
859 return put_user(v32
, uaddr32
);
860 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U128
) {
861 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
863 return copy_to_user(uaddr
, vs
, 16) ? -EFAULT
: 0;
869 static int kvm_mips_set_reg(struct kvm_vcpu
*vcpu
,
870 const struct kvm_one_reg
*reg
)
872 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
873 struct mips_fpu_struct
*fpu
= &vcpu
->arch
.fpu
;
878 if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
) {
879 u64 __user
*uaddr64
= (u64 __user
*)(long)reg
->addr
;
881 if (get_user(v
, uaddr64
) != 0)
883 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U32
) {
884 u32 __user
*uaddr32
= (u32 __user
*)(long)reg
->addr
;
887 if (get_user(v32
, uaddr32
) != 0)
890 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U128
) {
891 void __user
*uaddr
= (void __user
*)(long)reg
->addr
;
893 return copy_from_user(vs
, uaddr
, 16) ? -EFAULT
: 0;
899 /* General purpose registers */
900 case KVM_REG_MIPS_R0
:
901 /* Silently ignore requests to set $0 */
903 case KVM_REG_MIPS_R1
... KVM_REG_MIPS_R31
:
904 vcpu
->arch
.gprs
[reg
->id
- KVM_REG_MIPS_R0
] = v
;
906 #ifndef CONFIG_CPU_MIPSR6
907 case KVM_REG_MIPS_HI
:
910 case KVM_REG_MIPS_LO
:
914 case KVM_REG_MIPS_PC
:
918 /* Floating point registers */
919 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
920 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
922 idx
= reg
->id
- KVM_REG_MIPS_FPR_32(0);
923 /* Odd singles in top of even double when FR=0 */
924 if (kvm_read_c0_guest_status(cop0
) & ST0_FR
)
925 set_fpr32(&fpu
->fpr
[idx
], 0, v
);
927 set_fpr32(&fpu
->fpr
[idx
& ~1], idx
& 1, v
);
929 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
930 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
932 idx
= reg
->id
- KVM_REG_MIPS_FPR_64(0);
933 /* Can't access odd doubles in FR=0 mode */
934 if (idx
& 1 && !(kvm_read_c0_guest_status(cop0
) & ST0_FR
))
936 set_fpr64(&fpu
->fpr
[idx
], 0, v
);
938 case KVM_REG_MIPS_FCR_IR
:
939 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
943 case KVM_REG_MIPS_FCR_CSR
:
944 if (!kvm_mips_guest_has_fpu(&vcpu
->arch
))
949 /* MIPS SIMD Architecture (MSA) registers */
950 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
951 if (!kvm_mips_guest_has_msa(&vcpu
->arch
))
953 idx
= reg
->id
- KVM_REG_MIPS_VEC_128(0);
954 #ifdef CONFIG_CPU_LITTLE_ENDIAN
955 /* least significant byte first */
956 set_fpr64(&fpu
->fpr
[idx
], 0, vs
[0]);
957 set_fpr64(&fpu
->fpr
[idx
], 1, vs
[1]);
959 /* most significant byte first */
960 set_fpr64(&fpu
->fpr
[idx
], 1, vs
[0]);
961 set_fpr64(&fpu
->fpr
[idx
], 0, vs
[1]);
964 case KVM_REG_MIPS_MSA_IR
:
965 if (!kvm_mips_guest_has_msa(&vcpu
->arch
))
969 case KVM_REG_MIPS_MSA_CSR
:
970 if (!kvm_mips_guest_has_msa(&vcpu
->arch
))
975 /* Co-processor 0 registers */
976 case KVM_REG_MIPS_CP0_INDEX
:
977 kvm_write_c0_guest_index(cop0
, v
);
979 case KVM_REG_MIPS_CP0_CONTEXT
:
980 kvm_write_c0_guest_context(cop0
, v
);
982 case KVM_REG_MIPS_CP0_USERLOCAL
:
983 kvm_write_c0_guest_userlocal(cop0
, v
);
985 case KVM_REG_MIPS_CP0_PAGEMASK
:
986 kvm_write_c0_guest_pagemask(cop0
, v
);
988 case KVM_REG_MIPS_CP0_WIRED
:
989 kvm_write_c0_guest_wired(cop0
, v
);
991 case KVM_REG_MIPS_CP0_HWRENA
:
992 kvm_write_c0_guest_hwrena(cop0
, v
);
994 case KVM_REG_MIPS_CP0_BADVADDR
:
995 kvm_write_c0_guest_badvaddr(cop0
, v
);
997 case KVM_REG_MIPS_CP0_ENTRYHI
:
998 kvm_write_c0_guest_entryhi(cop0
, v
);
1000 case KVM_REG_MIPS_CP0_STATUS
:
1001 kvm_write_c0_guest_status(cop0
, v
);
1003 case KVM_REG_MIPS_CP0_EPC
:
1004 kvm_write_c0_guest_epc(cop0
, v
);
1006 case KVM_REG_MIPS_CP0_PRID
:
1007 kvm_write_c0_guest_prid(cop0
, v
);
1009 case KVM_REG_MIPS_CP0_ERROREPC
:
1010 kvm_write_c0_guest_errorepc(cop0
, v
);
1012 case KVM_REG_MIPS_CP0_KSCRATCH1
... KVM_REG_MIPS_CP0_KSCRATCH6
:
1013 idx
= reg
->id
- KVM_REG_MIPS_CP0_KSCRATCH1
+ 2;
1014 if (!(vcpu
->arch
.kscratch_enabled
& BIT(idx
)))
1018 kvm_write_c0_guest_kscratch1(cop0
, v
);
1021 kvm_write_c0_guest_kscratch2(cop0
, v
);
1024 kvm_write_c0_guest_kscratch3(cop0
, v
);
1027 kvm_write_c0_guest_kscratch4(cop0
, v
);
1030 kvm_write_c0_guest_kscratch5(cop0
, v
);
1033 kvm_write_c0_guest_kscratch6(cop0
, v
);
1037 /* registers to be handled specially */
1039 return kvm_mips_callbacks
->set_one_reg(vcpu
, reg
, v
);
1044 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu
*vcpu
,
1045 struct kvm_enable_cap
*cap
)
1049 if (!kvm_vm_ioctl_check_extension(vcpu
->kvm
, cap
->cap
))
1057 case KVM_CAP_MIPS_FPU
:
1058 vcpu
->arch
.fpu_enabled
= true;
1060 case KVM_CAP_MIPS_MSA
:
1061 vcpu
->arch
.msa_enabled
= true;
1071 long kvm_arch_vcpu_ioctl(struct file
*filp
, unsigned int ioctl
,
1074 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1075 void __user
*argp
= (void __user
*)arg
;
1079 case KVM_SET_ONE_REG
:
1080 case KVM_GET_ONE_REG
: {
1081 struct kvm_one_reg reg
;
1083 if (copy_from_user(®
, argp
, sizeof(reg
)))
1085 if (ioctl
== KVM_SET_ONE_REG
)
1086 return kvm_mips_set_reg(vcpu
, ®
);
1088 return kvm_mips_get_reg(vcpu
, ®
);
1090 case KVM_GET_REG_LIST
: {
1091 struct kvm_reg_list __user
*user_list
= argp
;
1092 struct kvm_reg_list reg_list
;
1095 if (copy_from_user(®_list
, user_list
, sizeof(reg_list
)))
1098 reg_list
.n
= kvm_mips_num_regs(vcpu
);
1099 if (copy_to_user(user_list
, ®_list
, sizeof(reg_list
)))
1103 return kvm_mips_copy_reg_indices(vcpu
, user_list
->reg
);
1106 /* Treat the NMI as a CPU reset */
1107 r
= kvm_mips_reset_vcpu(vcpu
);
1111 struct kvm_mips_interrupt irq
;
1114 if (copy_from_user(&irq
, argp
, sizeof(irq
)))
1117 kvm_debug("[%d] %s: irq: %d\n", vcpu
->vcpu_id
, __func__
,
1120 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1123 case KVM_ENABLE_CAP
: {
1124 struct kvm_enable_cap cap
;
1127 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
1129 r
= kvm_vcpu_ioctl_enable_cap(vcpu
, &cap
);
1140 /* Get (and clear) the dirty memory log for a memory slot. */
1141 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
, struct kvm_dirty_log
*log
)
1143 struct kvm_memslots
*slots
;
1144 struct kvm_memory_slot
*memslot
;
1145 unsigned long ga
, ga_end
;
1150 mutex_lock(&kvm
->slots_lock
);
1152 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1156 /* If nothing is dirty, don't bother messing with page tables. */
1158 slots
= kvm_memslots(kvm
);
1159 memslot
= id_to_memslot(slots
, log
->slot
);
1161 ga
= memslot
->base_gfn
<< PAGE_SHIFT
;
1162 ga_end
= ga
+ (memslot
->npages
<< PAGE_SHIFT
);
1164 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__
, ga
,
1167 n
= kvm_dirty_bitmap_bytes(memslot
);
1168 memset(memslot
->dirty_bitmap
, 0, n
);
1173 mutex_unlock(&kvm
->slots_lock
);
1178 long kvm_arch_vm_ioctl(struct file
*filp
, unsigned int ioctl
, unsigned long arg
)
1190 int kvm_arch_init(void *opaque
)
1192 if (kvm_mips_callbacks
) {
1193 kvm_err("kvm: module already exists\n");
1197 return kvm_mips_emulation_init(&kvm_mips_callbacks
);
1200 void kvm_arch_exit(void)
1202 kvm_mips_callbacks
= NULL
;
1205 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
1206 struct kvm_sregs
*sregs
)
1208 return -ENOIOCTLCMD
;
1211 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
1212 struct kvm_sregs
*sregs
)
1214 return -ENOIOCTLCMD
;
1217 void kvm_arch_vcpu_postcreate(struct kvm_vcpu
*vcpu
)
1221 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
1223 return -ENOIOCTLCMD
;
1226 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
1228 return -ENOIOCTLCMD
;
1231 int kvm_arch_vcpu_fault(struct kvm_vcpu
*vcpu
, struct vm_fault
*vmf
)
1233 return VM_FAULT_SIGBUS
;
1236 int kvm_vm_ioctl_check_extension(struct kvm
*kvm
, long ext
)
1241 case KVM_CAP_ONE_REG
:
1242 case KVM_CAP_ENABLE_CAP
:
1245 case KVM_CAP_COALESCED_MMIO
:
1246 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
1248 case KVM_CAP_MIPS_FPU
:
1249 /* We don't handle systems with inconsistent cpu_has_fpu */
1250 r
= !!raw_cpu_has_fpu
;
1252 case KVM_CAP_MIPS_MSA
:
1254 * We don't support MSA vector partitioning yet:
1255 * 1) It would require explicit support which can't be tested
1256 * yet due to lack of support in current hardware.
1257 * 2) It extends the state that would need to be saved/restored
1258 * by e.g. QEMU for migration.
1260 * When vector partitioning hardware becomes available, support
1261 * could be added by requiring a flag when enabling
1262 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1263 * to save/restore the appropriate extra state.
1265 r
= cpu_has_msa
&& !(boot_cpu_data
.msa_id
& MSA_IR_WRPF
);
1274 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
1276 return kvm_mips_pending_timer(vcpu
);
1279 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu
*vcpu
)
1282 struct mips_coproc
*cop0
;
1287 kvm_debug("VCPU Register Dump:\n");
1288 kvm_debug("\tpc = 0x%08lx\n", vcpu
->arch
.pc
);
1289 kvm_debug("\texceptions: %08lx\n", vcpu
->arch
.pending_exceptions
);
1291 for (i
= 0; i
< 32; i
+= 4) {
1292 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i
,
1294 vcpu
->arch
.gprs
[i
+ 1],
1295 vcpu
->arch
.gprs
[i
+ 2], vcpu
->arch
.gprs
[i
+ 3]);
1297 kvm_debug("\thi: 0x%08lx\n", vcpu
->arch
.hi
);
1298 kvm_debug("\tlo: 0x%08lx\n", vcpu
->arch
.lo
);
1300 cop0
= vcpu
->arch
.cop0
;
1301 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1302 kvm_read_c0_guest_status(cop0
),
1303 kvm_read_c0_guest_cause(cop0
));
1305 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0
));
1310 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
1314 for (i
= 1; i
< ARRAY_SIZE(vcpu
->arch
.gprs
); i
++)
1315 vcpu
->arch
.gprs
[i
] = regs
->gpr
[i
];
1316 vcpu
->arch
.gprs
[0] = 0; /* zero is special, and cannot be set. */
1317 vcpu
->arch
.hi
= regs
->hi
;
1318 vcpu
->arch
.lo
= regs
->lo
;
1319 vcpu
->arch
.pc
= regs
->pc
;
1324 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
1328 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.gprs
); i
++)
1329 regs
->gpr
[i
] = vcpu
->arch
.gprs
[i
];
1331 regs
->hi
= vcpu
->arch
.hi
;
1332 regs
->lo
= vcpu
->arch
.lo
;
1333 regs
->pc
= vcpu
->arch
.pc
;
1338 static void kvm_mips_comparecount_func(unsigned long data
)
1340 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)data
;
1342 kvm_mips_callbacks
->queue_timer_int(vcpu
);
1344 vcpu
->arch
.wait
= 0;
1345 if (swait_active(&vcpu
->wq
))
1346 swake_up(&vcpu
->wq
);
1349 /* low level hrtimer wake routine */
1350 static enum hrtimer_restart
kvm_mips_comparecount_wakeup(struct hrtimer
*timer
)
1352 struct kvm_vcpu
*vcpu
;
1354 vcpu
= container_of(timer
, struct kvm_vcpu
, arch
.comparecount_timer
);
1355 kvm_mips_comparecount_func((unsigned long) vcpu
);
1356 return kvm_mips_count_timeout(vcpu
);
1359 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
1361 kvm_mips_callbacks
->vcpu_init(vcpu
);
1362 hrtimer_init(&vcpu
->arch
.comparecount_timer
, CLOCK_MONOTONIC
,
1364 vcpu
->arch
.comparecount_timer
.function
= kvm_mips_comparecount_wakeup
;
1368 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
1369 struct kvm_translation
*tr
)
1374 /* Initial guest state */
1375 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
1377 return kvm_mips_callbacks
->vcpu_setup(vcpu
);
1380 static void kvm_mips_set_c0_status(void)
1382 u32 status
= read_c0_status();
1387 write_c0_status(status
);
1392 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1394 int kvm_mips_handle_exit(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
1396 u32 cause
= vcpu
->arch
.host_cp0_cause
;
1397 u32 exccode
= (cause
>> CAUSEB_EXCCODE
) & 0x1f;
1398 u32 __user
*opc
= (u32 __user
*) vcpu
->arch
.pc
;
1399 unsigned long badvaddr
= vcpu
->arch
.host_cp0_badvaddr
;
1400 enum emulation_result er
= EMULATE_DONE
;
1401 int ret
= RESUME_GUEST
;
1403 /* re-enable HTW before enabling interrupts */
1406 /* Set a default exit reason */
1407 run
->exit_reason
= KVM_EXIT_UNKNOWN
;
1408 run
->ready_for_interrupt_injection
= 1;
1411 * Set the appropriate status bits based on host CPU features,
1412 * before we hit the scheduler
1414 kvm_mips_set_c0_status();
1418 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1419 cause
, opc
, run
, vcpu
);
1420 trace_kvm_exit(vcpu
, exccode
);
1423 * Do a privilege check, if in UM most of these exit conditions end up
1424 * causing an exception to be delivered to the Guest Kernel
1426 er
= kvm_mips_check_privilege(cause
, opc
, run
, vcpu
);
1427 if (er
== EMULATE_PRIV_FAIL
) {
1429 } else if (er
== EMULATE_FAIL
) {
1430 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1437 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu
->vcpu_id
, opc
);
1439 ++vcpu
->stat
.int_exits
;
1448 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc
);
1450 ++vcpu
->stat
.cop_unusable_exits
;
1451 ret
= kvm_mips_callbacks
->handle_cop_unusable(vcpu
);
1452 /* XXXKYMA: Might need to return to user space */
1453 if (run
->exit_reason
== KVM_EXIT_IRQ_WINDOW_OPEN
)
1458 ++vcpu
->stat
.tlbmod_exits
;
1459 ret
= kvm_mips_callbacks
->handle_tlb_mod(vcpu
);
1463 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1464 cause
, kvm_read_c0_guest_status(vcpu
->arch
.cop0
), opc
,
1467 ++vcpu
->stat
.tlbmiss_st_exits
;
1468 ret
= kvm_mips_callbacks
->handle_tlb_st_miss(vcpu
);
1472 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1473 cause
, opc
, badvaddr
);
1475 ++vcpu
->stat
.tlbmiss_ld_exits
;
1476 ret
= kvm_mips_callbacks
->handle_tlb_ld_miss(vcpu
);
1480 ++vcpu
->stat
.addrerr_st_exits
;
1481 ret
= kvm_mips_callbacks
->handle_addr_err_st(vcpu
);
1485 ++vcpu
->stat
.addrerr_ld_exits
;
1486 ret
= kvm_mips_callbacks
->handle_addr_err_ld(vcpu
);
1490 ++vcpu
->stat
.syscall_exits
;
1491 ret
= kvm_mips_callbacks
->handle_syscall(vcpu
);
1495 ++vcpu
->stat
.resvd_inst_exits
;
1496 ret
= kvm_mips_callbacks
->handle_res_inst(vcpu
);
1500 ++vcpu
->stat
.break_inst_exits
;
1501 ret
= kvm_mips_callbacks
->handle_break(vcpu
);
1505 ++vcpu
->stat
.trap_inst_exits
;
1506 ret
= kvm_mips_callbacks
->handle_trap(vcpu
);
1509 case EXCCODE_MSAFPE
:
1510 ++vcpu
->stat
.msa_fpe_exits
;
1511 ret
= kvm_mips_callbacks
->handle_msa_fpe(vcpu
);
1515 ++vcpu
->stat
.fpe_exits
;
1516 ret
= kvm_mips_callbacks
->handle_fpe(vcpu
);
1519 case EXCCODE_MSADIS
:
1520 ++vcpu
->stat
.msa_disabled_exits
;
1521 ret
= kvm_mips_callbacks
->handle_msa_disabled(vcpu
);
1525 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1526 exccode
, opc
, kvm_get_inst(opc
, vcpu
), badvaddr
,
1527 kvm_read_c0_guest_status(vcpu
->arch
.cop0
));
1528 kvm_arch_vcpu_dump_regs(vcpu
);
1529 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1536 local_irq_disable();
1538 if (er
== EMULATE_DONE
&& !(ret
& RESUME_HOST
))
1539 kvm_mips_deliver_interrupts(vcpu
, cause
);
1541 if (!(ret
& RESUME_HOST
)) {
1542 /* Only check for signals if not already exiting to userspace */
1543 if (signal_pending(current
)) {
1544 run
->exit_reason
= KVM_EXIT_INTR
;
1545 ret
= (-EINTR
<< 2) | RESUME_HOST
;
1546 ++vcpu
->stat
.signal_exits
;
1547 trace_kvm_exit(vcpu
, KVM_TRACE_EXIT_SIGNAL
);
1551 if (ret
== RESUME_GUEST
) {
1552 trace_kvm_reenter(vcpu
);
1555 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1556 * is live), restore FCR31 / MSACSR.
1558 * This should be before returning to the guest exception
1559 * vector, as it may well cause an [MSA] FP exception if there
1560 * are pending exception bits unmasked. (see
1561 * kvm_mips_csr_die_notifier() for how that is handled).
1563 if (kvm_mips_guest_has_fpu(&vcpu
->arch
) &&
1564 read_c0_status() & ST0_CU1
)
1565 __kvm_restore_fcsr(&vcpu
->arch
);
1567 if (kvm_mips_guest_has_msa(&vcpu
->arch
) &&
1568 read_c0_config5() & MIPS_CONF5_MSAEN
)
1569 __kvm_restore_msacsr(&vcpu
->arch
);
1572 /* Disable HTW before returning to guest or host */
1578 /* Enable FPU for guest and restore context */
1579 void kvm_own_fpu(struct kvm_vcpu
*vcpu
)
1581 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
1582 unsigned int sr
, cfg5
;
1586 sr
= kvm_read_c0_guest_status(cop0
);
1589 * If MSA state is already live, it is undefined how it interacts with
1590 * FR=0 FPU state, and we don't want to hit reserved instruction
1591 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1592 * play it safe and save it first.
1594 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1595 * get called when guest CU1 is set, however we can't trust the guest
1596 * not to clobber the status register directly via the commpage.
1598 if (cpu_has_msa
&& sr
& ST0_CU1
&& !(sr
& ST0_FR
) &&
1599 vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_MSA
)
1603 * Enable FPU for guest
1604 * We set FR and FRE according to guest context
1606 change_c0_status(ST0_CU1
| ST0_FR
, sr
);
1608 cfg5
= kvm_read_c0_guest_config5(cop0
);
1609 change_c0_config5(MIPS_CONF5_FRE
, cfg5
);
1611 enable_fpu_hazard();
1613 /* If guest FPU state not active, restore it now */
1614 if (!(vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_FPU
)) {
1615 __kvm_restore_fpu(&vcpu
->arch
);
1616 vcpu
->arch
.aux_inuse
|= KVM_MIPS_AUX_FPU
;
1617 trace_kvm_aux(vcpu
, KVM_TRACE_AUX_RESTORE
, KVM_TRACE_AUX_FPU
);
1619 trace_kvm_aux(vcpu
, KVM_TRACE_AUX_ENABLE
, KVM_TRACE_AUX_FPU
);
1625 #ifdef CONFIG_CPU_HAS_MSA
1626 /* Enable MSA for guest and restore context */
1627 void kvm_own_msa(struct kvm_vcpu
*vcpu
)
1629 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
1630 unsigned int sr
, cfg5
;
1635 * Enable FPU if enabled in guest, since we're restoring FPU context
1636 * anyway. We set FR and FRE according to guest context.
1638 if (kvm_mips_guest_has_fpu(&vcpu
->arch
)) {
1639 sr
= kvm_read_c0_guest_status(cop0
);
1642 * If FR=0 FPU state is already live, it is undefined how it
1643 * interacts with MSA state, so play it safe and save it first.
1645 if (!(sr
& ST0_FR
) &&
1646 (vcpu
->arch
.aux_inuse
& (KVM_MIPS_AUX_FPU
|
1647 KVM_MIPS_AUX_MSA
)) == KVM_MIPS_AUX_FPU
)
1650 change_c0_status(ST0_CU1
| ST0_FR
, sr
);
1651 if (sr
& ST0_CU1
&& cpu_has_fre
) {
1652 cfg5
= kvm_read_c0_guest_config5(cop0
);
1653 change_c0_config5(MIPS_CONF5_FRE
, cfg5
);
1657 /* Enable MSA for guest */
1658 set_c0_config5(MIPS_CONF5_MSAEN
);
1659 enable_fpu_hazard();
1661 switch (vcpu
->arch
.aux_inuse
& (KVM_MIPS_AUX_FPU
| KVM_MIPS_AUX_MSA
)) {
1662 case KVM_MIPS_AUX_FPU
:
1664 * Guest FPU state already loaded, only restore upper MSA state
1666 __kvm_restore_msa_upper(&vcpu
->arch
);
1667 vcpu
->arch
.aux_inuse
|= KVM_MIPS_AUX_MSA
;
1668 trace_kvm_aux(vcpu
, KVM_TRACE_AUX_RESTORE
, KVM_TRACE_AUX_MSA
);
1671 /* Neither FPU or MSA already active, restore full MSA state */
1672 __kvm_restore_msa(&vcpu
->arch
);
1673 vcpu
->arch
.aux_inuse
|= KVM_MIPS_AUX_MSA
;
1674 if (kvm_mips_guest_has_fpu(&vcpu
->arch
))
1675 vcpu
->arch
.aux_inuse
|= KVM_MIPS_AUX_FPU
;
1676 trace_kvm_aux(vcpu
, KVM_TRACE_AUX_RESTORE
,
1677 KVM_TRACE_AUX_FPU_MSA
);
1680 trace_kvm_aux(vcpu
, KVM_TRACE_AUX_ENABLE
, KVM_TRACE_AUX_MSA
);
1688 /* Drop FPU & MSA without saving it */
1689 void kvm_drop_fpu(struct kvm_vcpu
*vcpu
)
1692 if (cpu_has_msa
&& vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_MSA
) {
1694 trace_kvm_aux(vcpu
, KVM_TRACE_AUX_DISCARD
, KVM_TRACE_AUX_MSA
);
1695 vcpu
->arch
.aux_inuse
&= ~KVM_MIPS_AUX_MSA
;
1697 if (vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_FPU
) {
1698 clear_c0_status(ST0_CU1
| ST0_FR
);
1699 trace_kvm_aux(vcpu
, KVM_TRACE_AUX_DISCARD
, KVM_TRACE_AUX_FPU
);
1700 vcpu
->arch
.aux_inuse
&= ~KVM_MIPS_AUX_FPU
;
1705 /* Save and disable FPU & MSA */
1706 void kvm_lose_fpu(struct kvm_vcpu
*vcpu
)
1709 * FPU & MSA get disabled in root context (hardware) when it is disabled
1710 * in guest context (software), but the register state in the hardware
1711 * may still be in use. This is why we explicitly re-enable the hardware
1716 if (cpu_has_msa
&& vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_MSA
) {
1717 set_c0_config5(MIPS_CONF5_MSAEN
);
1718 enable_fpu_hazard();
1720 __kvm_save_msa(&vcpu
->arch
);
1721 trace_kvm_aux(vcpu
, KVM_TRACE_AUX_SAVE
, KVM_TRACE_AUX_FPU_MSA
);
1723 /* Disable MSA & FPU */
1725 if (vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_FPU
) {
1726 clear_c0_status(ST0_CU1
| ST0_FR
);
1727 disable_fpu_hazard();
1729 vcpu
->arch
.aux_inuse
&= ~(KVM_MIPS_AUX_FPU
| KVM_MIPS_AUX_MSA
);
1730 } else if (vcpu
->arch
.aux_inuse
& KVM_MIPS_AUX_FPU
) {
1731 set_c0_status(ST0_CU1
);
1732 enable_fpu_hazard();
1734 __kvm_save_fpu(&vcpu
->arch
);
1735 vcpu
->arch
.aux_inuse
&= ~KVM_MIPS_AUX_FPU
;
1736 trace_kvm_aux(vcpu
, KVM_TRACE_AUX_SAVE
, KVM_TRACE_AUX_FPU
);
1739 clear_c0_status(ST0_CU1
| ST0_FR
);
1740 disable_fpu_hazard();
1746 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1747 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1748 * exception if cause bits are set in the value being written.
1750 static int kvm_mips_csr_die_notify(struct notifier_block
*self
,
1751 unsigned long cmd
, void *ptr
)
1753 struct die_args
*args
= (struct die_args
*)ptr
;
1754 struct pt_regs
*regs
= args
->regs
;
1757 /* Only interested in FPE and MSAFPE */
1758 if (cmd
!= DIE_FP
&& cmd
!= DIE_MSAFP
)
1761 /* Return immediately if guest context isn't active */
1762 if (!(current
->flags
& PF_VCPU
))
1765 /* Should never get here from user mode */
1766 BUG_ON(user_mode(regs
));
1768 pc
= instruction_pointer(regs
);
1771 /* match 2nd instruction in __kvm_restore_fcsr */
1772 if (pc
!= (unsigned long)&__kvm_restore_fcsr
+ 4)
1776 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1778 pc
< (unsigned long)&__kvm_restore_msacsr
+ 4 ||
1779 pc
> (unsigned long)&__kvm_restore_msacsr
+ 8)
1784 /* Move PC forward a little and continue executing */
1785 instruction_pointer(regs
) += 4;
1790 static struct notifier_block kvm_mips_csr_die_notifier
= {
1791 .notifier_call
= kvm_mips_csr_die_notify
,
1794 static int __init
kvm_mips_init(void)
1798 ret
= kvm_mips_entry_setup();
1802 ret
= kvm_init(NULL
, sizeof(struct kvm_vcpu
), 0, THIS_MODULE
);
1807 register_die_notifier(&kvm_mips_csr_die_notifier
);
1812 static void __exit
kvm_mips_exit(void)
1816 unregister_die_notifier(&kvm_mips_csr_die_notifier
);
1819 module_init(kvm_mips_init
);
1820 module_exit(kvm_mips_exit
);
1822 EXPORT_TRACEPOINT_SYMBOL(kvm_exit
);