2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * KVM/MIPS: MIPS specific KVM APIs
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
12 #include <linux/errno.h>
13 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/vmalloc.h>
17 #include <linux/bootmem.h>
19 #include <asm/cacheflush.h>
20 #include <asm/mmu_context.h>
22 #include <linux/kvm_host.h>
24 #include "kvm_mips_int.h"
25 #include "kvm_mips_comm.h"
27 #define CREATE_TRACE_POINTS
31 #define VECTORSPACING 0x100 /* for EI/VI mode */
34 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
35 struct kvm_stats_debugfs_item debugfs_entries
[] = {
36 { "wait", VCPU_STAT(wait_exits
), KVM_STAT_VCPU
},
37 { "cache", VCPU_STAT(cache_exits
), KVM_STAT_VCPU
},
38 { "signal", VCPU_STAT(signal_exits
), KVM_STAT_VCPU
},
39 { "interrupt", VCPU_STAT(int_exits
), KVM_STAT_VCPU
},
40 { "cop_unsuable", VCPU_STAT(cop_unusable_exits
), KVM_STAT_VCPU
},
41 { "tlbmod", VCPU_STAT(tlbmod_exits
), KVM_STAT_VCPU
},
42 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits
), KVM_STAT_VCPU
},
43 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits
), KVM_STAT_VCPU
},
44 { "addrerr_st", VCPU_STAT(addrerr_st_exits
), KVM_STAT_VCPU
},
45 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits
), KVM_STAT_VCPU
},
46 { "syscall", VCPU_STAT(syscall_exits
), KVM_STAT_VCPU
},
47 { "resvd_inst", VCPU_STAT(resvd_inst_exits
), KVM_STAT_VCPU
},
48 { "break_inst", VCPU_STAT(break_inst_exits
), KVM_STAT_VCPU
},
49 { "flush_dcache", VCPU_STAT(flush_dcache_exits
), KVM_STAT_VCPU
},
50 { "halt_wakeup", VCPU_STAT(halt_wakeup
), KVM_STAT_VCPU
},
54 static int kvm_mips_reset_vcpu(struct kvm_vcpu
*vcpu
)
58 for_each_possible_cpu(i
) {
59 vcpu
->arch
.guest_kernel_asid
[i
] = 0;
60 vcpu
->arch
.guest_user_asid
[i
] = 0;
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 *garbage
)
85 void kvm_arch_hardware_disable(void *garbage
)
89 int kvm_arch_hardware_setup(void)
94 void kvm_arch_hardware_unsetup(void)
98 void kvm_arch_check_processor_compat(void *rtn
)
105 static void kvm_mips_init_tlbs(struct kvm
*kvm
)
110 * Add a wired entry to the TLB, it is used to map the commpage to
113 wired
= read_c0_wired();
114 write_c0_wired(wired
+ 1);
116 kvm
->arch
.commpage_tlb
= wired
;
118 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
119 kvm
->arch
.commpage_tlb
);
122 static void kvm_mips_init_vm_percpu(void *arg
)
124 struct kvm
*kvm
= (struct kvm
*)arg
;
126 kvm_mips_init_tlbs(kvm
);
127 kvm_mips_callbacks
->vm_init(kvm
);
131 int kvm_arch_init_vm(struct kvm
*kvm
, unsigned long type
)
133 if (atomic_inc_return(&kvm_mips_instance
) == 1) {
134 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
136 on_each_cpu(kvm_mips_init_vm_percpu
, kvm
, 1);
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_mips_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 void kvm_arch_sync_events(struct kvm
*kvm
)
172 static void kvm_mips_uninit_tlbs(void *arg
)
174 /* Restore wired count */
177 /* Clear out all the TLBs */
178 kvm_local_flush_tlb_all();
181 void kvm_arch_destroy_vm(struct kvm
*kvm
)
183 kvm_mips_free_vcpus(kvm
);
185 /* If this is the last instance, restore wired count */
186 if (atomic_dec_return(&kvm_mips_instance
) == 0) {
187 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
189 on_each_cpu(kvm_mips_uninit_tlbs
, NULL
, 1);
193 long kvm_arch_dev_ioctl(struct file
*filp
, unsigned int ioctl
,
199 void kvm_arch_free_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*free
,
200 struct kvm_memory_slot
*dont
)
204 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
205 unsigned long npages
)
210 void kvm_arch_memslots_updated(struct kvm
*kvm
)
214 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
215 struct kvm_memory_slot
*memslot
,
216 struct kvm_userspace_memory_region
*mem
,
217 enum kvm_mr_change change
)
222 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
223 struct kvm_userspace_memory_region
*mem
,
224 const struct kvm_memory_slot
*old
,
225 enum kvm_mr_change change
)
227 unsigned long npages
= 0;
230 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
231 __func__
, kvm
, mem
->slot
, mem
->guest_phys_addr
,
232 mem
->memory_size
, mem
->userspace_addr
);
234 /* Setup Guest PMAP table */
235 if (!kvm
->arch
.guest_pmap
) {
237 npages
= mem
->memory_size
>> PAGE_SHIFT
;
240 kvm
->arch
.guest_pmap_npages
= npages
;
241 kvm
->arch
.guest_pmap
=
242 kzalloc(npages
* sizeof(unsigned long), GFP_KERNEL
);
244 if (!kvm
->arch
.guest_pmap
) {
245 kvm_err("Failed to allocate guest PMAP");
250 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
251 npages
, kvm
->arch
.guest_pmap
);
253 /* Now setup the page table */
254 for (i
= 0; i
< npages
; i
++)
255 kvm
->arch
.guest_pmap
[i
] = KVM_INVALID_PAGE
;
262 void kvm_arch_flush_shadow_all(struct kvm
*kvm
)
266 void kvm_arch_flush_shadow_memslot(struct kvm
*kvm
,
267 struct kvm_memory_slot
*slot
)
271 void kvm_arch_flush_shadow(struct kvm
*kvm
)
275 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
, unsigned int id
)
277 int err
, size
, offset
;
281 struct kvm_vcpu
*vcpu
= kzalloc(sizeof(struct kvm_vcpu
), GFP_KERNEL
);
288 err
= kvm_vcpu_init(vcpu
, kvm
, id
);
293 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm
, id
, vcpu
);
296 * Allocate space for host mode exception handlers that handle
299 if (cpu_has_veic
|| cpu_has_vint
)
300 size
= 0x200 + VECTORSPACING
* 64;
304 /* Save Linux EBASE */
305 vcpu
->arch
.host_ebase
= (void *)read_c0_ebase();
307 gebase
= kzalloc(ALIGN(size
, PAGE_SIZE
), GFP_KERNEL
);
313 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
314 ALIGN(size
, PAGE_SIZE
), gebase
);
317 vcpu
->arch
.guest_ebase
= gebase
;
319 /* Copy L1 Guest Exception handler to correct offset */
321 /* TLB Refill, EXL = 0 */
322 memcpy(gebase
, mips32_exception
,
323 mips32_exceptionEnd
- mips32_exception
);
325 /* General Exception Entry point */
326 memcpy(gebase
+ 0x180, mips32_exception
,
327 mips32_exceptionEnd
- mips32_exception
);
329 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
330 for (i
= 0; i
< 8; i
++) {
331 kvm_debug("L1 Vectored handler @ %p\n",
332 gebase
+ 0x200 + (i
* VECTORSPACING
));
333 memcpy(gebase
+ 0x200 + (i
* VECTORSPACING
), mips32_exception
,
334 mips32_exceptionEnd
- mips32_exception
);
337 /* General handler, relocate to unmapped space for sanity's sake */
339 kvm_debug("Installing KVM Exception handlers @ %p, %#x bytes\n",
341 mips32_GuestExceptionEnd
- mips32_GuestException
);
343 memcpy(gebase
+ offset
, mips32_GuestException
,
344 mips32_GuestExceptionEnd
- mips32_GuestException
);
346 /* Invalidate the icache for these ranges */
347 local_flush_icache_range((unsigned long)gebase
,
348 (unsigned long)gebase
+ ALIGN(size
, PAGE_SIZE
));
351 * Allocate comm page for guest kernel, a TLB will be reserved for
352 * mapping GVA @ 0xFFFF8000 to this page
354 vcpu
->arch
.kseg0_commpage
= kzalloc(PAGE_SIZE
<< 1, GFP_KERNEL
);
356 if (!vcpu
->arch
.kseg0_commpage
) {
358 goto out_free_gebase
;
361 kvm_debug("Allocated COMM page @ %p\n", vcpu
->arch
.kseg0_commpage
);
362 kvm_mips_commpage_init(vcpu
);
365 vcpu
->arch
.last_sched_cpu
= -1;
367 /* Start off the timer */
368 kvm_mips_init_count(vcpu
);
382 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
384 hrtimer_cancel(&vcpu
->arch
.comparecount_timer
);
386 kvm_vcpu_uninit(vcpu
);
388 kvm_mips_dump_stats(vcpu
);
390 kfree(vcpu
->arch
.guest_ebase
);
391 kfree(vcpu
->arch
.kseg0_commpage
);
394 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
396 kvm_arch_vcpu_free(vcpu
);
399 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
400 struct kvm_guest_debug
*dbg
)
405 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
410 if (vcpu
->sigset_active
)
411 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
413 if (vcpu
->mmio_needed
) {
414 if (!vcpu
->mmio_is_write
)
415 kvm_mips_complete_mmio_load(vcpu
, run
);
416 vcpu
->mmio_needed
= 0;
420 /* Check if we have any exceptions/interrupts pending */
421 kvm_mips_deliver_interrupts(vcpu
,
422 kvm_read_c0_guest_cause(vcpu
->arch
.cop0
));
426 r
= __kvm_mips_vcpu_run(run
, vcpu
);
431 if (vcpu
->sigset_active
)
432 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
437 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
438 struct kvm_mips_interrupt
*irq
)
440 int intr
= (int)irq
->irq
;
441 struct kvm_vcpu
*dvcpu
= NULL
;
443 if (intr
== 3 || intr
== -3 || intr
== 4 || intr
== -4)
444 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__
, irq
->cpu
,
450 dvcpu
= vcpu
->kvm
->vcpus
[irq
->cpu
];
452 if (intr
== 2 || intr
== 3 || intr
== 4) {
453 kvm_mips_callbacks
->queue_io_int(dvcpu
, irq
);
455 } else if (intr
== -2 || intr
== -3 || intr
== -4) {
456 kvm_mips_callbacks
->dequeue_io_int(dvcpu
, irq
);
458 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__
,
463 dvcpu
->arch
.wait
= 0;
465 if (waitqueue_active(&dvcpu
->wq
))
466 wake_up_interruptible(&dvcpu
->wq
);
471 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
472 struct kvm_mp_state
*mp_state
)
477 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
478 struct kvm_mp_state
*mp_state
)
483 static u64 kvm_mips_get_one_regs
[] = {
521 KVM_REG_MIPS_CP0_INDEX
,
522 KVM_REG_MIPS_CP0_CONTEXT
,
523 KVM_REG_MIPS_CP0_USERLOCAL
,
524 KVM_REG_MIPS_CP0_PAGEMASK
,
525 KVM_REG_MIPS_CP0_WIRED
,
526 KVM_REG_MIPS_CP0_HWRENA
,
527 KVM_REG_MIPS_CP0_BADVADDR
,
528 KVM_REG_MIPS_CP0_COUNT
,
529 KVM_REG_MIPS_CP0_ENTRYHI
,
530 KVM_REG_MIPS_CP0_COMPARE
,
531 KVM_REG_MIPS_CP0_STATUS
,
532 KVM_REG_MIPS_CP0_CAUSE
,
533 KVM_REG_MIPS_CP0_EPC
,
534 KVM_REG_MIPS_CP0_CONFIG
,
535 KVM_REG_MIPS_CP0_CONFIG1
,
536 KVM_REG_MIPS_CP0_CONFIG2
,
537 KVM_REG_MIPS_CP0_CONFIG3
,
538 KVM_REG_MIPS_CP0_CONFIG7
,
539 KVM_REG_MIPS_CP0_ERROREPC
,
541 KVM_REG_MIPS_COUNT_CTL
,
542 KVM_REG_MIPS_COUNT_RESUME
,
543 KVM_REG_MIPS_COUNT_HZ
,
546 static int kvm_mips_get_reg(struct kvm_vcpu
*vcpu
,
547 const struct kvm_one_reg
*reg
)
549 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
554 case KVM_REG_MIPS_R0
... KVM_REG_MIPS_R31
:
555 v
= (long)vcpu
->arch
.gprs
[reg
->id
- KVM_REG_MIPS_R0
];
557 case KVM_REG_MIPS_HI
:
558 v
= (long)vcpu
->arch
.hi
;
560 case KVM_REG_MIPS_LO
:
561 v
= (long)vcpu
->arch
.lo
;
563 case KVM_REG_MIPS_PC
:
564 v
= (long)vcpu
->arch
.pc
;
567 case KVM_REG_MIPS_CP0_INDEX
:
568 v
= (long)kvm_read_c0_guest_index(cop0
);
570 case KVM_REG_MIPS_CP0_CONTEXT
:
571 v
= (long)kvm_read_c0_guest_context(cop0
);
573 case KVM_REG_MIPS_CP0_USERLOCAL
:
574 v
= (long)kvm_read_c0_guest_userlocal(cop0
);
576 case KVM_REG_MIPS_CP0_PAGEMASK
:
577 v
= (long)kvm_read_c0_guest_pagemask(cop0
);
579 case KVM_REG_MIPS_CP0_WIRED
:
580 v
= (long)kvm_read_c0_guest_wired(cop0
);
582 case KVM_REG_MIPS_CP0_HWRENA
:
583 v
= (long)kvm_read_c0_guest_hwrena(cop0
);
585 case KVM_REG_MIPS_CP0_BADVADDR
:
586 v
= (long)kvm_read_c0_guest_badvaddr(cop0
);
588 case KVM_REG_MIPS_CP0_ENTRYHI
:
589 v
= (long)kvm_read_c0_guest_entryhi(cop0
);
591 case KVM_REG_MIPS_CP0_COMPARE
:
592 v
= (long)kvm_read_c0_guest_compare(cop0
);
594 case KVM_REG_MIPS_CP0_STATUS
:
595 v
= (long)kvm_read_c0_guest_status(cop0
);
597 case KVM_REG_MIPS_CP0_CAUSE
:
598 v
= (long)kvm_read_c0_guest_cause(cop0
);
600 case KVM_REG_MIPS_CP0_EPC
:
601 v
= (long)kvm_read_c0_guest_epc(cop0
);
603 case KVM_REG_MIPS_CP0_ERROREPC
:
604 v
= (long)kvm_read_c0_guest_errorepc(cop0
);
606 case KVM_REG_MIPS_CP0_CONFIG
:
607 v
= (long)kvm_read_c0_guest_config(cop0
);
609 case KVM_REG_MIPS_CP0_CONFIG1
:
610 v
= (long)kvm_read_c0_guest_config1(cop0
);
612 case KVM_REG_MIPS_CP0_CONFIG2
:
613 v
= (long)kvm_read_c0_guest_config2(cop0
);
615 case KVM_REG_MIPS_CP0_CONFIG3
:
616 v
= (long)kvm_read_c0_guest_config3(cop0
);
618 case KVM_REG_MIPS_CP0_CONFIG7
:
619 v
= (long)kvm_read_c0_guest_config7(cop0
);
621 /* registers to be handled specially */
622 case KVM_REG_MIPS_CP0_COUNT
:
623 case KVM_REG_MIPS_COUNT_CTL
:
624 case KVM_REG_MIPS_COUNT_RESUME
:
625 case KVM_REG_MIPS_COUNT_HZ
:
626 ret
= kvm_mips_callbacks
->get_one_reg(vcpu
, reg
, &v
);
633 if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
) {
634 u64 __user
*uaddr64
= (u64 __user
*)(long)reg
->addr
;
636 return put_user(v
, uaddr64
);
637 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U32
) {
638 u32 __user
*uaddr32
= (u32 __user
*)(long)reg
->addr
;
641 return put_user(v32
, uaddr32
);
647 static int kvm_mips_set_reg(struct kvm_vcpu
*vcpu
,
648 const struct kvm_one_reg
*reg
)
650 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
653 if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
) {
654 u64 __user
*uaddr64
= (u64 __user
*)(long)reg
->addr
;
656 if (get_user(v
, uaddr64
) != 0)
658 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U32
) {
659 u32 __user
*uaddr32
= (u32 __user
*)(long)reg
->addr
;
662 if (get_user(v32
, uaddr32
) != 0)
670 case KVM_REG_MIPS_R0
:
671 /* Silently ignore requests to set $0 */
673 case KVM_REG_MIPS_R1
... KVM_REG_MIPS_R31
:
674 vcpu
->arch
.gprs
[reg
->id
- KVM_REG_MIPS_R0
] = v
;
676 case KVM_REG_MIPS_HI
:
679 case KVM_REG_MIPS_LO
:
682 case KVM_REG_MIPS_PC
:
686 case KVM_REG_MIPS_CP0_INDEX
:
687 kvm_write_c0_guest_index(cop0
, v
);
689 case KVM_REG_MIPS_CP0_CONTEXT
:
690 kvm_write_c0_guest_context(cop0
, v
);
692 case KVM_REG_MIPS_CP0_USERLOCAL
:
693 kvm_write_c0_guest_userlocal(cop0
, v
);
695 case KVM_REG_MIPS_CP0_PAGEMASK
:
696 kvm_write_c0_guest_pagemask(cop0
, v
);
698 case KVM_REG_MIPS_CP0_WIRED
:
699 kvm_write_c0_guest_wired(cop0
, v
);
701 case KVM_REG_MIPS_CP0_HWRENA
:
702 kvm_write_c0_guest_hwrena(cop0
, v
);
704 case KVM_REG_MIPS_CP0_BADVADDR
:
705 kvm_write_c0_guest_badvaddr(cop0
, v
);
707 case KVM_REG_MIPS_CP0_ENTRYHI
:
708 kvm_write_c0_guest_entryhi(cop0
, v
);
710 case KVM_REG_MIPS_CP0_STATUS
:
711 kvm_write_c0_guest_status(cop0
, v
);
713 case KVM_REG_MIPS_CP0_EPC
:
714 kvm_write_c0_guest_epc(cop0
, v
);
716 case KVM_REG_MIPS_CP0_ERROREPC
:
717 kvm_write_c0_guest_errorepc(cop0
, v
);
719 /* registers to be handled specially */
720 case KVM_REG_MIPS_CP0_COUNT
:
721 case KVM_REG_MIPS_CP0_COMPARE
:
722 case KVM_REG_MIPS_CP0_CAUSE
:
723 case KVM_REG_MIPS_COUNT_CTL
:
724 case KVM_REG_MIPS_COUNT_RESUME
:
725 case KVM_REG_MIPS_COUNT_HZ
:
726 return kvm_mips_callbacks
->set_one_reg(vcpu
, reg
, v
);
733 long kvm_arch_vcpu_ioctl(struct file
*filp
, unsigned int ioctl
,
736 struct kvm_vcpu
*vcpu
= filp
->private_data
;
737 void __user
*argp
= (void __user
*)arg
;
741 case KVM_SET_ONE_REG
:
742 case KVM_GET_ONE_REG
: {
743 struct kvm_one_reg reg
;
745 if (copy_from_user(®
, argp
, sizeof(reg
)))
747 if (ioctl
== KVM_SET_ONE_REG
)
748 return kvm_mips_set_reg(vcpu
, ®
);
750 return kvm_mips_get_reg(vcpu
, ®
);
752 case KVM_GET_REG_LIST
: {
753 struct kvm_reg_list __user
*user_list
= argp
;
754 u64 __user
*reg_dest
;
755 struct kvm_reg_list reg_list
;
758 if (copy_from_user(®_list
, user_list
, sizeof(reg_list
)))
761 reg_list
.n
= ARRAY_SIZE(kvm_mips_get_one_regs
);
762 if (copy_to_user(user_list
, ®_list
, sizeof(reg_list
)))
766 reg_dest
= user_list
->reg
;
767 if (copy_to_user(reg_dest
, kvm_mips_get_one_regs
,
768 sizeof(kvm_mips_get_one_regs
)))
773 /* Treat the NMI as a CPU reset */
774 r
= kvm_mips_reset_vcpu(vcpu
);
778 struct kvm_mips_interrupt irq
;
781 if (copy_from_user(&irq
, argp
, sizeof(irq
)))
784 kvm_debug("[%d] %s: irq: %d\n", vcpu
->vcpu_id
, __func__
,
787 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
798 /* Get (and clear) the dirty memory log for a memory slot. */
799 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
, struct kvm_dirty_log
*log
)
801 struct kvm_memory_slot
*memslot
;
802 unsigned long ga
, ga_end
;
807 mutex_lock(&kvm
->slots_lock
);
809 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
813 /* If nothing is dirty, don't bother messing with page tables. */
815 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
817 ga
= memslot
->base_gfn
<< PAGE_SHIFT
;
818 ga_end
= ga
+ (memslot
->npages
<< PAGE_SHIFT
);
820 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__
, ga
,
823 n
= kvm_dirty_bitmap_bytes(memslot
);
824 memset(memslot
->dirty_bitmap
, 0, n
);
829 mutex_unlock(&kvm
->slots_lock
);
834 long kvm_arch_vm_ioctl(struct file
*filp
, unsigned int ioctl
, unsigned long arg
)
846 int kvm_arch_init(void *opaque
)
850 if (kvm_mips_callbacks
) {
851 kvm_err("kvm: module already exists\n");
855 ret
= kvm_mips_emulation_init(&kvm_mips_callbacks
);
860 void kvm_arch_exit(void)
862 kvm_mips_callbacks
= NULL
;
865 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
866 struct kvm_sregs
*sregs
)
871 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
872 struct kvm_sregs
*sregs
)
877 int kvm_arch_vcpu_postcreate(struct kvm_vcpu
*vcpu
)
882 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
887 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
892 int kvm_arch_vcpu_fault(struct kvm_vcpu
*vcpu
, struct vm_fault
*vmf
)
894 return VM_FAULT_SIGBUS
;
897 int kvm_dev_ioctl_check_extension(long ext
)
902 case KVM_CAP_ONE_REG
:
905 case KVM_CAP_COALESCED_MMIO
:
906 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
915 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
917 return kvm_mips_pending_timer(vcpu
);
920 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu
*vcpu
)
923 struct mips_coproc
*cop0
;
928 kvm_debug("VCPU Register Dump:\n");
929 kvm_debug("\tpc = 0x%08lx\n", vcpu
->arch
.pc
);
930 kvm_debug("\texceptions: %08lx\n", vcpu
->arch
.pending_exceptions
);
932 for (i
= 0; i
< 32; i
+= 4) {
933 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i
,
935 vcpu
->arch
.gprs
[i
+ 1],
936 vcpu
->arch
.gprs
[i
+ 2], vcpu
->arch
.gprs
[i
+ 3]);
938 kvm_debug("\thi: 0x%08lx\n", vcpu
->arch
.hi
);
939 kvm_debug("\tlo: 0x%08lx\n", vcpu
->arch
.lo
);
941 cop0
= vcpu
->arch
.cop0
;
942 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
943 kvm_read_c0_guest_status(cop0
),
944 kvm_read_c0_guest_cause(cop0
));
946 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0
));
951 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
955 for (i
= 1; i
< ARRAY_SIZE(vcpu
->arch
.gprs
); i
++)
956 vcpu
->arch
.gprs
[i
] = regs
->gpr
[i
];
957 vcpu
->arch
.gprs
[0] = 0; /* zero is special, and cannot be set. */
958 vcpu
->arch
.hi
= regs
->hi
;
959 vcpu
->arch
.lo
= regs
->lo
;
960 vcpu
->arch
.pc
= regs
->pc
;
965 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
969 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.gprs
); i
++)
970 regs
->gpr
[i
] = vcpu
->arch
.gprs
[i
];
972 regs
->hi
= vcpu
->arch
.hi
;
973 regs
->lo
= vcpu
->arch
.lo
;
974 regs
->pc
= vcpu
->arch
.pc
;
979 static void kvm_mips_comparecount_func(unsigned long data
)
981 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)data
;
983 kvm_mips_callbacks
->queue_timer_int(vcpu
);
986 if (waitqueue_active(&vcpu
->wq
))
987 wake_up_interruptible(&vcpu
->wq
);
990 /* low level hrtimer wake routine */
991 static enum hrtimer_restart
kvm_mips_comparecount_wakeup(struct hrtimer
*timer
)
993 struct kvm_vcpu
*vcpu
;
995 vcpu
= container_of(timer
, struct kvm_vcpu
, arch
.comparecount_timer
);
996 kvm_mips_comparecount_func((unsigned long) vcpu
);
997 return kvm_mips_count_timeout(vcpu
);
1000 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
1002 kvm_mips_callbacks
->vcpu_init(vcpu
);
1003 hrtimer_init(&vcpu
->arch
.comparecount_timer
, CLOCK_MONOTONIC
,
1005 vcpu
->arch
.comparecount_timer
.function
= kvm_mips_comparecount_wakeup
;
1009 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
1014 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
1015 struct kvm_translation
*tr
)
1020 /* Initial guest state */
1021 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
1023 return kvm_mips_callbacks
->vcpu_setup(vcpu
);
1026 static void kvm_mips_set_c0_status(void)
1028 uint32_t status
= read_c0_status();
1031 status
|= (ST0_CU1
);
1036 write_c0_status(status
);
1041 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1043 int kvm_mips_handle_exit(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
1045 uint32_t cause
= vcpu
->arch
.host_cp0_cause
;
1046 uint32_t exccode
= (cause
>> CAUSEB_EXCCODE
) & 0x1f;
1047 uint32_t __user
*opc
= (uint32_t __user
*) vcpu
->arch
.pc
;
1048 unsigned long badvaddr
= vcpu
->arch
.host_cp0_badvaddr
;
1049 enum emulation_result er
= EMULATE_DONE
;
1050 int ret
= RESUME_GUEST
;
1052 /* Set a default exit reason */
1053 run
->exit_reason
= KVM_EXIT_UNKNOWN
;
1054 run
->ready_for_interrupt_injection
= 1;
1057 * Set the appropriate status bits based on host CPU features,
1058 * before we hit the scheduler
1060 kvm_mips_set_c0_status();
1064 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1065 cause
, opc
, run
, vcpu
);
1068 * Do a privilege check, if in UM most of these exit conditions end up
1069 * causing an exception to be delivered to the Guest Kernel
1071 er
= kvm_mips_check_privilege(cause
, opc
, run
, vcpu
);
1072 if (er
== EMULATE_PRIV_FAIL
) {
1074 } else if (er
== EMULATE_FAIL
) {
1075 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1082 kvm_debug("[%d]T_INT @ %p\n", vcpu
->vcpu_id
, opc
);
1084 ++vcpu
->stat
.int_exits
;
1085 trace_kvm_exit(vcpu
, INT_EXITS
);
1093 case T_COP_UNUSABLE
:
1094 kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc
);
1096 ++vcpu
->stat
.cop_unusable_exits
;
1097 trace_kvm_exit(vcpu
, COP_UNUSABLE_EXITS
);
1098 ret
= kvm_mips_callbacks
->handle_cop_unusable(vcpu
);
1099 /* XXXKYMA: Might need to return to user space */
1100 if (run
->exit_reason
== KVM_EXIT_IRQ_WINDOW_OPEN
)
1105 ++vcpu
->stat
.tlbmod_exits
;
1106 trace_kvm_exit(vcpu
, TLBMOD_EXITS
);
1107 ret
= kvm_mips_callbacks
->handle_tlb_mod(vcpu
);
1111 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1112 cause
, kvm_read_c0_guest_status(vcpu
->arch
.cop0
), opc
,
1115 ++vcpu
->stat
.tlbmiss_st_exits
;
1116 trace_kvm_exit(vcpu
, TLBMISS_ST_EXITS
);
1117 ret
= kvm_mips_callbacks
->handle_tlb_st_miss(vcpu
);
1121 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1122 cause
, opc
, badvaddr
);
1124 ++vcpu
->stat
.tlbmiss_ld_exits
;
1125 trace_kvm_exit(vcpu
, TLBMISS_LD_EXITS
);
1126 ret
= kvm_mips_callbacks
->handle_tlb_ld_miss(vcpu
);
1130 ++vcpu
->stat
.addrerr_st_exits
;
1131 trace_kvm_exit(vcpu
, ADDRERR_ST_EXITS
);
1132 ret
= kvm_mips_callbacks
->handle_addr_err_st(vcpu
);
1136 ++vcpu
->stat
.addrerr_ld_exits
;
1137 trace_kvm_exit(vcpu
, ADDRERR_LD_EXITS
);
1138 ret
= kvm_mips_callbacks
->handle_addr_err_ld(vcpu
);
1142 ++vcpu
->stat
.syscall_exits
;
1143 trace_kvm_exit(vcpu
, SYSCALL_EXITS
);
1144 ret
= kvm_mips_callbacks
->handle_syscall(vcpu
);
1148 ++vcpu
->stat
.resvd_inst_exits
;
1149 trace_kvm_exit(vcpu
, RESVD_INST_EXITS
);
1150 ret
= kvm_mips_callbacks
->handle_res_inst(vcpu
);
1154 ++vcpu
->stat
.break_inst_exits
;
1155 trace_kvm_exit(vcpu
, BREAK_INST_EXITS
);
1156 ret
= kvm_mips_callbacks
->handle_break(vcpu
);
1160 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1161 exccode
, opc
, kvm_get_inst(opc
, vcpu
), badvaddr
,
1162 kvm_read_c0_guest_status(vcpu
->arch
.cop0
));
1163 kvm_arch_vcpu_dump_regs(vcpu
);
1164 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1171 local_irq_disable();
1173 if (er
== EMULATE_DONE
&& !(ret
& RESUME_HOST
))
1174 kvm_mips_deliver_interrupts(vcpu
, cause
);
1176 if (!(ret
& RESUME_HOST
)) {
1177 /* Only check for signals if not already exiting to userspace */
1178 if (signal_pending(current
)) {
1179 run
->exit_reason
= KVM_EXIT_INTR
;
1180 ret
= (-EINTR
<< 2) | RESUME_HOST
;
1181 ++vcpu
->stat
.signal_exits
;
1182 trace_kvm_exit(vcpu
, SIGNAL_EXITS
);
1189 int __init
kvm_mips_init(void)
1193 ret
= kvm_init(NULL
, sizeof(struct kvm_vcpu
), 0, THIS_MODULE
);
1199 * On MIPS, kernel modules are executed from "mapped space", which
1200 * requires TLBs. The TLB handling code is statically linked with
1201 * the rest of the kernel (kvm_tlb.c) to avoid the possibility of
1202 * double faulting. The issue is that the TLB code references
1203 * routines that are part of the the KVM module, which are only
1204 * available once the module is loaded.
1206 kvm_mips_gfn_to_pfn
= gfn_to_pfn
;
1207 kvm_mips_release_pfn_clean
= kvm_release_pfn_clean
;
1208 kvm_mips_is_error_pfn
= is_error_pfn
;
1210 pr_info("KVM/MIPS Initialized\n");
1214 void __exit
kvm_mips_exit(void)
1218 kvm_mips_gfn_to_pfn
= NULL
;
1219 kvm_mips_release_pfn_clean
= NULL
;
1220 kvm_mips_is_error_pfn
= NULL
;
1222 pr_info("KVM/MIPS unloaded\n");
1225 module_init(kvm_mips_init
);
1226 module_exit(kvm_mips_exit
);
1228 EXPORT_TRACEPOINT_SYMBOL(kvm_exit
);