blob: e3ae1229f147edca713762da571324d977530dbd [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
James Hogan05108702016-06-15 19:29:56 +010012#include <linux/bitops.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080013#include <linux/errno.h>
14#include <linux/err.h>
James Hogan98e91b82014-11-18 14:09:12 +000015#include <linux/kdebug.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080016#include <linux/module.h>
17#include <linux/vmalloc.h>
18#include <linux/fs.h>
19#include <linux/bootmem.h>
James Hoganf7982172015-02-04 17:06:37 +000020#include <asm/fpu.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080021#include <asm/page.h>
22#include <asm/cacheflush.h>
23#include <asm/mmu_context.h>
James Hoganc4c6f2c2015-02-04 10:52:03 +000024#include <asm/pgtable.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080025
26#include <linux/kvm_host.h>
27
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070028#include "interrupt.h"
29#include "commpage.h"
Sanjay Lal669e8462012-11-21 18:34:02 -080030
31#define CREATE_TRACE_POINTS
32#include "trace.h"
33
34#ifndef VECTORSPACING
35#define VECTORSPACING 0x100 /* for EI/VI mode */
36#endif
37
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070038#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
Sanjay Lal669e8462012-11-21 18:34:02 -080039struct kvm_stats_debugfs_item debugfs_entries[] = {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070040 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
41 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
42 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
43 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
44 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
45 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
46 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
47 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
48 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
49 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
50 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
51 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
52 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
James Hogan0a560422015-02-06 16:03:57 +000053 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000054 { "msa_fpe", VCPU_STAT(msa_fpe_exits), KVM_STAT_VCPU },
James Hogan1c0cd662015-02-06 10:56:27 +000055 { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000056 { "msa_disabled", VCPU_STAT(msa_disabled_exits), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070057 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
Paolo Bonzinif7819512015-02-04 18:20:58 +010058 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +020059 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), KVM_STAT_VCPU },
Christian Borntraeger3491caf2016-05-13 12:16:35 +020060 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070061 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
Sanjay Lal669e8462012-11-21 18:34:02 -080062 {NULL}
63};
64
65static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu)
66{
67 int i;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070068
Sanjay Lal669e8462012-11-21 18:34:02 -080069 for_each_possible_cpu(i) {
70 vcpu->arch.guest_kernel_asid[i] = 0;
71 vcpu->arch.guest_user_asid[i] = 0;
72 }
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070073
Sanjay Lal669e8462012-11-21 18:34:02 -080074 return 0;
75}
76
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070077/*
78 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
79 * Config7, so we are "runnable" if interrupts are pending
Sanjay Lal669e8462012-11-21 18:34:02 -080080 */
81int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
82{
83 return !!(vcpu->arch.pending_exceptions);
84}
85
86int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
87{
88 return 1;
89}
90
Radim Krčmář13a34e02014-08-28 15:13:03 +020091int kvm_arch_hardware_enable(void)
Sanjay Lal669e8462012-11-21 18:34:02 -080092{
93 return 0;
94}
95
Sanjay Lal669e8462012-11-21 18:34:02 -080096int kvm_arch_hardware_setup(void)
97{
98 return 0;
99}
100
Sanjay Lal669e8462012-11-21 18:34:02 -0800101void kvm_arch_check_processor_compat(void *rtn)
102{
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700103 *(int *)rtn = 0;
Sanjay Lal669e8462012-11-21 18:34:02 -0800104}
105
106static void kvm_mips_init_tlbs(struct kvm *kvm)
107{
108 unsigned long wired;
109
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700110 /*
111 * Add a wired entry to the TLB, it is used to map the commpage to
112 * the Guest kernel
113 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800114 wired = read_c0_wired();
115 write_c0_wired(wired + 1);
116 mtc0_tlbw_hazard();
117 kvm->arch.commpage_tlb = wired;
118
119 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
120 kvm->arch.commpage_tlb);
121}
122
123static void kvm_mips_init_vm_percpu(void *arg)
124{
125 struct kvm *kvm = (struct kvm *)arg;
126
127 kvm_mips_init_tlbs(kvm);
128 kvm_mips_callbacks->vm_init(kvm);
129
130}
131
132int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
133{
134 if (atomic_inc_return(&kvm_mips_instance) == 1) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100135 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
136 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800137 on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
138 }
139
Sanjay Lal669e8462012-11-21 18:34:02 -0800140 return 0;
141}
142
143void kvm_mips_free_vcpus(struct kvm *kvm)
144{
145 unsigned int i;
146 struct kvm_vcpu *vcpu;
147
148 /* Put the pages we reserved for the guest pmap */
149 for (i = 0; i < kvm->arch.guest_pmap_npages; i++) {
150 if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE)
James Hogan9befad22016-06-09 14:19:11 +0100151 kvm_release_pfn_clean(kvm->arch.guest_pmap[i]);
Sanjay Lal669e8462012-11-21 18:34:02 -0800152 }
James Hoganc6c0a662014-05-29 10:16:44 +0100153 kfree(kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800154
155 kvm_for_each_vcpu(i, vcpu, kvm) {
156 kvm_arch_vcpu_free(vcpu);
157 }
158
159 mutex_lock(&kvm->lock);
160
161 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
162 kvm->vcpus[i] = NULL;
163
164 atomic_set(&kvm->online_vcpus, 0);
165
166 mutex_unlock(&kvm->lock);
167}
168
Sanjay Lal669e8462012-11-21 18:34:02 -0800169static void kvm_mips_uninit_tlbs(void *arg)
170{
171 /* Restore wired count */
172 write_c0_wired(0);
173 mtc0_tlbw_hazard();
174 /* Clear out all the TLBs */
175 kvm_local_flush_tlb_all();
176}
177
178void kvm_arch_destroy_vm(struct kvm *kvm)
179{
180 kvm_mips_free_vcpus(kvm);
181
182 /* If this is the last instance, restore wired count */
183 if (atomic_dec_return(&kvm_mips_instance) == 0) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100184 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
185 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800186 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
187 }
188}
189
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700190long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
191 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800192{
David Daneyed829852013-05-23 09:49:10 -0700193 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800194}
195
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530196int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
197 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800198{
199 return 0;
200}
201
202int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700203 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200204 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700205 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800206{
207 return 0;
208}
209
210void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200211 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700212 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200213 const struct kvm_memory_slot *new,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700214 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800215{
216 unsigned long npages = 0;
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700217 int i;
Sanjay Lal669e8462012-11-21 18:34:02 -0800218
219 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
220 __func__, kvm, mem->slot, mem->guest_phys_addr,
221 mem->memory_size, mem->userspace_addr);
222
223 /* Setup Guest PMAP table */
224 if (!kvm->arch.guest_pmap) {
225 if (mem->slot == 0)
226 npages = mem->memory_size >> PAGE_SHIFT;
227
228 if (npages) {
229 kvm->arch.guest_pmap_npages = npages;
230 kvm->arch.guest_pmap =
231 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
232
233 if (!kvm->arch.guest_pmap) {
James Hoganf7fdcb62015-12-16 23:49:39 +0000234 kvm_err("Failed to allocate guest PMAP\n");
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700235 return;
Sanjay Lal669e8462012-11-21 18:34:02 -0800236 }
237
James Hogan6e95bfd2014-05-29 10:16:43 +0100238 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
239 npages, kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800240
241 /* Now setup the page table */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700242 for (i = 0; i < npages; i++)
Sanjay Lal669e8462012-11-21 18:34:02 -0800243 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
Sanjay Lal669e8462012-11-21 18:34:02 -0800244 }
245 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800246}
247
Sanjay Lal669e8462012-11-21 18:34:02 -0800248struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
249{
James Hogan90e93112016-06-23 17:34:39 +0100250 int err, size;
251 void *gebase, *p;
Sanjay Lal669e8462012-11-21 18:34:02 -0800252 int i;
253
254 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
255
256 if (!vcpu) {
257 err = -ENOMEM;
258 goto out;
259 }
260
261 err = kvm_vcpu_init(vcpu, kvm, id);
262
263 if (err)
264 goto out_free_cpu;
265
James Hogan6e95bfd2014-05-29 10:16:43 +0100266 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800267
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700268 /*
269 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800270 * guest mode exits
271 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700272 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800273 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700274 else
James Hogan7006e2d2014-05-29 10:16:23 +0100275 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800276
Sanjay Lal669e8462012-11-21 18:34:02 -0800277 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
278
279 if (!gebase) {
280 err = -ENOMEM;
James Hogan585bb8f2015-11-11 14:21:20 +0000281 goto out_uninit_cpu;
Sanjay Lal669e8462012-11-21 18:34:02 -0800282 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100283 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
284 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800285
286 /* Save new ebase */
287 vcpu->arch.guest_ebase = gebase;
288
James Hogan90e93112016-06-23 17:34:39 +0100289 /* Build guest exception vectors dynamically in unmapped memory */
Sanjay Lal669e8462012-11-21 18:34:02 -0800290
291 /* TLB Refill, EXL = 0 */
James Hogan90e93112016-06-23 17:34:39 +0100292 kvm_mips_build_exception(gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800293
294 /* General Exception Entry point */
James Hogan90e93112016-06-23 17:34:39 +0100295 kvm_mips_build_exception(gebase + 0x180);
Sanjay Lal669e8462012-11-21 18:34:02 -0800296
297 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
298 for (i = 0; i < 8; i++) {
299 kvm_debug("L1 Vectored handler @ %p\n",
300 gebase + 0x200 + (i * VECTORSPACING));
James Hogan90e93112016-06-23 17:34:39 +0100301 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING);
Sanjay Lal669e8462012-11-21 18:34:02 -0800302 }
303
James Hogan90e93112016-06-23 17:34:39 +0100304 /* General exit handler */
305 p = gebase + 0x2000;
306 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800307
James Hogan90e93112016-06-23 17:34:39 +0100308 /* Guest entry routine */
309 vcpu->arch.vcpu_run = p;
310 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100311
Sanjay Lal669e8462012-11-21 18:34:02 -0800312 /* 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
James Hogan585bb8f2015-11-11 14:21:20 +0000341out_uninit_cpu:
342 kvm_vcpu_uninit(vcpu);
343
Sanjay Lal669e8462012-11-21 18:34:02 -0800344out_free_cpu:
345 kfree(vcpu);
346
347out:
348 return ERR_PTR(err);
349}
350
351void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
352{
353 hrtimer_cancel(&vcpu->arch.comparecount_timer);
354
355 kvm_vcpu_uninit(vcpu);
356
357 kvm_mips_dump_stats(vcpu);
358
James Hoganc6c0a662014-05-29 10:16:44 +0100359 kfree(vcpu->arch.guest_ebase);
360 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700361 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800362}
363
364void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
365{
366 kvm_arch_vcpu_free(vcpu);
367}
368
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700369int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
370 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800371{
David Daneyed829852013-05-23 09:49:10 -0700372 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800373}
374
375int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
376{
377 int r = 0;
378 sigset_t sigsaved;
379
380 if (vcpu->sigset_active)
381 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
382
383 if (vcpu->mmio_needed) {
384 if (!vcpu->mmio_is_write)
385 kvm_mips_complete_mmio_load(vcpu, run);
386 vcpu->mmio_needed = 0;
387 }
388
James Hoganf7982172015-02-04 17:06:37 +0000389 lose_fpu(1);
390
James Hogan044f0f02014-05-29 10:16:32 +0100391 local_irq_disable();
Sanjay Lal669e8462012-11-21 18:34:02 -0800392 /* Check if we have any exceptions/interrupts pending */
393 kvm_mips_deliver_interrupts(vcpu,
394 kvm_read_c0_guest_cause(vcpu->arch.cop0));
395
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200396 guest_enter_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800397
James Hoganc4c6f2c2015-02-04 10:52:03 +0000398 /* Disable hardware page table walking while in guest */
399 htw_stop();
400
James Hogan93258602016-06-14 09:40:14 +0100401 trace_kvm_enter(vcpu);
James Hogan797179b2016-06-09 10:50:43 +0100402 r = vcpu->arch.vcpu_run(run, vcpu);
James Hogan93258602016-06-14 09:40:14 +0100403 trace_kvm_out(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800404
James Hoganc4c6f2c2015-02-04 10:52:03 +0000405 /* Re-enable HTW before enabling interrupts */
406 htw_start();
407
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200408 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800409 local_irq_enable();
410
411 if (vcpu->sigset_active)
412 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
413
414 return r;
415}
416
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700417int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
418 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800419{
420 int intr = (int)irq->irq;
421 struct kvm_vcpu *dvcpu = NULL;
422
423 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
424 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
425 (int)intr);
426
427 if (irq->cpu == -1)
428 dvcpu = vcpu;
429 else
430 dvcpu = vcpu->kvm->vcpus[irq->cpu];
431
432 if (intr == 2 || intr == 3 || intr == 4) {
433 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
434
435 } else if (intr == -2 || intr == -3 || intr == -4) {
436 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
437 } else {
438 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
439 irq->cpu, irq->irq);
440 return -EINVAL;
441 }
442
443 dvcpu->arch.wait = 0;
444
Marcelo Tosatti85773702016-02-19 09:46:39 +0100445 if (swait_active(&dvcpu->wq))
446 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800447
448 return 0;
449}
450
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700451int kvm_arch_vcpu_ioctl_get_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
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700457int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
458 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800459{
David Daneyed829852013-05-23 09:49:10 -0700460 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800461}
462
David Daney4c73fb22013-05-23 09:49:09 -0700463static u64 kvm_mips_get_one_regs[] = {
464 KVM_REG_MIPS_R0,
465 KVM_REG_MIPS_R1,
466 KVM_REG_MIPS_R2,
467 KVM_REG_MIPS_R3,
468 KVM_REG_MIPS_R4,
469 KVM_REG_MIPS_R5,
470 KVM_REG_MIPS_R6,
471 KVM_REG_MIPS_R7,
472 KVM_REG_MIPS_R8,
473 KVM_REG_MIPS_R9,
474 KVM_REG_MIPS_R10,
475 KVM_REG_MIPS_R11,
476 KVM_REG_MIPS_R12,
477 KVM_REG_MIPS_R13,
478 KVM_REG_MIPS_R14,
479 KVM_REG_MIPS_R15,
480 KVM_REG_MIPS_R16,
481 KVM_REG_MIPS_R17,
482 KVM_REG_MIPS_R18,
483 KVM_REG_MIPS_R19,
484 KVM_REG_MIPS_R20,
485 KVM_REG_MIPS_R21,
486 KVM_REG_MIPS_R22,
487 KVM_REG_MIPS_R23,
488 KVM_REG_MIPS_R24,
489 KVM_REG_MIPS_R25,
490 KVM_REG_MIPS_R26,
491 KVM_REG_MIPS_R27,
492 KVM_REG_MIPS_R28,
493 KVM_REG_MIPS_R29,
494 KVM_REG_MIPS_R30,
495 KVM_REG_MIPS_R31,
496
497 KVM_REG_MIPS_HI,
498 KVM_REG_MIPS_LO,
499 KVM_REG_MIPS_PC,
500
501 KVM_REG_MIPS_CP0_INDEX,
502 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100503 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700504 KVM_REG_MIPS_CP0_PAGEMASK,
505 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100506 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700507 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100508 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700509 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100510 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700511 KVM_REG_MIPS_CP0_STATUS,
512 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100513 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100514 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700515 KVM_REG_MIPS_CP0_CONFIG,
516 KVM_REG_MIPS_CP0_CONFIG1,
517 KVM_REG_MIPS_CP0_CONFIG2,
518 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100519 KVM_REG_MIPS_CP0_CONFIG4,
520 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700521 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100522 KVM_REG_MIPS_CP0_ERROREPC,
523
524 KVM_REG_MIPS_COUNT_CTL,
525 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100526 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700527};
528
James Hogane5775932016-06-15 19:29:51 +0100529static u64 kvm_mips_get_one_regs_fpu[] = {
530 KVM_REG_MIPS_FCR_IR,
531 KVM_REG_MIPS_FCR_CSR,
532};
533
534static u64 kvm_mips_get_one_regs_msa[] = {
535 KVM_REG_MIPS_MSA_IR,
536 KVM_REG_MIPS_MSA_CSR,
537};
538
James Hogan05108702016-06-15 19:29:56 +0100539static u64 kvm_mips_get_one_regs_kscratch[] = {
540 KVM_REG_MIPS_CP0_KSCRATCH1,
541 KVM_REG_MIPS_CP0_KSCRATCH2,
542 KVM_REG_MIPS_CP0_KSCRATCH3,
543 KVM_REG_MIPS_CP0_KSCRATCH4,
544 KVM_REG_MIPS_CP0_KSCRATCH5,
545 KVM_REG_MIPS_CP0_KSCRATCH6,
546};
547
James Hoganf5c43bd2016-06-15 19:29:49 +0100548static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
549{
550 unsigned long ret;
551
552 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100553 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
554 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
555 /* odd doubles */
556 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
557 ret += 16;
558 }
559 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
560 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hogan05108702016-06-15 19:29:56 +0100561 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
James Hoganf5c43bd2016-06-15 19:29:49 +0100562 ret += kvm_mips_callbacks->num_regs(vcpu);
563
564 return ret;
565}
566
567static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
568{
James Hogane5775932016-06-15 19:29:51 +0100569 u64 index;
570 unsigned int i;
571
James Hoganf5c43bd2016-06-15 19:29:49 +0100572 if (copy_to_user(indices, kvm_mips_get_one_regs,
573 sizeof(kvm_mips_get_one_regs)))
574 return -EFAULT;
575 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
576
James Hogane5775932016-06-15 19:29:51 +0100577 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
578 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
579 sizeof(kvm_mips_get_one_regs_fpu)))
580 return -EFAULT;
581 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
582
583 for (i = 0; i < 32; ++i) {
584 index = KVM_REG_MIPS_FPR_32(i);
585 if (copy_to_user(indices, &index, sizeof(index)))
586 return -EFAULT;
587 ++indices;
588
589 /* skip odd doubles if no F64 */
590 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
591 continue;
592
593 index = KVM_REG_MIPS_FPR_64(i);
594 if (copy_to_user(indices, &index, sizeof(index)))
595 return -EFAULT;
596 ++indices;
597 }
598 }
599
600 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
601 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
602 sizeof(kvm_mips_get_one_regs_msa)))
603 return -EFAULT;
604 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
605
606 for (i = 0; i < 32; ++i) {
607 index = KVM_REG_MIPS_VEC_128(i);
608 if (copy_to_user(indices, &index, sizeof(index)))
609 return -EFAULT;
610 ++indices;
611 }
612 }
613
James Hogan05108702016-06-15 19:29:56 +0100614 for (i = 0; i < 6; ++i) {
615 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
616 continue;
617
618 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
619 sizeof(kvm_mips_get_one_regs_kscratch[i])))
620 return -EFAULT;
621 ++indices;
622 }
623
James Hoganf5c43bd2016-06-15 19:29:49 +0100624 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
625}
626
David Daney4c73fb22013-05-23 09:49:09 -0700627static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
628 const struct kvm_one_reg *reg)
629{
David Daney4c73fb22013-05-23 09:49:09 -0700630 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000631 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100632 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700633 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000634 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000635 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700636
637 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000638 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700639 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
640 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
641 break;
642 case KVM_REG_MIPS_HI:
643 v = (long)vcpu->arch.hi;
644 break;
645 case KVM_REG_MIPS_LO:
646 v = (long)vcpu->arch.lo;
647 break;
648 case KVM_REG_MIPS_PC:
649 v = (long)vcpu->arch.pc;
650 break;
651
James Hogan379245c2014-12-02 15:48:24 +0000652 /* Floating point registers */
653 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
654 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
655 return -EINVAL;
656 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
657 /* Odd singles in top of even double when FR=0 */
658 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
659 v = get_fpr32(&fpu->fpr[idx], 0);
660 else
661 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
662 break;
663 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
664 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
665 return -EINVAL;
666 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
667 /* Can't access odd doubles in FR=0 mode */
668 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
669 return -EINVAL;
670 v = get_fpr64(&fpu->fpr[idx], 0);
671 break;
672 case KVM_REG_MIPS_FCR_IR:
673 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
674 return -EINVAL;
675 v = boot_cpu_data.fpu_id;
676 break;
677 case KVM_REG_MIPS_FCR_CSR:
678 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
679 return -EINVAL;
680 v = fpu->fcr31;
681 break;
682
James Hoganab86bd62014-12-02 15:48:24 +0000683 /* MIPS SIMD Architecture (MSA) registers */
684 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
685 if (!kvm_mips_guest_has_msa(&vcpu->arch))
686 return -EINVAL;
687 /* Can't access MSA registers in FR=0 mode */
688 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
689 return -EINVAL;
690 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
691#ifdef CONFIG_CPU_LITTLE_ENDIAN
692 /* least significant byte first */
693 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
694 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
695#else
696 /* most significant byte first */
697 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
698 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
699#endif
700 break;
701 case KVM_REG_MIPS_MSA_IR:
702 if (!kvm_mips_guest_has_msa(&vcpu->arch))
703 return -EINVAL;
704 v = boot_cpu_data.msa_id;
705 break;
706 case KVM_REG_MIPS_MSA_CSR:
707 if (!kvm_mips_guest_has_msa(&vcpu->arch))
708 return -EINVAL;
709 v = fpu->msacsr;
710 break;
711
James Hogan379245c2014-12-02 15:48:24 +0000712 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700713 case KVM_REG_MIPS_CP0_INDEX:
714 v = (long)kvm_read_c0_guest_index(cop0);
715 break;
716 case KVM_REG_MIPS_CP0_CONTEXT:
717 v = (long)kvm_read_c0_guest_context(cop0);
718 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100719 case KVM_REG_MIPS_CP0_USERLOCAL:
720 v = (long)kvm_read_c0_guest_userlocal(cop0);
721 break;
David Daney4c73fb22013-05-23 09:49:09 -0700722 case KVM_REG_MIPS_CP0_PAGEMASK:
723 v = (long)kvm_read_c0_guest_pagemask(cop0);
724 break;
725 case KVM_REG_MIPS_CP0_WIRED:
726 v = (long)kvm_read_c0_guest_wired(cop0);
727 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100728 case KVM_REG_MIPS_CP0_HWRENA:
729 v = (long)kvm_read_c0_guest_hwrena(cop0);
730 break;
David Daney4c73fb22013-05-23 09:49:09 -0700731 case KVM_REG_MIPS_CP0_BADVADDR:
732 v = (long)kvm_read_c0_guest_badvaddr(cop0);
733 break;
734 case KVM_REG_MIPS_CP0_ENTRYHI:
735 v = (long)kvm_read_c0_guest_entryhi(cop0);
736 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100737 case KVM_REG_MIPS_CP0_COMPARE:
738 v = (long)kvm_read_c0_guest_compare(cop0);
739 break;
David Daney4c73fb22013-05-23 09:49:09 -0700740 case KVM_REG_MIPS_CP0_STATUS:
741 v = (long)kvm_read_c0_guest_status(cop0);
742 break;
743 case KVM_REG_MIPS_CP0_CAUSE:
744 v = (long)kvm_read_c0_guest_cause(cop0);
745 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100746 case KVM_REG_MIPS_CP0_EPC:
747 v = (long)kvm_read_c0_guest_epc(cop0);
748 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100749 case KVM_REG_MIPS_CP0_PRID:
750 v = (long)kvm_read_c0_guest_prid(cop0);
751 break;
David Daney4c73fb22013-05-23 09:49:09 -0700752 case KVM_REG_MIPS_CP0_CONFIG:
753 v = (long)kvm_read_c0_guest_config(cop0);
754 break;
755 case KVM_REG_MIPS_CP0_CONFIG1:
756 v = (long)kvm_read_c0_guest_config1(cop0);
757 break;
758 case KVM_REG_MIPS_CP0_CONFIG2:
759 v = (long)kvm_read_c0_guest_config2(cop0);
760 break;
761 case KVM_REG_MIPS_CP0_CONFIG3:
762 v = (long)kvm_read_c0_guest_config3(cop0);
763 break;
James Hoganc7716072014-06-26 15:11:29 +0100764 case KVM_REG_MIPS_CP0_CONFIG4:
765 v = (long)kvm_read_c0_guest_config4(cop0);
766 break;
767 case KVM_REG_MIPS_CP0_CONFIG5:
768 v = (long)kvm_read_c0_guest_config5(cop0);
769 break;
David Daney4c73fb22013-05-23 09:49:09 -0700770 case KVM_REG_MIPS_CP0_CONFIG7:
771 v = (long)kvm_read_c0_guest_config7(cop0);
772 break;
James Hogane93d4c12014-06-26 13:47:22 +0100773 case KVM_REG_MIPS_CP0_ERROREPC:
774 v = (long)kvm_read_c0_guest_errorepc(cop0);
775 break;
James Hogan05108702016-06-15 19:29:56 +0100776 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
777 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
778 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
779 return -EINVAL;
780 switch (idx) {
781 case 2:
782 v = (long)kvm_read_c0_guest_kscratch1(cop0);
783 break;
784 case 3:
785 v = (long)kvm_read_c0_guest_kscratch2(cop0);
786 break;
787 case 4:
788 v = (long)kvm_read_c0_guest_kscratch3(cop0);
789 break;
790 case 5:
791 v = (long)kvm_read_c0_guest_kscratch4(cop0);
792 break;
793 case 6:
794 v = (long)kvm_read_c0_guest_kscratch5(cop0);
795 break;
796 case 7:
797 v = (long)kvm_read_c0_guest_kscratch6(cop0);
798 break;
799 }
800 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100801 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100802 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100803 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
804 if (ret)
805 return ret;
806 break;
David Daney4c73fb22013-05-23 09:49:09 -0700807 }
David Daney681865d2013-06-10 12:33:48 -0700808 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
809 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700810
David Daney681865d2013-06-10 12:33:48 -0700811 return put_user(v, uaddr64);
812 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
813 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
814 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700815
David Daney681865d2013-06-10 12:33:48 -0700816 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000817 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
818 void __user *uaddr = (void __user *)(long)reg->addr;
819
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200820 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700821 } else {
822 return -EINVAL;
823 }
David Daney4c73fb22013-05-23 09:49:09 -0700824}
825
826static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
827 const struct kvm_one_reg *reg)
828{
David Daney4c73fb22013-05-23 09:49:09 -0700829 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000830 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
831 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000832 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000833 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700834
David Daney681865d2013-06-10 12:33:48 -0700835 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
836 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
837
838 if (get_user(v, uaddr64) != 0)
839 return -EFAULT;
840 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
841 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
842 s32 v32;
843
844 if (get_user(v32, uaddr32) != 0)
845 return -EFAULT;
846 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000847 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
848 void __user *uaddr = (void __user *)(long)reg->addr;
849
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200850 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700851 } else {
852 return -EINVAL;
853 }
David Daney4c73fb22013-05-23 09:49:09 -0700854
855 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000856 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700857 case KVM_REG_MIPS_R0:
858 /* Silently ignore requests to set $0 */
859 break;
860 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
861 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
862 break;
863 case KVM_REG_MIPS_HI:
864 vcpu->arch.hi = v;
865 break;
866 case KVM_REG_MIPS_LO:
867 vcpu->arch.lo = v;
868 break;
869 case KVM_REG_MIPS_PC:
870 vcpu->arch.pc = v;
871 break;
872
James Hogan379245c2014-12-02 15:48:24 +0000873 /* Floating point registers */
874 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
875 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
876 return -EINVAL;
877 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
878 /* Odd singles in top of even double when FR=0 */
879 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
880 set_fpr32(&fpu->fpr[idx], 0, v);
881 else
882 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
883 break;
884 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
885 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
886 return -EINVAL;
887 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
888 /* Can't access odd doubles in FR=0 mode */
889 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
890 return -EINVAL;
891 set_fpr64(&fpu->fpr[idx], 0, v);
892 break;
893 case KVM_REG_MIPS_FCR_IR:
894 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
895 return -EINVAL;
896 /* Read-only */
897 break;
898 case KVM_REG_MIPS_FCR_CSR:
899 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
900 return -EINVAL;
901 fpu->fcr31 = v;
902 break;
903
James Hoganab86bd62014-12-02 15:48:24 +0000904 /* MIPS SIMD Architecture (MSA) registers */
905 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
906 if (!kvm_mips_guest_has_msa(&vcpu->arch))
907 return -EINVAL;
908 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
909#ifdef CONFIG_CPU_LITTLE_ENDIAN
910 /* least significant byte first */
911 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
912 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
913#else
914 /* most significant byte first */
915 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
916 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
917#endif
918 break;
919 case KVM_REG_MIPS_MSA_IR:
920 if (!kvm_mips_guest_has_msa(&vcpu->arch))
921 return -EINVAL;
922 /* Read-only */
923 break;
924 case KVM_REG_MIPS_MSA_CSR:
925 if (!kvm_mips_guest_has_msa(&vcpu->arch))
926 return -EINVAL;
927 fpu->msacsr = v;
928 break;
929
James Hogan379245c2014-12-02 15:48:24 +0000930 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700931 case KVM_REG_MIPS_CP0_INDEX:
932 kvm_write_c0_guest_index(cop0, v);
933 break;
934 case KVM_REG_MIPS_CP0_CONTEXT:
935 kvm_write_c0_guest_context(cop0, v);
936 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100937 case KVM_REG_MIPS_CP0_USERLOCAL:
938 kvm_write_c0_guest_userlocal(cop0, v);
939 break;
David Daney4c73fb22013-05-23 09:49:09 -0700940 case KVM_REG_MIPS_CP0_PAGEMASK:
941 kvm_write_c0_guest_pagemask(cop0, v);
942 break;
943 case KVM_REG_MIPS_CP0_WIRED:
944 kvm_write_c0_guest_wired(cop0, v);
945 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100946 case KVM_REG_MIPS_CP0_HWRENA:
947 kvm_write_c0_guest_hwrena(cop0, v);
948 break;
David Daney4c73fb22013-05-23 09:49:09 -0700949 case KVM_REG_MIPS_CP0_BADVADDR:
950 kvm_write_c0_guest_badvaddr(cop0, v);
951 break;
952 case KVM_REG_MIPS_CP0_ENTRYHI:
953 kvm_write_c0_guest_entryhi(cop0, v);
954 break;
955 case KVM_REG_MIPS_CP0_STATUS:
956 kvm_write_c0_guest_status(cop0, v);
957 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100958 case KVM_REG_MIPS_CP0_EPC:
959 kvm_write_c0_guest_epc(cop0, v);
960 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100961 case KVM_REG_MIPS_CP0_PRID:
962 kvm_write_c0_guest_prid(cop0, v);
963 break;
David Daney4c73fb22013-05-23 09:49:09 -0700964 case KVM_REG_MIPS_CP0_ERROREPC:
965 kvm_write_c0_guest_errorepc(cop0, v);
966 break;
James Hogan05108702016-06-15 19:29:56 +0100967 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
968 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
969 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
970 return -EINVAL;
971 switch (idx) {
972 case 2:
973 kvm_write_c0_guest_kscratch1(cop0, v);
974 break;
975 case 3:
976 kvm_write_c0_guest_kscratch2(cop0, v);
977 break;
978 case 4:
979 kvm_write_c0_guest_kscratch3(cop0, v);
980 break;
981 case 5:
982 kvm_write_c0_guest_kscratch4(cop0, v);
983 break;
984 case 6:
985 kvm_write_c0_guest_kscratch5(cop0, v);
986 break;
987 case 7:
988 kvm_write_c0_guest_kscratch6(cop0, v);
989 break;
990 }
991 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100992 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -0700993 default:
James Hogancc68d222016-06-15 19:29:48 +0100994 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700995 }
996 return 0;
997}
998
James Hogan5fafd8742014-12-08 23:07:56 +0000999static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1000 struct kvm_enable_cap *cap)
1001{
1002 int r = 0;
1003
1004 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1005 return -EINVAL;
1006 if (cap->flags)
1007 return -EINVAL;
1008 if (cap->args[0])
1009 return -EINVAL;
1010
1011 switch (cap->cap) {
1012 case KVM_CAP_MIPS_FPU:
1013 vcpu->arch.fpu_enabled = true;
1014 break;
James Hogand952bd02014-12-08 23:07:56 +00001015 case KVM_CAP_MIPS_MSA:
1016 vcpu->arch.msa_enabled = true;
1017 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001018 default:
1019 r = -EINVAL;
1020 break;
1021 }
1022
1023 return r;
1024}
1025
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001026long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1027 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -08001028{
1029 struct kvm_vcpu *vcpu = filp->private_data;
1030 void __user *argp = (void __user *)arg;
1031 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001032
1033 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -07001034 case KVM_SET_ONE_REG:
1035 case KVM_GET_ONE_REG: {
1036 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001037
David Daney4c73fb22013-05-23 09:49:09 -07001038 if (copy_from_user(&reg, argp, sizeof(reg)))
1039 return -EFAULT;
1040 if (ioctl == KVM_SET_ONE_REG)
1041 return kvm_mips_set_reg(vcpu, &reg);
1042 else
1043 return kvm_mips_get_reg(vcpu, &reg);
1044 }
1045 case KVM_GET_REG_LIST: {
1046 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -07001047 struct kvm_reg_list reg_list;
1048 unsigned n;
1049
1050 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1051 return -EFAULT;
1052 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +01001053 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -07001054 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1055 return -EFAULT;
1056 if (n < reg_list.n)
1057 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +01001058 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -07001059 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001060 case KVM_NMI:
1061 /* Treat the NMI as a CPU reset */
1062 r = kvm_mips_reset_vcpu(vcpu);
1063 break;
1064 case KVM_INTERRUPT:
1065 {
1066 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001067
Sanjay Lal669e8462012-11-21 18:34:02 -08001068 r = -EFAULT;
1069 if (copy_from_user(&irq, argp, sizeof(irq)))
1070 goto out;
1071
Sanjay Lal669e8462012-11-21 18:34:02 -08001072 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1073 irq.irq);
1074
1075 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1076 break;
1077 }
James Hogan5fafd8742014-12-08 23:07:56 +00001078 case KVM_ENABLE_CAP: {
1079 struct kvm_enable_cap cap;
1080
1081 r = -EFAULT;
1082 if (copy_from_user(&cap, argp, sizeof(cap)))
1083 goto out;
1084 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1085 break;
1086 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001087 default:
David Daney4c73fb22013-05-23 09:49:09 -07001088 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001089 }
1090
1091out:
1092 return r;
1093}
1094
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001095/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001096int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1097{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001098 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001099 struct kvm_memory_slot *memslot;
1100 unsigned long ga, ga_end;
1101 int is_dirty = 0;
1102 int r;
1103 unsigned long n;
1104
1105 mutex_lock(&kvm->slots_lock);
1106
1107 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1108 if (r)
1109 goto out;
1110
1111 /* If nothing is dirty, don't bother messing with page tables. */
1112 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001113 slots = kvm_memslots(kvm);
1114 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001115
1116 ga = memslot->base_gfn << PAGE_SHIFT;
1117 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1118
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001119 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
1120 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -08001121
1122 n = kvm_dirty_bitmap_bytes(memslot);
1123 memset(memslot->dirty_bitmap, 0, n);
1124 }
1125
1126 r = 0;
1127out:
1128 mutex_unlock(&kvm->slots_lock);
1129 return r;
1130
1131}
1132
1133long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1134{
1135 long r;
1136
1137 switch (ioctl) {
1138 default:
David Daneyed829852013-05-23 09:49:10 -07001139 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001140 }
1141
1142 return r;
1143}
1144
1145int kvm_arch_init(void *opaque)
1146{
Sanjay Lal669e8462012-11-21 18:34:02 -08001147 if (kvm_mips_callbacks) {
1148 kvm_err("kvm: module already exists\n");
1149 return -EEXIST;
1150 }
1151
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001152 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001153}
1154
1155void kvm_arch_exit(void)
1156{
1157 kvm_mips_callbacks = NULL;
1158}
1159
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001160int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1161 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001162{
David Daneyed829852013-05-23 09:49:10 -07001163 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001164}
1165
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001166int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1167 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001168{
David Daneyed829852013-05-23 09:49:10 -07001169 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001170}
1171
Dominik Dingel31928aa2014-12-04 15:47:07 +01001172void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001173{
Sanjay Lal669e8462012-11-21 18:34:02 -08001174}
1175
1176int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1177{
David Daneyed829852013-05-23 09:49:10 -07001178 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001179}
1180
1181int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1182{
David Daneyed829852013-05-23 09:49:10 -07001183 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001184}
1185
1186int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1187{
1188 return VM_FAULT_SIGBUS;
1189}
1190
Alexander Graf784aa3d2014-07-14 18:27:35 +02001191int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001192{
1193 int r;
1194
1195 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001196 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001197 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001198 r = 1;
1199 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001200 case KVM_CAP_COALESCED_MMIO:
1201 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1202 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001203 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001204 /* We don't handle systems with inconsistent cpu_has_fpu */
1205 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001206 break;
James Hogand952bd02014-12-08 23:07:56 +00001207 case KVM_CAP_MIPS_MSA:
1208 /*
1209 * We don't support MSA vector partitioning yet:
1210 * 1) It would require explicit support which can't be tested
1211 * yet due to lack of support in current hardware.
1212 * 2) It extends the state that would need to be saved/restored
1213 * by e.g. QEMU for migration.
1214 *
1215 * When vector partitioning hardware becomes available, support
1216 * could be added by requiring a flag when enabling
1217 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1218 * to save/restore the appropriate extra state.
1219 */
1220 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1221 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001222 default:
1223 r = 0;
1224 break;
1225 }
1226 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001227}
1228
1229int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1230{
1231 return kvm_mips_pending_timer(vcpu);
1232}
1233
1234int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1235{
1236 int i;
1237 struct mips_coproc *cop0;
1238
1239 if (!vcpu)
1240 return -1;
1241
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001242 kvm_debug("VCPU Register Dump:\n");
1243 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1244 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001245
1246 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001247 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001248 vcpu->arch.gprs[i],
1249 vcpu->arch.gprs[i + 1],
1250 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1251 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001252 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1253 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001254
1255 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001256 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1257 kvm_read_c0_guest_status(cop0),
1258 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001259
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001260 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001261
1262 return 0;
1263}
1264
1265int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1266{
1267 int i;
1268
David Daney8d17dd02013-05-23 09:49:08 -07001269 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001270 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001271 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001272 vcpu->arch.hi = regs->hi;
1273 vcpu->arch.lo = regs->lo;
1274 vcpu->arch.pc = regs->pc;
1275
David Daney4c73fb22013-05-23 09:49:09 -07001276 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001277}
1278
1279int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1280{
1281 int i;
1282
David Daney8d17dd02013-05-23 09:49:08 -07001283 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001284 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001285
1286 regs->hi = vcpu->arch.hi;
1287 regs->lo = vcpu->arch.lo;
1288 regs->pc = vcpu->arch.pc;
1289
David Daney4c73fb22013-05-23 09:49:09 -07001290 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001291}
1292
James Hogan0fae34f2014-05-29 10:16:39 +01001293static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001294{
1295 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1296
1297 kvm_mips_callbacks->queue_timer_int(vcpu);
1298
1299 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001300 if (swait_active(&vcpu->wq))
1301 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001302}
1303
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001304/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001305static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001306{
1307 struct kvm_vcpu *vcpu;
1308
1309 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1310 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001311 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001312}
1313
1314int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1315{
1316 kvm_mips_callbacks->vcpu_init(vcpu);
1317 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1318 HRTIMER_MODE_REL);
1319 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001320 return 0;
1321}
1322
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001323int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1324 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001325{
1326 return 0;
1327}
1328
1329/* Initial guest state */
1330int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1331{
1332 return kvm_mips_callbacks->vcpu_setup(vcpu);
1333}
1334
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001335static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001336{
James Hogan8cffd192016-06-09 14:19:08 +01001337 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001338
Sanjay Lal669e8462012-11-21 18:34:02 -08001339 if (cpu_has_dsp)
1340 status |= (ST0_MX);
1341
1342 write_c0_status(status);
1343 ehb();
1344}
1345
1346/*
1347 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1348 */
1349int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1350{
James Hogan8cffd192016-06-09 14:19:08 +01001351 u32 cause = vcpu->arch.host_cp0_cause;
1352 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1353 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001354 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1355 enum emulation_result er = EMULATE_DONE;
1356 int ret = RESUME_GUEST;
1357
James Hoganc4c6f2c2015-02-04 10:52:03 +00001358 /* re-enable HTW before enabling interrupts */
1359 htw_start();
1360
Sanjay Lal669e8462012-11-21 18:34:02 -08001361 /* Set a default exit reason */
1362 run->exit_reason = KVM_EXIT_UNKNOWN;
1363 run->ready_for_interrupt_injection = 1;
1364
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001365 /*
1366 * Set the appropriate status bits based on host CPU features,
1367 * before we hit the scheduler
1368 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001369 kvm_mips_set_c0_status();
1370
1371 local_irq_enable();
1372
1373 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1374 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001375 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001376
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001377 /*
1378 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001379 * causing an exception to be delivered to the Guest Kernel
1380 */
1381 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1382 if (er == EMULATE_PRIV_FAIL) {
1383 goto skip_emul;
1384 } else if (er == EMULATE_FAIL) {
1385 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1386 ret = RESUME_HOST;
1387 goto skip_emul;
1388 }
1389
1390 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001391 case EXCCODE_INT:
1392 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001393
1394 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001395
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001396 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001397 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001398
1399 ret = RESUME_GUEST;
1400 break;
1401
James Hogan16d100db2015-12-16 23:49:33 +00001402 case EXCCODE_CPU:
1403 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001404
1405 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001406 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1407 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001408 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001409 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001410 break;
1411
James Hogan16d100db2015-12-16 23:49:33 +00001412 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001413 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001414 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1415 break;
1416
James Hogan16d100db2015-12-16 23:49:33 +00001417 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001418 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1419 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1420 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001421
1422 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001423 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1424 break;
1425
James Hogan16d100db2015-12-16 23:49:33 +00001426 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001427 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1428 cause, opc, badvaddr);
1429
1430 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001431 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1432 break;
1433
James Hogan16d100db2015-12-16 23:49:33 +00001434 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001435 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001436 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1437 break;
1438
James Hogan16d100db2015-12-16 23:49:33 +00001439 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001440 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001441 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1442 break;
1443
James Hogan16d100db2015-12-16 23:49:33 +00001444 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001445 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001446 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1447 break;
1448
James Hogan16d100db2015-12-16 23:49:33 +00001449 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001450 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001451 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1452 break;
1453
James Hogan16d100db2015-12-16 23:49:33 +00001454 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001455 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001456 ret = kvm_mips_callbacks->handle_break(vcpu);
1457 break;
1458
James Hogan16d100db2015-12-16 23:49:33 +00001459 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001460 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001461 ret = kvm_mips_callbacks->handle_trap(vcpu);
1462 break;
1463
James Hogan16d100db2015-12-16 23:49:33 +00001464 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001465 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001466 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1467 break;
1468
James Hogan16d100db2015-12-16 23:49:33 +00001469 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001470 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001471 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1472 break;
1473
James Hogan16d100db2015-12-16 23:49:33 +00001474 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001475 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001476 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1477 break;
1478
Sanjay Lal669e8462012-11-21 18:34:02 -08001479 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001480 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1481 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1482 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001483 kvm_arch_vcpu_dump_regs(vcpu);
1484 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1485 ret = RESUME_HOST;
1486 break;
1487
1488 }
1489
1490skip_emul:
1491 local_irq_disable();
1492
1493 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1494 kvm_mips_deliver_interrupts(vcpu, cause);
1495
1496 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001497 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001498 if (signal_pending(current)) {
1499 run->exit_reason = KVM_EXIT_INTR;
1500 ret = (-EINTR << 2) | RESUME_HOST;
1501 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001502 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001503 }
1504 }
1505
James Hogan98e91b82014-11-18 14:09:12 +00001506 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001507 trace_kvm_reenter(vcpu);
1508
James Hogan98e91b82014-11-18 14:09:12 +00001509 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001510 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1511 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001512 *
1513 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001514 * vector, as it may well cause an [MSA] FP exception if there
1515 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001516 * kvm_mips_csr_die_notifier() for how that is handled).
1517 */
1518 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1519 read_c0_status() & ST0_CU1)
1520 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001521
1522 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1523 read_c0_config5() & MIPS_CONF5_MSAEN)
1524 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001525 }
1526
James Hoganc4c6f2c2015-02-04 10:52:03 +00001527 /* Disable HTW before returning to guest or host */
1528 htw_stop();
1529
Sanjay Lal669e8462012-11-21 18:34:02 -08001530 return ret;
1531}
1532
James Hogan98e91b82014-11-18 14:09:12 +00001533/* Enable FPU for guest and restore context */
1534void kvm_own_fpu(struct kvm_vcpu *vcpu)
1535{
1536 struct mips_coproc *cop0 = vcpu->arch.cop0;
1537 unsigned int sr, cfg5;
1538
1539 preempt_disable();
1540
James Hogan539cb89fb2015-03-05 11:43:36 +00001541 sr = kvm_read_c0_guest_status(cop0);
1542
1543 /*
1544 * If MSA state is already live, it is undefined how it interacts with
1545 * FR=0 FPU state, and we don't want to hit reserved instruction
1546 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1547 * play it safe and save it first.
1548 *
1549 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1550 * get called when guest CU1 is set, however we can't trust the guest
1551 * not to clobber the status register directly via the commpage.
1552 */
1553 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001554 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001555 kvm_lose_fpu(vcpu);
1556
James Hogan98e91b82014-11-18 14:09:12 +00001557 /*
1558 * Enable FPU for guest
1559 * We set FR and FRE according to guest context
1560 */
James Hogan98e91b82014-11-18 14:09:12 +00001561 change_c0_status(ST0_CU1 | ST0_FR, sr);
1562 if (cpu_has_fre) {
1563 cfg5 = kvm_read_c0_guest_config5(cop0);
1564 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1565 }
1566 enable_fpu_hazard();
1567
1568 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001569 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001570 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001571 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001572 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1573 } else {
1574 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001575 }
1576
1577 preempt_enable();
1578}
1579
James Hogan539cb89fb2015-03-05 11:43:36 +00001580#ifdef CONFIG_CPU_HAS_MSA
1581/* Enable MSA for guest and restore context */
1582void kvm_own_msa(struct kvm_vcpu *vcpu)
1583{
1584 struct mips_coproc *cop0 = vcpu->arch.cop0;
1585 unsigned int sr, cfg5;
1586
1587 preempt_disable();
1588
1589 /*
1590 * Enable FPU if enabled in guest, since we're restoring FPU context
1591 * anyway. We set FR and FRE according to guest context.
1592 */
1593 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1594 sr = kvm_read_c0_guest_status(cop0);
1595
1596 /*
1597 * If FR=0 FPU state is already live, it is undefined how it
1598 * interacts with MSA state, so play it safe and save it first.
1599 */
1600 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001601 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1602 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001603 kvm_lose_fpu(vcpu);
1604
1605 change_c0_status(ST0_CU1 | ST0_FR, sr);
1606 if (sr & ST0_CU1 && cpu_has_fre) {
1607 cfg5 = kvm_read_c0_guest_config5(cop0);
1608 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1609 }
1610 }
1611
1612 /* Enable MSA for guest */
1613 set_c0_config5(MIPS_CONF5_MSAEN);
1614 enable_fpu_hazard();
1615
James Hoganf9431762016-06-14 09:40:10 +01001616 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1617 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001618 /*
1619 * Guest FPU state already loaded, only restore upper MSA state
1620 */
1621 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001622 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001623 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001624 break;
1625 case 0:
1626 /* Neither FPU or MSA already active, restore full MSA state */
1627 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001628 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001629 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001630 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001631 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1632 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001633 break;
1634 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001635 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001636 break;
1637 }
1638
1639 preempt_enable();
1640}
1641#endif
1642
1643/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001644void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1645{
1646 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001647 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001648 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001649 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001650 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001651 }
James Hoganf9431762016-06-14 09:40:10 +01001652 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001653 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001654 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001655 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001656 }
1657 preempt_enable();
1658}
1659
James Hogan539cb89fb2015-03-05 11:43:36 +00001660/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001661void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1662{
1663 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001664 * FPU & MSA get disabled in root context (hardware) when it is disabled
1665 * in guest context (software), but the register state in the hardware
1666 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001667 * before saving.
1668 */
1669
1670 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001671 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001672 set_c0_config5(MIPS_CONF5_MSAEN);
1673 enable_fpu_hazard();
1674
1675 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001676 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001677
1678 /* Disable MSA & FPU */
1679 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001680 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001681 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001682 disable_fpu_hazard();
1683 }
James Hoganf9431762016-06-14 09:40:10 +01001684 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1685 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001686 set_c0_status(ST0_CU1);
1687 enable_fpu_hazard();
1688
1689 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001690 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001691 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001692
1693 /* Disable FPU */
1694 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001695 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001696 }
1697 preempt_enable();
1698}
1699
1700/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001701 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1702 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1703 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001704 */
1705static int kvm_mips_csr_die_notify(struct notifier_block *self,
1706 unsigned long cmd, void *ptr)
1707{
1708 struct die_args *args = (struct die_args *)ptr;
1709 struct pt_regs *regs = args->regs;
1710 unsigned long pc;
1711
James Hogan539cb89fb2015-03-05 11:43:36 +00001712 /* Only interested in FPE and MSAFPE */
1713 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001714 return NOTIFY_DONE;
1715
1716 /* Return immediately if guest context isn't active */
1717 if (!(current->flags & PF_VCPU))
1718 return NOTIFY_DONE;
1719
1720 /* Should never get here from user mode */
1721 BUG_ON(user_mode(regs));
1722
1723 pc = instruction_pointer(regs);
1724 switch (cmd) {
1725 case DIE_FP:
1726 /* match 2nd instruction in __kvm_restore_fcsr */
1727 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1728 return NOTIFY_DONE;
1729 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001730 case DIE_MSAFP:
1731 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1732 if (!cpu_has_msa ||
1733 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1734 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1735 return NOTIFY_DONE;
1736 break;
James Hogan98e91b82014-11-18 14:09:12 +00001737 }
1738
1739 /* Move PC forward a little and continue executing */
1740 instruction_pointer(regs) += 4;
1741
1742 return NOTIFY_STOP;
1743}
1744
1745static struct notifier_block kvm_mips_csr_die_notifier = {
1746 .notifier_call = kvm_mips_csr_die_notify,
1747};
1748
James Hogan2db9d232015-12-16 23:49:32 +00001749static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001750{
1751 int ret;
1752
1753 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1754
1755 if (ret)
1756 return ret;
1757
James Hogan98e91b82014-11-18 14:09:12 +00001758 register_die_notifier(&kvm_mips_csr_die_notifier);
1759
Sanjay Lal669e8462012-11-21 18:34:02 -08001760 return 0;
1761}
1762
James Hogan2db9d232015-12-16 23:49:32 +00001763static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001764{
1765 kvm_exit();
1766
James Hogan98e91b82014-11-18 14:09:12 +00001767 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001768}
1769
1770module_init(kvm_mips_init);
1771module_exit(kvm_mips_exit);
1772
1773EXPORT_TRACEPOINT_SYMBOL(kvm_exit);