blob: 7c76768ff364ed5bfedaf1191a367fbef95c7e32 [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
James Hogand7b8f892016-06-23 17:34:40 +0100248static inline void dump_handler(const char *symbol, void *start, void *end)
249{
250 u32 *p;
251
252 pr_debug("LEAF(%s)\n", symbol);
253
254 pr_debug("\t.set push\n");
255 pr_debug("\t.set noreorder\n");
256
257 for (p = start; p < (u32 *)end; ++p)
258 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p);
259
260 pr_debug("\t.set\tpop\n");
261
262 pr_debug("\tEND(%s)\n", symbol);
263}
264
Sanjay Lal669e8462012-11-21 18:34:02 -0800265struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
266{
James Hogan90e93112016-06-23 17:34:39 +0100267 int err, size;
James Hogan1f9ca622016-06-23 17:34:46 +0100268 void *gebase, *p, *handler;
Sanjay Lal669e8462012-11-21 18:34:02 -0800269 int i;
270
271 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
272
273 if (!vcpu) {
274 err = -ENOMEM;
275 goto out;
276 }
277
278 err = kvm_vcpu_init(vcpu, kvm, id);
279
280 if (err)
281 goto out_free_cpu;
282
James Hogan6e95bfd2014-05-29 10:16:43 +0100283 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800284
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700285 /*
286 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800287 * guest mode exits
288 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700289 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800290 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700291 else
James Hogan7006e2d2014-05-29 10:16:23 +0100292 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800293
Sanjay Lal669e8462012-11-21 18:34:02 -0800294 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
295
296 if (!gebase) {
297 err = -ENOMEM;
James Hogan585bb8f2015-11-11 14:21:20 +0000298 goto out_uninit_cpu;
Sanjay Lal669e8462012-11-21 18:34:02 -0800299 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100300 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
301 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800302
303 /* Save new ebase */
304 vcpu->arch.guest_ebase = gebase;
305
James Hogan90e93112016-06-23 17:34:39 +0100306 /* Build guest exception vectors dynamically in unmapped memory */
James Hogan1f9ca622016-06-23 17:34:46 +0100307 handler = gebase + 0x2000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800308
309 /* TLB Refill, EXL = 0 */
James Hogan1f9ca622016-06-23 17:34:46 +0100310 kvm_mips_build_exception(gebase, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800311
312 /* General Exception Entry point */
James Hogan1f9ca622016-06-23 17:34:46 +0100313 kvm_mips_build_exception(gebase + 0x180, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800314
315 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
316 for (i = 0; i < 8; i++) {
317 kvm_debug("L1 Vectored handler @ %p\n",
318 gebase + 0x200 + (i * VECTORSPACING));
James Hogan1f9ca622016-06-23 17:34:46 +0100319 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
320 handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800321 }
322
James Hogan90e93112016-06-23 17:34:39 +0100323 /* General exit handler */
James Hogan1f9ca622016-06-23 17:34:46 +0100324 p = handler;
James Hogan90e93112016-06-23 17:34:39 +0100325 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800326
James Hogan90e93112016-06-23 17:34:39 +0100327 /* Guest entry routine */
328 vcpu->arch.vcpu_run = p;
329 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100330
James Hogand7b8f892016-06-23 17:34:40 +0100331 /* Dump the generated code */
332 pr_debug("#include <asm/asm.h>\n");
333 pr_debug("#include <asm/regdef.h>\n");
334 pr_debug("\n");
335 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
336 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
337 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
338
Sanjay Lal669e8462012-11-21 18:34:02 -0800339 /* Invalidate the icache for these ranges */
James Hoganfacaaec2014-05-29 10:16:25 +0100340 local_flush_icache_range((unsigned long)gebase,
341 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800342
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700343 /*
344 * Allocate comm page for guest kernel, a TLB will be reserved for
345 * mapping GVA @ 0xFFFF8000 to this page
346 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800347 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
348
349 if (!vcpu->arch.kseg0_commpage) {
350 err = -ENOMEM;
351 goto out_free_gebase;
352 }
353
James Hogan6e95bfd2014-05-29 10:16:43 +0100354 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800355 kvm_mips_commpage_init(vcpu);
356
357 /* Init */
358 vcpu->arch.last_sched_cpu = -1;
359
360 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100361 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800362
363 return vcpu;
364
365out_free_gebase:
366 kfree(gebase);
367
James Hogan585bb8f2015-11-11 14:21:20 +0000368out_uninit_cpu:
369 kvm_vcpu_uninit(vcpu);
370
Sanjay Lal669e8462012-11-21 18:34:02 -0800371out_free_cpu:
372 kfree(vcpu);
373
374out:
375 return ERR_PTR(err);
376}
377
378void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
379{
380 hrtimer_cancel(&vcpu->arch.comparecount_timer);
381
382 kvm_vcpu_uninit(vcpu);
383
384 kvm_mips_dump_stats(vcpu);
385
James Hoganc6c0a662014-05-29 10:16:44 +0100386 kfree(vcpu->arch.guest_ebase);
387 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700388 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800389}
390
391void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
392{
393 kvm_arch_vcpu_free(vcpu);
394}
395
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700396int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
397 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800398{
David Daneyed829852013-05-23 09:49:10 -0700399 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800400}
401
402int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
403{
404 int r = 0;
405 sigset_t sigsaved;
406
407 if (vcpu->sigset_active)
408 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
409
410 if (vcpu->mmio_needed) {
411 if (!vcpu->mmio_is_write)
412 kvm_mips_complete_mmio_load(vcpu, run);
413 vcpu->mmio_needed = 0;
414 }
415
James Hoganf7982172015-02-04 17:06:37 +0000416 lose_fpu(1);
417
James Hogan044f0f02014-05-29 10:16:32 +0100418 local_irq_disable();
Sanjay Lal669e8462012-11-21 18:34:02 -0800419 /* Check if we have any exceptions/interrupts pending */
420 kvm_mips_deliver_interrupts(vcpu,
421 kvm_read_c0_guest_cause(vcpu->arch.cop0));
422
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200423 guest_enter_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800424
James Hoganc4c6f2c2015-02-04 10:52:03 +0000425 /* Disable hardware page table walking while in guest */
426 htw_stop();
427
James Hogan93258602016-06-14 09:40:14 +0100428 trace_kvm_enter(vcpu);
James Hogan797179b2016-06-09 10:50:43 +0100429 r = vcpu->arch.vcpu_run(run, vcpu);
James Hogan93258602016-06-14 09:40:14 +0100430 trace_kvm_out(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800431
James Hoganc4c6f2c2015-02-04 10:52:03 +0000432 /* Re-enable HTW before enabling interrupts */
433 htw_start();
434
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200435 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800436 local_irq_enable();
437
438 if (vcpu->sigset_active)
439 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
440
441 return r;
442}
443
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700444int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
445 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800446{
447 int intr = (int)irq->irq;
448 struct kvm_vcpu *dvcpu = NULL;
449
450 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
451 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
452 (int)intr);
453
454 if (irq->cpu == -1)
455 dvcpu = vcpu;
456 else
457 dvcpu = vcpu->kvm->vcpus[irq->cpu];
458
459 if (intr == 2 || intr == 3 || intr == 4) {
460 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
461
462 } else if (intr == -2 || intr == -3 || intr == -4) {
463 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
464 } else {
465 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
466 irq->cpu, irq->irq);
467 return -EINVAL;
468 }
469
470 dvcpu->arch.wait = 0;
471
Marcelo Tosatti85773702016-02-19 09:46:39 +0100472 if (swait_active(&dvcpu->wq))
473 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800474
475 return 0;
476}
477
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700478int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
479 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800480{
David Daneyed829852013-05-23 09:49:10 -0700481 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800482}
483
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700484int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
485 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800486{
David Daneyed829852013-05-23 09:49:10 -0700487 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800488}
489
David Daney4c73fb22013-05-23 09:49:09 -0700490static u64 kvm_mips_get_one_regs[] = {
491 KVM_REG_MIPS_R0,
492 KVM_REG_MIPS_R1,
493 KVM_REG_MIPS_R2,
494 KVM_REG_MIPS_R3,
495 KVM_REG_MIPS_R4,
496 KVM_REG_MIPS_R5,
497 KVM_REG_MIPS_R6,
498 KVM_REG_MIPS_R7,
499 KVM_REG_MIPS_R8,
500 KVM_REG_MIPS_R9,
501 KVM_REG_MIPS_R10,
502 KVM_REG_MIPS_R11,
503 KVM_REG_MIPS_R12,
504 KVM_REG_MIPS_R13,
505 KVM_REG_MIPS_R14,
506 KVM_REG_MIPS_R15,
507 KVM_REG_MIPS_R16,
508 KVM_REG_MIPS_R17,
509 KVM_REG_MIPS_R18,
510 KVM_REG_MIPS_R19,
511 KVM_REG_MIPS_R20,
512 KVM_REG_MIPS_R21,
513 KVM_REG_MIPS_R22,
514 KVM_REG_MIPS_R23,
515 KVM_REG_MIPS_R24,
516 KVM_REG_MIPS_R25,
517 KVM_REG_MIPS_R26,
518 KVM_REG_MIPS_R27,
519 KVM_REG_MIPS_R28,
520 KVM_REG_MIPS_R29,
521 KVM_REG_MIPS_R30,
522 KVM_REG_MIPS_R31,
523
524 KVM_REG_MIPS_HI,
525 KVM_REG_MIPS_LO,
526 KVM_REG_MIPS_PC,
527
528 KVM_REG_MIPS_CP0_INDEX,
529 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100530 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700531 KVM_REG_MIPS_CP0_PAGEMASK,
532 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100533 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700534 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100535 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700536 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100537 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700538 KVM_REG_MIPS_CP0_STATUS,
539 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100540 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100541 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700542 KVM_REG_MIPS_CP0_CONFIG,
543 KVM_REG_MIPS_CP0_CONFIG1,
544 KVM_REG_MIPS_CP0_CONFIG2,
545 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100546 KVM_REG_MIPS_CP0_CONFIG4,
547 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700548 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100549 KVM_REG_MIPS_CP0_ERROREPC,
550
551 KVM_REG_MIPS_COUNT_CTL,
552 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100553 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700554};
555
James Hogane5775932016-06-15 19:29:51 +0100556static u64 kvm_mips_get_one_regs_fpu[] = {
557 KVM_REG_MIPS_FCR_IR,
558 KVM_REG_MIPS_FCR_CSR,
559};
560
561static u64 kvm_mips_get_one_regs_msa[] = {
562 KVM_REG_MIPS_MSA_IR,
563 KVM_REG_MIPS_MSA_CSR,
564};
565
James Hogan05108702016-06-15 19:29:56 +0100566static u64 kvm_mips_get_one_regs_kscratch[] = {
567 KVM_REG_MIPS_CP0_KSCRATCH1,
568 KVM_REG_MIPS_CP0_KSCRATCH2,
569 KVM_REG_MIPS_CP0_KSCRATCH3,
570 KVM_REG_MIPS_CP0_KSCRATCH4,
571 KVM_REG_MIPS_CP0_KSCRATCH5,
572 KVM_REG_MIPS_CP0_KSCRATCH6,
573};
574
James Hoganf5c43bd2016-06-15 19:29:49 +0100575static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
576{
577 unsigned long ret;
578
579 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100580 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
581 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
582 /* odd doubles */
583 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
584 ret += 16;
585 }
586 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
587 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hogan05108702016-06-15 19:29:56 +0100588 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
James Hoganf5c43bd2016-06-15 19:29:49 +0100589 ret += kvm_mips_callbacks->num_regs(vcpu);
590
591 return ret;
592}
593
594static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
595{
James Hogane5775932016-06-15 19:29:51 +0100596 u64 index;
597 unsigned int i;
598
James Hoganf5c43bd2016-06-15 19:29:49 +0100599 if (copy_to_user(indices, kvm_mips_get_one_regs,
600 sizeof(kvm_mips_get_one_regs)))
601 return -EFAULT;
602 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
603
James Hogane5775932016-06-15 19:29:51 +0100604 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
605 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
606 sizeof(kvm_mips_get_one_regs_fpu)))
607 return -EFAULT;
608 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
609
610 for (i = 0; i < 32; ++i) {
611 index = KVM_REG_MIPS_FPR_32(i);
612 if (copy_to_user(indices, &index, sizeof(index)))
613 return -EFAULT;
614 ++indices;
615
616 /* skip odd doubles if no F64 */
617 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
618 continue;
619
620 index = KVM_REG_MIPS_FPR_64(i);
621 if (copy_to_user(indices, &index, sizeof(index)))
622 return -EFAULT;
623 ++indices;
624 }
625 }
626
627 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
628 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
629 sizeof(kvm_mips_get_one_regs_msa)))
630 return -EFAULT;
631 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
632
633 for (i = 0; i < 32; ++i) {
634 index = KVM_REG_MIPS_VEC_128(i);
635 if (copy_to_user(indices, &index, sizeof(index)))
636 return -EFAULT;
637 ++indices;
638 }
639 }
640
James Hogan05108702016-06-15 19:29:56 +0100641 for (i = 0; i < 6; ++i) {
642 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
643 continue;
644
645 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
646 sizeof(kvm_mips_get_one_regs_kscratch[i])))
647 return -EFAULT;
648 ++indices;
649 }
650
James Hoganf5c43bd2016-06-15 19:29:49 +0100651 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
652}
653
David Daney4c73fb22013-05-23 09:49:09 -0700654static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
655 const struct kvm_one_reg *reg)
656{
David Daney4c73fb22013-05-23 09:49:09 -0700657 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000658 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100659 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700660 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000661 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000662 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700663
664 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000665 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700666 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
667 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
668 break;
669 case KVM_REG_MIPS_HI:
670 v = (long)vcpu->arch.hi;
671 break;
672 case KVM_REG_MIPS_LO:
673 v = (long)vcpu->arch.lo;
674 break;
675 case KVM_REG_MIPS_PC:
676 v = (long)vcpu->arch.pc;
677 break;
678
James Hogan379245c2014-12-02 15:48:24 +0000679 /* Floating point registers */
680 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
681 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
682 return -EINVAL;
683 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
684 /* Odd singles in top of even double when FR=0 */
685 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
686 v = get_fpr32(&fpu->fpr[idx], 0);
687 else
688 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
689 break;
690 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
691 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
692 return -EINVAL;
693 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
694 /* Can't access odd doubles in FR=0 mode */
695 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
696 return -EINVAL;
697 v = get_fpr64(&fpu->fpr[idx], 0);
698 break;
699 case KVM_REG_MIPS_FCR_IR:
700 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
701 return -EINVAL;
702 v = boot_cpu_data.fpu_id;
703 break;
704 case KVM_REG_MIPS_FCR_CSR:
705 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
706 return -EINVAL;
707 v = fpu->fcr31;
708 break;
709
James Hoganab86bd62014-12-02 15:48:24 +0000710 /* MIPS SIMD Architecture (MSA) registers */
711 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
712 if (!kvm_mips_guest_has_msa(&vcpu->arch))
713 return -EINVAL;
714 /* Can't access MSA registers in FR=0 mode */
715 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
716 return -EINVAL;
717 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
718#ifdef CONFIG_CPU_LITTLE_ENDIAN
719 /* least significant byte first */
720 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
721 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
722#else
723 /* most significant byte first */
724 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
725 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
726#endif
727 break;
728 case KVM_REG_MIPS_MSA_IR:
729 if (!kvm_mips_guest_has_msa(&vcpu->arch))
730 return -EINVAL;
731 v = boot_cpu_data.msa_id;
732 break;
733 case KVM_REG_MIPS_MSA_CSR:
734 if (!kvm_mips_guest_has_msa(&vcpu->arch))
735 return -EINVAL;
736 v = fpu->msacsr;
737 break;
738
James Hogan379245c2014-12-02 15:48:24 +0000739 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700740 case KVM_REG_MIPS_CP0_INDEX:
741 v = (long)kvm_read_c0_guest_index(cop0);
742 break;
743 case KVM_REG_MIPS_CP0_CONTEXT:
744 v = (long)kvm_read_c0_guest_context(cop0);
745 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100746 case KVM_REG_MIPS_CP0_USERLOCAL:
747 v = (long)kvm_read_c0_guest_userlocal(cop0);
748 break;
David Daney4c73fb22013-05-23 09:49:09 -0700749 case KVM_REG_MIPS_CP0_PAGEMASK:
750 v = (long)kvm_read_c0_guest_pagemask(cop0);
751 break;
752 case KVM_REG_MIPS_CP0_WIRED:
753 v = (long)kvm_read_c0_guest_wired(cop0);
754 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100755 case KVM_REG_MIPS_CP0_HWRENA:
756 v = (long)kvm_read_c0_guest_hwrena(cop0);
757 break;
David Daney4c73fb22013-05-23 09:49:09 -0700758 case KVM_REG_MIPS_CP0_BADVADDR:
759 v = (long)kvm_read_c0_guest_badvaddr(cop0);
760 break;
761 case KVM_REG_MIPS_CP0_ENTRYHI:
762 v = (long)kvm_read_c0_guest_entryhi(cop0);
763 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100764 case KVM_REG_MIPS_CP0_COMPARE:
765 v = (long)kvm_read_c0_guest_compare(cop0);
766 break;
David Daney4c73fb22013-05-23 09:49:09 -0700767 case KVM_REG_MIPS_CP0_STATUS:
768 v = (long)kvm_read_c0_guest_status(cop0);
769 break;
770 case KVM_REG_MIPS_CP0_CAUSE:
771 v = (long)kvm_read_c0_guest_cause(cop0);
772 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100773 case KVM_REG_MIPS_CP0_EPC:
774 v = (long)kvm_read_c0_guest_epc(cop0);
775 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100776 case KVM_REG_MIPS_CP0_PRID:
777 v = (long)kvm_read_c0_guest_prid(cop0);
778 break;
David Daney4c73fb22013-05-23 09:49:09 -0700779 case KVM_REG_MIPS_CP0_CONFIG:
780 v = (long)kvm_read_c0_guest_config(cop0);
781 break;
782 case KVM_REG_MIPS_CP0_CONFIG1:
783 v = (long)kvm_read_c0_guest_config1(cop0);
784 break;
785 case KVM_REG_MIPS_CP0_CONFIG2:
786 v = (long)kvm_read_c0_guest_config2(cop0);
787 break;
788 case KVM_REG_MIPS_CP0_CONFIG3:
789 v = (long)kvm_read_c0_guest_config3(cop0);
790 break;
James Hoganc7716072014-06-26 15:11:29 +0100791 case KVM_REG_MIPS_CP0_CONFIG4:
792 v = (long)kvm_read_c0_guest_config4(cop0);
793 break;
794 case KVM_REG_MIPS_CP0_CONFIG5:
795 v = (long)kvm_read_c0_guest_config5(cop0);
796 break;
David Daney4c73fb22013-05-23 09:49:09 -0700797 case KVM_REG_MIPS_CP0_CONFIG7:
798 v = (long)kvm_read_c0_guest_config7(cop0);
799 break;
James Hogane93d4c12014-06-26 13:47:22 +0100800 case KVM_REG_MIPS_CP0_ERROREPC:
801 v = (long)kvm_read_c0_guest_errorepc(cop0);
802 break;
James Hogan05108702016-06-15 19:29:56 +0100803 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
804 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
805 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
806 return -EINVAL;
807 switch (idx) {
808 case 2:
809 v = (long)kvm_read_c0_guest_kscratch1(cop0);
810 break;
811 case 3:
812 v = (long)kvm_read_c0_guest_kscratch2(cop0);
813 break;
814 case 4:
815 v = (long)kvm_read_c0_guest_kscratch3(cop0);
816 break;
817 case 5:
818 v = (long)kvm_read_c0_guest_kscratch4(cop0);
819 break;
820 case 6:
821 v = (long)kvm_read_c0_guest_kscratch5(cop0);
822 break;
823 case 7:
824 v = (long)kvm_read_c0_guest_kscratch6(cop0);
825 break;
826 }
827 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100828 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100829 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100830 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
831 if (ret)
832 return ret;
833 break;
David Daney4c73fb22013-05-23 09:49:09 -0700834 }
David Daney681865d2013-06-10 12:33:48 -0700835 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
836 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700837
David Daney681865d2013-06-10 12:33:48 -0700838 return put_user(v, uaddr64);
839 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
840 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
841 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700842
David Daney681865d2013-06-10 12:33:48 -0700843 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000844 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
845 void __user *uaddr = (void __user *)(long)reg->addr;
846
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200847 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700848 } else {
849 return -EINVAL;
850 }
David Daney4c73fb22013-05-23 09:49:09 -0700851}
852
853static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
854 const struct kvm_one_reg *reg)
855{
David Daney4c73fb22013-05-23 09:49:09 -0700856 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000857 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
858 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000859 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000860 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700861
David Daney681865d2013-06-10 12:33:48 -0700862 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
863 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
864
865 if (get_user(v, uaddr64) != 0)
866 return -EFAULT;
867 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
868 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
869 s32 v32;
870
871 if (get_user(v32, uaddr32) != 0)
872 return -EFAULT;
873 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000874 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
875 void __user *uaddr = (void __user *)(long)reg->addr;
876
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200877 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700878 } else {
879 return -EINVAL;
880 }
David Daney4c73fb22013-05-23 09:49:09 -0700881
882 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000883 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700884 case KVM_REG_MIPS_R0:
885 /* Silently ignore requests to set $0 */
886 break;
887 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
888 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
889 break;
890 case KVM_REG_MIPS_HI:
891 vcpu->arch.hi = v;
892 break;
893 case KVM_REG_MIPS_LO:
894 vcpu->arch.lo = v;
895 break;
896 case KVM_REG_MIPS_PC:
897 vcpu->arch.pc = v;
898 break;
899
James Hogan379245c2014-12-02 15:48:24 +0000900 /* Floating point registers */
901 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
902 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
903 return -EINVAL;
904 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
905 /* Odd singles in top of even double when FR=0 */
906 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
907 set_fpr32(&fpu->fpr[idx], 0, v);
908 else
909 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
910 break;
911 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
912 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
913 return -EINVAL;
914 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
915 /* Can't access odd doubles in FR=0 mode */
916 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
917 return -EINVAL;
918 set_fpr64(&fpu->fpr[idx], 0, v);
919 break;
920 case KVM_REG_MIPS_FCR_IR:
921 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
922 return -EINVAL;
923 /* Read-only */
924 break;
925 case KVM_REG_MIPS_FCR_CSR:
926 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
927 return -EINVAL;
928 fpu->fcr31 = v;
929 break;
930
James Hoganab86bd62014-12-02 15:48:24 +0000931 /* MIPS SIMD Architecture (MSA) registers */
932 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
933 if (!kvm_mips_guest_has_msa(&vcpu->arch))
934 return -EINVAL;
935 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
936#ifdef CONFIG_CPU_LITTLE_ENDIAN
937 /* least significant byte first */
938 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
939 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
940#else
941 /* most significant byte first */
942 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
943 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
944#endif
945 break;
946 case KVM_REG_MIPS_MSA_IR:
947 if (!kvm_mips_guest_has_msa(&vcpu->arch))
948 return -EINVAL;
949 /* Read-only */
950 break;
951 case KVM_REG_MIPS_MSA_CSR:
952 if (!kvm_mips_guest_has_msa(&vcpu->arch))
953 return -EINVAL;
954 fpu->msacsr = v;
955 break;
956
James Hogan379245c2014-12-02 15:48:24 +0000957 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700958 case KVM_REG_MIPS_CP0_INDEX:
959 kvm_write_c0_guest_index(cop0, v);
960 break;
961 case KVM_REG_MIPS_CP0_CONTEXT:
962 kvm_write_c0_guest_context(cop0, v);
963 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100964 case KVM_REG_MIPS_CP0_USERLOCAL:
965 kvm_write_c0_guest_userlocal(cop0, v);
966 break;
David Daney4c73fb22013-05-23 09:49:09 -0700967 case KVM_REG_MIPS_CP0_PAGEMASK:
968 kvm_write_c0_guest_pagemask(cop0, v);
969 break;
970 case KVM_REG_MIPS_CP0_WIRED:
971 kvm_write_c0_guest_wired(cop0, v);
972 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100973 case KVM_REG_MIPS_CP0_HWRENA:
974 kvm_write_c0_guest_hwrena(cop0, v);
975 break;
David Daney4c73fb22013-05-23 09:49:09 -0700976 case KVM_REG_MIPS_CP0_BADVADDR:
977 kvm_write_c0_guest_badvaddr(cop0, v);
978 break;
979 case KVM_REG_MIPS_CP0_ENTRYHI:
980 kvm_write_c0_guest_entryhi(cop0, v);
981 break;
982 case KVM_REG_MIPS_CP0_STATUS:
983 kvm_write_c0_guest_status(cop0, v);
984 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100985 case KVM_REG_MIPS_CP0_EPC:
986 kvm_write_c0_guest_epc(cop0, v);
987 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100988 case KVM_REG_MIPS_CP0_PRID:
989 kvm_write_c0_guest_prid(cop0, v);
990 break;
David Daney4c73fb22013-05-23 09:49:09 -0700991 case KVM_REG_MIPS_CP0_ERROREPC:
992 kvm_write_c0_guest_errorepc(cop0, v);
993 break;
James Hogan05108702016-06-15 19:29:56 +0100994 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
995 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
996 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
997 return -EINVAL;
998 switch (idx) {
999 case 2:
1000 kvm_write_c0_guest_kscratch1(cop0, v);
1001 break;
1002 case 3:
1003 kvm_write_c0_guest_kscratch2(cop0, v);
1004 break;
1005 case 4:
1006 kvm_write_c0_guest_kscratch3(cop0, v);
1007 break;
1008 case 5:
1009 kvm_write_c0_guest_kscratch4(cop0, v);
1010 break;
1011 case 6:
1012 kvm_write_c0_guest_kscratch5(cop0, v);
1013 break;
1014 case 7:
1015 kvm_write_c0_guest_kscratch6(cop0, v);
1016 break;
1017 }
1018 break;
James Hoganf8be02d2014-05-29 10:16:29 +01001019 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -07001020 default:
James Hogancc68d222016-06-15 19:29:48 +01001021 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -07001022 }
1023 return 0;
1024}
1025
James Hogan5fafd8742014-12-08 23:07:56 +00001026static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1027 struct kvm_enable_cap *cap)
1028{
1029 int r = 0;
1030
1031 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1032 return -EINVAL;
1033 if (cap->flags)
1034 return -EINVAL;
1035 if (cap->args[0])
1036 return -EINVAL;
1037
1038 switch (cap->cap) {
1039 case KVM_CAP_MIPS_FPU:
1040 vcpu->arch.fpu_enabled = true;
1041 break;
James Hogand952bd02014-12-08 23:07:56 +00001042 case KVM_CAP_MIPS_MSA:
1043 vcpu->arch.msa_enabled = true;
1044 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001045 default:
1046 r = -EINVAL;
1047 break;
1048 }
1049
1050 return r;
1051}
1052
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001053long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1054 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -08001055{
1056 struct kvm_vcpu *vcpu = filp->private_data;
1057 void __user *argp = (void __user *)arg;
1058 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001059
1060 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -07001061 case KVM_SET_ONE_REG:
1062 case KVM_GET_ONE_REG: {
1063 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001064
David Daney4c73fb22013-05-23 09:49:09 -07001065 if (copy_from_user(&reg, argp, sizeof(reg)))
1066 return -EFAULT;
1067 if (ioctl == KVM_SET_ONE_REG)
1068 return kvm_mips_set_reg(vcpu, &reg);
1069 else
1070 return kvm_mips_get_reg(vcpu, &reg);
1071 }
1072 case KVM_GET_REG_LIST: {
1073 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -07001074 struct kvm_reg_list reg_list;
1075 unsigned n;
1076
1077 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1078 return -EFAULT;
1079 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +01001080 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -07001081 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1082 return -EFAULT;
1083 if (n < reg_list.n)
1084 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +01001085 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -07001086 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001087 case KVM_NMI:
1088 /* Treat the NMI as a CPU reset */
1089 r = kvm_mips_reset_vcpu(vcpu);
1090 break;
1091 case KVM_INTERRUPT:
1092 {
1093 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001094
Sanjay Lal669e8462012-11-21 18:34:02 -08001095 r = -EFAULT;
1096 if (copy_from_user(&irq, argp, sizeof(irq)))
1097 goto out;
1098
Sanjay Lal669e8462012-11-21 18:34:02 -08001099 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1100 irq.irq);
1101
1102 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1103 break;
1104 }
James Hogan5fafd8742014-12-08 23:07:56 +00001105 case KVM_ENABLE_CAP: {
1106 struct kvm_enable_cap cap;
1107
1108 r = -EFAULT;
1109 if (copy_from_user(&cap, argp, sizeof(cap)))
1110 goto out;
1111 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1112 break;
1113 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001114 default:
David Daney4c73fb22013-05-23 09:49:09 -07001115 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001116 }
1117
1118out:
1119 return r;
1120}
1121
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001122/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001123int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1124{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001125 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001126 struct kvm_memory_slot *memslot;
1127 unsigned long ga, ga_end;
1128 int is_dirty = 0;
1129 int r;
1130 unsigned long n;
1131
1132 mutex_lock(&kvm->slots_lock);
1133
1134 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1135 if (r)
1136 goto out;
1137
1138 /* If nothing is dirty, don't bother messing with page tables. */
1139 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001140 slots = kvm_memslots(kvm);
1141 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001142
1143 ga = memslot->base_gfn << PAGE_SHIFT;
1144 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1145
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001146 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
1147 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -08001148
1149 n = kvm_dirty_bitmap_bytes(memslot);
1150 memset(memslot->dirty_bitmap, 0, n);
1151 }
1152
1153 r = 0;
1154out:
1155 mutex_unlock(&kvm->slots_lock);
1156 return r;
1157
1158}
1159
1160long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1161{
1162 long r;
1163
1164 switch (ioctl) {
1165 default:
David Daneyed829852013-05-23 09:49:10 -07001166 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001167 }
1168
1169 return r;
1170}
1171
1172int kvm_arch_init(void *opaque)
1173{
Sanjay Lal669e8462012-11-21 18:34:02 -08001174 if (kvm_mips_callbacks) {
1175 kvm_err("kvm: module already exists\n");
1176 return -EEXIST;
1177 }
1178
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001179 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001180}
1181
1182void kvm_arch_exit(void)
1183{
1184 kvm_mips_callbacks = NULL;
1185}
1186
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001187int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1188 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001189{
David Daneyed829852013-05-23 09:49:10 -07001190 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001191}
1192
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001193int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1194 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001195{
David Daneyed829852013-05-23 09:49:10 -07001196 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001197}
1198
Dominik Dingel31928aa2014-12-04 15:47:07 +01001199void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001200{
Sanjay Lal669e8462012-11-21 18:34:02 -08001201}
1202
1203int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1204{
David Daneyed829852013-05-23 09:49:10 -07001205 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001206}
1207
1208int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1209{
David Daneyed829852013-05-23 09:49:10 -07001210 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001211}
1212
1213int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1214{
1215 return VM_FAULT_SIGBUS;
1216}
1217
Alexander Graf784aa3d2014-07-14 18:27:35 +02001218int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001219{
1220 int r;
1221
1222 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001223 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001224 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001225 r = 1;
1226 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001227 case KVM_CAP_COALESCED_MMIO:
1228 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1229 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001230 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001231 /* We don't handle systems with inconsistent cpu_has_fpu */
1232 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001233 break;
James Hogand952bd02014-12-08 23:07:56 +00001234 case KVM_CAP_MIPS_MSA:
1235 /*
1236 * We don't support MSA vector partitioning yet:
1237 * 1) It would require explicit support which can't be tested
1238 * yet due to lack of support in current hardware.
1239 * 2) It extends the state that would need to be saved/restored
1240 * by e.g. QEMU for migration.
1241 *
1242 * When vector partitioning hardware becomes available, support
1243 * could be added by requiring a flag when enabling
1244 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1245 * to save/restore the appropriate extra state.
1246 */
1247 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1248 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001249 default:
1250 r = 0;
1251 break;
1252 }
1253 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001254}
1255
1256int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1257{
1258 return kvm_mips_pending_timer(vcpu);
1259}
1260
1261int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1262{
1263 int i;
1264 struct mips_coproc *cop0;
1265
1266 if (!vcpu)
1267 return -1;
1268
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001269 kvm_debug("VCPU Register Dump:\n");
1270 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1271 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001272
1273 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001274 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001275 vcpu->arch.gprs[i],
1276 vcpu->arch.gprs[i + 1],
1277 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1278 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001279 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1280 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001281
1282 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001283 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1284 kvm_read_c0_guest_status(cop0),
1285 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001286
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001287 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001288
1289 return 0;
1290}
1291
1292int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1293{
1294 int i;
1295
David Daney8d17dd02013-05-23 09:49:08 -07001296 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001297 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001298 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001299 vcpu->arch.hi = regs->hi;
1300 vcpu->arch.lo = regs->lo;
1301 vcpu->arch.pc = regs->pc;
1302
David Daney4c73fb22013-05-23 09:49:09 -07001303 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001304}
1305
1306int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1307{
1308 int i;
1309
David Daney8d17dd02013-05-23 09:49:08 -07001310 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001311 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001312
1313 regs->hi = vcpu->arch.hi;
1314 regs->lo = vcpu->arch.lo;
1315 regs->pc = vcpu->arch.pc;
1316
David Daney4c73fb22013-05-23 09:49:09 -07001317 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001318}
1319
James Hogan0fae34f2014-05-29 10:16:39 +01001320static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001321{
1322 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1323
1324 kvm_mips_callbacks->queue_timer_int(vcpu);
1325
1326 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001327 if (swait_active(&vcpu->wq))
1328 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001329}
1330
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001331/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001332static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001333{
1334 struct kvm_vcpu *vcpu;
1335
1336 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1337 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001338 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001339}
1340
1341int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1342{
1343 kvm_mips_callbacks->vcpu_init(vcpu);
1344 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1345 HRTIMER_MODE_REL);
1346 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001347 return 0;
1348}
1349
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001350int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1351 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001352{
1353 return 0;
1354}
1355
1356/* Initial guest state */
1357int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1358{
1359 return kvm_mips_callbacks->vcpu_setup(vcpu);
1360}
1361
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001362static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001363{
James Hogan8cffd192016-06-09 14:19:08 +01001364 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001365
Sanjay Lal669e8462012-11-21 18:34:02 -08001366 if (cpu_has_dsp)
1367 status |= (ST0_MX);
1368
1369 write_c0_status(status);
1370 ehb();
1371}
1372
1373/*
1374 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1375 */
1376int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1377{
James Hogan8cffd192016-06-09 14:19:08 +01001378 u32 cause = vcpu->arch.host_cp0_cause;
1379 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1380 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001381 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1382 enum emulation_result er = EMULATE_DONE;
1383 int ret = RESUME_GUEST;
1384
James Hoganc4c6f2c2015-02-04 10:52:03 +00001385 /* re-enable HTW before enabling interrupts */
1386 htw_start();
1387
Sanjay Lal669e8462012-11-21 18:34:02 -08001388 /* Set a default exit reason */
1389 run->exit_reason = KVM_EXIT_UNKNOWN;
1390 run->ready_for_interrupt_injection = 1;
1391
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001392 /*
1393 * Set the appropriate status bits based on host CPU features,
1394 * before we hit the scheduler
1395 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001396 kvm_mips_set_c0_status();
1397
1398 local_irq_enable();
1399
1400 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1401 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001402 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001403
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001404 /*
1405 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001406 * causing an exception to be delivered to the Guest Kernel
1407 */
1408 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1409 if (er == EMULATE_PRIV_FAIL) {
1410 goto skip_emul;
1411 } else if (er == EMULATE_FAIL) {
1412 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1413 ret = RESUME_HOST;
1414 goto skip_emul;
1415 }
1416
1417 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001418 case EXCCODE_INT:
1419 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001420
1421 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001422
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001423 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001424 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001425
1426 ret = RESUME_GUEST;
1427 break;
1428
James Hogan16d100db2015-12-16 23:49:33 +00001429 case EXCCODE_CPU:
1430 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001431
1432 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001433 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1434 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001435 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001436 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001437 break;
1438
James Hogan16d100db2015-12-16 23:49:33 +00001439 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001440 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001441 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1442 break;
1443
James Hogan16d100db2015-12-16 23:49:33 +00001444 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001445 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1446 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1447 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001448
1449 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001450 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1451 break;
1452
James Hogan16d100db2015-12-16 23:49:33 +00001453 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001454 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1455 cause, opc, badvaddr);
1456
1457 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001458 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1459 break;
1460
James Hogan16d100db2015-12-16 23:49:33 +00001461 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001462 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001463 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1464 break;
1465
James Hogan16d100db2015-12-16 23:49:33 +00001466 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001467 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001468 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1469 break;
1470
James Hogan16d100db2015-12-16 23:49:33 +00001471 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001472 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001473 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1474 break;
1475
James Hogan16d100db2015-12-16 23:49:33 +00001476 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001477 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001478 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1479 break;
1480
James Hogan16d100db2015-12-16 23:49:33 +00001481 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001482 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001483 ret = kvm_mips_callbacks->handle_break(vcpu);
1484 break;
1485
James Hogan16d100db2015-12-16 23:49:33 +00001486 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001487 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001488 ret = kvm_mips_callbacks->handle_trap(vcpu);
1489 break;
1490
James Hogan16d100db2015-12-16 23:49:33 +00001491 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001492 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001493 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1494 break;
1495
James Hogan16d100db2015-12-16 23:49:33 +00001496 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001497 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001498 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1499 break;
1500
James Hogan16d100db2015-12-16 23:49:33 +00001501 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001502 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001503 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1504 break;
1505
Sanjay Lal669e8462012-11-21 18:34:02 -08001506 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001507 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1508 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1509 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001510 kvm_arch_vcpu_dump_regs(vcpu);
1511 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1512 ret = RESUME_HOST;
1513 break;
1514
1515 }
1516
1517skip_emul:
1518 local_irq_disable();
1519
1520 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1521 kvm_mips_deliver_interrupts(vcpu, cause);
1522
1523 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001524 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001525 if (signal_pending(current)) {
1526 run->exit_reason = KVM_EXIT_INTR;
1527 ret = (-EINTR << 2) | RESUME_HOST;
1528 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001529 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001530 }
1531 }
1532
James Hogan98e91b82014-11-18 14:09:12 +00001533 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001534 trace_kvm_reenter(vcpu);
1535
James Hogan98e91b82014-11-18 14:09:12 +00001536 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001537 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1538 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001539 *
1540 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001541 * vector, as it may well cause an [MSA] FP exception if there
1542 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001543 * kvm_mips_csr_die_notifier() for how that is handled).
1544 */
1545 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1546 read_c0_status() & ST0_CU1)
1547 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001548
1549 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1550 read_c0_config5() & MIPS_CONF5_MSAEN)
1551 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001552 }
1553
James Hoganc4c6f2c2015-02-04 10:52:03 +00001554 /* Disable HTW before returning to guest or host */
1555 htw_stop();
1556
Sanjay Lal669e8462012-11-21 18:34:02 -08001557 return ret;
1558}
1559
James Hogan98e91b82014-11-18 14:09:12 +00001560/* Enable FPU for guest and restore context */
1561void kvm_own_fpu(struct kvm_vcpu *vcpu)
1562{
1563 struct mips_coproc *cop0 = vcpu->arch.cop0;
1564 unsigned int sr, cfg5;
1565
1566 preempt_disable();
1567
James Hogan539cb89fb2015-03-05 11:43:36 +00001568 sr = kvm_read_c0_guest_status(cop0);
1569
1570 /*
1571 * If MSA state is already live, it is undefined how it interacts with
1572 * FR=0 FPU state, and we don't want to hit reserved instruction
1573 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1574 * play it safe and save it first.
1575 *
1576 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1577 * get called when guest CU1 is set, however we can't trust the guest
1578 * not to clobber the status register directly via the commpage.
1579 */
1580 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001581 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001582 kvm_lose_fpu(vcpu);
1583
James Hogan98e91b82014-11-18 14:09:12 +00001584 /*
1585 * Enable FPU for guest
1586 * We set FR and FRE according to guest context
1587 */
James Hogan98e91b82014-11-18 14:09:12 +00001588 change_c0_status(ST0_CU1 | ST0_FR, sr);
1589 if (cpu_has_fre) {
1590 cfg5 = kvm_read_c0_guest_config5(cop0);
1591 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1592 }
1593 enable_fpu_hazard();
1594
1595 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001596 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001597 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001598 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001599 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1600 } else {
1601 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001602 }
1603
1604 preempt_enable();
1605}
1606
James Hogan539cb89fb2015-03-05 11:43:36 +00001607#ifdef CONFIG_CPU_HAS_MSA
1608/* Enable MSA for guest and restore context */
1609void kvm_own_msa(struct kvm_vcpu *vcpu)
1610{
1611 struct mips_coproc *cop0 = vcpu->arch.cop0;
1612 unsigned int sr, cfg5;
1613
1614 preempt_disable();
1615
1616 /*
1617 * Enable FPU if enabled in guest, since we're restoring FPU context
1618 * anyway. We set FR and FRE according to guest context.
1619 */
1620 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1621 sr = kvm_read_c0_guest_status(cop0);
1622
1623 /*
1624 * If FR=0 FPU state is already live, it is undefined how it
1625 * interacts with MSA state, so play it safe and save it first.
1626 */
1627 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001628 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1629 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001630 kvm_lose_fpu(vcpu);
1631
1632 change_c0_status(ST0_CU1 | ST0_FR, sr);
1633 if (sr & ST0_CU1 && cpu_has_fre) {
1634 cfg5 = kvm_read_c0_guest_config5(cop0);
1635 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1636 }
1637 }
1638
1639 /* Enable MSA for guest */
1640 set_c0_config5(MIPS_CONF5_MSAEN);
1641 enable_fpu_hazard();
1642
James Hoganf9431762016-06-14 09:40:10 +01001643 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1644 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001645 /*
1646 * Guest FPU state already loaded, only restore upper MSA state
1647 */
1648 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001649 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001650 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001651 break;
1652 case 0:
1653 /* Neither FPU or MSA already active, restore full MSA state */
1654 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001655 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001656 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001657 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001658 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1659 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001660 break;
1661 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001662 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001663 break;
1664 }
1665
1666 preempt_enable();
1667}
1668#endif
1669
1670/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001671void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1672{
1673 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001674 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001675 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001676 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001677 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001678 }
James Hoganf9431762016-06-14 09:40:10 +01001679 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001680 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001681 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001682 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001683 }
1684 preempt_enable();
1685}
1686
James Hogan539cb89fb2015-03-05 11:43:36 +00001687/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001688void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1689{
1690 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001691 * FPU & MSA get disabled in root context (hardware) when it is disabled
1692 * in guest context (software), but the register state in the hardware
1693 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001694 * before saving.
1695 */
1696
1697 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001698 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001699 set_c0_config5(MIPS_CONF5_MSAEN);
1700 enable_fpu_hazard();
1701
1702 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001703 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001704
1705 /* Disable MSA & FPU */
1706 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001707 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001708 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001709 disable_fpu_hazard();
1710 }
James Hoganf9431762016-06-14 09:40:10 +01001711 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1712 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001713 set_c0_status(ST0_CU1);
1714 enable_fpu_hazard();
1715
1716 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001717 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001718 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001719
1720 /* Disable FPU */
1721 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001722 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001723 }
1724 preempt_enable();
1725}
1726
1727/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001728 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1729 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1730 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001731 */
1732static int kvm_mips_csr_die_notify(struct notifier_block *self,
1733 unsigned long cmd, void *ptr)
1734{
1735 struct die_args *args = (struct die_args *)ptr;
1736 struct pt_regs *regs = args->regs;
1737 unsigned long pc;
1738
James Hogan539cb89fb2015-03-05 11:43:36 +00001739 /* Only interested in FPE and MSAFPE */
1740 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001741 return NOTIFY_DONE;
1742
1743 /* Return immediately if guest context isn't active */
1744 if (!(current->flags & PF_VCPU))
1745 return NOTIFY_DONE;
1746
1747 /* Should never get here from user mode */
1748 BUG_ON(user_mode(regs));
1749
1750 pc = instruction_pointer(regs);
1751 switch (cmd) {
1752 case DIE_FP:
1753 /* match 2nd instruction in __kvm_restore_fcsr */
1754 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1755 return NOTIFY_DONE;
1756 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001757 case DIE_MSAFP:
1758 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1759 if (!cpu_has_msa ||
1760 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1761 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1762 return NOTIFY_DONE;
1763 break;
James Hogan98e91b82014-11-18 14:09:12 +00001764 }
1765
1766 /* Move PC forward a little and continue executing */
1767 instruction_pointer(regs) += 4;
1768
1769 return NOTIFY_STOP;
1770}
1771
1772static struct notifier_block kvm_mips_csr_die_notifier = {
1773 .notifier_call = kvm_mips_csr_die_notify,
1774};
1775
James Hogan2db9d232015-12-16 23:49:32 +00001776static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001777{
1778 int ret;
1779
James Hogan1e5217f52016-06-23 17:34:45 +01001780 ret = kvm_mips_entry_setup();
1781 if (ret)
1782 return ret;
1783
Sanjay Lal669e8462012-11-21 18:34:02 -08001784 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1785
1786 if (ret)
1787 return ret;
1788
James Hogan98e91b82014-11-18 14:09:12 +00001789 register_die_notifier(&kvm_mips_csr_die_notifier);
1790
Sanjay Lal669e8462012-11-21 18:34:02 -08001791 return 0;
1792}
1793
James Hogan2db9d232015-12-16 23:49:32 +00001794static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001795{
1796 kvm_exit();
1797
James Hogan98e91b82014-11-18 14:09:12 +00001798 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001799}
1800
1801module_init(kvm_mips_init);
1802module_exit(kvm_mips_exit);
1803
1804EXPORT_TRACEPOINT_SYMBOL(kvm_exit);