blob: 330b3af701a64c9f6c549e6bb5a13440b6143d1e [file] [log] [blame]
Sanjay Lal669e8462012-11-21 18:34:02 -08001/*
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.
5 *
6 * KVM/MIPS: MIPS specific KVM APIs
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070010 */
Sanjay Lal669e8462012-11-21 18:34:02 -080011
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>
21
22#include <linux/kvm_host.h>
23
24#include "kvm_mips_int.h"
25#include "kvm_mips_comm.h"
26
27#define CREATE_TRACE_POINTS
28#include "trace.h"
29
30#ifndef VECTORSPACING
31#define VECTORSPACING 0x100 /* for EI/VI mode */
32#endif
33
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070034#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
Sanjay Lal669e8462012-11-21 18:34:02 -080035struct kvm_stats_debugfs_item debugfs_entries[] = {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070036 { "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 },
Sanjay Lal669e8462012-11-21 18:34:02 -080051 {NULL}
52};
53
54static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu)
55{
56 int i;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070057
Sanjay Lal669e8462012-11-21 18:34:02 -080058 for_each_possible_cpu(i) {
59 vcpu->arch.guest_kernel_asid[i] = 0;
60 vcpu->arch.guest_user_asid[i] = 0;
61 }
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070062
Sanjay Lal669e8462012-11-21 18:34:02 -080063 return 0;
64}
65
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070066/*
67 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
68 * Config7, so we are "runnable" if interrupts are pending
Sanjay Lal669e8462012-11-21 18:34:02 -080069 */
70int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
71{
72 return !!(vcpu->arch.pending_exceptions);
73}
74
75int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
76{
77 return 1;
78}
79
80int kvm_arch_hardware_enable(void *garbage)
81{
82 return 0;
83}
84
85void kvm_arch_hardware_disable(void *garbage)
86{
87}
88
89int kvm_arch_hardware_setup(void)
90{
91 return 0;
92}
93
94void kvm_arch_hardware_unsetup(void)
95{
96}
97
98void kvm_arch_check_processor_compat(void *rtn)
99{
100 int *r = (int *)rtn;
101 *r = 0;
102 return;
103}
104
105static void kvm_mips_init_tlbs(struct kvm *kvm)
106{
107 unsigned long wired;
108
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700109 /*
110 * Add a wired entry to the TLB, it is used to map the commpage to
111 * the Guest kernel
112 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800113 wired = read_c0_wired();
114 write_c0_wired(wired + 1);
115 mtc0_tlbw_hazard();
116 kvm->arch.commpage_tlb = wired;
117
118 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
119 kvm->arch.commpage_tlb);
120}
121
122static void kvm_mips_init_vm_percpu(void *arg)
123{
124 struct kvm *kvm = (struct kvm *)arg;
125
126 kvm_mips_init_tlbs(kvm);
127 kvm_mips_callbacks->vm_init(kvm);
128
129}
130
131int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
132{
133 if (atomic_inc_return(&kvm_mips_instance) == 1) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100134 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
135 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800136 on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
137 }
138
Sanjay Lal669e8462012-11-21 18:34:02 -0800139 return 0;
140}
141
142void kvm_mips_free_vcpus(struct kvm *kvm)
143{
144 unsigned int i;
145 struct kvm_vcpu *vcpu;
146
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]);
151 }
James Hoganc6c0a662014-05-29 10:16:44 +0100152 kfree(kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800153
154 kvm_for_each_vcpu(i, vcpu, kvm) {
155 kvm_arch_vcpu_free(vcpu);
156 }
157
158 mutex_lock(&kvm->lock);
159
160 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
161 kvm->vcpus[i] = NULL;
162
163 atomic_set(&kvm->online_vcpus, 0);
164
165 mutex_unlock(&kvm->lock);
166}
167
168void kvm_arch_sync_events(struct kvm *kvm)
169{
170}
171
172static void kvm_mips_uninit_tlbs(void *arg)
173{
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();
179}
180
181void kvm_arch_destroy_vm(struct kvm *kvm)
182{
183 kvm_mips_free_vcpus(kvm);
184
185 /* If this is the last instance, restore wired count */
186 if (atomic_dec_return(&kvm_mips_instance) == 0) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100187 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
188 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800189 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
190 }
191}
192
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700193long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
194 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800195{
David Daneyed829852013-05-23 09:49:10 -0700196 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800197}
198
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530199void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Sanjay Lal669e8462012-11-21 18:34:02 -0800200 struct kvm_memory_slot *dont)
201{
202}
203
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530204int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
205 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800206{
207 return 0;
208}
209
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +0900210void kvm_arch_memslots_updated(struct kvm *kvm)
211{
212}
213
Sanjay Lal669e8462012-11-21 18:34:02 -0800214int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700215 struct kvm_memory_slot *memslot,
216 struct kvm_userspace_memory_region *mem,
217 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800218{
219 return 0;
220}
221
222void kvm_arch_commit_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700223 struct kvm_userspace_memory_region *mem,
224 const struct kvm_memory_slot *old,
225 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800226{
227 unsigned long npages = 0;
228 int i, err = 0;
229
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);
233
234 /* Setup Guest PMAP table */
235 if (!kvm->arch.guest_pmap) {
236 if (mem->slot == 0)
237 npages = mem->memory_size >> PAGE_SHIFT;
238
239 if (npages) {
240 kvm->arch.guest_pmap_npages = npages;
241 kvm->arch.guest_pmap =
242 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
243
244 if (!kvm->arch.guest_pmap) {
245 kvm_err("Failed to allocate guest PMAP");
246 err = -ENOMEM;
247 goto out;
248 }
249
James Hogan6e95bfd2014-05-29 10:16:43 +0100250 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
251 npages, kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800252
253 /* Now setup the page table */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700254 for (i = 0; i < npages; i++)
Sanjay Lal669e8462012-11-21 18:34:02 -0800255 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
Sanjay Lal669e8462012-11-21 18:34:02 -0800256 }
257 }
258out:
259 return;
260}
261
262void kvm_arch_flush_shadow_all(struct kvm *kvm)
263{
264}
265
266void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
267 struct kvm_memory_slot *slot)
268{
269}
270
271void kvm_arch_flush_shadow(struct kvm *kvm)
272{
273}
274
275struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
276{
Sanjay Lal669e8462012-11-21 18:34:02 -0800277 int err, size, offset;
278 void *gebase;
279 int i;
280
281 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
282
283 if (!vcpu) {
284 err = -ENOMEM;
285 goto out;
286 }
287
288 err = kvm_vcpu_init(vcpu, kvm, id);
289
290 if (err)
291 goto out_free_cpu;
292
James Hogan6e95bfd2014-05-29 10:16:43 +0100293 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800294
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700295 /*
296 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800297 * guest mode exits
298 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700299 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800300 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700301 else
James Hogan7006e2d2014-05-29 10:16:23 +0100302 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800303
304 /* Save Linux EBASE */
305 vcpu->arch.host_ebase = (void *)read_c0_ebase();
306
307 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
308
309 if (!gebase) {
310 err = -ENOMEM;
311 goto out_free_cpu;
312 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100313 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
314 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800315
316 /* Save new ebase */
317 vcpu->arch.guest_ebase = gebase;
318
319 /* Copy L1 Guest Exception handler to correct offset */
320
321 /* TLB Refill, EXL = 0 */
322 memcpy(gebase, mips32_exception,
323 mips32_exceptionEnd - mips32_exception);
324
325 /* General Exception Entry point */
326 memcpy(gebase + 0x180, mips32_exception,
327 mips32_exceptionEnd - mips32_exception);
328
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);
335 }
336
337 /* General handler, relocate to unmapped space for sanity's sake */
338 offset = 0x2000;
James Hogan6e95bfd2014-05-29 10:16:43 +0100339 kvm_debug("Installing KVM Exception handlers @ %p, %#x bytes\n",
340 gebase + offset,
341 mips32_GuestExceptionEnd - mips32_GuestException);
Sanjay Lal669e8462012-11-21 18:34:02 -0800342
343 memcpy(gebase + offset, mips32_GuestException,
344 mips32_GuestExceptionEnd - mips32_GuestException);
345
346 /* Invalidate the icache for these ranges */
James Hoganfacaaec2014-05-29 10:16:25 +0100347 local_flush_icache_range((unsigned long)gebase,
348 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800349
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700350 /*
351 * Allocate comm page for guest kernel, a TLB will be reserved for
352 * mapping GVA @ 0xFFFF8000 to this page
353 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800354 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
355
356 if (!vcpu->arch.kseg0_commpage) {
357 err = -ENOMEM;
358 goto out_free_gebase;
359 }
360
James Hogan6e95bfd2014-05-29 10:16:43 +0100361 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800362 kvm_mips_commpage_init(vcpu);
363
364 /* Init */
365 vcpu->arch.last_sched_cpu = -1;
366
367 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100368 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800369
370 return vcpu;
371
372out_free_gebase:
373 kfree(gebase);
374
375out_free_cpu:
376 kfree(vcpu);
377
378out:
379 return ERR_PTR(err);
380}
381
382void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
383{
384 hrtimer_cancel(&vcpu->arch.comparecount_timer);
385
386 kvm_vcpu_uninit(vcpu);
387
388 kvm_mips_dump_stats(vcpu);
389
James Hoganc6c0a662014-05-29 10:16:44 +0100390 kfree(vcpu->arch.guest_ebase);
391 kfree(vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800392}
393
394void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
395{
396 kvm_arch_vcpu_free(vcpu);
397}
398
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700399int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
400 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800401{
David Daneyed829852013-05-23 09:49:10 -0700402 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800403}
404
405int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
406{
407 int r = 0;
408 sigset_t sigsaved;
409
410 if (vcpu->sigset_active)
411 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
412
413 if (vcpu->mmio_needed) {
414 if (!vcpu->mmio_is_write)
415 kvm_mips_complete_mmio_load(vcpu, run);
416 vcpu->mmio_needed = 0;
417 }
418
James Hogan044f0f02014-05-29 10:16:32 +0100419 local_irq_disable();
Sanjay Lal669e8462012-11-21 18:34:02 -0800420 /* Check if we have any exceptions/interrupts pending */
421 kvm_mips_deliver_interrupts(vcpu,
422 kvm_read_c0_guest_cause(vcpu->arch.cop0));
423
Sanjay Lal669e8462012-11-21 18:34:02 -0800424 kvm_guest_enter();
425
426 r = __kvm_mips_vcpu_run(run, vcpu);
427
428 kvm_guest_exit();
429 local_irq_enable();
430
431 if (vcpu->sigset_active)
432 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
433
434 return r;
435}
436
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700437int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
438 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800439{
440 int intr = (int)irq->irq;
441 struct kvm_vcpu *dvcpu = NULL;
442
443 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
444 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
445 (int)intr);
446
447 if (irq->cpu == -1)
448 dvcpu = vcpu;
449 else
450 dvcpu = vcpu->kvm->vcpus[irq->cpu];
451
452 if (intr == 2 || intr == 3 || intr == 4) {
453 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
454
455 } else if (intr == -2 || intr == -3 || intr == -4) {
456 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
457 } else {
458 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
459 irq->cpu, irq->irq);
460 return -EINVAL;
461 }
462
463 dvcpu->arch.wait = 0;
464
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700465 if (waitqueue_active(&dvcpu->wq))
Sanjay Lal669e8462012-11-21 18:34:02 -0800466 wake_up_interruptible(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800467
468 return 0;
469}
470
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700471int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
472 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800473{
David Daneyed829852013-05-23 09:49:10 -0700474 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800475}
476
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700477int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
478 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800479{
David Daneyed829852013-05-23 09:49:10 -0700480 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800481}
482
David Daney4c73fb22013-05-23 09:49:09 -0700483static u64 kvm_mips_get_one_regs[] = {
484 KVM_REG_MIPS_R0,
485 KVM_REG_MIPS_R1,
486 KVM_REG_MIPS_R2,
487 KVM_REG_MIPS_R3,
488 KVM_REG_MIPS_R4,
489 KVM_REG_MIPS_R5,
490 KVM_REG_MIPS_R6,
491 KVM_REG_MIPS_R7,
492 KVM_REG_MIPS_R8,
493 KVM_REG_MIPS_R9,
494 KVM_REG_MIPS_R10,
495 KVM_REG_MIPS_R11,
496 KVM_REG_MIPS_R12,
497 KVM_REG_MIPS_R13,
498 KVM_REG_MIPS_R14,
499 KVM_REG_MIPS_R15,
500 KVM_REG_MIPS_R16,
501 KVM_REG_MIPS_R17,
502 KVM_REG_MIPS_R18,
503 KVM_REG_MIPS_R19,
504 KVM_REG_MIPS_R20,
505 KVM_REG_MIPS_R21,
506 KVM_REG_MIPS_R22,
507 KVM_REG_MIPS_R23,
508 KVM_REG_MIPS_R24,
509 KVM_REG_MIPS_R25,
510 KVM_REG_MIPS_R26,
511 KVM_REG_MIPS_R27,
512 KVM_REG_MIPS_R28,
513 KVM_REG_MIPS_R29,
514 KVM_REG_MIPS_R30,
515 KVM_REG_MIPS_R31,
516
517 KVM_REG_MIPS_HI,
518 KVM_REG_MIPS_LO,
519 KVM_REG_MIPS_PC,
520
521 KVM_REG_MIPS_CP0_INDEX,
522 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100523 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700524 KVM_REG_MIPS_CP0_PAGEMASK,
525 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100526 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700527 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100528 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700529 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100530 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700531 KVM_REG_MIPS_CP0_STATUS,
532 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100533 KVM_REG_MIPS_CP0_EPC,
David Daney4c73fb22013-05-23 09:49:09 -0700534 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,
James Hoganf8239342014-05-29 10:16:37 +0100539 KVM_REG_MIPS_CP0_ERROREPC,
540
541 KVM_REG_MIPS_COUNT_CTL,
542 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100543 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700544};
545
546static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
547 const struct kvm_one_reg *reg)
548{
David Daney4c73fb22013-05-23 09:49:09 -0700549 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganf8be02d2014-05-29 10:16:29 +0100550 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700551 s64 v;
552
553 switch (reg->id) {
554 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
555 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
556 break;
557 case KVM_REG_MIPS_HI:
558 v = (long)vcpu->arch.hi;
559 break;
560 case KVM_REG_MIPS_LO:
561 v = (long)vcpu->arch.lo;
562 break;
563 case KVM_REG_MIPS_PC:
564 v = (long)vcpu->arch.pc;
565 break;
566
567 case KVM_REG_MIPS_CP0_INDEX:
568 v = (long)kvm_read_c0_guest_index(cop0);
569 break;
570 case KVM_REG_MIPS_CP0_CONTEXT:
571 v = (long)kvm_read_c0_guest_context(cop0);
572 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100573 case KVM_REG_MIPS_CP0_USERLOCAL:
574 v = (long)kvm_read_c0_guest_userlocal(cop0);
575 break;
David Daney4c73fb22013-05-23 09:49:09 -0700576 case KVM_REG_MIPS_CP0_PAGEMASK:
577 v = (long)kvm_read_c0_guest_pagemask(cop0);
578 break;
579 case KVM_REG_MIPS_CP0_WIRED:
580 v = (long)kvm_read_c0_guest_wired(cop0);
581 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100582 case KVM_REG_MIPS_CP0_HWRENA:
583 v = (long)kvm_read_c0_guest_hwrena(cop0);
584 break;
David Daney4c73fb22013-05-23 09:49:09 -0700585 case KVM_REG_MIPS_CP0_BADVADDR:
586 v = (long)kvm_read_c0_guest_badvaddr(cop0);
587 break;
588 case KVM_REG_MIPS_CP0_ENTRYHI:
589 v = (long)kvm_read_c0_guest_entryhi(cop0);
590 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100591 case KVM_REG_MIPS_CP0_COMPARE:
592 v = (long)kvm_read_c0_guest_compare(cop0);
593 break;
David Daney4c73fb22013-05-23 09:49:09 -0700594 case KVM_REG_MIPS_CP0_STATUS:
595 v = (long)kvm_read_c0_guest_status(cop0);
596 break;
597 case KVM_REG_MIPS_CP0_CAUSE:
598 v = (long)kvm_read_c0_guest_cause(cop0);
599 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100600 case KVM_REG_MIPS_CP0_EPC:
601 v = (long)kvm_read_c0_guest_epc(cop0);
602 break;
David Daney4c73fb22013-05-23 09:49:09 -0700603 case KVM_REG_MIPS_CP0_ERROREPC:
604 v = (long)kvm_read_c0_guest_errorepc(cop0);
605 break;
606 case KVM_REG_MIPS_CP0_CONFIG:
607 v = (long)kvm_read_c0_guest_config(cop0);
608 break;
609 case KVM_REG_MIPS_CP0_CONFIG1:
610 v = (long)kvm_read_c0_guest_config1(cop0);
611 break;
612 case KVM_REG_MIPS_CP0_CONFIG2:
613 v = (long)kvm_read_c0_guest_config2(cop0);
614 break;
615 case KVM_REG_MIPS_CP0_CONFIG3:
616 v = (long)kvm_read_c0_guest_config3(cop0);
617 break;
618 case KVM_REG_MIPS_CP0_CONFIG7:
619 v = (long)kvm_read_c0_guest_config7(cop0);
620 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100621 /* registers to be handled specially */
622 case KVM_REG_MIPS_CP0_COUNT:
James Hoganf8239342014-05-29 10:16:37 +0100623 case KVM_REG_MIPS_COUNT_CTL:
624 case KVM_REG_MIPS_COUNT_RESUME:
James Hoganf74a8e22014-05-29 10:16:38 +0100625 case KVM_REG_MIPS_COUNT_HZ:
James Hoganf8be02d2014-05-29 10:16:29 +0100626 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
627 if (ret)
628 return ret;
629 break;
David Daney4c73fb22013-05-23 09:49:09 -0700630 default:
631 return -EINVAL;
632 }
David Daney681865d2013-06-10 12:33:48 -0700633 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
634 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700635
David Daney681865d2013-06-10 12:33:48 -0700636 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;
639 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700640
David Daney681865d2013-06-10 12:33:48 -0700641 return put_user(v32, uaddr32);
642 } else {
643 return -EINVAL;
644 }
David Daney4c73fb22013-05-23 09:49:09 -0700645}
646
647static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
648 const struct kvm_one_reg *reg)
649{
David Daney4c73fb22013-05-23 09:49:09 -0700650 struct mips_coproc *cop0 = vcpu->arch.cop0;
651 u64 v;
652
David Daney681865d2013-06-10 12:33:48 -0700653 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
654 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
655
656 if (get_user(v, uaddr64) != 0)
657 return -EFAULT;
658 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
659 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
660 s32 v32;
661
662 if (get_user(v32, uaddr32) != 0)
663 return -EFAULT;
664 v = (s64)v32;
665 } else {
666 return -EINVAL;
667 }
David Daney4c73fb22013-05-23 09:49:09 -0700668
669 switch (reg->id) {
670 case KVM_REG_MIPS_R0:
671 /* Silently ignore requests to set $0 */
672 break;
673 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
674 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
675 break;
676 case KVM_REG_MIPS_HI:
677 vcpu->arch.hi = v;
678 break;
679 case KVM_REG_MIPS_LO:
680 vcpu->arch.lo = v;
681 break;
682 case KVM_REG_MIPS_PC:
683 vcpu->arch.pc = v;
684 break;
685
686 case KVM_REG_MIPS_CP0_INDEX:
687 kvm_write_c0_guest_index(cop0, v);
688 break;
689 case KVM_REG_MIPS_CP0_CONTEXT:
690 kvm_write_c0_guest_context(cop0, v);
691 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100692 case KVM_REG_MIPS_CP0_USERLOCAL:
693 kvm_write_c0_guest_userlocal(cop0, v);
694 break;
David Daney4c73fb22013-05-23 09:49:09 -0700695 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;
James Hogan16fd5c12014-05-29 10:16:31 +0100701 case KVM_REG_MIPS_CP0_HWRENA:
702 kvm_write_c0_guest_hwrena(cop0, v);
703 break;
David Daney4c73fb22013-05-23 09:49:09 -0700704 case KVM_REG_MIPS_CP0_BADVADDR:
705 kvm_write_c0_guest_badvaddr(cop0, v);
706 break;
707 case KVM_REG_MIPS_CP0_ENTRYHI:
708 kvm_write_c0_guest_entryhi(cop0, v);
709 break;
710 case KVM_REG_MIPS_CP0_STATUS:
711 kvm_write_c0_guest_status(cop0, v);
712 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100713 case KVM_REG_MIPS_CP0_EPC:
714 kvm_write_c0_guest_epc(cop0, v);
715 break;
David Daney4c73fb22013-05-23 09:49:09 -0700716 case KVM_REG_MIPS_CP0_ERROREPC:
717 kvm_write_c0_guest_errorepc(cop0, v);
718 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100719 /* registers to be handled specially */
720 case KVM_REG_MIPS_CP0_COUNT:
721 case KVM_REG_MIPS_CP0_COMPARE:
James Hogane30492b2014-05-29 10:16:35 +0100722 case KVM_REG_MIPS_CP0_CAUSE:
James Hoganf8239342014-05-29 10:16:37 +0100723 case KVM_REG_MIPS_COUNT_CTL:
724 case KVM_REG_MIPS_COUNT_RESUME:
James Hoganf74a8e22014-05-29 10:16:38 +0100725 case KVM_REG_MIPS_COUNT_HZ:
James Hoganf8be02d2014-05-29 10:16:29 +0100726 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700727 default:
728 return -EINVAL;
729 }
730 return 0;
731}
732
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700733long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
734 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800735{
736 struct kvm_vcpu *vcpu = filp->private_data;
737 void __user *argp = (void __user *)arg;
738 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800739
740 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -0700741 case KVM_SET_ONE_REG:
742 case KVM_GET_ONE_REG: {
743 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700744
David Daney4c73fb22013-05-23 09:49:09 -0700745 if (copy_from_user(&reg, argp, sizeof(reg)))
746 return -EFAULT;
747 if (ioctl == KVM_SET_ONE_REG)
748 return kvm_mips_set_reg(vcpu, &reg);
749 else
750 return kvm_mips_get_reg(vcpu, &reg);
751 }
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;
756 unsigned n;
757
758 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
759 return -EFAULT;
760 n = reg_list.n;
761 reg_list.n = ARRAY_SIZE(kvm_mips_get_one_regs);
762 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
763 return -EFAULT;
764 if (n < reg_list.n)
765 return -E2BIG;
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)))
769 return -EFAULT;
770 return 0;
771 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800772 case KVM_NMI:
773 /* Treat the NMI as a CPU reset */
774 r = kvm_mips_reset_vcpu(vcpu);
775 break;
776 case KVM_INTERRUPT:
777 {
778 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700779
Sanjay Lal669e8462012-11-21 18:34:02 -0800780 r = -EFAULT;
781 if (copy_from_user(&irq, argp, sizeof(irq)))
782 goto out;
783
Sanjay Lal669e8462012-11-21 18:34:02 -0800784 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
785 irq.irq);
786
787 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
788 break;
789 }
790 default:
David Daney4c73fb22013-05-23 09:49:09 -0700791 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800792 }
793
794out:
795 return r;
796}
797
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700798/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -0800799int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
800{
801 struct kvm_memory_slot *memslot;
802 unsigned long ga, ga_end;
803 int is_dirty = 0;
804 int r;
805 unsigned long n;
806
807 mutex_lock(&kvm->slots_lock);
808
809 r = kvm_get_dirty_log(kvm, log, &is_dirty);
810 if (r)
811 goto out;
812
813 /* If nothing is dirty, don't bother messing with page tables. */
814 if (is_dirty) {
815 memslot = &kvm->memslots->memslots[log->slot];
816
817 ga = memslot->base_gfn << PAGE_SHIFT;
818 ga_end = ga + (memslot->npages << PAGE_SHIFT);
819
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700820 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
821 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -0800822
823 n = kvm_dirty_bitmap_bytes(memslot);
824 memset(memslot->dirty_bitmap, 0, n);
825 }
826
827 r = 0;
828out:
829 mutex_unlock(&kvm->slots_lock);
830 return r;
831
832}
833
834long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
835{
836 long r;
837
838 switch (ioctl) {
839 default:
David Daneyed829852013-05-23 09:49:10 -0700840 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800841 }
842
843 return r;
844}
845
846int kvm_arch_init(void *opaque)
847{
848 int ret;
849
850 if (kvm_mips_callbacks) {
851 kvm_err("kvm: module already exists\n");
852 return -EEXIST;
853 }
854
855 ret = kvm_mips_emulation_init(&kvm_mips_callbacks);
856
857 return ret;
858}
859
860void kvm_arch_exit(void)
861{
862 kvm_mips_callbacks = NULL;
863}
864
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700865int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
866 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -0800867{
David Daneyed829852013-05-23 09:49:10 -0700868 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800869}
870
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700871int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
872 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -0800873{
David Daneyed829852013-05-23 09:49:10 -0700874 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800875}
876
877int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
878{
879 return 0;
880}
881
882int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
883{
David Daneyed829852013-05-23 09:49:10 -0700884 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800885}
886
887int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
888{
David Daneyed829852013-05-23 09:49:10 -0700889 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800890}
891
892int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
893{
894 return VM_FAULT_SIGBUS;
895}
896
897int kvm_dev_ioctl_check_extension(long ext)
898{
899 int r;
900
901 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -0700902 case KVM_CAP_ONE_REG:
903 r = 1;
904 break;
Sanjay Lal669e8462012-11-21 18:34:02 -0800905 case KVM_CAP_COALESCED_MMIO:
906 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
907 break;
908 default:
909 r = 0;
910 break;
911 }
912 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800913}
914
915int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
916{
917 return kvm_mips_pending_timer(vcpu);
918}
919
920int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
921{
922 int i;
923 struct mips_coproc *cop0;
924
925 if (!vcpu)
926 return -1;
927
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700928 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);
Sanjay Lal669e8462012-11-21 18:34:02 -0800931
932 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700933 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -0800934 vcpu->arch.gprs[i],
935 vcpu->arch.gprs[i + 1],
936 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
937 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700938 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
939 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -0800940
941 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700942 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
943 kvm_read_c0_guest_status(cop0),
944 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -0800945
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700946 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -0800947
948 return 0;
949}
950
951int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
952{
953 int i;
954
David Daney8d17dd02013-05-23 09:49:08 -0700955 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -0700956 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -0700957 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -0800958 vcpu->arch.hi = regs->hi;
959 vcpu->arch.lo = regs->lo;
960 vcpu->arch.pc = regs->pc;
961
David Daney4c73fb22013-05-23 09:49:09 -0700962 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -0800963}
964
965int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
966{
967 int i;
968
David Daney8d17dd02013-05-23 09:49:08 -0700969 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -0700970 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -0800971
972 regs->hi = vcpu->arch.hi;
973 regs->lo = vcpu->arch.lo;
974 regs->pc = vcpu->arch.pc;
975
David Daney4c73fb22013-05-23 09:49:09 -0700976 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -0800977}
978
James Hogan0fae34f2014-05-29 10:16:39 +0100979static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -0800980{
981 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
982
983 kvm_mips_callbacks->queue_timer_int(vcpu);
984
985 vcpu->arch.wait = 0;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700986 if (waitqueue_active(&vcpu->wq))
Sanjay Lal669e8462012-11-21 18:34:02 -0800987 wake_up_interruptible(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800988}
989
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700990/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +0100991static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -0800992{
993 struct kvm_vcpu *vcpu;
994
995 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
996 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100997 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800998}
999
1000int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1001{
1002 kvm_mips_callbacks->vcpu_init(vcpu);
1003 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1004 HRTIMER_MODE_REL);
1005 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001006 return 0;
1007}
1008
1009void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1010{
1011 return;
1012}
1013
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001014int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1015 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001016{
1017 return 0;
1018}
1019
1020/* Initial guest state */
1021int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1022{
1023 return kvm_mips_callbacks->vcpu_setup(vcpu);
1024}
1025
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001026static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001027{
1028 uint32_t status = read_c0_status();
1029
1030 if (cpu_has_fpu)
1031 status |= (ST0_CU1);
1032
1033 if (cpu_has_dsp)
1034 status |= (ST0_MX);
1035
1036 write_c0_status(status);
1037 ehb();
1038}
1039
1040/*
1041 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1042 */
1043int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1044{
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;
1051
1052 /* Set a default exit reason */
1053 run->exit_reason = KVM_EXIT_UNKNOWN;
1054 run->ready_for_interrupt_injection = 1;
1055
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001056 /*
1057 * Set the appropriate status bits based on host CPU features,
1058 * before we hit the scheduler
1059 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001060 kvm_mips_set_c0_status();
1061
1062 local_irq_enable();
1063
1064 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1065 cause, opc, run, vcpu);
1066
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001067 /*
1068 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001069 * causing an exception to be delivered to the Guest Kernel
1070 */
1071 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1072 if (er == EMULATE_PRIV_FAIL) {
1073 goto skip_emul;
1074 } else if (er == EMULATE_FAIL) {
1075 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1076 ret = RESUME_HOST;
1077 goto skip_emul;
1078 }
1079
1080 switch (exccode) {
1081 case T_INT:
1082 kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc);
1083
1084 ++vcpu->stat.int_exits;
1085 trace_kvm_exit(vcpu, INT_EXITS);
1086
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001087 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001088 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001089
1090 ret = RESUME_GUEST;
1091 break;
1092
1093 case T_COP_UNUSABLE:
1094 kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc);
1095
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 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001100 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001101 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001102 break;
1103
1104 case T_TLB_MOD:
1105 ++vcpu->stat.tlbmod_exits;
1106 trace_kvm_exit(vcpu, TLBMOD_EXITS);
1107 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1108 break;
1109
1110 case T_TLB_ST_MISS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001111 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,
1113 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001114
1115 ++vcpu->stat.tlbmiss_st_exits;
1116 trace_kvm_exit(vcpu, TLBMISS_ST_EXITS);
1117 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1118 break;
1119
1120 case T_TLB_LD_MISS:
1121 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1122 cause, opc, badvaddr);
1123
1124 ++vcpu->stat.tlbmiss_ld_exits;
1125 trace_kvm_exit(vcpu, TLBMISS_LD_EXITS);
1126 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1127 break;
1128
1129 case T_ADDR_ERR_ST:
1130 ++vcpu->stat.addrerr_st_exits;
1131 trace_kvm_exit(vcpu, ADDRERR_ST_EXITS);
1132 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1133 break;
1134
1135 case T_ADDR_ERR_LD:
1136 ++vcpu->stat.addrerr_ld_exits;
1137 trace_kvm_exit(vcpu, ADDRERR_LD_EXITS);
1138 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1139 break;
1140
1141 case T_SYSCALL:
1142 ++vcpu->stat.syscall_exits;
1143 trace_kvm_exit(vcpu, SYSCALL_EXITS);
1144 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1145 break;
1146
1147 case T_RES_INST:
1148 ++vcpu->stat.resvd_inst_exits;
1149 trace_kvm_exit(vcpu, RESVD_INST_EXITS);
1150 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1151 break;
1152
1153 case T_BREAK:
1154 ++vcpu->stat.break_inst_exits;
1155 trace_kvm_exit(vcpu, BREAK_INST_EXITS);
1156 ret = kvm_mips_callbacks->handle_break(vcpu);
1157 break;
1158
1159 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001160 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));
Sanjay Lal669e8462012-11-21 18:34:02 -08001163 kvm_arch_vcpu_dump_regs(vcpu);
1164 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1165 ret = RESUME_HOST;
1166 break;
1167
1168 }
1169
1170skip_emul:
1171 local_irq_disable();
1172
1173 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1174 kvm_mips_deliver_interrupts(vcpu, cause);
1175
1176 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001177 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001178 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);
1183 }
1184 }
1185
1186 return ret;
1187}
1188
1189int __init kvm_mips_init(void)
1190{
1191 int ret;
1192
1193 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1194
1195 if (ret)
1196 return ret;
1197
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001198 /*
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.
Sanjay Lal669e8462012-11-21 18:34:02 -08001205 */
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;
1209
1210 pr_info("KVM/MIPS Initialized\n");
1211 return 0;
1212}
1213
1214void __exit kvm_mips_exit(void)
1215{
1216 kvm_exit();
1217
1218 kvm_mips_gfn_to_pfn = NULL;
1219 kvm_mips_release_pfn_clean = NULL;
1220 kvm_mips_is_error_pfn = NULL;
1221
1222 pr_info("KVM/MIPS unloaded\n");
1223}
1224
1225module_init(kvm_mips_init);
1226module_exit(kvm_mips_exit);
1227
1228EXPORT_TRACEPOINT_SYMBOL(kvm_exit);