blob: b26a48d8146702cf52a0d9ee58e8390cd60c784d [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>
James Hogan98e91b82014-11-18 14:09:12 +000014#include <linux/kdebug.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080015#include <linux/module.h>
16#include <linux/vmalloc.h>
17#include <linux/fs.h>
18#include <linux/bootmem.h>
James Hoganf7982172015-02-04 17:06:37 +000019#include <asm/fpu.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080020#include <asm/page.h>
21#include <asm/cacheflush.h>
22#include <asm/mmu_context.h>
James Hoganc4c6f2c2015-02-04 10:52:03 +000023#include <asm/pgtable.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080024
25#include <linux/kvm_host.h>
26
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070027#include "interrupt.h"
28#include "commpage.h"
Sanjay Lal669e8462012-11-21 18:34:02 -080029
30#define CREATE_TRACE_POINTS
31#include "trace.h"
32
33#ifndef VECTORSPACING
34#define VECTORSPACING 0x100 /* for EI/VI mode */
35#endif
36
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070037#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
Sanjay Lal669e8462012-11-21 18:34:02 -080038struct kvm_stats_debugfs_item debugfs_entries[] = {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070039 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
40 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
41 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
42 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
43 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
44 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
45 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
46 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
47 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
48 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
49 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
50 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
51 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
James Hogan0a560422015-02-06 16:03:57 +000052 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070053 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
Paolo Bonzinif7819512015-02-04 18:20:58 +010054 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070055 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
Sanjay Lal669e8462012-11-21 18:34:02 -080056 {NULL}
57};
58
59static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu)
60{
61 int i;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070062
Sanjay Lal669e8462012-11-21 18:34:02 -080063 for_each_possible_cpu(i) {
64 vcpu->arch.guest_kernel_asid[i] = 0;
65 vcpu->arch.guest_user_asid[i] = 0;
66 }
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070067
Sanjay Lal669e8462012-11-21 18:34:02 -080068 return 0;
69}
70
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070071/*
72 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
73 * Config7, so we are "runnable" if interrupts are pending
Sanjay Lal669e8462012-11-21 18:34:02 -080074 */
75int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
76{
77 return !!(vcpu->arch.pending_exceptions);
78}
79
80int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
81{
82 return 1;
83}
84
Radim Krčmář13a34e02014-08-28 15:13:03 +020085int kvm_arch_hardware_enable(void)
Sanjay Lal669e8462012-11-21 18:34:02 -080086{
87 return 0;
88}
89
Sanjay Lal669e8462012-11-21 18:34:02 -080090int kvm_arch_hardware_setup(void)
91{
92 return 0;
93}
94
Sanjay Lal669e8462012-11-21 18:34:02 -080095void kvm_arch_check_processor_compat(void *rtn)
96{
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -070097 *(int *)rtn = 0;
Sanjay Lal669e8462012-11-21 18:34:02 -080098}
99
100static void kvm_mips_init_tlbs(struct kvm *kvm)
101{
102 unsigned long wired;
103
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700104 /*
105 * Add a wired entry to the TLB, it is used to map the commpage to
106 * the Guest kernel
107 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800108 wired = read_c0_wired();
109 write_c0_wired(wired + 1);
110 mtc0_tlbw_hazard();
111 kvm->arch.commpage_tlb = wired;
112
113 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
114 kvm->arch.commpage_tlb);
115}
116
117static void kvm_mips_init_vm_percpu(void *arg)
118{
119 struct kvm *kvm = (struct kvm *)arg;
120
121 kvm_mips_init_tlbs(kvm);
122 kvm_mips_callbacks->vm_init(kvm);
123
124}
125
126int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
127{
128 if (atomic_inc_return(&kvm_mips_instance) == 1) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100129 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
130 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800131 on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
132 }
133
Sanjay Lal669e8462012-11-21 18:34:02 -0800134 return 0;
135}
136
137void kvm_mips_free_vcpus(struct kvm *kvm)
138{
139 unsigned int i;
140 struct kvm_vcpu *vcpu;
141
142 /* Put the pages we reserved for the guest pmap */
143 for (i = 0; i < kvm->arch.guest_pmap_npages; i++) {
144 if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE)
145 kvm_mips_release_pfn_clean(kvm->arch.guest_pmap[i]);
146 }
James Hoganc6c0a662014-05-29 10:16:44 +0100147 kfree(kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800148
149 kvm_for_each_vcpu(i, vcpu, kvm) {
150 kvm_arch_vcpu_free(vcpu);
151 }
152
153 mutex_lock(&kvm->lock);
154
155 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
156 kvm->vcpus[i] = NULL;
157
158 atomic_set(&kvm->online_vcpus, 0);
159
160 mutex_unlock(&kvm->lock);
161}
162
Sanjay Lal669e8462012-11-21 18:34:02 -0800163static void kvm_mips_uninit_tlbs(void *arg)
164{
165 /* Restore wired count */
166 write_c0_wired(0);
167 mtc0_tlbw_hazard();
168 /* Clear out all the TLBs */
169 kvm_local_flush_tlb_all();
170}
171
172void kvm_arch_destroy_vm(struct kvm *kvm)
173{
174 kvm_mips_free_vcpus(kvm);
175
176 /* If this is the last instance, restore wired count */
177 if (atomic_dec_return(&kvm_mips_instance) == 0) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100178 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
179 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800180 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
181 }
182}
183
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700184long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
185 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800186{
David Daneyed829852013-05-23 09:49:10 -0700187 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800188}
189
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530190int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
191 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800192{
193 return 0;
194}
195
196int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700197 struct kvm_memory_slot *memslot,
198 struct kvm_userspace_memory_region *mem,
199 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800200{
201 return 0;
202}
203
204void kvm_arch_commit_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700205 struct kvm_userspace_memory_region *mem,
206 const struct kvm_memory_slot *old,
207 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800208{
209 unsigned long npages = 0;
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700210 int i;
Sanjay Lal669e8462012-11-21 18:34:02 -0800211
212 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
213 __func__, kvm, mem->slot, mem->guest_phys_addr,
214 mem->memory_size, mem->userspace_addr);
215
216 /* Setup Guest PMAP table */
217 if (!kvm->arch.guest_pmap) {
218 if (mem->slot == 0)
219 npages = mem->memory_size >> PAGE_SHIFT;
220
221 if (npages) {
222 kvm->arch.guest_pmap_npages = npages;
223 kvm->arch.guest_pmap =
224 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
225
226 if (!kvm->arch.guest_pmap) {
227 kvm_err("Failed to allocate guest PMAP");
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700228 return;
Sanjay Lal669e8462012-11-21 18:34:02 -0800229 }
230
James Hogan6e95bfd2014-05-29 10:16:43 +0100231 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
232 npages, kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800233
234 /* Now setup the page table */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700235 for (i = 0; i < npages; i++)
Sanjay Lal669e8462012-11-21 18:34:02 -0800236 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
Sanjay Lal669e8462012-11-21 18:34:02 -0800237 }
238 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800239}
240
Sanjay Lal669e8462012-11-21 18:34:02 -0800241struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
242{
Sanjay Lal669e8462012-11-21 18:34:02 -0800243 int err, size, offset;
244 void *gebase;
245 int i;
246
247 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
248
249 if (!vcpu) {
250 err = -ENOMEM;
251 goto out;
252 }
253
254 err = kvm_vcpu_init(vcpu, kvm, id);
255
256 if (err)
257 goto out_free_cpu;
258
James Hogan6e95bfd2014-05-29 10:16:43 +0100259 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800260
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700261 /*
262 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800263 * guest mode exits
264 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700265 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800266 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700267 else
James Hogan7006e2d2014-05-29 10:16:23 +0100268 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800269
270 /* Save Linux EBASE */
271 vcpu->arch.host_ebase = (void *)read_c0_ebase();
272
273 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
274
275 if (!gebase) {
276 err = -ENOMEM;
277 goto out_free_cpu;
278 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100279 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
280 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800281
282 /* Save new ebase */
283 vcpu->arch.guest_ebase = gebase;
284
285 /* Copy L1 Guest Exception handler to correct offset */
286
287 /* TLB Refill, EXL = 0 */
288 memcpy(gebase, mips32_exception,
289 mips32_exceptionEnd - mips32_exception);
290
291 /* General Exception Entry point */
292 memcpy(gebase + 0x180, mips32_exception,
293 mips32_exceptionEnd - mips32_exception);
294
295 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
296 for (i = 0; i < 8; i++) {
297 kvm_debug("L1 Vectored handler @ %p\n",
298 gebase + 0x200 + (i * VECTORSPACING));
299 memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception,
300 mips32_exceptionEnd - mips32_exception);
301 }
302
303 /* General handler, relocate to unmapped space for sanity's sake */
304 offset = 0x2000;
James Hogan6e95bfd2014-05-29 10:16:43 +0100305 kvm_debug("Installing KVM Exception handlers @ %p, %#x bytes\n",
306 gebase + offset,
307 mips32_GuestExceptionEnd - mips32_GuestException);
Sanjay Lal669e8462012-11-21 18:34:02 -0800308
309 memcpy(gebase + offset, mips32_GuestException,
310 mips32_GuestExceptionEnd - mips32_GuestException);
311
312 /* Invalidate the icache for these ranges */
James Hoganfacaaec2014-05-29 10:16:25 +0100313 local_flush_icache_range((unsigned long)gebase,
314 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800315
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700316 /*
317 * Allocate comm page for guest kernel, a TLB will be reserved for
318 * mapping GVA @ 0xFFFF8000 to this page
319 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800320 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
321
322 if (!vcpu->arch.kseg0_commpage) {
323 err = -ENOMEM;
324 goto out_free_gebase;
325 }
326
James Hogan6e95bfd2014-05-29 10:16:43 +0100327 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800328 kvm_mips_commpage_init(vcpu);
329
330 /* Init */
331 vcpu->arch.last_sched_cpu = -1;
332
333 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100334 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800335
336 return vcpu;
337
338out_free_gebase:
339 kfree(gebase);
340
341out_free_cpu:
342 kfree(vcpu);
343
344out:
345 return ERR_PTR(err);
346}
347
348void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
349{
350 hrtimer_cancel(&vcpu->arch.comparecount_timer);
351
352 kvm_vcpu_uninit(vcpu);
353
354 kvm_mips_dump_stats(vcpu);
355
James Hoganc6c0a662014-05-29 10:16:44 +0100356 kfree(vcpu->arch.guest_ebase);
357 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700358 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800359}
360
361void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
362{
363 kvm_arch_vcpu_free(vcpu);
364}
365
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700366int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
367 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800368{
David Daneyed829852013-05-23 09:49:10 -0700369 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800370}
371
372int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
373{
374 int r = 0;
375 sigset_t sigsaved;
376
377 if (vcpu->sigset_active)
378 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
379
380 if (vcpu->mmio_needed) {
381 if (!vcpu->mmio_is_write)
382 kvm_mips_complete_mmio_load(vcpu, run);
383 vcpu->mmio_needed = 0;
384 }
385
James Hoganf7982172015-02-04 17:06:37 +0000386 lose_fpu(1);
387
James Hogan044f0f02014-05-29 10:16:32 +0100388 local_irq_disable();
Sanjay Lal669e8462012-11-21 18:34:02 -0800389 /* Check if we have any exceptions/interrupts pending */
390 kvm_mips_deliver_interrupts(vcpu,
391 kvm_read_c0_guest_cause(vcpu->arch.cop0));
392
Sanjay Lal669e8462012-11-21 18:34:02 -0800393 kvm_guest_enter();
394
James Hoganc4c6f2c2015-02-04 10:52:03 +0000395 /* Disable hardware page table walking while in guest */
396 htw_stop();
397
Sanjay Lal669e8462012-11-21 18:34:02 -0800398 r = __kvm_mips_vcpu_run(run, vcpu);
399
James Hoganc4c6f2c2015-02-04 10:52:03 +0000400 /* Re-enable HTW before enabling interrupts */
401 htw_start();
402
Sanjay Lal669e8462012-11-21 18:34:02 -0800403 kvm_guest_exit();
404 local_irq_enable();
405
406 if (vcpu->sigset_active)
407 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
408
409 return r;
410}
411
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700412int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
413 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800414{
415 int intr = (int)irq->irq;
416 struct kvm_vcpu *dvcpu = NULL;
417
418 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
419 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
420 (int)intr);
421
422 if (irq->cpu == -1)
423 dvcpu = vcpu;
424 else
425 dvcpu = vcpu->kvm->vcpus[irq->cpu];
426
427 if (intr == 2 || intr == 3 || intr == 4) {
428 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
429
430 } else if (intr == -2 || intr == -3 || intr == -4) {
431 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
432 } else {
433 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
434 irq->cpu, irq->irq);
435 return -EINVAL;
436 }
437
438 dvcpu->arch.wait = 0;
439
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700440 if (waitqueue_active(&dvcpu->wq))
Sanjay Lal669e8462012-11-21 18:34:02 -0800441 wake_up_interruptible(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800442
443 return 0;
444}
445
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700446int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
447 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800448{
David Daneyed829852013-05-23 09:49:10 -0700449 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800450}
451
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700452int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
453 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800454{
David Daneyed829852013-05-23 09:49:10 -0700455 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800456}
457
David Daney4c73fb22013-05-23 09:49:09 -0700458static u64 kvm_mips_get_one_regs[] = {
459 KVM_REG_MIPS_R0,
460 KVM_REG_MIPS_R1,
461 KVM_REG_MIPS_R2,
462 KVM_REG_MIPS_R3,
463 KVM_REG_MIPS_R4,
464 KVM_REG_MIPS_R5,
465 KVM_REG_MIPS_R6,
466 KVM_REG_MIPS_R7,
467 KVM_REG_MIPS_R8,
468 KVM_REG_MIPS_R9,
469 KVM_REG_MIPS_R10,
470 KVM_REG_MIPS_R11,
471 KVM_REG_MIPS_R12,
472 KVM_REG_MIPS_R13,
473 KVM_REG_MIPS_R14,
474 KVM_REG_MIPS_R15,
475 KVM_REG_MIPS_R16,
476 KVM_REG_MIPS_R17,
477 KVM_REG_MIPS_R18,
478 KVM_REG_MIPS_R19,
479 KVM_REG_MIPS_R20,
480 KVM_REG_MIPS_R21,
481 KVM_REG_MIPS_R22,
482 KVM_REG_MIPS_R23,
483 KVM_REG_MIPS_R24,
484 KVM_REG_MIPS_R25,
485 KVM_REG_MIPS_R26,
486 KVM_REG_MIPS_R27,
487 KVM_REG_MIPS_R28,
488 KVM_REG_MIPS_R29,
489 KVM_REG_MIPS_R30,
490 KVM_REG_MIPS_R31,
491
492 KVM_REG_MIPS_HI,
493 KVM_REG_MIPS_LO,
494 KVM_REG_MIPS_PC,
495
496 KVM_REG_MIPS_CP0_INDEX,
497 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100498 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700499 KVM_REG_MIPS_CP0_PAGEMASK,
500 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100501 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700502 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100503 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700504 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100505 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700506 KVM_REG_MIPS_CP0_STATUS,
507 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100508 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100509 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700510 KVM_REG_MIPS_CP0_CONFIG,
511 KVM_REG_MIPS_CP0_CONFIG1,
512 KVM_REG_MIPS_CP0_CONFIG2,
513 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100514 KVM_REG_MIPS_CP0_CONFIG4,
515 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700516 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100517 KVM_REG_MIPS_CP0_ERROREPC,
518
519 KVM_REG_MIPS_COUNT_CTL,
520 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100521 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700522};
523
524static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
525 const struct kvm_one_reg *reg)
526{
David Daney4c73fb22013-05-23 09:49:09 -0700527 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganf8be02d2014-05-29 10:16:29 +0100528 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700529 s64 v;
530
531 switch (reg->id) {
532 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
533 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
534 break;
535 case KVM_REG_MIPS_HI:
536 v = (long)vcpu->arch.hi;
537 break;
538 case KVM_REG_MIPS_LO:
539 v = (long)vcpu->arch.lo;
540 break;
541 case KVM_REG_MIPS_PC:
542 v = (long)vcpu->arch.pc;
543 break;
544
545 case KVM_REG_MIPS_CP0_INDEX:
546 v = (long)kvm_read_c0_guest_index(cop0);
547 break;
548 case KVM_REG_MIPS_CP0_CONTEXT:
549 v = (long)kvm_read_c0_guest_context(cop0);
550 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100551 case KVM_REG_MIPS_CP0_USERLOCAL:
552 v = (long)kvm_read_c0_guest_userlocal(cop0);
553 break;
David Daney4c73fb22013-05-23 09:49:09 -0700554 case KVM_REG_MIPS_CP0_PAGEMASK:
555 v = (long)kvm_read_c0_guest_pagemask(cop0);
556 break;
557 case KVM_REG_MIPS_CP0_WIRED:
558 v = (long)kvm_read_c0_guest_wired(cop0);
559 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100560 case KVM_REG_MIPS_CP0_HWRENA:
561 v = (long)kvm_read_c0_guest_hwrena(cop0);
562 break;
David Daney4c73fb22013-05-23 09:49:09 -0700563 case KVM_REG_MIPS_CP0_BADVADDR:
564 v = (long)kvm_read_c0_guest_badvaddr(cop0);
565 break;
566 case KVM_REG_MIPS_CP0_ENTRYHI:
567 v = (long)kvm_read_c0_guest_entryhi(cop0);
568 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100569 case KVM_REG_MIPS_CP0_COMPARE:
570 v = (long)kvm_read_c0_guest_compare(cop0);
571 break;
David Daney4c73fb22013-05-23 09:49:09 -0700572 case KVM_REG_MIPS_CP0_STATUS:
573 v = (long)kvm_read_c0_guest_status(cop0);
574 break;
575 case KVM_REG_MIPS_CP0_CAUSE:
576 v = (long)kvm_read_c0_guest_cause(cop0);
577 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100578 case KVM_REG_MIPS_CP0_EPC:
579 v = (long)kvm_read_c0_guest_epc(cop0);
580 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100581 case KVM_REG_MIPS_CP0_PRID:
582 v = (long)kvm_read_c0_guest_prid(cop0);
583 break;
David Daney4c73fb22013-05-23 09:49:09 -0700584 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;
James Hoganc7716072014-06-26 15:11:29 +0100596 case KVM_REG_MIPS_CP0_CONFIG4:
597 v = (long)kvm_read_c0_guest_config4(cop0);
598 break;
599 case KVM_REG_MIPS_CP0_CONFIG5:
600 v = (long)kvm_read_c0_guest_config5(cop0);
601 break;
David Daney4c73fb22013-05-23 09:49:09 -0700602 case KVM_REG_MIPS_CP0_CONFIG7:
603 v = (long)kvm_read_c0_guest_config7(cop0);
604 break;
James Hogane93d4c12014-06-26 13:47:22 +0100605 case KVM_REG_MIPS_CP0_ERROREPC:
606 v = (long)kvm_read_c0_guest_errorepc(cop0);
607 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100608 /* registers to be handled specially */
609 case KVM_REG_MIPS_CP0_COUNT:
James Hoganf8239342014-05-29 10:16:37 +0100610 case KVM_REG_MIPS_COUNT_CTL:
611 case KVM_REG_MIPS_COUNT_RESUME:
James Hoganf74a8e22014-05-29 10:16:38 +0100612 case KVM_REG_MIPS_COUNT_HZ:
James Hoganf8be02d2014-05-29 10:16:29 +0100613 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
614 if (ret)
615 return ret;
616 break;
David Daney4c73fb22013-05-23 09:49:09 -0700617 default:
618 return -EINVAL;
619 }
David Daney681865d2013-06-10 12:33:48 -0700620 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
621 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700622
David Daney681865d2013-06-10 12:33:48 -0700623 return put_user(v, uaddr64);
624 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
625 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
626 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700627
David Daney681865d2013-06-10 12:33:48 -0700628 return put_user(v32, uaddr32);
629 } else {
630 return -EINVAL;
631 }
David Daney4c73fb22013-05-23 09:49:09 -0700632}
633
634static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
635 const struct kvm_one_reg *reg)
636{
David Daney4c73fb22013-05-23 09:49:09 -0700637 struct mips_coproc *cop0 = vcpu->arch.cop0;
638 u64 v;
639
David Daney681865d2013-06-10 12:33:48 -0700640 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
641 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
642
643 if (get_user(v, uaddr64) != 0)
644 return -EFAULT;
645 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
646 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
647 s32 v32;
648
649 if (get_user(v32, uaddr32) != 0)
650 return -EFAULT;
651 v = (s64)v32;
652 } else {
653 return -EINVAL;
654 }
David Daney4c73fb22013-05-23 09:49:09 -0700655
656 switch (reg->id) {
657 case KVM_REG_MIPS_R0:
658 /* Silently ignore requests to set $0 */
659 break;
660 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
661 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
662 break;
663 case KVM_REG_MIPS_HI:
664 vcpu->arch.hi = v;
665 break;
666 case KVM_REG_MIPS_LO:
667 vcpu->arch.lo = v;
668 break;
669 case KVM_REG_MIPS_PC:
670 vcpu->arch.pc = v;
671 break;
672
673 case KVM_REG_MIPS_CP0_INDEX:
674 kvm_write_c0_guest_index(cop0, v);
675 break;
676 case KVM_REG_MIPS_CP0_CONTEXT:
677 kvm_write_c0_guest_context(cop0, v);
678 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100679 case KVM_REG_MIPS_CP0_USERLOCAL:
680 kvm_write_c0_guest_userlocal(cop0, v);
681 break;
David Daney4c73fb22013-05-23 09:49:09 -0700682 case KVM_REG_MIPS_CP0_PAGEMASK:
683 kvm_write_c0_guest_pagemask(cop0, v);
684 break;
685 case KVM_REG_MIPS_CP0_WIRED:
686 kvm_write_c0_guest_wired(cop0, v);
687 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100688 case KVM_REG_MIPS_CP0_HWRENA:
689 kvm_write_c0_guest_hwrena(cop0, v);
690 break;
David Daney4c73fb22013-05-23 09:49:09 -0700691 case KVM_REG_MIPS_CP0_BADVADDR:
692 kvm_write_c0_guest_badvaddr(cop0, v);
693 break;
694 case KVM_REG_MIPS_CP0_ENTRYHI:
695 kvm_write_c0_guest_entryhi(cop0, v);
696 break;
697 case KVM_REG_MIPS_CP0_STATUS:
698 kvm_write_c0_guest_status(cop0, v);
699 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100700 case KVM_REG_MIPS_CP0_EPC:
701 kvm_write_c0_guest_epc(cop0, v);
702 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100703 case KVM_REG_MIPS_CP0_PRID:
704 kvm_write_c0_guest_prid(cop0, v);
705 break;
David Daney4c73fb22013-05-23 09:49:09 -0700706 case KVM_REG_MIPS_CP0_ERROREPC:
707 kvm_write_c0_guest_errorepc(cop0, v);
708 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100709 /* registers to be handled specially */
710 case KVM_REG_MIPS_CP0_COUNT:
711 case KVM_REG_MIPS_CP0_COMPARE:
James Hogane30492b2014-05-29 10:16:35 +0100712 case KVM_REG_MIPS_CP0_CAUSE:
James Hoganc7716072014-06-26 15:11:29 +0100713 case KVM_REG_MIPS_CP0_CONFIG:
714 case KVM_REG_MIPS_CP0_CONFIG1:
715 case KVM_REG_MIPS_CP0_CONFIG2:
716 case KVM_REG_MIPS_CP0_CONFIG3:
717 case KVM_REG_MIPS_CP0_CONFIG4:
718 case KVM_REG_MIPS_CP0_CONFIG5:
James Hoganf8239342014-05-29 10:16:37 +0100719 case KVM_REG_MIPS_COUNT_CTL:
720 case KVM_REG_MIPS_COUNT_RESUME:
James Hoganf74a8e22014-05-29 10:16:38 +0100721 case KVM_REG_MIPS_COUNT_HZ:
James Hoganf8be02d2014-05-29 10:16:29 +0100722 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700723 default:
724 return -EINVAL;
725 }
726 return 0;
727}
728
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700729long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
730 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800731{
732 struct kvm_vcpu *vcpu = filp->private_data;
733 void __user *argp = (void __user *)arg;
734 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800735
736 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -0700737 case KVM_SET_ONE_REG:
738 case KVM_GET_ONE_REG: {
739 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700740
David Daney4c73fb22013-05-23 09:49:09 -0700741 if (copy_from_user(&reg, argp, sizeof(reg)))
742 return -EFAULT;
743 if (ioctl == KVM_SET_ONE_REG)
744 return kvm_mips_set_reg(vcpu, &reg);
745 else
746 return kvm_mips_get_reg(vcpu, &reg);
747 }
748 case KVM_GET_REG_LIST: {
749 struct kvm_reg_list __user *user_list = argp;
750 u64 __user *reg_dest;
751 struct kvm_reg_list reg_list;
752 unsigned n;
753
754 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
755 return -EFAULT;
756 n = reg_list.n;
757 reg_list.n = ARRAY_SIZE(kvm_mips_get_one_regs);
758 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
759 return -EFAULT;
760 if (n < reg_list.n)
761 return -E2BIG;
762 reg_dest = user_list->reg;
763 if (copy_to_user(reg_dest, kvm_mips_get_one_regs,
764 sizeof(kvm_mips_get_one_regs)))
765 return -EFAULT;
766 return 0;
767 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800768 case KVM_NMI:
769 /* Treat the NMI as a CPU reset */
770 r = kvm_mips_reset_vcpu(vcpu);
771 break;
772 case KVM_INTERRUPT:
773 {
774 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700775
Sanjay Lal669e8462012-11-21 18:34:02 -0800776 r = -EFAULT;
777 if (copy_from_user(&irq, argp, sizeof(irq)))
778 goto out;
779
Sanjay Lal669e8462012-11-21 18:34:02 -0800780 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
781 irq.irq);
782
783 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
784 break;
785 }
786 default:
David Daney4c73fb22013-05-23 09:49:09 -0700787 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800788 }
789
790out:
791 return r;
792}
793
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700794/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -0800795int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
796{
797 struct kvm_memory_slot *memslot;
798 unsigned long ga, ga_end;
799 int is_dirty = 0;
800 int r;
801 unsigned long n;
802
803 mutex_lock(&kvm->slots_lock);
804
805 r = kvm_get_dirty_log(kvm, log, &is_dirty);
806 if (r)
807 goto out;
808
809 /* If nothing is dirty, don't bother messing with page tables. */
810 if (is_dirty) {
811 memslot = &kvm->memslots->memslots[log->slot];
812
813 ga = memslot->base_gfn << PAGE_SHIFT;
814 ga_end = ga + (memslot->npages << PAGE_SHIFT);
815
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700816 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
817 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -0800818
819 n = kvm_dirty_bitmap_bytes(memslot);
820 memset(memslot->dirty_bitmap, 0, n);
821 }
822
823 r = 0;
824out:
825 mutex_unlock(&kvm->slots_lock);
826 return r;
827
828}
829
830long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
831{
832 long r;
833
834 switch (ioctl) {
835 default:
David Daneyed829852013-05-23 09:49:10 -0700836 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800837 }
838
839 return r;
840}
841
842int kvm_arch_init(void *opaque)
843{
Sanjay Lal669e8462012-11-21 18:34:02 -0800844 if (kvm_mips_callbacks) {
845 kvm_err("kvm: module already exists\n");
846 return -EEXIST;
847 }
848
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700849 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -0800850}
851
852void kvm_arch_exit(void)
853{
854 kvm_mips_callbacks = NULL;
855}
856
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700857int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
858 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -0800859{
David Daneyed829852013-05-23 09:49:10 -0700860 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800861}
862
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700863int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
864 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -0800865{
David Daneyed829852013-05-23 09:49:10 -0700866 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800867}
868
Dominik Dingel31928aa2014-12-04 15:47:07 +0100869void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -0800870{
Sanjay Lal669e8462012-11-21 18:34:02 -0800871}
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
Alexander Graf784aa3d2014-07-14 18:27:35 +0200888int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -0800889{
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
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001000int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1001 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001002{
1003 return 0;
1004}
1005
1006/* Initial guest state */
1007int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1008{
1009 return kvm_mips_callbacks->vcpu_setup(vcpu);
1010}
1011
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001012static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001013{
1014 uint32_t status = read_c0_status();
1015
Sanjay Lal669e8462012-11-21 18:34:02 -08001016 if (cpu_has_dsp)
1017 status |= (ST0_MX);
1018
1019 write_c0_status(status);
1020 ehb();
1021}
1022
1023/*
1024 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1025 */
1026int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1027{
1028 uint32_t cause = vcpu->arch.host_cp0_cause;
1029 uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1030 uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc;
1031 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1032 enum emulation_result er = EMULATE_DONE;
1033 int ret = RESUME_GUEST;
1034
James Hoganc4c6f2c2015-02-04 10:52:03 +00001035 /* re-enable HTW before enabling interrupts */
1036 htw_start();
1037
Sanjay Lal669e8462012-11-21 18:34:02 -08001038 /* Set a default exit reason */
1039 run->exit_reason = KVM_EXIT_UNKNOWN;
1040 run->ready_for_interrupt_injection = 1;
1041
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001042 /*
1043 * Set the appropriate status bits based on host CPU features,
1044 * before we hit the scheduler
1045 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001046 kvm_mips_set_c0_status();
1047
1048 local_irq_enable();
1049
1050 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1051 cause, opc, run, vcpu);
1052
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001053 /*
1054 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001055 * causing an exception to be delivered to the Guest Kernel
1056 */
1057 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1058 if (er == EMULATE_PRIV_FAIL) {
1059 goto skip_emul;
1060 } else if (er == EMULATE_FAIL) {
1061 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1062 ret = RESUME_HOST;
1063 goto skip_emul;
1064 }
1065
1066 switch (exccode) {
1067 case T_INT:
1068 kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc);
1069
1070 ++vcpu->stat.int_exits;
1071 trace_kvm_exit(vcpu, INT_EXITS);
1072
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001073 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001074 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001075
1076 ret = RESUME_GUEST;
1077 break;
1078
1079 case T_COP_UNUSABLE:
1080 kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc);
1081
1082 ++vcpu->stat.cop_unusable_exits;
1083 trace_kvm_exit(vcpu, COP_UNUSABLE_EXITS);
1084 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1085 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001086 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001087 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001088 break;
1089
1090 case T_TLB_MOD:
1091 ++vcpu->stat.tlbmod_exits;
1092 trace_kvm_exit(vcpu, TLBMOD_EXITS);
1093 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1094 break;
1095
1096 case T_TLB_ST_MISS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001097 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1098 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1099 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001100
1101 ++vcpu->stat.tlbmiss_st_exits;
1102 trace_kvm_exit(vcpu, TLBMISS_ST_EXITS);
1103 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1104 break;
1105
1106 case T_TLB_LD_MISS:
1107 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1108 cause, opc, badvaddr);
1109
1110 ++vcpu->stat.tlbmiss_ld_exits;
1111 trace_kvm_exit(vcpu, TLBMISS_LD_EXITS);
1112 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1113 break;
1114
1115 case T_ADDR_ERR_ST:
1116 ++vcpu->stat.addrerr_st_exits;
1117 trace_kvm_exit(vcpu, ADDRERR_ST_EXITS);
1118 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1119 break;
1120
1121 case T_ADDR_ERR_LD:
1122 ++vcpu->stat.addrerr_ld_exits;
1123 trace_kvm_exit(vcpu, ADDRERR_LD_EXITS);
1124 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1125 break;
1126
1127 case T_SYSCALL:
1128 ++vcpu->stat.syscall_exits;
1129 trace_kvm_exit(vcpu, SYSCALL_EXITS);
1130 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1131 break;
1132
1133 case T_RES_INST:
1134 ++vcpu->stat.resvd_inst_exits;
1135 trace_kvm_exit(vcpu, RESVD_INST_EXITS);
1136 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1137 break;
1138
1139 case T_BREAK:
1140 ++vcpu->stat.break_inst_exits;
1141 trace_kvm_exit(vcpu, BREAK_INST_EXITS);
1142 ret = kvm_mips_callbacks->handle_break(vcpu);
1143 break;
1144
James Hogan0a560422015-02-06 16:03:57 +00001145 case T_TRAP:
1146 ++vcpu->stat.trap_inst_exits;
1147 trace_kvm_exit(vcpu, TRAP_INST_EXITS);
1148 ret = kvm_mips_callbacks->handle_trap(vcpu);
1149 break;
1150
James Hogan98119ad2015-02-06 11:11:56 +00001151 case T_MSADIS:
1152 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1153 break;
1154
Sanjay Lal669e8462012-11-21 18:34:02 -08001155 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001156 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1157 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1158 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001159 kvm_arch_vcpu_dump_regs(vcpu);
1160 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1161 ret = RESUME_HOST;
1162 break;
1163
1164 }
1165
1166skip_emul:
1167 local_irq_disable();
1168
1169 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1170 kvm_mips_deliver_interrupts(vcpu, cause);
1171
1172 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001173 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001174 if (signal_pending(current)) {
1175 run->exit_reason = KVM_EXIT_INTR;
1176 ret = (-EINTR << 2) | RESUME_HOST;
1177 ++vcpu->stat.signal_exits;
1178 trace_kvm_exit(vcpu, SIGNAL_EXITS);
1179 }
1180 }
1181
James Hogan98e91b82014-11-18 14:09:12 +00001182 if (ret == RESUME_GUEST) {
1183 /*
1184 * If FPU is enabled (i.e. the guest's FPU context is live),
1185 * restore FCR31.
1186 *
1187 * This should be before returning to the guest exception
1188 * vector, as it may well cause an FP exception if there are
1189 * pending exception bits unmasked. (see
1190 * kvm_mips_csr_die_notifier() for how that is handled).
1191 */
1192 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1193 read_c0_status() & ST0_CU1)
1194 __kvm_restore_fcsr(&vcpu->arch);
1195 }
1196
James Hoganc4c6f2c2015-02-04 10:52:03 +00001197 /* Disable HTW before returning to guest or host */
1198 htw_stop();
1199
Sanjay Lal669e8462012-11-21 18:34:02 -08001200 return ret;
1201}
1202
James Hogan98e91b82014-11-18 14:09:12 +00001203/* Enable FPU for guest and restore context */
1204void kvm_own_fpu(struct kvm_vcpu *vcpu)
1205{
1206 struct mips_coproc *cop0 = vcpu->arch.cop0;
1207 unsigned int sr, cfg5;
1208
1209 preempt_disable();
1210
1211 /*
1212 * Enable FPU for guest
1213 * We set FR and FRE according to guest context
1214 */
1215 sr = kvm_read_c0_guest_status(cop0);
1216 change_c0_status(ST0_CU1 | ST0_FR, sr);
1217 if (cpu_has_fre) {
1218 cfg5 = kvm_read_c0_guest_config5(cop0);
1219 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1220 }
1221 enable_fpu_hazard();
1222
1223 /* If guest FPU state not active, restore it now */
1224 if (!(vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU)) {
1225 __kvm_restore_fpu(&vcpu->arch);
1226 vcpu->arch.fpu_inuse |= KVM_MIPS_FPU_FPU;
1227 }
1228
1229 preempt_enable();
1230}
1231
1232/* Drop FPU without saving it */
1233void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1234{
1235 preempt_disable();
1236 if (vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU) {
1237 clear_c0_status(ST0_CU1 | ST0_FR);
1238 vcpu->arch.fpu_inuse &= ~KVM_MIPS_FPU_FPU;
1239 }
1240 preempt_enable();
1241}
1242
1243/* Save and disable FPU */
1244void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1245{
1246 /*
1247 * FPU gets disabled in root context (hardware) when it is disabled in
1248 * guest context (software), but the register state in the hardware may
1249 * still be in use. This is why we explicitly re-enable the hardware
1250 * before saving.
1251 */
1252
1253 preempt_disable();
1254 if (vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU) {
1255 set_c0_status(ST0_CU1);
1256 enable_fpu_hazard();
1257
1258 __kvm_save_fpu(&vcpu->arch);
1259 vcpu->arch.fpu_inuse &= ~KVM_MIPS_FPU_FPU;
1260
1261 /* Disable FPU */
1262 clear_c0_status(ST0_CU1 | ST0_FR);
1263 }
1264 preempt_enable();
1265}
1266
1267/*
1268 * Step over a specific ctc1 to FCSR which is used to restore guest FCSR state
1269 * and may trigger a "harmless" FP exception if cause bits are set in the value
1270 * being written.
1271 */
1272static int kvm_mips_csr_die_notify(struct notifier_block *self,
1273 unsigned long cmd, void *ptr)
1274{
1275 struct die_args *args = (struct die_args *)ptr;
1276 struct pt_regs *regs = args->regs;
1277 unsigned long pc;
1278
1279 /* Only interested in FPE */
1280 if (cmd != DIE_FP)
1281 return NOTIFY_DONE;
1282
1283 /* Return immediately if guest context isn't active */
1284 if (!(current->flags & PF_VCPU))
1285 return NOTIFY_DONE;
1286
1287 /* Should never get here from user mode */
1288 BUG_ON(user_mode(regs));
1289
1290 pc = instruction_pointer(regs);
1291 switch (cmd) {
1292 case DIE_FP:
1293 /* match 2nd instruction in __kvm_restore_fcsr */
1294 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1295 return NOTIFY_DONE;
1296 break;
1297 }
1298
1299 /* Move PC forward a little and continue executing */
1300 instruction_pointer(regs) += 4;
1301
1302 return NOTIFY_STOP;
1303}
1304
1305static struct notifier_block kvm_mips_csr_die_notifier = {
1306 .notifier_call = kvm_mips_csr_die_notify,
1307};
1308
Sanjay Lal669e8462012-11-21 18:34:02 -08001309int __init kvm_mips_init(void)
1310{
1311 int ret;
1312
1313 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1314
1315 if (ret)
1316 return ret;
1317
James Hogan98e91b82014-11-18 14:09:12 +00001318 register_die_notifier(&kvm_mips_csr_die_notifier);
1319
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001320 /*
1321 * On MIPS, kernel modules are executed from "mapped space", which
1322 * requires TLBs. The TLB handling code is statically linked with
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -07001323 * the rest of the kernel (tlb.c) to avoid the possibility of
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001324 * double faulting. The issue is that the TLB code references
1325 * routines that are part of the the KVM module, which are only
1326 * available once the module is loaded.
Sanjay Lal669e8462012-11-21 18:34:02 -08001327 */
1328 kvm_mips_gfn_to_pfn = gfn_to_pfn;
1329 kvm_mips_release_pfn_clean = kvm_release_pfn_clean;
1330 kvm_mips_is_error_pfn = is_error_pfn;
1331
Sanjay Lal669e8462012-11-21 18:34:02 -08001332 return 0;
1333}
1334
1335void __exit kvm_mips_exit(void)
1336{
1337 kvm_exit();
1338
1339 kvm_mips_gfn_to_pfn = NULL;
1340 kvm_mips_release_pfn_clean = NULL;
1341 kvm_mips_is_error_pfn = NULL;
James Hogan98e91b82014-11-18 14:09:12 +00001342
1343 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001344}
1345
1346module_init(kvm_mips_init);
1347module_exit(kvm_mips_exit);
1348
1349EXPORT_TRACEPOINT_SYMBOL(kvm_exit);