x86/xen: resume timer irqs early
[linux/fpc-iii.git] / arch / mips / kvm / kvm_mips.c
blob3f3e5b2b2f3834b7351c3b6b972fd6fa1e3a48ec
1 /*
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
4 * for more details.
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>
16 #include <linux/fs.h>
17 #include <linux/bootmem.h>
18 #include <asm/page.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
28 #include "trace.h"
30 #ifndef VECTORSPACING
31 #define VECTORSPACING 0x100 /* for EI/VI mode */
32 #endif
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) },
51 {NULL}
54 static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu)
56 int i;
57 for_each_possible_cpu(i) {
58 vcpu->arch.guest_kernel_asid[i] = 0;
59 vcpu->arch.guest_user_asid[i] = 0;
61 return 0;
64 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
66 return gfn;
69 /* XXXKYMA: We are simulatoring a processor that has the WII bit set in Config7, so we
70 * are "runnable" if interrupts are pending
72 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
74 return !!(vcpu->arch.pending_exceptions);
77 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
79 return 1;
82 int kvm_arch_hardware_enable(void *garbage)
84 return 0;
87 void kvm_arch_hardware_disable(void *garbage)
91 int kvm_arch_hardware_setup(void)
93 return 0;
96 void kvm_arch_hardware_unsetup(void)
100 void kvm_arch_check_processor_compat(void *rtn)
102 int *r = (int *)rtn;
103 *r = 0;
104 return;
107 static void kvm_mips_init_tlbs(struct kvm *kvm)
109 unsigned long wired;
111 /* Add a wired entry to the TLB, it is used to map the commpage to the Guest kernel */
112 wired = read_c0_wired();
113 write_c0_wired(wired + 1);
114 mtc0_tlbw_hazard();
115 kvm->arch.commpage_tlb = wired;
117 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
118 kvm->arch.commpage_tlb);
121 static void kvm_mips_init_vm_percpu(void *arg)
123 struct kvm *kvm = (struct kvm *)arg;
125 kvm_mips_init_tlbs(kvm);
126 kvm_mips_callbacks->vm_init(kvm);
130 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
132 if (atomic_inc_return(&kvm_mips_instance) == 1) {
133 kvm_info("%s: 1st KVM instance, setup host TLB parameters\n",
134 __func__);
135 on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
139 return 0;
142 void kvm_mips_free_vcpus(struct kvm *kvm)
144 unsigned int i;
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 */
175 write_c0_wired(0);
176 mtc0_tlbw_hazard();
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_info("%s: last KVM instance, restoring TLB parameters\n",
188 __func__);
189 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
193 long
194 kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
196 return -ENOIOCTLCMD;
199 void kvm_arch_free_memslot(struct kvm_memory_slot *free,
200 struct kvm_memory_slot *dont)
204 int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
206 return 0;
209 void kvm_arch_memslots_updated(struct kvm *kvm)
213 int kvm_arch_prepare_memory_region(struct kvm *kvm,
214 struct kvm_memory_slot *memslot,
215 struct kvm_userspace_memory_region *mem,
216 enum kvm_mr_change change)
218 return 0;
221 void kvm_arch_commit_memory_region(struct kvm *kvm,
222 struct kvm_userspace_memory_region *mem,
223 const struct kvm_memory_slot *old,
224 enum kvm_mr_change change)
226 unsigned long npages = 0;
227 int i, err = 0;
229 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
230 __func__, kvm, mem->slot, mem->guest_phys_addr,
231 mem->memory_size, mem->userspace_addr);
233 /* Setup Guest PMAP table */
234 if (!kvm->arch.guest_pmap) {
235 if (mem->slot == 0)
236 npages = mem->memory_size >> PAGE_SHIFT;
238 if (npages) {
239 kvm->arch.guest_pmap_npages = npages;
240 kvm->arch.guest_pmap =
241 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
243 if (!kvm->arch.guest_pmap) {
244 kvm_err("Failed to allocate guest PMAP");
245 err = -ENOMEM;
246 goto out;
249 kvm_info
250 ("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;
259 out:
260 return;
263 void kvm_arch_flush_shadow_all(struct kvm *kvm)
267 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
268 struct kvm_memory_slot *slot)
272 void kvm_arch_flush_shadow(struct kvm *kvm)
276 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
278 extern char mips32_exception[], mips32_exceptionEnd[];
279 extern char mips32_GuestException[], mips32_GuestExceptionEnd[];
280 int err, size, offset;
281 void *gebase;
282 int i;
284 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
286 if (!vcpu) {
287 err = -ENOMEM;
288 goto out;
291 err = kvm_vcpu_init(vcpu, kvm, id);
293 if (err)
294 goto out_free_cpu;
296 kvm_info("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
298 /* Allocate space for host mode exception handlers that handle
299 * guest mode exits
301 if (cpu_has_veic || cpu_has_vint) {
302 size = 0x200 + VECTORSPACING * 64;
303 } else {
304 size = 0x4000;
307 /* Save Linux EBASE */
308 vcpu->arch.host_ebase = (void *)read_c0_ebase();
310 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
312 if (!gebase) {
313 err = -ENOMEM;
314 goto out_free_cpu;
316 kvm_info("Allocated %d bytes for KVM Exception Handlers @ %p\n",
317 ALIGN(size, PAGE_SIZE), gebase);
319 /* Save new ebase */
320 vcpu->arch.guest_ebase = gebase;
322 /* Copy L1 Guest Exception handler to correct offset */
324 /* TLB Refill, EXL = 0 */
325 memcpy(gebase, mips32_exception,
326 mips32_exceptionEnd - mips32_exception);
328 /* General Exception Entry point */
329 memcpy(gebase + 0x180, mips32_exception,
330 mips32_exceptionEnd - mips32_exception);
332 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
333 for (i = 0; i < 8; i++) {
334 kvm_debug("L1 Vectored handler @ %p\n",
335 gebase + 0x200 + (i * VECTORSPACING));
336 memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception,
337 mips32_exceptionEnd - mips32_exception);
340 /* General handler, relocate to unmapped space for sanity's sake */
341 offset = 0x2000;
342 kvm_info("Installing KVM Exception handlers @ %p, %#x bytes\n",
343 gebase + offset,
344 mips32_GuestExceptionEnd - mips32_GuestException);
346 memcpy(gebase + offset, mips32_GuestException,
347 mips32_GuestExceptionEnd - mips32_GuestException);
349 /* Invalidate the icache for these ranges */
350 mips32_SyncICache((unsigned long) gebase, ALIGN(size, PAGE_SIZE));
352 /* Allocate comm page for guest kernel, a TLB will be reserved for mapping GVA @ 0xFFFF8000 to this page */
353 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
355 if (!vcpu->arch.kseg0_commpage) {
356 err = -ENOMEM;
357 goto out_free_gebase;
360 kvm_info("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
361 kvm_mips_commpage_init(vcpu);
363 /* Init */
364 vcpu->arch.last_sched_cpu = -1;
366 /* Start off the timer */
367 kvm_mips_emulate_count(vcpu);
369 return vcpu;
371 out_free_gebase:
372 kfree(gebase);
374 out_free_cpu:
375 kfree(vcpu);
377 out:
378 return ERR_PTR(err);
381 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
383 hrtimer_cancel(&vcpu->arch.comparecount_timer);
385 kvm_vcpu_uninit(vcpu);
387 kvm_mips_dump_stats(vcpu);
389 kfree(vcpu->arch.guest_ebase);
390 kfree(vcpu->arch.kseg0_commpage);
391 kfree(vcpu);
394 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
396 kvm_arch_vcpu_free(vcpu);
400 kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
401 struct kvm_guest_debug *dbg)
403 return -ENOIOCTLCMD;
406 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
408 int r = 0;
409 sigset_t sigsaved;
411 if (vcpu->sigset_active)
412 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
414 if (vcpu->mmio_needed) {
415 if (!vcpu->mmio_is_write)
416 kvm_mips_complete_mmio_load(vcpu, run);
417 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));
424 local_irq_disable();
425 kvm_guest_enter();
427 r = __kvm_mips_vcpu_run(run, vcpu);
429 kvm_guest_exit();
430 local_irq_enable();
432 if (vcpu->sigset_active)
433 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
435 return r;
439 kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_mips_interrupt *irq)
441 int intr = (int)irq->irq;
442 struct kvm_vcpu *dvcpu = NULL;
444 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
445 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
446 (int)intr);
448 if (irq->cpu == -1)
449 dvcpu = vcpu;
450 else
451 dvcpu = vcpu->kvm->vcpus[irq->cpu];
453 if (intr == 2 || intr == 3 || intr == 4) {
454 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
456 } else if (intr == -2 || intr == -3 || intr == -4) {
457 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
458 } else {
459 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
460 irq->cpu, irq->irq);
461 return -EINVAL;
464 dvcpu->arch.wait = 0;
466 if (waitqueue_active(&dvcpu->wq)) {
467 wake_up_interruptible(&dvcpu->wq);
470 return 0;
474 kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
475 struct kvm_mp_state *mp_state)
477 return -ENOIOCTLCMD;
481 kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
482 struct kvm_mp_state *mp_state)
484 return -ENOIOCTLCMD;
487 #define MIPS_CP0_32(_R, _S) \
488 (KVM_REG_MIPS | KVM_REG_SIZE_U32 | 0x10000 | (8 * (_R) + (_S)))
490 #define MIPS_CP0_64(_R, _S) \
491 (KVM_REG_MIPS | KVM_REG_SIZE_U64 | 0x10000 | (8 * (_R) + (_S)))
493 #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
494 #define KVM_REG_MIPS_CP0_ENTRYLO0 MIPS_CP0_64(2, 0)
495 #define KVM_REG_MIPS_CP0_ENTRYLO1 MIPS_CP0_64(3, 0)
496 #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
497 #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
498 #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
499 #define KVM_REG_MIPS_CP0_PAGEGRAIN MIPS_CP0_32(5, 1)
500 #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
501 #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
502 #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
503 #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
504 #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
505 #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
506 #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
507 #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
508 #define KVM_REG_MIPS_CP0_EBASE MIPS_CP0_64(15, 1)
509 #define KVM_REG_MIPS_CP0_CONFIG MIPS_CP0_32(16, 0)
510 #define KVM_REG_MIPS_CP0_CONFIG1 MIPS_CP0_32(16, 1)
511 #define KVM_REG_MIPS_CP0_CONFIG2 MIPS_CP0_32(16, 2)
512 #define KVM_REG_MIPS_CP0_CONFIG3 MIPS_CP0_32(16, 3)
513 #define KVM_REG_MIPS_CP0_CONFIG7 MIPS_CP0_32(16, 7)
514 #define KVM_REG_MIPS_CP0_XCONTEXT MIPS_CP0_64(20, 0)
515 #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
517 static u64 kvm_mips_get_one_regs[] = {
518 KVM_REG_MIPS_R0,
519 KVM_REG_MIPS_R1,
520 KVM_REG_MIPS_R2,
521 KVM_REG_MIPS_R3,
522 KVM_REG_MIPS_R4,
523 KVM_REG_MIPS_R5,
524 KVM_REG_MIPS_R6,
525 KVM_REG_MIPS_R7,
526 KVM_REG_MIPS_R8,
527 KVM_REG_MIPS_R9,
528 KVM_REG_MIPS_R10,
529 KVM_REG_MIPS_R11,
530 KVM_REG_MIPS_R12,
531 KVM_REG_MIPS_R13,
532 KVM_REG_MIPS_R14,
533 KVM_REG_MIPS_R15,
534 KVM_REG_MIPS_R16,
535 KVM_REG_MIPS_R17,
536 KVM_REG_MIPS_R18,
537 KVM_REG_MIPS_R19,
538 KVM_REG_MIPS_R20,
539 KVM_REG_MIPS_R21,
540 KVM_REG_MIPS_R22,
541 KVM_REG_MIPS_R23,
542 KVM_REG_MIPS_R24,
543 KVM_REG_MIPS_R25,
544 KVM_REG_MIPS_R26,
545 KVM_REG_MIPS_R27,
546 KVM_REG_MIPS_R28,
547 KVM_REG_MIPS_R29,
548 KVM_REG_MIPS_R30,
549 KVM_REG_MIPS_R31,
551 KVM_REG_MIPS_HI,
552 KVM_REG_MIPS_LO,
553 KVM_REG_MIPS_PC,
555 KVM_REG_MIPS_CP0_INDEX,
556 KVM_REG_MIPS_CP0_CONTEXT,
557 KVM_REG_MIPS_CP0_PAGEMASK,
558 KVM_REG_MIPS_CP0_WIRED,
559 KVM_REG_MIPS_CP0_BADVADDR,
560 KVM_REG_MIPS_CP0_ENTRYHI,
561 KVM_REG_MIPS_CP0_STATUS,
562 KVM_REG_MIPS_CP0_CAUSE,
563 /* EPC set via kvm_regs, et al. */
564 KVM_REG_MIPS_CP0_CONFIG,
565 KVM_REG_MIPS_CP0_CONFIG1,
566 KVM_REG_MIPS_CP0_CONFIG2,
567 KVM_REG_MIPS_CP0_CONFIG3,
568 KVM_REG_MIPS_CP0_CONFIG7,
569 KVM_REG_MIPS_CP0_ERROREPC
572 static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
573 const struct kvm_one_reg *reg)
575 struct mips_coproc *cop0 = vcpu->arch.cop0;
576 s64 v;
578 switch (reg->id) {
579 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
580 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
581 break;
582 case KVM_REG_MIPS_HI:
583 v = (long)vcpu->arch.hi;
584 break;
585 case KVM_REG_MIPS_LO:
586 v = (long)vcpu->arch.lo;
587 break;
588 case KVM_REG_MIPS_PC:
589 v = (long)vcpu->arch.pc;
590 break;
592 case KVM_REG_MIPS_CP0_INDEX:
593 v = (long)kvm_read_c0_guest_index(cop0);
594 break;
595 case KVM_REG_MIPS_CP0_CONTEXT:
596 v = (long)kvm_read_c0_guest_context(cop0);
597 break;
598 case KVM_REG_MIPS_CP0_PAGEMASK:
599 v = (long)kvm_read_c0_guest_pagemask(cop0);
600 break;
601 case KVM_REG_MIPS_CP0_WIRED:
602 v = (long)kvm_read_c0_guest_wired(cop0);
603 break;
604 case KVM_REG_MIPS_CP0_BADVADDR:
605 v = (long)kvm_read_c0_guest_badvaddr(cop0);
606 break;
607 case KVM_REG_MIPS_CP0_ENTRYHI:
608 v = (long)kvm_read_c0_guest_entryhi(cop0);
609 break;
610 case KVM_REG_MIPS_CP0_STATUS:
611 v = (long)kvm_read_c0_guest_status(cop0);
612 break;
613 case KVM_REG_MIPS_CP0_CAUSE:
614 v = (long)kvm_read_c0_guest_cause(cop0);
615 break;
616 case KVM_REG_MIPS_CP0_ERROREPC:
617 v = (long)kvm_read_c0_guest_errorepc(cop0);
618 break;
619 case KVM_REG_MIPS_CP0_CONFIG:
620 v = (long)kvm_read_c0_guest_config(cop0);
621 break;
622 case KVM_REG_MIPS_CP0_CONFIG1:
623 v = (long)kvm_read_c0_guest_config1(cop0);
624 break;
625 case KVM_REG_MIPS_CP0_CONFIG2:
626 v = (long)kvm_read_c0_guest_config2(cop0);
627 break;
628 case KVM_REG_MIPS_CP0_CONFIG3:
629 v = (long)kvm_read_c0_guest_config3(cop0);
630 break;
631 case KVM_REG_MIPS_CP0_CONFIG7:
632 v = (long)kvm_read_c0_guest_config7(cop0);
633 break;
634 default:
635 return -EINVAL;
637 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
638 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
639 return put_user(v, uaddr64);
640 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
641 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
642 u32 v32 = (u32)v;
643 return put_user(v32, uaddr32);
644 } else {
645 return -EINVAL;
649 static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
650 const struct kvm_one_reg *reg)
652 struct mips_coproc *cop0 = vcpu->arch.cop0;
653 u64 v;
655 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
656 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
658 if (get_user(v, uaddr64) != 0)
659 return -EFAULT;
660 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
661 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
662 s32 v32;
664 if (get_user(v32, uaddr32) != 0)
665 return -EFAULT;
666 v = (s64)v32;
667 } else {
668 return -EINVAL;
671 switch (reg->id) {
672 case KVM_REG_MIPS_R0:
673 /* Silently ignore requests to set $0 */
674 break;
675 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
676 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
677 break;
678 case KVM_REG_MIPS_HI:
679 vcpu->arch.hi = v;
680 break;
681 case KVM_REG_MIPS_LO:
682 vcpu->arch.lo = v;
683 break;
684 case KVM_REG_MIPS_PC:
685 vcpu->arch.pc = v;
686 break;
688 case KVM_REG_MIPS_CP0_INDEX:
689 kvm_write_c0_guest_index(cop0, v);
690 break;
691 case KVM_REG_MIPS_CP0_CONTEXT:
692 kvm_write_c0_guest_context(cop0, v);
693 break;
694 case KVM_REG_MIPS_CP0_PAGEMASK:
695 kvm_write_c0_guest_pagemask(cop0, v);
696 break;
697 case KVM_REG_MIPS_CP0_WIRED:
698 kvm_write_c0_guest_wired(cop0, v);
699 break;
700 case KVM_REG_MIPS_CP0_BADVADDR:
701 kvm_write_c0_guest_badvaddr(cop0, v);
702 break;
703 case KVM_REG_MIPS_CP0_ENTRYHI:
704 kvm_write_c0_guest_entryhi(cop0, v);
705 break;
706 case KVM_REG_MIPS_CP0_STATUS:
707 kvm_write_c0_guest_status(cop0, v);
708 break;
709 case KVM_REG_MIPS_CP0_CAUSE:
710 kvm_write_c0_guest_cause(cop0, v);
711 break;
712 case KVM_REG_MIPS_CP0_ERROREPC:
713 kvm_write_c0_guest_errorepc(cop0, v);
714 break;
715 default:
716 return -EINVAL;
718 return 0;
721 long
722 kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
724 struct kvm_vcpu *vcpu = filp->private_data;
725 void __user *argp = (void __user *)arg;
726 long r;
728 switch (ioctl) {
729 case KVM_SET_ONE_REG:
730 case KVM_GET_ONE_REG: {
731 struct kvm_one_reg reg;
732 if (copy_from_user(&reg, argp, sizeof(reg)))
733 return -EFAULT;
734 if (ioctl == KVM_SET_ONE_REG)
735 return kvm_mips_set_reg(vcpu, &reg);
736 else
737 return kvm_mips_get_reg(vcpu, &reg);
739 case KVM_GET_REG_LIST: {
740 struct kvm_reg_list __user *user_list = argp;
741 u64 __user *reg_dest;
742 struct kvm_reg_list reg_list;
743 unsigned n;
745 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
746 return -EFAULT;
747 n = reg_list.n;
748 reg_list.n = ARRAY_SIZE(kvm_mips_get_one_regs);
749 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
750 return -EFAULT;
751 if (n < reg_list.n)
752 return -E2BIG;
753 reg_dest = user_list->reg;
754 if (copy_to_user(reg_dest, kvm_mips_get_one_regs,
755 sizeof(kvm_mips_get_one_regs)))
756 return -EFAULT;
757 return 0;
759 case KVM_NMI:
760 /* Treat the NMI as a CPU reset */
761 r = kvm_mips_reset_vcpu(vcpu);
762 break;
763 case KVM_INTERRUPT:
765 struct kvm_mips_interrupt irq;
766 r = -EFAULT;
767 if (copy_from_user(&irq, argp, sizeof(irq)))
768 goto out;
770 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
771 irq.irq);
773 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
774 break;
776 default:
777 r = -ENOIOCTLCMD;
780 out:
781 return r;
785 * Get (and clear) the dirty memory log for a memory slot.
787 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
789 struct kvm_memory_slot *memslot;
790 unsigned long ga, ga_end;
791 int is_dirty = 0;
792 int r;
793 unsigned long n;
795 mutex_lock(&kvm->slots_lock);
797 r = kvm_get_dirty_log(kvm, log, &is_dirty);
798 if (r)
799 goto out;
801 /* If nothing is dirty, don't bother messing with page tables. */
802 if (is_dirty) {
803 memslot = &kvm->memslots->memslots[log->slot];
805 ga = memslot->base_gfn << PAGE_SHIFT;
806 ga_end = ga + (memslot->npages << PAGE_SHIFT);
808 printk("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
809 ga_end);
811 n = kvm_dirty_bitmap_bytes(memslot);
812 memset(memslot->dirty_bitmap, 0, n);
815 r = 0;
816 out:
817 mutex_unlock(&kvm->slots_lock);
818 return r;
822 long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
824 long r;
826 switch (ioctl) {
827 default:
828 r = -ENOIOCTLCMD;
831 return r;
834 int kvm_arch_init(void *opaque)
836 int ret;
838 if (kvm_mips_callbacks) {
839 kvm_err("kvm: module already exists\n");
840 return -EEXIST;
843 ret = kvm_mips_emulation_init(&kvm_mips_callbacks);
845 return ret;
848 void kvm_arch_exit(void)
850 kvm_mips_callbacks = NULL;
854 kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
856 return -ENOIOCTLCMD;
860 kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
862 return -ENOIOCTLCMD;
865 int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
867 return 0;
870 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
872 return -ENOIOCTLCMD;
875 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
877 return -ENOIOCTLCMD;
880 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
882 return VM_FAULT_SIGBUS;
885 int kvm_dev_ioctl_check_extension(long ext)
887 int r;
889 switch (ext) {
890 case KVM_CAP_ONE_REG:
891 r = 1;
892 break;
893 case KVM_CAP_COALESCED_MMIO:
894 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
895 break;
896 default:
897 r = 0;
898 break;
900 return r;
903 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
905 return kvm_mips_pending_timer(vcpu);
908 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
910 int i;
911 struct mips_coproc *cop0;
913 if (!vcpu)
914 return -1;
916 printk("VCPU Register Dump:\n");
917 printk("\tpc = 0x%08lx\n", vcpu->arch.pc);;
918 printk("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
920 for (i = 0; i < 32; i += 4) {
921 printk("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
922 vcpu->arch.gprs[i],
923 vcpu->arch.gprs[i + 1],
924 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
926 printk("\thi: 0x%08lx\n", vcpu->arch.hi);
927 printk("\tlo: 0x%08lx\n", vcpu->arch.lo);
929 cop0 = vcpu->arch.cop0;
930 printk("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
931 kvm_read_c0_guest_status(cop0), kvm_read_c0_guest_cause(cop0));
933 printk("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
935 return 0;
938 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
940 int i;
942 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
943 vcpu->arch.gprs[i] = regs->gpr[i];
944 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
945 vcpu->arch.hi = regs->hi;
946 vcpu->arch.lo = regs->lo;
947 vcpu->arch.pc = regs->pc;
949 return 0;
952 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
954 int i;
956 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
957 regs->gpr[i] = vcpu->arch.gprs[i];
959 regs->hi = vcpu->arch.hi;
960 regs->lo = vcpu->arch.lo;
961 regs->pc = vcpu->arch.pc;
963 return 0;
966 void kvm_mips_comparecount_func(unsigned long data)
968 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
970 kvm_mips_callbacks->queue_timer_int(vcpu);
972 vcpu->arch.wait = 0;
973 if (waitqueue_active(&vcpu->wq)) {
974 wake_up_interruptible(&vcpu->wq);
979 * low level hrtimer wake routine.
981 enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
983 struct kvm_vcpu *vcpu;
985 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
986 kvm_mips_comparecount_func((unsigned long) vcpu);
987 hrtimer_forward_now(&vcpu->arch.comparecount_timer,
988 ktime_set(0, MS_TO_NS(10)));
989 return HRTIMER_RESTART;
992 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
994 kvm_mips_callbacks->vcpu_init(vcpu);
995 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
996 HRTIMER_MODE_REL);
997 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
998 kvm_mips_init_shadow_tlb(vcpu);
999 return 0;
1002 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1004 return;
1008 kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, struct kvm_translation *tr)
1010 return 0;
1013 /* Initial guest state */
1014 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1016 return kvm_mips_callbacks->vcpu_setup(vcpu);
1019 static
1020 void kvm_mips_set_c0_status(void)
1022 uint32_t status = read_c0_status();
1024 if (cpu_has_fpu)
1025 status |= (ST0_CU1);
1027 if (cpu_has_dsp)
1028 status |= (ST0_MX);
1030 write_c0_status(status);
1031 ehb();
1035 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1037 int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1039 uint32_t cause = vcpu->arch.host_cp0_cause;
1040 uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1041 uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
1042 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1043 enum emulation_result er = EMULATE_DONE;
1044 int ret = RESUME_GUEST;
1046 /* Set a default exit reason */
1047 run->exit_reason = KVM_EXIT_UNKNOWN;
1048 run->ready_for_interrupt_injection = 1;
1050 /* Set the appropriate status bits based on host CPU features, before we hit the scheduler */
1051 kvm_mips_set_c0_status();
1053 local_irq_enable();
1055 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1056 cause, opc, run, vcpu);
1058 /* Do a privilege check, if in UM most of these exit conditions end up
1059 * causing an exception to be delivered to the Guest Kernel
1061 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1062 if (er == EMULATE_PRIV_FAIL) {
1063 goto skip_emul;
1064 } else if (er == EMULATE_FAIL) {
1065 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1066 ret = RESUME_HOST;
1067 goto skip_emul;
1070 switch (exccode) {
1071 case T_INT:
1072 kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc);
1074 ++vcpu->stat.int_exits;
1075 trace_kvm_exit(vcpu, INT_EXITS);
1077 if (need_resched()) {
1078 cond_resched();
1081 ret = RESUME_GUEST;
1082 break;
1084 case T_COP_UNUSABLE:
1085 kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc);
1087 ++vcpu->stat.cop_unusable_exits;
1088 trace_kvm_exit(vcpu, COP_UNUSABLE_EXITS);
1089 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1090 /* XXXKYMA: Might need to return to user space */
1091 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN) {
1092 ret = RESUME_HOST;
1094 break;
1096 case T_TLB_MOD:
1097 ++vcpu->stat.tlbmod_exits;
1098 trace_kvm_exit(vcpu, TLBMOD_EXITS);
1099 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1100 break;
1102 case T_TLB_ST_MISS:
1103 kvm_debug
1104 ("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1105 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1106 badvaddr);
1108 ++vcpu->stat.tlbmiss_st_exits;
1109 trace_kvm_exit(vcpu, TLBMISS_ST_EXITS);
1110 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1111 break;
1113 case T_TLB_LD_MISS:
1114 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1115 cause, opc, badvaddr);
1117 ++vcpu->stat.tlbmiss_ld_exits;
1118 trace_kvm_exit(vcpu, TLBMISS_LD_EXITS);
1119 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1120 break;
1122 case T_ADDR_ERR_ST:
1123 ++vcpu->stat.addrerr_st_exits;
1124 trace_kvm_exit(vcpu, ADDRERR_ST_EXITS);
1125 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1126 break;
1128 case T_ADDR_ERR_LD:
1129 ++vcpu->stat.addrerr_ld_exits;
1130 trace_kvm_exit(vcpu, ADDRERR_LD_EXITS);
1131 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1132 break;
1134 case T_SYSCALL:
1135 ++vcpu->stat.syscall_exits;
1136 trace_kvm_exit(vcpu, SYSCALL_EXITS);
1137 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1138 break;
1140 case T_RES_INST:
1141 ++vcpu->stat.resvd_inst_exits;
1142 trace_kvm_exit(vcpu, RESVD_INST_EXITS);
1143 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1144 break;
1146 case T_BREAK:
1147 ++vcpu->stat.break_inst_exits;
1148 trace_kvm_exit(vcpu, BREAK_INST_EXITS);
1149 ret = kvm_mips_callbacks->handle_break(vcpu);
1150 break;
1152 default:
1153 kvm_err
1154 ("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1155 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1156 kvm_read_c0_guest_status(vcpu->arch.cop0));
1157 kvm_arch_vcpu_dump_regs(vcpu);
1158 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1159 ret = RESUME_HOST;
1160 break;
1164 skip_emul:
1165 local_irq_disable();
1167 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1168 kvm_mips_deliver_interrupts(vcpu, cause);
1170 if (!(ret & RESUME_HOST)) {
1171 /* Only check for signals if not already exiting to userspace */
1172 if (signal_pending(current)) {
1173 run->exit_reason = KVM_EXIT_INTR;
1174 ret = (-EINTR << 2) | RESUME_HOST;
1175 ++vcpu->stat.signal_exits;
1176 trace_kvm_exit(vcpu, SIGNAL_EXITS);
1180 return ret;
1183 int __init kvm_mips_init(void)
1185 int ret;
1187 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1189 if (ret)
1190 return ret;
1192 /* On MIPS, kernel modules are executed from "mapped space", which requires TLBs.
1193 * The TLB handling code is statically linked with the rest of the kernel (kvm_tlb.c)
1194 * to avoid the possibility of double faulting. The issue is that the TLB code
1195 * references routines that are part of the the KVM module,
1196 * which are only available once the module is loaded.
1198 kvm_mips_gfn_to_pfn = gfn_to_pfn;
1199 kvm_mips_release_pfn_clean = kvm_release_pfn_clean;
1200 kvm_mips_is_error_pfn = is_error_pfn;
1202 pr_info("KVM/MIPS Initialized\n");
1203 return 0;
1206 void __exit kvm_mips_exit(void)
1208 kvm_exit();
1210 kvm_mips_gfn_to_pfn = NULL;
1211 kvm_mips_release_pfn_clean = NULL;
1212 kvm_mips_is_error_pfn = NULL;
1214 pr_info("KVM/MIPS unloaded\n");
1217 module_init(kvm_mips_init);
1218 module_exit(kvm_mips_exit);
1220 EXPORT_TRACEPOINT_SYMBOL(kvm_exit);