powerpc: Delete __cpuinit usage from all users
[linux/fpc-iii.git] / arch / mips / kvm / kvm_mips.c
blobdd203e59e6fd650767a3ae5286e0599f4dbc15b7
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]);
153 if (kvm->arch.guest_pmap)
154 kfree(kvm->arch.guest_pmap);
156 kvm_for_each_vcpu(i, vcpu, kvm) {
157 kvm_arch_vcpu_free(vcpu);
160 mutex_lock(&kvm->lock);
162 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
163 kvm->vcpus[i] = NULL;
165 atomic_set(&kvm->online_vcpus, 0);
167 mutex_unlock(&kvm->lock);
170 void kvm_arch_sync_events(struct kvm *kvm)
174 static void kvm_mips_uninit_tlbs(void *arg)
176 /* Restore wired count */
177 write_c0_wired(0);
178 mtc0_tlbw_hazard();
179 /* Clear out all the TLBs */
180 kvm_local_flush_tlb_all();
183 void kvm_arch_destroy_vm(struct kvm *kvm)
185 kvm_mips_free_vcpus(kvm);
187 /* If this is the last instance, restore wired count */
188 if (atomic_dec_return(&kvm_mips_instance) == 0) {
189 kvm_info("%s: last KVM instance, restoring TLB parameters\n",
190 __func__);
191 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
195 long
196 kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
198 return -ENOIOCTLCMD;
201 void kvm_arch_free_memslot(struct kvm_memory_slot *free,
202 struct kvm_memory_slot *dont)
206 int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
208 return 0;
211 int kvm_arch_prepare_memory_region(struct kvm *kvm,
212 struct kvm_memory_slot *memslot,
213 struct kvm_userspace_memory_region *mem,
214 enum kvm_mr_change change)
216 return 0;
219 void kvm_arch_commit_memory_region(struct kvm *kvm,
220 struct kvm_userspace_memory_region *mem,
221 const struct kvm_memory_slot *old,
222 enum kvm_mr_change change)
224 unsigned long npages = 0;
225 int i, err = 0;
227 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
228 __func__, kvm, mem->slot, mem->guest_phys_addr,
229 mem->memory_size, mem->userspace_addr);
231 /* Setup Guest PMAP table */
232 if (!kvm->arch.guest_pmap) {
233 if (mem->slot == 0)
234 npages = mem->memory_size >> PAGE_SHIFT;
236 if (npages) {
237 kvm->arch.guest_pmap_npages = npages;
238 kvm->arch.guest_pmap =
239 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
241 if (!kvm->arch.guest_pmap) {
242 kvm_err("Failed to allocate guest PMAP");
243 err = -ENOMEM;
244 goto out;
247 kvm_info
248 ("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
249 npages, kvm->arch.guest_pmap);
251 /* Now setup the page table */
252 for (i = 0; i < npages; i++) {
253 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
257 out:
258 return;
261 void kvm_arch_flush_shadow_all(struct kvm *kvm)
265 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
266 struct kvm_memory_slot *slot)
270 void kvm_arch_flush_shadow(struct kvm *kvm)
274 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
276 extern char mips32_exception[], mips32_exceptionEnd[];
277 extern char mips32_GuestException[], mips32_GuestExceptionEnd[];
278 int err, size, offset;
279 void *gebase;
280 int i;
282 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
284 if (!vcpu) {
285 err = -ENOMEM;
286 goto out;
289 err = kvm_vcpu_init(vcpu, kvm, id);
291 if (err)
292 goto out_free_cpu;
294 kvm_info("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
296 /* Allocate space for host mode exception handlers that handle
297 * guest mode exits
299 if (cpu_has_veic || cpu_has_vint) {
300 size = 0x200 + VECTORSPACING * 64;
301 } else {
302 size = 0x200;
305 /* Save Linux EBASE */
306 vcpu->arch.host_ebase = (void *)read_c0_ebase();
308 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
310 if (!gebase) {
311 err = -ENOMEM;
312 goto out_free_cpu;
314 kvm_info("Allocated %d bytes for KVM Exception Handlers @ %p\n",
315 ALIGN(size, PAGE_SIZE), gebase);
317 /* Save new ebase */
318 vcpu->arch.guest_ebase = gebase;
320 /* Copy L1 Guest Exception handler to correct offset */
322 /* TLB Refill, EXL = 0 */
323 memcpy(gebase, mips32_exception,
324 mips32_exceptionEnd - mips32_exception);
326 /* General Exception Entry point */
327 memcpy(gebase + 0x180, mips32_exception,
328 mips32_exceptionEnd - mips32_exception);
330 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
331 for (i = 0; i < 8; i++) {
332 kvm_debug("L1 Vectored handler @ %p\n",
333 gebase + 0x200 + (i * VECTORSPACING));
334 memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception,
335 mips32_exceptionEnd - mips32_exception);
338 /* General handler, relocate to unmapped space for sanity's sake */
339 offset = 0x2000;
340 kvm_info("Installing KVM Exception handlers @ %p, %#x bytes\n",
341 gebase + offset,
342 mips32_GuestExceptionEnd - mips32_GuestException);
344 memcpy(gebase + offset, mips32_GuestException,
345 mips32_GuestExceptionEnd - mips32_GuestException);
347 /* Invalidate the icache for these ranges */
348 mips32_SyncICache((unsigned long) gebase, ALIGN(size, PAGE_SIZE));
350 /* Allocate comm page for guest kernel, a TLB will be reserved for mapping GVA @ 0xFFFF8000 to this page */
351 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
353 if (!vcpu->arch.kseg0_commpage) {
354 err = -ENOMEM;
355 goto out_free_gebase;
358 kvm_info("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
359 kvm_mips_commpage_init(vcpu);
361 /* Init */
362 vcpu->arch.last_sched_cpu = -1;
364 /* Start off the timer */
365 kvm_mips_emulate_count(vcpu);
367 return vcpu;
369 out_free_gebase:
370 kfree(gebase);
372 out_free_cpu:
373 kfree(vcpu);
375 out:
376 return ERR_PTR(err);
379 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
381 hrtimer_cancel(&vcpu->arch.comparecount_timer);
383 kvm_vcpu_uninit(vcpu);
385 kvm_mips_dump_stats(vcpu);
387 if (vcpu->arch.guest_ebase)
388 kfree(vcpu->arch.guest_ebase);
390 if (vcpu->arch.kseg0_commpage)
391 kfree(vcpu->arch.kseg0_commpage);
395 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
397 kvm_arch_vcpu_free(vcpu);
401 kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
402 struct kvm_guest_debug *dbg)
404 return -ENOIOCTLCMD;
407 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
409 int r = 0;
410 sigset_t sigsaved;
412 if (vcpu->sigset_active)
413 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
415 if (vcpu->mmio_needed) {
416 if (!vcpu->mmio_is_write)
417 kvm_mips_complete_mmio_load(vcpu, run);
418 vcpu->mmio_needed = 0;
421 /* Check if we have any exceptions/interrupts pending */
422 kvm_mips_deliver_interrupts(vcpu,
423 kvm_read_c0_guest_cause(vcpu->arch.cop0));
425 local_irq_disable();
426 kvm_guest_enter();
428 r = __kvm_mips_vcpu_run(run, vcpu);
430 kvm_guest_exit();
431 local_irq_enable();
433 if (vcpu->sigset_active)
434 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
436 return r;
440 kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_mips_interrupt *irq)
442 int intr = (int)irq->irq;
443 struct kvm_vcpu *dvcpu = NULL;
445 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
446 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
447 (int)intr);
449 if (irq->cpu == -1)
450 dvcpu = vcpu;
451 else
452 dvcpu = vcpu->kvm->vcpus[irq->cpu];
454 if (intr == 2 || intr == 3 || intr == 4) {
455 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
457 } else if (intr == -2 || intr == -3 || intr == -4) {
458 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
459 } else {
460 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
461 irq->cpu, irq->irq);
462 return -EINVAL;
465 dvcpu->arch.wait = 0;
467 if (waitqueue_active(&dvcpu->wq)) {
468 wake_up_interruptible(&dvcpu->wq);
471 return 0;
475 kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
476 struct kvm_mp_state *mp_state)
478 return -ENOIOCTLCMD;
482 kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
483 struct kvm_mp_state *mp_state)
485 return -ENOIOCTLCMD;
488 #define MIPS_CP0_32(_R, _S) \
489 (KVM_REG_MIPS | KVM_REG_SIZE_U32 | 0x10000 | (8 * (_R) + (_S)))
491 #define MIPS_CP0_64(_R, _S) \
492 (KVM_REG_MIPS | KVM_REG_SIZE_U64 | 0x10000 | (8 * (_R) + (_S)))
494 #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
495 #define KVM_REG_MIPS_CP0_ENTRYLO0 MIPS_CP0_64(2, 0)
496 #define KVM_REG_MIPS_CP0_ENTRYLO1 MIPS_CP0_64(3, 0)
497 #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
498 #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
499 #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
500 #define KVM_REG_MIPS_CP0_PAGEGRAIN MIPS_CP0_32(5, 1)
501 #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
502 #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
503 #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
504 #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
505 #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
506 #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
507 #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
508 #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
509 #define KVM_REG_MIPS_CP0_EBASE MIPS_CP0_64(15, 1)
510 #define KVM_REG_MIPS_CP0_CONFIG MIPS_CP0_32(16, 0)
511 #define KVM_REG_MIPS_CP0_CONFIG1 MIPS_CP0_32(16, 1)
512 #define KVM_REG_MIPS_CP0_CONFIG2 MIPS_CP0_32(16, 2)
513 #define KVM_REG_MIPS_CP0_CONFIG3 MIPS_CP0_32(16, 3)
514 #define KVM_REG_MIPS_CP0_CONFIG7 MIPS_CP0_32(16, 7)
515 #define KVM_REG_MIPS_CP0_XCONTEXT MIPS_CP0_64(20, 0)
516 #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
518 static u64 kvm_mips_get_one_regs[] = {
519 KVM_REG_MIPS_R0,
520 KVM_REG_MIPS_R1,
521 KVM_REG_MIPS_R2,
522 KVM_REG_MIPS_R3,
523 KVM_REG_MIPS_R4,
524 KVM_REG_MIPS_R5,
525 KVM_REG_MIPS_R6,
526 KVM_REG_MIPS_R7,
527 KVM_REG_MIPS_R8,
528 KVM_REG_MIPS_R9,
529 KVM_REG_MIPS_R10,
530 KVM_REG_MIPS_R11,
531 KVM_REG_MIPS_R12,
532 KVM_REG_MIPS_R13,
533 KVM_REG_MIPS_R14,
534 KVM_REG_MIPS_R15,
535 KVM_REG_MIPS_R16,
536 KVM_REG_MIPS_R17,
537 KVM_REG_MIPS_R18,
538 KVM_REG_MIPS_R19,
539 KVM_REG_MIPS_R20,
540 KVM_REG_MIPS_R21,
541 KVM_REG_MIPS_R22,
542 KVM_REG_MIPS_R23,
543 KVM_REG_MIPS_R24,
544 KVM_REG_MIPS_R25,
545 KVM_REG_MIPS_R26,
546 KVM_REG_MIPS_R27,
547 KVM_REG_MIPS_R28,
548 KVM_REG_MIPS_R29,
549 KVM_REG_MIPS_R30,
550 KVM_REG_MIPS_R31,
552 KVM_REG_MIPS_HI,
553 KVM_REG_MIPS_LO,
554 KVM_REG_MIPS_PC,
556 KVM_REG_MIPS_CP0_INDEX,
557 KVM_REG_MIPS_CP0_CONTEXT,
558 KVM_REG_MIPS_CP0_PAGEMASK,
559 KVM_REG_MIPS_CP0_WIRED,
560 KVM_REG_MIPS_CP0_BADVADDR,
561 KVM_REG_MIPS_CP0_ENTRYHI,
562 KVM_REG_MIPS_CP0_STATUS,
563 KVM_REG_MIPS_CP0_CAUSE,
564 /* EPC set via kvm_regs, et al. */
565 KVM_REG_MIPS_CP0_CONFIG,
566 KVM_REG_MIPS_CP0_CONFIG1,
567 KVM_REG_MIPS_CP0_CONFIG2,
568 KVM_REG_MIPS_CP0_CONFIG3,
569 KVM_REG_MIPS_CP0_CONFIG7,
570 KVM_REG_MIPS_CP0_ERROREPC
573 static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
574 const struct kvm_one_reg *reg)
576 struct mips_coproc *cop0 = vcpu->arch.cop0;
577 s64 v;
579 switch (reg->id) {
580 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
581 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
582 break;
583 case KVM_REG_MIPS_HI:
584 v = (long)vcpu->arch.hi;
585 break;
586 case KVM_REG_MIPS_LO:
587 v = (long)vcpu->arch.lo;
588 break;
589 case KVM_REG_MIPS_PC:
590 v = (long)vcpu->arch.pc;
591 break;
593 case KVM_REG_MIPS_CP0_INDEX:
594 v = (long)kvm_read_c0_guest_index(cop0);
595 break;
596 case KVM_REG_MIPS_CP0_CONTEXT:
597 v = (long)kvm_read_c0_guest_context(cop0);
598 break;
599 case KVM_REG_MIPS_CP0_PAGEMASK:
600 v = (long)kvm_read_c0_guest_pagemask(cop0);
601 break;
602 case KVM_REG_MIPS_CP0_WIRED:
603 v = (long)kvm_read_c0_guest_wired(cop0);
604 break;
605 case KVM_REG_MIPS_CP0_BADVADDR:
606 v = (long)kvm_read_c0_guest_badvaddr(cop0);
607 break;
608 case KVM_REG_MIPS_CP0_ENTRYHI:
609 v = (long)kvm_read_c0_guest_entryhi(cop0);
610 break;
611 case KVM_REG_MIPS_CP0_STATUS:
612 v = (long)kvm_read_c0_guest_status(cop0);
613 break;
614 case KVM_REG_MIPS_CP0_CAUSE:
615 v = (long)kvm_read_c0_guest_cause(cop0);
616 break;
617 case KVM_REG_MIPS_CP0_ERROREPC:
618 v = (long)kvm_read_c0_guest_errorepc(cop0);
619 break;
620 case KVM_REG_MIPS_CP0_CONFIG:
621 v = (long)kvm_read_c0_guest_config(cop0);
622 break;
623 case KVM_REG_MIPS_CP0_CONFIG1:
624 v = (long)kvm_read_c0_guest_config1(cop0);
625 break;
626 case KVM_REG_MIPS_CP0_CONFIG2:
627 v = (long)kvm_read_c0_guest_config2(cop0);
628 break;
629 case KVM_REG_MIPS_CP0_CONFIG3:
630 v = (long)kvm_read_c0_guest_config3(cop0);
631 break;
632 case KVM_REG_MIPS_CP0_CONFIG7:
633 v = (long)kvm_read_c0_guest_config7(cop0);
634 break;
635 default:
636 return -EINVAL;
638 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
639 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
640 return put_user(v, uaddr64);
641 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
642 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
643 u32 v32 = (u32)v;
644 return put_user(v32, uaddr32);
645 } else {
646 return -EINVAL;
650 static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
651 const struct kvm_one_reg *reg)
653 struct mips_coproc *cop0 = vcpu->arch.cop0;
654 u64 v;
656 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
657 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
659 if (get_user(v, uaddr64) != 0)
660 return -EFAULT;
661 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
662 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
663 s32 v32;
665 if (get_user(v32, uaddr32) != 0)
666 return -EFAULT;
667 v = (s64)v32;
668 } else {
669 return -EINVAL;
672 switch (reg->id) {
673 case KVM_REG_MIPS_R0:
674 /* Silently ignore requests to set $0 */
675 break;
676 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
677 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
678 break;
679 case KVM_REG_MIPS_HI:
680 vcpu->arch.hi = v;
681 break;
682 case KVM_REG_MIPS_LO:
683 vcpu->arch.lo = v;
684 break;
685 case KVM_REG_MIPS_PC:
686 vcpu->arch.pc = v;
687 break;
689 case KVM_REG_MIPS_CP0_INDEX:
690 kvm_write_c0_guest_index(cop0, v);
691 break;
692 case KVM_REG_MIPS_CP0_CONTEXT:
693 kvm_write_c0_guest_context(cop0, v);
694 break;
695 case KVM_REG_MIPS_CP0_PAGEMASK:
696 kvm_write_c0_guest_pagemask(cop0, v);
697 break;
698 case KVM_REG_MIPS_CP0_WIRED:
699 kvm_write_c0_guest_wired(cop0, v);
700 break;
701 case KVM_REG_MIPS_CP0_BADVADDR:
702 kvm_write_c0_guest_badvaddr(cop0, v);
703 break;
704 case KVM_REG_MIPS_CP0_ENTRYHI:
705 kvm_write_c0_guest_entryhi(cop0, v);
706 break;
707 case KVM_REG_MIPS_CP0_STATUS:
708 kvm_write_c0_guest_status(cop0, v);
709 break;
710 case KVM_REG_MIPS_CP0_CAUSE:
711 kvm_write_c0_guest_cause(cop0, v);
712 break;
713 case KVM_REG_MIPS_CP0_ERROREPC:
714 kvm_write_c0_guest_errorepc(cop0, v);
715 break;
716 default:
717 return -EINVAL;
719 return 0;
722 long
723 kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
725 struct kvm_vcpu *vcpu = filp->private_data;
726 void __user *argp = (void __user *)arg;
727 long r;
729 switch (ioctl) {
730 case KVM_SET_ONE_REG:
731 case KVM_GET_ONE_REG: {
732 struct kvm_one_reg reg;
733 if (copy_from_user(&reg, argp, sizeof(reg)))
734 return -EFAULT;
735 if (ioctl == KVM_SET_ONE_REG)
736 return kvm_mips_set_reg(vcpu, &reg);
737 else
738 return kvm_mips_get_reg(vcpu, &reg);
740 case KVM_GET_REG_LIST: {
741 struct kvm_reg_list __user *user_list = argp;
742 u64 __user *reg_dest;
743 struct kvm_reg_list reg_list;
744 unsigned n;
746 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
747 return -EFAULT;
748 n = reg_list.n;
749 reg_list.n = ARRAY_SIZE(kvm_mips_get_one_regs);
750 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
751 return -EFAULT;
752 if (n < reg_list.n)
753 return -E2BIG;
754 reg_dest = user_list->reg;
755 if (copy_to_user(reg_dest, kvm_mips_get_one_regs,
756 sizeof(kvm_mips_get_one_regs)))
757 return -EFAULT;
758 return 0;
760 case KVM_NMI:
761 /* Treat the NMI as a CPU reset */
762 r = kvm_mips_reset_vcpu(vcpu);
763 break;
764 case KVM_INTERRUPT:
766 struct kvm_mips_interrupt irq;
767 r = -EFAULT;
768 if (copy_from_user(&irq, argp, sizeof(irq)))
769 goto out;
771 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
772 irq.irq);
774 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
775 break;
777 default:
778 r = -ENOIOCTLCMD;
781 out:
782 return r;
786 * Get (and clear) the dirty memory log for a memory slot.
788 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
790 struct kvm_memory_slot *memslot;
791 unsigned long ga, ga_end;
792 int is_dirty = 0;
793 int r;
794 unsigned long n;
796 mutex_lock(&kvm->slots_lock);
798 r = kvm_get_dirty_log(kvm, log, &is_dirty);
799 if (r)
800 goto out;
802 /* If nothing is dirty, don't bother messing with page tables. */
803 if (is_dirty) {
804 memslot = &kvm->memslots->memslots[log->slot];
806 ga = memslot->base_gfn << PAGE_SHIFT;
807 ga_end = ga + (memslot->npages << PAGE_SHIFT);
809 printk("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
810 ga_end);
812 n = kvm_dirty_bitmap_bytes(memslot);
813 memset(memslot->dirty_bitmap, 0, n);
816 r = 0;
817 out:
818 mutex_unlock(&kvm->slots_lock);
819 return r;
823 long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
825 long r;
827 switch (ioctl) {
828 default:
829 r = -ENOIOCTLCMD;
832 return r;
835 int kvm_arch_init(void *opaque)
837 int ret;
839 if (kvm_mips_callbacks) {
840 kvm_err("kvm: module already exists\n");
841 return -EEXIST;
844 ret = kvm_mips_emulation_init(&kvm_mips_callbacks);
846 return ret;
849 void kvm_arch_exit(void)
851 kvm_mips_callbacks = NULL;
855 kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
857 return -ENOIOCTLCMD;
861 kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
863 return -ENOIOCTLCMD;
866 int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
868 return 0;
871 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
873 return -ENOIOCTLCMD;
876 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
878 return -ENOIOCTLCMD;
881 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
883 return VM_FAULT_SIGBUS;
886 int kvm_dev_ioctl_check_extension(long ext)
888 int r;
890 switch (ext) {
891 case KVM_CAP_ONE_REG:
892 r = 1;
893 break;
894 case KVM_CAP_COALESCED_MMIO:
895 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
896 break;
897 default:
898 r = 0;
899 break;
901 return r;
904 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
906 return kvm_mips_pending_timer(vcpu);
909 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
911 int i;
912 struct mips_coproc *cop0;
914 if (!vcpu)
915 return -1;
917 printk("VCPU Register Dump:\n");
918 printk("\tpc = 0x%08lx\n", vcpu->arch.pc);;
919 printk("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
921 for (i = 0; i < 32; i += 4) {
922 printk("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
923 vcpu->arch.gprs[i],
924 vcpu->arch.gprs[i + 1],
925 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
927 printk("\thi: 0x%08lx\n", vcpu->arch.hi);
928 printk("\tlo: 0x%08lx\n", vcpu->arch.lo);
930 cop0 = vcpu->arch.cop0;
931 printk("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
932 kvm_read_c0_guest_status(cop0), kvm_read_c0_guest_cause(cop0));
934 printk("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
936 return 0;
939 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
941 int i;
943 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
944 vcpu->arch.gprs[i] = regs->gpr[i];
945 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
946 vcpu->arch.hi = regs->hi;
947 vcpu->arch.lo = regs->lo;
948 vcpu->arch.pc = regs->pc;
950 return 0;
953 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
955 int i;
957 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
958 regs->gpr[i] = vcpu->arch.gprs[i];
960 regs->hi = vcpu->arch.hi;
961 regs->lo = vcpu->arch.lo;
962 regs->pc = vcpu->arch.pc;
964 return 0;
967 void kvm_mips_comparecount_func(unsigned long data)
969 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
971 kvm_mips_callbacks->queue_timer_int(vcpu);
973 vcpu->arch.wait = 0;
974 if (waitqueue_active(&vcpu->wq)) {
975 wake_up_interruptible(&vcpu->wq);
980 * low level hrtimer wake routine.
982 enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
984 struct kvm_vcpu *vcpu;
986 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
987 kvm_mips_comparecount_func((unsigned long) vcpu);
988 hrtimer_forward_now(&vcpu->arch.comparecount_timer,
989 ktime_set(0, MS_TO_NS(10)));
990 return HRTIMER_RESTART;
993 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
995 kvm_mips_callbacks->vcpu_init(vcpu);
996 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
997 HRTIMER_MODE_REL);
998 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
999 kvm_mips_init_shadow_tlb(vcpu);
1000 return 0;
1003 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1005 return;
1009 kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, struct kvm_translation *tr)
1011 return 0;
1014 /* Initial guest state */
1015 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1017 return kvm_mips_callbacks->vcpu_setup(vcpu);
1020 static
1021 void kvm_mips_set_c0_status(void)
1023 uint32_t status = read_c0_status();
1025 if (cpu_has_fpu)
1026 status |= (ST0_CU1);
1028 if (cpu_has_dsp)
1029 status |= (ST0_MX);
1031 write_c0_status(status);
1032 ehb();
1036 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1038 int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1040 uint32_t cause = vcpu->arch.host_cp0_cause;
1041 uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1042 uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
1043 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1044 enum emulation_result er = EMULATE_DONE;
1045 int ret = RESUME_GUEST;
1047 /* Set a default exit reason */
1048 run->exit_reason = KVM_EXIT_UNKNOWN;
1049 run->ready_for_interrupt_injection = 1;
1051 /* Set the appropriate status bits based on host CPU features, before we hit the scheduler */
1052 kvm_mips_set_c0_status();
1054 local_irq_enable();
1056 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1057 cause, opc, run, vcpu);
1059 /* Do a privilege check, if in UM most of these exit conditions end up
1060 * causing an exception to be delivered to the Guest Kernel
1062 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1063 if (er == EMULATE_PRIV_FAIL) {
1064 goto skip_emul;
1065 } else if (er == EMULATE_FAIL) {
1066 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1067 ret = RESUME_HOST;
1068 goto skip_emul;
1071 switch (exccode) {
1072 case T_INT:
1073 kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc);
1075 ++vcpu->stat.int_exits;
1076 trace_kvm_exit(vcpu, INT_EXITS);
1078 if (need_resched()) {
1079 cond_resched();
1082 ret = RESUME_GUEST;
1083 break;
1085 case T_COP_UNUSABLE:
1086 kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc);
1088 ++vcpu->stat.cop_unusable_exits;
1089 trace_kvm_exit(vcpu, COP_UNUSABLE_EXITS);
1090 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1091 /* XXXKYMA: Might need to return to user space */
1092 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN) {
1093 ret = RESUME_HOST;
1095 break;
1097 case T_TLB_MOD:
1098 ++vcpu->stat.tlbmod_exits;
1099 trace_kvm_exit(vcpu, TLBMOD_EXITS);
1100 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1101 break;
1103 case T_TLB_ST_MISS:
1104 kvm_debug
1105 ("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1106 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1107 badvaddr);
1109 ++vcpu->stat.tlbmiss_st_exits;
1110 trace_kvm_exit(vcpu, TLBMISS_ST_EXITS);
1111 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1112 break;
1114 case T_TLB_LD_MISS:
1115 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1116 cause, opc, badvaddr);
1118 ++vcpu->stat.tlbmiss_ld_exits;
1119 trace_kvm_exit(vcpu, TLBMISS_LD_EXITS);
1120 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1121 break;
1123 case T_ADDR_ERR_ST:
1124 ++vcpu->stat.addrerr_st_exits;
1125 trace_kvm_exit(vcpu, ADDRERR_ST_EXITS);
1126 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1127 break;
1129 case T_ADDR_ERR_LD:
1130 ++vcpu->stat.addrerr_ld_exits;
1131 trace_kvm_exit(vcpu, ADDRERR_LD_EXITS);
1132 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1133 break;
1135 case T_SYSCALL:
1136 ++vcpu->stat.syscall_exits;
1137 trace_kvm_exit(vcpu, SYSCALL_EXITS);
1138 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1139 break;
1141 case T_RES_INST:
1142 ++vcpu->stat.resvd_inst_exits;
1143 trace_kvm_exit(vcpu, RESVD_INST_EXITS);
1144 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1145 break;
1147 case T_BREAK:
1148 ++vcpu->stat.break_inst_exits;
1149 trace_kvm_exit(vcpu, BREAK_INST_EXITS);
1150 ret = kvm_mips_callbacks->handle_break(vcpu);
1151 break;
1153 default:
1154 kvm_err
1155 ("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1156 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1157 kvm_read_c0_guest_status(vcpu->arch.cop0));
1158 kvm_arch_vcpu_dump_regs(vcpu);
1159 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1160 ret = RESUME_HOST;
1161 break;
1165 skip_emul:
1166 local_irq_disable();
1168 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1169 kvm_mips_deliver_interrupts(vcpu, cause);
1171 if (!(ret & RESUME_HOST)) {
1172 /* Only check for signals if not already exiting to userspace */
1173 if (signal_pending(current)) {
1174 run->exit_reason = KVM_EXIT_INTR;
1175 ret = (-EINTR << 2) | RESUME_HOST;
1176 ++vcpu->stat.signal_exits;
1177 trace_kvm_exit(vcpu, SIGNAL_EXITS);
1181 return ret;
1184 int __init kvm_mips_init(void)
1186 int ret;
1188 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1190 if (ret)
1191 return ret;
1193 /* On MIPS, kernel modules are executed from "mapped space", which requires TLBs.
1194 * The TLB handling code is statically linked with the rest of the kernel (kvm_tlb.c)
1195 * to avoid the possibility of double faulting. The issue is that the TLB code
1196 * references routines that are part of the the KVM module,
1197 * which are only available once the module is loaded.
1199 kvm_mips_gfn_to_pfn = gfn_to_pfn;
1200 kvm_mips_release_pfn_clean = kvm_release_pfn_clean;
1201 kvm_mips_is_error_pfn = is_error_pfn;
1203 pr_info("KVM/MIPS Initialized\n");
1204 return 0;
1207 void __exit kvm_mips_exit(void)
1209 kvm_exit();
1211 kvm_mips_gfn_to_pfn = NULL;
1212 kvm_mips_release_pfn_clean = NULL;
1213 kvm_mips_is_error_pfn = NULL;
1215 pr_info("KVM/MIPS unloaded\n");
1218 module_init(kvm_mips_init);
1219 module_exit(kvm_mips_exit);
1221 EXPORT_TRACEPOINT_SYMBOL(kvm_exit);