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), KVM_STAT_VCPU
35 struct kvm_stats_debugfs_item debugfs_entries
[] = {
36 { "wait", VCPU_STAT(wait_exits
) },
37 { "cache", VCPU_STAT(cache_exits
) },
38 { "signal", VCPU_STAT(signal_exits
) },
39 { "interrupt", VCPU_STAT(int_exits
) },
40 { "cop_unsuable", VCPU_STAT(cop_unusable_exits
) },
41 { "tlbmod", VCPU_STAT(tlbmod_exits
) },
42 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits
) },
43 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits
) },
44 { "addrerr_st", VCPU_STAT(addrerr_st_exits
) },
45 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits
) },
46 { "syscall", VCPU_STAT(syscall_exits
) },
47 { "resvd_inst", VCPU_STAT(resvd_inst_exits
) },
48 { "break_inst", VCPU_STAT(break_inst_exits
) },
49 { "flush_dcache", VCPU_STAT(flush_dcache_exits
) },
50 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
54 static int kvm_mips_reset_vcpu(struct kvm_vcpu
*vcpu
)
57 for_each_possible_cpu(i
) {
58 vcpu
->arch
.guest_kernel_asid
[i
] = 0;
59 vcpu
->arch
.guest_user_asid
[i
] = 0;
64 /* XXXKYMA: We are simulatoring a processor that has the WII bit set in Config7, so we
65 * are "runnable" if interrupts are pending
67 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
69 return !!(vcpu
->arch
.pending_exceptions
);
72 int kvm_arch_vcpu_should_kick(struct kvm_vcpu
*vcpu
)
77 int kvm_arch_hardware_enable(void *garbage
)
82 void kvm_arch_hardware_disable(void *garbage
)
86 int kvm_arch_hardware_setup(void)
91 void kvm_arch_hardware_unsetup(void)
95 void kvm_arch_check_processor_compat(void *rtn
)
102 static void kvm_mips_init_tlbs(struct kvm
*kvm
)
106 /* Add a wired entry to the TLB, it is used to map the commpage to the Guest kernel */
107 wired
= read_c0_wired();
108 write_c0_wired(wired
+ 1);
110 kvm
->arch
.commpage_tlb
= wired
;
112 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
113 kvm
->arch
.commpage_tlb
);
116 static void kvm_mips_init_vm_percpu(void *arg
)
118 struct kvm
*kvm
= (struct kvm
*)arg
;
120 kvm_mips_init_tlbs(kvm
);
121 kvm_mips_callbacks
->vm_init(kvm
);
125 int kvm_arch_init_vm(struct kvm
*kvm
, unsigned long type
)
127 if (atomic_inc_return(&kvm_mips_instance
) == 1) {
128 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
130 on_each_cpu(kvm_mips_init_vm_percpu
, kvm
, 1);
137 void kvm_mips_free_vcpus(struct kvm
*kvm
)
140 struct kvm_vcpu
*vcpu
;
142 /* Put the pages we reserved for the guest pmap */
143 for (i
= 0; i
< kvm
->arch
.guest_pmap_npages
; i
++) {
144 if (kvm
->arch
.guest_pmap
[i
] != KVM_INVALID_PAGE
)
145 kvm_mips_release_pfn_clean(kvm
->arch
.guest_pmap
[i
]);
147 kfree(kvm
->arch
.guest_pmap
);
149 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
150 kvm_arch_vcpu_free(vcpu
);
153 mutex_lock(&kvm
->lock
);
155 for (i
= 0; i
< atomic_read(&kvm
->online_vcpus
); i
++)
156 kvm
->vcpus
[i
] = NULL
;
158 atomic_set(&kvm
->online_vcpus
, 0);
160 mutex_unlock(&kvm
->lock
);
163 void kvm_arch_sync_events(struct kvm
*kvm
)
167 static void kvm_mips_uninit_tlbs(void *arg
)
169 /* Restore wired count */
172 /* Clear out all the TLBs */
173 kvm_local_flush_tlb_all();
176 void kvm_arch_destroy_vm(struct kvm
*kvm
)
178 kvm_mips_free_vcpus(kvm
);
180 /* If this is the last instance, restore wired count */
181 if (atomic_dec_return(&kvm_mips_instance
) == 0) {
182 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
184 on_each_cpu(kvm_mips_uninit_tlbs
, NULL
, 1);
189 kvm_arch_dev_ioctl(struct file
*filp
, unsigned int ioctl
, unsigned long arg
)
194 void kvm_arch_free_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*free
,
195 struct kvm_memory_slot
*dont
)
199 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
200 unsigned long npages
)
205 void kvm_arch_memslots_updated(struct kvm
*kvm
)
209 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
210 struct kvm_memory_slot
*memslot
,
211 struct kvm_userspace_memory_region
*mem
,
212 enum kvm_mr_change change
)
217 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
218 struct kvm_userspace_memory_region
*mem
,
219 const struct kvm_memory_slot
*old
,
220 enum kvm_mr_change change
)
222 unsigned long npages
= 0;
225 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
226 __func__
, kvm
, mem
->slot
, mem
->guest_phys_addr
,
227 mem
->memory_size
, mem
->userspace_addr
);
229 /* Setup Guest PMAP table */
230 if (!kvm
->arch
.guest_pmap
) {
232 npages
= mem
->memory_size
>> PAGE_SHIFT
;
235 kvm
->arch
.guest_pmap_npages
= npages
;
236 kvm
->arch
.guest_pmap
=
237 kzalloc(npages
* sizeof(unsigned long), GFP_KERNEL
);
239 if (!kvm
->arch
.guest_pmap
) {
240 kvm_err("Failed to allocate guest PMAP");
245 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
246 npages
, kvm
->arch
.guest_pmap
);
248 /* Now setup the page table */
249 for (i
= 0; i
< npages
; i
++) {
250 kvm
->arch
.guest_pmap
[i
] = KVM_INVALID_PAGE
;
258 void kvm_arch_flush_shadow_all(struct kvm
*kvm
)
262 void kvm_arch_flush_shadow_memslot(struct kvm
*kvm
,
263 struct kvm_memory_slot
*slot
)
267 void kvm_arch_flush_shadow(struct kvm
*kvm
)
271 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
, unsigned int id
)
273 extern char mips32_exception
[], mips32_exceptionEnd
[];
274 extern char mips32_GuestException
[], mips32_GuestExceptionEnd
[];
275 int err
, size
, offset
;
279 struct kvm_vcpu
*vcpu
= kzalloc(sizeof(struct kvm_vcpu
), GFP_KERNEL
);
286 err
= kvm_vcpu_init(vcpu
, kvm
, id
);
291 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm
, id
, vcpu
);
293 /* Allocate space for host mode exception handlers that handle
296 if (cpu_has_veic
|| cpu_has_vint
) {
297 size
= 0x200 + VECTORSPACING
* 64;
302 /* Save Linux EBASE */
303 vcpu
->arch
.host_ebase
= (void *)read_c0_ebase();
305 gebase
= kzalloc(ALIGN(size
, PAGE_SIZE
), GFP_KERNEL
);
311 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
312 ALIGN(size
, PAGE_SIZE
), gebase
);
315 vcpu
->arch
.guest_ebase
= gebase
;
317 /* Copy L1 Guest Exception handler to correct offset */
319 /* TLB Refill, EXL = 0 */
320 memcpy(gebase
, mips32_exception
,
321 mips32_exceptionEnd
- mips32_exception
);
323 /* General Exception Entry point */
324 memcpy(gebase
+ 0x180, mips32_exception
,
325 mips32_exceptionEnd
- mips32_exception
);
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 memcpy(gebase
+ 0x200 + (i
* VECTORSPACING
), mips32_exception
,
332 mips32_exceptionEnd
- mips32_exception
);
335 /* General handler, relocate to unmapped space for sanity's sake */
337 kvm_debug("Installing KVM Exception handlers @ %p, %#x bytes\n",
339 mips32_GuestExceptionEnd
- mips32_GuestException
);
341 memcpy(gebase
+ offset
, mips32_GuestException
,
342 mips32_GuestExceptionEnd
- mips32_GuestException
);
344 /* Invalidate the icache for these ranges */
345 local_flush_icache_range((unsigned long)gebase
,
346 (unsigned long)gebase
+ ALIGN(size
, PAGE_SIZE
));
348 /* Allocate comm page for guest kernel, a TLB will be reserved for mapping GVA @ 0xFFFF8000 to this page */
349 vcpu
->arch
.kseg0_commpage
= kzalloc(PAGE_SIZE
<< 1, GFP_KERNEL
);
351 if (!vcpu
->arch
.kseg0_commpage
) {
353 goto out_free_gebase
;
356 kvm_debug("Allocated COMM page @ %p\n", vcpu
->arch
.kseg0_commpage
);
357 kvm_mips_commpage_init(vcpu
);
360 vcpu
->arch
.last_sched_cpu
= -1;
362 /* Start off the timer */
363 kvm_mips_init_count(vcpu
);
377 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
379 hrtimer_cancel(&vcpu
->arch
.comparecount_timer
);
381 kvm_vcpu_uninit(vcpu
);
383 kvm_mips_dump_stats(vcpu
);
385 kfree(vcpu
->arch
.guest_ebase
);
386 kfree(vcpu
->arch
.kseg0_commpage
);
390 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
392 kvm_arch_vcpu_free(vcpu
);
396 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;
417 /* Check if we have any exceptions/interrupts pending */
418 kvm_mips_deliver_interrupts(vcpu
,
419 kvm_read_c0_guest_cause(vcpu
->arch
.cop0
));
423 r
= __kvm_mips_vcpu_run(run
, vcpu
);
428 if (vcpu
->sigset_active
)
429 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
435 kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
, struct kvm_mips_interrupt
*irq
)
437 int intr
= (int)irq
->irq
;
438 struct kvm_vcpu
*dvcpu
= NULL
;
440 if (intr
== 3 || intr
== -3 || intr
== 4 || intr
== -4)
441 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__
, irq
->cpu
,
447 dvcpu
= vcpu
->kvm
->vcpus
[irq
->cpu
];
449 if (intr
== 2 || intr
== 3 || intr
== 4) {
450 kvm_mips_callbacks
->queue_io_int(dvcpu
, irq
);
452 } else if (intr
== -2 || intr
== -3 || intr
== -4) {
453 kvm_mips_callbacks
->dequeue_io_int(dvcpu
, irq
);
455 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__
,
460 dvcpu
->arch
.wait
= 0;
462 if (waitqueue_active(&dvcpu
->wq
)) {
463 wake_up_interruptible(&dvcpu
->wq
);
470 kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
471 struct kvm_mp_state
*mp_state
)
477 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
;
635 return put_user(v
, uaddr64
);
636 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U32
) {
637 u32 __user
*uaddr32
= (u32 __user
*)(long)reg
->addr
;
639 return put_user(v32
, uaddr32
);
645 static int kvm_mips_set_reg(struct kvm_vcpu
*vcpu
,
646 const struct kvm_one_reg
*reg
)
648 struct mips_coproc
*cop0
= vcpu
->arch
.cop0
;
651 if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U64
) {
652 u64 __user
*uaddr64
= (u64 __user
*)(long)reg
->addr
;
654 if (get_user(v
, uaddr64
) != 0)
656 } else if ((reg
->id
& KVM_REG_SIZE_MASK
) == KVM_REG_SIZE_U32
) {
657 u32 __user
*uaddr32
= (u32 __user
*)(long)reg
->addr
;
660 if (get_user(v32
, uaddr32
) != 0)
668 case KVM_REG_MIPS_R0
:
669 /* Silently ignore requests to set $0 */
671 case KVM_REG_MIPS_R1
... KVM_REG_MIPS_R31
:
672 vcpu
->arch
.gprs
[reg
->id
- KVM_REG_MIPS_R0
] = v
;
674 case KVM_REG_MIPS_HI
:
677 case KVM_REG_MIPS_LO
:
680 case KVM_REG_MIPS_PC
:
684 case KVM_REG_MIPS_CP0_INDEX
:
685 kvm_write_c0_guest_index(cop0
, v
);
687 case KVM_REG_MIPS_CP0_CONTEXT
:
688 kvm_write_c0_guest_context(cop0
, v
);
690 case KVM_REG_MIPS_CP0_USERLOCAL
:
691 kvm_write_c0_guest_userlocal(cop0
, v
);
693 case KVM_REG_MIPS_CP0_PAGEMASK
:
694 kvm_write_c0_guest_pagemask(cop0
, v
);
696 case KVM_REG_MIPS_CP0_WIRED
:
697 kvm_write_c0_guest_wired(cop0
, v
);
699 case KVM_REG_MIPS_CP0_HWRENA
:
700 kvm_write_c0_guest_hwrena(cop0
, v
);
702 case KVM_REG_MIPS_CP0_BADVADDR
:
703 kvm_write_c0_guest_badvaddr(cop0
, v
);
705 case KVM_REG_MIPS_CP0_ENTRYHI
:
706 kvm_write_c0_guest_entryhi(cop0
, v
);
708 case KVM_REG_MIPS_CP0_STATUS
:
709 kvm_write_c0_guest_status(cop0
, v
);
711 case KVM_REG_MIPS_CP0_EPC
:
712 kvm_write_c0_guest_epc(cop0
, v
);
714 case KVM_REG_MIPS_CP0_ERROREPC
:
715 kvm_write_c0_guest_errorepc(cop0
, v
);
717 /* registers to be handled specially */
718 case KVM_REG_MIPS_CP0_COUNT
:
719 case KVM_REG_MIPS_CP0_COMPARE
:
720 case KVM_REG_MIPS_CP0_CAUSE
:
721 case KVM_REG_MIPS_COUNT_CTL
:
722 case KVM_REG_MIPS_COUNT_RESUME
:
723 case KVM_REG_MIPS_COUNT_HZ
:
724 return kvm_mips_callbacks
->set_one_reg(vcpu
, reg
, v
);
732 kvm_arch_vcpu_ioctl(struct file
*filp
, unsigned int ioctl
, unsigned long arg
)
734 struct kvm_vcpu
*vcpu
= filp
->private_data
;
735 void __user
*argp
= (void __user
*)arg
;
739 case KVM_SET_ONE_REG
:
740 case KVM_GET_ONE_REG
: {
741 struct kvm_one_reg reg
;
742 if (copy_from_user(®
, argp
, sizeof(reg
)))
744 if (ioctl
== KVM_SET_ONE_REG
)
745 return kvm_mips_set_reg(vcpu
, ®
);
747 return kvm_mips_get_reg(vcpu
, ®
);
749 case KVM_GET_REG_LIST
: {
750 struct kvm_reg_list __user
*user_list
= argp
;
751 u64 __user
*reg_dest
;
752 struct kvm_reg_list reg_list
;
755 if (copy_from_user(®_list
, user_list
, sizeof(reg_list
)))
758 reg_list
.n
= ARRAY_SIZE(kvm_mips_get_one_regs
);
759 if (copy_to_user(user_list
, ®_list
, sizeof(reg_list
)))
763 reg_dest
= user_list
->reg
;
764 if (copy_to_user(reg_dest
, kvm_mips_get_one_regs
,
765 sizeof(kvm_mips_get_one_regs
)))
770 /* Treat the NMI as a CPU reset */
771 r
= kvm_mips_reset_vcpu(vcpu
);
775 struct kvm_mips_interrupt irq
;
777 if (copy_from_user(&irq
, argp
, sizeof(irq
)))
780 kvm_debug("[%d] %s: irq: %d\n", vcpu
->vcpu_id
, __func__
,
783 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
795 * Get (and clear) the dirty memory log for a memory slot.
797 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
, struct kvm_dirty_log
*log
)
799 struct kvm_memory_slot
*memslot
;
800 unsigned long ga
, ga_end
;
805 mutex_lock(&kvm
->slots_lock
);
807 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
811 /* If nothing is dirty, don't bother messing with page tables. */
813 memslot
= &kvm
->memslots
->memslots
[log
->slot
];
815 ga
= memslot
->base_gfn
<< PAGE_SHIFT
;
816 ga_end
= ga
+ (memslot
->npages
<< PAGE_SHIFT
);
818 printk("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__
, ga
,
821 n
= kvm_dirty_bitmap_bytes(memslot
);
822 memset(memslot
->dirty_bitmap
, 0, n
);
827 mutex_unlock(&kvm
->slots_lock
);
832 long kvm_arch_vm_ioctl(struct file
*filp
, unsigned int ioctl
, unsigned long arg
)
844 int kvm_arch_init(void *opaque
)
848 if (kvm_mips_callbacks
) {
849 kvm_err("kvm: module already exists\n");
853 ret
= kvm_mips_emulation_init(&kvm_mips_callbacks
);
858 void kvm_arch_exit(void)
860 kvm_mips_callbacks
= NULL
;
864 kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
, struct kvm_sregs
*sregs
)
870 kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
, struct kvm_sregs
*sregs
)
875 int kvm_arch_vcpu_postcreate(struct kvm_vcpu
*vcpu
)
880 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
885 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
890 int kvm_arch_vcpu_fault(struct kvm_vcpu
*vcpu
, struct vm_fault
*vmf
)
892 return VM_FAULT_SIGBUS
;
895 int kvm_dev_ioctl_check_extension(long ext
)
900 case KVM_CAP_ONE_REG
:
903 case KVM_CAP_COALESCED_MMIO
:
904 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
913 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
915 return kvm_mips_pending_timer(vcpu
);
918 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu
*vcpu
)
921 struct mips_coproc
*cop0
;
926 printk("VCPU Register Dump:\n");
927 printk("\tpc = 0x%08lx\n", vcpu
->arch
.pc
);
928 printk("\texceptions: %08lx\n", vcpu
->arch
.pending_exceptions
);
930 for (i
= 0; i
< 32; i
+= 4) {
931 printk("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i
,
933 vcpu
->arch
.gprs
[i
+ 1],
934 vcpu
->arch
.gprs
[i
+ 2], vcpu
->arch
.gprs
[i
+ 3]);
936 printk("\thi: 0x%08lx\n", vcpu
->arch
.hi
);
937 printk("\tlo: 0x%08lx\n", vcpu
->arch
.lo
);
939 cop0
= vcpu
->arch
.cop0
;
940 printk("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
941 kvm_read_c0_guest_status(cop0
), kvm_read_c0_guest_cause(cop0
));
943 printk("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0
));
948 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
952 for (i
= 1; i
< ARRAY_SIZE(vcpu
->arch
.gprs
); i
++)
953 vcpu
->arch
.gprs
[i
] = regs
->gpr
[i
];
954 vcpu
->arch
.gprs
[0] = 0; /* zero is special, and cannot be set. */
955 vcpu
->arch
.hi
= regs
->hi
;
956 vcpu
->arch
.lo
= regs
->lo
;
957 vcpu
->arch
.pc
= regs
->pc
;
962 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
966 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.gprs
); i
++)
967 regs
->gpr
[i
] = vcpu
->arch
.gprs
[i
];
969 regs
->hi
= vcpu
->arch
.hi
;
970 regs
->lo
= vcpu
->arch
.lo
;
971 regs
->pc
= vcpu
->arch
.pc
;
976 static void kvm_mips_comparecount_func(unsigned long data
)
978 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)data
;
980 kvm_mips_callbacks
->queue_timer_int(vcpu
);
983 if (waitqueue_active(&vcpu
->wq
)) {
984 wake_up_interruptible(&vcpu
->wq
);
989 * 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
)
1015 kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
, 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
);
1027 void kvm_mips_set_c0_status(void)
1029 uint32_t status
= read_c0_status();
1032 status
|= (ST0_CU1
);
1037 write_c0_status(status
);
1042 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1044 int kvm_mips_handle_exit(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
1046 uint32_t cause
= vcpu
->arch
.host_cp0_cause
;
1047 uint32_t exccode
= (cause
>> CAUSEB_EXCCODE
) & 0x1f;
1048 uint32_t __user
*opc
= (uint32_t __user
*) vcpu
->arch
.pc
;
1049 unsigned long badvaddr
= vcpu
->arch
.host_cp0_badvaddr
;
1050 enum emulation_result er
= EMULATE_DONE
;
1051 int ret
= RESUME_GUEST
;
1053 /* Set a default exit reason */
1054 run
->exit_reason
= KVM_EXIT_UNKNOWN
;
1055 run
->ready_for_interrupt_injection
= 1;
1057 /* Set the appropriate status bits based on host CPU features, before we hit the scheduler */
1058 kvm_mips_set_c0_status();
1062 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1063 cause
, opc
, run
, vcpu
);
1065 /* Do a privilege check, if in UM most of these exit conditions end up
1066 * causing an exception to be delivered to the Guest Kernel
1068 er
= kvm_mips_check_privilege(cause
, opc
, run
, vcpu
);
1069 if (er
== EMULATE_PRIV_FAIL
) {
1071 } else if (er
== EMULATE_FAIL
) {
1072 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1079 kvm_debug("[%d]T_INT @ %p\n", vcpu
->vcpu_id
, opc
);
1081 ++vcpu
->stat
.int_exits
;
1082 trace_kvm_exit(vcpu
, INT_EXITS
);
1084 if (need_resched()) {
1091 case T_COP_UNUSABLE
:
1092 kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc
);
1094 ++vcpu
->stat
.cop_unusable_exits
;
1095 trace_kvm_exit(vcpu
, COP_UNUSABLE_EXITS
);
1096 ret
= kvm_mips_callbacks
->handle_cop_unusable(vcpu
);
1097 /* XXXKYMA: Might need to return to user space */
1098 if (run
->exit_reason
== KVM_EXIT_IRQ_WINDOW_OPEN
) {
1104 ++vcpu
->stat
.tlbmod_exits
;
1105 trace_kvm_exit(vcpu
, TLBMOD_EXITS
);
1106 ret
= kvm_mips_callbacks
->handle_tlb_mod(vcpu
);
1111 ("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
);
1161 ("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1162 exccode
, opc
, kvm_get_inst(opc
, vcpu
), badvaddr
,
1163 kvm_read_c0_guest_status(vcpu
->arch
.cop0
));
1164 kvm_arch_vcpu_dump_regs(vcpu
);
1165 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1172 local_irq_disable();
1174 if (er
== EMULATE_DONE
&& !(ret
& RESUME_HOST
))
1175 kvm_mips_deliver_interrupts(vcpu
, cause
);
1177 if (!(ret
& RESUME_HOST
)) {
1178 /* Only check for signals if not already exiting to userspace */
1179 if (signal_pending(current
)) {
1180 run
->exit_reason
= KVM_EXIT_INTR
;
1181 ret
= (-EINTR
<< 2) | RESUME_HOST
;
1182 ++vcpu
->stat
.signal_exits
;
1183 trace_kvm_exit(vcpu
, SIGNAL_EXITS
);
1190 int __init
kvm_mips_init(void)
1194 ret
= kvm_init(NULL
, sizeof(struct kvm_vcpu
), 0, THIS_MODULE
);
1199 /* On MIPS, kernel modules are executed from "mapped space", which requires TLBs.
1200 * The TLB handling code is statically linked with the rest of the kernel (kvm_tlb.c)
1201 * to avoid the possibility of double faulting. The issue is that the TLB code
1202 * references routines that are part of the the KVM module,
1203 * which are only available once the module is loaded.
1205 kvm_mips_gfn_to_pfn
= gfn_to_pfn
;
1206 kvm_mips_release_pfn_clean
= kvm_release_pfn_clean
;
1207 kvm_mips_is_error_pfn
= is_error_pfn
;
1209 pr_info("KVM/MIPS Initialized\n");
1213 void __exit
kvm_mips_exit(void)
1217 kvm_mips_gfn_to_pfn
= NULL
;
1218 kvm_mips_release_pfn_clean
= NULL
;
1219 kvm_mips_is_error_pfn
= NULL
;
1221 pr_info("KVM/MIPS unloaded\n");
1224 module_init(kvm_mips_init
);
1225 module_exit(kvm_mips_exit
);
1227 EXPORT_TRACEPOINT_SYMBOL(kvm_exit
);