blob: fd620cc8a44c3f205cf108256cc323edeed9d246 [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>
James Hoganf7982172015-02-04 17:06:37 +000018#include <asm/fpu.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080019#include <asm/page.h>
20#include <asm/cacheflush.h>
21#include <asm/mmu_context.h>
James Hoganc4c6f2c2015-02-04 10:52:03 +000022#include <asm/pgtable.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080023
24#include <linux/kvm_host.h>
25
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070026#include "interrupt.h"
27#include "commpage.h"
Sanjay Lal669e8462012-11-21 18:34:02 -080028
29#define CREATE_TRACE_POINTS
30#include "trace.h"
31
32#ifndef VECTORSPACING
33#define VECTORSPACING 0x100 /* for EI/VI mode */
34#endif
35
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070036#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
Sanjay Lal669e8462012-11-21 18:34:02 -080037struct kvm_stats_debugfs_item debugfs_entries[] = {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070038 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
39 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
40 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
41 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
42 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
43 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
44 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
45 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
46 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
47 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
48 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
49 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
50 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
James Hogan0a560422015-02-06 16:03:57 +000051 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070052 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
Paolo Bonzinif7819512015-02-04 18:20:58 +010053 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070054 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
Sanjay Lal669e8462012-11-21 18:34:02 -080055 {NULL}
56};
57
58static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu)
59{
60 int i;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070061
Sanjay Lal669e8462012-11-21 18:34:02 -080062 for_each_possible_cpu(i) {
63 vcpu->arch.guest_kernel_asid[i] = 0;
64 vcpu->arch.guest_user_asid[i] = 0;
65 }
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070066
Sanjay Lal669e8462012-11-21 18:34:02 -080067 return 0;
68}
69
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070070/*
71 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
72 * Config7, so we are "runnable" if interrupts are pending
Sanjay Lal669e8462012-11-21 18:34:02 -080073 */
74int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
75{
76 return !!(vcpu->arch.pending_exceptions);
77}
78
79int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
80{
81 return 1;
82}
83
Radim Krčmář13a34e02014-08-28 15:13:03 +020084int kvm_arch_hardware_enable(void)
Sanjay Lal669e8462012-11-21 18:34:02 -080085{
86 return 0;
87}
88
Sanjay Lal669e8462012-11-21 18:34:02 -080089int kvm_arch_hardware_setup(void)
90{
91 return 0;
92}
93
Sanjay Lal669e8462012-11-21 18:34:02 -080094void kvm_arch_check_processor_compat(void *rtn)
95{
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -070096 *(int *)rtn = 0;
Sanjay Lal669e8462012-11-21 18:34:02 -080097}
98
99static void kvm_mips_init_tlbs(struct kvm *kvm)
100{
101 unsigned long wired;
102
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700103 /*
104 * Add a wired entry to the TLB, it is used to map the commpage to
105 * the Guest kernel
106 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800107 wired = read_c0_wired();
108 write_c0_wired(wired + 1);
109 mtc0_tlbw_hazard();
110 kvm->arch.commpage_tlb = wired;
111
112 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
113 kvm->arch.commpage_tlb);
114}
115
116static void kvm_mips_init_vm_percpu(void *arg)
117{
118 struct kvm *kvm = (struct kvm *)arg;
119
120 kvm_mips_init_tlbs(kvm);
121 kvm_mips_callbacks->vm_init(kvm);
122
123}
124
125int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
126{
127 if (atomic_inc_return(&kvm_mips_instance) == 1) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100128 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
129 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800130 on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
131 }
132
Sanjay Lal669e8462012-11-21 18:34:02 -0800133 return 0;
134}
135
136void kvm_mips_free_vcpus(struct kvm *kvm)
137{
138 unsigned int i;
139 struct kvm_vcpu *vcpu;
140
141 /* Put the pages we reserved for the guest pmap */
142 for (i = 0; i < kvm->arch.guest_pmap_npages; i++) {
143 if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE)
144 kvm_mips_release_pfn_clean(kvm->arch.guest_pmap[i]);
145 }
James Hoganc6c0a662014-05-29 10:16:44 +0100146 kfree(kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800147
148 kvm_for_each_vcpu(i, vcpu, kvm) {
149 kvm_arch_vcpu_free(vcpu);
150 }
151
152 mutex_lock(&kvm->lock);
153
154 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
155 kvm->vcpus[i] = NULL;
156
157 atomic_set(&kvm->online_vcpus, 0);
158
159 mutex_unlock(&kvm->lock);
160}
161
Sanjay Lal669e8462012-11-21 18:34:02 -0800162static void kvm_mips_uninit_tlbs(void *arg)
163{
164 /* Restore wired count */
165 write_c0_wired(0);
166 mtc0_tlbw_hazard();
167 /* Clear out all the TLBs */
168 kvm_local_flush_tlb_all();
169}
170
171void kvm_arch_destroy_vm(struct kvm *kvm)
172{
173 kvm_mips_free_vcpus(kvm);
174
175 /* If this is the last instance, restore wired count */
176 if (atomic_dec_return(&kvm_mips_instance) == 0) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100177 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
178 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800179 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
180 }
181}
182
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700183long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
184 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800185{
David Daneyed829852013-05-23 09:49:10 -0700186 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800187}
188
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530189int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
190 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800191{
192 return 0;
193}
194
195int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700196 struct kvm_memory_slot *memslot,
197 struct kvm_userspace_memory_region *mem,
198 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800199{
200 return 0;
201}
202
203void kvm_arch_commit_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700204 struct kvm_userspace_memory_region *mem,
205 const struct kvm_memory_slot *old,
206 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800207{
208 unsigned long npages = 0;
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700209 int i;
Sanjay Lal669e8462012-11-21 18:34:02 -0800210
211 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
212 __func__, kvm, mem->slot, mem->guest_phys_addr,
213 mem->memory_size, mem->userspace_addr);
214
215 /* Setup Guest PMAP table */
216 if (!kvm->arch.guest_pmap) {
217 if (mem->slot == 0)
218 npages = mem->memory_size >> PAGE_SHIFT;
219
220 if (npages) {
221 kvm->arch.guest_pmap_npages = npages;
222 kvm->arch.guest_pmap =
223 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
224
225 if (!kvm->arch.guest_pmap) {
226 kvm_err("Failed to allocate guest PMAP");
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700227 return;
Sanjay Lal669e8462012-11-21 18:34:02 -0800228 }
229
James Hogan6e95bfd2014-05-29 10:16:43 +0100230 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
231 npages, kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800232
233 /* Now setup the page table */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700234 for (i = 0; i < npages; i++)
Sanjay Lal669e8462012-11-21 18:34:02 -0800235 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
Sanjay Lal669e8462012-11-21 18:34:02 -0800236 }
237 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800238}
239
Sanjay Lal669e8462012-11-21 18:34:02 -0800240struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
241{
Sanjay Lal669e8462012-11-21 18:34:02 -0800242 int err, size, offset;
243 void *gebase;
244 int i;
245
246 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
247
248 if (!vcpu) {
249 err = -ENOMEM;
250 goto out;
251 }
252
253 err = kvm_vcpu_init(vcpu, kvm, id);
254
255 if (err)
256 goto out_free_cpu;
257
James Hogan6e95bfd2014-05-29 10:16:43 +0100258 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800259
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700260 /*
261 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800262 * guest mode exits
263 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700264 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800265 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700266 else
James Hogan7006e2d2014-05-29 10:16:23 +0100267 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800268
269 /* Save Linux EBASE */
270 vcpu->arch.host_ebase = (void *)read_c0_ebase();
271
272 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
273
274 if (!gebase) {
275 err = -ENOMEM;
276 goto out_free_cpu;
277 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100278 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
279 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800280
281 /* Save new ebase */
282 vcpu->arch.guest_ebase = gebase;
283
284 /* Copy L1 Guest Exception handler to correct offset */
285
286 /* TLB Refill, EXL = 0 */
287 memcpy(gebase, mips32_exception,
288 mips32_exceptionEnd - mips32_exception);
289
290 /* General Exception Entry point */
291 memcpy(gebase + 0x180, mips32_exception,
292 mips32_exceptionEnd - mips32_exception);
293
294 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
295 for (i = 0; i < 8; i++) {
296 kvm_debug("L1 Vectored handler @ %p\n",
297 gebase + 0x200 + (i * VECTORSPACING));
298 memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception,
299 mips32_exceptionEnd - mips32_exception);
300 }
301
302 /* General handler, relocate to unmapped space for sanity's sake */
303 offset = 0x2000;
James Hogan6e95bfd2014-05-29 10:16:43 +0100304 kvm_debug("Installing KVM Exception handlers @ %p, %#x bytes\n",
305 gebase + offset,
306 mips32_GuestExceptionEnd - mips32_GuestException);
Sanjay Lal669e8462012-11-21 18:34:02 -0800307
308 memcpy(gebase + offset, mips32_GuestException,
309 mips32_GuestExceptionEnd - mips32_GuestException);
310
311 /* Invalidate the icache for these ranges */
James Hoganfacaaec2014-05-29 10:16:25 +0100312 local_flush_icache_range((unsigned long)gebase,
313 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800314
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700315 /*
316 * Allocate comm page for guest kernel, a TLB will be reserved for
317 * mapping GVA @ 0xFFFF8000 to this page
318 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800319 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
320
321 if (!vcpu->arch.kseg0_commpage) {
322 err = -ENOMEM;
323 goto out_free_gebase;
324 }
325
James Hogan6e95bfd2014-05-29 10:16:43 +0100326 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800327 kvm_mips_commpage_init(vcpu);
328
329 /* Init */
330 vcpu->arch.last_sched_cpu = -1;
331
332 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100333 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800334
335 return vcpu;
336
337out_free_gebase:
338 kfree(gebase);
339
340out_free_cpu:
341 kfree(vcpu);
342
343out:
344 return ERR_PTR(err);
345}
346
347void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
348{
349 hrtimer_cancel(&vcpu->arch.comparecount_timer);
350
351 kvm_vcpu_uninit(vcpu);
352
353 kvm_mips_dump_stats(vcpu);
354
James Hoganc6c0a662014-05-29 10:16:44 +0100355 kfree(vcpu->arch.guest_ebase);
356 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700357 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800358}
359
360void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
361{
362 kvm_arch_vcpu_free(vcpu);
363}
364
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700365int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
366 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800367{
David Daneyed829852013-05-23 09:49:10 -0700368 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800369}
370
371int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
372{
373 int r = 0;
374 sigset_t sigsaved;
375
376 if (vcpu->sigset_active)
377 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
378
379 if (vcpu->mmio_needed) {
380 if (!vcpu->mmio_is_write)
381 kvm_mips_complete_mmio_load(vcpu, run);
382 vcpu->mmio_needed = 0;
383 }
384
James Hoganf7982172015-02-04 17:06:37 +0000385 lose_fpu(1);
386
James Hogan044f0f02014-05-29 10:16:32 +0100387 local_irq_disable();
Sanjay Lal669e8462012-11-21 18:34:02 -0800388 /* Check if we have any exceptions/interrupts pending */
389 kvm_mips_deliver_interrupts(vcpu,
390 kvm_read_c0_guest_cause(vcpu->arch.cop0));
391
Sanjay Lal669e8462012-11-21 18:34:02 -0800392 kvm_guest_enter();
393
James Hoganc4c6f2c2015-02-04 10:52:03 +0000394 /* Disable hardware page table walking while in guest */
395 htw_stop();
396
Sanjay Lal669e8462012-11-21 18:34:02 -0800397 r = __kvm_mips_vcpu_run(run, vcpu);
398
James Hoganc4c6f2c2015-02-04 10:52:03 +0000399 /* Re-enable HTW before enabling interrupts */
400 htw_start();
401
Sanjay Lal669e8462012-11-21 18:34:02 -0800402 kvm_guest_exit();
403 local_irq_enable();
404
405 if (vcpu->sigset_active)
406 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
407
408 return r;
409}
410
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700411int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
412 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800413{
414 int intr = (int)irq->irq;
415 struct kvm_vcpu *dvcpu = NULL;
416
417 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
418 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
419 (int)intr);
420
421 if (irq->cpu == -1)
422 dvcpu = vcpu;
423 else
424 dvcpu = vcpu->kvm->vcpus[irq->cpu];
425
426 if (intr == 2 || intr == 3 || intr == 4) {
427 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
428
429 } else if (intr == -2 || intr == -3 || intr == -4) {
430 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
431 } else {
432 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
433 irq->cpu, irq->irq);
434 return -EINVAL;
435 }
436
437 dvcpu->arch.wait = 0;
438
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700439 if (waitqueue_active(&dvcpu->wq))
Sanjay Lal669e8462012-11-21 18:34:02 -0800440 wake_up_interruptible(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800441
442 return 0;
443}
444
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700445int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
446 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800447{
David Daneyed829852013-05-23 09:49:10 -0700448 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800449}
450
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700451int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
452 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800453{
David Daneyed829852013-05-23 09:49:10 -0700454 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800455}
456
David Daney4c73fb22013-05-23 09:49:09 -0700457static u64 kvm_mips_get_one_regs[] = {
458 KVM_REG_MIPS_R0,
459 KVM_REG_MIPS_R1,
460 KVM_REG_MIPS_R2,
461 KVM_REG_MIPS_R3,
462 KVM_REG_MIPS_R4,
463 KVM_REG_MIPS_R5,
464 KVM_REG_MIPS_R6,
465 KVM_REG_MIPS_R7,
466 KVM_REG_MIPS_R8,
467 KVM_REG_MIPS_R9,
468 KVM_REG_MIPS_R10,
469 KVM_REG_MIPS_R11,
470 KVM_REG_MIPS_R12,
471 KVM_REG_MIPS_R13,
472 KVM_REG_MIPS_R14,
473 KVM_REG_MIPS_R15,
474 KVM_REG_MIPS_R16,
475 KVM_REG_MIPS_R17,
476 KVM_REG_MIPS_R18,
477 KVM_REG_MIPS_R19,
478 KVM_REG_MIPS_R20,
479 KVM_REG_MIPS_R21,
480 KVM_REG_MIPS_R22,
481 KVM_REG_MIPS_R23,
482 KVM_REG_MIPS_R24,
483 KVM_REG_MIPS_R25,
484 KVM_REG_MIPS_R26,
485 KVM_REG_MIPS_R27,
486 KVM_REG_MIPS_R28,
487 KVM_REG_MIPS_R29,
488 KVM_REG_MIPS_R30,
489 KVM_REG_MIPS_R31,
490
491 KVM_REG_MIPS_HI,
492 KVM_REG_MIPS_LO,
493 KVM_REG_MIPS_PC,
494
495 KVM_REG_MIPS_CP0_INDEX,
496 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100497 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700498 KVM_REG_MIPS_CP0_PAGEMASK,
499 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100500 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700501 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100502 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700503 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100504 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700505 KVM_REG_MIPS_CP0_STATUS,
506 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100507 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100508 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700509 KVM_REG_MIPS_CP0_CONFIG,
510 KVM_REG_MIPS_CP0_CONFIG1,
511 KVM_REG_MIPS_CP0_CONFIG2,
512 KVM_REG_MIPS_CP0_CONFIG3,
513 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100514 KVM_REG_MIPS_CP0_ERROREPC,
515
516 KVM_REG_MIPS_COUNT_CTL,
517 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100518 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700519};
520
521static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
522 const struct kvm_one_reg *reg)
523{
David Daney4c73fb22013-05-23 09:49:09 -0700524 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganf8be02d2014-05-29 10:16:29 +0100525 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700526 s64 v;
527
528 switch (reg->id) {
529 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
530 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
531 break;
532 case KVM_REG_MIPS_HI:
533 v = (long)vcpu->arch.hi;
534 break;
535 case KVM_REG_MIPS_LO:
536 v = (long)vcpu->arch.lo;
537 break;
538 case KVM_REG_MIPS_PC:
539 v = (long)vcpu->arch.pc;
540 break;
541
542 case KVM_REG_MIPS_CP0_INDEX:
543 v = (long)kvm_read_c0_guest_index(cop0);
544 break;
545 case KVM_REG_MIPS_CP0_CONTEXT:
546 v = (long)kvm_read_c0_guest_context(cop0);
547 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100548 case KVM_REG_MIPS_CP0_USERLOCAL:
549 v = (long)kvm_read_c0_guest_userlocal(cop0);
550 break;
David Daney4c73fb22013-05-23 09:49:09 -0700551 case KVM_REG_MIPS_CP0_PAGEMASK:
552 v = (long)kvm_read_c0_guest_pagemask(cop0);
553 break;
554 case KVM_REG_MIPS_CP0_WIRED:
555 v = (long)kvm_read_c0_guest_wired(cop0);
556 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100557 case KVM_REG_MIPS_CP0_HWRENA:
558 v = (long)kvm_read_c0_guest_hwrena(cop0);
559 break;
David Daney4c73fb22013-05-23 09:49:09 -0700560 case KVM_REG_MIPS_CP0_BADVADDR:
561 v = (long)kvm_read_c0_guest_badvaddr(cop0);
562 break;
563 case KVM_REG_MIPS_CP0_ENTRYHI:
564 v = (long)kvm_read_c0_guest_entryhi(cop0);
565 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100566 case KVM_REG_MIPS_CP0_COMPARE:
567 v = (long)kvm_read_c0_guest_compare(cop0);
568 break;
David Daney4c73fb22013-05-23 09:49:09 -0700569 case KVM_REG_MIPS_CP0_STATUS:
570 v = (long)kvm_read_c0_guest_status(cop0);
571 break;
572 case KVM_REG_MIPS_CP0_CAUSE:
573 v = (long)kvm_read_c0_guest_cause(cop0);
574 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100575 case KVM_REG_MIPS_CP0_EPC:
576 v = (long)kvm_read_c0_guest_epc(cop0);
577 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100578 case KVM_REG_MIPS_CP0_PRID:
579 v = (long)kvm_read_c0_guest_prid(cop0);
580 break;
David Daney4c73fb22013-05-23 09:49:09 -0700581 case KVM_REG_MIPS_CP0_ERROREPC:
582 v = (long)kvm_read_c0_guest_errorepc(cop0);
583 break;
584 case KVM_REG_MIPS_CP0_CONFIG:
585 v = (long)kvm_read_c0_guest_config(cop0);
586 break;
587 case KVM_REG_MIPS_CP0_CONFIG1:
588 v = (long)kvm_read_c0_guest_config1(cop0);
589 break;
590 case KVM_REG_MIPS_CP0_CONFIG2:
591 v = (long)kvm_read_c0_guest_config2(cop0);
592 break;
593 case KVM_REG_MIPS_CP0_CONFIG3:
594 v = (long)kvm_read_c0_guest_config3(cop0);
595 break;
596 case KVM_REG_MIPS_CP0_CONFIG7:
597 v = (long)kvm_read_c0_guest_config7(cop0);
598 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100599 /* registers to be handled specially */
600 case KVM_REG_MIPS_CP0_COUNT:
James Hoganf8239342014-05-29 10:16:37 +0100601 case KVM_REG_MIPS_COUNT_CTL:
602 case KVM_REG_MIPS_COUNT_RESUME:
James Hoganf74a8e22014-05-29 10:16:38 +0100603 case KVM_REG_MIPS_COUNT_HZ:
James Hoganf8be02d2014-05-29 10:16:29 +0100604 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
605 if (ret)
606 return ret;
607 break;
David Daney4c73fb22013-05-23 09:49:09 -0700608 default:
609 return -EINVAL;
610 }
David Daney681865d2013-06-10 12:33:48 -0700611 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
612 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700613
David Daney681865d2013-06-10 12:33:48 -0700614 return put_user(v, uaddr64);
615 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
616 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
617 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700618
David Daney681865d2013-06-10 12:33:48 -0700619 return put_user(v32, uaddr32);
620 } else {
621 return -EINVAL;
622 }
David Daney4c73fb22013-05-23 09:49:09 -0700623}
624
625static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
626 const struct kvm_one_reg *reg)
627{
David Daney4c73fb22013-05-23 09:49:09 -0700628 struct mips_coproc *cop0 = vcpu->arch.cop0;
629 u64 v;
630
David Daney681865d2013-06-10 12:33:48 -0700631 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
632 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
633
634 if (get_user(v, uaddr64) != 0)
635 return -EFAULT;
636 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
637 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
638 s32 v32;
639
640 if (get_user(v32, uaddr32) != 0)
641 return -EFAULT;
642 v = (s64)v32;
643 } else {
644 return -EINVAL;
645 }
David Daney4c73fb22013-05-23 09:49:09 -0700646
647 switch (reg->id) {
648 case KVM_REG_MIPS_R0:
649 /* Silently ignore requests to set $0 */
650 break;
651 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
652 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
653 break;
654 case KVM_REG_MIPS_HI:
655 vcpu->arch.hi = v;
656 break;
657 case KVM_REG_MIPS_LO:
658 vcpu->arch.lo = v;
659 break;
660 case KVM_REG_MIPS_PC:
661 vcpu->arch.pc = v;
662 break;
663
664 case KVM_REG_MIPS_CP0_INDEX:
665 kvm_write_c0_guest_index(cop0, v);
666 break;
667 case KVM_REG_MIPS_CP0_CONTEXT:
668 kvm_write_c0_guest_context(cop0, v);
669 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100670 case KVM_REG_MIPS_CP0_USERLOCAL:
671 kvm_write_c0_guest_userlocal(cop0, v);
672 break;
David Daney4c73fb22013-05-23 09:49:09 -0700673 case KVM_REG_MIPS_CP0_PAGEMASK:
674 kvm_write_c0_guest_pagemask(cop0, v);
675 break;
676 case KVM_REG_MIPS_CP0_WIRED:
677 kvm_write_c0_guest_wired(cop0, v);
678 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100679 case KVM_REG_MIPS_CP0_HWRENA:
680 kvm_write_c0_guest_hwrena(cop0, v);
681 break;
David Daney4c73fb22013-05-23 09:49:09 -0700682 case KVM_REG_MIPS_CP0_BADVADDR:
683 kvm_write_c0_guest_badvaddr(cop0, v);
684 break;
685 case KVM_REG_MIPS_CP0_ENTRYHI:
686 kvm_write_c0_guest_entryhi(cop0, v);
687 break;
688 case KVM_REG_MIPS_CP0_STATUS:
689 kvm_write_c0_guest_status(cop0, v);
690 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100691 case KVM_REG_MIPS_CP0_EPC:
692 kvm_write_c0_guest_epc(cop0, v);
693 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100694 case KVM_REG_MIPS_CP0_PRID:
695 kvm_write_c0_guest_prid(cop0, v);
696 break;
David Daney4c73fb22013-05-23 09:49:09 -0700697 case KVM_REG_MIPS_CP0_ERROREPC:
698 kvm_write_c0_guest_errorepc(cop0, v);
699 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100700 /* registers to be handled specially */
701 case KVM_REG_MIPS_CP0_COUNT:
702 case KVM_REG_MIPS_CP0_COMPARE:
James Hogane30492b2014-05-29 10:16:35 +0100703 case KVM_REG_MIPS_CP0_CAUSE:
James Hoganf8239342014-05-29 10:16:37 +0100704 case KVM_REG_MIPS_COUNT_CTL:
705 case KVM_REG_MIPS_COUNT_RESUME:
James Hoganf74a8e22014-05-29 10:16:38 +0100706 case KVM_REG_MIPS_COUNT_HZ:
James Hoganf8be02d2014-05-29 10:16:29 +0100707 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700708 default:
709 return -EINVAL;
710 }
711 return 0;
712}
713
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700714long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
715 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800716{
717 struct kvm_vcpu *vcpu = filp->private_data;
718 void __user *argp = (void __user *)arg;
719 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800720
721 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -0700722 case KVM_SET_ONE_REG:
723 case KVM_GET_ONE_REG: {
724 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700725
David Daney4c73fb22013-05-23 09:49:09 -0700726 if (copy_from_user(&reg, argp, sizeof(reg)))
727 return -EFAULT;
728 if (ioctl == KVM_SET_ONE_REG)
729 return kvm_mips_set_reg(vcpu, &reg);
730 else
731 return kvm_mips_get_reg(vcpu, &reg);
732 }
733 case KVM_GET_REG_LIST: {
734 struct kvm_reg_list __user *user_list = argp;
735 u64 __user *reg_dest;
736 struct kvm_reg_list reg_list;
737 unsigned n;
738
739 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
740 return -EFAULT;
741 n = reg_list.n;
742 reg_list.n = ARRAY_SIZE(kvm_mips_get_one_regs);
743 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
744 return -EFAULT;
745 if (n < reg_list.n)
746 return -E2BIG;
747 reg_dest = user_list->reg;
748 if (copy_to_user(reg_dest, kvm_mips_get_one_regs,
749 sizeof(kvm_mips_get_one_regs)))
750 return -EFAULT;
751 return 0;
752 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800753 case KVM_NMI:
754 /* Treat the NMI as a CPU reset */
755 r = kvm_mips_reset_vcpu(vcpu);
756 break;
757 case KVM_INTERRUPT:
758 {
759 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700760
Sanjay Lal669e8462012-11-21 18:34:02 -0800761 r = -EFAULT;
762 if (copy_from_user(&irq, argp, sizeof(irq)))
763 goto out;
764
Sanjay Lal669e8462012-11-21 18:34:02 -0800765 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
766 irq.irq);
767
768 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
769 break;
770 }
771 default:
David Daney4c73fb22013-05-23 09:49:09 -0700772 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800773 }
774
775out:
776 return r;
777}
778
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700779/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -0800780int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
781{
782 struct kvm_memory_slot *memslot;
783 unsigned long ga, ga_end;
784 int is_dirty = 0;
785 int r;
786 unsigned long n;
787
788 mutex_lock(&kvm->slots_lock);
789
790 r = kvm_get_dirty_log(kvm, log, &is_dirty);
791 if (r)
792 goto out;
793
794 /* If nothing is dirty, don't bother messing with page tables. */
795 if (is_dirty) {
796 memslot = &kvm->memslots->memslots[log->slot];
797
798 ga = memslot->base_gfn << PAGE_SHIFT;
799 ga_end = ga + (memslot->npages << PAGE_SHIFT);
800
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700801 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
802 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -0800803
804 n = kvm_dirty_bitmap_bytes(memslot);
805 memset(memslot->dirty_bitmap, 0, n);
806 }
807
808 r = 0;
809out:
810 mutex_unlock(&kvm->slots_lock);
811 return r;
812
813}
814
815long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
816{
817 long r;
818
819 switch (ioctl) {
820 default:
David Daneyed829852013-05-23 09:49:10 -0700821 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800822 }
823
824 return r;
825}
826
827int kvm_arch_init(void *opaque)
828{
Sanjay Lal669e8462012-11-21 18:34:02 -0800829 if (kvm_mips_callbacks) {
830 kvm_err("kvm: module already exists\n");
831 return -EEXIST;
832 }
833
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700834 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -0800835}
836
837void kvm_arch_exit(void)
838{
839 kvm_mips_callbacks = NULL;
840}
841
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700842int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
843 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -0800844{
David Daneyed829852013-05-23 09:49:10 -0700845 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800846}
847
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700848int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
849 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -0800850{
David Daneyed829852013-05-23 09:49:10 -0700851 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800852}
853
Dominik Dingel31928aa2014-12-04 15:47:07 +0100854void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -0800855{
Sanjay Lal669e8462012-11-21 18:34:02 -0800856}
857
858int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
859{
David Daneyed829852013-05-23 09:49:10 -0700860 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800861}
862
863int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
864{
David Daneyed829852013-05-23 09:49:10 -0700865 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800866}
867
868int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
869{
870 return VM_FAULT_SIGBUS;
871}
872
Alexander Graf784aa3d2014-07-14 18:27:35 +0200873int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -0800874{
875 int r;
876
877 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -0700878 case KVM_CAP_ONE_REG:
879 r = 1;
880 break;
Sanjay Lal669e8462012-11-21 18:34:02 -0800881 case KVM_CAP_COALESCED_MMIO:
882 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
883 break;
884 default:
885 r = 0;
886 break;
887 }
888 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800889}
890
891int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
892{
893 return kvm_mips_pending_timer(vcpu);
894}
895
896int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
897{
898 int i;
899 struct mips_coproc *cop0;
900
901 if (!vcpu)
902 return -1;
903
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700904 kvm_debug("VCPU Register Dump:\n");
905 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
906 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -0800907
908 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700909 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -0800910 vcpu->arch.gprs[i],
911 vcpu->arch.gprs[i + 1],
912 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
913 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700914 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
915 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -0800916
917 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700918 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
919 kvm_read_c0_guest_status(cop0),
920 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -0800921
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700922 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -0800923
924 return 0;
925}
926
927int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
928{
929 int i;
930
David Daney8d17dd02013-05-23 09:49:08 -0700931 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -0700932 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -0700933 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -0800934 vcpu->arch.hi = regs->hi;
935 vcpu->arch.lo = regs->lo;
936 vcpu->arch.pc = regs->pc;
937
David Daney4c73fb22013-05-23 09:49:09 -0700938 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -0800939}
940
941int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
942{
943 int i;
944
David Daney8d17dd02013-05-23 09:49:08 -0700945 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -0700946 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -0800947
948 regs->hi = vcpu->arch.hi;
949 regs->lo = vcpu->arch.lo;
950 regs->pc = vcpu->arch.pc;
951
David Daney4c73fb22013-05-23 09:49:09 -0700952 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -0800953}
954
James Hogan0fae34f2014-05-29 10:16:39 +0100955static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -0800956{
957 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
958
959 kvm_mips_callbacks->queue_timer_int(vcpu);
960
961 vcpu->arch.wait = 0;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700962 if (waitqueue_active(&vcpu->wq))
Sanjay Lal669e8462012-11-21 18:34:02 -0800963 wake_up_interruptible(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800964}
965
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700966/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +0100967static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -0800968{
969 struct kvm_vcpu *vcpu;
970
971 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
972 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +0100973 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800974}
975
976int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
977{
978 kvm_mips_callbacks->vcpu_init(vcpu);
979 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
980 HRTIMER_MODE_REL);
981 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -0800982 return 0;
983}
984
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700985int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
986 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -0800987{
988 return 0;
989}
990
991/* Initial guest state */
992int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
993{
994 return kvm_mips_callbacks->vcpu_setup(vcpu);
995}
996
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700997static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -0800998{
999 uint32_t status = read_c0_status();
1000
Sanjay Lal669e8462012-11-21 18:34:02 -08001001 if (cpu_has_dsp)
1002 status |= (ST0_MX);
1003
1004 write_c0_status(status);
1005 ehb();
1006}
1007
1008/*
1009 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1010 */
1011int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1012{
1013 uint32_t cause = vcpu->arch.host_cp0_cause;
1014 uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1015 uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
1016 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1017 enum emulation_result er = EMULATE_DONE;
1018 int ret = RESUME_GUEST;
1019
James Hoganc4c6f2c2015-02-04 10:52:03 +00001020 /* re-enable HTW before enabling interrupts */
1021 htw_start();
1022
Sanjay Lal669e8462012-11-21 18:34:02 -08001023 /* Set a default exit reason */
1024 run->exit_reason = KVM_EXIT_UNKNOWN;
1025 run->ready_for_interrupt_injection = 1;
1026
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001027 /*
1028 * Set the appropriate status bits based on host CPU features,
1029 * before we hit the scheduler
1030 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001031 kvm_mips_set_c0_status();
1032
1033 local_irq_enable();
1034
1035 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1036 cause, opc, run, vcpu);
1037
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001038 /*
1039 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001040 * causing an exception to be delivered to the Guest Kernel
1041 */
1042 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1043 if (er == EMULATE_PRIV_FAIL) {
1044 goto skip_emul;
1045 } else if (er == EMULATE_FAIL) {
1046 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1047 ret = RESUME_HOST;
1048 goto skip_emul;
1049 }
1050
1051 switch (exccode) {
1052 case T_INT:
1053 kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc);
1054
1055 ++vcpu->stat.int_exits;
1056 trace_kvm_exit(vcpu, INT_EXITS);
1057
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001058 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001059 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001060
1061 ret = RESUME_GUEST;
1062 break;
1063
1064 case T_COP_UNUSABLE:
1065 kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc);
1066
1067 ++vcpu->stat.cop_unusable_exits;
1068 trace_kvm_exit(vcpu, COP_UNUSABLE_EXITS);
1069 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1070 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001071 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001072 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001073 break;
1074
1075 case T_TLB_MOD:
1076 ++vcpu->stat.tlbmod_exits;
1077 trace_kvm_exit(vcpu, TLBMOD_EXITS);
1078 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1079 break;
1080
1081 case T_TLB_ST_MISS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001082 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1083 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1084 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001085
1086 ++vcpu->stat.tlbmiss_st_exits;
1087 trace_kvm_exit(vcpu, TLBMISS_ST_EXITS);
1088 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1089 break;
1090
1091 case T_TLB_LD_MISS:
1092 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1093 cause, opc, badvaddr);
1094
1095 ++vcpu->stat.tlbmiss_ld_exits;
1096 trace_kvm_exit(vcpu, TLBMISS_LD_EXITS);
1097 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1098 break;
1099
1100 case T_ADDR_ERR_ST:
1101 ++vcpu->stat.addrerr_st_exits;
1102 trace_kvm_exit(vcpu, ADDRERR_ST_EXITS);
1103 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1104 break;
1105
1106 case T_ADDR_ERR_LD:
1107 ++vcpu->stat.addrerr_ld_exits;
1108 trace_kvm_exit(vcpu, ADDRERR_LD_EXITS);
1109 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1110 break;
1111
1112 case T_SYSCALL:
1113 ++vcpu->stat.syscall_exits;
1114 trace_kvm_exit(vcpu, SYSCALL_EXITS);
1115 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1116 break;
1117
1118 case T_RES_INST:
1119 ++vcpu->stat.resvd_inst_exits;
1120 trace_kvm_exit(vcpu, RESVD_INST_EXITS);
1121 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1122 break;
1123
1124 case T_BREAK:
1125 ++vcpu->stat.break_inst_exits;
1126 trace_kvm_exit(vcpu, BREAK_INST_EXITS);
1127 ret = kvm_mips_callbacks->handle_break(vcpu);
1128 break;
1129
James Hogan0a560422015-02-06 16:03:57 +00001130 case T_TRAP:
1131 ++vcpu->stat.trap_inst_exits;
1132 trace_kvm_exit(vcpu, TRAP_INST_EXITS);
1133 ret = kvm_mips_callbacks->handle_trap(vcpu);
1134 break;
1135
James Hogan98119ad2015-02-06 11:11:56 +00001136 case T_MSADIS:
1137 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1138 break;
1139
Sanjay Lal669e8462012-11-21 18:34:02 -08001140 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001141 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1142 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1143 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001144 kvm_arch_vcpu_dump_regs(vcpu);
1145 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1146 ret = RESUME_HOST;
1147 break;
1148
1149 }
1150
1151skip_emul:
1152 local_irq_disable();
1153
1154 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1155 kvm_mips_deliver_interrupts(vcpu, cause);
1156
1157 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001158 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001159 if (signal_pending(current)) {
1160 run->exit_reason = KVM_EXIT_INTR;
1161 ret = (-EINTR << 2) | RESUME_HOST;
1162 ++vcpu->stat.signal_exits;
1163 trace_kvm_exit(vcpu, SIGNAL_EXITS);
1164 }
1165 }
1166
James Hoganc4c6f2c2015-02-04 10:52:03 +00001167 /* Disable HTW before returning to guest or host */
1168 htw_stop();
1169
Sanjay Lal669e8462012-11-21 18:34:02 -08001170 return ret;
1171}
1172
1173int __init kvm_mips_init(void)
1174{
1175 int ret;
1176
1177 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1178
1179 if (ret)
1180 return ret;
1181
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001182 /*
1183 * On MIPS, kernel modules are executed from "mapped space", which
1184 * requires TLBs. The TLB handling code is statically linked with
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -07001185 * the rest of the kernel (tlb.c) to avoid the possibility of
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001186 * double faulting. The issue is that the TLB code references
1187 * routines that are part of the the KVM module, which are only
1188 * available once the module is loaded.
Sanjay Lal669e8462012-11-21 18:34:02 -08001189 */
1190 kvm_mips_gfn_to_pfn = gfn_to_pfn;
1191 kvm_mips_release_pfn_clean = kvm_release_pfn_clean;
1192 kvm_mips_is_error_pfn = is_error_pfn;
1193
1194 pr_info("KVM/MIPS Initialized\n");
1195 return 0;
1196}
1197
1198void __exit kvm_mips_exit(void)
1199{
1200 kvm_exit();
1201
1202 kvm_mips_gfn_to_pfn = NULL;
1203 kvm_mips_release_pfn_clean = NULL;
1204 kvm_mips_is_error_pfn = NULL;
1205
1206 pr_info("KVM/MIPS unloaded\n");
1207}
1208
1209module_init(kvm_mips_init);
1210module_exit(kvm_mips_exit);
1211
1212EXPORT_TRACEPOINT_SYMBOL(kvm_exit);