blob: d687c6e3258d76cdbc044066b54174b40b30e343 [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
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070024#include "interrupt.h"
25#include "commpage.h"
Sanjay Lal669e8462012-11-21 18:34:02 -080026
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{
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700100 *(int *)rtn = 0;
Sanjay Lal669e8462012-11-21 18:34:02 -0800101}
102
103static void kvm_mips_init_tlbs(struct kvm *kvm)
104{
105 unsigned long wired;
106
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700107 /*
108 * Add a wired entry to the TLB, it is used to map the commpage to
109 * the Guest kernel
110 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800111 wired = read_c0_wired();
112 write_c0_wired(wired + 1);
113 mtc0_tlbw_hazard();
114 kvm->arch.commpage_tlb = wired;
115
116 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
117 kvm->arch.commpage_tlb);
118}
119
120static void kvm_mips_init_vm_percpu(void *arg)
121{
122 struct kvm *kvm = (struct kvm *)arg;
123
124 kvm_mips_init_tlbs(kvm);
125 kvm_mips_callbacks->vm_init(kvm);
126
127}
128
129int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
130{
131 if (atomic_inc_return(&kvm_mips_instance) == 1) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100132 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
133 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800134 on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
135 }
136
Sanjay Lal669e8462012-11-21 18:34:02 -0800137 return 0;
138}
139
140void kvm_mips_free_vcpus(struct kvm *kvm)
141{
142 unsigned int i;
143 struct kvm_vcpu *vcpu;
144
145 /* Put the pages we reserved for the guest pmap */
146 for (i = 0; i < kvm->arch.guest_pmap_npages; i++) {
147 if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE)
148 kvm_mips_release_pfn_clean(kvm->arch.guest_pmap[i]);
149 }
James Hoganc6c0a662014-05-29 10:16:44 +0100150 kfree(kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800151
152 kvm_for_each_vcpu(i, vcpu, kvm) {
153 kvm_arch_vcpu_free(vcpu);
154 }
155
156 mutex_lock(&kvm->lock);
157
158 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
159 kvm->vcpus[i] = NULL;
160
161 atomic_set(&kvm->online_vcpus, 0);
162
163 mutex_unlock(&kvm->lock);
164}
165
166void kvm_arch_sync_events(struct kvm *kvm)
167{
168}
169
170static void kvm_mips_uninit_tlbs(void *arg)
171{
172 /* Restore wired count */
173 write_c0_wired(0);
174 mtc0_tlbw_hazard();
175 /* Clear out all the TLBs */
176 kvm_local_flush_tlb_all();
177}
178
179void kvm_arch_destroy_vm(struct kvm *kvm)
180{
181 kvm_mips_free_vcpus(kvm);
182
183 /* If this is the last instance, restore wired count */
184 if (atomic_dec_return(&kvm_mips_instance) == 0) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100185 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
186 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800187 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
188 }
189}
190
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700191long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
192 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800193{
David Daneyed829852013-05-23 09:49:10 -0700194 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800195}
196
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530197void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Sanjay Lal669e8462012-11-21 18:34:02 -0800198 struct kvm_memory_slot *dont)
199{
200}
201
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530202int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
203 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800204{
205 return 0;
206}
207
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +0900208void kvm_arch_memslots_updated(struct kvm *kvm)
209{
210}
211
Sanjay Lal669e8462012-11-21 18:34:02 -0800212int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700213 struct kvm_memory_slot *memslot,
214 struct kvm_userspace_memory_region *mem,
215 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800216{
217 return 0;
218}
219
220void kvm_arch_commit_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700221 struct kvm_userspace_memory_region *mem,
222 const struct kvm_memory_slot *old,
223 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800224{
225 unsigned long npages = 0;
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700226 int i;
Sanjay Lal669e8462012-11-21 18:34:02 -0800227
228 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
229 __func__, kvm, mem->slot, mem->guest_phys_addr,
230 mem->memory_size, mem->userspace_addr);
231
232 /* Setup Guest PMAP table */
233 if (!kvm->arch.guest_pmap) {
234 if (mem->slot == 0)
235 npages = mem->memory_size >> PAGE_SHIFT;
236
237 if (npages) {
238 kvm->arch.guest_pmap_npages = npages;
239 kvm->arch.guest_pmap =
240 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
241
242 if (!kvm->arch.guest_pmap) {
243 kvm_err("Failed to allocate guest PMAP");
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700244 return;
Sanjay Lal669e8462012-11-21 18:34:02 -0800245 }
246
James Hogan6e95bfd2014-05-29 10:16:43 +0100247 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
248 npages, kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800249
250 /* Now setup the page table */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700251 for (i = 0; i < npages; i++)
Sanjay Lal669e8462012-11-21 18:34:02 -0800252 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
Sanjay Lal669e8462012-11-21 18:34:02 -0800253 }
254 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800255}
256
257void kvm_arch_flush_shadow_all(struct kvm *kvm)
258{
259}
260
261void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
262 struct kvm_memory_slot *slot)
263{
264}
265
266void kvm_arch_flush_shadow(struct kvm *kvm)
267{
268}
269
270struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
271{
Sanjay Lal669e8462012-11-21 18:34:02 -0800272 int err, size, offset;
273 void *gebase;
274 int i;
275
276 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
277
278 if (!vcpu) {
279 err = -ENOMEM;
280 goto out;
281 }
282
283 err = kvm_vcpu_init(vcpu, kvm, id);
284
285 if (err)
286 goto out_free_cpu;
287
James Hogan6e95bfd2014-05-29 10:16:43 +0100288 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800289
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700290 /*
291 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800292 * guest mode exits
293 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700294 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800295 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700296 else
James Hogan7006e2d2014-05-29 10:16:23 +0100297 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800298
299 /* Save Linux EBASE */
300 vcpu->arch.host_ebase = (void *)read_c0_ebase();
301
302 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
303
304 if (!gebase) {
305 err = -ENOMEM;
306 goto out_free_cpu;
307 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100308 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
309 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800310
311 /* Save new ebase */
312 vcpu->arch.guest_ebase = gebase;
313
314 /* Copy L1 Guest Exception handler to correct offset */
315
316 /* TLB Refill, EXL = 0 */
317 memcpy(gebase, mips32_exception,
318 mips32_exceptionEnd - mips32_exception);
319
320 /* General Exception Entry point */
321 memcpy(gebase + 0x180, mips32_exception,
322 mips32_exceptionEnd - mips32_exception);
323
324 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
325 for (i = 0; i < 8; i++) {
326 kvm_debug("L1 Vectored handler @ %p\n",
327 gebase + 0x200 + (i * VECTORSPACING));
328 memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception,
329 mips32_exceptionEnd - mips32_exception);
330 }
331
332 /* General handler, relocate to unmapped space for sanity's sake */
333 offset = 0x2000;
James Hogan6e95bfd2014-05-29 10:16:43 +0100334 kvm_debug("Installing KVM Exception handlers @ %p, %#x bytes\n",
335 gebase + offset,
336 mips32_GuestExceptionEnd - mips32_GuestException);
Sanjay Lal669e8462012-11-21 18:34:02 -0800337
338 memcpy(gebase + offset, mips32_GuestException,
339 mips32_GuestExceptionEnd - mips32_GuestException);
340
341 /* Invalidate the icache for these ranges */
James Hoganfacaaec2014-05-29 10:16:25 +0100342 local_flush_icache_range((unsigned long)gebase,
343 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800344
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700345 /*
346 * Allocate comm page for guest kernel, a TLB will be reserved for
347 * mapping GVA @ 0xFFFF8000 to this page
348 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800349 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
350
351 if (!vcpu->arch.kseg0_commpage) {
352 err = -ENOMEM;
353 goto out_free_gebase;
354 }
355
James Hogan6e95bfd2014-05-29 10:16:43 +0100356 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800357 kvm_mips_commpage_init(vcpu);
358
359 /* Init */
360 vcpu->arch.last_sched_cpu = -1;
361
362 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100363 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800364
365 return vcpu;
366
367out_free_gebase:
368 kfree(gebase);
369
370out_free_cpu:
371 kfree(vcpu);
372
373out:
374 return ERR_PTR(err);
375}
376
377void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
378{
379 hrtimer_cancel(&vcpu->arch.comparecount_timer);
380
381 kvm_vcpu_uninit(vcpu);
382
383 kvm_mips_dump_stats(vcpu);
384
James Hoganc6c0a662014-05-29 10:16:44 +0100385 kfree(vcpu->arch.guest_ebase);
386 kfree(vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800387}
388
389void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
390{
391 kvm_arch_vcpu_free(vcpu);
392}
393
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700394int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
395 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800396{
David Daneyed829852013-05-23 09:49:10 -0700397 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800398}
399
400int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
401{
402 int r = 0;
403 sigset_t sigsaved;
404
405 if (vcpu->sigset_active)
406 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
407
408 if (vcpu->mmio_needed) {
409 if (!vcpu->mmio_is_write)
410 kvm_mips_complete_mmio_load(vcpu, run);
411 vcpu->mmio_needed = 0;
412 }
413
James Hogan044f0f02014-05-29 10:16:32 +0100414 local_irq_disable();
Sanjay Lal669e8462012-11-21 18:34:02 -0800415 /* Check if we have any exceptions/interrupts pending */
416 kvm_mips_deliver_interrupts(vcpu,
417 kvm_read_c0_guest_cause(vcpu->arch.cop0));
418
Sanjay Lal669e8462012-11-21 18:34:02 -0800419 kvm_guest_enter();
420
421 r = __kvm_mips_vcpu_run(run, vcpu);
422
423 kvm_guest_exit();
424 local_irq_enable();
425
426 if (vcpu->sigset_active)
427 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
428
429 return r;
430}
431
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700432int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
433 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800434{
435 int intr = (int)irq->irq;
436 struct kvm_vcpu *dvcpu = NULL;
437
438 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
439 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
440 (int)intr);
441
442 if (irq->cpu == -1)
443 dvcpu = vcpu;
444 else
445 dvcpu = vcpu->kvm->vcpus[irq->cpu];
446
447 if (intr == 2 || intr == 3 || intr == 4) {
448 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
449
450 } else if (intr == -2 || intr == -3 || intr == -4) {
451 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
452 } else {
453 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
454 irq->cpu, irq->irq);
455 return -EINVAL;
456 }
457
458 dvcpu->arch.wait = 0;
459
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700460 if (waitqueue_active(&dvcpu->wq))
Sanjay Lal669e8462012-11-21 18:34:02 -0800461 wake_up_interruptible(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800462
463 return 0;
464}
465
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700466int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
467 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800468{
David Daneyed829852013-05-23 09:49:10 -0700469 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800470}
471
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700472int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
473 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800474{
David Daneyed829852013-05-23 09:49:10 -0700475 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800476}
477
David Daney4c73fb22013-05-23 09:49:09 -0700478static u64 kvm_mips_get_one_regs[] = {
479 KVM_REG_MIPS_R0,
480 KVM_REG_MIPS_R1,
481 KVM_REG_MIPS_R2,
482 KVM_REG_MIPS_R3,
483 KVM_REG_MIPS_R4,
484 KVM_REG_MIPS_R5,
485 KVM_REG_MIPS_R6,
486 KVM_REG_MIPS_R7,
487 KVM_REG_MIPS_R8,
488 KVM_REG_MIPS_R9,
489 KVM_REG_MIPS_R10,
490 KVM_REG_MIPS_R11,
491 KVM_REG_MIPS_R12,
492 KVM_REG_MIPS_R13,
493 KVM_REG_MIPS_R14,
494 KVM_REG_MIPS_R15,
495 KVM_REG_MIPS_R16,
496 KVM_REG_MIPS_R17,
497 KVM_REG_MIPS_R18,
498 KVM_REG_MIPS_R19,
499 KVM_REG_MIPS_R20,
500 KVM_REG_MIPS_R21,
501 KVM_REG_MIPS_R22,
502 KVM_REG_MIPS_R23,
503 KVM_REG_MIPS_R24,
504 KVM_REG_MIPS_R25,
505 KVM_REG_MIPS_R26,
506 KVM_REG_MIPS_R27,
507 KVM_REG_MIPS_R28,
508 KVM_REG_MIPS_R29,
509 KVM_REG_MIPS_R30,
510 KVM_REG_MIPS_R31,
511
512 KVM_REG_MIPS_HI,
513 KVM_REG_MIPS_LO,
514 KVM_REG_MIPS_PC,
515
516 KVM_REG_MIPS_CP0_INDEX,
517 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100518 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700519 KVM_REG_MIPS_CP0_PAGEMASK,
520 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100521 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700522 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100523 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700524 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100525 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700526 KVM_REG_MIPS_CP0_STATUS,
527 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100528 KVM_REG_MIPS_CP0_EPC,
David Daney4c73fb22013-05-23 09:49:09 -0700529 KVM_REG_MIPS_CP0_CONFIG,
530 KVM_REG_MIPS_CP0_CONFIG1,
531 KVM_REG_MIPS_CP0_CONFIG2,
532 KVM_REG_MIPS_CP0_CONFIG3,
533 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100534 KVM_REG_MIPS_CP0_ERROREPC,
535
536 KVM_REG_MIPS_COUNT_CTL,
537 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100538 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700539};
540
541static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
542 const struct kvm_one_reg *reg)
543{
David Daney4c73fb22013-05-23 09:49:09 -0700544 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganf8be02d2014-05-29 10:16:29 +0100545 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700546 s64 v;
547
548 switch (reg->id) {
549 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
550 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
551 break;
552 case KVM_REG_MIPS_HI:
553 v = (long)vcpu->arch.hi;
554 break;
555 case KVM_REG_MIPS_LO:
556 v = (long)vcpu->arch.lo;
557 break;
558 case KVM_REG_MIPS_PC:
559 v = (long)vcpu->arch.pc;
560 break;
561
562 case KVM_REG_MIPS_CP0_INDEX:
563 v = (long)kvm_read_c0_guest_index(cop0);
564 break;
565 case KVM_REG_MIPS_CP0_CONTEXT:
566 v = (long)kvm_read_c0_guest_context(cop0);
567 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100568 case KVM_REG_MIPS_CP0_USERLOCAL:
569 v = (long)kvm_read_c0_guest_userlocal(cop0);
570 break;
David Daney4c73fb22013-05-23 09:49:09 -0700571 case KVM_REG_MIPS_CP0_PAGEMASK:
572 v = (long)kvm_read_c0_guest_pagemask(cop0);
573 break;
574 case KVM_REG_MIPS_CP0_WIRED:
575 v = (long)kvm_read_c0_guest_wired(cop0);
576 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100577 case KVM_REG_MIPS_CP0_HWRENA:
578 v = (long)kvm_read_c0_guest_hwrena(cop0);
579 break;
David Daney4c73fb22013-05-23 09:49:09 -0700580 case KVM_REG_MIPS_CP0_BADVADDR:
581 v = (long)kvm_read_c0_guest_badvaddr(cop0);
582 break;
583 case KVM_REG_MIPS_CP0_ENTRYHI:
584 v = (long)kvm_read_c0_guest_entryhi(cop0);
585 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100586 case KVM_REG_MIPS_CP0_COMPARE:
587 v = (long)kvm_read_c0_guest_compare(cop0);
588 break;
David Daney4c73fb22013-05-23 09:49:09 -0700589 case KVM_REG_MIPS_CP0_STATUS:
590 v = (long)kvm_read_c0_guest_status(cop0);
591 break;
592 case KVM_REG_MIPS_CP0_CAUSE:
593 v = (long)kvm_read_c0_guest_cause(cop0);
594 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100595 case KVM_REG_MIPS_CP0_EPC:
596 v = (long)kvm_read_c0_guest_epc(cop0);
597 break;
David Daney4c73fb22013-05-23 09:49:09 -0700598 case KVM_REG_MIPS_CP0_ERROREPC:
599 v = (long)kvm_read_c0_guest_errorepc(cop0);
600 break;
601 case KVM_REG_MIPS_CP0_CONFIG:
602 v = (long)kvm_read_c0_guest_config(cop0);
603 break;
604 case KVM_REG_MIPS_CP0_CONFIG1:
605 v = (long)kvm_read_c0_guest_config1(cop0);
606 break;
607 case KVM_REG_MIPS_CP0_CONFIG2:
608 v = (long)kvm_read_c0_guest_config2(cop0);
609 break;
610 case KVM_REG_MIPS_CP0_CONFIG3:
611 v = (long)kvm_read_c0_guest_config3(cop0);
612 break;
613 case KVM_REG_MIPS_CP0_CONFIG7:
614 v = (long)kvm_read_c0_guest_config7(cop0);
615 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100616 /* registers to be handled specially */
617 case KVM_REG_MIPS_CP0_COUNT:
James Hoganf8239342014-05-29 10:16:37 +0100618 case KVM_REG_MIPS_COUNT_CTL:
619 case KVM_REG_MIPS_COUNT_RESUME:
James Hoganf74a8e22014-05-29 10:16:38 +0100620 case KVM_REG_MIPS_COUNT_HZ:
James Hoganf8be02d2014-05-29 10:16:29 +0100621 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
622 if (ret)
623 return ret;
624 break;
David Daney4c73fb22013-05-23 09:49:09 -0700625 default:
626 return -EINVAL;
627 }
David Daney681865d2013-06-10 12:33:48 -0700628 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
629 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700630
David Daney681865d2013-06-10 12:33:48 -0700631 return put_user(v, uaddr64);
632 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
633 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
634 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700635
David Daney681865d2013-06-10 12:33:48 -0700636 return put_user(v32, uaddr32);
637 } else {
638 return -EINVAL;
639 }
David Daney4c73fb22013-05-23 09:49:09 -0700640}
641
642static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
643 const struct kvm_one_reg *reg)
644{
David Daney4c73fb22013-05-23 09:49:09 -0700645 struct mips_coproc *cop0 = vcpu->arch.cop0;
646 u64 v;
647
David Daney681865d2013-06-10 12:33:48 -0700648 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
649 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
650
651 if (get_user(v, uaddr64) != 0)
652 return -EFAULT;
653 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
654 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
655 s32 v32;
656
657 if (get_user(v32, uaddr32) != 0)
658 return -EFAULT;
659 v = (s64)v32;
660 } else {
661 return -EINVAL;
662 }
David Daney4c73fb22013-05-23 09:49:09 -0700663
664 switch (reg->id) {
665 case KVM_REG_MIPS_R0:
666 /* Silently ignore requests to set $0 */
667 break;
668 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
669 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
670 break;
671 case KVM_REG_MIPS_HI:
672 vcpu->arch.hi = v;
673 break;
674 case KVM_REG_MIPS_LO:
675 vcpu->arch.lo = v;
676 break;
677 case KVM_REG_MIPS_PC:
678 vcpu->arch.pc = v;
679 break;
680
681 case KVM_REG_MIPS_CP0_INDEX:
682 kvm_write_c0_guest_index(cop0, v);
683 break;
684 case KVM_REG_MIPS_CP0_CONTEXT:
685 kvm_write_c0_guest_context(cop0, v);
686 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100687 case KVM_REG_MIPS_CP0_USERLOCAL:
688 kvm_write_c0_guest_userlocal(cop0, v);
689 break;
David Daney4c73fb22013-05-23 09:49:09 -0700690 case KVM_REG_MIPS_CP0_PAGEMASK:
691 kvm_write_c0_guest_pagemask(cop0, v);
692 break;
693 case KVM_REG_MIPS_CP0_WIRED:
694 kvm_write_c0_guest_wired(cop0, v);
695 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100696 case KVM_REG_MIPS_CP0_HWRENA:
697 kvm_write_c0_guest_hwrena(cop0, v);
698 break;
David Daney4c73fb22013-05-23 09:49:09 -0700699 case KVM_REG_MIPS_CP0_BADVADDR:
700 kvm_write_c0_guest_badvaddr(cop0, v);
701 break;
702 case KVM_REG_MIPS_CP0_ENTRYHI:
703 kvm_write_c0_guest_entryhi(cop0, v);
704 break;
705 case KVM_REG_MIPS_CP0_STATUS:
706 kvm_write_c0_guest_status(cop0, v);
707 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100708 case KVM_REG_MIPS_CP0_EPC:
709 kvm_write_c0_guest_epc(cop0, v);
710 break;
David Daney4c73fb22013-05-23 09:49:09 -0700711 case KVM_REG_MIPS_CP0_ERROREPC:
712 kvm_write_c0_guest_errorepc(cop0, v);
713 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100714 /* registers to be handled specially */
715 case KVM_REG_MIPS_CP0_COUNT:
716 case KVM_REG_MIPS_CP0_COMPARE:
James Hogane30492b2014-05-29 10:16:35 +0100717 case KVM_REG_MIPS_CP0_CAUSE:
James Hoganf8239342014-05-29 10:16:37 +0100718 case KVM_REG_MIPS_COUNT_CTL:
719 case KVM_REG_MIPS_COUNT_RESUME:
James Hoganf74a8e22014-05-29 10:16:38 +0100720 case KVM_REG_MIPS_COUNT_HZ:
James Hoganf8be02d2014-05-29 10:16:29 +0100721 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700722 default:
723 return -EINVAL;
724 }
725 return 0;
726}
727
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700728long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
729 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800730{
731 struct kvm_vcpu *vcpu = filp->private_data;
732 void __user *argp = (void __user *)arg;
733 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800734
735 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -0700736 case KVM_SET_ONE_REG:
737 case KVM_GET_ONE_REG: {
738 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700739
David Daney4c73fb22013-05-23 09:49:09 -0700740 if (copy_from_user(&reg, argp, sizeof(reg)))
741 return -EFAULT;
742 if (ioctl == KVM_SET_ONE_REG)
743 return kvm_mips_set_reg(vcpu, &reg);
744 else
745 return kvm_mips_get_reg(vcpu, &reg);
746 }
747 case KVM_GET_REG_LIST: {
748 struct kvm_reg_list __user *user_list = argp;
749 u64 __user *reg_dest;
750 struct kvm_reg_list reg_list;
751 unsigned n;
752
753 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
754 return -EFAULT;
755 n = reg_list.n;
756 reg_list.n = ARRAY_SIZE(kvm_mips_get_one_regs);
757 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
758 return -EFAULT;
759 if (n < reg_list.n)
760 return -E2BIG;
761 reg_dest = user_list->reg;
762 if (copy_to_user(reg_dest, kvm_mips_get_one_regs,
763 sizeof(kvm_mips_get_one_regs)))
764 return -EFAULT;
765 return 0;
766 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800767 case KVM_NMI:
768 /* Treat the NMI as a CPU reset */
769 r = kvm_mips_reset_vcpu(vcpu);
770 break;
771 case KVM_INTERRUPT:
772 {
773 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700774
Sanjay Lal669e8462012-11-21 18:34:02 -0800775 r = -EFAULT;
776 if (copy_from_user(&irq, argp, sizeof(irq)))
777 goto out;
778
Sanjay Lal669e8462012-11-21 18:34:02 -0800779 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
780 irq.irq);
781
782 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
783 break;
784 }
785 default:
David Daney4c73fb22013-05-23 09:49:09 -0700786 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800787 }
788
789out:
790 return r;
791}
792
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700793/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -0800794int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
795{
796 struct kvm_memory_slot *memslot;
797 unsigned long ga, ga_end;
798 int is_dirty = 0;
799 int r;
800 unsigned long n;
801
802 mutex_lock(&kvm->slots_lock);
803
804 r = kvm_get_dirty_log(kvm, log, &is_dirty);
805 if (r)
806 goto out;
807
808 /* If nothing is dirty, don't bother messing with page tables. */
809 if (is_dirty) {
810 memslot = &kvm->memslots->memslots[log->slot];
811
812 ga = memslot->base_gfn << PAGE_SHIFT;
813 ga_end = ga + (memslot->npages << PAGE_SHIFT);
814
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700815 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
816 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -0800817
818 n = kvm_dirty_bitmap_bytes(memslot);
819 memset(memslot->dirty_bitmap, 0, n);
820 }
821
822 r = 0;
823out:
824 mutex_unlock(&kvm->slots_lock);
825 return r;
826
827}
828
829long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
830{
831 long r;
832
833 switch (ioctl) {
834 default:
David Daneyed829852013-05-23 09:49:10 -0700835 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800836 }
837
838 return r;
839}
840
841int kvm_arch_init(void *opaque)
842{
Sanjay Lal669e8462012-11-21 18:34:02 -0800843 if (kvm_mips_callbacks) {
844 kvm_err("kvm: module already exists\n");
845 return -EEXIST;
846 }
847
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700848 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -0800849}
850
851void kvm_arch_exit(void)
852{
853 kvm_mips_callbacks = NULL;
854}
855
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700856int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
857 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -0800858{
David Daneyed829852013-05-23 09:49:10 -0700859 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800860}
861
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700862int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
863 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -0800864{
David Daneyed829852013-05-23 09:49:10 -0700865 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800866}
867
868int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
869{
870 return 0;
871}
872
873int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
874{
David Daneyed829852013-05-23 09:49:10 -0700875 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800876}
877
878int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
879{
David Daneyed829852013-05-23 09:49:10 -0700880 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800881}
882
883int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
884{
885 return VM_FAULT_SIGBUS;
886}
887
888int kvm_dev_ioctl_check_extension(long ext)
889{
890 int r;
891
892 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -0700893 case KVM_CAP_ONE_REG:
894 r = 1;
895 break;
Sanjay Lal669e8462012-11-21 18:34:02 -0800896 case KVM_CAP_COALESCED_MMIO:
897 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
898 break;
899 default:
900 r = 0;
901 break;
902 }
903 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800904}
905
906int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
907{
908 return kvm_mips_pending_timer(vcpu);
909}
910
911int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
912{
913 int i;
914 struct mips_coproc *cop0;
915
916 if (!vcpu)
917 return -1;
918
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700919 kvm_debug("VCPU Register Dump:\n");
920 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
921 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -0800922
923 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700924 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -0800925 vcpu->arch.gprs[i],
926 vcpu->arch.gprs[i + 1],
927 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
928 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700929 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
930 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -0800931
932 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700933 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
934 kvm_read_c0_guest_status(cop0),
935 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -0800936
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700937 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -0800938
939 return 0;
940}
941
942int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
943{
944 int i;
945
David Daney8d17dd02013-05-23 09:49:08 -0700946 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -0700947 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -0700948 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -0800949 vcpu->arch.hi = regs->hi;
950 vcpu->arch.lo = regs->lo;
951 vcpu->arch.pc = regs->pc;
952
David Daney4c73fb22013-05-23 09:49:09 -0700953 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -0800954}
955
956int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
957{
958 int i;
959
David Daney8d17dd02013-05-23 09:49:08 -0700960 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -0700961 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -0800962
963 regs->hi = vcpu->arch.hi;
964 regs->lo = vcpu->arch.lo;
965 regs->pc = vcpu->arch.pc;
966
David Daney4c73fb22013-05-23 09:49:09 -0700967 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -0800968}
969
James Hogan0fae34f2014-05-29 10:16:39 +0100970static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -0800971{
972 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
973
974 kvm_mips_callbacks->queue_timer_int(vcpu);
975
976 vcpu->arch.wait = 0;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700977 if (waitqueue_active(&vcpu->wq))
Sanjay Lal669e8462012-11-21 18:34:02 -0800978 wake_up_interruptible(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800979}
980
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700981/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +0100982static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -0800983{
984 struct kvm_vcpu *vcpu;
985
986 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
987 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100988 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800989}
990
991int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
992{
993 kvm_mips_callbacks->vcpu_init(vcpu);
994 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
995 HRTIMER_MODE_REL);
996 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -0800997 return 0;
998}
999
1000void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1001{
Sanjay Lal669e8462012-11-21 18:34:02 -08001002}
1003
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001004int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1005 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001006{
1007 return 0;
1008}
1009
1010/* Initial guest state */
1011int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1012{
1013 return kvm_mips_callbacks->vcpu_setup(vcpu);
1014}
1015
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001016static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001017{
1018 uint32_t status = read_c0_status();
1019
1020 if (cpu_has_fpu)
1021 status |= (ST0_CU1);
1022
1023 if (cpu_has_dsp)
1024 status |= (ST0_MX);
1025
1026 write_c0_status(status);
1027 ehb();
1028}
1029
1030/*
1031 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1032 */
1033int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1034{
1035 uint32_t cause = vcpu->arch.host_cp0_cause;
1036 uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1037 uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
1038 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1039 enum emulation_result er = EMULATE_DONE;
1040 int ret = RESUME_GUEST;
1041
1042 /* Set a default exit reason */
1043 run->exit_reason = KVM_EXIT_UNKNOWN;
1044 run->ready_for_interrupt_injection = 1;
1045
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001046 /*
1047 * Set the appropriate status bits based on host CPU features,
1048 * before we hit the scheduler
1049 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001050 kvm_mips_set_c0_status();
1051
1052 local_irq_enable();
1053
1054 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1055 cause, opc, run, vcpu);
1056
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001057 /*
1058 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001059 * causing an exception to be delivered to the Guest Kernel
1060 */
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;
1068 }
1069
1070 switch (exccode) {
1071 case T_INT:
1072 kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc);
1073
1074 ++vcpu->stat.int_exits;
1075 trace_kvm_exit(vcpu, INT_EXITS);
1076
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001077 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001078 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001079
1080 ret = RESUME_GUEST;
1081 break;
1082
1083 case T_COP_UNUSABLE:
1084 kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc);
1085
1086 ++vcpu->stat.cop_unusable_exits;
1087 trace_kvm_exit(vcpu, COP_UNUSABLE_EXITS);
1088 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1089 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001090 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001091 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001092 break;
1093
1094 case T_TLB_MOD:
1095 ++vcpu->stat.tlbmod_exits;
1096 trace_kvm_exit(vcpu, TLBMOD_EXITS);
1097 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1098 break;
1099
1100 case T_TLB_ST_MISS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001101 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1102 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1103 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001104
1105 ++vcpu->stat.tlbmiss_st_exits;
1106 trace_kvm_exit(vcpu, TLBMISS_ST_EXITS);
1107 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1108 break;
1109
1110 case T_TLB_LD_MISS:
1111 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1112 cause, opc, badvaddr);
1113
1114 ++vcpu->stat.tlbmiss_ld_exits;
1115 trace_kvm_exit(vcpu, TLBMISS_LD_EXITS);
1116 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1117 break;
1118
1119 case T_ADDR_ERR_ST:
1120 ++vcpu->stat.addrerr_st_exits;
1121 trace_kvm_exit(vcpu, ADDRERR_ST_EXITS);
1122 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1123 break;
1124
1125 case T_ADDR_ERR_LD:
1126 ++vcpu->stat.addrerr_ld_exits;
1127 trace_kvm_exit(vcpu, ADDRERR_LD_EXITS);
1128 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1129 break;
1130
1131 case T_SYSCALL:
1132 ++vcpu->stat.syscall_exits;
1133 trace_kvm_exit(vcpu, SYSCALL_EXITS);
1134 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1135 break;
1136
1137 case T_RES_INST:
1138 ++vcpu->stat.resvd_inst_exits;
1139 trace_kvm_exit(vcpu, RESVD_INST_EXITS);
1140 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1141 break;
1142
1143 case T_BREAK:
1144 ++vcpu->stat.break_inst_exits;
1145 trace_kvm_exit(vcpu, BREAK_INST_EXITS);
1146 ret = kvm_mips_callbacks->handle_break(vcpu);
1147 break;
1148
1149 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001150 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1151 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1152 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001153 kvm_arch_vcpu_dump_regs(vcpu);
1154 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1155 ret = RESUME_HOST;
1156 break;
1157
1158 }
1159
1160skip_emul:
1161 local_irq_disable();
1162
1163 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1164 kvm_mips_deliver_interrupts(vcpu, cause);
1165
1166 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001167 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001168 if (signal_pending(current)) {
1169 run->exit_reason = KVM_EXIT_INTR;
1170 ret = (-EINTR << 2) | RESUME_HOST;
1171 ++vcpu->stat.signal_exits;
1172 trace_kvm_exit(vcpu, SIGNAL_EXITS);
1173 }
1174 }
1175
1176 return ret;
1177}
1178
1179int __init kvm_mips_init(void)
1180{
1181 int ret;
1182
1183 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1184
1185 if (ret)
1186 return ret;
1187
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001188 /*
1189 * On MIPS, kernel modules are executed from "mapped space", which
1190 * requires TLBs. The TLB handling code is statically linked with
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -07001191 * the rest of the kernel (tlb.c) to avoid the possibility of
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001192 * double faulting. The issue is that the TLB code references
1193 * routines that are part of the the KVM module, which are only
1194 * available once the module is loaded.
Sanjay Lal669e8462012-11-21 18:34:02 -08001195 */
1196 kvm_mips_gfn_to_pfn = gfn_to_pfn;
1197 kvm_mips_release_pfn_clean = kvm_release_pfn_clean;
1198 kvm_mips_is_error_pfn = is_error_pfn;
1199
1200 pr_info("KVM/MIPS Initialized\n");
1201 return 0;
1202}
1203
1204void __exit kvm_mips_exit(void)
1205{
1206 kvm_exit();
1207
1208 kvm_mips_gfn_to_pfn = NULL;
1209 kvm_mips_release_pfn_clean = NULL;
1210 kvm_mips_is_error_pfn = NULL;
1211
1212 pr_info("KVM/MIPS unloaded\n");
1213}
1214
1215module_init(kvm_mips_init);
1216module_exit(kvm_mips_exit);
1217
1218EXPORT_TRACEPOINT_SYMBOL(kvm_exit);