blob: 01f3fa1b9f0ead69ed2cba29a1cdfd85ce234863 [file] [log] [blame]
Sanjay Lal669e8462012-11-21 18:34:02 -08001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * KVM/MIPS: MIPS specific KVM APIs
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070010 */
Sanjay Lal669e8462012-11-21 18:34:02 -080011
James Hogan05108702016-06-15 19:29:56 +010012#include <linux/bitops.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080013#include <linux/errno.h>
14#include <linux/err.h>
James Hogan98e91b82014-11-18 14:09:12 +000015#include <linux/kdebug.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080016#include <linux/module.h>
James Hogand852b5f2016-10-19 00:24:27 +010017#include <linux/uaccess.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080018#include <linux/vmalloc.h>
19#include <linux/fs.h>
20#include <linux/bootmem.h>
James Hoganf7982172015-02-04 17:06:37 +000021#include <asm/fpu.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080022#include <asm/page.h>
23#include <asm/cacheflush.h>
24#include <asm/mmu_context.h>
James Hogan06c158c2015-05-01 13:50:18 +010025#include <asm/pgalloc.h>
James Hoganc4c6f2c2015-02-04 10:52:03 +000026#include <asm/pgtable.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080027
28#include <linux/kvm_host.h>
29
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070030#include "interrupt.h"
31#include "commpage.h"
Sanjay Lal669e8462012-11-21 18:34:02 -080032
33#define CREATE_TRACE_POINTS
34#include "trace.h"
35
36#ifndef VECTORSPACING
37#define VECTORSPACING 0x100 /* for EI/VI mode */
38#endif
39
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070040#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
Sanjay Lal669e8462012-11-21 18:34:02 -080041struct kvm_stats_debugfs_item debugfs_entries[] = {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070042 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
43 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
44 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
45 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
46 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
47 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
48 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
49 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
50 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
51 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
52 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
53 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
54 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
James Hogan0a560422015-02-06 16:03:57 +000055 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000056 { "msa_fpe", VCPU_STAT(msa_fpe_exits), KVM_STAT_VCPU },
James Hogan1c0cd662015-02-06 10:56:27 +000057 { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000058 { "msa_disabled", VCPU_STAT(msa_disabled_exits), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070059 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
Paolo Bonzinif7819512015-02-04 18:20:58 +010060 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +020061 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), KVM_STAT_VCPU },
Christian Borntraeger3491caf2016-05-13 12:16:35 +020062 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070063 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
Sanjay Lal669e8462012-11-21 18:34:02 -080064 {NULL}
65};
66
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070067/*
68 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
69 * Config7, so we are "runnable" if interrupts are pending
Sanjay Lal669e8462012-11-21 18:34:02 -080070 */
71int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
72{
73 return !!(vcpu->arch.pending_exceptions);
74}
75
76int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
77{
78 return 1;
79}
80
Radim Krčmář13a34e02014-08-28 15:13:03 +020081int kvm_arch_hardware_enable(void)
Sanjay Lal669e8462012-11-21 18:34:02 -080082{
83 return 0;
84}
85
Sanjay Lal669e8462012-11-21 18:34:02 -080086int kvm_arch_hardware_setup(void)
87{
88 return 0;
89}
90
Sanjay Lal669e8462012-11-21 18:34:02 -080091void kvm_arch_check_processor_compat(void *rtn)
92{
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -070093 *(int *)rtn = 0;
Sanjay Lal669e8462012-11-21 18:34:02 -080094}
95
Sanjay Lal669e8462012-11-21 18:34:02 -080096int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
97{
James Hogan06c158c2015-05-01 13:50:18 +010098 /* Allocate page table to map GPA -> RPA */
99 kvm->arch.gpa_mm.pgd = kvm_pgd_alloc();
100 if (!kvm->arch.gpa_mm.pgd)
101 return -ENOMEM;
102
Sanjay Lal669e8462012-11-21 18:34:02 -0800103 return 0;
104}
105
Luiz Capitulino235539b2016-09-07 14:47:23 -0400106bool kvm_arch_has_vcpu_debugfs(void)
107{
108 return false;
109}
110
111int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
112{
113 return 0;
114}
115
Sanjay Lal669e8462012-11-21 18:34:02 -0800116void kvm_mips_free_vcpus(struct kvm *kvm)
117{
118 unsigned int i;
119 struct kvm_vcpu *vcpu;
120
Sanjay Lal669e8462012-11-21 18:34:02 -0800121 kvm_for_each_vcpu(i, vcpu, kvm) {
122 kvm_arch_vcpu_free(vcpu);
123 }
124
125 mutex_lock(&kvm->lock);
126
127 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
128 kvm->vcpus[i] = NULL;
129
130 atomic_set(&kvm->online_vcpus, 0);
131
132 mutex_unlock(&kvm->lock);
133}
134
James Hogan06c158c2015-05-01 13:50:18 +0100135static void kvm_mips_free_gpa_pt(struct kvm *kvm)
136{
137 /* It should always be safe to remove after flushing the whole range */
138 WARN_ON(!kvm_mips_flush_gpa_pt(kvm, 0, ~0));
139 pgd_free(NULL, kvm->arch.gpa_mm.pgd);
140}
141
Sanjay Lal669e8462012-11-21 18:34:02 -0800142void kvm_arch_destroy_vm(struct kvm *kvm)
143{
144 kvm_mips_free_vcpus(kvm);
James Hogan06c158c2015-05-01 13:50:18 +0100145 kvm_mips_free_gpa_pt(kvm);
Sanjay Lal669e8462012-11-21 18:34:02 -0800146}
147
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700148long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
149 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800150{
David Daneyed829852013-05-23 09:49:10 -0700151 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800152}
153
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530154int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
155 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800156{
157 return 0;
158}
159
James Hoganb6209112016-10-25 00:01:37 +0100160void kvm_arch_flush_shadow_all(struct kvm *kvm)
161{
162 /* Flush whole GPA */
163 kvm_mips_flush_gpa_pt(kvm, 0, ~0);
164
165 /* Let implementation do the rest */
166 kvm_mips_callbacks->flush_shadow_all(kvm);
167}
168
169void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
170 struct kvm_memory_slot *slot)
171{
172 /*
173 * The slot has been made invalid (ready for moving or deletion), so we
174 * need to ensure that it can no longer be accessed by any guest VCPUs.
175 */
176
177 spin_lock(&kvm->mmu_lock);
178 /* Flush slot from GPA */
179 kvm_mips_flush_gpa_pt(kvm, slot->base_gfn,
180 slot->base_gfn + slot->npages - 1);
181 /* Let implementation do the rest */
182 kvm_mips_callbacks->flush_shadow_memslot(kvm, slot);
183 spin_unlock(&kvm->mmu_lock);
184}
185
Sanjay Lal669e8462012-11-21 18:34:02 -0800186int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700187 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200188 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700189 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800190{
191 return 0;
192}
193
194void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200195 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700196 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200197 const struct kvm_memory_slot *new,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700198 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800199{
Sanjay Lal669e8462012-11-21 18:34:02 -0800200 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
201 __func__, kvm, mem->slot, mem->guest_phys_addr,
202 mem->memory_size, mem->userspace_addr);
Sanjay Lal669e8462012-11-21 18:34:02 -0800203}
204
James Hogand7b8f892016-06-23 17:34:40 +0100205static inline void dump_handler(const char *symbol, void *start, void *end)
206{
207 u32 *p;
208
209 pr_debug("LEAF(%s)\n", symbol);
210
211 pr_debug("\t.set push\n");
212 pr_debug("\t.set noreorder\n");
213
214 for (p = start; p < (u32 *)end; ++p)
215 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p);
216
217 pr_debug("\t.set\tpop\n");
218
219 pr_debug("\tEND(%s)\n", symbol);
220}
221
Sanjay Lal669e8462012-11-21 18:34:02 -0800222struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
223{
James Hogan90e93112016-06-23 17:34:39 +0100224 int err, size;
James Hogana7cfa7a2016-09-10 23:56:46 +0100225 void *gebase, *p, *handler, *refill_start, *refill_end;
Sanjay Lal669e8462012-11-21 18:34:02 -0800226 int i;
227
228 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
229
230 if (!vcpu) {
231 err = -ENOMEM;
232 goto out;
233 }
234
235 err = kvm_vcpu_init(vcpu, kvm, id);
236
237 if (err)
238 goto out_free_cpu;
239
James Hogan6e95bfd2014-05-29 10:16:43 +0100240 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800241
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700242 /*
243 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800244 * guest mode exits
245 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700246 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800247 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700248 else
James Hogan7006e2d2014-05-29 10:16:23 +0100249 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800250
Sanjay Lal669e8462012-11-21 18:34:02 -0800251 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
252
253 if (!gebase) {
254 err = -ENOMEM;
James Hogan585bb8f2015-11-11 14:21:20 +0000255 goto out_uninit_cpu;
Sanjay Lal669e8462012-11-21 18:34:02 -0800256 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100257 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
258 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800259
James Hogan2a06dab2016-07-08 11:53:26 +0100260 /*
261 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
262 * limits us to the low 512MB of physical address space. If the memory
263 * we allocate is out of range, just give up now.
264 */
265 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) {
266 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
267 gebase);
268 err = -ENOMEM;
269 goto out_free_gebase;
270 }
271
Sanjay Lal669e8462012-11-21 18:34:02 -0800272 /* Save new ebase */
273 vcpu->arch.guest_ebase = gebase;
274
James Hogan90e93112016-06-23 17:34:39 +0100275 /* Build guest exception vectors dynamically in unmapped memory */
James Hogan1f9ca622016-06-23 17:34:46 +0100276 handler = gebase + 0x2000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800277
James Hogana7cfa7a2016-09-10 23:56:46 +0100278 /* TLB refill */
279 refill_start = gebase;
280 refill_end = kvm_mips_build_tlb_refill_exception(refill_start, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800281
282 /* General Exception Entry point */
James Hogan1f9ca622016-06-23 17:34:46 +0100283 kvm_mips_build_exception(gebase + 0x180, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800284
285 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
286 for (i = 0; i < 8; i++) {
287 kvm_debug("L1 Vectored handler @ %p\n",
288 gebase + 0x200 + (i * VECTORSPACING));
James Hogan1f9ca622016-06-23 17:34:46 +0100289 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
290 handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800291 }
292
James Hogan90e93112016-06-23 17:34:39 +0100293 /* General exit handler */
James Hogan1f9ca622016-06-23 17:34:46 +0100294 p = handler;
James Hogan90e93112016-06-23 17:34:39 +0100295 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800296
James Hogan90e93112016-06-23 17:34:39 +0100297 /* Guest entry routine */
298 vcpu->arch.vcpu_run = p;
299 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100300
James Hogand7b8f892016-06-23 17:34:40 +0100301 /* Dump the generated code */
302 pr_debug("#include <asm/asm.h>\n");
303 pr_debug("#include <asm/regdef.h>\n");
304 pr_debug("\n");
305 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
James Hogana7cfa7a2016-09-10 23:56:46 +0100306 dump_handler("kvm_tlb_refill", refill_start, refill_end);
James Hogand7b8f892016-06-23 17:34:40 +0100307 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
308 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
309
Sanjay Lal669e8462012-11-21 18:34:02 -0800310 /* Invalidate the icache for these ranges */
James Hogan32eb12a2017-01-03 17:43:01 +0000311 flush_icache_range((unsigned long)gebase,
312 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800313
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700314 /*
315 * Allocate comm page for guest kernel, a TLB will be reserved for
316 * mapping GVA @ 0xFFFF8000 to this page
317 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800318 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
319
320 if (!vcpu->arch.kseg0_commpage) {
321 err = -ENOMEM;
322 goto out_free_gebase;
323 }
324
James Hogan6e95bfd2014-05-29 10:16:43 +0100325 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800326 kvm_mips_commpage_init(vcpu);
327
328 /* Init */
329 vcpu->arch.last_sched_cpu = -1;
330
331 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100332 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800333
334 return vcpu;
335
336out_free_gebase:
337 kfree(gebase);
338
James Hogan585bb8f2015-11-11 14:21:20 +0000339out_uninit_cpu:
340 kvm_vcpu_uninit(vcpu);
341
Sanjay Lal669e8462012-11-21 18:34:02 -0800342out_free_cpu:
343 kfree(vcpu);
344
345out:
346 return ERR_PTR(err);
347}
348
349void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
350{
351 hrtimer_cancel(&vcpu->arch.comparecount_timer);
352
353 kvm_vcpu_uninit(vcpu);
354
355 kvm_mips_dump_stats(vcpu);
356
James Hoganaba859292016-12-16 15:57:00 +0000357 kvm_mmu_free_memory_caches(vcpu);
James Hoganc6c0a662014-05-29 10:16:44 +0100358 kfree(vcpu->arch.guest_ebase);
359 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700360 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800361}
362
363void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
364{
365 kvm_arch_vcpu_free(vcpu);
366}
367
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700368int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
369 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800370{
David Daneyed829852013-05-23 09:49:10 -0700371 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800372}
373
374int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
375{
376 int r = 0;
377 sigset_t sigsaved;
378
379 if (vcpu->sigset_active)
380 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
381
382 if (vcpu->mmio_needed) {
383 if (!vcpu->mmio_is_write)
384 kvm_mips_complete_mmio_load(vcpu, run);
385 vcpu->mmio_needed = 0;
386 }
387
James Hoganf7982172015-02-04 17:06:37 +0000388 lose_fpu(1);
389
James Hogan044f0f02014-05-29 10:16:32 +0100390 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200391 guest_enter_irqoff();
James Hogan93258602016-06-14 09:40:14 +0100392 trace_kvm_enter(vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100393
James Hogan4841e0d2016-11-28 22:45:04 +0000394 /*
395 * Make sure the read of VCPU requests in vcpu_run() callback is not
396 * reordered ahead of the write to vcpu->mode, or we could miss a TLB
397 * flush request while the requester sees the VCPU as outside of guest
398 * mode and not needing an IPI.
399 */
400 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
401
James Hogana2c046e2016-11-18 13:14:37 +0000402 r = kvm_mips_callbacks->vcpu_run(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100403
James Hogan93258602016-06-14 09:40:14 +0100404 trace_kvm_out(vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200405 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800406 local_irq_enable();
407
408 if (vcpu->sigset_active)
409 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
410
411 return r;
412}
413
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700414int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
415 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800416{
417 int intr = (int)irq->irq;
418 struct kvm_vcpu *dvcpu = NULL;
419
420 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
421 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
422 (int)intr);
423
424 if (irq->cpu == -1)
425 dvcpu = vcpu;
426 else
427 dvcpu = vcpu->kvm->vcpus[irq->cpu];
428
429 if (intr == 2 || intr == 3 || intr == 4) {
430 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
431
432 } else if (intr == -2 || intr == -3 || intr == -4) {
433 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
434 } else {
435 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
436 irq->cpu, irq->irq);
437 return -EINVAL;
438 }
439
440 dvcpu->arch.wait = 0;
441
Marcelo Tosatti85773702016-02-19 09:46:39 +0100442 if (swait_active(&dvcpu->wq))
443 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800444
445 return 0;
446}
447
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700448int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
449 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800450{
David Daneyed829852013-05-23 09:49:10 -0700451 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800452}
453
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700454int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
455 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800456{
David Daneyed829852013-05-23 09:49:10 -0700457 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800458}
459
David Daney4c73fb22013-05-23 09:49:09 -0700460static u64 kvm_mips_get_one_regs[] = {
461 KVM_REG_MIPS_R0,
462 KVM_REG_MIPS_R1,
463 KVM_REG_MIPS_R2,
464 KVM_REG_MIPS_R3,
465 KVM_REG_MIPS_R4,
466 KVM_REG_MIPS_R5,
467 KVM_REG_MIPS_R6,
468 KVM_REG_MIPS_R7,
469 KVM_REG_MIPS_R8,
470 KVM_REG_MIPS_R9,
471 KVM_REG_MIPS_R10,
472 KVM_REG_MIPS_R11,
473 KVM_REG_MIPS_R12,
474 KVM_REG_MIPS_R13,
475 KVM_REG_MIPS_R14,
476 KVM_REG_MIPS_R15,
477 KVM_REG_MIPS_R16,
478 KVM_REG_MIPS_R17,
479 KVM_REG_MIPS_R18,
480 KVM_REG_MIPS_R19,
481 KVM_REG_MIPS_R20,
482 KVM_REG_MIPS_R21,
483 KVM_REG_MIPS_R22,
484 KVM_REG_MIPS_R23,
485 KVM_REG_MIPS_R24,
486 KVM_REG_MIPS_R25,
487 KVM_REG_MIPS_R26,
488 KVM_REG_MIPS_R27,
489 KVM_REG_MIPS_R28,
490 KVM_REG_MIPS_R29,
491 KVM_REG_MIPS_R30,
492 KVM_REG_MIPS_R31,
493
James Hogan70e92c7e2016-07-04 19:35:11 +0100494#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700495 KVM_REG_MIPS_HI,
496 KVM_REG_MIPS_LO,
James Hogan70e92c7e2016-07-04 19:35:11 +0100497#endif
David Daney4c73fb22013-05-23 09:49:09 -0700498 KVM_REG_MIPS_PC,
499
500 KVM_REG_MIPS_CP0_INDEX,
501 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100502 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700503 KVM_REG_MIPS_CP0_PAGEMASK,
504 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100505 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700506 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100507 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700508 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100509 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700510 KVM_REG_MIPS_CP0_STATUS,
511 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100512 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100513 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700514 KVM_REG_MIPS_CP0_CONFIG,
515 KVM_REG_MIPS_CP0_CONFIG1,
516 KVM_REG_MIPS_CP0_CONFIG2,
517 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100518 KVM_REG_MIPS_CP0_CONFIG4,
519 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700520 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100521 KVM_REG_MIPS_CP0_ERROREPC,
522
523 KVM_REG_MIPS_COUNT_CTL,
524 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100525 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700526};
527
James Hogane5775932016-06-15 19:29:51 +0100528static u64 kvm_mips_get_one_regs_fpu[] = {
529 KVM_REG_MIPS_FCR_IR,
530 KVM_REG_MIPS_FCR_CSR,
531};
532
533static u64 kvm_mips_get_one_regs_msa[] = {
534 KVM_REG_MIPS_MSA_IR,
535 KVM_REG_MIPS_MSA_CSR,
536};
537
James Hogan05108702016-06-15 19:29:56 +0100538static u64 kvm_mips_get_one_regs_kscratch[] = {
539 KVM_REG_MIPS_CP0_KSCRATCH1,
540 KVM_REG_MIPS_CP0_KSCRATCH2,
541 KVM_REG_MIPS_CP0_KSCRATCH3,
542 KVM_REG_MIPS_CP0_KSCRATCH4,
543 KVM_REG_MIPS_CP0_KSCRATCH5,
544 KVM_REG_MIPS_CP0_KSCRATCH6,
545};
546
James Hoganf5c43bd2016-06-15 19:29:49 +0100547static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
548{
549 unsigned long ret;
550
551 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100552 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
553 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
554 /* odd doubles */
555 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
556 ret += 16;
557 }
558 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
559 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hogan05108702016-06-15 19:29:56 +0100560 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
James Hoganf5c43bd2016-06-15 19:29:49 +0100561 ret += kvm_mips_callbacks->num_regs(vcpu);
562
563 return ret;
564}
565
566static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
567{
James Hogane5775932016-06-15 19:29:51 +0100568 u64 index;
569 unsigned int i;
570
James Hoganf5c43bd2016-06-15 19:29:49 +0100571 if (copy_to_user(indices, kvm_mips_get_one_regs,
572 sizeof(kvm_mips_get_one_regs)))
573 return -EFAULT;
574 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
575
James Hogane5775932016-06-15 19:29:51 +0100576 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
577 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
578 sizeof(kvm_mips_get_one_regs_fpu)))
579 return -EFAULT;
580 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
581
582 for (i = 0; i < 32; ++i) {
583 index = KVM_REG_MIPS_FPR_32(i);
584 if (copy_to_user(indices, &index, sizeof(index)))
585 return -EFAULT;
586 ++indices;
587
588 /* skip odd doubles if no F64 */
589 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
590 continue;
591
592 index = KVM_REG_MIPS_FPR_64(i);
593 if (copy_to_user(indices, &index, sizeof(index)))
594 return -EFAULT;
595 ++indices;
596 }
597 }
598
599 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
600 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
601 sizeof(kvm_mips_get_one_regs_msa)))
602 return -EFAULT;
603 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
604
605 for (i = 0; i < 32; ++i) {
606 index = KVM_REG_MIPS_VEC_128(i);
607 if (copy_to_user(indices, &index, sizeof(index)))
608 return -EFAULT;
609 ++indices;
610 }
611 }
612
James Hogan05108702016-06-15 19:29:56 +0100613 for (i = 0; i < 6; ++i) {
614 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
615 continue;
616
617 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
618 sizeof(kvm_mips_get_one_regs_kscratch[i])))
619 return -EFAULT;
620 ++indices;
621 }
622
James Hoganf5c43bd2016-06-15 19:29:49 +0100623 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
624}
625
David Daney4c73fb22013-05-23 09:49:09 -0700626static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
627 const struct kvm_one_reg *reg)
628{
David Daney4c73fb22013-05-23 09:49:09 -0700629 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000630 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100631 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700632 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000633 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000634 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700635
636 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000637 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700638 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
639 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
640 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100641#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700642 case KVM_REG_MIPS_HI:
643 v = (long)vcpu->arch.hi;
644 break;
645 case KVM_REG_MIPS_LO:
646 v = (long)vcpu->arch.lo;
647 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100648#endif
David Daney4c73fb22013-05-23 09:49:09 -0700649 case KVM_REG_MIPS_PC:
650 v = (long)vcpu->arch.pc;
651 break;
652
James Hogan379245c2014-12-02 15:48:24 +0000653 /* Floating point registers */
654 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
655 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
656 return -EINVAL;
657 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
658 /* Odd singles in top of even double when FR=0 */
659 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
660 v = get_fpr32(&fpu->fpr[idx], 0);
661 else
662 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
663 break;
664 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
665 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
666 return -EINVAL;
667 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
668 /* Can't access odd doubles in FR=0 mode */
669 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
670 return -EINVAL;
671 v = get_fpr64(&fpu->fpr[idx], 0);
672 break;
673 case KVM_REG_MIPS_FCR_IR:
674 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
675 return -EINVAL;
676 v = boot_cpu_data.fpu_id;
677 break;
678 case KVM_REG_MIPS_FCR_CSR:
679 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
680 return -EINVAL;
681 v = fpu->fcr31;
682 break;
683
James Hoganab86bd62014-12-02 15:48:24 +0000684 /* MIPS SIMD Architecture (MSA) registers */
685 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
686 if (!kvm_mips_guest_has_msa(&vcpu->arch))
687 return -EINVAL;
688 /* Can't access MSA registers in FR=0 mode */
689 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
690 return -EINVAL;
691 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
692#ifdef CONFIG_CPU_LITTLE_ENDIAN
693 /* least significant byte first */
694 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
695 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
696#else
697 /* most significant byte first */
698 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
699 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
700#endif
701 break;
702 case KVM_REG_MIPS_MSA_IR:
703 if (!kvm_mips_guest_has_msa(&vcpu->arch))
704 return -EINVAL;
705 v = boot_cpu_data.msa_id;
706 break;
707 case KVM_REG_MIPS_MSA_CSR:
708 if (!kvm_mips_guest_has_msa(&vcpu->arch))
709 return -EINVAL;
710 v = fpu->msacsr;
711 break;
712
James Hogan379245c2014-12-02 15:48:24 +0000713 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700714 case KVM_REG_MIPS_CP0_INDEX:
715 v = (long)kvm_read_c0_guest_index(cop0);
716 break;
717 case KVM_REG_MIPS_CP0_CONTEXT:
718 v = (long)kvm_read_c0_guest_context(cop0);
719 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100720 case KVM_REG_MIPS_CP0_USERLOCAL:
721 v = (long)kvm_read_c0_guest_userlocal(cop0);
722 break;
David Daney4c73fb22013-05-23 09:49:09 -0700723 case KVM_REG_MIPS_CP0_PAGEMASK:
724 v = (long)kvm_read_c0_guest_pagemask(cop0);
725 break;
726 case KVM_REG_MIPS_CP0_WIRED:
727 v = (long)kvm_read_c0_guest_wired(cop0);
728 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100729 case KVM_REG_MIPS_CP0_HWRENA:
730 v = (long)kvm_read_c0_guest_hwrena(cop0);
731 break;
David Daney4c73fb22013-05-23 09:49:09 -0700732 case KVM_REG_MIPS_CP0_BADVADDR:
733 v = (long)kvm_read_c0_guest_badvaddr(cop0);
734 break;
735 case KVM_REG_MIPS_CP0_ENTRYHI:
736 v = (long)kvm_read_c0_guest_entryhi(cop0);
737 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100738 case KVM_REG_MIPS_CP0_COMPARE:
739 v = (long)kvm_read_c0_guest_compare(cop0);
740 break;
David Daney4c73fb22013-05-23 09:49:09 -0700741 case KVM_REG_MIPS_CP0_STATUS:
742 v = (long)kvm_read_c0_guest_status(cop0);
743 break;
744 case KVM_REG_MIPS_CP0_CAUSE:
745 v = (long)kvm_read_c0_guest_cause(cop0);
746 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100747 case KVM_REG_MIPS_CP0_EPC:
748 v = (long)kvm_read_c0_guest_epc(cop0);
749 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100750 case KVM_REG_MIPS_CP0_PRID:
751 v = (long)kvm_read_c0_guest_prid(cop0);
752 break;
David Daney4c73fb22013-05-23 09:49:09 -0700753 case KVM_REG_MIPS_CP0_CONFIG:
754 v = (long)kvm_read_c0_guest_config(cop0);
755 break;
756 case KVM_REG_MIPS_CP0_CONFIG1:
757 v = (long)kvm_read_c0_guest_config1(cop0);
758 break;
759 case KVM_REG_MIPS_CP0_CONFIG2:
760 v = (long)kvm_read_c0_guest_config2(cop0);
761 break;
762 case KVM_REG_MIPS_CP0_CONFIG3:
763 v = (long)kvm_read_c0_guest_config3(cop0);
764 break;
James Hoganc7716072014-06-26 15:11:29 +0100765 case KVM_REG_MIPS_CP0_CONFIG4:
766 v = (long)kvm_read_c0_guest_config4(cop0);
767 break;
768 case KVM_REG_MIPS_CP0_CONFIG5:
769 v = (long)kvm_read_c0_guest_config5(cop0);
770 break;
David Daney4c73fb22013-05-23 09:49:09 -0700771 case KVM_REG_MIPS_CP0_CONFIG7:
772 v = (long)kvm_read_c0_guest_config7(cop0);
773 break;
James Hogane93d4c12014-06-26 13:47:22 +0100774 case KVM_REG_MIPS_CP0_ERROREPC:
775 v = (long)kvm_read_c0_guest_errorepc(cop0);
776 break;
James Hogan05108702016-06-15 19:29:56 +0100777 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
778 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
779 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
780 return -EINVAL;
781 switch (idx) {
782 case 2:
783 v = (long)kvm_read_c0_guest_kscratch1(cop0);
784 break;
785 case 3:
786 v = (long)kvm_read_c0_guest_kscratch2(cop0);
787 break;
788 case 4:
789 v = (long)kvm_read_c0_guest_kscratch3(cop0);
790 break;
791 case 5:
792 v = (long)kvm_read_c0_guest_kscratch4(cop0);
793 break;
794 case 6:
795 v = (long)kvm_read_c0_guest_kscratch5(cop0);
796 break;
797 case 7:
798 v = (long)kvm_read_c0_guest_kscratch6(cop0);
799 break;
800 }
801 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100802 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100803 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100804 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
805 if (ret)
806 return ret;
807 break;
David Daney4c73fb22013-05-23 09:49:09 -0700808 }
David Daney681865d2013-06-10 12:33:48 -0700809 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
810 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700811
David Daney681865d2013-06-10 12:33:48 -0700812 return put_user(v, uaddr64);
813 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
814 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
815 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700816
David Daney681865d2013-06-10 12:33:48 -0700817 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000818 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
819 void __user *uaddr = (void __user *)(long)reg->addr;
820
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200821 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700822 } else {
823 return -EINVAL;
824 }
David Daney4c73fb22013-05-23 09:49:09 -0700825}
826
827static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
828 const struct kvm_one_reg *reg)
829{
David Daney4c73fb22013-05-23 09:49:09 -0700830 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000831 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
832 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000833 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000834 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700835
David Daney681865d2013-06-10 12:33:48 -0700836 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
837 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
838
839 if (get_user(v, uaddr64) != 0)
840 return -EFAULT;
841 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
842 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
843 s32 v32;
844
845 if (get_user(v32, uaddr32) != 0)
846 return -EFAULT;
847 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000848 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
849 void __user *uaddr = (void __user *)(long)reg->addr;
850
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200851 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700852 } else {
853 return -EINVAL;
854 }
David Daney4c73fb22013-05-23 09:49:09 -0700855
856 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000857 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700858 case KVM_REG_MIPS_R0:
859 /* Silently ignore requests to set $0 */
860 break;
861 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
862 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
863 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100864#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700865 case KVM_REG_MIPS_HI:
866 vcpu->arch.hi = v;
867 break;
868 case KVM_REG_MIPS_LO:
869 vcpu->arch.lo = v;
870 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100871#endif
David Daney4c73fb22013-05-23 09:49:09 -0700872 case KVM_REG_MIPS_PC:
873 vcpu->arch.pc = v;
874 break;
875
James Hogan379245c2014-12-02 15:48:24 +0000876 /* Floating point registers */
877 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
878 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
879 return -EINVAL;
880 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
881 /* Odd singles in top of even double when FR=0 */
882 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
883 set_fpr32(&fpu->fpr[idx], 0, v);
884 else
885 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
886 break;
887 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
888 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
889 return -EINVAL;
890 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
891 /* Can't access odd doubles in FR=0 mode */
892 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
893 return -EINVAL;
894 set_fpr64(&fpu->fpr[idx], 0, v);
895 break;
896 case KVM_REG_MIPS_FCR_IR:
897 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
898 return -EINVAL;
899 /* Read-only */
900 break;
901 case KVM_REG_MIPS_FCR_CSR:
902 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
903 return -EINVAL;
904 fpu->fcr31 = v;
905 break;
906
James Hoganab86bd62014-12-02 15:48:24 +0000907 /* MIPS SIMD Architecture (MSA) registers */
908 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
909 if (!kvm_mips_guest_has_msa(&vcpu->arch))
910 return -EINVAL;
911 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
912#ifdef CONFIG_CPU_LITTLE_ENDIAN
913 /* least significant byte first */
914 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
915 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
916#else
917 /* most significant byte first */
918 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
919 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
920#endif
921 break;
922 case KVM_REG_MIPS_MSA_IR:
923 if (!kvm_mips_guest_has_msa(&vcpu->arch))
924 return -EINVAL;
925 /* Read-only */
926 break;
927 case KVM_REG_MIPS_MSA_CSR:
928 if (!kvm_mips_guest_has_msa(&vcpu->arch))
929 return -EINVAL;
930 fpu->msacsr = v;
931 break;
932
James Hogan379245c2014-12-02 15:48:24 +0000933 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700934 case KVM_REG_MIPS_CP0_INDEX:
935 kvm_write_c0_guest_index(cop0, v);
936 break;
937 case KVM_REG_MIPS_CP0_CONTEXT:
938 kvm_write_c0_guest_context(cop0, v);
939 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100940 case KVM_REG_MIPS_CP0_USERLOCAL:
941 kvm_write_c0_guest_userlocal(cop0, v);
942 break;
David Daney4c73fb22013-05-23 09:49:09 -0700943 case KVM_REG_MIPS_CP0_PAGEMASK:
944 kvm_write_c0_guest_pagemask(cop0, v);
945 break;
946 case KVM_REG_MIPS_CP0_WIRED:
947 kvm_write_c0_guest_wired(cop0, v);
948 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100949 case KVM_REG_MIPS_CP0_HWRENA:
950 kvm_write_c0_guest_hwrena(cop0, v);
951 break;
David Daney4c73fb22013-05-23 09:49:09 -0700952 case KVM_REG_MIPS_CP0_BADVADDR:
953 kvm_write_c0_guest_badvaddr(cop0, v);
954 break;
955 case KVM_REG_MIPS_CP0_ENTRYHI:
956 kvm_write_c0_guest_entryhi(cop0, v);
957 break;
958 case KVM_REG_MIPS_CP0_STATUS:
959 kvm_write_c0_guest_status(cop0, v);
960 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100961 case KVM_REG_MIPS_CP0_EPC:
962 kvm_write_c0_guest_epc(cop0, v);
963 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100964 case KVM_REG_MIPS_CP0_PRID:
965 kvm_write_c0_guest_prid(cop0, v);
966 break;
David Daney4c73fb22013-05-23 09:49:09 -0700967 case KVM_REG_MIPS_CP0_ERROREPC:
968 kvm_write_c0_guest_errorepc(cop0, v);
969 break;
James Hogan05108702016-06-15 19:29:56 +0100970 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
971 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
972 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
973 return -EINVAL;
974 switch (idx) {
975 case 2:
976 kvm_write_c0_guest_kscratch1(cop0, v);
977 break;
978 case 3:
979 kvm_write_c0_guest_kscratch2(cop0, v);
980 break;
981 case 4:
982 kvm_write_c0_guest_kscratch3(cop0, v);
983 break;
984 case 5:
985 kvm_write_c0_guest_kscratch4(cop0, v);
986 break;
987 case 6:
988 kvm_write_c0_guest_kscratch5(cop0, v);
989 break;
990 case 7:
991 kvm_write_c0_guest_kscratch6(cop0, v);
992 break;
993 }
994 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100995 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -0700996 default:
James Hogancc68d222016-06-15 19:29:48 +0100997 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700998 }
999 return 0;
1000}
1001
James Hogan5fafd8742014-12-08 23:07:56 +00001002static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1003 struct kvm_enable_cap *cap)
1004{
1005 int r = 0;
1006
1007 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1008 return -EINVAL;
1009 if (cap->flags)
1010 return -EINVAL;
1011 if (cap->args[0])
1012 return -EINVAL;
1013
1014 switch (cap->cap) {
1015 case KVM_CAP_MIPS_FPU:
1016 vcpu->arch.fpu_enabled = true;
1017 break;
James Hogand952bd02014-12-08 23:07:56 +00001018 case KVM_CAP_MIPS_MSA:
1019 vcpu->arch.msa_enabled = true;
1020 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001021 default:
1022 r = -EINVAL;
1023 break;
1024 }
1025
1026 return r;
1027}
1028
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001029long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1030 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -08001031{
1032 struct kvm_vcpu *vcpu = filp->private_data;
1033 void __user *argp = (void __user *)arg;
1034 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001035
1036 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -07001037 case KVM_SET_ONE_REG:
1038 case KVM_GET_ONE_REG: {
1039 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001040
David Daney4c73fb22013-05-23 09:49:09 -07001041 if (copy_from_user(&reg, argp, sizeof(reg)))
1042 return -EFAULT;
1043 if (ioctl == KVM_SET_ONE_REG)
1044 return kvm_mips_set_reg(vcpu, &reg);
1045 else
1046 return kvm_mips_get_reg(vcpu, &reg);
1047 }
1048 case KVM_GET_REG_LIST: {
1049 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -07001050 struct kvm_reg_list reg_list;
1051 unsigned n;
1052
1053 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1054 return -EFAULT;
1055 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +01001056 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -07001057 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1058 return -EFAULT;
1059 if (n < reg_list.n)
1060 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +01001061 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -07001062 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001063 case KVM_INTERRUPT:
1064 {
1065 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001066
Sanjay Lal669e8462012-11-21 18:34:02 -08001067 if (copy_from_user(&irq, argp, sizeof(irq)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +01001068 return -EFAULT;
Sanjay Lal669e8462012-11-21 18:34:02 -08001069 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1070 irq.irq);
1071
1072 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1073 break;
1074 }
James Hogan5fafd8742014-12-08 23:07:56 +00001075 case KVM_ENABLE_CAP: {
1076 struct kvm_enable_cap cap;
1077
James Hogan5fafd8742014-12-08 23:07:56 +00001078 if (copy_from_user(&cap, argp, sizeof(cap)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +01001079 return -EFAULT;
James Hogan5fafd8742014-12-08 23:07:56 +00001080 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1081 break;
1082 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001083 default:
David Daney4c73fb22013-05-23 09:49:09 -07001084 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001085 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001086 return r;
1087}
1088
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001089/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001090int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1091{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001092 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001093 struct kvm_memory_slot *memslot;
1094 unsigned long ga, ga_end;
1095 int is_dirty = 0;
1096 int r;
1097 unsigned long n;
1098
1099 mutex_lock(&kvm->slots_lock);
1100
1101 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1102 if (r)
1103 goto out;
1104
1105 /* If nothing is dirty, don't bother messing with page tables. */
1106 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001107 slots = kvm_memslots(kvm);
1108 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001109
1110 ga = memslot->base_gfn << PAGE_SHIFT;
1111 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1112
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001113 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
1114 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -08001115
1116 n = kvm_dirty_bitmap_bytes(memslot);
1117 memset(memslot->dirty_bitmap, 0, n);
1118 }
1119
1120 r = 0;
1121out:
1122 mutex_unlock(&kvm->slots_lock);
1123 return r;
1124
1125}
1126
1127long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1128{
1129 long r;
1130
1131 switch (ioctl) {
1132 default:
David Daneyed829852013-05-23 09:49:10 -07001133 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001134 }
1135
1136 return r;
1137}
1138
1139int kvm_arch_init(void *opaque)
1140{
Sanjay Lal669e8462012-11-21 18:34:02 -08001141 if (kvm_mips_callbacks) {
1142 kvm_err("kvm: module already exists\n");
1143 return -EEXIST;
1144 }
1145
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001146 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001147}
1148
1149void kvm_arch_exit(void)
1150{
1151 kvm_mips_callbacks = NULL;
1152}
1153
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001154int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1155 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001156{
David Daneyed829852013-05-23 09:49:10 -07001157 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001158}
1159
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001160int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1161 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001162{
David Daneyed829852013-05-23 09:49:10 -07001163 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001164}
1165
Dominik Dingel31928aa2014-12-04 15:47:07 +01001166void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001167{
Sanjay Lal669e8462012-11-21 18:34:02 -08001168}
1169
1170int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1171{
David Daneyed829852013-05-23 09:49:10 -07001172 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001173}
1174
1175int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1176{
David Daneyed829852013-05-23 09:49:10 -07001177 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001178}
1179
1180int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1181{
1182 return VM_FAULT_SIGBUS;
1183}
1184
Alexander Graf784aa3d2014-07-14 18:27:35 +02001185int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001186{
1187 int r;
1188
1189 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001190 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001191 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001192 r = 1;
1193 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001194 case KVM_CAP_COALESCED_MMIO:
1195 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1196 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001197 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001198 /* We don't handle systems with inconsistent cpu_has_fpu */
1199 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001200 break;
James Hogand952bd02014-12-08 23:07:56 +00001201 case KVM_CAP_MIPS_MSA:
1202 /*
1203 * We don't support MSA vector partitioning yet:
1204 * 1) It would require explicit support which can't be tested
1205 * yet due to lack of support in current hardware.
1206 * 2) It extends the state that would need to be saved/restored
1207 * by e.g. QEMU for migration.
1208 *
1209 * When vector partitioning hardware becomes available, support
1210 * could be added by requiring a flag when enabling
1211 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1212 * to save/restore the appropriate extra state.
1213 */
1214 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1215 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001216 default:
1217 r = 0;
1218 break;
1219 }
1220 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001221}
1222
1223int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1224{
1225 return kvm_mips_pending_timer(vcpu);
1226}
1227
1228int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1229{
1230 int i;
1231 struct mips_coproc *cop0;
1232
1233 if (!vcpu)
1234 return -1;
1235
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001236 kvm_debug("VCPU Register Dump:\n");
1237 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1238 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001239
1240 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001241 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001242 vcpu->arch.gprs[i],
1243 vcpu->arch.gprs[i + 1],
1244 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1245 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001246 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1247 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001248
1249 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001250 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1251 kvm_read_c0_guest_status(cop0),
1252 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001253
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001254 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001255
1256 return 0;
1257}
1258
1259int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1260{
1261 int i;
1262
David Daney8d17dd02013-05-23 09:49:08 -07001263 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001264 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001265 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001266 vcpu->arch.hi = regs->hi;
1267 vcpu->arch.lo = regs->lo;
1268 vcpu->arch.pc = regs->pc;
1269
David Daney4c73fb22013-05-23 09:49:09 -07001270 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001271}
1272
1273int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1274{
1275 int i;
1276
David Daney8d17dd02013-05-23 09:49:08 -07001277 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001278 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001279
1280 regs->hi = vcpu->arch.hi;
1281 regs->lo = vcpu->arch.lo;
1282 regs->pc = vcpu->arch.pc;
1283
David Daney4c73fb22013-05-23 09:49:09 -07001284 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001285}
1286
James Hogan0fae34f2014-05-29 10:16:39 +01001287static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001288{
1289 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1290
1291 kvm_mips_callbacks->queue_timer_int(vcpu);
1292
1293 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001294 if (swait_active(&vcpu->wq))
1295 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001296}
1297
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001298/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001299static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001300{
1301 struct kvm_vcpu *vcpu;
1302
1303 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1304 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001305 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001306}
1307
1308int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1309{
James Hoganf7f14272016-09-08 22:57:03 +01001310 int err;
1311
1312 err = kvm_mips_callbacks->vcpu_init(vcpu);
1313 if (err)
1314 return err;
1315
Sanjay Lal669e8462012-11-21 18:34:02 -08001316 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1317 HRTIMER_MODE_REL);
1318 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001319 return 0;
1320}
1321
James Hogan630766b2016-09-08 23:00:24 +01001322void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1323{
1324 kvm_mips_callbacks->vcpu_uninit(vcpu);
1325}
1326
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001327int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1328 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001329{
1330 return 0;
1331}
1332
1333/* Initial guest state */
1334int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1335{
1336 return kvm_mips_callbacks->vcpu_setup(vcpu);
1337}
1338
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001339static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001340{
James Hogan8cffd192016-06-09 14:19:08 +01001341 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001342
Sanjay Lal669e8462012-11-21 18:34:02 -08001343 if (cpu_has_dsp)
1344 status |= (ST0_MX);
1345
1346 write_c0_status(status);
1347 ehb();
1348}
1349
1350/*
1351 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1352 */
1353int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1354{
James Hogan8cffd192016-06-09 14:19:08 +01001355 u32 cause = vcpu->arch.host_cp0_cause;
1356 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1357 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001358 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1359 enum emulation_result er = EMULATE_DONE;
James Hogan122e51d2016-11-28 17:23:14 +00001360 u32 inst;
Sanjay Lal669e8462012-11-21 18:34:02 -08001361 int ret = RESUME_GUEST;
1362
James Hogan4841e0d2016-11-28 22:45:04 +00001363 vcpu->mode = OUTSIDE_GUEST_MODE;
1364
James Hoganc4c6f2c2015-02-04 10:52:03 +00001365 /* re-enable HTW before enabling interrupts */
1366 htw_start();
1367
Sanjay Lal669e8462012-11-21 18:34:02 -08001368 /* Set a default exit reason */
1369 run->exit_reason = KVM_EXIT_UNKNOWN;
1370 run->ready_for_interrupt_injection = 1;
1371
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001372 /*
1373 * Set the appropriate status bits based on host CPU features,
1374 * before we hit the scheduler
1375 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001376 kvm_mips_set_c0_status();
1377
1378 local_irq_enable();
1379
1380 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1381 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001382 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001383
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001384 /*
1385 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001386 * causing an exception to be delivered to the Guest Kernel
1387 */
1388 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1389 if (er == EMULATE_PRIV_FAIL) {
1390 goto skip_emul;
1391 } else if (er == EMULATE_FAIL) {
1392 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1393 ret = RESUME_HOST;
1394 goto skip_emul;
1395 }
1396
1397 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001398 case EXCCODE_INT:
1399 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001400
1401 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001402
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001403 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001404 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001405
1406 ret = RESUME_GUEST;
1407 break;
1408
James Hogan16d100db2015-12-16 23:49:33 +00001409 case EXCCODE_CPU:
1410 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001411
1412 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001413 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1414 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001415 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001416 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001417 break;
1418
James Hogan16d100db2015-12-16 23:49:33 +00001419 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001420 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001421 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1422 break;
1423
James Hogan16d100db2015-12-16 23:49:33 +00001424 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001425 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1426 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1427 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001428
1429 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001430 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1431 break;
1432
James Hogan16d100db2015-12-16 23:49:33 +00001433 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001434 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1435 cause, opc, badvaddr);
1436
1437 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001438 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1439 break;
1440
James Hogan16d100db2015-12-16 23:49:33 +00001441 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001442 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001443 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1444 break;
1445
James Hogan16d100db2015-12-16 23:49:33 +00001446 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001447 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001448 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1449 break;
1450
James Hogan16d100db2015-12-16 23:49:33 +00001451 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001452 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001453 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1454 break;
1455
James Hogan16d100db2015-12-16 23:49:33 +00001456 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001457 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001458 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1459 break;
1460
James Hogan16d100db2015-12-16 23:49:33 +00001461 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001462 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001463 ret = kvm_mips_callbacks->handle_break(vcpu);
1464 break;
1465
James Hogan16d100db2015-12-16 23:49:33 +00001466 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001467 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001468 ret = kvm_mips_callbacks->handle_trap(vcpu);
1469 break;
1470
James Hogan16d100db2015-12-16 23:49:33 +00001471 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001472 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001473 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1474 break;
1475
James Hogan16d100db2015-12-16 23:49:33 +00001476 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001477 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001478 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1479 break;
1480
James Hogan16d100db2015-12-16 23:49:33 +00001481 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001482 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001483 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1484 break;
1485
Sanjay Lal669e8462012-11-21 18:34:02 -08001486 default:
James Hogan122e51d2016-11-28 17:23:14 +00001487 if (cause & CAUSEF_BD)
1488 opc += 1;
1489 inst = 0;
James Hogan6a97c772015-04-23 16:54:35 +01001490 kvm_get_badinstr(opc, vcpu, &inst);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001491 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
James Hogan122e51d2016-11-28 17:23:14 +00001492 exccode, opc, inst, badvaddr,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001493 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001494 kvm_arch_vcpu_dump_regs(vcpu);
1495 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1496 ret = RESUME_HOST;
1497 break;
1498
1499 }
1500
1501skip_emul:
1502 local_irq_disable();
1503
1504 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1505 kvm_mips_deliver_interrupts(vcpu, cause);
1506
1507 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001508 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001509 if (signal_pending(current)) {
1510 run->exit_reason = KVM_EXIT_INTR;
1511 ret = (-EINTR << 2) | RESUME_HOST;
1512 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001513 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001514 }
1515 }
1516
James Hogan98e91b82014-11-18 14:09:12 +00001517 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001518 trace_kvm_reenter(vcpu);
1519
James Hogan4841e0d2016-11-28 22:45:04 +00001520 /*
1521 * Make sure the read of VCPU requests in vcpu_reenter()
1522 * callback is not reordered ahead of the write to vcpu->mode,
1523 * or we could miss a TLB flush request while the requester sees
1524 * the VCPU as outside of guest mode and not needing an IPI.
1525 */
1526 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
1527
James Hogana2c046e2016-11-18 13:14:37 +00001528 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +01001529
James Hogan98e91b82014-11-18 14:09:12 +00001530 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001531 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1532 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001533 *
1534 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001535 * vector, as it may well cause an [MSA] FP exception if there
1536 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001537 * kvm_mips_csr_die_notifier() for how that is handled).
1538 */
1539 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1540 read_c0_status() & ST0_CU1)
1541 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001542
1543 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1544 read_c0_config5() & MIPS_CONF5_MSAEN)
1545 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001546 }
1547
James Hoganc4c6f2c2015-02-04 10:52:03 +00001548 /* Disable HTW before returning to guest or host */
1549 htw_stop();
1550
Sanjay Lal669e8462012-11-21 18:34:02 -08001551 return ret;
1552}
1553
James Hogan98e91b82014-11-18 14:09:12 +00001554/* Enable FPU for guest and restore context */
1555void kvm_own_fpu(struct kvm_vcpu *vcpu)
1556{
1557 struct mips_coproc *cop0 = vcpu->arch.cop0;
1558 unsigned int sr, cfg5;
1559
1560 preempt_disable();
1561
James Hogan539cb89fb2015-03-05 11:43:36 +00001562 sr = kvm_read_c0_guest_status(cop0);
1563
1564 /*
1565 * If MSA state is already live, it is undefined how it interacts with
1566 * FR=0 FPU state, and we don't want to hit reserved instruction
1567 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1568 * play it safe and save it first.
1569 *
1570 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1571 * get called when guest CU1 is set, however we can't trust the guest
1572 * not to clobber the status register directly via the commpage.
1573 */
1574 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001575 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001576 kvm_lose_fpu(vcpu);
1577
James Hogan98e91b82014-11-18 14:09:12 +00001578 /*
1579 * Enable FPU for guest
1580 * We set FR and FRE according to guest context
1581 */
James Hogan98e91b82014-11-18 14:09:12 +00001582 change_c0_status(ST0_CU1 | ST0_FR, sr);
1583 if (cpu_has_fre) {
1584 cfg5 = kvm_read_c0_guest_config5(cop0);
1585 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1586 }
1587 enable_fpu_hazard();
1588
1589 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001590 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001591 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001592 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001593 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1594 } else {
1595 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001596 }
1597
1598 preempt_enable();
1599}
1600
James Hogan539cb89fb2015-03-05 11:43:36 +00001601#ifdef CONFIG_CPU_HAS_MSA
1602/* Enable MSA for guest and restore context */
1603void kvm_own_msa(struct kvm_vcpu *vcpu)
1604{
1605 struct mips_coproc *cop0 = vcpu->arch.cop0;
1606 unsigned int sr, cfg5;
1607
1608 preempt_disable();
1609
1610 /*
1611 * Enable FPU if enabled in guest, since we're restoring FPU context
1612 * anyway. We set FR and FRE according to guest context.
1613 */
1614 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1615 sr = kvm_read_c0_guest_status(cop0);
1616
1617 /*
1618 * If FR=0 FPU state is already live, it is undefined how it
1619 * interacts with MSA state, so play it safe and save it first.
1620 */
1621 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001622 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1623 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001624 kvm_lose_fpu(vcpu);
1625
1626 change_c0_status(ST0_CU1 | ST0_FR, sr);
1627 if (sr & ST0_CU1 && cpu_has_fre) {
1628 cfg5 = kvm_read_c0_guest_config5(cop0);
1629 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1630 }
1631 }
1632
1633 /* Enable MSA for guest */
1634 set_c0_config5(MIPS_CONF5_MSAEN);
1635 enable_fpu_hazard();
1636
James Hoganf9431762016-06-14 09:40:10 +01001637 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1638 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001639 /*
1640 * Guest FPU state already loaded, only restore upper MSA state
1641 */
1642 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001643 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001644 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001645 break;
1646 case 0:
1647 /* Neither FPU or MSA already active, restore full MSA state */
1648 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001649 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001650 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001651 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001652 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1653 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001654 break;
1655 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001656 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001657 break;
1658 }
1659
1660 preempt_enable();
1661}
1662#endif
1663
1664/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001665void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1666{
1667 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001668 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001669 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001670 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001671 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001672 }
James Hoganf9431762016-06-14 09:40:10 +01001673 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001674 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001675 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001676 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001677 }
1678 preempt_enable();
1679}
1680
James Hogan539cb89fb2015-03-05 11:43:36 +00001681/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001682void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1683{
1684 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001685 * FPU & MSA get disabled in root context (hardware) when it is disabled
1686 * in guest context (software), but the register state in the hardware
1687 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001688 * before saving.
1689 */
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 set_c0_config5(MIPS_CONF5_MSAEN);
1694 enable_fpu_hazard();
1695
1696 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001697 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001698
1699 /* Disable MSA & FPU */
1700 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001701 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001702 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001703 disable_fpu_hazard();
1704 }
James Hoganf9431762016-06-14 09:40:10 +01001705 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1706 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001707 set_c0_status(ST0_CU1);
1708 enable_fpu_hazard();
1709
1710 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001711 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001712 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001713
1714 /* Disable FPU */
1715 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001716 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001717 }
1718 preempt_enable();
1719}
1720
1721/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001722 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1723 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1724 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001725 */
1726static int kvm_mips_csr_die_notify(struct notifier_block *self,
1727 unsigned long cmd, void *ptr)
1728{
1729 struct die_args *args = (struct die_args *)ptr;
1730 struct pt_regs *regs = args->regs;
1731 unsigned long pc;
1732
James Hogan539cb89fb2015-03-05 11:43:36 +00001733 /* Only interested in FPE and MSAFPE */
1734 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001735 return NOTIFY_DONE;
1736
1737 /* Return immediately if guest context isn't active */
1738 if (!(current->flags & PF_VCPU))
1739 return NOTIFY_DONE;
1740
1741 /* Should never get here from user mode */
1742 BUG_ON(user_mode(regs));
1743
1744 pc = instruction_pointer(regs);
1745 switch (cmd) {
1746 case DIE_FP:
1747 /* match 2nd instruction in __kvm_restore_fcsr */
1748 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1749 return NOTIFY_DONE;
1750 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001751 case DIE_MSAFP:
1752 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1753 if (!cpu_has_msa ||
1754 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1755 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1756 return NOTIFY_DONE;
1757 break;
James Hogan98e91b82014-11-18 14:09:12 +00001758 }
1759
1760 /* Move PC forward a little and continue executing */
1761 instruction_pointer(regs) += 4;
1762
1763 return NOTIFY_STOP;
1764}
1765
1766static struct notifier_block kvm_mips_csr_die_notifier = {
1767 .notifier_call = kvm_mips_csr_die_notify,
1768};
1769
James Hogan2db9d232015-12-16 23:49:32 +00001770static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001771{
1772 int ret;
1773
James Hogan1e5217f52016-06-23 17:34:45 +01001774 ret = kvm_mips_entry_setup();
1775 if (ret)
1776 return ret;
1777
Sanjay Lal669e8462012-11-21 18:34:02 -08001778 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1779
1780 if (ret)
1781 return ret;
1782
James Hogan98e91b82014-11-18 14:09:12 +00001783 register_die_notifier(&kvm_mips_csr_die_notifier);
1784
Sanjay Lal669e8462012-11-21 18:34:02 -08001785 return 0;
1786}
1787
James Hogan2db9d232015-12-16 23:49:32 +00001788static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001789{
1790 kvm_exit();
1791
James Hogan98e91b82014-11-18 14:09:12 +00001792 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001793}
1794
1795module_init(kvm_mips_init);
1796module_exit(kvm_mips_exit);
1797
1798EXPORT_TRACEPOINT_SYMBOL(kvm_exit);