blob: a6ea084b4d9d5fe500d219cc3d03901f143b4388 [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
James Hogan2a06dab2016-07-08 11:53:26 +0100303 /*
304 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
305 * limits us to the low 512MB of physical address space. If the memory
306 * we allocate is out of range, just give up now.
307 */
308 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) {
309 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
310 gebase);
311 err = -ENOMEM;
312 goto out_free_gebase;
313 }
314
Sanjay Lal669e8462012-11-21 18:34:02 -0800315 /* Save new ebase */
316 vcpu->arch.guest_ebase = gebase;
317
James Hogan90e93112016-06-23 17:34:39 +0100318 /* Build guest exception vectors dynamically in unmapped memory */
James Hogan1f9ca622016-06-23 17:34:46 +0100319 handler = gebase + 0x2000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800320
321 /* TLB Refill, EXL = 0 */
James Hogan1f9ca622016-06-23 17:34:46 +0100322 kvm_mips_build_exception(gebase, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800323
324 /* General Exception Entry point */
James Hogan1f9ca622016-06-23 17:34:46 +0100325 kvm_mips_build_exception(gebase + 0x180, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800326
327 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
328 for (i = 0; i < 8; i++) {
329 kvm_debug("L1 Vectored handler @ %p\n",
330 gebase + 0x200 + (i * VECTORSPACING));
James Hogan1f9ca622016-06-23 17:34:46 +0100331 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
332 handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800333 }
334
James Hogan90e93112016-06-23 17:34:39 +0100335 /* General exit handler */
James Hogan1f9ca622016-06-23 17:34:46 +0100336 p = handler;
James Hogan90e93112016-06-23 17:34:39 +0100337 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800338
James Hogan90e93112016-06-23 17:34:39 +0100339 /* Guest entry routine */
340 vcpu->arch.vcpu_run = p;
341 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100342
James Hogand7b8f892016-06-23 17:34:40 +0100343 /* Dump the generated code */
344 pr_debug("#include <asm/asm.h>\n");
345 pr_debug("#include <asm/regdef.h>\n");
346 pr_debug("\n");
347 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
348 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
349 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
350
Sanjay Lal669e8462012-11-21 18:34:02 -0800351 /* Invalidate the icache for these ranges */
James Hoganfacaaec2014-05-29 10:16:25 +0100352 local_flush_icache_range((unsigned long)gebase,
353 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800354
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700355 /*
356 * Allocate comm page for guest kernel, a TLB will be reserved for
357 * mapping GVA @ 0xFFFF8000 to this page
358 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800359 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
360
361 if (!vcpu->arch.kseg0_commpage) {
362 err = -ENOMEM;
363 goto out_free_gebase;
364 }
365
James Hogan6e95bfd2014-05-29 10:16:43 +0100366 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800367 kvm_mips_commpage_init(vcpu);
368
369 /* Init */
370 vcpu->arch.last_sched_cpu = -1;
371
372 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100373 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800374
375 return vcpu;
376
377out_free_gebase:
378 kfree(gebase);
379
James Hogan585bb8f2015-11-11 14:21:20 +0000380out_uninit_cpu:
381 kvm_vcpu_uninit(vcpu);
382
Sanjay Lal669e8462012-11-21 18:34:02 -0800383out_free_cpu:
384 kfree(vcpu);
385
386out:
387 return ERR_PTR(err);
388}
389
390void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
391{
392 hrtimer_cancel(&vcpu->arch.comparecount_timer);
393
394 kvm_vcpu_uninit(vcpu);
395
396 kvm_mips_dump_stats(vcpu);
397
James Hoganc6c0a662014-05-29 10:16:44 +0100398 kfree(vcpu->arch.guest_ebase);
399 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700400 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800401}
402
403void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
404{
405 kvm_arch_vcpu_free(vcpu);
406}
407
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700408int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
409 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800410{
David Daneyed829852013-05-23 09:49:10 -0700411 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800412}
413
414int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
415{
416 int r = 0;
417 sigset_t sigsaved;
418
419 if (vcpu->sigset_active)
420 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
421
422 if (vcpu->mmio_needed) {
423 if (!vcpu->mmio_is_write)
424 kvm_mips_complete_mmio_load(vcpu, run);
425 vcpu->mmio_needed = 0;
426 }
427
James Hoganf7982172015-02-04 17:06:37 +0000428 lose_fpu(1);
429
James Hogan044f0f02014-05-29 10:16:32 +0100430 local_irq_disable();
Sanjay Lal669e8462012-11-21 18:34:02 -0800431 /* Check if we have any exceptions/interrupts pending */
432 kvm_mips_deliver_interrupts(vcpu,
433 kvm_read_c0_guest_cause(vcpu->arch.cop0));
434
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200435 guest_enter_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800436
James Hoganc4c6f2c2015-02-04 10:52:03 +0000437 /* Disable hardware page table walking while in guest */
438 htw_stop();
439
James Hogan93258602016-06-14 09:40:14 +0100440 trace_kvm_enter(vcpu);
James Hogan797179b2016-06-09 10:50:43 +0100441 r = vcpu->arch.vcpu_run(run, vcpu);
James Hogan93258602016-06-14 09:40:14 +0100442 trace_kvm_out(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800443
James Hoganc4c6f2c2015-02-04 10:52:03 +0000444 /* Re-enable HTW before enabling interrupts */
445 htw_start();
446
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200447 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800448 local_irq_enable();
449
450 if (vcpu->sigset_active)
451 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
452
453 return r;
454}
455
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700456int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
457 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800458{
459 int intr = (int)irq->irq;
460 struct kvm_vcpu *dvcpu = NULL;
461
462 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
463 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
464 (int)intr);
465
466 if (irq->cpu == -1)
467 dvcpu = vcpu;
468 else
469 dvcpu = vcpu->kvm->vcpus[irq->cpu];
470
471 if (intr == 2 || intr == 3 || intr == 4) {
472 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
473
474 } else if (intr == -2 || intr == -3 || intr == -4) {
475 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
476 } else {
477 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
478 irq->cpu, irq->irq);
479 return -EINVAL;
480 }
481
482 dvcpu->arch.wait = 0;
483
Marcelo Tosatti85773702016-02-19 09:46:39 +0100484 if (swait_active(&dvcpu->wq))
485 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800486
487 return 0;
488}
489
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700490int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
491 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800492{
David Daneyed829852013-05-23 09:49:10 -0700493 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800494}
495
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700496int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
497 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800498{
David Daneyed829852013-05-23 09:49:10 -0700499 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800500}
501
David Daney4c73fb22013-05-23 09:49:09 -0700502static u64 kvm_mips_get_one_regs[] = {
503 KVM_REG_MIPS_R0,
504 KVM_REG_MIPS_R1,
505 KVM_REG_MIPS_R2,
506 KVM_REG_MIPS_R3,
507 KVM_REG_MIPS_R4,
508 KVM_REG_MIPS_R5,
509 KVM_REG_MIPS_R6,
510 KVM_REG_MIPS_R7,
511 KVM_REG_MIPS_R8,
512 KVM_REG_MIPS_R9,
513 KVM_REG_MIPS_R10,
514 KVM_REG_MIPS_R11,
515 KVM_REG_MIPS_R12,
516 KVM_REG_MIPS_R13,
517 KVM_REG_MIPS_R14,
518 KVM_REG_MIPS_R15,
519 KVM_REG_MIPS_R16,
520 KVM_REG_MIPS_R17,
521 KVM_REG_MIPS_R18,
522 KVM_REG_MIPS_R19,
523 KVM_REG_MIPS_R20,
524 KVM_REG_MIPS_R21,
525 KVM_REG_MIPS_R22,
526 KVM_REG_MIPS_R23,
527 KVM_REG_MIPS_R24,
528 KVM_REG_MIPS_R25,
529 KVM_REG_MIPS_R26,
530 KVM_REG_MIPS_R27,
531 KVM_REG_MIPS_R28,
532 KVM_REG_MIPS_R29,
533 KVM_REG_MIPS_R30,
534 KVM_REG_MIPS_R31,
535
James Hogan70e92c7e2016-07-04 19:35:11 +0100536#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700537 KVM_REG_MIPS_HI,
538 KVM_REG_MIPS_LO,
James Hogan70e92c7e2016-07-04 19:35:11 +0100539#endif
David Daney4c73fb22013-05-23 09:49:09 -0700540 KVM_REG_MIPS_PC,
541
542 KVM_REG_MIPS_CP0_INDEX,
543 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100544 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700545 KVM_REG_MIPS_CP0_PAGEMASK,
546 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100547 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700548 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100549 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700550 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100551 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700552 KVM_REG_MIPS_CP0_STATUS,
553 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100554 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100555 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700556 KVM_REG_MIPS_CP0_CONFIG,
557 KVM_REG_MIPS_CP0_CONFIG1,
558 KVM_REG_MIPS_CP0_CONFIG2,
559 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100560 KVM_REG_MIPS_CP0_CONFIG4,
561 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700562 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100563 KVM_REG_MIPS_CP0_ERROREPC,
564
565 KVM_REG_MIPS_COUNT_CTL,
566 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100567 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700568};
569
James Hogane5775932016-06-15 19:29:51 +0100570static u64 kvm_mips_get_one_regs_fpu[] = {
571 KVM_REG_MIPS_FCR_IR,
572 KVM_REG_MIPS_FCR_CSR,
573};
574
575static u64 kvm_mips_get_one_regs_msa[] = {
576 KVM_REG_MIPS_MSA_IR,
577 KVM_REG_MIPS_MSA_CSR,
578};
579
James Hogan05108702016-06-15 19:29:56 +0100580static u64 kvm_mips_get_one_regs_kscratch[] = {
581 KVM_REG_MIPS_CP0_KSCRATCH1,
582 KVM_REG_MIPS_CP0_KSCRATCH2,
583 KVM_REG_MIPS_CP0_KSCRATCH3,
584 KVM_REG_MIPS_CP0_KSCRATCH4,
585 KVM_REG_MIPS_CP0_KSCRATCH5,
586 KVM_REG_MIPS_CP0_KSCRATCH6,
587};
588
James Hoganf5c43bd2016-06-15 19:29:49 +0100589static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
590{
591 unsigned long ret;
592
593 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100594 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
595 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
596 /* odd doubles */
597 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
598 ret += 16;
599 }
600 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
601 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hogan05108702016-06-15 19:29:56 +0100602 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
James Hoganf5c43bd2016-06-15 19:29:49 +0100603 ret += kvm_mips_callbacks->num_regs(vcpu);
604
605 return ret;
606}
607
608static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
609{
James Hogane5775932016-06-15 19:29:51 +0100610 u64 index;
611 unsigned int i;
612
James Hoganf5c43bd2016-06-15 19:29:49 +0100613 if (copy_to_user(indices, kvm_mips_get_one_regs,
614 sizeof(kvm_mips_get_one_regs)))
615 return -EFAULT;
616 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
617
James Hogane5775932016-06-15 19:29:51 +0100618 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
619 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
620 sizeof(kvm_mips_get_one_regs_fpu)))
621 return -EFAULT;
622 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
623
624 for (i = 0; i < 32; ++i) {
625 index = KVM_REG_MIPS_FPR_32(i);
626 if (copy_to_user(indices, &index, sizeof(index)))
627 return -EFAULT;
628 ++indices;
629
630 /* skip odd doubles if no F64 */
631 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
632 continue;
633
634 index = KVM_REG_MIPS_FPR_64(i);
635 if (copy_to_user(indices, &index, sizeof(index)))
636 return -EFAULT;
637 ++indices;
638 }
639 }
640
641 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
642 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
643 sizeof(kvm_mips_get_one_regs_msa)))
644 return -EFAULT;
645 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
646
647 for (i = 0; i < 32; ++i) {
648 index = KVM_REG_MIPS_VEC_128(i);
649 if (copy_to_user(indices, &index, sizeof(index)))
650 return -EFAULT;
651 ++indices;
652 }
653 }
654
James Hogan05108702016-06-15 19:29:56 +0100655 for (i = 0; i < 6; ++i) {
656 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
657 continue;
658
659 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
660 sizeof(kvm_mips_get_one_regs_kscratch[i])))
661 return -EFAULT;
662 ++indices;
663 }
664
James Hoganf5c43bd2016-06-15 19:29:49 +0100665 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
666}
667
David Daney4c73fb22013-05-23 09:49:09 -0700668static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
669 const struct kvm_one_reg *reg)
670{
David Daney4c73fb22013-05-23 09:49:09 -0700671 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000672 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100673 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700674 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000675 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000676 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700677
678 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000679 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700680 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
681 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
682 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100683#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700684 case KVM_REG_MIPS_HI:
685 v = (long)vcpu->arch.hi;
686 break;
687 case KVM_REG_MIPS_LO:
688 v = (long)vcpu->arch.lo;
689 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100690#endif
David Daney4c73fb22013-05-23 09:49:09 -0700691 case KVM_REG_MIPS_PC:
692 v = (long)vcpu->arch.pc;
693 break;
694
James Hogan379245c2014-12-02 15:48:24 +0000695 /* Floating point registers */
696 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
697 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
698 return -EINVAL;
699 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
700 /* Odd singles in top of even double when FR=0 */
701 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
702 v = get_fpr32(&fpu->fpr[idx], 0);
703 else
704 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
705 break;
706 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
707 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
708 return -EINVAL;
709 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
710 /* Can't access odd doubles in FR=0 mode */
711 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
712 return -EINVAL;
713 v = get_fpr64(&fpu->fpr[idx], 0);
714 break;
715 case KVM_REG_MIPS_FCR_IR:
716 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
717 return -EINVAL;
718 v = boot_cpu_data.fpu_id;
719 break;
720 case KVM_REG_MIPS_FCR_CSR:
721 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
722 return -EINVAL;
723 v = fpu->fcr31;
724 break;
725
James Hoganab86bd62014-12-02 15:48:24 +0000726 /* MIPS SIMD Architecture (MSA) registers */
727 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
728 if (!kvm_mips_guest_has_msa(&vcpu->arch))
729 return -EINVAL;
730 /* Can't access MSA registers in FR=0 mode */
731 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
732 return -EINVAL;
733 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
734#ifdef CONFIG_CPU_LITTLE_ENDIAN
735 /* least significant byte first */
736 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
737 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
738#else
739 /* most significant byte first */
740 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
741 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
742#endif
743 break;
744 case KVM_REG_MIPS_MSA_IR:
745 if (!kvm_mips_guest_has_msa(&vcpu->arch))
746 return -EINVAL;
747 v = boot_cpu_data.msa_id;
748 break;
749 case KVM_REG_MIPS_MSA_CSR:
750 if (!kvm_mips_guest_has_msa(&vcpu->arch))
751 return -EINVAL;
752 v = fpu->msacsr;
753 break;
754
James Hogan379245c2014-12-02 15:48:24 +0000755 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700756 case KVM_REG_MIPS_CP0_INDEX:
757 v = (long)kvm_read_c0_guest_index(cop0);
758 break;
759 case KVM_REG_MIPS_CP0_CONTEXT:
760 v = (long)kvm_read_c0_guest_context(cop0);
761 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100762 case KVM_REG_MIPS_CP0_USERLOCAL:
763 v = (long)kvm_read_c0_guest_userlocal(cop0);
764 break;
David Daney4c73fb22013-05-23 09:49:09 -0700765 case KVM_REG_MIPS_CP0_PAGEMASK:
766 v = (long)kvm_read_c0_guest_pagemask(cop0);
767 break;
768 case KVM_REG_MIPS_CP0_WIRED:
769 v = (long)kvm_read_c0_guest_wired(cop0);
770 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100771 case KVM_REG_MIPS_CP0_HWRENA:
772 v = (long)kvm_read_c0_guest_hwrena(cop0);
773 break;
David Daney4c73fb22013-05-23 09:49:09 -0700774 case KVM_REG_MIPS_CP0_BADVADDR:
775 v = (long)kvm_read_c0_guest_badvaddr(cop0);
776 break;
777 case KVM_REG_MIPS_CP0_ENTRYHI:
778 v = (long)kvm_read_c0_guest_entryhi(cop0);
779 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100780 case KVM_REG_MIPS_CP0_COMPARE:
781 v = (long)kvm_read_c0_guest_compare(cop0);
782 break;
David Daney4c73fb22013-05-23 09:49:09 -0700783 case KVM_REG_MIPS_CP0_STATUS:
784 v = (long)kvm_read_c0_guest_status(cop0);
785 break;
786 case KVM_REG_MIPS_CP0_CAUSE:
787 v = (long)kvm_read_c0_guest_cause(cop0);
788 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100789 case KVM_REG_MIPS_CP0_EPC:
790 v = (long)kvm_read_c0_guest_epc(cop0);
791 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100792 case KVM_REG_MIPS_CP0_PRID:
793 v = (long)kvm_read_c0_guest_prid(cop0);
794 break;
David Daney4c73fb22013-05-23 09:49:09 -0700795 case KVM_REG_MIPS_CP0_CONFIG:
796 v = (long)kvm_read_c0_guest_config(cop0);
797 break;
798 case KVM_REG_MIPS_CP0_CONFIG1:
799 v = (long)kvm_read_c0_guest_config1(cop0);
800 break;
801 case KVM_REG_MIPS_CP0_CONFIG2:
802 v = (long)kvm_read_c0_guest_config2(cop0);
803 break;
804 case KVM_REG_MIPS_CP0_CONFIG3:
805 v = (long)kvm_read_c0_guest_config3(cop0);
806 break;
James Hoganc7716072014-06-26 15:11:29 +0100807 case KVM_REG_MIPS_CP0_CONFIG4:
808 v = (long)kvm_read_c0_guest_config4(cop0);
809 break;
810 case KVM_REG_MIPS_CP0_CONFIG5:
811 v = (long)kvm_read_c0_guest_config5(cop0);
812 break;
David Daney4c73fb22013-05-23 09:49:09 -0700813 case KVM_REG_MIPS_CP0_CONFIG7:
814 v = (long)kvm_read_c0_guest_config7(cop0);
815 break;
James Hogane93d4c12014-06-26 13:47:22 +0100816 case KVM_REG_MIPS_CP0_ERROREPC:
817 v = (long)kvm_read_c0_guest_errorepc(cop0);
818 break;
James Hogan05108702016-06-15 19:29:56 +0100819 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
820 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
821 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
822 return -EINVAL;
823 switch (idx) {
824 case 2:
825 v = (long)kvm_read_c0_guest_kscratch1(cop0);
826 break;
827 case 3:
828 v = (long)kvm_read_c0_guest_kscratch2(cop0);
829 break;
830 case 4:
831 v = (long)kvm_read_c0_guest_kscratch3(cop0);
832 break;
833 case 5:
834 v = (long)kvm_read_c0_guest_kscratch4(cop0);
835 break;
836 case 6:
837 v = (long)kvm_read_c0_guest_kscratch5(cop0);
838 break;
839 case 7:
840 v = (long)kvm_read_c0_guest_kscratch6(cop0);
841 break;
842 }
843 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100844 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100845 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100846 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
847 if (ret)
848 return ret;
849 break;
David Daney4c73fb22013-05-23 09:49:09 -0700850 }
David Daney681865d2013-06-10 12:33:48 -0700851 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
852 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700853
David Daney681865d2013-06-10 12:33:48 -0700854 return put_user(v, uaddr64);
855 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
856 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
857 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700858
David Daney681865d2013-06-10 12:33:48 -0700859 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000860 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
861 void __user *uaddr = (void __user *)(long)reg->addr;
862
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200863 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700864 } else {
865 return -EINVAL;
866 }
David Daney4c73fb22013-05-23 09:49:09 -0700867}
868
869static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
870 const struct kvm_one_reg *reg)
871{
David Daney4c73fb22013-05-23 09:49:09 -0700872 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000873 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
874 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000875 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000876 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700877
David Daney681865d2013-06-10 12:33:48 -0700878 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
879 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
880
881 if (get_user(v, uaddr64) != 0)
882 return -EFAULT;
883 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
884 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
885 s32 v32;
886
887 if (get_user(v32, uaddr32) != 0)
888 return -EFAULT;
889 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000890 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
891 void __user *uaddr = (void __user *)(long)reg->addr;
892
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200893 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700894 } else {
895 return -EINVAL;
896 }
David Daney4c73fb22013-05-23 09:49:09 -0700897
898 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000899 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700900 case KVM_REG_MIPS_R0:
901 /* Silently ignore requests to set $0 */
902 break;
903 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
904 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
905 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100906#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700907 case KVM_REG_MIPS_HI:
908 vcpu->arch.hi = v;
909 break;
910 case KVM_REG_MIPS_LO:
911 vcpu->arch.lo = v;
912 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100913#endif
David Daney4c73fb22013-05-23 09:49:09 -0700914 case KVM_REG_MIPS_PC:
915 vcpu->arch.pc = v;
916 break;
917
James Hogan379245c2014-12-02 15:48:24 +0000918 /* Floating point registers */
919 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
920 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
921 return -EINVAL;
922 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
923 /* Odd singles in top of even double when FR=0 */
924 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
925 set_fpr32(&fpu->fpr[idx], 0, v);
926 else
927 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
928 break;
929 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
930 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
931 return -EINVAL;
932 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
933 /* Can't access odd doubles in FR=0 mode */
934 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
935 return -EINVAL;
936 set_fpr64(&fpu->fpr[idx], 0, v);
937 break;
938 case KVM_REG_MIPS_FCR_IR:
939 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
940 return -EINVAL;
941 /* Read-only */
942 break;
943 case KVM_REG_MIPS_FCR_CSR:
944 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
945 return -EINVAL;
946 fpu->fcr31 = v;
947 break;
948
James Hoganab86bd62014-12-02 15:48:24 +0000949 /* MIPS SIMD Architecture (MSA) registers */
950 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
951 if (!kvm_mips_guest_has_msa(&vcpu->arch))
952 return -EINVAL;
953 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
954#ifdef CONFIG_CPU_LITTLE_ENDIAN
955 /* least significant byte first */
956 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
957 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
958#else
959 /* most significant byte first */
960 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
961 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
962#endif
963 break;
964 case KVM_REG_MIPS_MSA_IR:
965 if (!kvm_mips_guest_has_msa(&vcpu->arch))
966 return -EINVAL;
967 /* Read-only */
968 break;
969 case KVM_REG_MIPS_MSA_CSR:
970 if (!kvm_mips_guest_has_msa(&vcpu->arch))
971 return -EINVAL;
972 fpu->msacsr = v;
973 break;
974
James Hogan379245c2014-12-02 15:48:24 +0000975 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700976 case KVM_REG_MIPS_CP0_INDEX:
977 kvm_write_c0_guest_index(cop0, v);
978 break;
979 case KVM_REG_MIPS_CP0_CONTEXT:
980 kvm_write_c0_guest_context(cop0, v);
981 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100982 case KVM_REG_MIPS_CP0_USERLOCAL:
983 kvm_write_c0_guest_userlocal(cop0, v);
984 break;
David Daney4c73fb22013-05-23 09:49:09 -0700985 case KVM_REG_MIPS_CP0_PAGEMASK:
986 kvm_write_c0_guest_pagemask(cop0, v);
987 break;
988 case KVM_REG_MIPS_CP0_WIRED:
989 kvm_write_c0_guest_wired(cop0, v);
990 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100991 case KVM_REG_MIPS_CP0_HWRENA:
992 kvm_write_c0_guest_hwrena(cop0, v);
993 break;
David Daney4c73fb22013-05-23 09:49:09 -0700994 case KVM_REG_MIPS_CP0_BADVADDR:
995 kvm_write_c0_guest_badvaddr(cop0, v);
996 break;
997 case KVM_REG_MIPS_CP0_ENTRYHI:
998 kvm_write_c0_guest_entryhi(cop0, v);
999 break;
1000 case KVM_REG_MIPS_CP0_STATUS:
1001 kvm_write_c0_guest_status(cop0, v);
1002 break;
James Hoganfb6df0c2014-05-29 10:16:27 +01001003 case KVM_REG_MIPS_CP0_EPC:
1004 kvm_write_c0_guest_epc(cop0, v);
1005 break;
James Hogan1068eaa2014-06-26 13:56:52 +01001006 case KVM_REG_MIPS_CP0_PRID:
1007 kvm_write_c0_guest_prid(cop0, v);
1008 break;
David Daney4c73fb22013-05-23 09:49:09 -07001009 case KVM_REG_MIPS_CP0_ERROREPC:
1010 kvm_write_c0_guest_errorepc(cop0, v);
1011 break;
James Hogan05108702016-06-15 19:29:56 +01001012 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
1013 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
1014 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
1015 return -EINVAL;
1016 switch (idx) {
1017 case 2:
1018 kvm_write_c0_guest_kscratch1(cop0, v);
1019 break;
1020 case 3:
1021 kvm_write_c0_guest_kscratch2(cop0, v);
1022 break;
1023 case 4:
1024 kvm_write_c0_guest_kscratch3(cop0, v);
1025 break;
1026 case 5:
1027 kvm_write_c0_guest_kscratch4(cop0, v);
1028 break;
1029 case 6:
1030 kvm_write_c0_guest_kscratch5(cop0, v);
1031 break;
1032 case 7:
1033 kvm_write_c0_guest_kscratch6(cop0, v);
1034 break;
1035 }
1036 break;
James Hoganf8be02d2014-05-29 10:16:29 +01001037 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -07001038 default:
James Hogancc68d222016-06-15 19:29:48 +01001039 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -07001040 }
1041 return 0;
1042}
1043
James Hogan5fafd8742014-12-08 23:07:56 +00001044static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1045 struct kvm_enable_cap *cap)
1046{
1047 int r = 0;
1048
1049 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1050 return -EINVAL;
1051 if (cap->flags)
1052 return -EINVAL;
1053 if (cap->args[0])
1054 return -EINVAL;
1055
1056 switch (cap->cap) {
1057 case KVM_CAP_MIPS_FPU:
1058 vcpu->arch.fpu_enabled = true;
1059 break;
James Hogand952bd02014-12-08 23:07:56 +00001060 case KVM_CAP_MIPS_MSA:
1061 vcpu->arch.msa_enabled = true;
1062 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001063 default:
1064 r = -EINVAL;
1065 break;
1066 }
1067
1068 return r;
1069}
1070
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001071long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1072 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -08001073{
1074 struct kvm_vcpu *vcpu = filp->private_data;
1075 void __user *argp = (void __user *)arg;
1076 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001077
1078 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -07001079 case KVM_SET_ONE_REG:
1080 case KVM_GET_ONE_REG: {
1081 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001082
David Daney4c73fb22013-05-23 09:49:09 -07001083 if (copy_from_user(&reg, argp, sizeof(reg)))
1084 return -EFAULT;
1085 if (ioctl == KVM_SET_ONE_REG)
1086 return kvm_mips_set_reg(vcpu, &reg);
1087 else
1088 return kvm_mips_get_reg(vcpu, &reg);
1089 }
1090 case KVM_GET_REG_LIST: {
1091 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -07001092 struct kvm_reg_list reg_list;
1093 unsigned n;
1094
1095 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1096 return -EFAULT;
1097 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +01001098 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -07001099 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1100 return -EFAULT;
1101 if (n < reg_list.n)
1102 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +01001103 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -07001104 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001105 case KVM_NMI:
1106 /* Treat the NMI as a CPU reset */
1107 r = kvm_mips_reset_vcpu(vcpu);
1108 break;
1109 case KVM_INTERRUPT:
1110 {
1111 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001112
Sanjay Lal669e8462012-11-21 18:34:02 -08001113 r = -EFAULT;
1114 if (copy_from_user(&irq, argp, sizeof(irq)))
1115 goto out;
1116
Sanjay Lal669e8462012-11-21 18:34:02 -08001117 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1118 irq.irq);
1119
1120 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1121 break;
1122 }
James Hogan5fafd8742014-12-08 23:07:56 +00001123 case KVM_ENABLE_CAP: {
1124 struct kvm_enable_cap cap;
1125
1126 r = -EFAULT;
1127 if (copy_from_user(&cap, argp, sizeof(cap)))
1128 goto out;
1129 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1130 break;
1131 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001132 default:
David Daney4c73fb22013-05-23 09:49:09 -07001133 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001134 }
1135
1136out:
1137 return r;
1138}
1139
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001140/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001141int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1142{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001143 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001144 struct kvm_memory_slot *memslot;
1145 unsigned long ga, ga_end;
1146 int is_dirty = 0;
1147 int r;
1148 unsigned long n;
1149
1150 mutex_lock(&kvm->slots_lock);
1151
1152 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1153 if (r)
1154 goto out;
1155
1156 /* If nothing is dirty, don't bother messing with page tables. */
1157 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001158 slots = kvm_memslots(kvm);
1159 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001160
1161 ga = memslot->base_gfn << PAGE_SHIFT;
1162 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1163
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001164 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
1165 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -08001166
1167 n = kvm_dirty_bitmap_bytes(memslot);
1168 memset(memslot->dirty_bitmap, 0, n);
1169 }
1170
1171 r = 0;
1172out:
1173 mutex_unlock(&kvm->slots_lock);
1174 return r;
1175
1176}
1177
1178long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1179{
1180 long r;
1181
1182 switch (ioctl) {
1183 default:
David Daneyed829852013-05-23 09:49:10 -07001184 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001185 }
1186
1187 return r;
1188}
1189
1190int kvm_arch_init(void *opaque)
1191{
Sanjay Lal669e8462012-11-21 18:34:02 -08001192 if (kvm_mips_callbacks) {
1193 kvm_err("kvm: module already exists\n");
1194 return -EEXIST;
1195 }
1196
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001197 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001198}
1199
1200void kvm_arch_exit(void)
1201{
1202 kvm_mips_callbacks = NULL;
1203}
1204
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001205int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1206 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001207{
David Daneyed829852013-05-23 09:49:10 -07001208 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001209}
1210
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001211int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1212 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001213{
David Daneyed829852013-05-23 09:49:10 -07001214 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001215}
1216
Dominik Dingel31928aa2014-12-04 15:47:07 +01001217void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001218{
Sanjay Lal669e8462012-11-21 18:34:02 -08001219}
1220
1221int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1222{
David Daneyed829852013-05-23 09:49:10 -07001223 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001224}
1225
1226int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1227{
David Daneyed829852013-05-23 09:49:10 -07001228 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001229}
1230
1231int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1232{
1233 return VM_FAULT_SIGBUS;
1234}
1235
Alexander Graf784aa3d2014-07-14 18:27:35 +02001236int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001237{
1238 int r;
1239
1240 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001241 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001242 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001243 r = 1;
1244 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001245 case KVM_CAP_COALESCED_MMIO:
1246 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1247 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001248 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001249 /* We don't handle systems with inconsistent cpu_has_fpu */
1250 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001251 break;
James Hogand952bd02014-12-08 23:07:56 +00001252 case KVM_CAP_MIPS_MSA:
1253 /*
1254 * We don't support MSA vector partitioning yet:
1255 * 1) It would require explicit support which can't be tested
1256 * yet due to lack of support in current hardware.
1257 * 2) It extends the state that would need to be saved/restored
1258 * by e.g. QEMU for migration.
1259 *
1260 * When vector partitioning hardware becomes available, support
1261 * could be added by requiring a flag when enabling
1262 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1263 * to save/restore the appropriate extra state.
1264 */
1265 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1266 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001267 default:
1268 r = 0;
1269 break;
1270 }
1271 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001272}
1273
1274int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1275{
1276 return kvm_mips_pending_timer(vcpu);
1277}
1278
1279int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1280{
1281 int i;
1282 struct mips_coproc *cop0;
1283
1284 if (!vcpu)
1285 return -1;
1286
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001287 kvm_debug("VCPU Register Dump:\n");
1288 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1289 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001290
1291 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001292 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001293 vcpu->arch.gprs[i],
1294 vcpu->arch.gprs[i + 1],
1295 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1296 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001297 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1298 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001299
1300 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001301 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1302 kvm_read_c0_guest_status(cop0),
1303 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001304
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001305 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001306
1307 return 0;
1308}
1309
1310int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1311{
1312 int i;
1313
David Daney8d17dd02013-05-23 09:49:08 -07001314 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001315 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001316 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001317 vcpu->arch.hi = regs->hi;
1318 vcpu->arch.lo = regs->lo;
1319 vcpu->arch.pc = regs->pc;
1320
David Daney4c73fb22013-05-23 09:49:09 -07001321 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001322}
1323
1324int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1325{
1326 int i;
1327
David Daney8d17dd02013-05-23 09:49:08 -07001328 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001329 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001330
1331 regs->hi = vcpu->arch.hi;
1332 regs->lo = vcpu->arch.lo;
1333 regs->pc = vcpu->arch.pc;
1334
David Daney4c73fb22013-05-23 09:49:09 -07001335 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001336}
1337
James Hogan0fae34f2014-05-29 10:16:39 +01001338static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001339{
1340 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1341
1342 kvm_mips_callbacks->queue_timer_int(vcpu);
1343
1344 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001345 if (swait_active(&vcpu->wq))
1346 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001347}
1348
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001349/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001350static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001351{
1352 struct kvm_vcpu *vcpu;
1353
1354 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1355 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001356 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001357}
1358
1359int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1360{
1361 kvm_mips_callbacks->vcpu_init(vcpu);
1362 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1363 HRTIMER_MODE_REL);
1364 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001365 return 0;
1366}
1367
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001368int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1369 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001370{
1371 return 0;
1372}
1373
1374/* Initial guest state */
1375int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1376{
1377 return kvm_mips_callbacks->vcpu_setup(vcpu);
1378}
1379
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001380static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001381{
James Hogan8cffd192016-06-09 14:19:08 +01001382 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001383
Sanjay Lal669e8462012-11-21 18:34:02 -08001384 if (cpu_has_dsp)
1385 status |= (ST0_MX);
1386
1387 write_c0_status(status);
1388 ehb();
1389}
1390
1391/*
1392 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1393 */
1394int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1395{
James Hogan8cffd192016-06-09 14:19:08 +01001396 u32 cause = vcpu->arch.host_cp0_cause;
1397 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1398 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001399 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1400 enum emulation_result er = EMULATE_DONE;
1401 int ret = RESUME_GUEST;
1402
James Hoganc4c6f2c2015-02-04 10:52:03 +00001403 /* re-enable HTW before enabling interrupts */
1404 htw_start();
1405
Sanjay Lal669e8462012-11-21 18:34:02 -08001406 /* Set a default exit reason */
1407 run->exit_reason = KVM_EXIT_UNKNOWN;
1408 run->ready_for_interrupt_injection = 1;
1409
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001410 /*
1411 * Set the appropriate status bits based on host CPU features,
1412 * before we hit the scheduler
1413 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001414 kvm_mips_set_c0_status();
1415
1416 local_irq_enable();
1417
1418 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1419 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001420 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001421
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001422 /*
1423 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001424 * causing an exception to be delivered to the Guest Kernel
1425 */
1426 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1427 if (er == EMULATE_PRIV_FAIL) {
1428 goto skip_emul;
1429 } else if (er == EMULATE_FAIL) {
1430 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1431 ret = RESUME_HOST;
1432 goto skip_emul;
1433 }
1434
1435 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001436 case EXCCODE_INT:
1437 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001438
1439 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001440
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001441 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001442 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001443
1444 ret = RESUME_GUEST;
1445 break;
1446
James Hogan16d100db2015-12-16 23:49:33 +00001447 case EXCCODE_CPU:
1448 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001449
1450 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001451 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1452 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001453 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001454 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001455 break;
1456
James Hogan16d100db2015-12-16 23:49:33 +00001457 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001458 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001459 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1460 break;
1461
James Hogan16d100db2015-12-16 23:49:33 +00001462 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001463 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1464 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1465 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001466
1467 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001468 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1469 break;
1470
James Hogan16d100db2015-12-16 23:49:33 +00001471 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001472 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1473 cause, opc, badvaddr);
1474
1475 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001476 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1477 break;
1478
James Hogan16d100db2015-12-16 23:49:33 +00001479 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001480 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001481 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1482 break;
1483
James Hogan16d100db2015-12-16 23:49:33 +00001484 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001485 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001486 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1487 break;
1488
James Hogan16d100db2015-12-16 23:49:33 +00001489 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001490 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001491 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1492 break;
1493
James Hogan16d100db2015-12-16 23:49:33 +00001494 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001495 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001496 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1497 break;
1498
James Hogan16d100db2015-12-16 23:49:33 +00001499 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001500 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001501 ret = kvm_mips_callbacks->handle_break(vcpu);
1502 break;
1503
James Hogan16d100db2015-12-16 23:49:33 +00001504 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001505 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001506 ret = kvm_mips_callbacks->handle_trap(vcpu);
1507 break;
1508
James Hogan16d100db2015-12-16 23:49:33 +00001509 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001510 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001511 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1512 break;
1513
James Hogan16d100db2015-12-16 23:49:33 +00001514 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001515 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001516 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1517 break;
1518
James Hogan16d100db2015-12-16 23:49:33 +00001519 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001520 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001521 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1522 break;
1523
Sanjay Lal669e8462012-11-21 18:34:02 -08001524 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001525 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1526 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1527 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001528 kvm_arch_vcpu_dump_regs(vcpu);
1529 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1530 ret = RESUME_HOST;
1531 break;
1532
1533 }
1534
1535skip_emul:
1536 local_irq_disable();
1537
1538 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1539 kvm_mips_deliver_interrupts(vcpu, cause);
1540
1541 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001542 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001543 if (signal_pending(current)) {
1544 run->exit_reason = KVM_EXIT_INTR;
1545 ret = (-EINTR << 2) | RESUME_HOST;
1546 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001547 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001548 }
1549 }
1550
James Hogan98e91b82014-11-18 14:09:12 +00001551 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001552 trace_kvm_reenter(vcpu);
1553
James Hogan98e91b82014-11-18 14:09:12 +00001554 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001555 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1556 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001557 *
1558 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001559 * vector, as it may well cause an [MSA] FP exception if there
1560 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001561 * kvm_mips_csr_die_notifier() for how that is handled).
1562 */
1563 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1564 read_c0_status() & ST0_CU1)
1565 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001566
1567 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1568 read_c0_config5() & MIPS_CONF5_MSAEN)
1569 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001570 }
1571
James Hoganc4c6f2c2015-02-04 10:52:03 +00001572 /* Disable HTW before returning to guest or host */
1573 htw_stop();
1574
Sanjay Lal669e8462012-11-21 18:34:02 -08001575 return ret;
1576}
1577
James Hogan98e91b82014-11-18 14:09:12 +00001578/* Enable FPU for guest and restore context */
1579void kvm_own_fpu(struct kvm_vcpu *vcpu)
1580{
1581 struct mips_coproc *cop0 = vcpu->arch.cop0;
1582 unsigned int sr, cfg5;
1583
1584 preempt_disable();
1585
James Hogan539cb89fb2015-03-05 11:43:36 +00001586 sr = kvm_read_c0_guest_status(cop0);
1587
1588 /*
1589 * If MSA state is already live, it is undefined how it interacts with
1590 * FR=0 FPU state, and we don't want to hit reserved instruction
1591 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1592 * play it safe and save it first.
1593 *
1594 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1595 * get called when guest CU1 is set, however we can't trust the guest
1596 * not to clobber the status register directly via the commpage.
1597 */
1598 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001599 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001600 kvm_lose_fpu(vcpu);
1601
James Hogan98e91b82014-11-18 14:09:12 +00001602 /*
1603 * Enable FPU for guest
1604 * We set FR and FRE according to guest context
1605 */
James Hogan98e91b82014-11-18 14:09:12 +00001606 change_c0_status(ST0_CU1 | ST0_FR, sr);
1607 if (cpu_has_fre) {
1608 cfg5 = kvm_read_c0_guest_config5(cop0);
1609 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1610 }
1611 enable_fpu_hazard();
1612
1613 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001614 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001615 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001616 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001617 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1618 } else {
1619 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001620 }
1621
1622 preempt_enable();
1623}
1624
James Hogan539cb89fb2015-03-05 11:43:36 +00001625#ifdef CONFIG_CPU_HAS_MSA
1626/* Enable MSA for guest and restore context */
1627void kvm_own_msa(struct kvm_vcpu *vcpu)
1628{
1629 struct mips_coproc *cop0 = vcpu->arch.cop0;
1630 unsigned int sr, cfg5;
1631
1632 preempt_disable();
1633
1634 /*
1635 * Enable FPU if enabled in guest, since we're restoring FPU context
1636 * anyway. We set FR and FRE according to guest context.
1637 */
1638 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1639 sr = kvm_read_c0_guest_status(cop0);
1640
1641 /*
1642 * If FR=0 FPU state is already live, it is undefined how it
1643 * interacts with MSA state, so play it safe and save it first.
1644 */
1645 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001646 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1647 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001648 kvm_lose_fpu(vcpu);
1649
1650 change_c0_status(ST0_CU1 | ST0_FR, sr);
1651 if (sr & ST0_CU1 && cpu_has_fre) {
1652 cfg5 = kvm_read_c0_guest_config5(cop0);
1653 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1654 }
1655 }
1656
1657 /* Enable MSA for guest */
1658 set_c0_config5(MIPS_CONF5_MSAEN);
1659 enable_fpu_hazard();
1660
James Hoganf9431762016-06-14 09:40:10 +01001661 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1662 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001663 /*
1664 * Guest FPU state already loaded, only restore upper MSA state
1665 */
1666 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001667 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001668 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001669 break;
1670 case 0:
1671 /* Neither FPU or MSA already active, restore full MSA state */
1672 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001673 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001674 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001675 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001676 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1677 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001678 break;
1679 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001680 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001681 break;
1682 }
1683
1684 preempt_enable();
1685}
1686#endif
1687
1688/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001689void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1690{
1691 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001692 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001693 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001694 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001695 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001696 }
James Hoganf9431762016-06-14 09:40:10 +01001697 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001698 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001699 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001700 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001701 }
1702 preempt_enable();
1703}
1704
James Hogan539cb89fb2015-03-05 11:43:36 +00001705/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001706void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1707{
1708 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001709 * FPU & MSA get disabled in root context (hardware) when it is disabled
1710 * in guest context (software), but the register state in the hardware
1711 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001712 * before saving.
1713 */
1714
1715 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001716 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001717 set_c0_config5(MIPS_CONF5_MSAEN);
1718 enable_fpu_hazard();
1719
1720 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001721 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001722
1723 /* Disable MSA & FPU */
1724 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001725 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001726 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001727 disable_fpu_hazard();
1728 }
James Hoganf9431762016-06-14 09:40:10 +01001729 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1730 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001731 set_c0_status(ST0_CU1);
1732 enable_fpu_hazard();
1733
1734 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001735 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001736 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001737
1738 /* Disable FPU */
1739 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001740 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001741 }
1742 preempt_enable();
1743}
1744
1745/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001746 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1747 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1748 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001749 */
1750static int kvm_mips_csr_die_notify(struct notifier_block *self,
1751 unsigned long cmd, void *ptr)
1752{
1753 struct die_args *args = (struct die_args *)ptr;
1754 struct pt_regs *regs = args->regs;
1755 unsigned long pc;
1756
James Hogan539cb89fb2015-03-05 11:43:36 +00001757 /* Only interested in FPE and MSAFPE */
1758 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001759 return NOTIFY_DONE;
1760
1761 /* Return immediately if guest context isn't active */
1762 if (!(current->flags & PF_VCPU))
1763 return NOTIFY_DONE;
1764
1765 /* Should never get here from user mode */
1766 BUG_ON(user_mode(regs));
1767
1768 pc = instruction_pointer(regs);
1769 switch (cmd) {
1770 case DIE_FP:
1771 /* match 2nd instruction in __kvm_restore_fcsr */
1772 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1773 return NOTIFY_DONE;
1774 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001775 case DIE_MSAFP:
1776 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1777 if (!cpu_has_msa ||
1778 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1779 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1780 return NOTIFY_DONE;
1781 break;
James Hogan98e91b82014-11-18 14:09:12 +00001782 }
1783
1784 /* Move PC forward a little and continue executing */
1785 instruction_pointer(regs) += 4;
1786
1787 return NOTIFY_STOP;
1788}
1789
1790static struct notifier_block kvm_mips_csr_die_notifier = {
1791 .notifier_call = kvm_mips_csr_die_notify,
1792};
1793
James Hogan2db9d232015-12-16 23:49:32 +00001794static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001795{
1796 int ret;
1797
James Hogan1e5217f52016-06-23 17:34:45 +01001798 ret = kvm_mips_entry_setup();
1799 if (ret)
1800 return ret;
1801
Sanjay Lal669e8462012-11-21 18:34:02 -08001802 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1803
1804 if (ret)
1805 return ret;
1806
James Hogan98e91b82014-11-18 14:09:12 +00001807 register_die_notifier(&kvm_mips_csr_die_notifier);
1808
Sanjay Lal669e8462012-11-21 18:34:02 -08001809 return 0;
1810}
1811
James Hogan2db9d232015-12-16 23:49:32 +00001812static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001813{
1814 kvm_exit();
1815
James Hogan98e91b82014-11-18 14:09:12 +00001816 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001817}
1818
1819module_init(kvm_mips_init);
1820module_exit(kvm_mips_exit);
1821
1822EXPORT_TRACEPOINT_SYMBOL(kvm_exit);