blob: 1733877d8a53718cad5791fd03e0141b6c9bedfa [file] [log] [blame]
Sanjay Lal669e8462012-11-21 18:34:02 -08001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * KVM/MIPS: MIPS specific KVM APIs
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070010 */
Sanjay Lal669e8462012-11-21 18:34:02 -080011
James Hogan05108702016-06-15 19:29:56 +010012#include <linux/bitops.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080013#include <linux/errno.h>
14#include <linux/err.h>
James Hogan98e91b82014-11-18 14:09:12 +000015#include <linux/kdebug.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080016#include <linux/module.h>
James Hogand852b5f2016-10-19 00:24:27 +010017#include <linux/uaccess.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080018#include <linux/vmalloc.h>
19#include <linux/fs.h>
20#include <linux/bootmem.h>
James Hoganf7982172015-02-04 17:06:37 +000021#include <asm/fpu.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080022#include <asm/page.h>
23#include <asm/cacheflush.h>
24#include <asm/mmu_context.h>
James Hoganc4c6f2c2015-02-04 10:52:03 +000025#include <asm/pgtable.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080026
27#include <linux/kvm_host.h>
28
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070029#include "interrupt.h"
30#include "commpage.h"
Sanjay Lal669e8462012-11-21 18:34:02 -080031
32#define CREATE_TRACE_POINTS
33#include "trace.h"
34
35#ifndef VECTORSPACING
36#define VECTORSPACING 0x100 /* for EI/VI mode */
37#endif
38
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070039#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
Sanjay Lal669e8462012-11-21 18:34:02 -080040struct kvm_stats_debugfs_item debugfs_entries[] = {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070041 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
42 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
43 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
44 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
45 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
46 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
47 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
48 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
49 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
50 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
51 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
52 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
53 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
James Hogan0a560422015-02-06 16:03:57 +000054 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000055 { "msa_fpe", VCPU_STAT(msa_fpe_exits), KVM_STAT_VCPU },
James Hogan1c0cd662015-02-06 10:56:27 +000056 { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000057 { "msa_disabled", VCPU_STAT(msa_disabled_exits), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070058 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
Paolo Bonzinif7819512015-02-04 18:20:58 +010059 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +020060 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), KVM_STAT_VCPU },
Christian Borntraeger3491caf2016-05-13 12:16:35 +020061 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070062 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
Sanjay Lal669e8462012-11-21 18:34:02 -080063 {NULL}
64};
65
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070066/*
67 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
68 * Config7, so we are "runnable" if interrupts are pending
Sanjay Lal669e8462012-11-21 18:34:02 -080069 */
70int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
71{
72 return !!(vcpu->arch.pending_exceptions);
73}
74
75int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
76{
77 return 1;
78}
79
Radim Krčmář13a34e02014-08-28 15:13:03 +020080int kvm_arch_hardware_enable(void)
Sanjay Lal669e8462012-11-21 18:34:02 -080081{
82 return 0;
83}
84
Sanjay Lal669e8462012-11-21 18:34:02 -080085int kvm_arch_hardware_setup(void)
86{
87 return 0;
88}
89
Sanjay Lal669e8462012-11-21 18:34:02 -080090void kvm_arch_check_processor_compat(void *rtn)
91{
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -070092 *(int *)rtn = 0;
Sanjay Lal669e8462012-11-21 18:34:02 -080093}
94
95static void kvm_mips_init_tlbs(struct kvm *kvm)
96{
97 unsigned long wired;
98
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070099 /*
100 * Add a wired entry to the TLB, it is used to map the commpage to
101 * the Guest kernel
102 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800103 wired = read_c0_wired();
104 write_c0_wired(wired + 1);
105 mtc0_tlbw_hazard();
106 kvm->arch.commpage_tlb = wired;
107
108 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
109 kvm->arch.commpage_tlb);
110}
111
112static void kvm_mips_init_vm_percpu(void *arg)
113{
114 struct kvm *kvm = (struct kvm *)arg;
115
116 kvm_mips_init_tlbs(kvm);
117 kvm_mips_callbacks->vm_init(kvm);
118
119}
120
121int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
122{
123 if (atomic_inc_return(&kvm_mips_instance) == 1) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100124 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
125 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800126 on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
127 }
128
Sanjay Lal669e8462012-11-21 18:34:02 -0800129 return 0;
130}
131
Luiz Capitulino235539b2016-09-07 14:47:23 -0400132bool kvm_arch_has_vcpu_debugfs(void)
133{
134 return false;
135}
136
137int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
138{
139 return 0;
140}
141
Sanjay Lal669e8462012-11-21 18:34:02 -0800142void kvm_mips_free_vcpus(struct kvm *kvm)
143{
144 unsigned int i;
145 struct kvm_vcpu *vcpu;
146
147 /* Put the pages we reserved for the guest pmap */
148 for (i = 0; i < kvm->arch.guest_pmap_npages; i++) {
149 if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE)
James Hogan9befad22016-06-09 14:19:11 +0100150 kvm_release_pfn_clean(kvm->arch.guest_pmap[i]);
Sanjay Lal669e8462012-11-21 18:34:02 -0800151 }
James Hoganc6c0a662014-05-29 10:16:44 +0100152 kfree(kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800153
154 kvm_for_each_vcpu(i, vcpu, kvm) {
155 kvm_arch_vcpu_free(vcpu);
156 }
157
158 mutex_lock(&kvm->lock);
159
160 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
161 kvm->vcpus[i] = NULL;
162
163 atomic_set(&kvm->online_vcpus, 0);
164
165 mutex_unlock(&kvm->lock);
166}
167
Sanjay Lal669e8462012-11-21 18:34:02 -0800168static void kvm_mips_uninit_tlbs(void *arg)
169{
170 /* Restore wired count */
171 write_c0_wired(0);
172 mtc0_tlbw_hazard();
173 /* Clear out all the TLBs */
174 kvm_local_flush_tlb_all();
175}
176
177void kvm_arch_destroy_vm(struct kvm *kvm)
178{
179 kvm_mips_free_vcpus(kvm);
180
181 /* If this is the last instance, restore wired count */
182 if (atomic_dec_return(&kvm_mips_instance) == 0) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100183 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
184 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800185 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
186 }
187}
188
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700189long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
190 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800191{
David Daneyed829852013-05-23 09:49:10 -0700192 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800193}
194
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530195int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
196 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800197{
198 return 0;
199}
200
201int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700202 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200203 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700204 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800205{
206 return 0;
207}
208
209void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200210 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700211 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200212 const struct kvm_memory_slot *new,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700213 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800214{
215 unsigned long npages = 0;
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700216 int i;
Sanjay Lal669e8462012-11-21 18:34:02 -0800217
218 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
219 __func__, kvm, mem->slot, mem->guest_phys_addr,
220 mem->memory_size, mem->userspace_addr);
221
222 /* Setup Guest PMAP table */
223 if (!kvm->arch.guest_pmap) {
224 if (mem->slot == 0)
225 npages = mem->memory_size >> PAGE_SHIFT;
226
227 if (npages) {
228 kvm->arch.guest_pmap_npages = npages;
229 kvm->arch.guest_pmap =
230 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
231
232 if (!kvm->arch.guest_pmap) {
James Hoganf7fdcb62015-12-16 23:49:39 +0000233 kvm_err("Failed to allocate guest PMAP\n");
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700234 return;
Sanjay Lal669e8462012-11-21 18:34:02 -0800235 }
236
James Hogan6e95bfd2014-05-29 10:16:43 +0100237 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
238 npages, kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800239
240 /* Now setup the page table */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700241 for (i = 0; i < npages; i++)
Sanjay Lal669e8462012-11-21 18:34:02 -0800242 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
Sanjay Lal669e8462012-11-21 18:34:02 -0800243 }
244 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800245}
246
James Hogand7b8f892016-06-23 17:34:40 +0100247static inline void dump_handler(const char *symbol, void *start, void *end)
248{
249 u32 *p;
250
251 pr_debug("LEAF(%s)\n", symbol);
252
253 pr_debug("\t.set push\n");
254 pr_debug("\t.set noreorder\n");
255
256 for (p = start; p < (u32 *)end; ++p)
257 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p);
258
259 pr_debug("\t.set\tpop\n");
260
261 pr_debug("\tEND(%s)\n", symbol);
262}
263
Sanjay Lal669e8462012-11-21 18:34:02 -0800264struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
265{
James Hogan90e93112016-06-23 17:34:39 +0100266 int err, size;
James Hogan1f9ca622016-06-23 17:34:46 +0100267 void *gebase, *p, *handler;
Sanjay Lal669e8462012-11-21 18:34:02 -0800268 int i;
269
270 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
271
272 if (!vcpu) {
273 err = -ENOMEM;
274 goto out;
275 }
276
277 err = kvm_vcpu_init(vcpu, kvm, id);
278
279 if (err)
280 goto out_free_cpu;
281
James Hogan6e95bfd2014-05-29 10:16:43 +0100282 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800283
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700284 /*
285 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800286 * guest mode exits
287 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700288 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800289 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700290 else
James Hogan7006e2d2014-05-29 10:16:23 +0100291 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800292
Sanjay Lal669e8462012-11-21 18:34:02 -0800293 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
294
295 if (!gebase) {
296 err = -ENOMEM;
James Hogan585bb8f2015-11-11 14:21:20 +0000297 goto out_uninit_cpu;
Sanjay Lal669e8462012-11-21 18:34:02 -0800298 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100299 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
300 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800301
James Hogan2a06dab2016-07-08 11:53:26 +0100302 /*
303 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
304 * limits us to the low 512MB of physical address space. If the memory
305 * we allocate is out of range, just give up now.
306 */
307 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) {
308 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
309 gebase);
310 err = -ENOMEM;
311 goto out_free_gebase;
312 }
313
Sanjay Lal669e8462012-11-21 18:34:02 -0800314 /* Save new ebase */
315 vcpu->arch.guest_ebase = gebase;
316
James Hogan90e93112016-06-23 17:34:39 +0100317 /* Build guest exception vectors dynamically in unmapped memory */
James Hogan1f9ca622016-06-23 17:34:46 +0100318 handler = gebase + 0x2000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800319
320 /* TLB Refill, EXL = 0 */
James Hogan1f9ca622016-06-23 17:34:46 +0100321 kvm_mips_build_exception(gebase, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800322
323 /* General Exception Entry point */
James Hogan1f9ca622016-06-23 17:34:46 +0100324 kvm_mips_build_exception(gebase + 0x180, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800325
326 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
327 for (i = 0; i < 8; i++) {
328 kvm_debug("L1 Vectored handler @ %p\n",
329 gebase + 0x200 + (i * VECTORSPACING));
James Hogan1f9ca622016-06-23 17:34:46 +0100330 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
331 handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800332 }
333
James Hogan90e93112016-06-23 17:34:39 +0100334 /* General exit handler */
James Hogan1f9ca622016-06-23 17:34:46 +0100335 p = handler;
James Hogan90e93112016-06-23 17:34:39 +0100336 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800337
James Hogan90e93112016-06-23 17:34:39 +0100338 /* Guest entry routine */
339 vcpu->arch.vcpu_run = p;
340 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100341
James Hogand7b8f892016-06-23 17:34:40 +0100342 /* Dump the generated code */
343 pr_debug("#include <asm/asm.h>\n");
344 pr_debug("#include <asm/regdef.h>\n");
345 pr_debug("\n");
346 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
347 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
348 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
349
Sanjay Lal669e8462012-11-21 18:34:02 -0800350 /* Invalidate the icache for these ranges */
James Hogan32eb12a2017-01-03 17:43:01 +0000351 flush_icache_range((unsigned long)gebase,
352 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800353
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700354 /*
355 * Allocate comm page for guest kernel, a TLB will be reserved for
356 * mapping GVA @ 0xFFFF8000 to this page
357 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800358 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
359
360 if (!vcpu->arch.kseg0_commpage) {
361 err = -ENOMEM;
362 goto out_free_gebase;
363 }
364
James Hogan6e95bfd2014-05-29 10:16:43 +0100365 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800366 kvm_mips_commpage_init(vcpu);
367
368 /* Init */
369 vcpu->arch.last_sched_cpu = -1;
370
371 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100372 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800373
374 return vcpu;
375
376out_free_gebase:
377 kfree(gebase);
378
James Hogan585bb8f2015-11-11 14:21:20 +0000379out_uninit_cpu:
380 kvm_vcpu_uninit(vcpu);
381
Sanjay Lal669e8462012-11-21 18:34:02 -0800382out_free_cpu:
383 kfree(vcpu);
384
385out:
386 return ERR_PTR(err);
387}
388
389void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
390{
391 hrtimer_cancel(&vcpu->arch.comparecount_timer);
392
393 kvm_vcpu_uninit(vcpu);
394
395 kvm_mips_dump_stats(vcpu);
396
James Hoganc6c0a662014-05-29 10:16:44 +0100397 kfree(vcpu->arch.guest_ebase);
398 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700399 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800400}
401
402void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
403{
404 kvm_arch_vcpu_free(vcpu);
405}
406
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700407int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
408 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800409{
David Daneyed829852013-05-23 09:49:10 -0700410 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800411}
412
413int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
414{
415 int r = 0;
416 sigset_t sigsaved;
417
418 if (vcpu->sigset_active)
419 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
420
421 if (vcpu->mmio_needed) {
422 if (!vcpu->mmio_is_write)
423 kvm_mips_complete_mmio_load(vcpu, run);
424 vcpu->mmio_needed = 0;
425 }
426
James Hoganf7982172015-02-04 17:06:37 +0000427 lose_fpu(1);
428
James Hogan044f0f02014-05-29 10:16:32 +0100429 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200430 guest_enter_irqoff();
James Hogan93258602016-06-14 09:40:14 +0100431 trace_kvm_enter(vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100432
James Hogana2c046e2016-11-18 13:14:37 +0000433 r = kvm_mips_callbacks->vcpu_run(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100434
James Hogan93258602016-06-14 09:40:14 +0100435 trace_kvm_out(vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200436 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800437 local_irq_enable();
438
439 if (vcpu->sigset_active)
440 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
441
442 return r;
443}
444
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700445int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
446 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800447{
448 int intr = (int)irq->irq;
449 struct kvm_vcpu *dvcpu = NULL;
450
451 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
452 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
453 (int)intr);
454
455 if (irq->cpu == -1)
456 dvcpu = vcpu;
457 else
458 dvcpu = vcpu->kvm->vcpus[irq->cpu];
459
460 if (intr == 2 || intr == 3 || intr == 4) {
461 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
462
463 } else if (intr == -2 || intr == -3 || intr == -4) {
464 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
465 } else {
466 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
467 irq->cpu, irq->irq);
468 return -EINVAL;
469 }
470
471 dvcpu->arch.wait = 0;
472
Marcelo Tosatti85773702016-02-19 09:46:39 +0100473 if (swait_active(&dvcpu->wq))
474 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800475
476 return 0;
477}
478
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700479int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
480 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800481{
David Daneyed829852013-05-23 09:49:10 -0700482 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800483}
484
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700485int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
486 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800487{
David Daneyed829852013-05-23 09:49:10 -0700488 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800489}
490
David Daney4c73fb22013-05-23 09:49:09 -0700491static u64 kvm_mips_get_one_regs[] = {
492 KVM_REG_MIPS_R0,
493 KVM_REG_MIPS_R1,
494 KVM_REG_MIPS_R2,
495 KVM_REG_MIPS_R3,
496 KVM_REG_MIPS_R4,
497 KVM_REG_MIPS_R5,
498 KVM_REG_MIPS_R6,
499 KVM_REG_MIPS_R7,
500 KVM_REG_MIPS_R8,
501 KVM_REG_MIPS_R9,
502 KVM_REG_MIPS_R10,
503 KVM_REG_MIPS_R11,
504 KVM_REG_MIPS_R12,
505 KVM_REG_MIPS_R13,
506 KVM_REG_MIPS_R14,
507 KVM_REG_MIPS_R15,
508 KVM_REG_MIPS_R16,
509 KVM_REG_MIPS_R17,
510 KVM_REG_MIPS_R18,
511 KVM_REG_MIPS_R19,
512 KVM_REG_MIPS_R20,
513 KVM_REG_MIPS_R21,
514 KVM_REG_MIPS_R22,
515 KVM_REG_MIPS_R23,
516 KVM_REG_MIPS_R24,
517 KVM_REG_MIPS_R25,
518 KVM_REG_MIPS_R26,
519 KVM_REG_MIPS_R27,
520 KVM_REG_MIPS_R28,
521 KVM_REG_MIPS_R29,
522 KVM_REG_MIPS_R30,
523 KVM_REG_MIPS_R31,
524
James Hogan70e92c7e2016-07-04 19:35:11 +0100525#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700526 KVM_REG_MIPS_HI,
527 KVM_REG_MIPS_LO,
James Hogan70e92c7e2016-07-04 19:35:11 +0100528#endif
David Daney4c73fb22013-05-23 09:49:09 -0700529 KVM_REG_MIPS_PC,
530
531 KVM_REG_MIPS_CP0_INDEX,
532 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100533 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700534 KVM_REG_MIPS_CP0_PAGEMASK,
535 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100536 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700537 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100538 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700539 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100540 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700541 KVM_REG_MIPS_CP0_STATUS,
542 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100543 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100544 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700545 KVM_REG_MIPS_CP0_CONFIG,
546 KVM_REG_MIPS_CP0_CONFIG1,
547 KVM_REG_MIPS_CP0_CONFIG2,
548 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100549 KVM_REG_MIPS_CP0_CONFIG4,
550 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700551 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100552 KVM_REG_MIPS_CP0_ERROREPC,
553
554 KVM_REG_MIPS_COUNT_CTL,
555 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100556 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700557};
558
James Hogane5775932016-06-15 19:29:51 +0100559static u64 kvm_mips_get_one_regs_fpu[] = {
560 KVM_REG_MIPS_FCR_IR,
561 KVM_REG_MIPS_FCR_CSR,
562};
563
564static u64 kvm_mips_get_one_regs_msa[] = {
565 KVM_REG_MIPS_MSA_IR,
566 KVM_REG_MIPS_MSA_CSR,
567};
568
James Hogan05108702016-06-15 19:29:56 +0100569static u64 kvm_mips_get_one_regs_kscratch[] = {
570 KVM_REG_MIPS_CP0_KSCRATCH1,
571 KVM_REG_MIPS_CP0_KSCRATCH2,
572 KVM_REG_MIPS_CP0_KSCRATCH3,
573 KVM_REG_MIPS_CP0_KSCRATCH4,
574 KVM_REG_MIPS_CP0_KSCRATCH5,
575 KVM_REG_MIPS_CP0_KSCRATCH6,
576};
577
James Hoganf5c43bd2016-06-15 19:29:49 +0100578static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
579{
580 unsigned long ret;
581
582 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100583 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
584 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
585 /* odd doubles */
586 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
587 ret += 16;
588 }
589 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
590 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hogan05108702016-06-15 19:29:56 +0100591 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
James Hoganf5c43bd2016-06-15 19:29:49 +0100592 ret += kvm_mips_callbacks->num_regs(vcpu);
593
594 return ret;
595}
596
597static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
598{
James Hogane5775932016-06-15 19:29:51 +0100599 u64 index;
600 unsigned int i;
601
James Hoganf5c43bd2016-06-15 19:29:49 +0100602 if (copy_to_user(indices, kvm_mips_get_one_regs,
603 sizeof(kvm_mips_get_one_regs)))
604 return -EFAULT;
605 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
606
James Hogane5775932016-06-15 19:29:51 +0100607 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
608 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
609 sizeof(kvm_mips_get_one_regs_fpu)))
610 return -EFAULT;
611 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
612
613 for (i = 0; i < 32; ++i) {
614 index = KVM_REG_MIPS_FPR_32(i);
615 if (copy_to_user(indices, &index, sizeof(index)))
616 return -EFAULT;
617 ++indices;
618
619 /* skip odd doubles if no F64 */
620 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
621 continue;
622
623 index = KVM_REG_MIPS_FPR_64(i);
624 if (copy_to_user(indices, &index, sizeof(index)))
625 return -EFAULT;
626 ++indices;
627 }
628 }
629
630 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
631 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
632 sizeof(kvm_mips_get_one_regs_msa)))
633 return -EFAULT;
634 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
635
636 for (i = 0; i < 32; ++i) {
637 index = KVM_REG_MIPS_VEC_128(i);
638 if (copy_to_user(indices, &index, sizeof(index)))
639 return -EFAULT;
640 ++indices;
641 }
642 }
643
James Hogan05108702016-06-15 19:29:56 +0100644 for (i = 0; i < 6; ++i) {
645 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
646 continue;
647
648 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
649 sizeof(kvm_mips_get_one_regs_kscratch[i])))
650 return -EFAULT;
651 ++indices;
652 }
653
James Hoganf5c43bd2016-06-15 19:29:49 +0100654 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
655}
656
David Daney4c73fb22013-05-23 09:49:09 -0700657static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
658 const struct kvm_one_reg *reg)
659{
David Daney4c73fb22013-05-23 09:49:09 -0700660 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000661 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100662 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700663 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000664 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000665 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700666
667 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000668 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700669 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
670 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
671 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100672#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700673 case KVM_REG_MIPS_HI:
674 v = (long)vcpu->arch.hi;
675 break;
676 case KVM_REG_MIPS_LO:
677 v = (long)vcpu->arch.lo;
678 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100679#endif
David Daney4c73fb22013-05-23 09:49:09 -0700680 case KVM_REG_MIPS_PC:
681 v = (long)vcpu->arch.pc;
682 break;
683
James Hogan379245c2014-12-02 15:48:24 +0000684 /* Floating point registers */
685 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
686 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
687 return -EINVAL;
688 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
689 /* Odd singles in top of even double when FR=0 */
690 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
691 v = get_fpr32(&fpu->fpr[idx], 0);
692 else
693 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
694 break;
695 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
696 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
697 return -EINVAL;
698 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
699 /* Can't access odd doubles in FR=0 mode */
700 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
701 return -EINVAL;
702 v = get_fpr64(&fpu->fpr[idx], 0);
703 break;
704 case KVM_REG_MIPS_FCR_IR:
705 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
706 return -EINVAL;
707 v = boot_cpu_data.fpu_id;
708 break;
709 case KVM_REG_MIPS_FCR_CSR:
710 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
711 return -EINVAL;
712 v = fpu->fcr31;
713 break;
714
James Hoganab86bd62014-12-02 15:48:24 +0000715 /* MIPS SIMD Architecture (MSA) registers */
716 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
717 if (!kvm_mips_guest_has_msa(&vcpu->arch))
718 return -EINVAL;
719 /* Can't access MSA registers in FR=0 mode */
720 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
721 return -EINVAL;
722 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
723#ifdef CONFIG_CPU_LITTLE_ENDIAN
724 /* least significant byte first */
725 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
726 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
727#else
728 /* most significant byte first */
729 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
730 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
731#endif
732 break;
733 case KVM_REG_MIPS_MSA_IR:
734 if (!kvm_mips_guest_has_msa(&vcpu->arch))
735 return -EINVAL;
736 v = boot_cpu_data.msa_id;
737 break;
738 case KVM_REG_MIPS_MSA_CSR:
739 if (!kvm_mips_guest_has_msa(&vcpu->arch))
740 return -EINVAL;
741 v = fpu->msacsr;
742 break;
743
James Hogan379245c2014-12-02 15:48:24 +0000744 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700745 case KVM_REG_MIPS_CP0_INDEX:
746 v = (long)kvm_read_c0_guest_index(cop0);
747 break;
748 case KVM_REG_MIPS_CP0_CONTEXT:
749 v = (long)kvm_read_c0_guest_context(cop0);
750 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100751 case KVM_REG_MIPS_CP0_USERLOCAL:
752 v = (long)kvm_read_c0_guest_userlocal(cop0);
753 break;
David Daney4c73fb22013-05-23 09:49:09 -0700754 case KVM_REG_MIPS_CP0_PAGEMASK:
755 v = (long)kvm_read_c0_guest_pagemask(cop0);
756 break;
757 case KVM_REG_MIPS_CP0_WIRED:
758 v = (long)kvm_read_c0_guest_wired(cop0);
759 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100760 case KVM_REG_MIPS_CP0_HWRENA:
761 v = (long)kvm_read_c0_guest_hwrena(cop0);
762 break;
David Daney4c73fb22013-05-23 09:49:09 -0700763 case KVM_REG_MIPS_CP0_BADVADDR:
764 v = (long)kvm_read_c0_guest_badvaddr(cop0);
765 break;
766 case KVM_REG_MIPS_CP0_ENTRYHI:
767 v = (long)kvm_read_c0_guest_entryhi(cop0);
768 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100769 case KVM_REG_MIPS_CP0_COMPARE:
770 v = (long)kvm_read_c0_guest_compare(cop0);
771 break;
David Daney4c73fb22013-05-23 09:49:09 -0700772 case KVM_REG_MIPS_CP0_STATUS:
773 v = (long)kvm_read_c0_guest_status(cop0);
774 break;
775 case KVM_REG_MIPS_CP0_CAUSE:
776 v = (long)kvm_read_c0_guest_cause(cop0);
777 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100778 case KVM_REG_MIPS_CP0_EPC:
779 v = (long)kvm_read_c0_guest_epc(cop0);
780 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100781 case KVM_REG_MIPS_CP0_PRID:
782 v = (long)kvm_read_c0_guest_prid(cop0);
783 break;
David Daney4c73fb22013-05-23 09:49:09 -0700784 case KVM_REG_MIPS_CP0_CONFIG:
785 v = (long)kvm_read_c0_guest_config(cop0);
786 break;
787 case KVM_REG_MIPS_CP0_CONFIG1:
788 v = (long)kvm_read_c0_guest_config1(cop0);
789 break;
790 case KVM_REG_MIPS_CP0_CONFIG2:
791 v = (long)kvm_read_c0_guest_config2(cop0);
792 break;
793 case KVM_REG_MIPS_CP0_CONFIG3:
794 v = (long)kvm_read_c0_guest_config3(cop0);
795 break;
James Hoganc7716072014-06-26 15:11:29 +0100796 case KVM_REG_MIPS_CP0_CONFIG4:
797 v = (long)kvm_read_c0_guest_config4(cop0);
798 break;
799 case KVM_REG_MIPS_CP0_CONFIG5:
800 v = (long)kvm_read_c0_guest_config5(cop0);
801 break;
David Daney4c73fb22013-05-23 09:49:09 -0700802 case KVM_REG_MIPS_CP0_CONFIG7:
803 v = (long)kvm_read_c0_guest_config7(cop0);
804 break;
James Hogane93d4c12014-06-26 13:47:22 +0100805 case KVM_REG_MIPS_CP0_ERROREPC:
806 v = (long)kvm_read_c0_guest_errorepc(cop0);
807 break;
James Hogan05108702016-06-15 19:29:56 +0100808 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
809 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
810 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
811 return -EINVAL;
812 switch (idx) {
813 case 2:
814 v = (long)kvm_read_c0_guest_kscratch1(cop0);
815 break;
816 case 3:
817 v = (long)kvm_read_c0_guest_kscratch2(cop0);
818 break;
819 case 4:
820 v = (long)kvm_read_c0_guest_kscratch3(cop0);
821 break;
822 case 5:
823 v = (long)kvm_read_c0_guest_kscratch4(cop0);
824 break;
825 case 6:
826 v = (long)kvm_read_c0_guest_kscratch5(cop0);
827 break;
828 case 7:
829 v = (long)kvm_read_c0_guest_kscratch6(cop0);
830 break;
831 }
832 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100833 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100834 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100835 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
836 if (ret)
837 return ret;
838 break;
David Daney4c73fb22013-05-23 09:49:09 -0700839 }
David Daney681865d2013-06-10 12:33:48 -0700840 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
841 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700842
David Daney681865d2013-06-10 12:33:48 -0700843 return put_user(v, uaddr64);
844 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
845 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
846 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700847
David Daney681865d2013-06-10 12:33:48 -0700848 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000849 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
850 void __user *uaddr = (void __user *)(long)reg->addr;
851
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200852 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700853 } else {
854 return -EINVAL;
855 }
David Daney4c73fb22013-05-23 09:49:09 -0700856}
857
858static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
859 const struct kvm_one_reg *reg)
860{
David Daney4c73fb22013-05-23 09:49:09 -0700861 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000862 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
863 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000864 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000865 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700866
David Daney681865d2013-06-10 12:33:48 -0700867 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
868 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
869
870 if (get_user(v, uaddr64) != 0)
871 return -EFAULT;
872 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
873 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
874 s32 v32;
875
876 if (get_user(v32, uaddr32) != 0)
877 return -EFAULT;
878 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000879 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
880 void __user *uaddr = (void __user *)(long)reg->addr;
881
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200882 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700883 } else {
884 return -EINVAL;
885 }
David Daney4c73fb22013-05-23 09:49:09 -0700886
887 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000888 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700889 case KVM_REG_MIPS_R0:
890 /* Silently ignore requests to set $0 */
891 break;
892 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
893 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
894 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100895#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700896 case KVM_REG_MIPS_HI:
897 vcpu->arch.hi = v;
898 break;
899 case KVM_REG_MIPS_LO:
900 vcpu->arch.lo = v;
901 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100902#endif
David Daney4c73fb22013-05-23 09:49:09 -0700903 case KVM_REG_MIPS_PC:
904 vcpu->arch.pc = v;
905 break;
906
James Hogan379245c2014-12-02 15:48:24 +0000907 /* Floating point registers */
908 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
909 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
910 return -EINVAL;
911 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
912 /* Odd singles in top of even double when FR=0 */
913 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
914 set_fpr32(&fpu->fpr[idx], 0, v);
915 else
916 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
917 break;
918 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
919 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
920 return -EINVAL;
921 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
922 /* Can't access odd doubles in FR=0 mode */
923 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
924 return -EINVAL;
925 set_fpr64(&fpu->fpr[idx], 0, v);
926 break;
927 case KVM_REG_MIPS_FCR_IR:
928 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
929 return -EINVAL;
930 /* Read-only */
931 break;
932 case KVM_REG_MIPS_FCR_CSR:
933 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
934 return -EINVAL;
935 fpu->fcr31 = v;
936 break;
937
James Hoganab86bd62014-12-02 15:48:24 +0000938 /* MIPS SIMD Architecture (MSA) registers */
939 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
940 if (!kvm_mips_guest_has_msa(&vcpu->arch))
941 return -EINVAL;
942 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
943#ifdef CONFIG_CPU_LITTLE_ENDIAN
944 /* least significant byte first */
945 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
946 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
947#else
948 /* most significant byte first */
949 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
950 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
951#endif
952 break;
953 case KVM_REG_MIPS_MSA_IR:
954 if (!kvm_mips_guest_has_msa(&vcpu->arch))
955 return -EINVAL;
956 /* Read-only */
957 break;
958 case KVM_REG_MIPS_MSA_CSR:
959 if (!kvm_mips_guest_has_msa(&vcpu->arch))
960 return -EINVAL;
961 fpu->msacsr = v;
962 break;
963
James Hogan379245c2014-12-02 15:48:24 +0000964 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700965 case KVM_REG_MIPS_CP0_INDEX:
966 kvm_write_c0_guest_index(cop0, v);
967 break;
968 case KVM_REG_MIPS_CP0_CONTEXT:
969 kvm_write_c0_guest_context(cop0, v);
970 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100971 case KVM_REG_MIPS_CP0_USERLOCAL:
972 kvm_write_c0_guest_userlocal(cop0, v);
973 break;
David Daney4c73fb22013-05-23 09:49:09 -0700974 case KVM_REG_MIPS_CP0_PAGEMASK:
975 kvm_write_c0_guest_pagemask(cop0, v);
976 break;
977 case KVM_REG_MIPS_CP0_WIRED:
978 kvm_write_c0_guest_wired(cop0, v);
979 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100980 case KVM_REG_MIPS_CP0_HWRENA:
981 kvm_write_c0_guest_hwrena(cop0, v);
982 break;
David Daney4c73fb22013-05-23 09:49:09 -0700983 case KVM_REG_MIPS_CP0_BADVADDR:
984 kvm_write_c0_guest_badvaddr(cop0, v);
985 break;
986 case KVM_REG_MIPS_CP0_ENTRYHI:
987 kvm_write_c0_guest_entryhi(cop0, v);
988 break;
989 case KVM_REG_MIPS_CP0_STATUS:
990 kvm_write_c0_guest_status(cop0, v);
991 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100992 case KVM_REG_MIPS_CP0_EPC:
993 kvm_write_c0_guest_epc(cop0, v);
994 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100995 case KVM_REG_MIPS_CP0_PRID:
996 kvm_write_c0_guest_prid(cop0, v);
997 break;
David Daney4c73fb22013-05-23 09:49:09 -0700998 case KVM_REG_MIPS_CP0_ERROREPC:
999 kvm_write_c0_guest_errorepc(cop0, v);
1000 break;
James Hogan05108702016-06-15 19:29:56 +01001001 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
1002 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
1003 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
1004 return -EINVAL;
1005 switch (idx) {
1006 case 2:
1007 kvm_write_c0_guest_kscratch1(cop0, v);
1008 break;
1009 case 3:
1010 kvm_write_c0_guest_kscratch2(cop0, v);
1011 break;
1012 case 4:
1013 kvm_write_c0_guest_kscratch3(cop0, v);
1014 break;
1015 case 5:
1016 kvm_write_c0_guest_kscratch4(cop0, v);
1017 break;
1018 case 6:
1019 kvm_write_c0_guest_kscratch5(cop0, v);
1020 break;
1021 case 7:
1022 kvm_write_c0_guest_kscratch6(cop0, v);
1023 break;
1024 }
1025 break;
James Hoganf8be02d2014-05-29 10:16:29 +01001026 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -07001027 default:
James Hogancc68d222016-06-15 19:29:48 +01001028 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -07001029 }
1030 return 0;
1031}
1032
James Hogan5fafd8742014-12-08 23:07:56 +00001033static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1034 struct kvm_enable_cap *cap)
1035{
1036 int r = 0;
1037
1038 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1039 return -EINVAL;
1040 if (cap->flags)
1041 return -EINVAL;
1042 if (cap->args[0])
1043 return -EINVAL;
1044
1045 switch (cap->cap) {
1046 case KVM_CAP_MIPS_FPU:
1047 vcpu->arch.fpu_enabled = true;
1048 break;
James Hogand952bd02014-12-08 23:07:56 +00001049 case KVM_CAP_MIPS_MSA:
1050 vcpu->arch.msa_enabled = true;
1051 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001052 default:
1053 r = -EINVAL;
1054 break;
1055 }
1056
1057 return r;
1058}
1059
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001060long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1061 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -08001062{
1063 struct kvm_vcpu *vcpu = filp->private_data;
1064 void __user *argp = (void __user *)arg;
1065 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001066
1067 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -07001068 case KVM_SET_ONE_REG:
1069 case KVM_GET_ONE_REG: {
1070 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001071
David Daney4c73fb22013-05-23 09:49:09 -07001072 if (copy_from_user(&reg, argp, sizeof(reg)))
1073 return -EFAULT;
1074 if (ioctl == KVM_SET_ONE_REG)
1075 return kvm_mips_set_reg(vcpu, &reg);
1076 else
1077 return kvm_mips_get_reg(vcpu, &reg);
1078 }
1079 case KVM_GET_REG_LIST: {
1080 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -07001081 struct kvm_reg_list reg_list;
1082 unsigned n;
1083
1084 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1085 return -EFAULT;
1086 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +01001087 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -07001088 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1089 return -EFAULT;
1090 if (n < reg_list.n)
1091 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +01001092 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -07001093 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001094 case KVM_INTERRUPT:
1095 {
1096 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001097
Sanjay Lal669e8462012-11-21 18:34:02 -08001098 if (copy_from_user(&irq, argp, sizeof(irq)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +01001099 return -EFAULT;
Sanjay Lal669e8462012-11-21 18:34:02 -08001100 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1101 irq.irq);
1102
1103 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1104 break;
1105 }
James Hogan5fafd8742014-12-08 23:07:56 +00001106 case KVM_ENABLE_CAP: {
1107 struct kvm_enable_cap cap;
1108
James Hogan5fafd8742014-12-08 23:07:56 +00001109 if (copy_from_user(&cap, argp, sizeof(cap)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +01001110 return -EFAULT;
James Hogan5fafd8742014-12-08 23:07:56 +00001111 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1112 break;
1113 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001114 default:
David Daney4c73fb22013-05-23 09:49:09 -07001115 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001116 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001117 return r;
1118}
1119
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001120/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001121int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1122{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001123 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001124 struct kvm_memory_slot *memslot;
1125 unsigned long ga, ga_end;
1126 int is_dirty = 0;
1127 int r;
1128 unsigned long n;
1129
1130 mutex_lock(&kvm->slots_lock);
1131
1132 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1133 if (r)
1134 goto out;
1135
1136 /* If nothing is dirty, don't bother messing with page tables. */
1137 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001138 slots = kvm_memslots(kvm);
1139 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001140
1141 ga = memslot->base_gfn << PAGE_SHIFT;
1142 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1143
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001144 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
1145 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -08001146
1147 n = kvm_dirty_bitmap_bytes(memslot);
1148 memset(memslot->dirty_bitmap, 0, n);
1149 }
1150
1151 r = 0;
1152out:
1153 mutex_unlock(&kvm->slots_lock);
1154 return r;
1155
1156}
1157
1158long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1159{
1160 long r;
1161
1162 switch (ioctl) {
1163 default:
David Daneyed829852013-05-23 09:49:10 -07001164 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001165 }
1166
1167 return r;
1168}
1169
1170int kvm_arch_init(void *opaque)
1171{
Sanjay Lal669e8462012-11-21 18:34:02 -08001172 if (kvm_mips_callbacks) {
1173 kvm_err("kvm: module already exists\n");
1174 return -EEXIST;
1175 }
1176
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001177 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001178}
1179
1180void kvm_arch_exit(void)
1181{
1182 kvm_mips_callbacks = NULL;
1183}
1184
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001185int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1186 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001187{
David Daneyed829852013-05-23 09:49:10 -07001188 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001189}
1190
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001191int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1192 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001193{
David Daneyed829852013-05-23 09:49:10 -07001194 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001195}
1196
Dominik Dingel31928aa2014-12-04 15:47:07 +01001197void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001198{
Sanjay Lal669e8462012-11-21 18:34:02 -08001199}
1200
1201int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1202{
David Daneyed829852013-05-23 09:49:10 -07001203 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001204}
1205
1206int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1207{
David Daneyed829852013-05-23 09:49:10 -07001208 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001209}
1210
1211int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1212{
1213 return VM_FAULT_SIGBUS;
1214}
1215
Alexander Graf784aa3d2014-07-14 18:27:35 +02001216int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001217{
1218 int r;
1219
1220 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001221 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001222 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001223 r = 1;
1224 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001225 case KVM_CAP_COALESCED_MMIO:
1226 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1227 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001228 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001229 /* We don't handle systems with inconsistent cpu_has_fpu */
1230 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001231 break;
James Hogand952bd02014-12-08 23:07:56 +00001232 case KVM_CAP_MIPS_MSA:
1233 /*
1234 * We don't support MSA vector partitioning yet:
1235 * 1) It would require explicit support which can't be tested
1236 * yet due to lack of support in current hardware.
1237 * 2) It extends the state that would need to be saved/restored
1238 * by e.g. QEMU for migration.
1239 *
1240 * When vector partitioning hardware becomes available, support
1241 * could be added by requiring a flag when enabling
1242 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1243 * to save/restore the appropriate extra state.
1244 */
1245 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1246 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001247 default:
1248 r = 0;
1249 break;
1250 }
1251 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001252}
1253
1254int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1255{
1256 return kvm_mips_pending_timer(vcpu);
1257}
1258
1259int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1260{
1261 int i;
1262 struct mips_coproc *cop0;
1263
1264 if (!vcpu)
1265 return -1;
1266
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001267 kvm_debug("VCPU Register Dump:\n");
1268 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1269 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001270
1271 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001272 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001273 vcpu->arch.gprs[i],
1274 vcpu->arch.gprs[i + 1],
1275 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1276 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001277 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1278 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001279
1280 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001281 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1282 kvm_read_c0_guest_status(cop0),
1283 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001284
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001285 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001286
1287 return 0;
1288}
1289
1290int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1291{
1292 int i;
1293
David Daney8d17dd02013-05-23 09:49:08 -07001294 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001295 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001296 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001297 vcpu->arch.hi = regs->hi;
1298 vcpu->arch.lo = regs->lo;
1299 vcpu->arch.pc = regs->pc;
1300
David Daney4c73fb22013-05-23 09:49:09 -07001301 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001302}
1303
1304int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1305{
1306 int i;
1307
David Daney8d17dd02013-05-23 09:49:08 -07001308 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001309 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001310
1311 regs->hi = vcpu->arch.hi;
1312 regs->lo = vcpu->arch.lo;
1313 regs->pc = vcpu->arch.pc;
1314
David Daney4c73fb22013-05-23 09:49:09 -07001315 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001316}
1317
James Hogan0fae34f2014-05-29 10:16:39 +01001318static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001319{
1320 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1321
1322 kvm_mips_callbacks->queue_timer_int(vcpu);
1323
1324 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001325 if (swait_active(&vcpu->wq))
1326 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001327}
1328
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001329/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001330static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001331{
1332 struct kvm_vcpu *vcpu;
1333
1334 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1335 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001336 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001337}
1338
1339int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1340{
1341 kvm_mips_callbacks->vcpu_init(vcpu);
1342 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1343 HRTIMER_MODE_REL);
1344 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001345 return 0;
1346}
1347
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001348int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1349 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001350{
1351 return 0;
1352}
1353
1354/* Initial guest state */
1355int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1356{
1357 return kvm_mips_callbacks->vcpu_setup(vcpu);
1358}
1359
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001360static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001361{
James Hogan8cffd192016-06-09 14:19:08 +01001362 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001363
Sanjay Lal669e8462012-11-21 18:34:02 -08001364 if (cpu_has_dsp)
1365 status |= (ST0_MX);
1366
1367 write_c0_status(status);
1368 ehb();
1369}
1370
1371/*
1372 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1373 */
1374int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1375{
James Hogan8cffd192016-06-09 14:19:08 +01001376 u32 cause = vcpu->arch.host_cp0_cause;
1377 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1378 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001379 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1380 enum emulation_result er = EMULATE_DONE;
1381 int ret = RESUME_GUEST;
1382
James Hoganc4c6f2c2015-02-04 10:52:03 +00001383 /* re-enable HTW before enabling interrupts */
1384 htw_start();
1385
Sanjay Lal669e8462012-11-21 18:34:02 -08001386 /* Set a default exit reason */
1387 run->exit_reason = KVM_EXIT_UNKNOWN;
1388 run->ready_for_interrupt_injection = 1;
1389
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001390 /*
1391 * Set the appropriate status bits based on host CPU features,
1392 * before we hit the scheduler
1393 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001394 kvm_mips_set_c0_status();
1395
1396 local_irq_enable();
1397
1398 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1399 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001400 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001401
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001402 /*
1403 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001404 * causing an exception to be delivered to the Guest Kernel
1405 */
1406 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1407 if (er == EMULATE_PRIV_FAIL) {
1408 goto skip_emul;
1409 } else if (er == EMULATE_FAIL) {
1410 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1411 ret = RESUME_HOST;
1412 goto skip_emul;
1413 }
1414
1415 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001416 case EXCCODE_INT:
1417 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001418
1419 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001420
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001421 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001422 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001423
1424 ret = RESUME_GUEST;
1425 break;
1426
James Hogan16d100db2015-12-16 23:49:33 +00001427 case EXCCODE_CPU:
1428 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001429
1430 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001431 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1432 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001433 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001434 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001435 break;
1436
James Hogan16d100db2015-12-16 23:49:33 +00001437 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001438 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001439 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1440 break;
1441
James Hogan16d100db2015-12-16 23:49:33 +00001442 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001443 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1444 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1445 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001446
1447 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001448 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1449 break;
1450
James Hogan16d100db2015-12-16 23:49:33 +00001451 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001452 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1453 cause, opc, badvaddr);
1454
1455 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001456 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1457 break;
1458
James Hogan16d100db2015-12-16 23:49:33 +00001459 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001460 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001461 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1462 break;
1463
James Hogan16d100db2015-12-16 23:49:33 +00001464 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001465 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001466 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1467 break;
1468
James Hogan16d100db2015-12-16 23:49:33 +00001469 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001470 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001471 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1472 break;
1473
James Hogan16d100db2015-12-16 23:49:33 +00001474 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001475 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001476 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1477 break;
1478
James Hogan16d100db2015-12-16 23:49:33 +00001479 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001480 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001481 ret = kvm_mips_callbacks->handle_break(vcpu);
1482 break;
1483
James Hogan16d100db2015-12-16 23:49:33 +00001484 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001485 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001486 ret = kvm_mips_callbacks->handle_trap(vcpu);
1487 break;
1488
James Hogan16d100db2015-12-16 23:49:33 +00001489 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001490 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001491 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1492 break;
1493
James Hogan16d100db2015-12-16 23:49:33 +00001494 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001495 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001496 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1497 break;
1498
James Hogan16d100db2015-12-16 23:49:33 +00001499 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001500 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001501 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1502 break;
1503
Sanjay Lal669e8462012-11-21 18:34:02 -08001504 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001505 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1506 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1507 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001508 kvm_arch_vcpu_dump_regs(vcpu);
1509 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1510 ret = RESUME_HOST;
1511 break;
1512
1513 }
1514
1515skip_emul:
1516 local_irq_disable();
1517
1518 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1519 kvm_mips_deliver_interrupts(vcpu, cause);
1520
1521 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001522 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001523 if (signal_pending(current)) {
1524 run->exit_reason = KVM_EXIT_INTR;
1525 ret = (-EINTR << 2) | RESUME_HOST;
1526 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001527 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001528 }
1529 }
1530
James Hogan98e91b82014-11-18 14:09:12 +00001531 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001532 trace_kvm_reenter(vcpu);
1533
James Hogana2c046e2016-11-18 13:14:37 +00001534 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +01001535
James Hogan98e91b82014-11-18 14:09:12 +00001536 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001537 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1538 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001539 *
1540 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001541 * vector, as it may well cause an [MSA] FP exception if there
1542 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001543 * kvm_mips_csr_die_notifier() for how that is handled).
1544 */
1545 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1546 read_c0_status() & ST0_CU1)
1547 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001548
1549 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1550 read_c0_config5() & MIPS_CONF5_MSAEN)
1551 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001552 }
1553
James Hoganc4c6f2c2015-02-04 10:52:03 +00001554 /* Disable HTW before returning to guest or host */
1555 htw_stop();
1556
Sanjay Lal669e8462012-11-21 18:34:02 -08001557 return ret;
1558}
1559
James Hogan98e91b82014-11-18 14:09:12 +00001560/* Enable FPU for guest and restore context */
1561void kvm_own_fpu(struct kvm_vcpu *vcpu)
1562{
1563 struct mips_coproc *cop0 = vcpu->arch.cop0;
1564 unsigned int sr, cfg5;
1565
1566 preempt_disable();
1567
James Hogan539cb89fb2015-03-05 11:43:36 +00001568 sr = kvm_read_c0_guest_status(cop0);
1569
1570 /*
1571 * If MSA state is already live, it is undefined how it interacts with
1572 * FR=0 FPU state, and we don't want to hit reserved instruction
1573 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1574 * play it safe and save it first.
1575 *
1576 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1577 * get called when guest CU1 is set, however we can't trust the guest
1578 * not to clobber the status register directly via the commpage.
1579 */
1580 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001581 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001582 kvm_lose_fpu(vcpu);
1583
James Hogan98e91b82014-11-18 14:09:12 +00001584 /*
1585 * Enable FPU for guest
1586 * We set FR and FRE according to guest context
1587 */
James Hogan98e91b82014-11-18 14:09:12 +00001588 change_c0_status(ST0_CU1 | ST0_FR, sr);
1589 if (cpu_has_fre) {
1590 cfg5 = kvm_read_c0_guest_config5(cop0);
1591 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1592 }
1593 enable_fpu_hazard();
1594
1595 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001596 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001597 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001598 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001599 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1600 } else {
1601 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001602 }
1603
1604 preempt_enable();
1605}
1606
James Hogan539cb89fb2015-03-05 11:43:36 +00001607#ifdef CONFIG_CPU_HAS_MSA
1608/* Enable MSA for guest and restore context */
1609void kvm_own_msa(struct kvm_vcpu *vcpu)
1610{
1611 struct mips_coproc *cop0 = vcpu->arch.cop0;
1612 unsigned int sr, cfg5;
1613
1614 preempt_disable();
1615
1616 /*
1617 * Enable FPU if enabled in guest, since we're restoring FPU context
1618 * anyway. We set FR and FRE according to guest context.
1619 */
1620 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1621 sr = kvm_read_c0_guest_status(cop0);
1622
1623 /*
1624 * If FR=0 FPU state is already live, it is undefined how it
1625 * interacts with MSA state, so play it safe and save it first.
1626 */
1627 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001628 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1629 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001630 kvm_lose_fpu(vcpu);
1631
1632 change_c0_status(ST0_CU1 | ST0_FR, sr);
1633 if (sr & ST0_CU1 && cpu_has_fre) {
1634 cfg5 = kvm_read_c0_guest_config5(cop0);
1635 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1636 }
1637 }
1638
1639 /* Enable MSA for guest */
1640 set_c0_config5(MIPS_CONF5_MSAEN);
1641 enable_fpu_hazard();
1642
James Hoganf9431762016-06-14 09:40:10 +01001643 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1644 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001645 /*
1646 * Guest FPU state already loaded, only restore upper MSA state
1647 */
1648 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001649 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001650 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001651 break;
1652 case 0:
1653 /* Neither FPU or MSA already active, restore full MSA state */
1654 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001655 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001656 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001657 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001658 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1659 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001660 break;
1661 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001662 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001663 break;
1664 }
1665
1666 preempt_enable();
1667}
1668#endif
1669
1670/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001671void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1672{
1673 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001674 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001675 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001676 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001677 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001678 }
James Hoganf9431762016-06-14 09:40:10 +01001679 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001680 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001681 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001682 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001683 }
1684 preempt_enable();
1685}
1686
James Hogan539cb89fb2015-03-05 11:43:36 +00001687/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001688void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1689{
1690 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001691 * FPU & MSA get disabled in root context (hardware) when it is disabled
1692 * in guest context (software), but the register state in the hardware
1693 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001694 * before saving.
1695 */
1696
1697 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001698 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001699 set_c0_config5(MIPS_CONF5_MSAEN);
1700 enable_fpu_hazard();
1701
1702 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001703 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001704
1705 /* Disable MSA & FPU */
1706 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001707 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001708 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001709 disable_fpu_hazard();
1710 }
James Hoganf9431762016-06-14 09:40:10 +01001711 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1712 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001713 set_c0_status(ST0_CU1);
1714 enable_fpu_hazard();
1715
1716 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001717 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001718 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001719
1720 /* Disable FPU */
1721 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001722 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001723 }
1724 preempt_enable();
1725}
1726
1727/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001728 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1729 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1730 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001731 */
1732static int kvm_mips_csr_die_notify(struct notifier_block *self,
1733 unsigned long cmd, void *ptr)
1734{
1735 struct die_args *args = (struct die_args *)ptr;
1736 struct pt_regs *regs = args->regs;
1737 unsigned long pc;
1738
James Hogan539cb89fb2015-03-05 11:43:36 +00001739 /* Only interested in FPE and MSAFPE */
1740 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001741 return NOTIFY_DONE;
1742
1743 /* Return immediately if guest context isn't active */
1744 if (!(current->flags & PF_VCPU))
1745 return NOTIFY_DONE;
1746
1747 /* Should never get here from user mode */
1748 BUG_ON(user_mode(regs));
1749
1750 pc = instruction_pointer(regs);
1751 switch (cmd) {
1752 case DIE_FP:
1753 /* match 2nd instruction in __kvm_restore_fcsr */
1754 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1755 return NOTIFY_DONE;
1756 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001757 case DIE_MSAFP:
1758 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1759 if (!cpu_has_msa ||
1760 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1761 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1762 return NOTIFY_DONE;
1763 break;
James Hogan98e91b82014-11-18 14:09:12 +00001764 }
1765
1766 /* Move PC forward a little and continue executing */
1767 instruction_pointer(regs) += 4;
1768
1769 return NOTIFY_STOP;
1770}
1771
1772static struct notifier_block kvm_mips_csr_die_notifier = {
1773 .notifier_call = kvm_mips_csr_die_notify,
1774};
1775
James Hogan2db9d232015-12-16 23:49:32 +00001776static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001777{
1778 int ret;
1779
James Hogan1e5217f52016-06-23 17:34:45 +01001780 ret = kvm_mips_entry_setup();
1781 if (ret)
1782 return ret;
1783
Sanjay Lal669e8462012-11-21 18:34:02 -08001784 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1785
1786 if (ret)
1787 return ret;
1788
James Hogan98e91b82014-11-18 14:09:12 +00001789 register_die_notifier(&kvm_mips_csr_die_notifier);
1790
Sanjay Lal669e8462012-11-21 18:34:02 -08001791 return 0;
1792}
1793
James Hogan2db9d232015-12-16 23:49:32 +00001794static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001795{
1796 kvm_exit();
1797
James Hogan98e91b82014-11-18 14:09:12 +00001798 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001799}
1800
1801module_init(kvm_mips_init);
1802module_exit(kvm_mips_exit);
1803
1804EXPORT_TRACEPOINT_SYMBOL(kvm_exit);