blob: 5a2b9034a05ce1fa7f4772e9c58a097c315bcca2 [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{
Sanjay Lal669e8462012-11-21 18:34:02 -0800250 int err, size, offset;
251 void *gebase;
252 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
289 /* Copy L1 Guest Exception handler to correct offset */
290
291 /* TLB Refill, EXL = 0 */
292 memcpy(gebase, mips32_exception,
293 mips32_exceptionEnd - mips32_exception);
294
295 /* General Exception Entry point */
296 memcpy(gebase + 0x180, mips32_exception,
297 mips32_exceptionEnd - mips32_exception);
298
299 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
300 for (i = 0; i < 8; i++) {
301 kvm_debug("L1 Vectored handler @ %p\n",
302 gebase + 0x200 + (i * VECTORSPACING));
303 memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception,
304 mips32_exceptionEnd - mips32_exception);
305 }
306
307 /* General handler, relocate to unmapped space for sanity's sake */
308 offset = 0x2000;
James Hogan6e95bfd2014-05-29 10:16:43 +0100309 kvm_debug("Installing KVM Exception handlers @ %p, %#x bytes\n",
310 gebase + offset,
311 mips32_GuestExceptionEnd - mips32_GuestException);
Sanjay Lal669e8462012-11-21 18:34:02 -0800312
313 memcpy(gebase + offset, mips32_GuestException,
314 mips32_GuestExceptionEnd - mips32_GuestException);
315
James Hogan797179b2016-06-09 10:50:43 +0100316#ifdef MODULE
317 offset += mips32_GuestExceptionEnd - mips32_GuestException;
318 memcpy(gebase + offset, (char *)__kvm_mips_vcpu_run,
319 __kvm_mips_vcpu_run_end - (char *)__kvm_mips_vcpu_run);
320 vcpu->arch.vcpu_run = gebase + offset;
321#else
322 vcpu->arch.vcpu_run = __kvm_mips_vcpu_run;
323#endif
324
Sanjay Lal669e8462012-11-21 18:34:02 -0800325 /* Invalidate the icache for these ranges */
James Hoganfacaaec2014-05-29 10:16:25 +0100326 local_flush_icache_range((unsigned long)gebase,
327 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800328
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700329 /*
330 * Allocate comm page for guest kernel, a TLB will be reserved for
331 * mapping GVA @ 0xFFFF8000 to this page
332 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800333 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
334
335 if (!vcpu->arch.kseg0_commpage) {
336 err = -ENOMEM;
337 goto out_free_gebase;
338 }
339
James Hogan6e95bfd2014-05-29 10:16:43 +0100340 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800341 kvm_mips_commpage_init(vcpu);
342
343 /* Init */
344 vcpu->arch.last_sched_cpu = -1;
345
346 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100347 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800348
349 return vcpu;
350
351out_free_gebase:
352 kfree(gebase);
353
James Hogan585bb8f2015-11-11 14:21:20 +0000354out_uninit_cpu:
355 kvm_vcpu_uninit(vcpu);
356
Sanjay Lal669e8462012-11-21 18:34:02 -0800357out_free_cpu:
358 kfree(vcpu);
359
360out:
361 return ERR_PTR(err);
362}
363
364void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
365{
366 hrtimer_cancel(&vcpu->arch.comparecount_timer);
367
368 kvm_vcpu_uninit(vcpu);
369
370 kvm_mips_dump_stats(vcpu);
371
James Hoganc6c0a662014-05-29 10:16:44 +0100372 kfree(vcpu->arch.guest_ebase);
373 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700374 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800375}
376
377void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
378{
379 kvm_arch_vcpu_free(vcpu);
380}
381
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700382int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
383 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800384{
David Daneyed829852013-05-23 09:49:10 -0700385 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800386}
387
388int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
389{
390 int r = 0;
391 sigset_t sigsaved;
392
393 if (vcpu->sigset_active)
394 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
395
396 if (vcpu->mmio_needed) {
397 if (!vcpu->mmio_is_write)
398 kvm_mips_complete_mmio_load(vcpu, run);
399 vcpu->mmio_needed = 0;
400 }
401
James Hoganf7982172015-02-04 17:06:37 +0000402 lose_fpu(1);
403
James Hogan044f0f02014-05-29 10:16:32 +0100404 local_irq_disable();
Sanjay Lal669e8462012-11-21 18:34:02 -0800405 /* Check if we have any exceptions/interrupts pending */
406 kvm_mips_deliver_interrupts(vcpu,
407 kvm_read_c0_guest_cause(vcpu->arch.cop0));
408
Christian Borntraegerccf73aaf2015-04-30 13:43:31 +0200409 __kvm_guest_enter();
Sanjay Lal669e8462012-11-21 18:34:02 -0800410
James Hoganc4c6f2c2015-02-04 10:52:03 +0000411 /* Disable hardware page table walking while in guest */
412 htw_stop();
413
James Hogan93258602016-06-14 09:40:14 +0100414 trace_kvm_enter(vcpu);
James Hogan797179b2016-06-09 10:50:43 +0100415 r = vcpu->arch.vcpu_run(run, vcpu);
James Hogan93258602016-06-14 09:40:14 +0100416 trace_kvm_out(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800417
James Hoganc4c6f2c2015-02-04 10:52:03 +0000418 /* Re-enable HTW before enabling interrupts */
419 htw_start();
420
Christian Borntraegerccf73aaf2015-04-30 13:43:31 +0200421 __kvm_guest_exit();
Sanjay Lal669e8462012-11-21 18:34:02 -0800422 local_irq_enable();
423
424 if (vcpu->sigset_active)
425 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
426
427 return r;
428}
429
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700430int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
431 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800432{
433 int intr = (int)irq->irq;
434 struct kvm_vcpu *dvcpu = NULL;
435
436 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
437 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
438 (int)intr);
439
440 if (irq->cpu == -1)
441 dvcpu = vcpu;
442 else
443 dvcpu = vcpu->kvm->vcpus[irq->cpu];
444
445 if (intr == 2 || intr == 3 || intr == 4) {
446 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
447
448 } else if (intr == -2 || intr == -3 || intr == -4) {
449 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
450 } else {
451 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
452 irq->cpu, irq->irq);
453 return -EINVAL;
454 }
455
456 dvcpu->arch.wait = 0;
457
Marcelo Tosatti85773702016-02-19 09:46:39 +0100458 if (swait_active(&dvcpu->wq))
459 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800460
461 return 0;
462}
463
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700464int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
465 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800466{
David Daneyed829852013-05-23 09:49:10 -0700467 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800468}
469
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700470int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
471 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800472{
David Daneyed829852013-05-23 09:49:10 -0700473 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800474}
475
David Daney4c73fb22013-05-23 09:49:09 -0700476static u64 kvm_mips_get_one_regs[] = {
477 KVM_REG_MIPS_R0,
478 KVM_REG_MIPS_R1,
479 KVM_REG_MIPS_R2,
480 KVM_REG_MIPS_R3,
481 KVM_REG_MIPS_R4,
482 KVM_REG_MIPS_R5,
483 KVM_REG_MIPS_R6,
484 KVM_REG_MIPS_R7,
485 KVM_REG_MIPS_R8,
486 KVM_REG_MIPS_R9,
487 KVM_REG_MIPS_R10,
488 KVM_REG_MIPS_R11,
489 KVM_REG_MIPS_R12,
490 KVM_REG_MIPS_R13,
491 KVM_REG_MIPS_R14,
492 KVM_REG_MIPS_R15,
493 KVM_REG_MIPS_R16,
494 KVM_REG_MIPS_R17,
495 KVM_REG_MIPS_R18,
496 KVM_REG_MIPS_R19,
497 KVM_REG_MIPS_R20,
498 KVM_REG_MIPS_R21,
499 KVM_REG_MIPS_R22,
500 KVM_REG_MIPS_R23,
501 KVM_REG_MIPS_R24,
502 KVM_REG_MIPS_R25,
503 KVM_REG_MIPS_R26,
504 KVM_REG_MIPS_R27,
505 KVM_REG_MIPS_R28,
506 KVM_REG_MIPS_R29,
507 KVM_REG_MIPS_R30,
508 KVM_REG_MIPS_R31,
509
510 KVM_REG_MIPS_HI,
511 KVM_REG_MIPS_LO,
512 KVM_REG_MIPS_PC,
513
514 KVM_REG_MIPS_CP0_INDEX,
515 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100516 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700517 KVM_REG_MIPS_CP0_PAGEMASK,
518 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100519 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700520 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100521 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700522 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100523 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700524 KVM_REG_MIPS_CP0_STATUS,
525 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100526 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100527 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700528 KVM_REG_MIPS_CP0_CONFIG,
529 KVM_REG_MIPS_CP0_CONFIG1,
530 KVM_REG_MIPS_CP0_CONFIG2,
531 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100532 KVM_REG_MIPS_CP0_CONFIG4,
533 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700534 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100535 KVM_REG_MIPS_CP0_ERROREPC,
536
537 KVM_REG_MIPS_COUNT_CTL,
538 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100539 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700540};
541
James Hogane5775932016-06-15 19:29:51 +0100542static u64 kvm_mips_get_one_regs_fpu[] = {
543 KVM_REG_MIPS_FCR_IR,
544 KVM_REG_MIPS_FCR_CSR,
545};
546
547static u64 kvm_mips_get_one_regs_msa[] = {
548 KVM_REG_MIPS_MSA_IR,
549 KVM_REG_MIPS_MSA_CSR,
550};
551
James Hogan05108702016-06-15 19:29:56 +0100552static u64 kvm_mips_get_one_regs_kscratch[] = {
553 KVM_REG_MIPS_CP0_KSCRATCH1,
554 KVM_REG_MIPS_CP0_KSCRATCH2,
555 KVM_REG_MIPS_CP0_KSCRATCH3,
556 KVM_REG_MIPS_CP0_KSCRATCH4,
557 KVM_REG_MIPS_CP0_KSCRATCH5,
558 KVM_REG_MIPS_CP0_KSCRATCH6,
559};
560
James Hoganf5c43bd2016-06-15 19:29:49 +0100561static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
562{
563 unsigned long ret;
564
565 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100566 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
567 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
568 /* odd doubles */
569 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
570 ret += 16;
571 }
572 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
573 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hogan05108702016-06-15 19:29:56 +0100574 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
James Hoganf5c43bd2016-06-15 19:29:49 +0100575 ret += kvm_mips_callbacks->num_regs(vcpu);
576
577 return ret;
578}
579
580static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
581{
James Hogane5775932016-06-15 19:29:51 +0100582 u64 index;
583 unsigned int i;
584
James Hoganf5c43bd2016-06-15 19:29:49 +0100585 if (copy_to_user(indices, kvm_mips_get_one_regs,
586 sizeof(kvm_mips_get_one_regs)))
587 return -EFAULT;
588 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
589
James Hogane5775932016-06-15 19:29:51 +0100590 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
591 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
592 sizeof(kvm_mips_get_one_regs_fpu)))
593 return -EFAULT;
594 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
595
596 for (i = 0; i < 32; ++i) {
597 index = KVM_REG_MIPS_FPR_32(i);
598 if (copy_to_user(indices, &index, sizeof(index)))
599 return -EFAULT;
600 ++indices;
601
602 /* skip odd doubles if no F64 */
603 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
604 continue;
605
606 index = KVM_REG_MIPS_FPR_64(i);
607 if (copy_to_user(indices, &index, sizeof(index)))
608 return -EFAULT;
609 ++indices;
610 }
611 }
612
613 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
614 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
615 sizeof(kvm_mips_get_one_regs_msa)))
616 return -EFAULT;
617 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
618
619 for (i = 0; i < 32; ++i) {
620 index = KVM_REG_MIPS_VEC_128(i);
621 if (copy_to_user(indices, &index, sizeof(index)))
622 return -EFAULT;
623 ++indices;
624 }
625 }
626
James Hogan05108702016-06-15 19:29:56 +0100627 for (i = 0; i < 6; ++i) {
628 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
629 continue;
630
631 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
632 sizeof(kvm_mips_get_one_regs_kscratch[i])))
633 return -EFAULT;
634 ++indices;
635 }
636
James Hoganf5c43bd2016-06-15 19:29:49 +0100637 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
638}
639
David Daney4c73fb22013-05-23 09:49:09 -0700640static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
641 const struct kvm_one_reg *reg)
642{
David Daney4c73fb22013-05-23 09:49:09 -0700643 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000644 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100645 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700646 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000647 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000648 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700649
650 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000651 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700652 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
653 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
654 break;
655 case KVM_REG_MIPS_HI:
656 v = (long)vcpu->arch.hi;
657 break;
658 case KVM_REG_MIPS_LO:
659 v = (long)vcpu->arch.lo;
660 break;
661 case KVM_REG_MIPS_PC:
662 v = (long)vcpu->arch.pc;
663 break;
664
James Hogan379245c2014-12-02 15:48:24 +0000665 /* Floating point registers */
666 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
667 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
668 return -EINVAL;
669 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
670 /* Odd singles in top of even double when FR=0 */
671 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
672 v = get_fpr32(&fpu->fpr[idx], 0);
673 else
674 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
675 break;
676 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
677 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
678 return -EINVAL;
679 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
680 /* Can't access odd doubles in FR=0 mode */
681 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
682 return -EINVAL;
683 v = get_fpr64(&fpu->fpr[idx], 0);
684 break;
685 case KVM_REG_MIPS_FCR_IR:
686 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
687 return -EINVAL;
688 v = boot_cpu_data.fpu_id;
689 break;
690 case KVM_REG_MIPS_FCR_CSR:
691 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
692 return -EINVAL;
693 v = fpu->fcr31;
694 break;
695
James Hoganab86bd62014-12-02 15:48:24 +0000696 /* MIPS SIMD Architecture (MSA) registers */
697 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
698 if (!kvm_mips_guest_has_msa(&vcpu->arch))
699 return -EINVAL;
700 /* Can't access MSA registers in FR=0 mode */
701 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
702 return -EINVAL;
703 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
704#ifdef CONFIG_CPU_LITTLE_ENDIAN
705 /* least significant byte first */
706 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
707 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
708#else
709 /* most significant byte first */
710 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
711 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
712#endif
713 break;
714 case KVM_REG_MIPS_MSA_IR:
715 if (!kvm_mips_guest_has_msa(&vcpu->arch))
716 return -EINVAL;
717 v = boot_cpu_data.msa_id;
718 break;
719 case KVM_REG_MIPS_MSA_CSR:
720 if (!kvm_mips_guest_has_msa(&vcpu->arch))
721 return -EINVAL;
722 v = fpu->msacsr;
723 break;
724
James Hogan379245c2014-12-02 15:48:24 +0000725 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700726 case KVM_REG_MIPS_CP0_INDEX:
727 v = (long)kvm_read_c0_guest_index(cop0);
728 break;
729 case KVM_REG_MIPS_CP0_CONTEXT:
730 v = (long)kvm_read_c0_guest_context(cop0);
731 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100732 case KVM_REG_MIPS_CP0_USERLOCAL:
733 v = (long)kvm_read_c0_guest_userlocal(cop0);
734 break;
David Daney4c73fb22013-05-23 09:49:09 -0700735 case KVM_REG_MIPS_CP0_PAGEMASK:
736 v = (long)kvm_read_c0_guest_pagemask(cop0);
737 break;
738 case KVM_REG_MIPS_CP0_WIRED:
739 v = (long)kvm_read_c0_guest_wired(cop0);
740 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100741 case KVM_REG_MIPS_CP0_HWRENA:
742 v = (long)kvm_read_c0_guest_hwrena(cop0);
743 break;
David Daney4c73fb22013-05-23 09:49:09 -0700744 case KVM_REG_MIPS_CP0_BADVADDR:
745 v = (long)kvm_read_c0_guest_badvaddr(cop0);
746 break;
747 case KVM_REG_MIPS_CP0_ENTRYHI:
748 v = (long)kvm_read_c0_guest_entryhi(cop0);
749 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100750 case KVM_REG_MIPS_CP0_COMPARE:
751 v = (long)kvm_read_c0_guest_compare(cop0);
752 break;
David Daney4c73fb22013-05-23 09:49:09 -0700753 case KVM_REG_MIPS_CP0_STATUS:
754 v = (long)kvm_read_c0_guest_status(cop0);
755 break;
756 case KVM_REG_MIPS_CP0_CAUSE:
757 v = (long)kvm_read_c0_guest_cause(cop0);
758 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100759 case KVM_REG_MIPS_CP0_EPC:
760 v = (long)kvm_read_c0_guest_epc(cop0);
761 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100762 case KVM_REG_MIPS_CP0_PRID:
763 v = (long)kvm_read_c0_guest_prid(cop0);
764 break;
David Daney4c73fb22013-05-23 09:49:09 -0700765 case KVM_REG_MIPS_CP0_CONFIG:
766 v = (long)kvm_read_c0_guest_config(cop0);
767 break;
768 case KVM_REG_MIPS_CP0_CONFIG1:
769 v = (long)kvm_read_c0_guest_config1(cop0);
770 break;
771 case KVM_REG_MIPS_CP0_CONFIG2:
772 v = (long)kvm_read_c0_guest_config2(cop0);
773 break;
774 case KVM_REG_MIPS_CP0_CONFIG3:
775 v = (long)kvm_read_c0_guest_config3(cop0);
776 break;
James Hoganc7716072014-06-26 15:11:29 +0100777 case KVM_REG_MIPS_CP0_CONFIG4:
778 v = (long)kvm_read_c0_guest_config4(cop0);
779 break;
780 case KVM_REG_MIPS_CP0_CONFIG5:
781 v = (long)kvm_read_c0_guest_config5(cop0);
782 break;
David Daney4c73fb22013-05-23 09:49:09 -0700783 case KVM_REG_MIPS_CP0_CONFIG7:
784 v = (long)kvm_read_c0_guest_config7(cop0);
785 break;
James Hogane93d4c12014-06-26 13:47:22 +0100786 case KVM_REG_MIPS_CP0_ERROREPC:
787 v = (long)kvm_read_c0_guest_errorepc(cop0);
788 break;
James Hogan05108702016-06-15 19:29:56 +0100789 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
790 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
791 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
792 return -EINVAL;
793 switch (idx) {
794 case 2:
795 v = (long)kvm_read_c0_guest_kscratch1(cop0);
796 break;
797 case 3:
798 v = (long)kvm_read_c0_guest_kscratch2(cop0);
799 break;
800 case 4:
801 v = (long)kvm_read_c0_guest_kscratch3(cop0);
802 break;
803 case 5:
804 v = (long)kvm_read_c0_guest_kscratch4(cop0);
805 break;
806 case 6:
807 v = (long)kvm_read_c0_guest_kscratch5(cop0);
808 break;
809 case 7:
810 v = (long)kvm_read_c0_guest_kscratch6(cop0);
811 break;
812 }
813 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100814 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100815 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100816 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
817 if (ret)
818 return ret;
819 break;
David Daney4c73fb22013-05-23 09:49:09 -0700820 }
David Daney681865d2013-06-10 12:33:48 -0700821 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
822 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700823
David Daney681865d2013-06-10 12:33:48 -0700824 return put_user(v, uaddr64);
825 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
826 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
827 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700828
David Daney681865d2013-06-10 12:33:48 -0700829 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000830 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
831 void __user *uaddr = (void __user *)(long)reg->addr;
832
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200833 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700834 } else {
835 return -EINVAL;
836 }
David Daney4c73fb22013-05-23 09:49:09 -0700837}
838
839static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
840 const struct kvm_one_reg *reg)
841{
David Daney4c73fb22013-05-23 09:49:09 -0700842 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000843 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
844 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000845 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000846 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700847
David Daney681865d2013-06-10 12:33:48 -0700848 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
849 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
850
851 if (get_user(v, uaddr64) != 0)
852 return -EFAULT;
853 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
854 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
855 s32 v32;
856
857 if (get_user(v32, uaddr32) != 0)
858 return -EFAULT;
859 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000860 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
861 void __user *uaddr = (void __user *)(long)reg->addr;
862
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200863 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700864 } else {
865 return -EINVAL;
866 }
David Daney4c73fb22013-05-23 09:49:09 -0700867
868 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000869 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700870 case KVM_REG_MIPS_R0:
871 /* Silently ignore requests to set $0 */
872 break;
873 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
874 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
875 break;
876 case KVM_REG_MIPS_HI:
877 vcpu->arch.hi = v;
878 break;
879 case KVM_REG_MIPS_LO:
880 vcpu->arch.lo = v;
881 break;
882 case KVM_REG_MIPS_PC:
883 vcpu->arch.pc = v;
884 break;
885
James Hogan379245c2014-12-02 15:48:24 +0000886 /* Floating point registers */
887 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
888 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
889 return -EINVAL;
890 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
891 /* Odd singles in top of even double when FR=0 */
892 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
893 set_fpr32(&fpu->fpr[idx], 0, v);
894 else
895 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
896 break;
897 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
898 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
899 return -EINVAL;
900 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
901 /* Can't access odd doubles in FR=0 mode */
902 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
903 return -EINVAL;
904 set_fpr64(&fpu->fpr[idx], 0, v);
905 break;
906 case KVM_REG_MIPS_FCR_IR:
907 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
908 return -EINVAL;
909 /* Read-only */
910 break;
911 case KVM_REG_MIPS_FCR_CSR:
912 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
913 return -EINVAL;
914 fpu->fcr31 = v;
915 break;
916
James Hoganab86bd62014-12-02 15:48:24 +0000917 /* MIPS SIMD Architecture (MSA) registers */
918 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
919 if (!kvm_mips_guest_has_msa(&vcpu->arch))
920 return -EINVAL;
921 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
922#ifdef CONFIG_CPU_LITTLE_ENDIAN
923 /* least significant byte first */
924 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
925 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
926#else
927 /* most significant byte first */
928 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
929 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
930#endif
931 break;
932 case KVM_REG_MIPS_MSA_IR:
933 if (!kvm_mips_guest_has_msa(&vcpu->arch))
934 return -EINVAL;
935 /* Read-only */
936 break;
937 case KVM_REG_MIPS_MSA_CSR:
938 if (!kvm_mips_guest_has_msa(&vcpu->arch))
939 return -EINVAL;
940 fpu->msacsr = v;
941 break;
942
James Hogan379245c2014-12-02 15:48:24 +0000943 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700944 case KVM_REG_MIPS_CP0_INDEX:
945 kvm_write_c0_guest_index(cop0, v);
946 break;
947 case KVM_REG_MIPS_CP0_CONTEXT:
948 kvm_write_c0_guest_context(cop0, v);
949 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100950 case KVM_REG_MIPS_CP0_USERLOCAL:
951 kvm_write_c0_guest_userlocal(cop0, v);
952 break;
David Daney4c73fb22013-05-23 09:49:09 -0700953 case KVM_REG_MIPS_CP0_PAGEMASK:
954 kvm_write_c0_guest_pagemask(cop0, v);
955 break;
956 case KVM_REG_MIPS_CP0_WIRED:
957 kvm_write_c0_guest_wired(cop0, v);
958 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100959 case KVM_REG_MIPS_CP0_HWRENA:
960 kvm_write_c0_guest_hwrena(cop0, v);
961 break;
David Daney4c73fb22013-05-23 09:49:09 -0700962 case KVM_REG_MIPS_CP0_BADVADDR:
963 kvm_write_c0_guest_badvaddr(cop0, v);
964 break;
965 case KVM_REG_MIPS_CP0_ENTRYHI:
966 kvm_write_c0_guest_entryhi(cop0, v);
967 break;
968 case KVM_REG_MIPS_CP0_STATUS:
969 kvm_write_c0_guest_status(cop0, v);
970 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100971 case KVM_REG_MIPS_CP0_EPC:
972 kvm_write_c0_guest_epc(cop0, v);
973 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100974 case KVM_REG_MIPS_CP0_PRID:
975 kvm_write_c0_guest_prid(cop0, v);
976 break;
David Daney4c73fb22013-05-23 09:49:09 -0700977 case KVM_REG_MIPS_CP0_ERROREPC:
978 kvm_write_c0_guest_errorepc(cop0, v);
979 break;
James Hogan05108702016-06-15 19:29:56 +0100980 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
981 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
982 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
983 return -EINVAL;
984 switch (idx) {
985 case 2:
986 kvm_write_c0_guest_kscratch1(cop0, v);
987 break;
988 case 3:
989 kvm_write_c0_guest_kscratch2(cop0, v);
990 break;
991 case 4:
992 kvm_write_c0_guest_kscratch3(cop0, v);
993 break;
994 case 5:
995 kvm_write_c0_guest_kscratch4(cop0, v);
996 break;
997 case 6:
998 kvm_write_c0_guest_kscratch5(cop0, v);
999 break;
1000 case 7:
1001 kvm_write_c0_guest_kscratch6(cop0, v);
1002 break;
1003 }
1004 break;
James Hoganf8be02d2014-05-29 10:16:29 +01001005 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -07001006 default:
James Hogancc68d222016-06-15 19:29:48 +01001007 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -07001008 }
1009 return 0;
1010}
1011
James Hogan5fafd8742014-12-08 23:07:56 +00001012static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1013 struct kvm_enable_cap *cap)
1014{
1015 int r = 0;
1016
1017 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1018 return -EINVAL;
1019 if (cap->flags)
1020 return -EINVAL;
1021 if (cap->args[0])
1022 return -EINVAL;
1023
1024 switch (cap->cap) {
1025 case KVM_CAP_MIPS_FPU:
1026 vcpu->arch.fpu_enabled = true;
1027 break;
James Hogand952bd02014-12-08 23:07:56 +00001028 case KVM_CAP_MIPS_MSA:
1029 vcpu->arch.msa_enabled = true;
1030 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001031 default:
1032 r = -EINVAL;
1033 break;
1034 }
1035
1036 return r;
1037}
1038
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001039long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1040 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -08001041{
1042 struct kvm_vcpu *vcpu = filp->private_data;
1043 void __user *argp = (void __user *)arg;
1044 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001045
1046 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -07001047 case KVM_SET_ONE_REG:
1048 case KVM_GET_ONE_REG: {
1049 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001050
David Daney4c73fb22013-05-23 09:49:09 -07001051 if (copy_from_user(&reg, argp, sizeof(reg)))
1052 return -EFAULT;
1053 if (ioctl == KVM_SET_ONE_REG)
1054 return kvm_mips_set_reg(vcpu, &reg);
1055 else
1056 return kvm_mips_get_reg(vcpu, &reg);
1057 }
1058 case KVM_GET_REG_LIST: {
1059 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -07001060 struct kvm_reg_list reg_list;
1061 unsigned n;
1062
1063 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1064 return -EFAULT;
1065 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +01001066 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -07001067 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1068 return -EFAULT;
1069 if (n < reg_list.n)
1070 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +01001071 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -07001072 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001073 case KVM_NMI:
1074 /* Treat the NMI as a CPU reset */
1075 r = kvm_mips_reset_vcpu(vcpu);
1076 break;
1077 case KVM_INTERRUPT:
1078 {
1079 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001080
Sanjay Lal669e8462012-11-21 18:34:02 -08001081 r = -EFAULT;
1082 if (copy_from_user(&irq, argp, sizeof(irq)))
1083 goto out;
1084
Sanjay Lal669e8462012-11-21 18:34:02 -08001085 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1086 irq.irq);
1087
1088 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1089 break;
1090 }
James Hogan5fafd8742014-12-08 23:07:56 +00001091 case KVM_ENABLE_CAP: {
1092 struct kvm_enable_cap cap;
1093
1094 r = -EFAULT;
1095 if (copy_from_user(&cap, argp, sizeof(cap)))
1096 goto out;
1097 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1098 break;
1099 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001100 default:
David Daney4c73fb22013-05-23 09:49:09 -07001101 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001102 }
1103
1104out:
1105 return r;
1106}
1107
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001108/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001109int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1110{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001111 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001112 struct kvm_memory_slot *memslot;
1113 unsigned long ga, ga_end;
1114 int is_dirty = 0;
1115 int r;
1116 unsigned long n;
1117
1118 mutex_lock(&kvm->slots_lock);
1119
1120 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1121 if (r)
1122 goto out;
1123
1124 /* If nothing is dirty, don't bother messing with page tables. */
1125 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001126 slots = kvm_memslots(kvm);
1127 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001128
1129 ga = memslot->base_gfn << PAGE_SHIFT;
1130 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1131
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001132 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
1133 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -08001134
1135 n = kvm_dirty_bitmap_bytes(memslot);
1136 memset(memslot->dirty_bitmap, 0, n);
1137 }
1138
1139 r = 0;
1140out:
1141 mutex_unlock(&kvm->slots_lock);
1142 return r;
1143
1144}
1145
1146long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1147{
1148 long r;
1149
1150 switch (ioctl) {
1151 default:
David Daneyed829852013-05-23 09:49:10 -07001152 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001153 }
1154
1155 return r;
1156}
1157
1158int kvm_arch_init(void *opaque)
1159{
Sanjay Lal669e8462012-11-21 18:34:02 -08001160 if (kvm_mips_callbacks) {
1161 kvm_err("kvm: module already exists\n");
1162 return -EEXIST;
1163 }
1164
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001165 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001166}
1167
1168void kvm_arch_exit(void)
1169{
1170 kvm_mips_callbacks = NULL;
1171}
1172
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001173int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1174 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001175{
David Daneyed829852013-05-23 09:49:10 -07001176 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001177}
1178
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001179int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1180 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001181{
David Daneyed829852013-05-23 09:49:10 -07001182 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001183}
1184
Dominik Dingel31928aa2014-12-04 15:47:07 +01001185void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001186{
Sanjay Lal669e8462012-11-21 18:34:02 -08001187}
1188
1189int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1190{
David Daneyed829852013-05-23 09:49:10 -07001191 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001192}
1193
1194int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1195{
David Daneyed829852013-05-23 09:49:10 -07001196 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001197}
1198
1199int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1200{
1201 return VM_FAULT_SIGBUS;
1202}
1203
Alexander Graf784aa3d2014-07-14 18:27:35 +02001204int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001205{
1206 int r;
1207
1208 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001209 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001210 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001211 r = 1;
1212 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001213 case KVM_CAP_COALESCED_MMIO:
1214 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1215 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001216 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001217 /* We don't handle systems with inconsistent cpu_has_fpu */
1218 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001219 break;
James Hogand952bd02014-12-08 23:07:56 +00001220 case KVM_CAP_MIPS_MSA:
1221 /*
1222 * We don't support MSA vector partitioning yet:
1223 * 1) It would require explicit support which can't be tested
1224 * yet due to lack of support in current hardware.
1225 * 2) It extends the state that would need to be saved/restored
1226 * by e.g. QEMU for migration.
1227 *
1228 * When vector partitioning hardware becomes available, support
1229 * could be added by requiring a flag when enabling
1230 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1231 * to save/restore the appropriate extra state.
1232 */
1233 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1234 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001235 default:
1236 r = 0;
1237 break;
1238 }
1239 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001240}
1241
1242int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1243{
1244 return kvm_mips_pending_timer(vcpu);
1245}
1246
1247int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1248{
1249 int i;
1250 struct mips_coproc *cop0;
1251
1252 if (!vcpu)
1253 return -1;
1254
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001255 kvm_debug("VCPU Register Dump:\n");
1256 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1257 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001258
1259 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001260 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001261 vcpu->arch.gprs[i],
1262 vcpu->arch.gprs[i + 1],
1263 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1264 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001265 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1266 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001267
1268 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001269 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1270 kvm_read_c0_guest_status(cop0),
1271 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001272
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001273 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001274
1275 return 0;
1276}
1277
1278int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1279{
1280 int i;
1281
David Daney8d17dd02013-05-23 09:49:08 -07001282 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001283 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001284 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001285 vcpu->arch.hi = regs->hi;
1286 vcpu->arch.lo = regs->lo;
1287 vcpu->arch.pc = regs->pc;
1288
David Daney4c73fb22013-05-23 09:49:09 -07001289 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001290}
1291
1292int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1293{
1294 int i;
1295
David Daney8d17dd02013-05-23 09:49:08 -07001296 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001297 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001298
1299 regs->hi = vcpu->arch.hi;
1300 regs->lo = vcpu->arch.lo;
1301 regs->pc = vcpu->arch.pc;
1302
David Daney4c73fb22013-05-23 09:49:09 -07001303 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001304}
1305
James Hogan0fae34f2014-05-29 10:16:39 +01001306static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001307{
1308 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1309
1310 kvm_mips_callbacks->queue_timer_int(vcpu);
1311
1312 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001313 if (swait_active(&vcpu->wq))
1314 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001315}
1316
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001317/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001318static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001319{
1320 struct kvm_vcpu *vcpu;
1321
1322 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1323 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001324 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001325}
1326
1327int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1328{
1329 kvm_mips_callbacks->vcpu_init(vcpu);
1330 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1331 HRTIMER_MODE_REL);
1332 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001333 return 0;
1334}
1335
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001336int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1337 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001338{
1339 return 0;
1340}
1341
1342/* Initial guest state */
1343int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1344{
1345 return kvm_mips_callbacks->vcpu_setup(vcpu);
1346}
1347
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001348static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001349{
James Hogan8cffd192016-06-09 14:19:08 +01001350 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001351
Sanjay Lal669e8462012-11-21 18:34:02 -08001352 if (cpu_has_dsp)
1353 status |= (ST0_MX);
1354
1355 write_c0_status(status);
1356 ehb();
1357}
1358
1359/*
1360 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1361 */
1362int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1363{
James Hogan8cffd192016-06-09 14:19:08 +01001364 u32 cause = vcpu->arch.host_cp0_cause;
1365 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1366 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001367 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1368 enum emulation_result er = EMULATE_DONE;
1369 int ret = RESUME_GUEST;
1370
James Hoganc4c6f2c2015-02-04 10:52:03 +00001371 /* re-enable HTW before enabling interrupts */
1372 htw_start();
1373
Sanjay Lal669e8462012-11-21 18:34:02 -08001374 /* Set a default exit reason */
1375 run->exit_reason = KVM_EXIT_UNKNOWN;
1376 run->ready_for_interrupt_injection = 1;
1377
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001378 /*
1379 * Set the appropriate status bits based on host CPU features,
1380 * before we hit the scheduler
1381 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001382 kvm_mips_set_c0_status();
1383
1384 local_irq_enable();
1385
1386 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1387 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001388 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001389
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001390 /*
1391 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001392 * causing an exception to be delivered to the Guest Kernel
1393 */
1394 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1395 if (er == EMULATE_PRIV_FAIL) {
1396 goto skip_emul;
1397 } else if (er == EMULATE_FAIL) {
1398 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1399 ret = RESUME_HOST;
1400 goto skip_emul;
1401 }
1402
1403 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001404 case EXCCODE_INT:
1405 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001406
1407 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001408
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001409 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001410 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001411
1412 ret = RESUME_GUEST;
1413 break;
1414
James Hogan16d100db2015-12-16 23:49:33 +00001415 case EXCCODE_CPU:
1416 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001417
1418 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001419 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1420 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001421 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001422 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001423 break;
1424
James Hogan16d100db2015-12-16 23:49:33 +00001425 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001426 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001427 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1428 break;
1429
James Hogan16d100db2015-12-16 23:49:33 +00001430 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001431 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1432 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1433 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001434
1435 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001436 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1437 break;
1438
James Hogan16d100db2015-12-16 23:49:33 +00001439 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001440 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1441 cause, opc, badvaddr);
1442
1443 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001444 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1445 break;
1446
James Hogan16d100db2015-12-16 23:49:33 +00001447 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001448 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001449 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1450 break;
1451
James Hogan16d100db2015-12-16 23:49:33 +00001452 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001453 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001454 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1455 break;
1456
James Hogan16d100db2015-12-16 23:49:33 +00001457 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001458 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001459 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1460 break;
1461
James Hogan16d100db2015-12-16 23:49:33 +00001462 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001463 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001464 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1465 break;
1466
James Hogan16d100db2015-12-16 23:49:33 +00001467 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001468 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001469 ret = kvm_mips_callbacks->handle_break(vcpu);
1470 break;
1471
James Hogan16d100db2015-12-16 23:49:33 +00001472 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001473 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001474 ret = kvm_mips_callbacks->handle_trap(vcpu);
1475 break;
1476
James Hogan16d100db2015-12-16 23:49:33 +00001477 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001478 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001479 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1480 break;
1481
James Hogan16d100db2015-12-16 23:49:33 +00001482 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001483 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001484 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1485 break;
1486
James Hogan16d100db2015-12-16 23:49:33 +00001487 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001488 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001489 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1490 break;
1491
Sanjay Lal669e8462012-11-21 18:34:02 -08001492 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001493 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1494 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1495 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001496 kvm_arch_vcpu_dump_regs(vcpu);
1497 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1498 ret = RESUME_HOST;
1499 break;
1500
1501 }
1502
1503skip_emul:
1504 local_irq_disable();
1505
1506 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1507 kvm_mips_deliver_interrupts(vcpu, cause);
1508
1509 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001510 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001511 if (signal_pending(current)) {
1512 run->exit_reason = KVM_EXIT_INTR;
1513 ret = (-EINTR << 2) | RESUME_HOST;
1514 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001515 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001516 }
1517 }
1518
James Hogan98e91b82014-11-18 14:09:12 +00001519 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001520 trace_kvm_reenter(vcpu);
1521
James Hogan98e91b82014-11-18 14:09:12 +00001522 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001523 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1524 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001525 *
1526 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001527 * vector, as it may well cause an [MSA] FP exception if there
1528 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001529 * kvm_mips_csr_die_notifier() for how that is handled).
1530 */
1531 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1532 read_c0_status() & ST0_CU1)
1533 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001534
1535 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1536 read_c0_config5() & MIPS_CONF5_MSAEN)
1537 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001538 }
1539
James Hoganc4c6f2c2015-02-04 10:52:03 +00001540 /* Disable HTW before returning to guest or host */
1541 htw_stop();
1542
Sanjay Lal669e8462012-11-21 18:34:02 -08001543 return ret;
1544}
1545
James Hogan98e91b82014-11-18 14:09:12 +00001546/* Enable FPU for guest and restore context */
1547void kvm_own_fpu(struct kvm_vcpu *vcpu)
1548{
1549 struct mips_coproc *cop0 = vcpu->arch.cop0;
1550 unsigned int sr, cfg5;
1551
1552 preempt_disable();
1553
James Hogan539cb89fb2015-03-05 11:43:36 +00001554 sr = kvm_read_c0_guest_status(cop0);
1555
1556 /*
1557 * If MSA state is already live, it is undefined how it interacts with
1558 * FR=0 FPU state, and we don't want to hit reserved instruction
1559 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1560 * play it safe and save it first.
1561 *
1562 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1563 * get called when guest CU1 is set, however we can't trust the guest
1564 * not to clobber the status register directly via the commpage.
1565 */
1566 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001567 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001568 kvm_lose_fpu(vcpu);
1569
James Hogan98e91b82014-11-18 14:09:12 +00001570 /*
1571 * Enable FPU for guest
1572 * We set FR and FRE according to guest context
1573 */
James Hogan98e91b82014-11-18 14:09:12 +00001574 change_c0_status(ST0_CU1 | ST0_FR, sr);
1575 if (cpu_has_fre) {
1576 cfg5 = kvm_read_c0_guest_config5(cop0);
1577 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1578 }
1579 enable_fpu_hazard();
1580
1581 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001582 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001583 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001584 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001585 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1586 } else {
1587 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001588 }
1589
1590 preempt_enable();
1591}
1592
James Hogan539cb89fb2015-03-05 11:43:36 +00001593#ifdef CONFIG_CPU_HAS_MSA
1594/* Enable MSA for guest and restore context */
1595void kvm_own_msa(struct kvm_vcpu *vcpu)
1596{
1597 struct mips_coproc *cop0 = vcpu->arch.cop0;
1598 unsigned int sr, cfg5;
1599
1600 preempt_disable();
1601
1602 /*
1603 * Enable FPU if enabled in guest, since we're restoring FPU context
1604 * anyway. We set FR and FRE according to guest context.
1605 */
1606 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1607 sr = kvm_read_c0_guest_status(cop0);
1608
1609 /*
1610 * If FR=0 FPU state is already live, it is undefined how it
1611 * interacts with MSA state, so play it safe and save it first.
1612 */
1613 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001614 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1615 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001616 kvm_lose_fpu(vcpu);
1617
1618 change_c0_status(ST0_CU1 | ST0_FR, sr);
1619 if (sr & ST0_CU1 && cpu_has_fre) {
1620 cfg5 = kvm_read_c0_guest_config5(cop0);
1621 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1622 }
1623 }
1624
1625 /* Enable MSA for guest */
1626 set_c0_config5(MIPS_CONF5_MSAEN);
1627 enable_fpu_hazard();
1628
James Hoganf9431762016-06-14 09:40:10 +01001629 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1630 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001631 /*
1632 * Guest FPU state already loaded, only restore upper MSA state
1633 */
1634 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001635 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001636 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001637 break;
1638 case 0:
1639 /* Neither FPU or MSA already active, restore full MSA state */
1640 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001641 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001642 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001643 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001644 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1645 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001646 break;
1647 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001648 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001649 break;
1650 }
1651
1652 preempt_enable();
1653}
1654#endif
1655
1656/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001657void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1658{
1659 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001660 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001661 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001662 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001663 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001664 }
James Hoganf9431762016-06-14 09:40:10 +01001665 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001666 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001667 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001668 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001669 }
1670 preempt_enable();
1671}
1672
James Hogan539cb89fb2015-03-05 11:43:36 +00001673/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001674void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1675{
1676 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001677 * FPU & MSA get disabled in root context (hardware) when it is disabled
1678 * in guest context (software), but the register state in the hardware
1679 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001680 * before saving.
1681 */
1682
1683 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001684 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001685 set_c0_config5(MIPS_CONF5_MSAEN);
1686 enable_fpu_hazard();
1687
1688 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001689 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001690
1691 /* Disable MSA & FPU */
1692 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001693 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001694 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001695 disable_fpu_hazard();
1696 }
James Hoganf9431762016-06-14 09:40:10 +01001697 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1698 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001699 set_c0_status(ST0_CU1);
1700 enable_fpu_hazard();
1701
1702 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001703 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001704 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001705
1706 /* Disable FPU */
1707 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001708 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001709 }
1710 preempt_enable();
1711}
1712
1713/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001714 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1715 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1716 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001717 */
1718static int kvm_mips_csr_die_notify(struct notifier_block *self,
1719 unsigned long cmd, void *ptr)
1720{
1721 struct die_args *args = (struct die_args *)ptr;
1722 struct pt_regs *regs = args->regs;
1723 unsigned long pc;
1724
James Hogan539cb89fb2015-03-05 11:43:36 +00001725 /* Only interested in FPE and MSAFPE */
1726 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001727 return NOTIFY_DONE;
1728
1729 /* Return immediately if guest context isn't active */
1730 if (!(current->flags & PF_VCPU))
1731 return NOTIFY_DONE;
1732
1733 /* Should never get here from user mode */
1734 BUG_ON(user_mode(regs));
1735
1736 pc = instruction_pointer(regs);
1737 switch (cmd) {
1738 case DIE_FP:
1739 /* match 2nd instruction in __kvm_restore_fcsr */
1740 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1741 return NOTIFY_DONE;
1742 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001743 case DIE_MSAFP:
1744 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1745 if (!cpu_has_msa ||
1746 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1747 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1748 return NOTIFY_DONE;
1749 break;
James Hogan98e91b82014-11-18 14:09:12 +00001750 }
1751
1752 /* Move PC forward a little and continue executing */
1753 instruction_pointer(regs) += 4;
1754
1755 return NOTIFY_STOP;
1756}
1757
1758static struct notifier_block kvm_mips_csr_die_notifier = {
1759 .notifier_call = kvm_mips_csr_die_notify,
1760};
1761
James Hogan2db9d232015-12-16 23:49:32 +00001762static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001763{
1764 int ret;
1765
1766 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1767
1768 if (ret)
1769 return ret;
1770
James Hogan98e91b82014-11-18 14:09:12 +00001771 register_die_notifier(&kvm_mips_csr_die_notifier);
1772
Sanjay Lal669e8462012-11-21 18:34:02 -08001773 return 0;
1774}
1775
James Hogan2db9d232015-12-16 23:49:32 +00001776static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001777{
1778 kvm_exit();
1779
James Hogan98e91b82014-11-18 14:09:12 +00001780 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001781}
1782
1783module_init(kvm_mips_init);
1784module_exit(kvm_mips_exit);
1785
1786EXPORT_TRACEPOINT_SYMBOL(kvm_exit);