blob: b8f04070bf39b997f36f1e7da8c9212ea82257d8 [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>
James Hogand852b5f2016-10-19 00:24:27 +010017#include <linux/uaccess.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080018#include <linux/vmalloc.h>
19#include <linux/fs.h>
20#include <linux/bootmem.h>
James Hoganf7982172015-02-04 17:06:37 +000021#include <asm/fpu.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080022#include <asm/page.h>
23#include <asm/cacheflush.h>
24#include <asm/mmu_context.h>
James Hoganc4c6f2c2015-02-04 10:52:03 +000025#include <asm/pgtable.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080026
27#include <linux/kvm_host.h>
28
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070029#include "interrupt.h"
30#include "commpage.h"
Sanjay Lal669e8462012-11-21 18:34:02 -080031
32#define CREATE_TRACE_POINTS
33#include "trace.h"
34
35#ifndef VECTORSPACING
36#define VECTORSPACING 0x100 /* for EI/VI mode */
37#endif
38
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070039#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
Sanjay Lal669e8462012-11-21 18:34:02 -080040struct kvm_stats_debugfs_item debugfs_entries[] = {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070041 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
42 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
43 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
44 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
45 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
46 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
47 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
48 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
49 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
50 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
51 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
52 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
53 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
James Hogan0a560422015-02-06 16:03:57 +000054 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000055 { "msa_fpe", VCPU_STAT(msa_fpe_exits), KVM_STAT_VCPU },
James Hogan1c0cd662015-02-06 10:56:27 +000056 { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000057 { "msa_disabled", VCPU_STAT(msa_disabled_exits), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070058 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
Paolo Bonzinif7819512015-02-04 18:20:58 +010059 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +020060 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), KVM_STAT_VCPU },
Christian Borntraeger3491caf2016-05-13 12:16:35 +020061 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070062 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
Sanjay Lal669e8462012-11-21 18:34:02 -080063 {NULL}
64};
65
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070066/*
67 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
68 * Config7, so we are "runnable" if interrupts are pending
Sanjay Lal669e8462012-11-21 18:34:02 -080069 */
70int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
71{
72 return !!(vcpu->arch.pending_exceptions);
73}
74
75int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
76{
77 return 1;
78}
79
Radim Krčmář13a34e02014-08-28 15:13:03 +020080int kvm_arch_hardware_enable(void)
Sanjay Lal669e8462012-11-21 18:34:02 -080081{
82 return 0;
83}
84
Sanjay Lal669e8462012-11-21 18:34:02 -080085int kvm_arch_hardware_setup(void)
86{
87 return 0;
88}
89
Sanjay Lal669e8462012-11-21 18:34:02 -080090void kvm_arch_check_processor_compat(void *rtn)
91{
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -070092 *(int *)rtn = 0;
Sanjay Lal669e8462012-11-21 18:34:02 -080093}
94
Sanjay Lal669e8462012-11-21 18:34:02 -080095int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
96{
Sanjay Lal669e8462012-11-21 18:34:02 -080097 return 0;
98}
99
Luiz Capitulino235539b2016-09-07 14:47:23 -0400100bool kvm_arch_has_vcpu_debugfs(void)
101{
102 return false;
103}
104
105int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
106{
107 return 0;
108}
109
Sanjay Lal669e8462012-11-21 18:34:02 -0800110void kvm_mips_free_vcpus(struct kvm *kvm)
111{
112 unsigned int i;
113 struct kvm_vcpu *vcpu;
114
115 /* Put the pages we reserved for the guest pmap */
116 for (i = 0; i < kvm->arch.guest_pmap_npages; i++) {
117 if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE)
James Hogan9befad22016-06-09 14:19:11 +0100118 kvm_release_pfn_clean(kvm->arch.guest_pmap[i]);
Sanjay Lal669e8462012-11-21 18:34:02 -0800119 }
James Hoganc6c0a662014-05-29 10:16:44 +0100120 kfree(kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800121
122 kvm_for_each_vcpu(i, vcpu, kvm) {
123 kvm_arch_vcpu_free(vcpu);
124 }
125
126 mutex_lock(&kvm->lock);
127
128 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
129 kvm->vcpus[i] = NULL;
130
131 atomic_set(&kvm->online_vcpus, 0);
132
133 mutex_unlock(&kvm->lock);
134}
135
Sanjay Lal669e8462012-11-21 18:34:02 -0800136void kvm_arch_destroy_vm(struct kvm *kvm)
137{
138 kvm_mips_free_vcpus(kvm);
Sanjay Lal669e8462012-11-21 18:34:02 -0800139}
140
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700141long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
142 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800143{
David Daneyed829852013-05-23 09:49:10 -0700144 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800145}
146
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530147int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
148 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800149{
150 return 0;
151}
152
153int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700154 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200155 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700156 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800157{
158 return 0;
159}
160
161void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200162 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700163 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200164 const struct kvm_memory_slot *new,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700165 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800166{
167 unsigned long npages = 0;
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700168 int i;
Sanjay Lal669e8462012-11-21 18:34:02 -0800169
170 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
171 __func__, kvm, mem->slot, mem->guest_phys_addr,
172 mem->memory_size, mem->userspace_addr);
173
174 /* Setup Guest PMAP table */
175 if (!kvm->arch.guest_pmap) {
176 if (mem->slot == 0)
177 npages = mem->memory_size >> PAGE_SHIFT;
178
179 if (npages) {
180 kvm->arch.guest_pmap_npages = npages;
181 kvm->arch.guest_pmap =
182 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
183
184 if (!kvm->arch.guest_pmap) {
James Hoganf7fdcb62015-12-16 23:49:39 +0000185 kvm_err("Failed to allocate guest PMAP\n");
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700186 return;
Sanjay Lal669e8462012-11-21 18:34:02 -0800187 }
188
James Hogan6e95bfd2014-05-29 10:16:43 +0100189 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
190 npages, kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800191
192 /* Now setup the page table */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700193 for (i = 0; i < npages; i++)
Sanjay Lal669e8462012-11-21 18:34:02 -0800194 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
Sanjay Lal669e8462012-11-21 18:34:02 -0800195 }
196 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800197}
198
James Hogand7b8f892016-06-23 17:34:40 +0100199static inline void dump_handler(const char *symbol, void *start, void *end)
200{
201 u32 *p;
202
203 pr_debug("LEAF(%s)\n", symbol);
204
205 pr_debug("\t.set push\n");
206 pr_debug("\t.set noreorder\n");
207
208 for (p = start; p < (u32 *)end; ++p)
209 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p);
210
211 pr_debug("\t.set\tpop\n");
212
213 pr_debug("\tEND(%s)\n", symbol);
214}
215
Sanjay Lal669e8462012-11-21 18:34:02 -0800216struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
217{
James Hogan90e93112016-06-23 17:34:39 +0100218 int err, size;
James Hogana7cfa7a2016-09-10 23:56:46 +0100219 void *gebase, *p, *handler, *refill_start, *refill_end;
Sanjay Lal669e8462012-11-21 18:34:02 -0800220 int i;
221
222 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
223
224 if (!vcpu) {
225 err = -ENOMEM;
226 goto out;
227 }
228
229 err = kvm_vcpu_init(vcpu, kvm, id);
230
231 if (err)
232 goto out_free_cpu;
233
James Hogan6e95bfd2014-05-29 10:16:43 +0100234 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800235
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700236 /*
237 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800238 * guest mode exits
239 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700240 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800241 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700242 else
James Hogan7006e2d2014-05-29 10:16:23 +0100243 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800244
Sanjay Lal669e8462012-11-21 18:34:02 -0800245 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
246
247 if (!gebase) {
248 err = -ENOMEM;
James Hogan585bb8f2015-11-11 14:21:20 +0000249 goto out_uninit_cpu;
Sanjay Lal669e8462012-11-21 18:34:02 -0800250 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100251 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
252 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800253
James Hogan2a06dab2016-07-08 11:53:26 +0100254 /*
255 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
256 * limits us to the low 512MB of physical address space. If the memory
257 * we allocate is out of range, just give up now.
258 */
259 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) {
260 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
261 gebase);
262 err = -ENOMEM;
263 goto out_free_gebase;
264 }
265
Sanjay Lal669e8462012-11-21 18:34:02 -0800266 /* Save new ebase */
267 vcpu->arch.guest_ebase = gebase;
268
James Hogan90e93112016-06-23 17:34:39 +0100269 /* Build guest exception vectors dynamically in unmapped memory */
James Hogan1f9ca622016-06-23 17:34:46 +0100270 handler = gebase + 0x2000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800271
James Hogana7cfa7a2016-09-10 23:56:46 +0100272 /* TLB refill */
273 refill_start = gebase;
274 refill_end = kvm_mips_build_tlb_refill_exception(refill_start, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800275
276 /* General Exception Entry point */
James Hogan1f9ca622016-06-23 17:34:46 +0100277 kvm_mips_build_exception(gebase + 0x180, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800278
279 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
280 for (i = 0; i < 8; i++) {
281 kvm_debug("L1 Vectored handler @ %p\n",
282 gebase + 0x200 + (i * VECTORSPACING));
James Hogan1f9ca622016-06-23 17:34:46 +0100283 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
284 handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800285 }
286
James Hogan90e93112016-06-23 17:34:39 +0100287 /* General exit handler */
James Hogan1f9ca622016-06-23 17:34:46 +0100288 p = handler;
James Hogan90e93112016-06-23 17:34:39 +0100289 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800290
James Hogan90e93112016-06-23 17:34:39 +0100291 /* Guest entry routine */
292 vcpu->arch.vcpu_run = p;
293 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100294
James Hogand7b8f892016-06-23 17:34:40 +0100295 /* Dump the generated code */
296 pr_debug("#include <asm/asm.h>\n");
297 pr_debug("#include <asm/regdef.h>\n");
298 pr_debug("\n");
299 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
James Hogana7cfa7a2016-09-10 23:56:46 +0100300 dump_handler("kvm_tlb_refill", refill_start, refill_end);
James Hogand7b8f892016-06-23 17:34:40 +0100301 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
302 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
303
Sanjay Lal669e8462012-11-21 18:34:02 -0800304 /* Invalidate the icache for these ranges */
James Hogan32eb12a2017-01-03 17:43:01 +0000305 flush_icache_range((unsigned long)gebase,
306 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800307
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700308 /*
309 * Allocate comm page for guest kernel, a TLB will be reserved for
310 * mapping GVA @ 0xFFFF8000 to this page
311 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800312 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
313
314 if (!vcpu->arch.kseg0_commpage) {
315 err = -ENOMEM;
316 goto out_free_gebase;
317 }
318
James Hogan6e95bfd2014-05-29 10:16:43 +0100319 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800320 kvm_mips_commpage_init(vcpu);
321
322 /* Init */
323 vcpu->arch.last_sched_cpu = -1;
324
325 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100326 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800327
328 return vcpu;
329
330out_free_gebase:
331 kfree(gebase);
332
James Hogan585bb8f2015-11-11 14:21:20 +0000333out_uninit_cpu:
334 kvm_vcpu_uninit(vcpu);
335
Sanjay Lal669e8462012-11-21 18:34:02 -0800336out_free_cpu:
337 kfree(vcpu);
338
339out:
340 return ERR_PTR(err);
341}
342
343void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
344{
345 hrtimer_cancel(&vcpu->arch.comparecount_timer);
346
347 kvm_vcpu_uninit(vcpu);
348
349 kvm_mips_dump_stats(vcpu);
350
James Hoganaba859292016-12-16 15:57:00 +0000351 kvm_mmu_free_memory_caches(vcpu);
James Hoganc6c0a662014-05-29 10:16:44 +0100352 kfree(vcpu->arch.guest_ebase);
353 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700354 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800355}
356
357void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
358{
359 kvm_arch_vcpu_free(vcpu);
360}
361
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700362int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
363 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800364{
David Daneyed829852013-05-23 09:49:10 -0700365 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800366}
367
368int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
369{
370 int r = 0;
371 sigset_t sigsaved;
372
373 if (vcpu->sigset_active)
374 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
375
376 if (vcpu->mmio_needed) {
377 if (!vcpu->mmio_is_write)
378 kvm_mips_complete_mmio_load(vcpu, run);
379 vcpu->mmio_needed = 0;
380 }
381
James Hoganf7982172015-02-04 17:06:37 +0000382 lose_fpu(1);
383
James Hogan044f0f02014-05-29 10:16:32 +0100384 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200385 guest_enter_irqoff();
James Hogan93258602016-06-14 09:40:14 +0100386 trace_kvm_enter(vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100387
James Hogana2c046e2016-11-18 13:14:37 +0000388 r = kvm_mips_callbacks->vcpu_run(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100389
James Hogan93258602016-06-14 09:40:14 +0100390 trace_kvm_out(vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200391 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800392 local_irq_enable();
393
394 if (vcpu->sigset_active)
395 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
396
397 return r;
398}
399
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700400int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
401 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800402{
403 int intr = (int)irq->irq;
404 struct kvm_vcpu *dvcpu = NULL;
405
406 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
407 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
408 (int)intr);
409
410 if (irq->cpu == -1)
411 dvcpu = vcpu;
412 else
413 dvcpu = vcpu->kvm->vcpus[irq->cpu];
414
415 if (intr == 2 || intr == 3 || intr == 4) {
416 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
417
418 } else if (intr == -2 || intr == -3 || intr == -4) {
419 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
420 } else {
421 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
422 irq->cpu, irq->irq);
423 return -EINVAL;
424 }
425
426 dvcpu->arch.wait = 0;
427
Marcelo Tosatti85773702016-02-19 09:46:39 +0100428 if (swait_active(&dvcpu->wq))
429 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800430
431 return 0;
432}
433
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700434int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
435 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800436{
David Daneyed829852013-05-23 09:49:10 -0700437 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800438}
439
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700440int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
441 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800442{
David Daneyed829852013-05-23 09:49:10 -0700443 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800444}
445
David Daney4c73fb22013-05-23 09:49:09 -0700446static u64 kvm_mips_get_one_regs[] = {
447 KVM_REG_MIPS_R0,
448 KVM_REG_MIPS_R1,
449 KVM_REG_MIPS_R2,
450 KVM_REG_MIPS_R3,
451 KVM_REG_MIPS_R4,
452 KVM_REG_MIPS_R5,
453 KVM_REG_MIPS_R6,
454 KVM_REG_MIPS_R7,
455 KVM_REG_MIPS_R8,
456 KVM_REG_MIPS_R9,
457 KVM_REG_MIPS_R10,
458 KVM_REG_MIPS_R11,
459 KVM_REG_MIPS_R12,
460 KVM_REG_MIPS_R13,
461 KVM_REG_MIPS_R14,
462 KVM_REG_MIPS_R15,
463 KVM_REG_MIPS_R16,
464 KVM_REG_MIPS_R17,
465 KVM_REG_MIPS_R18,
466 KVM_REG_MIPS_R19,
467 KVM_REG_MIPS_R20,
468 KVM_REG_MIPS_R21,
469 KVM_REG_MIPS_R22,
470 KVM_REG_MIPS_R23,
471 KVM_REG_MIPS_R24,
472 KVM_REG_MIPS_R25,
473 KVM_REG_MIPS_R26,
474 KVM_REG_MIPS_R27,
475 KVM_REG_MIPS_R28,
476 KVM_REG_MIPS_R29,
477 KVM_REG_MIPS_R30,
478 KVM_REG_MIPS_R31,
479
James Hogan70e92c7e2016-07-04 19:35:11 +0100480#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700481 KVM_REG_MIPS_HI,
482 KVM_REG_MIPS_LO,
James Hogan70e92c7e2016-07-04 19:35:11 +0100483#endif
David Daney4c73fb22013-05-23 09:49:09 -0700484 KVM_REG_MIPS_PC,
485
486 KVM_REG_MIPS_CP0_INDEX,
487 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100488 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700489 KVM_REG_MIPS_CP0_PAGEMASK,
490 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100491 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700492 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100493 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700494 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100495 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700496 KVM_REG_MIPS_CP0_STATUS,
497 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100498 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100499 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700500 KVM_REG_MIPS_CP0_CONFIG,
501 KVM_REG_MIPS_CP0_CONFIG1,
502 KVM_REG_MIPS_CP0_CONFIG2,
503 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100504 KVM_REG_MIPS_CP0_CONFIG4,
505 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700506 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100507 KVM_REG_MIPS_CP0_ERROREPC,
508
509 KVM_REG_MIPS_COUNT_CTL,
510 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100511 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700512};
513
James Hogane5775932016-06-15 19:29:51 +0100514static u64 kvm_mips_get_one_regs_fpu[] = {
515 KVM_REG_MIPS_FCR_IR,
516 KVM_REG_MIPS_FCR_CSR,
517};
518
519static u64 kvm_mips_get_one_regs_msa[] = {
520 KVM_REG_MIPS_MSA_IR,
521 KVM_REG_MIPS_MSA_CSR,
522};
523
James Hogan05108702016-06-15 19:29:56 +0100524static u64 kvm_mips_get_one_regs_kscratch[] = {
525 KVM_REG_MIPS_CP0_KSCRATCH1,
526 KVM_REG_MIPS_CP0_KSCRATCH2,
527 KVM_REG_MIPS_CP0_KSCRATCH3,
528 KVM_REG_MIPS_CP0_KSCRATCH4,
529 KVM_REG_MIPS_CP0_KSCRATCH5,
530 KVM_REG_MIPS_CP0_KSCRATCH6,
531};
532
James Hoganf5c43bd2016-06-15 19:29:49 +0100533static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
534{
535 unsigned long ret;
536
537 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100538 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
539 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
540 /* odd doubles */
541 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
542 ret += 16;
543 }
544 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
545 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hogan05108702016-06-15 19:29:56 +0100546 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
James Hoganf5c43bd2016-06-15 19:29:49 +0100547 ret += kvm_mips_callbacks->num_regs(vcpu);
548
549 return ret;
550}
551
552static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
553{
James Hogane5775932016-06-15 19:29:51 +0100554 u64 index;
555 unsigned int i;
556
James Hoganf5c43bd2016-06-15 19:29:49 +0100557 if (copy_to_user(indices, kvm_mips_get_one_regs,
558 sizeof(kvm_mips_get_one_regs)))
559 return -EFAULT;
560 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
561
James Hogane5775932016-06-15 19:29:51 +0100562 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
563 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
564 sizeof(kvm_mips_get_one_regs_fpu)))
565 return -EFAULT;
566 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
567
568 for (i = 0; i < 32; ++i) {
569 index = KVM_REG_MIPS_FPR_32(i);
570 if (copy_to_user(indices, &index, sizeof(index)))
571 return -EFAULT;
572 ++indices;
573
574 /* skip odd doubles if no F64 */
575 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
576 continue;
577
578 index = KVM_REG_MIPS_FPR_64(i);
579 if (copy_to_user(indices, &index, sizeof(index)))
580 return -EFAULT;
581 ++indices;
582 }
583 }
584
585 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
586 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
587 sizeof(kvm_mips_get_one_regs_msa)))
588 return -EFAULT;
589 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
590
591 for (i = 0; i < 32; ++i) {
592 index = KVM_REG_MIPS_VEC_128(i);
593 if (copy_to_user(indices, &index, sizeof(index)))
594 return -EFAULT;
595 ++indices;
596 }
597 }
598
James Hogan05108702016-06-15 19:29:56 +0100599 for (i = 0; i < 6; ++i) {
600 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
601 continue;
602
603 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
604 sizeof(kvm_mips_get_one_regs_kscratch[i])))
605 return -EFAULT;
606 ++indices;
607 }
608
James Hoganf5c43bd2016-06-15 19:29:49 +0100609 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
610}
611
David Daney4c73fb22013-05-23 09:49:09 -0700612static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
613 const struct kvm_one_reg *reg)
614{
David Daney4c73fb22013-05-23 09:49:09 -0700615 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000616 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100617 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700618 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000619 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000620 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700621
622 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000623 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700624 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
625 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
626 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100627#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700628 case KVM_REG_MIPS_HI:
629 v = (long)vcpu->arch.hi;
630 break;
631 case KVM_REG_MIPS_LO:
632 v = (long)vcpu->arch.lo;
633 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100634#endif
David Daney4c73fb22013-05-23 09:49:09 -0700635 case KVM_REG_MIPS_PC:
636 v = (long)vcpu->arch.pc;
637 break;
638
James Hogan379245c2014-12-02 15:48:24 +0000639 /* Floating point registers */
640 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
641 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
642 return -EINVAL;
643 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
644 /* Odd singles in top of even double when FR=0 */
645 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
646 v = get_fpr32(&fpu->fpr[idx], 0);
647 else
648 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
649 break;
650 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
651 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
652 return -EINVAL;
653 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
654 /* Can't access odd doubles in FR=0 mode */
655 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
656 return -EINVAL;
657 v = get_fpr64(&fpu->fpr[idx], 0);
658 break;
659 case KVM_REG_MIPS_FCR_IR:
660 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
661 return -EINVAL;
662 v = boot_cpu_data.fpu_id;
663 break;
664 case KVM_REG_MIPS_FCR_CSR:
665 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
666 return -EINVAL;
667 v = fpu->fcr31;
668 break;
669
James Hoganab86bd62014-12-02 15:48:24 +0000670 /* MIPS SIMD Architecture (MSA) registers */
671 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
672 if (!kvm_mips_guest_has_msa(&vcpu->arch))
673 return -EINVAL;
674 /* Can't access MSA registers in FR=0 mode */
675 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
676 return -EINVAL;
677 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
678#ifdef CONFIG_CPU_LITTLE_ENDIAN
679 /* least significant byte first */
680 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
681 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
682#else
683 /* most significant byte first */
684 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
685 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
686#endif
687 break;
688 case KVM_REG_MIPS_MSA_IR:
689 if (!kvm_mips_guest_has_msa(&vcpu->arch))
690 return -EINVAL;
691 v = boot_cpu_data.msa_id;
692 break;
693 case KVM_REG_MIPS_MSA_CSR:
694 if (!kvm_mips_guest_has_msa(&vcpu->arch))
695 return -EINVAL;
696 v = fpu->msacsr;
697 break;
698
James Hogan379245c2014-12-02 15:48:24 +0000699 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700700 case KVM_REG_MIPS_CP0_INDEX:
701 v = (long)kvm_read_c0_guest_index(cop0);
702 break;
703 case KVM_REG_MIPS_CP0_CONTEXT:
704 v = (long)kvm_read_c0_guest_context(cop0);
705 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100706 case KVM_REG_MIPS_CP0_USERLOCAL:
707 v = (long)kvm_read_c0_guest_userlocal(cop0);
708 break;
David Daney4c73fb22013-05-23 09:49:09 -0700709 case KVM_REG_MIPS_CP0_PAGEMASK:
710 v = (long)kvm_read_c0_guest_pagemask(cop0);
711 break;
712 case KVM_REG_MIPS_CP0_WIRED:
713 v = (long)kvm_read_c0_guest_wired(cop0);
714 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100715 case KVM_REG_MIPS_CP0_HWRENA:
716 v = (long)kvm_read_c0_guest_hwrena(cop0);
717 break;
David Daney4c73fb22013-05-23 09:49:09 -0700718 case KVM_REG_MIPS_CP0_BADVADDR:
719 v = (long)kvm_read_c0_guest_badvaddr(cop0);
720 break;
721 case KVM_REG_MIPS_CP0_ENTRYHI:
722 v = (long)kvm_read_c0_guest_entryhi(cop0);
723 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100724 case KVM_REG_MIPS_CP0_COMPARE:
725 v = (long)kvm_read_c0_guest_compare(cop0);
726 break;
David Daney4c73fb22013-05-23 09:49:09 -0700727 case KVM_REG_MIPS_CP0_STATUS:
728 v = (long)kvm_read_c0_guest_status(cop0);
729 break;
730 case KVM_REG_MIPS_CP0_CAUSE:
731 v = (long)kvm_read_c0_guest_cause(cop0);
732 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100733 case KVM_REG_MIPS_CP0_EPC:
734 v = (long)kvm_read_c0_guest_epc(cop0);
735 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100736 case KVM_REG_MIPS_CP0_PRID:
737 v = (long)kvm_read_c0_guest_prid(cop0);
738 break;
David Daney4c73fb22013-05-23 09:49:09 -0700739 case KVM_REG_MIPS_CP0_CONFIG:
740 v = (long)kvm_read_c0_guest_config(cop0);
741 break;
742 case KVM_REG_MIPS_CP0_CONFIG1:
743 v = (long)kvm_read_c0_guest_config1(cop0);
744 break;
745 case KVM_REG_MIPS_CP0_CONFIG2:
746 v = (long)kvm_read_c0_guest_config2(cop0);
747 break;
748 case KVM_REG_MIPS_CP0_CONFIG3:
749 v = (long)kvm_read_c0_guest_config3(cop0);
750 break;
James Hoganc7716072014-06-26 15:11:29 +0100751 case KVM_REG_MIPS_CP0_CONFIG4:
752 v = (long)kvm_read_c0_guest_config4(cop0);
753 break;
754 case KVM_REG_MIPS_CP0_CONFIG5:
755 v = (long)kvm_read_c0_guest_config5(cop0);
756 break;
David Daney4c73fb22013-05-23 09:49:09 -0700757 case KVM_REG_MIPS_CP0_CONFIG7:
758 v = (long)kvm_read_c0_guest_config7(cop0);
759 break;
James Hogane93d4c12014-06-26 13:47:22 +0100760 case KVM_REG_MIPS_CP0_ERROREPC:
761 v = (long)kvm_read_c0_guest_errorepc(cop0);
762 break;
James Hogan05108702016-06-15 19:29:56 +0100763 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
764 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
765 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
766 return -EINVAL;
767 switch (idx) {
768 case 2:
769 v = (long)kvm_read_c0_guest_kscratch1(cop0);
770 break;
771 case 3:
772 v = (long)kvm_read_c0_guest_kscratch2(cop0);
773 break;
774 case 4:
775 v = (long)kvm_read_c0_guest_kscratch3(cop0);
776 break;
777 case 5:
778 v = (long)kvm_read_c0_guest_kscratch4(cop0);
779 break;
780 case 6:
781 v = (long)kvm_read_c0_guest_kscratch5(cop0);
782 break;
783 case 7:
784 v = (long)kvm_read_c0_guest_kscratch6(cop0);
785 break;
786 }
787 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100788 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100789 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100790 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
791 if (ret)
792 return ret;
793 break;
David Daney4c73fb22013-05-23 09:49:09 -0700794 }
David Daney681865d2013-06-10 12:33:48 -0700795 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
796 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700797
David Daney681865d2013-06-10 12:33:48 -0700798 return put_user(v, uaddr64);
799 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
800 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
801 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700802
David Daney681865d2013-06-10 12:33:48 -0700803 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000804 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
805 void __user *uaddr = (void __user *)(long)reg->addr;
806
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200807 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700808 } else {
809 return -EINVAL;
810 }
David Daney4c73fb22013-05-23 09:49:09 -0700811}
812
813static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
814 const struct kvm_one_reg *reg)
815{
David Daney4c73fb22013-05-23 09:49:09 -0700816 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000817 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
818 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000819 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000820 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700821
David Daney681865d2013-06-10 12:33:48 -0700822 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
823 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
824
825 if (get_user(v, uaddr64) != 0)
826 return -EFAULT;
827 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
828 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
829 s32 v32;
830
831 if (get_user(v32, uaddr32) != 0)
832 return -EFAULT;
833 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000834 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
835 void __user *uaddr = (void __user *)(long)reg->addr;
836
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200837 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700838 } else {
839 return -EINVAL;
840 }
David Daney4c73fb22013-05-23 09:49:09 -0700841
842 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000843 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700844 case KVM_REG_MIPS_R0:
845 /* Silently ignore requests to set $0 */
846 break;
847 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
848 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
849 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100850#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700851 case KVM_REG_MIPS_HI:
852 vcpu->arch.hi = v;
853 break;
854 case KVM_REG_MIPS_LO:
855 vcpu->arch.lo = v;
856 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100857#endif
David Daney4c73fb22013-05-23 09:49:09 -0700858 case KVM_REG_MIPS_PC:
859 vcpu->arch.pc = v;
860 break;
861
James Hogan379245c2014-12-02 15:48:24 +0000862 /* Floating point registers */
863 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
864 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
865 return -EINVAL;
866 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
867 /* Odd singles in top of even double when FR=0 */
868 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
869 set_fpr32(&fpu->fpr[idx], 0, v);
870 else
871 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
872 break;
873 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
874 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
875 return -EINVAL;
876 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
877 /* Can't access odd doubles in FR=0 mode */
878 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
879 return -EINVAL;
880 set_fpr64(&fpu->fpr[idx], 0, v);
881 break;
882 case KVM_REG_MIPS_FCR_IR:
883 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
884 return -EINVAL;
885 /* Read-only */
886 break;
887 case KVM_REG_MIPS_FCR_CSR:
888 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
889 return -EINVAL;
890 fpu->fcr31 = v;
891 break;
892
James Hoganab86bd62014-12-02 15:48:24 +0000893 /* MIPS SIMD Architecture (MSA) registers */
894 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
895 if (!kvm_mips_guest_has_msa(&vcpu->arch))
896 return -EINVAL;
897 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
898#ifdef CONFIG_CPU_LITTLE_ENDIAN
899 /* least significant byte first */
900 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
901 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
902#else
903 /* most significant byte first */
904 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
905 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
906#endif
907 break;
908 case KVM_REG_MIPS_MSA_IR:
909 if (!kvm_mips_guest_has_msa(&vcpu->arch))
910 return -EINVAL;
911 /* Read-only */
912 break;
913 case KVM_REG_MIPS_MSA_CSR:
914 if (!kvm_mips_guest_has_msa(&vcpu->arch))
915 return -EINVAL;
916 fpu->msacsr = v;
917 break;
918
James Hogan379245c2014-12-02 15:48:24 +0000919 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700920 case KVM_REG_MIPS_CP0_INDEX:
921 kvm_write_c0_guest_index(cop0, v);
922 break;
923 case KVM_REG_MIPS_CP0_CONTEXT:
924 kvm_write_c0_guest_context(cop0, v);
925 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100926 case KVM_REG_MIPS_CP0_USERLOCAL:
927 kvm_write_c0_guest_userlocal(cop0, v);
928 break;
David Daney4c73fb22013-05-23 09:49:09 -0700929 case KVM_REG_MIPS_CP0_PAGEMASK:
930 kvm_write_c0_guest_pagemask(cop0, v);
931 break;
932 case KVM_REG_MIPS_CP0_WIRED:
933 kvm_write_c0_guest_wired(cop0, v);
934 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100935 case KVM_REG_MIPS_CP0_HWRENA:
936 kvm_write_c0_guest_hwrena(cop0, v);
937 break;
David Daney4c73fb22013-05-23 09:49:09 -0700938 case KVM_REG_MIPS_CP0_BADVADDR:
939 kvm_write_c0_guest_badvaddr(cop0, v);
940 break;
941 case KVM_REG_MIPS_CP0_ENTRYHI:
942 kvm_write_c0_guest_entryhi(cop0, v);
943 break;
944 case KVM_REG_MIPS_CP0_STATUS:
945 kvm_write_c0_guest_status(cop0, v);
946 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100947 case KVM_REG_MIPS_CP0_EPC:
948 kvm_write_c0_guest_epc(cop0, v);
949 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100950 case KVM_REG_MIPS_CP0_PRID:
951 kvm_write_c0_guest_prid(cop0, v);
952 break;
David Daney4c73fb22013-05-23 09:49:09 -0700953 case KVM_REG_MIPS_CP0_ERROREPC:
954 kvm_write_c0_guest_errorepc(cop0, v);
955 break;
James Hogan05108702016-06-15 19:29:56 +0100956 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
957 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
958 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
959 return -EINVAL;
960 switch (idx) {
961 case 2:
962 kvm_write_c0_guest_kscratch1(cop0, v);
963 break;
964 case 3:
965 kvm_write_c0_guest_kscratch2(cop0, v);
966 break;
967 case 4:
968 kvm_write_c0_guest_kscratch3(cop0, v);
969 break;
970 case 5:
971 kvm_write_c0_guest_kscratch4(cop0, v);
972 break;
973 case 6:
974 kvm_write_c0_guest_kscratch5(cop0, v);
975 break;
976 case 7:
977 kvm_write_c0_guest_kscratch6(cop0, v);
978 break;
979 }
980 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100981 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -0700982 default:
James Hogancc68d222016-06-15 19:29:48 +0100983 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700984 }
985 return 0;
986}
987
James Hogan5fafd8742014-12-08 23:07:56 +0000988static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
989 struct kvm_enable_cap *cap)
990{
991 int r = 0;
992
993 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
994 return -EINVAL;
995 if (cap->flags)
996 return -EINVAL;
997 if (cap->args[0])
998 return -EINVAL;
999
1000 switch (cap->cap) {
1001 case KVM_CAP_MIPS_FPU:
1002 vcpu->arch.fpu_enabled = true;
1003 break;
James Hogand952bd02014-12-08 23:07:56 +00001004 case KVM_CAP_MIPS_MSA:
1005 vcpu->arch.msa_enabled = true;
1006 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001007 default:
1008 r = -EINVAL;
1009 break;
1010 }
1011
1012 return r;
1013}
1014
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001015long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1016 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -08001017{
1018 struct kvm_vcpu *vcpu = filp->private_data;
1019 void __user *argp = (void __user *)arg;
1020 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001021
1022 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -07001023 case KVM_SET_ONE_REG:
1024 case KVM_GET_ONE_REG: {
1025 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001026
David Daney4c73fb22013-05-23 09:49:09 -07001027 if (copy_from_user(&reg, argp, sizeof(reg)))
1028 return -EFAULT;
1029 if (ioctl == KVM_SET_ONE_REG)
1030 return kvm_mips_set_reg(vcpu, &reg);
1031 else
1032 return kvm_mips_get_reg(vcpu, &reg);
1033 }
1034 case KVM_GET_REG_LIST: {
1035 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -07001036 struct kvm_reg_list reg_list;
1037 unsigned n;
1038
1039 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1040 return -EFAULT;
1041 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +01001042 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -07001043 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1044 return -EFAULT;
1045 if (n < reg_list.n)
1046 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +01001047 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -07001048 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001049 case KVM_INTERRUPT:
1050 {
1051 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001052
Sanjay Lal669e8462012-11-21 18:34:02 -08001053 if (copy_from_user(&irq, argp, sizeof(irq)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +01001054 return -EFAULT;
Sanjay Lal669e8462012-11-21 18:34:02 -08001055 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1056 irq.irq);
1057
1058 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1059 break;
1060 }
James Hogan5fafd8742014-12-08 23:07:56 +00001061 case KVM_ENABLE_CAP: {
1062 struct kvm_enable_cap cap;
1063
James Hogan5fafd8742014-12-08 23:07:56 +00001064 if (copy_from_user(&cap, argp, sizeof(cap)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +01001065 return -EFAULT;
James Hogan5fafd8742014-12-08 23:07:56 +00001066 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1067 break;
1068 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001069 default:
David Daney4c73fb22013-05-23 09:49:09 -07001070 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001071 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001072 return r;
1073}
1074
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001075/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001076int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1077{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001078 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001079 struct kvm_memory_slot *memslot;
1080 unsigned long ga, ga_end;
1081 int is_dirty = 0;
1082 int r;
1083 unsigned long n;
1084
1085 mutex_lock(&kvm->slots_lock);
1086
1087 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1088 if (r)
1089 goto out;
1090
1091 /* If nothing is dirty, don't bother messing with page tables. */
1092 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001093 slots = kvm_memslots(kvm);
1094 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001095
1096 ga = memslot->base_gfn << PAGE_SHIFT;
1097 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1098
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001099 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
1100 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -08001101
1102 n = kvm_dirty_bitmap_bytes(memslot);
1103 memset(memslot->dirty_bitmap, 0, n);
1104 }
1105
1106 r = 0;
1107out:
1108 mutex_unlock(&kvm->slots_lock);
1109 return r;
1110
1111}
1112
1113long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1114{
1115 long r;
1116
1117 switch (ioctl) {
1118 default:
David Daneyed829852013-05-23 09:49:10 -07001119 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001120 }
1121
1122 return r;
1123}
1124
1125int kvm_arch_init(void *opaque)
1126{
Sanjay Lal669e8462012-11-21 18:34:02 -08001127 if (kvm_mips_callbacks) {
1128 kvm_err("kvm: module already exists\n");
1129 return -EEXIST;
1130 }
1131
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001132 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001133}
1134
1135void kvm_arch_exit(void)
1136{
1137 kvm_mips_callbacks = NULL;
1138}
1139
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001140int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1141 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001142{
David Daneyed829852013-05-23 09:49:10 -07001143 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001144}
1145
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001146int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1147 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001148{
David Daneyed829852013-05-23 09:49:10 -07001149 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001150}
1151
Dominik Dingel31928aa2014-12-04 15:47:07 +01001152void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001153{
Sanjay Lal669e8462012-11-21 18:34:02 -08001154}
1155
1156int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1157{
David Daneyed829852013-05-23 09:49:10 -07001158 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001159}
1160
1161int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1162{
David Daneyed829852013-05-23 09:49:10 -07001163 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001164}
1165
1166int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1167{
1168 return VM_FAULT_SIGBUS;
1169}
1170
Alexander Graf784aa3d2014-07-14 18:27:35 +02001171int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001172{
1173 int r;
1174
1175 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001176 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001177 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001178 r = 1;
1179 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001180 case KVM_CAP_COALESCED_MMIO:
1181 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1182 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001183 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001184 /* We don't handle systems with inconsistent cpu_has_fpu */
1185 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001186 break;
James Hogand952bd02014-12-08 23:07:56 +00001187 case KVM_CAP_MIPS_MSA:
1188 /*
1189 * We don't support MSA vector partitioning yet:
1190 * 1) It would require explicit support which can't be tested
1191 * yet due to lack of support in current hardware.
1192 * 2) It extends the state that would need to be saved/restored
1193 * by e.g. QEMU for migration.
1194 *
1195 * When vector partitioning hardware becomes available, support
1196 * could be added by requiring a flag when enabling
1197 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1198 * to save/restore the appropriate extra state.
1199 */
1200 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1201 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001202 default:
1203 r = 0;
1204 break;
1205 }
1206 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001207}
1208
1209int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1210{
1211 return kvm_mips_pending_timer(vcpu);
1212}
1213
1214int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1215{
1216 int i;
1217 struct mips_coproc *cop0;
1218
1219 if (!vcpu)
1220 return -1;
1221
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001222 kvm_debug("VCPU Register Dump:\n");
1223 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1224 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001225
1226 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001227 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001228 vcpu->arch.gprs[i],
1229 vcpu->arch.gprs[i + 1],
1230 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1231 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001232 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1233 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001234
1235 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001236 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1237 kvm_read_c0_guest_status(cop0),
1238 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001239
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001240 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001241
1242 return 0;
1243}
1244
1245int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1246{
1247 int i;
1248
David Daney8d17dd02013-05-23 09:49:08 -07001249 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001250 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001251 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001252 vcpu->arch.hi = regs->hi;
1253 vcpu->arch.lo = regs->lo;
1254 vcpu->arch.pc = regs->pc;
1255
David Daney4c73fb22013-05-23 09:49:09 -07001256 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001257}
1258
1259int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1260{
1261 int i;
1262
David Daney8d17dd02013-05-23 09:49:08 -07001263 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001264 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001265
1266 regs->hi = vcpu->arch.hi;
1267 regs->lo = vcpu->arch.lo;
1268 regs->pc = vcpu->arch.pc;
1269
David Daney4c73fb22013-05-23 09:49:09 -07001270 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001271}
1272
James Hogan0fae34f2014-05-29 10:16:39 +01001273static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001274{
1275 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1276
1277 kvm_mips_callbacks->queue_timer_int(vcpu);
1278
1279 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001280 if (swait_active(&vcpu->wq))
1281 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001282}
1283
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001284/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001285static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001286{
1287 struct kvm_vcpu *vcpu;
1288
1289 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1290 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001291 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001292}
1293
1294int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1295{
James Hoganf7f14272016-09-08 22:57:03 +01001296 int err;
1297
1298 err = kvm_mips_callbacks->vcpu_init(vcpu);
1299 if (err)
1300 return err;
1301
Sanjay Lal669e8462012-11-21 18:34:02 -08001302 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1303 HRTIMER_MODE_REL);
1304 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001305 return 0;
1306}
1307
James Hogan630766b2016-09-08 23:00:24 +01001308void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1309{
1310 kvm_mips_callbacks->vcpu_uninit(vcpu);
1311}
1312
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001313int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1314 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001315{
1316 return 0;
1317}
1318
1319/* Initial guest state */
1320int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1321{
1322 return kvm_mips_callbacks->vcpu_setup(vcpu);
1323}
1324
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001325static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001326{
James Hogan8cffd192016-06-09 14:19:08 +01001327 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001328
Sanjay Lal669e8462012-11-21 18:34:02 -08001329 if (cpu_has_dsp)
1330 status |= (ST0_MX);
1331
1332 write_c0_status(status);
1333 ehb();
1334}
1335
1336/*
1337 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1338 */
1339int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1340{
James Hogan8cffd192016-06-09 14:19:08 +01001341 u32 cause = vcpu->arch.host_cp0_cause;
1342 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1343 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001344 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1345 enum emulation_result er = EMULATE_DONE;
James Hogan122e51d2016-11-28 17:23:14 +00001346 u32 inst;
Sanjay Lal669e8462012-11-21 18:34:02 -08001347 int ret = RESUME_GUEST;
1348
James Hoganc4c6f2c2015-02-04 10:52:03 +00001349 /* re-enable HTW before enabling interrupts */
1350 htw_start();
1351
Sanjay Lal669e8462012-11-21 18:34:02 -08001352 /* Set a default exit reason */
1353 run->exit_reason = KVM_EXIT_UNKNOWN;
1354 run->ready_for_interrupt_injection = 1;
1355
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001356 /*
1357 * Set the appropriate status bits based on host CPU features,
1358 * before we hit the scheduler
1359 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001360 kvm_mips_set_c0_status();
1361
1362 local_irq_enable();
1363
1364 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1365 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001366 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001367
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001368 /*
1369 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001370 * causing an exception to be delivered to the Guest Kernel
1371 */
1372 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1373 if (er == EMULATE_PRIV_FAIL) {
1374 goto skip_emul;
1375 } else if (er == EMULATE_FAIL) {
1376 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1377 ret = RESUME_HOST;
1378 goto skip_emul;
1379 }
1380
1381 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001382 case EXCCODE_INT:
1383 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001384
1385 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001386
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001387 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001388 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001389
1390 ret = RESUME_GUEST;
1391 break;
1392
James Hogan16d100db2015-12-16 23:49:33 +00001393 case EXCCODE_CPU:
1394 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001395
1396 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001397 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1398 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001399 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001400 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001401 break;
1402
James Hogan16d100db2015-12-16 23:49:33 +00001403 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001404 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001405 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1406 break;
1407
James Hogan16d100db2015-12-16 23:49:33 +00001408 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001409 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1410 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1411 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001412
1413 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001414 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1415 break;
1416
James Hogan16d100db2015-12-16 23:49:33 +00001417 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001418 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1419 cause, opc, badvaddr);
1420
1421 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001422 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1423 break;
1424
James Hogan16d100db2015-12-16 23:49:33 +00001425 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001426 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001427 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1428 break;
1429
James Hogan16d100db2015-12-16 23:49:33 +00001430 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001431 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001432 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1433 break;
1434
James Hogan16d100db2015-12-16 23:49:33 +00001435 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001436 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001437 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1438 break;
1439
James Hogan16d100db2015-12-16 23:49:33 +00001440 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001441 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001442 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1443 break;
1444
James Hogan16d100db2015-12-16 23:49:33 +00001445 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001446 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001447 ret = kvm_mips_callbacks->handle_break(vcpu);
1448 break;
1449
James Hogan16d100db2015-12-16 23:49:33 +00001450 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001451 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001452 ret = kvm_mips_callbacks->handle_trap(vcpu);
1453 break;
1454
James Hogan16d100db2015-12-16 23:49:33 +00001455 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001456 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001457 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1458 break;
1459
James Hogan16d100db2015-12-16 23:49:33 +00001460 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001461 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001462 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1463 break;
1464
James Hogan16d100db2015-12-16 23:49:33 +00001465 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001466 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001467 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1468 break;
1469
Sanjay Lal669e8462012-11-21 18:34:02 -08001470 default:
James Hogan122e51d2016-11-28 17:23:14 +00001471 if (cause & CAUSEF_BD)
1472 opc += 1;
1473 inst = 0;
James Hogan6a97c772015-04-23 16:54:35 +01001474 kvm_get_badinstr(opc, vcpu, &inst);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001475 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
James Hogan122e51d2016-11-28 17:23:14 +00001476 exccode, opc, inst, badvaddr,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001477 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001478 kvm_arch_vcpu_dump_regs(vcpu);
1479 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1480 ret = RESUME_HOST;
1481 break;
1482
1483 }
1484
1485skip_emul:
1486 local_irq_disable();
1487
1488 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1489 kvm_mips_deliver_interrupts(vcpu, cause);
1490
1491 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001492 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001493 if (signal_pending(current)) {
1494 run->exit_reason = KVM_EXIT_INTR;
1495 ret = (-EINTR << 2) | RESUME_HOST;
1496 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001497 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001498 }
1499 }
1500
James Hogan98e91b82014-11-18 14:09:12 +00001501 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001502 trace_kvm_reenter(vcpu);
1503
James Hogana2c046e2016-11-18 13:14:37 +00001504 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +01001505
James Hogan98e91b82014-11-18 14:09:12 +00001506 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001507 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1508 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001509 *
1510 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001511 * vector, as it may well cause an [MSA] FP exception if there
1512 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001513 * kvm_mips_csr_die_notifier() for how that is handled).
1514 */
1515 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1516 read_c0_status() & ST0_CU1)
1517 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001518
1519 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1520 read_c0_config5() & MIPS_CONF5_MSAEN)
1521 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001522 }
1523
James Hoganc4c6f2c2015-02-04 10:52:03 +00001524 /* Disable HTW before returning to guest or host */
1525 htw_stop();
1526
Sanjay Lal669e8462012-11-21 18:34:02 -08001527 return ret;
1528}
1529
James Hogan98e91b82014-11-18 14:09:12 +00001530/* Enable FPU for guest and restore context */
1531void kvm_own_fpu(struct kvm_vcpu *vcpu)
1532{
1533 struct mips_coproc *cop0 = vcpu->arch.cop0;
1534 unsigned int sr, cfg5;
1535
1536 preempt_disable();
1537
James Hogan539cb89fb2015-03-05 11:43:36 +00001538 sr = kvm_read_c0_guest_status(cop0);
1539
1540 /*
1541 * If MSA state is already live, it is undefined how it interacts with
1542 * FR=0 FPU state, and we don't want to hit reserved instruction
1543 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1544 * play it safe and save it first.
1545 *
1546 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1547 * get called when guest CU1 is set, however we can't trust the guest
1548 * not to clobber the status register directly via the commpage.
1549 */
1550 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001551 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001552 kvm_lose_fpu(vcpu);
1553
James Hogan98e91b82014-11-18 14:09:12 +00001554 /*
1555 * Enable FPU for guest
1556 * We set FR and FRE according to guest context
1557 */
James Hogan98e91b82014-11-18 14:09:12 +00001558 change_c0_status(ST0_CU1 | ST0_FR, sr);
1559 if (cpu_has_fre) {
1560 cfg5 = kvm_read_c0_guest_config5(cop0);
1561 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1562 }
1563 enable_fpu_hazard();
1564
1565 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001566 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001567 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001568 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001569 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1570 } else {
1571 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001572 }
1573
1574 preempt_enable();
1575}
1576
James Hogan539cb89fb2015-03-05 11:43:36 +00001577#ifdef CONFIG_CPU_HAS_MSA
1578/* Enable MSA for guest and restore context */
1579void kvm_own_msa(struct kvm_vcpu *vcpu)
1580{
1581 struct mips_coproc *cop0 = vcpu->arch.cop0;
1582 unsigned int sr, cfg5;
1583
1584 preempt_disable();
1585
1586 /*
1587 * Enable FPU if enabled in guest, since we're restoring FPU context
1588 * anyway. We set FR and FRE according to guest context.
1589 */
1590 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1591 sr = kvm_read_c0_guest_status(cop0);
1592
1593 /*
1594 * If FR=0 FPU state is already live, it is undefined how it
1595 * interacts with MSA state, so play it safe and save it first.
1596 */
1597 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001598 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1599 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001600 kvm_lose_fpu(vcpu);
1601
1602 change_c0_status(ST0_CU1 | ST0_FR, sr);
1603 if (sr & ST0_CU1 && cpu_has_fre) {
1604 cfg5 = kvm_read_c0_guest_config5(cop0);
1605 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1606 }
1607 }
1608
1609 /* Enable MSA for guest */
1610 set_c0_config5(MIPS_CONF5_MSAEN);
1611 enable_fpu_hazard();
1612
James Hoganf9431762016-06-14 09:40:10 +01001613 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1614 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001615 /*
1616 * Guest FPU state already loaded, only restore upper MSA state
1617 */
1618 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001619 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001620 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001621 break;
1622 case 0:
1623 /* Neither FPU or MSA already active, restore full MSA state */
1624 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001625 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001626 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001627 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001628 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1629 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001630 break;
1631 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001632 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001633 break;
1634 }
1635
1636 preempt_enable();
1637}
1638#endif
1639
1640/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001641void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1642{
1643 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001644 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001645 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001646 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001647 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001648 }
James Hoganf9431762016-06-14 09:40:10 +01001649 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001650 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001651 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001652 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001653 }
1654 preempt_enable();
1655}
1656
James Hogan539cb89fb2015-03-05 11:43:36 +00001657/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001658void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1659{
1660 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001661 * FPU & MSA get disabled in root context (hardware) when it is disabled
1662 * in guest context (software), but the register state in the hardware
1663 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001664 * before saving.
1665 */
1666
1667 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001668 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001669 set_c0_config5(MIPS_CONF5_MSAEN);
1670 enable_fpu_hazard();
1671
1672 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001673 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001674
1675 /* Disable MSA & FPU */
1676 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001677 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001678 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001679 disable_fpu_hazard();
1680 }
James Hoganf9431762016-06-14 09:40:10 +01001681 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1682 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001683 set_c0_status(ST0_CU1);
1684 enable_fpu_hazard();
1685
1686 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001687 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001688 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001689
1690 /* Disable FPU */
1691 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001692 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001693 }
1694 preempt_enable();
1695}
1696
1697/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001698 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1699 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1700 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001701 */
1702static int kvm_mips_csr_die_notify(struct notifier_block *self,
1703 unsigned long cmd, void *ptr)
1704{
1705 struct die_args *args = (struct die_args *)ptr;
1706 struct pt_regs *regs = args->regs;
1707 unsigned long pc;
1708
James Hogan539cb89fb2015-03-05 11:43:36 +00001709 /* Only interested in FPE and MSAFPE */
1710 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001711 return NOTIFY_DONE;
1712
1713 /* Return immediately if guest context isn't active */
1714 if (!(current->flags & PF_VCPU))
1715 return NOTIFY_DONE;
1716
1717 /* Should never get here from user mode */
1718 BUG_ON(user_mode(regs));
1719
1720 pc = instruction_pointer(regs);
1721 switch (cmd) {
1722 case DIE_FP:
1723 /* match 2nd instruction in __kvm_restore_fcsr */
1724 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1725 return NOTIFY_DONE;
1726 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001727 case DIE_MSAFP:
1728 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1729 if (!cpu_has_msa ||
1730 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1731 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1732 return NOTIFY_DONE;
1733 break;
James Hogan98e91b82014-11-18 14:09:12 +00001734 }
1735
1736 /* Move PC forward a little and continue executing */
1737 instruction_pointer(regs) += 4;
1738
1739 return NOTIFY_STOP;
1740}
1741
1742static struct notifier_block kvm_mips_csr_die_notifier = {
1743 .notifier_call = kvm_mips_csr_die_notify,
1744};
1745
James Hogan2db9d232015-12-16 23:49:32 +00001746static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001747{
1748 int ret;
1749
James Hogan1e5217f52016-06-23 17:34:45 +01001750 ret = kvm_mips_entry_setup();
1751 if (ret)
1752 return ret;
1753
Sanjay Lal669e8462012-11-21 18:34:02 -08001754 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1755
1756 if (ret)
1757 return ret;
1758
James Hogan98e91b82014-11-18 14:09:12 +00001759 register_die_notifier(&kvm_mips_csr_die_notifier);
1760
Sanjay Lal669e8462012-11-21 18:34:02 -08001761 return 0;
1762}
1763
James Hogan2db9d232015-12-16 23:49:32 +00001764static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001765{
1766 kvm_exit();
1767
James Hogan98e91b82014-11-18 14:09:12 +00001768 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001769}
1770
1771module_init(kvm_mips_init);
1772module_exit(kvm_mips_exit);
1773
1774EXPORT_TRACEPOINT_SYMBOL(kvm_exit);