blob: 0b84b336ee4da93ab44e8cc3f2c490adacde609f [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 Hoganaba85922016-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
James Hogane88643b2016-12-06 14:50:52 +00001089/**
1090 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
1091 * @kvm: kvm instance
1092 * @log: slot id and address to which we copy the log
1093 *
1094 * Steps 1-4 below provide general overview of dirty page logging. See
1095 * kvm_get_dirty_log_protect() function description for additional details.
1096 *
1097 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1098 * always flush the TLB (step 4) even if previous step failed and the dirty
1099 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1100 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1101 * writes will be marked dirty for next log read.
1102 *
1103 * 1. Take a snapshot of the bit and clear it if needed.
1104 * 2. Write protect the corresponding page.
1105 * 3. Copy the snapshot to the userspace.
1106 * 4. Flush TLB's if needed.
1107 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001108int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1109{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001110 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001111 struct kvm_memory_slot *memslot;
James Hogane88643b2016-12-06 14:50:52 +00001112 bool is_dirty = false;
Sanjay Lal669e8462012-11-21 18:34:02 -08001113 int r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001114
1115 mutex_lock(&kvm->slots_lock);
1116
James Hogane88643b2016-12-06 14:50:52 +00001117 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Sanjay Lal669e8462012-11-21 18:34:02 -08001118
Sanjay Lal669e8462012-11-21 18:34:02 -08001119 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001120 slots = kvm_memslots(kvm);
1121 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001122
James Hogane88643b2016-12-06 14:50:52 +00001123 /* Let implementation handle TLB/GVA invalidation */
1124 kvm_mips_callbacks->flush_shadow_memslot(kvm, memslot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001125 }
1126
Sanjay Lal669e8462012-11-21 18:34:02 -08001127 mutex_unlock(&kvm->slots_lock);
1128 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001129}
1130
1131long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1132{
1133 long r;
1134
1135 switch (ioctl) {
1136 default:
David Daneyed829852013-05-23 09:49:10 -07001137 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001138 }
1139
1140 return r;
1141}
1142
1143int kvm_arch_init(void *opaque)
1144{
Sanjay Lal669e8462012-11-21 18:34:02 -08001145 if (kvm_mips_callbacks) {
1146 kvm_err("kvm: module already exists\n");
1147 return -EEXIST;
1148 }
1149
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001150 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001151}
1152
1153void kvm_arch_exit(void)
1154{
1155 kvm_mips_callbacks = NULL;
1156}
1157
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001158int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1159 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001160{
David Daneyed829852013-05-23 09:49:10 -07001161 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001162}
1163
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001164int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1165 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001166{
David Daneyed829852013-05-23 09:49:10 -07001167 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001168}
1169
Dominik Dingel31928aa2014-12-04 15:47:07 +01001170void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001171{
Sanjay Lal669e8462012-11-21 18:34:02 -08001172}
1173
1174int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1175{
David Daneyed829852013-05-23 09:49:10 -07001176 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001177}
1178
1179int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1180{
David Daneyed829852013-05-23 09:49:10 -07001181 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001182}
1183
1184int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1185{
1186 return VM_FAULT_SIGBUS;
1187}
1188
Alexander Graf784aa3d2014-07-14 18:27:35 +02001189int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001190{
1191 int r;
1192
1193 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001194 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001195 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001196 r = 1;
1197 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001198 case KVM_CAP_COALESCED_MMIO:
1199 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1200 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001201 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001202 /* We don't handle systems with inconsistent cpu_has_fpu */
1203 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001204 break;
James Hogand952bd02014-12-08 23:07:56 +00001205 case KVM_CAP_MIPS_MSA:
1206 /*
1207 * We don't support MSA vector partitioning yet:
1208 * 1) It would require explicit support which can't be tested
1209 * yet due to lack of support in current hardware.
1210 * 2) It extends the state that would need to be saved/restored
1211 * by e.g. QEMU for migration.
1212 *
1213 * When vector partitioning hardware becomes available, support
1214 * could be added by requiring a flag when enabling
1215 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1216 * to save/restore the appropriate extra state.
1217 */
1218 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1219 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001220 default:
1221 r = 0;
1222 break;
1223 }
1224 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001225}
1226
1227int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1228{
1229 return kvm_mips_pending_timer(vcpu);
1230}
1231
1232int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1233{
1234 int i;
1235 struct mips_coproc *cop0;
1236
1237 if (!vcpu)
1238 return -1;
1239
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001240 kvm_debug("VCPU Register Dump:\n");
1241 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1242 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001243
1244 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001245 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001246 vcpu->arch.gprs[i],
1247 vcpu->arch.gprs[i + 1],
1248 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1249 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001250 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1251 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001252
1253 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001254 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1255 kvm_read_c0_guest_status(cop0),
1256 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001257
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001258 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001259
1260 return 0;
1261}
1262
1263int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1264{
1265 int i;
1266
David Daney8d17dd02013-05-23 09:49:08 -07001267 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001268 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001269 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001270 vcpu->arch.hi = regs->hi;
1271 vcpu->arch.lo = regs->lo;
1272 vcpu->arch.pc = regs->pc;
1273
David Daney4c73fb22013-05-23 09:49:09 -07001274 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001275}
1276
1277int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1278{
1279 int i;
1280
David Daney8d17dd02013-05-23 09:49:08 -07001281 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001282 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001283
1284 regs->hi = vcpu->arch.hi;
1285 regs->lo = vcpu->arch.lo;
1286 regs->pc = vcpu->arch.pc;
1287
David Daney4c73fb22013-05-23 09:49:09 -07001288 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001289}
1290
James Hogan0fae34f2014-05-29 10:16:39 +01001291static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001292{
1293 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1294
1295 kvm_mips_callbacks->queue_timer_int(vcpu);
1296
1297 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001298 if (swait_active(&vcpu->wq))
1299 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001300}
1301
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001302/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001303static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001304{
1305 struct kvm_vcpu *vcpu;
1306
1307 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1308 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001309 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001310}
1311
1312int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1313{
James Hoganf7f14272016-09-08 22:57:03 +01001314 int err;
1315
1316 err = kvm_mips_callbacks->vcpu_init(vcpu);
1317 if (err)
1318 return err;
1319
Sanjay Lal669e8462012-11-21 18:34:02 -08001320 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1321 HRTIMER_MODE_REL);
1322 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001323 return 0;
1324}
1325
James Hogan630766b32016-09-08 23:00:24 +01001326void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1327{
1328 kvm_mips_callbacks->vcpu_uninit(vcpu);
1329}
1330
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001331int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1332 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001333{
1334 return 0;
1335}
1336
1337/* Initial guest state */
1338int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1339{
1340 return kvm_mips_callbacks->vcpu_setup(vcpu);
1341}
1342
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001343static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001344{
James Hogan8cffd192016-06-09 14:19:08 +01001345 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001346
Sanjay Lal669e8462012-11-21 18:34:02 -08001347 if (cpu_has_dsp)
1348 status |= (ST0_MX);
1349
1350 write_c0_status(status);
1351 ehb();
1352}
1353
1354/*
1355 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1356 */
1357int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1358{
James Hogan8cffd192016-06-09 14:19:08 +01001359 u32 cause = vcpu->arch.host_cp0_cause;
1360 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1361 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001362 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1363 enum emulation_result er = EMULATE_DONE;
James Hogan122e51d2016-11-28 17:23:14 +00001364 u32 inst;
Sanjay Lal669e8462012-11-21 18:34:02 -08001365 int ret = RESUME_GUEST;
1366
James Hogan4841e0d2016-11-28 22:45:04 +00001367 vcpu->mode = OUTSIDE_GUEST_MODE;
1368
James Hoganc4c6f2c2015-02-04 10:52:03 +00001369 /* re-enable HTW before enabling interrupts */
1370 htw_start();
1371
Sanjay Lal669e8462012-11-21 18:34:02 -08001372 /* Set a default exit reason */
1373 run->exit_reason = KVM_EXIT_UNKNOWN;
1374 run->ready_for_interrupt_injection = 1;
1375
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001376 /*
1377 * Set the appropriate status bits based on host CPU features,
1378 * before we hit the scheduler
1379 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001380 kvm_mips_set_c0_status();
1381
1382 local_irq_enable();
1383
1384 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1385 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001386 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001387
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001388 /*
1389 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001390 * causing an exception to be delivered to the Guest Kernel
1391 */
1392 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1393 if (er == EMULATE_PRIV_FAIL) {
1394 goto skip_emul;
1395 } else if (er == EMULATE_FAIL) {
1396 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1397 ret = RESUME_HOST;
1398 goto skip_emul;
1399 }
1400
1401 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001402 case EXCCODE_INT:
1403 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001404
1405 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001406
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001407 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001408 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001409
1410 ret = RESUME_GUEST;
1411 break;
1412
James Hogan16d100db2015-12-16 23:49:33 +00001413 case EXCCODE_CPU:
1414 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001415
1416 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001417 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1418 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001419 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001420 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001421 break;
1422
James Hogan16d100db2015-12-16 23:49:33 +00001423 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001424 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001425 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1426 break;
1427
James Hogan16d100db2015-12-16 23:49:33 +00001428 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001429 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1430 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1431 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001432
1433 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001434 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1435 break;
1436
James Hogan16d100db2015-12-16 23:49:33 +00001437 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001438 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1439 cause, opc, badvaddr);
1440
1441 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001442 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1443 break;
1444
James Hogan16d100db2015-12-16 23:49:33 +00001445 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001446 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001447 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1448 break;
1449
James Hogan16d100db2015-12-16 23:49:33 +00001450 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001451 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001452 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1453 break;
1454
James Hogan16d100db2015-12-16 23:49:33 +00001455 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001456 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001457 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1458 break;
1459
James Hogan16d100db2015-12-16 23:49:33 +00001460 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001461 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001462 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1463 break;
1464
James Hogan16d100db2015-12-16 23:49:33 +00001465 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001466 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001467 ret = kvm_mips_callbacks->handle_break(vcpu);
1468 break;
1469
James Hogan16d100db2015-12-16 23:49:33 +00001470 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001471 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001472 ret = kvm_mips_callbacks->handle_trap(vcpu);
1473 break;
1474
James Hogan16d100db2015-12-16 23:49:33 +00001475 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001476 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001477 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1478 break;
1479
James Hogan16d100db2015-12-16 23:49:33 +00001480 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001481 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001482 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1483 break;
1484
James Hogan16d100db2015-12-16 23:49:33 +00001485 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001486 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001487 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1488 break;
1489
Sanjay Lal669e8462012-11-21 18:34:02 -08001490 default:
James Hogan122e51d2016-11-28 17:23:14 +00001491 if (cause & CAUSEF_BD)
1492 opc += 1;
1493 inst = 0;
James Hogan6a97c772015-04-23 16:54:35 +01001494 kvm_get_badinstr(opc, vcpu, &inst);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001495 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 +00001496 exccode, opc, inst, badvaddr,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001497 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001498 kvm_arch_vcpu_dump_regs(vcpu);
1499 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1500 ret = RESUME_HOST;
1501 break;
1502
1503 }
1504
1505skip_emul:
1506 local_irq_disable();
1507
1508 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1509 kvm_mips_deliver_interrupts(vcpu, cause);
1510
1511 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001512 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001513 if (signal_pending(current)) {
1514 run->exit_reason = KVM_EXIT_INTR;
1515 ret = (-EINTR << 2) | RESUME_HOST;
1516 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001517 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001518 }
1519 }
1520
James Hogan98e91b82014-11-18 14:09:12 +00001521 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001522 trace_kvm_reenter(vcpu);
1523
James Hogan4841e0d2016-11-28 22:45:04 +00001524 /*
1525 * Make sure the read of VCPU requests in vcpu_reenter()
1526 * callback is not reordered ahead of the write to vcpu->mode,
1527 * or we could miss a TLB flush request while the requester sees
1528 * the VCPU as outside of guest mode and not needing an IPI.
1529 */
1530 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
1531
James Hogana2c046e2016-11-18 13:14:37 +00001532 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +01001533
James Hogan98e91b82014-11-18 14:09:12 +00001534 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001535 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1536 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001537 *
1538 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001539 * vector, as it may well cause an [MSA] FP exception if there
1540 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001541 * kvm_mips_csr_die_notifier() for how that is handled).
1542 */
1543 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1544 read_c0_status() & ST0_CU1)
1545 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001546
1547 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1548 read_c0_config5() & MIPS_CONF5_MSAEN)
1549 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001550 }
1551
James Hoganc4c6f2c2015-02-04 10:52:03 +00001552 /* Disable HTW before returning to guest or host */
1553 htw_stop();
1554
Sanjay Lal669e8462012-11-21 18:34:02 -08001555 return ret;
1556}
1557
James Hogan98e91b82014-11-18 14:09:12 +00001558/* Enable FPU for guest and restore context */
1559void kvm_own_fpu(struct kvm_vcpu *vcpu)
1560{
1561 struct mips_coproc *cop0 = vcpu->arch.cop0;
1562 unsigned int sr, cfg5;
1563
1564 preempt_disable();
1565
James Hogan539cb89fb2015-03-05 11:43:36 +00001566 sr = kvm_read_c0_guest_status(cop0);
1567
1568 /*
1569 * If MSA state is already live, it is undefined how it interacts with
1570 * FR=0 FPU state, and we don't want to hit reserved instruction
1571 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1572 * play it safe and save it first.
1573 *
1574 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1575 * get called when guest CU1 is set, however we can't trust the guest
1576 * not to clobber the status register directly via the commpage.
1577 */
1578 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001579 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001580 kvm_lose_fpu(vcpu);
1581
James Hogan98e91b82014-11-18 14:09:12 +00001582 /*
1583 * Enable FPU for guest
1584 * We set FR and FRE according to guest context
1585 */
James Hogan98e91b82014-11-18 14:09:12 +00001586 change_c0_status(ST0_CU1 | ST0_FR, sr);
1587 if (cpu_has_fre) {
1588 cfg5 = kvm_read_c0_guest_config5(cop0);
1589 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1590 }
1591 enable_fpu_hazard();
1592
1593 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001594 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001595 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001596 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001597 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1598 } else {
1599 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001600 }
1601
1602 preempt_enable();
1603}
1604
James Hogan539cb89fb2015-03-05 11:43:36 +00001605#ifdef CONFIG_CPU_HAS_MSA
1606/* Enable MSA for guest and restore context */
1607void kvm_own_msa(struct kvm_vcpu *vcpu)
1608{
1609 struct mips_coproc *cop0 = vcpu->arch.cop0;
1610 unsigned int sr, cfg5;
1611
1612 preempt_disable();
1613
1614 /*
1615 * Enable FPU if enabled in guest, since we're restoring FPU context
1616 * anyway. We set FR and FRE according to guest context.
1617 */
1618 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1619 sr = kvm_read_c0_guest_status(cop0);
1620
1621 /*
1622 * If FR=0 FPU state is already live, it is undefined how it
1623 * interacts with MSA state, so play it safe and save it first.
1624 */
1625 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001626 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1627 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001628 kvm_lose_fpu(vcpu);
1629
1630 change_c0_status(ST0_CU1 | ST0_FR, sr);
1631 if (sr & ST0_CU1 && cpu_has_fre) {
1632 cfg5 = kvm_read_c0_guest_config5(cop0);
1633 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1634 }
1635 }
1636
1637 /* Enable MSA for guest */
1638 set_c0_config5(MIPS_CONF5_MSAEN);
1639 enable_fpu_hazard();
1640
James Hoganf9431762016-06-14 09:40:10 +01001641 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1642 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001643 /*
1644 * Guest FPU state already loaded, only restore upper MSA state
1645 */
1646 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001647 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001648 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001649 break;
1650 case 0:
1651 /* Neither FPU or MSA already active, restore full MSA state */
1652 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001653 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001654 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001655 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001656 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1657 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001658 break;
1659 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001660 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001661 break;
1662 }
1663
1664 preempt_enable();
1665}
1666#endif
1667
1668/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001669void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1670{
1671 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001672 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001673 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001674 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001675 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001676 }
James Hoganf9431762016-06-14 09:40:10 +01001677 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001678 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001679 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001680 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001681 }
1682 preempt_enable();
1683}
1684
James Hogan539cb89fb2015-03-05 11:43:36 +00001685/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001686void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1687{
1688 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001689 * FPU & MSA get disabled in root context (hardware) when it is disabled
1690 * in guest context (software), but the register state in the hardware
1691 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001692 * before saving.
1693 */
1694
1695 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001696 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001697 set_c0_config5(MIPS_CONF5_MSAEN);
1698 enable_fpu_hazard();
1699
1700 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001701 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001702
1703 /* Disable MSA & FPU */
1704 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001705 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001706 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001707 disable_fpu_hazard();
1708 }
James Hoganf9431762016-06-14 09:40:10 +01001709 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1710 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001711 set_c0_status(ST0_CU1);
1712 enable_fpu_hazard();
1713
1714 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001715 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001716 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001717
1718 /* Disable FPU */
1719 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001720 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001721 }
1722 preempt_enable();
1723}
1724
1725/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001726 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1727 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1728 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001729 */
1730static int kvm_mips_csr_die_notify(struct notifier_block *self,
1731 unsigned long cmd, void *ptr)
1732{
1733 struct die_args *args = (struct die_args *)ptr;
1734 struct pt_regs *regs = args->regs;
1735 unsigned long pc;
1736
James Hogan539cb89fb2015-03-05 11:43:36 +00001737 /* Only interested in FPE and MSAFPE */
1738 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001739 return NOTIFY_DONE;
1740
1741 /* Return immediately if guest context isn't active */
1742 if (!(current->flags & PF_VCPU))
1743 return NOTIFY_DONE;
1744
1745 /* Should never get here from user mode */
1746 BUG_ON(user_mode(regs));
1747
1748 pc = instruction_pointer(regs);
1749 switch (cmd) {
1750 case DIE_FP:
1751 /* match 2nd instruction in __kvm_restore_fcsr */
1752 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1753 return NOTIFY_DONE;
1754 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001755 case DIE_MSAFP:
1756 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1757 if (!cpu_has_msa ||
1758 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1759 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1760 return NOTIFY_DONE;
1761 break;
James Hogan98e91b82014-11-18 14:09:12 +00001762 }
1763
1764 /* Move PC forward a little and continue executing */
1765 instruction_pointer(regs) += 4;
1766
1767 return NOTIFY_STOP;
1768}
1769
1770static struct notifier_block kvm_mips_csr_die_notifier = {
1771 .notifier_call = kvm_mips_csr_die_notify,
1772};
1773
James Hogan2db9d232015-12-16 23:49:32 +00001774static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001775{
1776 int ret;
1777
James Hogan1e5217f52016-06-23 17:34:45 +01001778 ret = kvm_mips_entry_setup();
1779 if (ret)
1780 return ret;
1781
Sanjay Lal669e8462012-11-21 18:34:02 -08001782 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1783
1784 if (ret)
1785 return ret;
1786
James Hogan98e91b82014-11-18 14:09:12 +00001787 register_die_notifier(&kvm_mips_csr_die_notifier);
1788
Sanjay Lal669e8462012-11-21 18:34:02 -08001789 return 0;
1790}
1791
James Hogan2db9d232015-12-16 23:49:32 +00001792static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001793{
1794 kvm_exit();
1795
James Hogan98e91b82014-11-18 14:09:12 +00001796 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001797}
1798
1799module_init(kvm_mips_init);
1800module_exit(kvm_mips_exit);
1801
1802EXPORT_TRACEPOINT_SYMBOL(kvm_exit);