blob: 622037d851a35a4abe9c51c7cae956f84dca452d [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
66static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu)
67{
68 int i;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070069
Sanjay Lal669e8462012-11-21 18:34:02 -080070 for_each_possible_cpu(i) {
71 vcpu->arch.guest_kernel_asid[i] = 0;
72 vcpu->arch.guest_user_asid[i] = 0;
73 }
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070074
Sanjay Lal669e8462012-11-21 18:34:02 -080075 return 0;
76}
77
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070078/*
79 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
80 * Config7, so we are "runnable" if interrupts are pending
Sanjay Lal669e8462012-11-21 18:34:02 -080081 */
82int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
83{
84 return !!(vcpu->arch.pending_exceptions);
85}
86
87int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
88{
89 return 1;
90}
91
Radim Krčmář13a34e02014-08-28 15:13:03 +020092int kvm_arch_hardware_enable(void)
Sanjay Lal669e8462012-11-21 18:34:02 -080093{
94 return 0;
95}
96
Sanjay Lal669e8462012-11-21 18:34:02 -080097int kvm_arch_hardware_setup(void)
98{
99 return 0;
100}
101
Sanjay Lal669e8462012-11-21 18:34:02 -0800102void kvm_arch_check_processor_compat(void *rtn)
103{
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700104 *(int *)rtn = 0;
Sanjay Lal669e8462012-11-21 18:34:02 -0800105}
106
107static void kvm_mips_init_tlbs(struct kvm *kvm)
108{
109 unsigned long wired;
110
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700111 /*
112 * Add a wired entry to the TLB, it is used to map the commpage to
113 * the Guest kernel
114 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800115 wired = read_c0_wired();
116 write_c0_wired(wired + 1);
117 mtc0_tlbw_hazard();
118 kvm->arch.commpage_tlb = wired;
119
120 kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(),
121 kvm->arch.commpage_tlb);
122}
123
124static void kvm_mips_init_vm_percpu(void *arg)
125{
126 struct kvm *kvm = (struct kvm *)arg;
127
128 kvm_mips_init_tlbs(kvm);
129 kvm_mips_callbacks->vm_init(kvm);
130
131}
132
133int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
134{
135 if (atomic_inc_return(&kvm_mips_instance) == 1) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100136 kvm_debug("%s: 1st KVM instance, setup host TLB parameters\n",
137 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800138 on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1);
139 }
140
Sanjay Lal669e8462012-11-21 18:34:02 -0800141 return 0;
142}
143
Luiz Capitulino235539b2016-09-07 14:47:23 -0400144bool kvm_arch_has_vcpu_debugfs(void)
145{
146 return false;
147}
148
149int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
150{
151 return 0;
152}
153
Sanjay Lal669e8462012-11-21 18:34:02 -0800154void kvm_mips_free_vcpus(struct kvm *kvm)
155{
156 unsigned int i;
157 struct kvm_vcpu *vcpu;
158
159 /* Put the pages we reserved for the guest pmap */
160 for (i = 0; i < kvm->arch.guest_pmap_npages; i++) {
161 if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE)
James Hogan9befad22016-06-09 14:19:11 +0100162 kvm_release_pfn_clean(kvm->arch.guest_pmap[i]);
Sanjay Lal669e8462012-11-21 18:34:02 -0800163 }
James Hoganc6c0a662014-05-29 10:16:44 +0100164 kfree(kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800165
166 kvm_for_each_vcpu(i, vcpu, kvm) {
167 kvm_arch_vcpu_free(vcpu);
168 }
169
170 mutex_lock(&kvm->lock);
171
172 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
173 kvm->vcpus[i] = NULL;
174
175 atomic_set(&kvm->online_vcpus, 0);
176
177 mutex_unlock(&kvm->lock);
178}
179
Sanjay Lal669e8462012-11-21 18:34:02 -0800180static void kvm_mips_uninit_tlbs(void *arg)
181{
182 /* Restore wired count */
183 write_c0_wired(0);
184 mtc0_tlbw_hazard();
185 /* Clear out all the TLBs */
186 kvm_local_flush_tlb_all();
187}
188
189void kvm_arch_destroy_vm(struct kvm *kvm)
190{
191 kvm_mips_free_vcpus(kvm);
192
193 /* If this is the last instance, restore wired count */
194 if (atomic_dec_return(&kvm_mips_instance) == 0) {
James Hogan6e95bfd2014-05-29 10:16:43 +0100195 kvm_debug("%s: last KVM instance, restoring TLB parameters\n",
196 __func__);
Sanjay Lal669e8462012-11-21 18:34:02 -0800197 on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1);
198 }
199}
200
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700201long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
202 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800203{
David Daneyed829852013-05-23 09:49:10 -0700204 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800205}
206
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530207int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
208 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800209{
210 return 0;
211}
212
213int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700214 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200215 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700216 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800217{
218 return 0;
219}
220
221void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200222 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700223 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200224 const struct kvm_memory_slot *new,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700225 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800226{
227 unsigned long npages = 0;
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700228 int i;
Sanjay Lal669e8462012-11-21 18:34:02 -0800229
230 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
231 __func__, kvm, mem->slot, mem->guest_phys_addr,
232 mem->memory_size, mem->userspace_addr);
233
234 /* Setup Guest PMAP table */
235 if (!kvm->arch.guest_pmap) {
236 if (mem->slot == 0)
237 npages = mem->memory_size >> PAGE_SHIFT;
238
239 if (npages) {
240 kvm->arch.guest_pmap_npages = npages;
241 kvm->arch.guest_pmap =
242 kzalloc(npages * sizeof(unsigned long), GFP_KERNEL);
243
244 if (!kvm->arch.guest_pmap) {
James Hoganf7fdcb62015-12-16 23:49:39 +0000245 kvm_err("Failed to allocate guest PMAP\n");
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700246 return;
Sanjay Lal669e8462012-11-21 18:34:02 -0800247 }
248
James Hogan6e95bfd2014-05-29 10:16:43 +0100249 kvm_debug("Allocated space for Guest PMAP Table (%ld pages) @ %p\n",
250 npages, kvm->arch.guest_pmap);
Sanjay Lal669e8462012-11-21 18:34:02 -0800251
252 /* Now setup the page table */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700253 for (i = 0; i < npages; i++)
Sanjay Lal669e8462012-11-21 18:34:02 -0800254 kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE;
Sanjay Lal669e8462012-11-21 18:34:02 -0800255 }
256 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800257}
258
James Hogand7b8f892016-06-23 17:34:40 +0100259static inline void dump_handler(const char *symbol, void *start, void *end)
260{
261 u32 *p;
262
263 pr_debug("LEAF(%s)\n", symbol);
264
265 pr_debug("\t.set push\n");
266 pr_debug("\t.set noreorder\n");
267
268 for (p = start; p < (u32 *)end; ++p)
269 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p);
270
271 pr_debug("\t.set\tpop\n");
272
273 pr_debug("\tEND(%s)\n", symbol);
274}
275
Sanjay Lal669e8462012-11-21 18:34:02 -0800276struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
277{
James Hogan90e93112016-06-23 17:34:39 +0100278 int err, size;
James Hogan1f9ca622016-06-23 17:34:46 +0100279 void *gebase, *p, *handler;
Sanjay Lal669e8462012-11-21 18:34:02 -0800280 int i;
281
282 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
283
284 if (!vcpu) {
285 err = -ENOMEM;
286 goto out;
287 }
288
289 err = kvm_vcpu_init(vcpu, kvm, id);
290
291 if (err)
292 goto out_free_cpu;
293
James Hogan6e95bfd2014-05-29 10:16:43 +0100294 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800295
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700296 /*
297 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800298 * guest mode exits
299 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700300 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800301 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700302 else
James Hogan7006e2d2014-05-29 10:16:23 +0100303 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800304
Sanjay Lal669e8462012-11-21 18:34:02 -0800305 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
306
307 if (!gebase) {
308 err = -ENOMEM;
James Hogan585bb8f2015-11-11 14:21:20 +0000309 goto out_uninit_cpu;
Sanjay Lal669e8462012-11-21 18:34:02 -0800310 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100311 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
312 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800313
James Hogan2a06dab2016-07-08 11:53:26 +0100314 /*
315 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
316 * limits us to the low 512MB of physical address space. If the memory
317 * we allocate is out of range, just give up now.
318 */
319 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) {
320 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
321 gebase);
322 err = -ENOMEM;
323 goto out_free_gebase;
324 }
325
Sanjay Lal669e8462012-11-21 18:34:02 -0800326 /* Save new ebase */
327 vcpu->arch.guest_ebase = gebase;
328
James Hogan90e93112016-06-23 17:34:39 +0100329 /* Build guest exception vectors dynamically in unmapped memory */
James Hogan1f9ca622016-06-23 17:34:46 +0100330 handler = gebase + 0x2000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800331
332 /* TLB Refill, EXL = 0 */
James Hogan1f9ca622016-06-23 17:34:46 +0100333 kvm_mips_build_exception(gebase, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800334
335 /* General Exception Entry point */
James Hogan1f9ca622016-06-23 17:34:46 +0100336 kvm_mips_build_exception(gebase + 0x180, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800337
338 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
339 for (i = 0; i < 8; i++) {
340 kvm_debug("L1 Vectored handler @ %p\n",
341 gebase + 0x200 + (i * VECTORSPACING));
James Hogan1f9ca622016-06-23 17:34:46 +0100342 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
343 handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800344 }
345
James Hogan90e93112016-06-23 17:34:39 +0100346 /* General exit handler */
James Hogan1f9ca622016-06-23 17:34:46 +0100347 p = handler;
James Hogan90e93112016-06-23 17:34:39 +0100348 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800349
James Hogan90e93112016-06-23 17:34:39 +0100350 /* Guest entry routine */
351 vcpu->arch.vcpu_run = p;
352 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100353
James Hogand7b8f892016-06-23 17:34:40 +0100354 /* Dump the generated code */
355 pr_debug("#include <asm/asm.h>\n");
356 pr_debug("#include <asm/regdef.h>\n");
357 pr_debug("\n");
358 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
359 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
360 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
361
Sanjay Lal669e8462012-11-21 18:34:02 -0800362 /* Invalidate the icache for these ranges */
James Hoganfacaaec2014-05-29 10:16:25 +0100363 local_flush_icache_range((unsigned long)gebase,
364 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800365
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700366 /*
367 * Allocate comm page for guest kernel, a TLB will be reserved for
368 * mapping GVA @ 0xFFFF8000 to this page
369 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800370 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
371
372 if (!vcpu->arch.kseg0_commpage) {
373 err = -ENOMEM;
374 goto out_free_gebase;
375 }
376
James Hogan6e95bfd2014-05-29 10:16:43 +0100377 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800378 kvm_mips_commpage_init(vcpu);
379
380 /* Init */
381 vcpu->arch.last_sched_cpu = -1;
382
383 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100384 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800385
386 return vcpu;
387
388out_free_gebase:
389 kfree(gebase);
390
James Hogan585bb8f2015-11-11 14:21:20 +0000391out_uninit_cpu:
392 kvm_vcpu_uninit(vcpu);
393
Sanjay Lal669e8462012-11-21 18:34:02 -0800394out_free_cpu:
395 kfree(vcpu);
396
397out:
398 return ERR_PTR(err);
399}
400
401void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
402{
403 hrtimer_cancel(&vcpu->arch.comparecount_timer);
404
405 kvm_vcpu_uninit(vcpu);
406
407 kvm_mips_dump_stats(vcpu);
408
James Hoganc6c0a662014-05-29 10:16:44 +0100409 kfree(vcpu->arch.guest_ebase);
410 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700411 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800412}
413
414void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
415{
416 kvm_arch_vcpu_free(vcpu);
417}
418
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700419int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
420 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800421{
David Daneyed829852013-05-23 09:49:10 -0700422 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800423}
424
James Hogan25b08c72016-09-16 00:06:43 +0100425/* Must be called with preemption disabled, just before entering guest */
426static void kvm_mips_check_asids(struct kvm_vcpu *vcpu)
427{
428 struct mips_coproc *cop0 = vcpu->arch.cop0;
429 int cpu = smp_processor_id();
430 unsigned int gasid;
431
432 /*
433 * Lazy host ASID regeneration for guest user mode.
434 * If the guest ASID has changed since the last guest usermode
435 * execution, regenerate the host ASID so as to invalidate stale TLB
436 * entries.
437 */
438 if (!KVM_GUEST_KERNEL_MODE(vcpu)) {
439 gasid = kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID;
440 if (gasid != vcpu->arch.last_user_gasid) {
441 kvm_get_new_mmu_context(&vcpu->arch.guest_user_mm, cpu,
442 vcpu);
443 vcpu->arch.guest_user_asid[cpu] =
444 vcpu->arch.guest_user_mm.context.asid[cpu];
445 vcpu->arch.last_user_gasid = gasid;
446 }
447 }
448}
449
Sanjay Lal669e8462012-11-21 18:34:02 -0800450int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
451{
452 int r = 0;
453 sigset_t sigsaved;
454
455 if (vcpu->sigset_active)
456 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
457
458 if (vcpu->mmio_needed) {
459 if (!vcpu->mmio_is_write)
460 kvm_mips_complete_mmio_load(vcpu, run);
461 vcpu->mmio_needed = 0;
462 }
463
James Hoganf7982172015-02-04 17:06:37 +0000464 lose_fpu(1);
465
James Hogan044f0f02014-05-29 10:16:32 +0100466 local_irq_disable();
Sanjay Lal669e8462012-11-21 18:34:02 -0800467 /* Check if we have any exceptions/interrupts pending */
468 kvm_mips_deliver_interrupts(vcpu,
469 kvm_read_c0_guest_cause(vcpu->arch.cop0));
470
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200471 guest_enter_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800472
James Hoganc4c6f2c2015-02-04 10:52:03 +0000473 /* Disable hardware page table walking while in guest */
474 htw_stop();
475
James Hogan93258602016-06-14 09:40:14 +0100476 trace_kvm_enter(vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100477
478 kvm_mips_check_asids(vcpu);
479
James Hogan797179b2016-06-09 10:50:43 +0100480 r = vcpu->arch.vcpu_run(run, vcpu);
James Hogan93258602016-06-14 09:40:14 +0100481 trace_kvm_out(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800482
James Hoganc4c6f2c2015-02-04 10:52:03 +0000483 /* Re-enable HTW before enabling interrupts */
484 htw_start();
485
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200486 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800487 local_irq_enable();
488
489 if (vcpu->sigset_active)
490 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
491
492 return r;
493}
494
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700495int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
496 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800497{
498 int intr = (int)irq->irq;
499 struct kvm_vcpu *dvcpu = NULL;
500
501 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
502 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
503 (int)intr);
504
505 if (irq->cpu == -1)
506 dvcpu = vcpu;
507 else
508 dvcpu = vcpu->kvm->vcpus[irq->cpu];
509
510 if (intr == 2 || intr == 3 || intr == 4) {
511 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
512
513 } else if (intr == -2 || intr == -3 || intr == -4) {
514 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
515 } else {
516 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
517 irq->cpu, irq->irq);
518 return -EINVAL;
519 }
520
521 dvcpu->arch.wait = 0;
522
Marcelo Tosatti85773702016-02-19 09:46:39 +0100523 if (swait_active(&dvcpu->wq))
524 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800525
526 return 0;
527}
528
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700529int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
530 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800531{
David Daneyed829852013-05-23 09:49:10 -0700532 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800533}
534
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700535int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
536 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800537{
David Daneyed829852013-05-23 09:49:10 -0700538 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800539}
540
David Daney4c73fb22013-05-23 09:49:09 -0700541static u64 kvm_mips_get_one_regs[] = {
542 KVM_REG_MIPS_R0,
543 KVM_REG_MIPS_R1,
544 KVM_REG_MIPS_R2,
545 KVM_REG_MIPS_R3,
546 KVM_REG_MIPS_R4,
547 KVM_REG_MIPS_R5,
548 KVM_REG_MIPS_R6,
549 KVM_REG_MIPS_R7,
550 KVM_REG_MIPS_R8,
551 KVM_REG_MIPS_R9,
552 KVM_REG_MIPS_R10,
553 KVM_REG_MIPS_R11,
554 KVM_REG_MIPS_R12,
555 KVM_REG_MIPS_R13,
556 KVM_REG_MIPS_R14,
557 KVM_REG_MIPS_R15,
558 KVM_REG_MIPS_R16,
559 KVM_REG_MIPS_R17,
560 KVM_REG_MIPS_R18,
561 KVM_REG_MIPS_R19,
562 KVM_REG_MIPS_R20,
563 KVM_REG_MIPS_R21,
564 KVM_REG_MIPS_R22,
565 KVM_REG_MIPS_R23,
566 KVM_REG_MIPS_R24,
567 KVM_REG_MIPS_R25,
568 KVM_REG_MIPS_R26,
569 KVM_REG_MIPS_R27,
570 KVM_REG_MIPS_R28,
571 KVM_REG_MIPS_R29,
572 KVM_REG_MIPS_R30,
573 KVM_REG_MIPS_R31,
574
James Hogan70e92c7e2016-07-04 19:35:11 +0100575#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700576 KVM_REG_MIPS_HI,
577 KVM_REG_MIPS_LO,
James Hogan70e92c7e2016-07-04 19:35:11 +0100578#endif
David Daney4c73fb22013-05-23 09:49:09 -0700579 KVM_REG_MIPS_PC,
580
581 KVM_REG_MIPS_CP0_INDEX,
582 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100583 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700584 KVM_REG_MIPS_CP0_PAGEMASK,
585 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100586 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700587 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100588 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700589 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100590 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700591 KVM_REG_MIPS_CP0_STATUS,
592 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100593 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100594 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700595 KVM_REG_MIPS_CP0_CONFIG,
596 KVM_REG_MIPS_CP0_CONFIG1,
597 KVM_REG_MIPS_CP0_CONFIG2,
598 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100599 KVM_REG_MIPS_CP0_CONFIG4,
600 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700601 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100602 KVM_REG_MIPS_CP0_ERROREPC,
603
604 KVM_REG_MIPS_COUNT_CTL,
605 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100606 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700607};
608
James Hogane5775932016-06-15 19:29:51 +0100609static u64 kvm_mips_get_one_regs_fpu[] = {
610 KVM_REG_MIPS_FCR_IR,
611 KVM_REG_MIPS_FCR_CSR,
612};
613
614static u64 kvm_mips_get_one_regs_msa[] = {
615 KVM_REG_MIPS_MSA_IR,
616 KVM_REG_MIPS_MSA_CSR,
617};
618
James Hogan05108702016-06-15 19:29:56 +0100619static u64 kvm_mips_get_one_regs_kscratch[] = {
620 KVM_REG_MIPS_CP0_KSCRATCH1,
621 KVM_REG_MIPS_CP0_KSCRATCH2,
622 KVM_REG_MIPS_CP0_KSCRATCH3,
623 KVM_REG_MIPS_CP0_KSCRATCH4,
624 KVM_REG_MIPS_CP0_KSCRATCH5,
625 KVM_REG_MIPS_CP0_KSCRATCH6,
626};
627
James Hoganf5c43bd2016-06-15 19:29:49 +0100628static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
629{
630 unsigned long ret;
631
632 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100633 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
634 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
635 /* odd doubles */
636 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
637 ret += 16;
638 }
639 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
640 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hogan05108702016-06-15 19:29:56 +0100641 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
James Hoganf5c43bd2016-06-15 19:29:49 +0100642 ret += kvm_mips_callbacks->num_regs(vcpu);
643
644 return ret;
645}
646
647static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
648{
James Hogane5775932016-06-15 19:29:51 +0100649 u64 index;
650 unsigned int i;
651
James Hoganf5c43bd2016-06-15 19:29:49 +0100652 if (copy_to_user(indices, kvm_mips_get_one_regs,
653 sizeof(kvm_mips_get_one_regs)))
654 return -EFAULT;
655 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
656
James Hogane5775932016-06-15 19:29:51 +0100657 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
658 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
659 sizeof(kvm_mips_get_one_regs_fpu)))
660 return -EFAULT;
661 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
662
663 for (i = 0; i < 32; ++i) {
664 index = KVM_REG_MIPS_FPR_32(i);
665 if (copy_to_user(indices, &index, sizeof(index)))
666 return -EFAULT;
667 ++indices;
668
669 /* skip odd doubles if no F64 */
670 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
671 continue;
672
673 index = KVM_REG_MIPS_FPR_64(i);
674 if (copy_to_user(indices, &index, sizeof(index)))
675 return -EFAULT;
676 ++indices;
677 }
678 }
679
680 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
681 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
682 sizeof(kvm_mips_get_one_regs_msa)))
683 return -EFAULT;
684 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
685
686 for (i = 0; i < 32; ++i) {
687 index = KVM_REG_MIPS_VEC_128(i);
688 if (copy_to_user(indices, &index, sizeof(index)))
689 return -EFAULT;
690 ++indices;
691 }
692 }
693
James Hogan05108702016-06-15 19:29:56 +0100694 for (i = 0; i < 6; ++i) {
695 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
696 continue;
697
698 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
699 sizeof(kvm_mips_get_one_regs_kscratch[i])))
700 return -EFAULT;
701 ++indices;
702 }
703
James Hoganf5c43bd2016-06-15 19:29:49 +0100704 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
705}
706
David Daney4c73fb22013-05-23 09:49:09 -0700707static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
708 const struct kvm_one_reg *reg)
709{
David Daney4c73fb22013-05-23 09:49:09 -0700710 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000711 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100712 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700713 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000714 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000715 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700716
717 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000718 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700719 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
720 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
721 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100722#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700723 case KVM_REG_MIPS_HI:
724 v = (long)vcpu->arch.hi;
725 break;
726 case KVM_REG_MIPS_LO:
727 v = (long)vcpu->arch.lo;
728 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100729#endif
David Daney4c73fb22013-05-23 09:49:09 -0700730 case KVM_REG_MIPS_PC:
731 v = (long)vcpu->arch.pc;
732 break;
733
James Hogan379245c2014-12-02 15:48:24 +0000734 /* Floating point registers */
735 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
736 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
737 return -EINVAL;
738 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
739 /* Odd singles in top of even double when FR=0 */
740 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
741 v = get_fpr32(&fpu->fpr[idx], 0);
742 else
743 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
744 break;
745 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
746 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
747 return -EINVAL;
748 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
749 /* Can't access odd doubles in FR=0 mode */
750 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
751 return -EINVAL;
752 v = get_fpr64(&fpu->fpr[idx], 0);
753 break;
754 case KVM_REG_MIPS_FCR_IR:
755 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
756 return -EINVAL;
757 v = boot_cpu_data.fpu_id;
758 break;
759 case KVM_REG_MIPS_FCR_CSR:
760 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
761 return -EINVAL;
762 v = fpu->fcr31;
763 break;
764
James Hoganab86bd62014-12-02 15:48:24 +0000765 /* MIPS SIMD Architecture (MSA) registers */
766 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
767 if (!kvm_mips_guest_has_msa(&vcpu->arch))
768 return -EINVAL;
769 /* Can't access MSA registers in FR=0 mode */
770 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
771 return -EINVAL;
772 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
773#ifdef CONFIG_CPU_LITTLE_ENDIAN
774 /* least significant byte first */
775 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
776 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
777#else
778 /* most significant byte first */
779 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
780 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
781#endif
782 break;
783 case KVM_REG_MIPS_MSA_IR:
784 if (!kvm_mips_guest_has_msa(&vcpu->arch))
785 return -EINVAL;
786 v = boot_cpu_data.msa_id;
787 break;
788 case KVM_REG_MIPS_MSA_CSR:
789 if (!kvm_mips_guest_has_msa(&vcpu->arch))
790 return -EINVAL;
791 v = fpu->msacsr;
792 break;
793
James Hogan379245c2014-12-02 15:48:24 +0000794 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700795 case KVM_REG_MIPS_CP0_INDEX:
796 v = (long)kvm_read_c0_guest_index(cop0);
797 break;
798 case KVM_REG_MIPS_CP0_CONTEXT:
799 v = (long)kvm_read_c0_guest_context(cop0);
800 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100801 case KVM_REG_MIPS_CP0_USERLOCAL:
802 v = (long)kvm_read_c0_guest_userlocal(cop0);
803 break;
David Daney4c73fb22013-05-23 09:49:09 -0700804 case KVM_REG_MIPS_CP0_PAGEMASK:
805 v = (long)kvm_read_c0_guest_pagemask(cop0);
806 break;
807 case KVM_REG_MIPS_CP0_WIRED:
808 v = (long)kvm_read_c0_guest_wired(cop0);
809 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100810 case KVM_REG_MIPS_CP0_HWRENA:
811 v = (long)kvm_read_c0_guest_hwrena(cop0);
812 break;
David Daney4c73fb22013-05-23 09:49:09 -0700813 case KVM_REG_MIPS_CP0_BADVADDR:
814 v = (long)kvm_read_c0_guest_badvaddr(cop0);
815 break;
816 case KVM_REG_MIPS_CP0_ENTRYHI:
817 v = (long)kvm_read_c0_guest_entryhi(cop0);
818 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100819 case KVM_REG_MIPS_CP0_COMPARE:
820 v = (long)kvm_read_c0_guest_compare(cop0);
821 break;
David Daney4c73fb22013-05-23 09:49:09 -0700822 case KVM_REG_MIPS_CP0_STATUS:
823 v = (long)kvm_read_c0_guest_status(cop0);
824 break;
825 case KVM_REG_MIPS_CP0_CAUSE:
826 v = (long)kvm_read_c0_guest_cause(cop0);
827 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100828 case KVM_REG_MIPS_CP0_EPC:
829 v = (long)kvm_read_c0_guest_epc(cop0);
830 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100831 case KVM_REG_MIPS_CP0_PRID:
832 v = (long)kvm_read_c0_guest_prid(cop0);
833 break;
David Daney4c73fb22013-05-23 09:49:09 -0700834 case KVM_REG_MIPS_CP0_CONFIG:
835 v = (long)kvm_read_c0_guest_config(cop0);
836 break;
837 case KVM_REG_MIPS_CP0_CONFIG1:
838 v = (long)kvm_read_c0_guest_config1(cop0);
839 break;
840 case KVM_REG_MIPS_CP0_CONFIG2:
841 v = (long)kvm_read_c0_guest_config2(cop0);
842 break;
843 case KVM_REG_MIPS_CP0_CONFIG3:
844 v = (long)kvm_read_c0_guest_config3(cop0);
845 break;
James Hoganc7716072014-06-26 15:11:29 +0100846 case KVM_REG_MIPS_CP0_CONFIG4:
847 v = (long)kvm_read_c0_guest_config4(cop0);
848 break;
849 case KVM_REG_MIPS_CP0_CONFIG5:
850 v = (long)kvm_read_c0_guest_config5(cop0);
851 break;
David Daney4c73fb22013-05-23 09:49:09 -0700852 case KVM_REG_MIPS_CP0_CONFIG7:
853 v = (long)kvm_read_c0_guest_config7(cop0);
854 break;
James Hogane93d4c12014-06-26 13:47:22 +0100855 case KVM_REG_MIPS_CP0_ERROREPC:
856 v = (long)kvm_read_c0_guest_errorepc(cop0);
857 break;
James Hogan05108702016-06-15 19:29:56 +0100858 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
859 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
860 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
861 return -EINVAL;
862 switch (idx) {
863 case 2:
864 v = (long)kvm_read_c0_guest_kscratch1(cop0);
865 break;
866 case 3:
867 v = (long)kvm_read_c0_guest_kscratch2(cop0);
868 break;
869 case 4:
870 v = (long)kvm_read_c0_guest_kscratch3(cop0);
871 break;
872 case 5:
873 v = (long)kvm_read_c0_guest_kscratch4(cop0);
874 break;
875 case 6:
876 v = (long)kvm_read_c0_guest_kscratch5(cop0);
877 break;
878 case 7:
879 v = (long)kvm_read_c0_guest_kscratch6(cop0);
880 break;
881 }
882 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100883 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100884 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100885 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
886 if (ret)
887 return ret;
888 break;
David Daney4c73fb22013-05-23 09:49:09 -0700889 }
David Daney681865d2013-06-10 12:33:48 -0700890 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
891 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700892
David Daney681865d2013-06-10 12:33:48 -0700893 return put_user(v, uaddr64);
894 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
895 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
896 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700897
David Daney681865d2013-06-10 12:33:48 -0700898 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000899 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
900 void __user *uaddr = (void __user *)(long)reg->addr;
901
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200902 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700903 } else {
904 return -EINVAL;
905 }
David Daney4c73fb22013-05-23 09:49:09 -0700906}
907
908static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
909 const struct kvm_one_reg *reg)
910{
David Daney4c73fb22013-05-23 09:49:09 -0700911 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000912 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
913 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000914 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000915 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700916
David Daney681865d2013-06-10 12:33:48 -0700917 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
918 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
919
920 if (get_user(v, uaddr64) != 0)
921 return -EFAULT;
922 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
923 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
924 s32 v32;
925
926 if (get_user(v32, uaddr32) != 0)
927 return -EFAULT;
928 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000929 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
930 void __user *uaddr = (void __user *)(long)reg->addr;
931
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200932 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700933 } else {
934 return -EINVAL;
935 }
David Daney4c73fb22013-05-23 09:49:09 -0700936
937 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000938 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700939 case KVM_REG_MIPS_R0:
940 /* Silently ignore requests to set $0 */
941 break;
942 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
943 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
944 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100945#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700946 case KVM_REG_MIPS_HI:
947 vcpu->arch.hi = v;
948 break;
949 case KVM_REG_MIPS_LO:
950 vcpu->arch.lo = v;
951 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100952#endif
David Daney4c73fb22013-05-23 09:49:09 -0700953 case KVM_REG_MIPS_PC:
954 vcpu->arch.pc = v;
955 break;
956
James Hogan379245c2014-12-02 15:48:24 +0000957 /* Floating point registers */
958 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
959 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
960 return -EINVAL;
961 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
962 /* Odd singles in top of even double when FR=0 */
963 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
964 set_fpr32(&fpu->fpr[idx], 0, v);
965 else
966 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
967 break;
968 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
969 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
970 return -EINVAL;
971 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
972 /* Can't access odd doubles in FR=0 mode */
973 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
974 return -EINVAL;
975 set_fpr64(&fpu->fpr[idx], 0, v);
976 break;
977 case KVM_REG_MIPS_FCR_IR:
978 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
979 return -EINVAL;
980 /* Read-only */
981 break;
982 case KVM_REG_MIPS_FCR_CSR:
983 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
984 return -EINVAL;
985 fpu->fcr31 = v;
986 break;
987
James Hoganab86bd62014-12-02 15:48:24 +0000988 /* MIPS SIMD Architecture (MSA) registers */
989 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
990 if (!kvm_mips_guest_has_msa(&vcpu->arch))
991 return -EINVAL;
992 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
993#ifdef CONFIG_CPU_LITTLE_ENDIAN
994 /* least significant byte first */
995 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
996 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
997#else
998 /* most significant byte first */
999 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
1000 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
1001#endif
1002 break;
1003 case KVM_REG_MIPS_MSA_IR:
1004 if (!kvm_mips_guest_has_msa(&vcpu->arch))
1005 return -EINVAL;
1006 /* Read-only */
1007 break;
1008 case KVM_REG_MIPS_MSA_CSR:
1009 if (!kvm_mips_guest_has_msa(&vcpu->arch))
1010 return -EINVAL;
1011 fpu->msacsr = v;
1012 break;
1013
James Hogan379245c2014-12-02 15:48:24 +00001014 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -07001015 case KVM_REG_MIPS_CP0_INDEX:
1016 kvm_write_c0_guest_index(cop0, v);
1017 break;
1018 case KVM_REG_MIPS_CP0_CONTEXT:
1019 kvm_write_c0_guest_context(cop0, v);
1020 break;
James Hogan7767b7d2014-05-29 10:16:30 +01001021 case KVM_REG_MIPS_CP0_USERLOCAL:
1022 kvm_write_c0_guest_userlocal(cop0, v);
1023 break;
David Daney4c73fb22013-05-23 09:49:09 -07001024 case KVM_REG_MIPS_CP0_PAGEMASK:
1025 kvm_write_c0_guest_pagemask(cop0, v);
1026 break;
1027 case KVM_REG_MIPS_CP0_WIRED:
1028 kvm_write_c0_guest_wired(cop0, v);
1029 break;
James Hogan16fd5c12014-05-29 10:16:31 +01001030 case KVM_REG_MIPS_CP0_HWRENA:
1031 kvm_write_c0_guest_hwrena(cop0, v);
1032 break;
David Daney4c73fb22013-05-23 09:49:09 -07001033 case KVM_REG_MIPS_CP0_BADVADDR:
1034 kvm_write_c0_guest_badvaddr(cop0, v);
1035 break;
1036 case KVM_REG_MIPS_CP0_ENTRYHI:
1037 kvm_write_c0_guest_entryhi(cop0, v);
1038 break;
1039 case KVM_REG_MIPS_CP0_STATUS:
1040 kvm_write_c0_guest_status(cop0, v);
1041 break;
James Hoganfb6df0c2014-05-29 10:16:27 +01001042 case KVM_REG_MIPS_CP0_EPC:
1043 kvm_write_c0_guest_epc(cop0, v);
1044 break;
James Hogan1068eaa2014-06-26 13:56:52 +01001045 case KVM_REG_MIPS_CP0_PRID:
1046 kvm_write_c0_guest_prid(cop0, v);
1047 break;
David Daney4c73fb22013-05-23 09:49:09 -07001048 case KVM_REG_MIPS_CP0_ERROREPC:
1049 kvm_write_c0_guest_errorepc(cop0, v);
1050 break;
James Hogan05108702016-06-15 19:29:56 +01001051 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
1052 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
1053 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
1054 return -EINVAL;
1055 switch (idx) {
1056 case 2:
1057 kvm_write_c0_guest_kscratch1(cop0, v);
1058 break;
1059 case 3:
1060 kvm_write_c0_guest_kscratch2(cop0, v);
1061 break;
1062 case 4:
1063 kvm_write_c0_guest_kscratch3(cop0, v);
1064 break;
1065 case 5:
1066 kvm_write_c0_guest_kscratch4(cop0, v);
1067 break;
1068 case 6:
1069 kvm_write_c0_guest_kscratch5(cop0, v);
1070 break;
1071 case 7:
1072 kvm_write_c0_guest_kscratch6(cop0, v);
1073 break;
1074 }
1075 break;
James Hoganf8be02d2014-05-29 10:16:29 +01001076 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -07001077 default:
James Hogancc68d222016-06-15 19:29:48 +01001078 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -07001079 }
1080 return 0;
1081}
1082
James Hogan5fafd8742014-12-08 23:07:56 +00001083static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1084 struct kvm_enable_cap *cap)
1085{
1086 int r = 0;
1087
1088 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1089 return -EINVAL;
1090 if (cap->flags)
1091 return -EINVAL;
1092 if (cap->args[0])
1093 return -EINVAL;
1094
1095 switch (cap->cap) {
1096 case KVM_CAP_MIPS_FPU:
1097 vcpu->arch.fpu_enabled = true;
1098 break;
James Hogand952bd02014-12-08 23:07:56 +00001099 case KVM_CAP_MIPS_MSA:
1100 vcpu->arch.msa_enabled = true;
1101 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001102 default:
1103 r = -EINVAL;
1104 break;
1105 }
1106
1107 return r;
1108}
1109
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001110long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1111 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -08001112{
1113 struct kvm_vcpu *vcpu = filp->private_data;
1114 void __user *argp = (void __user *)arg;
1115 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001116
1117 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -07001118 case KVM_SET_ONE_REG:
1119 case KVM_GET_ONE_REG: {
1120 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001121
David Daney4c73fb22013-05-23 09:49:09 -07001122 if (copy_from_user(&reg, argp, sizeof(reg)))
1123 return -EFAULT;
1124 if (ioctl == KVM_SET_ONE_REG)
1125 return kvm_mips_set_reg(vcpu, &reg);
1126 else
1127 return kvm_mips_get_reg(vcpu, &reg);
1128 }
1129 case KVM_GET_REG_LIST: {
1130 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -07001131 struct kvm_reg_list reg_list;
1132 unsigned n;
1133
1134 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1135 return -EFAULT;
1136 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +01001137 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -07001138 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1139 return -EFAULT;
1140 if (n < reg_list.n)
1141 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +01001142 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -07001143 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001144 case KVM_NMI:
1145 /* Treat the NMI as a CPU reset */
1146 r = kvm_mips_reset_vcpu(vcpu);
1147 break;
1148 case KVM_INTERRUPT:
1149 {
1150 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001151
Sanjay Lal669e8462012-11-21 18:34:02 -08001152 r = -EFAULT;
1153 if (copy_from_user(&irq, argp, sizeof(irq)))
1154 goto out;
1155
Sanjay Lal669e8462012-11-21 18:34:02 -08001156 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1157 irq.irq);
1158
1159 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1160 break;
1161 }
James Hogan5fafd8742014-12-08 23:07:56 +00001162 case KVM_ENABLE_CAP: {
1163 struct kvm_enable_cap cap;
1164
1165 r = -EFAULT;
1166 if (copy_from_user(&cap, argp, sizeof(cap)))
1167 goto out;
1168 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1169 break;
1170 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001171 default:
David Daney4c73fb22013-05-23 09:49:09 -07001172 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001173 }
1174
1175out:
1176 return r;
1177}
1178
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001179/* Get (and clear) the dirty memory log for a memory slot. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001180int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1181{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001182 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001183 struct kvm_memory_slot *memslot;
1184 unsigned long ga, ga_end;
1185 int is_dirty = 0;
1186 int r;
1187 unsigned long n;
1188
1189 mutex_lock(&kvm->slots_lock);
1190
1191 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1192 if (r)
1193 goto out;
1194
1195 /* If nothing is dirty, don't bother messing with page tables. */
1196 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001197 slots = kvm_memslots(kvm);
1198 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001199
1200 ga = memslot->base_gfn << PAGE_SHIFT;
1201 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1202
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001203 kvm_info("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga,
1204 ga_end);
Sanjay Lal669e8462012-11-21 18:34:02 -08001205
1206 n = kvm_dirty_bitmap_bytes(memslot);
1207 memset(memslot->dirty_bitmap, 0, n);
1208 }
1209
1210 r = 0;
1211out:
1212 mutex_unlock(&kvm->slots_lock);
1213 return r;
1214
1215}
1216
1217long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1218{
1219 long r;
1220
1221 switch (ioctl) {
1222 default:
David Daneyed829852013-05-23 09:49:10 -07001223 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001224 }
1225
1226 return r;
1227}
1228
1229int kvm_arch_init(void *opaque)
1230{
Sanjay Lal669e8462012-11-21 18:34:02 -08001231 if (kvm_mips_callbacks) {
1232 kvm_err("kvm: module already exists\n");
1233 return -EEXIST;
1234 }
1235
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001236 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001237}
1238
1239void kvm_arch_exit(void)
1240{
1241 kvm_mips_callbacks = NULL;
1242}
1243
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001244int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1245 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001246{
David Daneyed829852013-05-23 09:49:10 -07001247 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001248}
1249
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001250int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1251 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001252{
David Daneyed829852013-05-23 09:49:10 -07001253 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001254}
1255
Dominik Dingel31928aa2014-12-04 15:47:07 +01001256void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001257{
Sanjay Lal669e8462012-11-21 18:34:02 -08001258}
1259
1260int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1261{
David Daneyed829852013-05-23 09:49:10 -07001262 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001263}
1264
1265int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1266{
David Daneyed829852013-05-23 09:49:10 -07001267 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001268}
1269
1270int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1271{
1272 return VM_FAULT_SIGBUS;
1273}
1274
Alexander Graf784aa3d2014-07-14 18:27:35 +02001275int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001276{
1277 int r;
1278
1279 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001280 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001281 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001282 r = 1;
1283 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001284 case KVM_CAP_COALESCED_MMIO:
1285 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1286 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001287 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001288 /* We don't handle systems with inconsistent cpu_has_fpu */
1289 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001290 break;
James Hogand952bd02014-12-08 23:07:56 +00001291 case KVM_CAP_MIPS_MSA:
1292 /*
1293 * We don't support MSA vector partitioning yet:
1294 * 1) It would require explicit support which can't be tested
1295 * yet due to lack of support in current hardware.
1296 * 2) It extends the state that would need to be saved/restored
1297 * by e.g. QEMU for migration.
1298 *
1299 * When vector partitioning hardware becomes available, support
1300 * could be added by requiring a flag when enabling
1301 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1302 * to save/restore the appropriate extra state.
1303 */
1304 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1305 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001306 default:
1307 r = 0;
1308 break;
1309 }
1310 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001311}
1312
1313int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1314{
1315 return kvm_mips_pending_timer(vcpu);
1316}
1317
1318int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1319{
1320 int i;
1321 struct mips_coproc *cop0;
1322
1323 if (!vcpu)
1324 return -1;
1325
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001326 kvm_debug("VCPU Register Dump:\n");
1327 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1328 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001329
1330 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001331 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001332 vcpu->arch.gprs[i],
1333 vcpu->arch.gprs[i + 1],
1334 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1335 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001336 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1337 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001338
1339 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001340 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1341 kvm_read_c0_guest_status(cop0),
1342 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001343
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001344 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001345
1346 return 0;
1347}
1348
1349int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1350{
1351 int i;
1352
David Daney8d17dd02013-05-23 09:49:08 -07001353 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001354 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001355 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001356 vcpu->arch.hi = regs->hi;
1357 vcpu->arch.lo = regs->lo;
1358 vcpu->arch.pc = regs->pc;
1359
David Daney4c73fb22013-05-23 09:49:09 -07001360 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001361}
1362
1363int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1364{
1365 int i;
1366
David Daney8d17dd02013-05-23 09:49:08 -07001367 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001368 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001369
1370 regs->hi = vcpu->arch.hi;
1371 regs->lo = vcpu->arch.lo;
1372 regs->pc = vcpu->arch.pc;
1373
David Daney4c73fb22013-05-23 09:49:09 -07001374 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001375}
1376
James Hogan0fae34f2014-05-29 10:16:39 +01001377static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001378{
1379 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1380
1381 kvm_mips_callbacks->queue_timer_int(vcpu);
1382
1383 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001384 if (swait_active(&vcpu->wq))
1385 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001386}
1387
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001388/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001389static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001390{
1391 struct kvm_vcpu *vcpu;
1392
1393 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1394 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001395 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001396}
1397
1398int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1399{
1400 kvm_mips_callbacks->vcpu_init(vcpu);
1401 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1402 HRTIMER_MODE_REL);
1403 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001404 return 0;
1405}
1406
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001407int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1408 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001409{
1410 return 0;
1411}
1412
1413/* Initial guest state */
1414int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1415{
1416 return kvm_mips_callbacks->vcpu_setup(vcpu);
1417}
1418
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001419static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001420{
James Hogan8cffd192016-06-09 14:19:08 +01001421 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001422
Sanjay Lal669e8462012-11-21 18:34:02 -08001423 if (cpu_has_dsp)
1424 status |= (ST0_MX);
1425
1426 write_c0_status(status);
1427 ehb();
1428}
1429
1430/*
1431 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1432 */
1433int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1434{
James Hogan8cffd192016-06-09 14:19:08 +01001435 u32 cause = vcpu->arch.host_cp0_cause;
1436 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1437 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001438 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1439 enum emulation_result er = EMULATE_DONE;
1440 int ret = RESUME_GUEST;
1441
James Hoganc4c6f2c2015-02-04 10:52:03 +00001442 /* re-enable HTW before enabling interrupts */
1443 htw_start();
1444
Sanjay Lal669e8462012-11-21 18:34:02 -08001445 /* Set a default exit reason */
1446 run->exit_reason = KVM_EXIT_UNKNOWN;
1447 run->ready_for_interrupt_injection = 1;
1448
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001449 /*
1450 * Set the appropriate status bits based on host CPU features,
1451 * before we hit the scheduler
1452 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001453 kvm_mips_set_c0_status();
1454
1455 local_irq_enable();
1456
1457 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1458 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001459 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001460
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001461 /*
1462 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001463 * causing an exception to be delivered to the Guest Kernel
1464 */
1465 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1466 if (er == EMULATE_PRIV_FAIL) {
1467 goto skip_emul;
1468 } else if (er == EMULATE_FAIL) {
1469 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1470 ret = RESUME_HOST;
1471 goto skip_emul;
1472 }
1473
1474 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001475 case EXCCODE_INT:
1476 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001477
1478 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001479
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001480 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001481 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001482
1483 ret = RESUME_GUEST;
1484 break;
1485
James Hogan16d100db2015-12-16 23:49:33 +00001486 case EXCCODE_CPU:
1487 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001488
1489 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001490 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1491 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001492 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001493 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001494 break;
1495
James Hogan16d100db2015-12-16 23:49:33 +00001496 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001497 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001498 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1499 break;
1500
James Hogan16d100db2015-12-16 23:49:33 +00001501 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001502 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1503 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1504 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001505
1506 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001507 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1508 break;
1509
James Hogan16d100db2015-12-16 23:49:33 +00001510 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001511 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1512 cause, opc, badvaddr);
1513
1514 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001515 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1516 break;
1517
James Hogan16d100db2015-12-16 23:49:33 +00001518 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001519 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001520 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1521 break;
1522
James Hogan16d100db2015-12-16 23:49:33 +00001523 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001524 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001525 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1526 break;
1527
James Hogan16d100db2015-12-16 23:49:33 +00001528 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001529 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001530 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1531 break;
1532
James Hogan16d100db2015-12-16 23:49:33 +00001533 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001534 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001535 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1536 break;
1537
James Hogan16d100db2015-12-16 23:49:33 +00001538 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001539 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001540 ret = kvm_mips_callbacks->handle_break(vcpu);
1541 break;
1542
James Hogan16d100db2015-12-16 23:49:33 +00001543 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001544 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001545 ret = kvm_mips_callbacks->handle_trap(vcpu);
1546 break;
1547
James Hogan16d100db2015-12-16 23:49:33 +00001548 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001549 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001550 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1551 break;
1552
James Hogan16d100db2015-12-16 23:49:33 +00001553 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001554 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001555 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1556 break;
1557
James Hogan16d100db2015-12-16 23:49:33 +00001558 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001559 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001560 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1561 break;
1562
Sanjay Lal669e8462012-11-21 18:34:02 -08001563 default:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001564 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
1565 exccode, opc, kvm_get_inst(opc, vcpu), badvaddr,
1566 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001567 kvm_arch_vcpu_dump_regs(vcpu);
1568 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1569 ret = RESUME_HOST;
1570 break;
1571
1572 }
1573
1574skip_emul:
1575 local_irq_disable();
1576
1577 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1578 kvm_mips_deliver_interrupts(vcpu, cause);
1579
1580 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001581 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001582 if (signal_pending(current)) {
1583 run->exit_reason = KVM_EXIT_INTR;
1584 ret = (-EINTR << 2) | RESUME_HOST;
1585 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001586 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001587 }
1588 }
1589
James Hogan98e91b82014-11-18 14:09:12 +00001590 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001591 trace_kvm_reenter(vcpu);
1592
James Hogan25b08c72016-09-16 00:06:43 +01001593 kvm_mips_check_asids(vcpu);
1594
James Hogan98e91b82014-11-18 14:09:12 +00001595 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001596 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1597 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001598 *
1599 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001600 * vector, as it may well cause an [MSA] FP exception if there
1601 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001602 * kvm_mips_csr_die_notifier() for how that is handled).
1603 */
1604 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1605 read_c0_status() & ST0_CU1)
1606 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001607
1608 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1609 read_c0_config5() & MIPS_CONF5_MSAEN)
1610 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001611 }
1612
James Hoganc4c6f2c2015-02-04 10:52:03 +00001613 /* Disable HTW before returning to guest or host */
1614 htw_stop();
1615
Sanjay Lal669e8462012-11-21 18:34:02 -08001616 return ret;
1617}
1618
James Hogan98e91b82014-11-18 14:09:12 +00001619/* Enable FPU for guest and restore context */
1620void kvm_own_fpu(struct kvm_vcpu *vcpu)
1621{
1622 struct mips_coproc *cop0 = vcpu->arch.cop0;
1623 unsigned int sr, cfg5;
1624
1625 preempt_disable();
1626
James Hogan539cb89fb2015-03-05 11:43:36 +00001627 sr = kvm_read_c0_guest_status(cop0);
1628
1629 /*
1630 * If MSA state is already live, it is undefined how it interacts with
1631 * FR=0 FPU state, and we don't want to hit reserved instruction
1632 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1633 * play it safe and save it first.
1634 *
1635 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1636 * get called when guest CU1 is set, however we can't trust the guest
1637 * not to clobber the status register directly via the commpage.
1638 */
1639 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001640 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001641 kvm_lose_fpu(vcpu);
1642
James Hogan98e91b82014-11-18 14:09:12 +00001643 /*
1644 * Enable FPU for guest
1645 * We set FR and FRE according to guest context
1646 */
James Hogan98e91b82014-11-18 14:09:12 +00001647 change_c0_status(ST0_CU1 | ST0_FR, sr);
1648 if (cpu_has_fre) {
1649 cfg5 = kvm_read_c0_guest_config5(cop0);
1650 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1651 }
1652 enable_fpu_hazard();
1653
1654 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001655 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001656 __kvm_restore_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, KVM_TRACE_AUX_FPU);
1659 } else {
1660 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001661 }
1662
1663 preempt_enable();
1664}
1665
James Hogan539cb89fb2015-03-05 11:43:36 +00001666#ifdef CONFIG_CPU_HAS_MSA
1667/* Enable MSA for guest and restore context */
1668void kvm_own_msa(struct kvm_vcpu *vcpu)
1669{
1670 struct mips_coproc *cop0 = vcpu->arch.cop0;
1671 unsigned int sr, cfg5;
1672
1673 preempt_disable();
1674
1675 /*
1676 * Enable FPU if enabled in guest, since we're restoring FPU context
1677 * anyway. We set FR and FRE according to guest context.
1678 */
1679 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1680 sr = kvm_read_c0_guest_status(cop0);
1681
1682 /*
1683 * If FR=0 FPU state is already live, it is undefined how it
1684 * interacts with MSA state, so play it safe and save it first.
1685 */
1686 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001687 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1688 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001689 kvm_lose_fpu(vcpu);
1690
1691 change_c0_status(ST0_CU1 | ST0_FR, sr);
1692 if (sr & ST0_CU1 && cpu_has_fre) {
1693 cfg5 = kvm_read_c0_guest_config5(cop0);
1694 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1695 }
1696 }
1697
1698 /* Enable MSA for guest */
1699 set_c0_config5(MIPS_CONF5_MSAEN);
1700 enable_fpu_hazard();
1701
James Hoganf9431762016-06-14 09:40:10 +01001702 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1703 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001704 /*
1705 * Guest FPU state already loaded, only restore upper MSA state
1706 */
1707 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001708 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001709 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001710 break;
1711 case 0:
1712 /* Neither FPU or MSA already active, restore full MSA state */
1713 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001714 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001715 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001716 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001717 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1718 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001719 break;
1720 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001721 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001722 break;
1723 }
1724
1725 preempt_enable();
1726}
1727#endif
1728
1729/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001730void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1731{
1732 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001733 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001734 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001735 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001736 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001737 }
James Hoganf9431762016-06-14 09:40:10 +01001738 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001739 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001740 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001741 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001742 }
1743 preempt_enable();
1744}
1745
James Hogan539cb89fb2015-03-05 11:43:36 +00001746/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001747void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1748{
1749 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001750 * FPU & MSA get disabled in root context (hardware) when it is disabled
1751 * in guest context (software), but the register state in the hardware
1752 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001753 * before saving.
1754 */
1755
1756 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001757 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001758 set_c0_config5(MIPS_CONF5_MSAEN);
1759 enable_fpu_hazard();
1760
1761 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001762 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001763
1764 /* Disable MSA & FPU */
1765 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001766 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001767 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001768 disable_fpu_hazard();
1769 }
James Hoganf9431762016-06-14 09:40:10 +01001770 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1771 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001772 set_c0_status(ST0_CU1);
1773 enable_fpu_hazard();
1774
1775 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001776 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001777 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001778
1779 /* Disable FPU */
1780 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001781 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001782 }
1783 preempt_enable();
1784}
1785
1786/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001787 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1788 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1789 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001790 */
1791static int kvm_mips_csr_die_notify(struct notifier_block *self,
1792 unsigned long cmd, void *ptr)
1793{
1794 struct die_args *args = (struct die_args *)ptr;
1795 struct pt_regs *regs = args->regs;
1796 unsigned long pc;
1797
James Hogan539cb89fb2015-03-05 11:43:36 +00001798 /* Only interested in FPE and MSAFPE */
1799 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001800 return NOTIFY_DONE;
1801
1802 /* Return immediately if guest context isn't active */
1803 if (!(current->flags & PF_VCPU))
1804 return NOTIFY_DONE;
1805
1806 /* Should never get here from user mode */
1807 BUG_ON(user_mode(regs));
1808
1809 pc = instruction_pointer(regs);
1810 switch (cmd) {
1811 case DIE_FP:
1812 /* match 2nd instruction in __kvm_restore_fcsr */
1813 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1814 return NOTIFY_DONE;
1815 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001816 case DIE_MSAFP:
1817 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1818 if (!cpu_has_msa ||
1819 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1820 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1821 return NOTIFY_DONE;
1822 break;
James Hogan98e91b82014-11-18 14:09:12 +00001823 }
1824
1825 /* Move PC forward a little and continue executing */
1826 instruction_pointer(regs) += 4;
1827
1828 return NOTIFY_STOP;
1829}
1830
1831static struct notifier_block kvm_mips_csr_die_notifier = {
1832 .notifier_call = kvm_mips_csr_die_notify,
1833};
1834
James Hogan2db9d232015-12-16 23:49:32 +00001835static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001836{
1837 int ret;
1838
James Hogan1e5217f52016-06-23 17:34:45 +01001839 ret = kvm_mips_entry_setup();
1840 if (ret)
1841 return ret;
1842
Sanjay Lal669e8462012-11-21 18:34:02 -08001843 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1844
1845 if (ret)
1846 return ret;
1847
James Hogan98e91b82014-11-18 14:09:12 +00001848 register_die_notifier(&kvm_mips_csr_die_notifier);
1849
Sanjay Lal669e8462012-11-21 18:34:02 -08001850 return 0;
1851}
1852
James Hogan2db9d232015-12-16 23:49:32 +00001853static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001854{
1855 kvm_exit();
1856
James Hogan98e91b82014-11-18 14:09:12 +00001857 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001858}
1859
1860module_init(kvm_mips_init);
1861module_exit(kvm_mips_exit);
1862
1863EXPORT_TRACEPOINT_SYMBOL(kvm_exit);