blob: a687864de428a37a3d44d98f257b612fa92cb39e [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 Hogana7cfa7a2016-09-10 23:56:46 +0100267 void *gebase, *p, *handler, *refill_start, *refill_end;
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
James Hogana7cfa7a2016-09-10 23:56:46 +0100320 /* TLB refill */
321 refill_start = gebase;
322 refill_end = kvm_mips_build_tlb_refill_exception(refill_start, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800323
324 /* General Exception Entry point */
James Hogan1f9ca622016-06-23 17:34:46 +0100325 kvm_mips_build_exception(gebase + 0x180, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800326
327 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
328 for (i = 0; i < 8; i++) {
329 kvm_debug("L1 Vectored handler @ %p\n",
330 gebase + 0x200 + (i * VECTORSPACING));
James Hogan1f9ca622016-06-23 17:34:46 +0100331 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
332 handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800333 }
334
James Hogan90e93112016-06-23 17:34:39 +0100335 /* General exit handler */
James Hogan1f9ca622016-06-23 17:34:46 +0100336 p = handler;
James Hogan90e93112016-06-23 17:34:39 +0100337 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800338
James Hogan90e93112016-06-23 17:34:39 +0100339 /* Guest entry routine */
340 vcpu->arch.vcpu_run = p;
341 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100342
James Hogand7b8f892016-06-23 17:34:40 +0100343 /* Dump the generated code */
344 pr_debug("#include <asm/asm.h>\n");
345 pr_debug("#include <asm/regdef.h>\n");
346 pr_debug("\n");
347 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
James Hogana7cfa7a2016-09-10 23:56:46 +0100348 dump_handler("kvm_tlb_refill", refill_start, refill_end);
James Hogand7b8f892016-06-23 17:34:40 +0100349 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
350 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
351
Sanjay Lal669e8462012-11-21 18:34:02 -0800352 /* Invalidate the icache for these ranges */
James Hogan32eb12a2017-01-03 17:43:01 +0000353 flush_icache_range((unsigned long)gebase,
354 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800355
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700356 /*
357 * Allocate comm page for guest kernel, a TLB will be reserved for
358 * mapping GVA @ 0xFFFF8000 to this page
359 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800360 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
361
362 if (!vcpu->arch.kseg0_commpage) {
363 err = -ENOMEM;
364 goto out_free_gebase;
365 }
366
James Hogan6e95bfd2014-05-29 10:16:43 +0100367 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800368 kvm_mips_commpage_init(vcpu);
369
370 /* Init */
371 vcpu->arch.last_sched_cpu = -1;
372
373 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100374 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800375
376 return vcpu;
377
378out_free_gebase:
379 kfree(gebase);
380
James Hogan585bb8f2015-11-11 14:21:20 +0000381out_uninit_cpu:
382 kvm_vcpu_uninit(vcpu);
383
Sanjay Lal669e8462012-11-21 18:34:02 -0800384out_free_cpu:
385 kfree(vcpu);
386
387out:
388 return ERR_PTR(err);
389}
390
391void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
392{
393 hrtimer_cancel(&vcpu->arch.comparecount_timer);
394
395 kvm_vcpu_uninit(vcpu);
396
397 kvm_mips_dump_stats(vcpu);
398
James Hoganc6c0a662014-05-29 10:16:44 +0100399 kfree(vcpu->arch.guest_ebase);
400 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700401 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800402}
403
404void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
405{
406 kvm_arch_vcpu_free(vcpu);
407}
408
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700409int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
410 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800411{
David Daneyed829852013-05-23 09:49:10 -0700412 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800413}
414
415int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
416{
417 int r = 0;
418 sigset_t sigsaved;
419
420 if (vcpu->sigset_active)
421 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
422
423 if (vcpu->mmio_needed) {
424 if (!vcpu->mmio_is_write)
425 kvm_mips_complete_mmio_load(vcpu, run);
426 vcpu->mmio_needed = 0;
427 }
428
James Hoganf7982172015-02-04 17:06:37 +0000429 lose_fpu(1);
430
James Hogan044f0f02014-05-29 10:16:32 +0100431 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200432 guest_enter_irqoff();
James Hogan93258602016-06-14 09:40:14 +0100433 trace_kvm_enter(vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100434
James Hogana2c046e2016-11-18 13:14:37 +0000435 r = kvm_mips_callbacks->vcpu_run(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100436
James Hogan93258602016-06-14 09:40:14 +0100437 trace_kvm_out(vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200438 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800439 local_irq_enable();
440
441 if (vcpu->sigset_active)
442 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
443
444 return r;
445}
446
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700447int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
448 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800449{
450 int intr = (int)irq->irq;
451 struct kvm_vcpu *dvcpu = NULL;
452
453 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
454 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
455 (int)intr);
456
457 if (irq->cpu == -1)
458 dvcpu = vcpu;
459 else
460 dvcpu = vcpu->kvm->vcpus[irq->cpu];
461
462 if (intr == 2 || intr == 3 || intr == 4) {
463 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
464
465 } else if (intr == -2 || intr == -3 || intr == -4) {
466 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
467 } else {
468 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
469 irq->cpu, irq->irq);
470 return -EINVAL;
471 }
472
473 dvcpu->arch.wait = 0;
474
Marcelo Tosatti85773702016-02-19 09:46:39 +0100475 if (swait_active(&dvcpu->wq))
476 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800477
478 return 0;
479}
480
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700481int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
482 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800483{
David Daneyed829852013-05-23 09:49:10 -0700484 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800485}
486
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700487int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
488 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800489{
David Daneyed829852013-05-23 09:49:10 -0700490 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800491}
492
David Daney4c73fb22013-05-23 09:49:09 -0700493static u64 kvm_mips_get_one_regs[] = {
494 KVM_REG_MIPS_R0,
495 KVM_REG_MIPS_R1,
496 KVM_REG_MIPS_R2,
497 KVM_REG_MIPS_R3,
498 KVM_REG_MIPS_R4,
499 KVM_REG_MIPS_R5,
500 KVM_REG_MIPS_R6,
501 KVM_REG_MIPS_R7,
502 KVM_REG_MIPS_R8,
503 KVM_REG_MIPS_R9,
504 KVM_REG_MIPS_R10,
505 KVM_REG_MIPS_R11,
506 KVM_REG_MIPS_R12,
507 KVM_REG_MIPS_R13,
508 KVM_REG_MIPS_R14,
509 KVM_REG_MIPS_R15,
510 KVM_REG_MIPS_R16,
511 KVM_REG_MIPS_R17,
512 KVM_REG_MIPS_R18,
513 KVM_REG_MIPS_R19,
514 KVM_REG_MIPS_R20,
515 KVM_REG_MIPS_R21,
516 KVM_REG_MIPS_R22,
517 KVM_REG_MIPS_R23,
518 KVM_REG_MIPS_R24,
519 KVM_REG_MIPS_R25,
520 KVM_REG_MIPS_R26,
521 KVM_REG_MIPS_R27,
522 KVM_REG_MIPS_R28,
523 KVM_REG_MIPS_R29,
524 KVM_REG_MIPS_R30,
525 KVM_REG_MIPS_R31,
526
James Hogan70e92c7e2016-07-04 19:35:11 +0100527#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700528 KVM_REG_MIPS_HI,
529 KVM_REG_MIPS_LO,
James Hogan70e92c7e2016-07-04 19:35:11 +0100530#endif
David Daney4c73fb22013-05-23 09:49:09 -0700531 KVM_REG_MIPS_PC,
532
533 KVM_REG_MIPS_CP0_INDEX,
534 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100535 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700536 KVM_REG_MIPS_CP0_PAGEMASK,
537 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100538 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700539 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100540 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700541 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100542 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700543 KVM_REG_MIPS_CP0_STATUS,
544 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100545 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100546 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700547 KVM_REG_MIPS_CP0_CONFIG,
548 KVM_REG_MIPS_CP0_CONFIG1,
549 KVM_REG_MIPS_CP0_CONFIG2,
550 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100551 KVM_REG_MIPS_CP0_CONFIG4,
552 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700553 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100554 KVM_REG_MIPS_CP0_ERROREPC,
555
556 KVM_REG_MIPS_COUNT_CTL,
557 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100558 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700559};
560
James Hogane5775932016-06-15 19:29:51 +0100561static u64 kvm_mips_get_one_regs_fpu[] = {
562 KVM_REG_MIPS_FCR_IR,
563 KVM_REG_MIPS_FCR_CSR,
564};
565
566static u64 kvm_mips_get_one_regs_msa[] = {
567 KVM_REG_MIPS_MSA_IR,
568 KVM_REG_MIPS_MSA_CSR,
569};
570
James Hogan05108702016-06-15 19:29:56 +0100571static u64 kvm_mips_get_one_regs_kscratch[] = {
572 KVM_REG_MIPS_CP0_KSCRATCH1,
573 KVM_REG_MIPS_CP0_KSCRATCH2,
574 KVM_REG_MIPS_CP0_KSCRATCH3,
575 KVM_REG_MIPS_CP0_KSCRATCH4,
576 KVM_REG_MIPS_CP0_KSCRATCH5,
577 KVM_REG_MIPS_CP0_KSCRATCH6,
578};
579
James Hoganf5c43bd2016-06-15 19:29:49 +0100580static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
581{
582 unsigned long ret;
583
584 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100585 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
586 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
587 /* odd doubles */
588 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
589 ret += 16;
590 }
591 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
592 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hogan05108702016-06-15 19:29:56 +0100593 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
James Hoganf5c43bd2016-06-15 19:29:49 +0100594 ret += kvm_mips_callbacks->num_regs(vcpu);
595
596 return ret;
597}
598
599static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
600{
James Hogane5775932016-06-15 19:29:51 +0100601 u64 index;
602 unsigned int i;
603
James Hoganf5c43bd2016-06-15 19:29:49 +0100604 if (copy_to_user(indices, kvm_mips_get_one_regs,
605 sizeof(kvm_mips_get_one_regs)))
606 return -EFAULT;
607 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
608
James Hogane5775932016-06-15 19:29:51 +0100609 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
610 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
611 sizeof(kvm_mips_get_one_regs_fpu)))
612 return -EFAULT;
613 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
614
615 for (i = 0; i < 32; ++i) {
616 index = KVM_REG_MIPS_FPR_32(i);
617 if (copy_to_user(indices, &index, sizeof(index)))
618 return -EFAULT;
619 ++indices;
620
621 /* skip odd doubles if no F64 */
622 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
623 continue;
624
625 index = KVM_REG_MIPS_FPR_64(i);
626 if (copy_to_user(indices, &index, sizeof(index)))
627 return -EFAULT;
628 ++indices;
629 }
630 }
631
632 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
633 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
634 sizeof(kvm_mips_get_one_regs_msa)))
635 return -EFAULT;
636 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
637
638 for (i = 0; i < 32; ++i) {
639 index = KVM_REG_MIPS_VEC_128(i);
640 if (copy_to_user(indices, &index, sizeof(index)))
641 return -EFAULT;
642 ++indices;
643 }
644 }
645
James Hogan05108702016-06-15 19:29:56 +0100646 for (i = 0; i < 6; ++i) {
647 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
648 continue;
649
650 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
651 sizeof(kvm_mips_get_one_regs_kscratch[i])))
652 return -EFAULT;
653 ++indices;
654 }
655
James Hoganf5c43bd2016-06-15 19:29:49 +0100656 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
657}
658
David Daney4c73fb22013-05-23 09:49:09 -0700659static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
660 const struct kvm_one_reg *reg)
661{
David Daney4c73fb22013-05-23 09:49:09 -0700662 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000663 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100664 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700665 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000666 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000667 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700668
669 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000670 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700671 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
672 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
673 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100674#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700675 case KVM_REG_MIPS_HI:
676 v = (long)vcpu->arch.hi;
677 break;
678 case KVM_REG_MIPS_LO:
679 v = (long)vcpu->arch.lo;
680 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100681#endif
David Daney4c73fb22013-05-23 09:49:09 -0700682 case KVM_REG_MIPS_PC:
683 v = (long)vcpu->arch.pc;
684 break;
685
James Hogan379245c2014-12-02 15:48:24 +0000686 /* Floating point registers */
687 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
688 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
689 return -EINVAL;
690 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
691 /* Odd singles in top of even double when FR=0 */
692 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
693 v = get_fpr32(&fpu->fpr[idx], 0);
694 else
695 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
696 break;
697 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
698 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
699 return -EINVAL;
700 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
701 /* Can't access odd doubles in FR=0 mode */
702 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
703 return -EINVAL;
704 v = get_fpr64(&fpu->fpr[idx], 0);
705 break;
706 case KVM_REG_MIPS_FCR_IR:
707 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
708 return -EINVAL;
709 v = boot_cpu_data.fpu_id;
710 break;
711 case KVM_REG_MIPS_FCR_CSR:
712 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
713 return -EINVAL;
714 v = fpu->fcr31;
715 break;
716
James Hoganab86bd62014-12-02 15:48:24 +0000717 /* MIPS SIMD Architecture (MSA) registers */
718 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
719 if (!kvm_mips_guest_has_msa(&vcpu->arch))
720 return -EINVAL;
721 /* Can't access MSA registers in FR=0 mode */
722 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
723 return -EINVAL;
724 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
725#ifdef CONFIG_CPU_LITTLE_ENDIAN
726 /* least significant byte first */
727 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
728 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
729#else
730 /* most significant byte first */
731 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
732 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
733#endif
734 break;
735 case KVM_REG_MIPS_MSA_IR:
736 if (!kvm_mips_guest_has_msa(&vcpu->arch))
737 return -EINVAL;
738 v = boot_cpu_data.msa_id;
739 break;
740 case KVM_REG_MIPS_MSA_CSR:
741 if (!kvm_mips_guest_has_msa(&vcpu->arch))
742 return -EINVAL;
743 v = fpu->msacsr;
744 break;
745
James Hogan379245c2014-12-02 15:48:24 +0000746 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700747 case KVM_REG_MIPS_CP0_INDEX:
748 v = (long)kvm_read_c0_guest_index(cop0);
749 break;
750 case KVM_REG_MIPS_CP0_CONTEXT:
751 v = (long)kvm_read_c0_guest_context(cop0);
752 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100753 case KVM_REG_MIPS_CP0_USERLOCAL:
754 v = (long)kvm_read_c0_guest_userlocal(cop0);
755 break;
David Daney4c73fb22013-05-23 09:49:09 -0700756 case KVM_REG_MIPS_CP0_PAGEMASK:
757 v = (long)kvm_read_c0_guest_pagemask(cop0);
758 break;
759 case KVM_REG_MIPS_CP0_WIRED:
760 v = (long)kvm_read_c0_guest_wired(cop0);
761 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100762 case KVM_REG_MIPS_CP0_HWRENA:
763 v = (long)kvm_read_c0_guest_hwrena(cop0);
764 break;
David Daney4c73fb22013-05-23 09:49:09 -0700765 case KVM_REG_MIPS_CP0_BADVADDR:
766 v = (long)kvm_read_c0_guest_badvaddr(cop0);
767 break;
768 case KVM_REG_MIPS_CP0_ENTRYHI:
769 v = (long)kvm_read_c0_guest_entryhi(cop0);
770 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100771 case KVM_REG_MIPS_CP0_COMPARE:
772 v = (long)kvm_read_c0_guest_compare(cop0);
773 break;
David Daney4c73fb22013-05-23 09:49:09 -0700774 case KVM_REG_MIPS_CP0_STATUS:
775 v = (long)kvm_read_c0_guest_status(cop0);
776 break;
777 case KVM_REG_MIPS_CP0_CAUSE:
778 v = (long)kvm_read_c0_guest_cause(cop0);
779 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100780 case KVM_REG_MIPS_CP0_EPC:
781 v = (long)kvm_read_c0_guest_epc(cop0);
782 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100783 case KVM_REG_MIPS_CP0_PRID:
784 v = (long)kvm_read_c0_guest_prid(cop0);
785 break;
David Daney4c73fb22013-05-23 09:49:09 -0700786 case KVM_REG_MIPS_CP0_CONFIG:
787 v = (long)kvm_read_c0_guest_config(cop0);
788 break;
789 case KVM_REG_MIPS_CP0_CONFIG1:
790 v = (long)kvm_read_c0_guest_config1(cop0);
791 break;
792 case KVM_REG_MIPS_CP0_CONFIG2:
793 v = (long)kvm_read_c0_guest_config2(cop0);
794 break;
795 case KVM_REG_MIPS_CP0_CONFIG3:
796 v = (long)kvm_read_c0_guest_config3(cop0);
797 break;
James Hoganc7716072014-06-26 15:11:29 +0100798 case KVM_REG_MIPS_CP0_CONFIG4:
799 v = (long)kvm_read_c0_guest_config4(cop0);
800 break;
801 case KVM_REG_MIPS_CP0_CONFIG5:
802 v = (long)kvm_read_c0_guest_config5(cop0);
803 break;
David Daney4c73fb22013-05-23 09:49:09 -0700804 case KVM_REG_MIPS_CP0_CONFIG7:
805 v = (long)kvm_read_c0_guest_config7(cop0);
806 break;
James Hogane93d4c12014-06-26 13:47:22 +0100807 case KVM_REG_MIPS_CP0_ERROREPC:
808 v = (long)kvm_read_c0_guest_errorepc(cop0);
809 break;
James Hogan05108702016-06-15 19:29:56 +0100810 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
811 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
812 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
813 return -EINVAL;
814 switch (idx) {
815 case 2:
816 v = (long)kvm_read_c0_guest_kscratch1(cop0);
817 break;
818 case 3:
819 v = (long)kvm_read_c0_guest_kscratch2(cop0);
820 break;
821 case 4:
822 v = (long)kvm_read_c0_guest_kscratch3(cop0);
823 break;
824 case 5:
825 v = (long)kvm_read_c0_guest_kscratch4(cop0);
826 break;
827 case 6:
828 v = (long)kvm_read_c0_guest_kscratch5(cop0);
829 break;
830 case 7:
831 v = (long)kvm_read_c0_guest_kscratch6(cop0);
832 break;
833 }
834 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100835 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100836 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100837 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
838 if (ret)
839 return ret;
840 break;
David Daney4c73fb22013-05-23 09:49:09 -0700841 }
David Daney681865d2013-06-10 12:33:48 -0700842 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
843 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700844
David Daney681865d2013-06-10 12:33:48 -0700845 return put_user(v, uaddr64);
846 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
847 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
848 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700849
David Daney681865d2013-06-10 12:33:48 -0700850 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000851 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
852 void __user *uaddr = (void __user *)(long)reg->addr;
853
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200854 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700855 } else {
856 return -EINVAL;
857 }
David Daney4c73fb22013-05-23 09:49:09 -0700858}
859
860static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
861 const struct kvm_one_reg *reg)
862{
David Daney4c73fb22013-05-23 09:49:09 -0700863 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000864 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
865 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000866 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000867 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700868
David Daney681865d2013-06-10 12:33:48 -0700869 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
870 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
871
872 if (get_user(v, uaddr64) != 0)
873 return -EFAULT;
874 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
875 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
876 s32 v32;
877
878 if (get_user(v32, uaddr32) != 0)
879 return -EFAULT;
880 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000881 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
882 void __user *uaddr = (void __user *)(long)reg->addr;
883
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200884 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700885 } else {
886 return -EINVAL;
887 }
David Daney4c73fb22013-05-23 09:49:09 -0700888
889 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000890 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700891 case KVM_REG_MIPS_R0:
892 /* Silently ignore requests to set $0 */
893 break;
894 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
895 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
896 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100897#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700898 case KVM_REG_MIPS_HI:
899 vcpu->arch.hi = v;
900 break;
901 case KVM_REG_MIPS_LO:
902 vcpu->arch.lo = v;
903 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100904#endif
David Daney4c73fb22013-05-23 09:49:09 -0700905 case KVM_REG_MIPS_PC:
906 vcpu->arch.pc = v;
907 break;
908
James Hogan379245c2014-12-02 15:48:24 +0000909 /* Floating point registers */
910 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
911 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
912 return -EINVAL;
913 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
914 /* Odd singles in top of even double when FR=0 */
915 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
916 set_fpr32(&fpu->fpr[idx], 0, v);
917 else
918 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
919 break;
920 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
921 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
922 return -EINVAL;
923 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
924 /* Can't access odd doubles in FR=0 mode */
925 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
926 return -EINVAL;
927 set_fpr64(&fpu->fpr[idx], 0, v);
928 break;
929 case KVM_REG_MIPS_FCR_IR:
930 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
931 return -EINVAL;
932 /* Read-only */
933 break;
934 case KVM_REG_MIPS_FCR_CSR:
935 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
936 return -EINVAL;
937 fpu->fcr31 = v;
938 break;
939
James Hoganab86bd62014-12-02 15:48:24 +0000940 /* MIPS SIMD Architecture (MSA) registers */
941 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
942 if (!kvm_mips_guest_has_msa(&vcpu->arch))
943 return -EINVAL;
944 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
945#ifdef CONFIG_CPU_LITTLE_ENDIAN
946 /* least significant byte first */
947 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
948 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
949#else
950 /* most significant byte first */
951 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
952 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
953#endif
954 break;
955 case KVM_REG_MIPS_MSA_IR:
956 if (!kvm_mips_guest_has_msa(&vcpu->arch))
957 return -EINVAL;
958 /* Read-only */
959 break;
960 case KVM_REG_MIPS_MSA_CSR:
961 if (!kvm_mips_guest_has_msa(&vcpu->arch))
962 return -EINVAL;
963 fpu->msacsr = v;
964 break;
965
James Hogan379245c2014-12-02 15:48:24 +0000966 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700967 case KVM_REG_MIPS_CP0_INDEX:
968 kvm_write_c0_guest_index(cop0, v);
969 break;
970 case KVM_REG_MIPS_CP0_CONTEXT:
971 kvm_write_c0_guest_context(cop0, v);
972 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100973 case KVM_REG_MIPS_CP0_USERLOCAL:
974 kvm_write_c0_guest_userlocal(cop0, v);
975 break;
David Daney4c73fb22013-05-23 09:49:09 -0700976 case KVM_REG_MIPS_CP0_PAGEMASK:
977 kvm_write_c0_guest_pagemask(cop0, v);
978 break;
979 case KVM_REG_MIPS_CP0_WIRED:
980 kvm_write_c0_guest_wired(cop0, v);
981 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100982 case KVM_REG_MIPS_CP0_HWRENA:
983 kvm_write_c0_guest_hwrena(cop0, v);
984 break;
David Daney4c73fb22013-05-23 09:49:09 -0700985 case KVM_REG_MIPS_CP0_BADVADDR:
986 kvm_write_c0_guest_badvaddr(cop0, v);
987 break;
988 case KVM_REG_MIPS_CP0_ENTRYHI:
989 kvm_write_c0_guest_entryhi(cop0, v);
990 break;
991 case KVM_REG_MIPS_CP0_STATUS:
992 kvm_write_c0_guest_status(cop0, v);
993 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100994 case KVM_REG_MIPS_CP0_EPC:
995 kvm_write_c0_guest_epc(cop0, v);
996 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100997 case KVM_REG_MIPS_CP0_PRID:
998 kvm_write_c0_guest_prid(cop0, v);
999 break;
David Daney4c73fb22013-05-23 09:49:09 -07001000 case KVM_REG_MIPS_CP0_ERROREPC:
1001 kvm_write_c0_guest_errorepc(cop0, v);
1002 break;
James Hogan05108702016-06-15 19:29:56 +01001003 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
1004 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
1005 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
1006 return -EINVAL;
1007 switch (idx) {
1008 case 2:
1009 kvm_write_c0_guest_kscratch1(cop0, v);
1010 break;
1011 case 3:
1012 kvm_write_c0_guest_kscratch2(cop0, v);
1013 break;
1014 case 4:
1015 kvm_write_c0_guest_kscratch3(cop0, v);
1016 break;
1017 case 5:
1018 kvm_write_c0_guest_kscratch4(cop0, v);
1019 break;
1020 case 6:
1021 kvm_write_c0_guest_kscratch5(cop0, v);
1022 break;
1023 case 7:
1024 kvm_write_c0_guest_kscratch6(cop0, v);
1025 break;
1026 }
1027 break;
James Hoganf8be02d2014-05-29 10:16:29 +01001028 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -07001029 default:
James Hogancc68d222016-06-15 19:29:48 +01001030 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -07001031 }
1032 return 0;
1033}
1034
James Hogan5fafd8742014-12-08 23:07:56 +00001035static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1036 struct kvm_enable_cap *cap)
1037{
1038 int r = 0;
1039
1040 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1041 return -EINVAL;
1042 if (cap->flags)
1043 return -EINVAL;
1044 if (cap->args[0])
1045 return -EINVAL;
1046
1047 switch (cap->cap) {
1048 case KVM_CAP_MIPS_FPU:
1049 vcpu->arch.fpu_enabled = true;
1050 break;
James Hogand952bd02014-12-08 23:07:56 +00001051 case KVM_CAP_MIPS_MSA:
1052 vcpu->arch.msa_enabled = true;
1053 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001054 default:
1055 r = -EINVAL;
1056 break;
1057 }
1058
1059 return r;
1060}
1061
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001062long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1063 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -08001064{
1065 struct kvm_vcpu *vcpu = filp->private_data;
1066 void __user *argp = (void __user *)arg;
1067 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001068
1069 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -07001070 case KVM_SET_ONE_REG:
1071 case KVM_GET_ONE_REG: {
1072 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001073
David Daney4c73fb22013-05-23 09:49:09 -07001074 if (copy_from_user(&reg, argp, sizeof(reg)))
1075 return -EFAULT;
1076 if (ioctl == KVM_SET_ONE_REG)
1077 return kvm_mips_set_reg(vcpu, &reg);
1078 else
1079 return kvm_mips_get_reg(vcpu, &reg);
1080 }
1081 case KVM_GET_REG_LIST: {
1082 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -07001083 struct kvm_reg_list reg_list;
1084 unsigned n;
1085
1086 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1087 return -EFAULT;
1088 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +01001089 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -07001090 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1091 return -EFAULT;
1092 if (n < reg_list.n)
1093 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +01001094 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -07001095 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001096 case KVM_INTERRUPT:
1097 {
1098 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001099
Sanjay Lal669e8462012-11-21 18:34:02 -08001100 if (copy_from_user(&irq, argp, sizeof(irq)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +01001101 return -EFAULT;
Sanjay Lal669e8462012-11-21 18:34:02 -08001102 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1103 irq.irq);
1104
1105 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1106 break;
1107 }
James Hogan5fafd8742014-12-08 23:07:56 +00001108 case KVM_ENABLE_CAP: {
1109 struct kvm_enable_cap cap;
1110
James Hogan5fafd8742014-12-08 23:07:56 +00001111 if (copy_from_user(&cap, argp, sizeof(cap)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +01001112 return -EFAULT;
James Hogan5fafd8742014-12-08 23:07:56 +00001113 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1114 break;
1115 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001116 default:
David Daney4c73fb22013-05-23 09:49:09 -07001117 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001118 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001119 return r;
1120}
1121
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001122/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001123int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1124{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001125 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001126 struct kvm_memory_slot *memslot;
1127 unsigned long ga, ga_end;
1128 int is_dirty = 0;
1129 int r;
1130 unsigned long n;
1131
1132 mutex_lock(&kvm->slots_lock);
1133
1134 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1135 if (r)
1136 goto out;
1137
1138 /* If nothing is dirty, don't bother messing with page tables. */
1139 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001140 slots = kvm_memslots(kvm);
1141 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001142
1143 ga = memslot->base_gfn << PAGE_SHIFT;
1144 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1145
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001146 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
1147 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -08001148
1149 n = kvm_dirty_bitmap_bytes(memslot);
1150 memset(memslot->dirty_bitmap, 0, n);
1151 }
1152
1153 r = 0;
1154out:
1155 mutex_unlock(&kvm->slots_lock);
1156 return r;
1157
1158}
1159
1160long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1161{
1162 long r;
1163
1164 switch (ioctl) {
1165 default:
David Daneyed829852013-05-23 09:49:10 -07001166 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001167 }
1168
1169 return r;
1170}
1171
1172int kvm_arch_init(void *opaque)
1173{
Sanjay Lal669e8462012-11-21 18:34:02 -08001174 if (kvm_mips_callbacks) {
1175 kvm_err("kvm: module already exists\n");
1176 return -EEXIST;
1177 }
1178
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001179 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001180}
1181
1182void kvm_arch_exit(void)
1183{
1184 kvm_mips_callbacks = NULL;
1185}
1186
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001187int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1188 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001189{
David Daneyed829852013-05-23 09:49:10 -07001190 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001191}
1192
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001193int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1194 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001195{
David Daneyed829852013-05-23 09:49:10 -07001196 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001197}
1198
Dominik Dingel31928aa2014-12-04 15:47:07 +01001199void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001200{
Sanjay Lal669e8462012-11-21 18:34:02 -08001201}
1202
1203int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1204{
David Daneyed829852013-05-23 09:49:10 -07001205 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001206}
1207
1208int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1209{
David Daneyed829852013-05-23 09:49:10 -07001210 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001211}
1212
1213int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1214{
1215 return VM_FAULT_SIGBUS;
1216}
1217
Alexander Graf784aa3d2014-07-14 18:27:35 +02001218int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001219{
1220 int r;
1221
1222 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001223 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001224 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001225 r = 1;
1226 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001227 case KVM_CAP_COALESCED_MMIO:
1228 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1229 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001230 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001231 /* We don't handle systems with inconsistent cpu_has_fpu */
1232 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001233 break;
James Hogand952bd02014-12-08 23:07:56 +00001234 case KVM_CAP_MIPS_MSA:
1235 /*
1236 * We don't support MSA vector partitioning yet:
1237 * 1) It would require explicit support which can't be tested
1238 * yet due to lack of support in current hardware.
1239 * 2) It extends the state that would need to be saved/restored
1240 * by e.g. QEMU for migration.
1241 *
1242 * When vector partitioning hardware becomes available, support
1243 * could be added by requiring a flag when enabling
1244 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1245 * to save/restore the appropriate extra state.
1246 */
1247 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1248 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001249 default:
1250 r = 0;
1251 break;
1252 }
1253 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001254}
1255
1256int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1257{
1258 return kvm_mips_pending_timer(vcpu);
1259}
1260
1261int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1262{
1263 int i;
1264 struct mips_coproc *cop0;
1265
1266 if (!vcpu)
1267 return -1;
1268
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001269 kvm_debug("VCPU Register Dump:\n");
1270 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1271 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001272
1273 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001274 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001275 vcpu->arch.gprs[i],
1276 vcpu->arch.gprs[i + 1],
1277 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1278 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001279 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1280 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001281
1282 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001283 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1284 kvm_read_c0_guest_status(cop0),
1285 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001286
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001287 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001288
1289 return 0;
1290}
1291
1292int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1293{
1294 int i;
1295
David Daney8d17dd02013-05-23 09:49:08 -07001296 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001297 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001298 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001299 vcpu->arch.hi = regs->hi;
1300 vcpu->arch.lo = regs->lo;
1301 vcpu->arch.pc = regs->pc;
1302
David Daney4c73fb22013-05-23 09:49:09 -07001303 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001304}
1305
1306int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1307{
1308 int i;
1309
David Daney8d17dd02013-05-23 09:49:08 -07001310 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001311 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001312
1313 regs->hi = vcpu->arch.hi;
1314 regs->lo = vcpu->arch.lo;
1315 regs->pc = vcpu->arch.pc;
1316
David Daney4c73fb22013-05-23 09:49:09 -07001317 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001318}
1319
James Hogan0fae34f2014-05-29 10:16:39 +01001320static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001321{
1322 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1323
1324 kvm_mips_callbacks->queue_timer_int(vcpu);
1325
1326 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001327 if (swait_active(&vcpu->wq))
1328 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001329}
1330
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001331/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001332static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001333{
1334 struct kvm_vcpu *vcpu;
1335
1336 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1337 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001338 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001339}
1340
1341int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1342{
James Hoganf7f14272016-09-08 22:57:03 +01001343 int err;
1344
1345 err = kvm_mips_callbacks->vcpu_init(vcpu);
1346 if (err)
1347 return err;
1348
Sanjay Lal669e8462012-11-21 18:34:02 -08001349 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1350 HRTIMER_MODE_REL);
1351 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001352 return 0;
1353}
1354
James Hogan630766b2016-09-08 23:00:24 +01001355void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1356{
1357 kvm_mips_callbacks->vcpu_uninit(vcpu);
1358}
1359
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001360int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1361 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001362{
1363 return 0;
1364}
1365
1366/* Initial guest state */
1367int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1368{
1369 return kvm_mips_callbacks->vcpu_setup(vcpu);
1370}
1371
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001372static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001373{
James Hogan8cffd192016-06-09 14:19:08 +01001374 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001375
Sanjay Lal669e8462012-11-21 18:34:02 -08001376 if (cpu_has_dsp)
1377 status |= (ST0_MX);
1378
1379 write_c0_status(status);
1380 ehb();
1381}
1382
1383/*
1384 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1385 */
1386int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1387{
James Hogan8cffd192016-06-09 14:19:08 +01001388 u32 cause = vcpu->arch.host_cp0_cause;
1389 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1390 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001391 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1392 enum emulation_result er = EMULATE_DONE;
1393 int ret = RESUME_GUEST;
1394
James Hoganc4c6f2c2015-02-04 10:52:03 +00001395 /* re-enable HTW before enabling interrupts */
1396 htw_start();
1397
Sanjay Lal669e8462012-11-21 18:34:02 -08001398 /* Set a default exit reason */
1399 run->exit_reason = KVM_EXIT_UNKNOWN;
1400 run->ready_for_interrupt_injection = 1;
1401
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001402 /*
1403 * Set the appropriate status bits based on host CPU features,
1404 * before we hit the scheduler
1405 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001406 kvm_mips_set_c0_status();
1407
1408 local_irq_enable();
1409
1410 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1411 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001412 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001413
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001414 /*
1415 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001416 * causing an exception to be delivered to the Guest Kernel
1417 */
1418 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1419 if (er == EMULATE_PRIV_FAIL) {
1420 goto skip_emul;
1421 } else if (er == EMULATE_FAIL) {
1422 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1423 ret = RESUME_HOST;
1424 goto skip_emul;
1425 }
1426
1427 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001428 case EXCCODE_INT:
1429 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001430
1431 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001432
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001433 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001434 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001435
1436 ret = RESUME_GUEST;
1437 break;
1438
James Hogan16d100db2015-12-16 23:49:33 +00001439 case EXCCODE_CPU:
1440 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001441
1442 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001443 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1444 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001445 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001446 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001447 break;
1448
James Hogan16d100db2015-12-16 23:49:33 +00001449 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001450 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001451 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1452 break;
1453
James Hogan16d100db2015-12-16 23:49:33 +00001454 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001455 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1456 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1457 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001458
1459 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001460 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1461 break;
1462
James Hogan16d100db2015-12-16 23:49:33 +00001463 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001464 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1465 cause, opc, badvaddr);
1466
1467 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001468 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1469 break;
1470
James Hogan16d100db2015-12-16 23:49:33 +00001471 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001472 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001473 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1474 break;
1475
James Hogan16d100db2015-12-16 23:49:33 +00001476 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001477 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001478 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1479 break;
1480
James Hogan16d100db2015-12-16 23:49:33 +00001481 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001482 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001483 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1484 break;
1485
James Hogan16d100db2015-12-16 23:49:33 +00001486 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001487 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001488 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1489 break;
1490
James Hogan16d100db2015-12-16 23:49:33 +00001491 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001492 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001493 ret = kvm_mips_callbacks->handle_break(vcpu);
1494 break;
1495
James Hogan16d100db2015-12-16 23:49:33 +00001496 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001497 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001498 ret = kvm_mips_callbacks->handle_trap(vcpu);
1499 break;
1500
James Hogan16d100db2015-12-16 23:49:33 +00001501 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001502 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001503 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1504 break;
1505
James Hogan16d100db2015-12-16 23:49:33 +00001506 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001507 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001508 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1509 break;
1510
James Hogan16d100db2015-12-16 23:49:33 +00001511 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001512 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001513 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1514 break;
1515
Sanjay Lal669e8462012-11-21 18:34:02 -08001516 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001517 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1518 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1519 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001520 kvm_arch_vcpu_dump_regs(vcpu);
1521 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1522 ret = RESUME_HOST;
1523 break;
1524
1525 }
1526
1527skip_emul:
1528 local_irq_disable();
1529
1530 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1531 kvm_mips_deliver_interrupts(vcpu, cause);
1532
1533 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001534 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001535 if (signal_pending(current)) {
1536 run->exit_reason = KVM_EXIT_INTR;
1537 ret = (-EINTR << 2) | RESUME_HOST;
1538 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001539 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001540 }
1541 }
1542
James Hogan98e91b82014-11-18 14:09:12 +00001543 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001544 trace_kvm_reenter(vcpu);
1545
James Hogana2c046e2016-11-18 13:14:37 +00001546 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +01001547
James Hogan98e91b82014-11-18 14:09:12 +00001548 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001549 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1550 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001551 *
1552 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001553 * vector, as it may well cause an [MSA] FP exception if there
1554 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001555 * kvm_mips_csr_die_notifier() for how that is handled).
1556 */
1557 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1558 read_c0_status() & ST0_CU1)
1559 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001560
1561 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1562 read_c0_config5() & MIPS_CONF5_MSAEN)
1563 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001564 }
1565
James Hoganc4c6f2c2015-02-04 10:52:03 +00001566 /* Disable HTW before returning to guest or host */
1567 htw_stop();
1568
Sanjay Lal669e8462012-11-21 18:34:02 -08001569 return ret;
1570}
1571
James Hogan98e91b82014-11-18 14:09:12 +00001572/* Enable FPU for guest and restore context */
1573void kvm_own_fpu(struct kvm_vcpu *vcpu)
1574{
1575 struct mips_coproc *cop0 = vcpu->arch.cop0;
1576 unsigned int sr, cfg5;
1577
1578 preempt_disable();
1579
James Hogan539cb89fb2015-03-05 11:43:36 +00001580 sr = kvm_read_c0_guest_status(cop0);
1581
1582 /*
1583 * If MSA state is already live, it is undefined how it interacts with
1584 * FR=0 FPU state, and we don't want to hit reserved instruction
1585 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1586 * play it safe and save it first.
1587 *
1588 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1589 * get called when guest CU1 is set, however we can't trust the guest
1590 * not to clobber the status register directly via the commpage.
1591 */
1592 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001593 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001594 kvm_lose_fpu(vcpu);
1595
James Hogan98e91b82014-11-18 14:09:12 +00001596 /*
1597 * Enable FPU for guest
1598 * We set FR and FRE according to guest context
1599 */
James Hogan98e91b82014-11-18 14:09:12 +00001600 change_c0_status(ST0_CU1 | ST0_FR, sr);
1601 if (cpu_has_fre) {
1602 cfg5 = kvm_read_c0_guest_config5(cop0);
1603 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1604 }
1605 enable_fpu_hazard();
1606
1607 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001608 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001609 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001610 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001611 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1612 } else {
1613 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001614 }
1615
1616 preempt_enable();
1617}
1618
James Hogan539cb89fb2015-03-05 11:43:36 +00001619#ifdef CONFIG_CPU_HAS_MSA
1620/* Enable MSA for guest and restore context */
1621void kvm_own_msa(struct kvm_vcpu *vcpu)
1622{
1623 struct mips_coproc *cop0 = vcpu->arch.cop0;
1624 unsigned int sr, cfg5;
1625
1626 preempt_disable();
1627
1628 /*
1629 * Enable FPU if enabled in guest, since we're restoring FPU context
1630 * anyway. We set FR and FRE according to guest context.
1631 */
1632 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1633 sr = kvm_read_c0_guest_status(cop0);
1634
1635 /*
1636 * If FR=0 FPU state is already live, it is undefined how it
1637 * interacts with MSA state, so play it safe and save it first.
1638 */
1639 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001640 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1641 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001642 kvm_lose_fpu(vcpu);
1643
1644 change_c0_status(ST0_CU1 | ST0_FR, sr);
1645 if (sr & ST0_CU1 && cpu_has_fre) {
1646 cfg5 = kvm_read_c0_guest_config5(cop0);
1647 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1648 }
1649 }
1650
1651 /* Enable MSA for guest */
1652 set_c0_config5(MIPS_CONF5_MSAEN);
1653 enable_fpu_hazard();
1654
James Hoganf9431762016-06-14 09:40:10 +01001655 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1656 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001657 /*
1658 * Guest FPU state already loaded, only restore upper MSA state
1659 */
1660 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001661 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001662 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001663 break;
1664 case 0:
1665 /* Neither FPU or MSA already active, restore full MSA state */
1666 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001667 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001668 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001669 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001670 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1671 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001672 break;
1673 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001674 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001675 break;
1676 }
1677
1678 preempt_enable();
1679}
1680#endif
1681
1682/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001683void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1684{
1685 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001686 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001687 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001688 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001689 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001690 }
James Hoganf9431762016-06-14 09:40:10 +01001691 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001692 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001693 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001694 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001695 }
1696 preempt_enable();
1697}
1698
James Hogan539cb89fb2015-03-05 11:43:36 +00001699/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001700void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1701{
1702 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001703 * FPU & MSA get disabled in root context (hardware) when it is disabled
1704 * in guest context (software), but the register state in the hardware
1705 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001706 * before saving.
1707 */
1708
1709 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001710 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001711 set_c0_config5(MIPS_CONF5_MSAEN);
1712 enable_fpu_hazard();
1713
1714 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001715 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001716
1717 /* Disable MSA & FPU */
1718 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001719 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001720 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001721 disable_fpu_hazard();
1722 }
James Hoganf9431762016-06-14 09:40:10 +01001723 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1724 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001725 set_c0_status(ST0_CU1);
1726 enable_fpu_hazard();
1727
1728 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001729 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001730 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001731
1732 /* Disable FPU */
1733 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001734 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001735 }
1736 preempt_enable();
1737}
1738
1739/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001740 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1741 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1742 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001743 */
1744static int kvm_mips_csr_die_notify(struct notifier_block *self,
1745 unsigned long cmd, void *ptr)
1746{
1747 struct die_args *args = (struct die_args *)ptr;
1748 struct pt_regs *regs = args->regs;
1749 unsigned long pc;
1750
James Hogan539cb89fb2015-03-05 11:43:36 +00001751 /* Only interested in FPE and MSAFPE */
1752 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001753 return NOTIFY_DONE;
1754
1755 /* Return immediately if guest context isn't active */
1756 if (!(current->flags & PF_VCPU))
1757 return NOTIFY_DONE;
1758
1759 /* Should never get here from user mode */
1760 BUG_ON(user_mode(regs));
1761
1762 pc = instruction_pointer(regs);
1763 switch (cmd) {
1764 case DIE_FP:
1765 /* match 2nd instruction in __kvm_restore_fcsr */
1766 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1767 return NOTIFY_DONE;
1768 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001769 case DIE_MSAFP:
1770 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1771 if (!cpu_has_msa ||
1772 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1773 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1774 return NOTIFY_DONE;
1775 break;
James Hogan98e91b82014-11-18 14:09:12 +00001776 }
1777
1778 /* Move PC forward a little and continue executing */
1779 instruction_pointer(regs) += 4;
1780
1781 return NOTIFY_STOP;
1782}
1783
1784static struct notifier_block kvm_mips_csr_die_notifier = {
1785 .notifier_call = kvm_mips_csr_die_notify,
1786};
1787
James Hogan2db9d232015-12-16 23:49:32 +00001788static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001789{
1790 int ret;
1791
James Hogan1e5217f52016-06-23 17:34:45 +01001792 ret = kvm_mips_entry_setup();
1793 if (ret)
1794 return ret;
1795
Sanjay Lal669e8462012-11-21 18:34:02 -08001796 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1797
1798 if (ret)
1799 return ret;
1800
James Hogan98e91b82014-11-18 14:09:12 +00001801 register_die_notifier(&kvm_mips_csr_die_notifier);
1802
Sanjay Lal669e8462012-11-21 18:34:02 -08001803 return 0;
1804}
1805
James Hogan2db9d232015-12-16 23:49:32 +00001806static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001807{
1808 kvm_exit();
1809
James Hogan98e91b82014-11-18 14:09:12 +00001810 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001811}
1812
1813module_init(kvm_mips_init);
1814module_exit(kvm_mips_exit);
1815
1816EXPORT_TRACEPOINT_SYMBOL(kvm_exit);