blob: 475c4cc78bd6126e219f4345c60fb5f412a80150 [file] [log] [blame]
Sanjay Lal669e8462012-11-21 18:34:02 -08001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * KVM/MIPS: MIPS specific KVM APIs
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070010 */
Sanjay Lal669e8462012-11-21 18:34:02 -080011
James Hogan05108702016-06-15 19:29:56 +010012#include <linux/bitops.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080013#include <linux/errno.h>
14#include <linux/err.h>
James Hogan98e91b82014-11-18 14:09:12 +000015#include <linux/kdebug.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080016#include <linux/module.h>
James Hogand852b5f2016-10-19 00:24:27 +010017#include <linux/uaccess.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080018#include <linux/vmalloc.h>
19#include <linux/fs.h>
20#include <linux/bootmem.h>
James Hoganf7982172015-02-04 17:06:37 +000021#include <asm/fpu.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080022#include <asm/page.h>
23#include <asm/cacheflush.h>
24#include <asm/mmu_context.h>
James Hogan06c158c2015-05-01 13:50:18 +010025#include <asm/pgalloc.h>
James Hoganc4c6f2c2015-02-04 10:52:03 +000026#include <asm/pgtable.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080027
28#include <linux/kvm_host.h>
29
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070030#include "interrupt.h"
31#include "commpage.h"
Sanjay Lal669e8462012-11-21 18:34:02 -080032
33#define CREATE_TRACE_POINTS
34#include "trace.h"
35
36#ifndef VECTORSPACING
37#define VECTORSPACING 0x100 /* for EI/VI mode */
38#endif
39
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070040#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
Sanjay Lal669e8462012-11-21 18:34:02 -080041struct kvm_stats_debugfs_item debugfs_entries[] = {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070042 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
43 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
44 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
45 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
46 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
47 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
48 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
49 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
50 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
51 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
52 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
53 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
54 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
James Hogan0a560422015-02-06 16:03:57 +000055 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000056 { "msa_fpe", VCPU_STAT(msa_fpe_exits), KVM_STAT_VCPU },
James Hogan1c0cd662015-02-06 10:56:27 +000057 { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000058 { "msa_disabled", VCPU_STAT(msa_disabled_exits), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070059 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
Paolo Bonzinif7819512015-02-04 18:20:58 +010060 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +020061 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), KVM_STAT_VCPU },
Christian Borntraeger3491caf2016-05-13 12:16:35 +020062 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070063 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
Sanjay Lal669e8462012-11-21 18:34:02 -080064 {NULL}
65};
66
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070067/*
68 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
69 * Config7, so we are "runnable" if interrupts are pending
Sanjay Lal669e8462012-11-21 18:34:02 -080070 */
71int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
72{
73 return !!(vcpu->arch.pending_exceptions);
74}
75
76int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
77{
78 return 1;
79}
80
Radim Krčmář13a34e02014-08-28 15:13:03 +020081int kvm_arch_hardware_enable(void)
Sanjay Lal669e8462012-11-21 18:34:02 -080082{
83 return 0;
84}
85
Sanjay Lal669e8462012-11-21 18:34:02 -080086int kvm_arch_hardware_setup(void)
87{
88 return 0;
89}
90
Sanjay Lal669e8462012-11-21 18:34:02 -080091void kvm_arch_check_processor_compat(void *rtn)
92{
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -070093 *(int *)rtn = 0;
Sanjay Lal669e8462012-11-21 18:34:02 -080094}
95
Sanjay Lal669e8462012-11-21 18:34:02 -080096int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
97{
James Hogan06c158c2015-05-01 13:50:18 +010098 /* Allocate page table to map GPA -> RPA */
99 kvm->arch.gpa_mm.pgd = kvm_pgd_alloc();
100 if (!kvm->arch.gpa_mm.pgd)
101 return -ENOMEM;
102
Sanjay Lal669e8462012-11-21 18:34:02 -0800103 return 0;
104}
105
Luiz Capitulino235539b2016-09-07 14:47:23 -0400106bool kvm_arch_has_vcpu_debugfs(void)
107{
108 return false;
109}
110
111int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
112{
113 return 0;
114}
115
Sanjay Lal669e8462012-11-21 18:34:02 -0800116void kvm_mips_free_vcpus(struct kvm *kvm)
117{
118 unsigned int i;
119 struct kvm_vcpu *vcpu;
120
Sanjay Lal669e8462012-11-21 18:34:02 -0800121 kvm_for_each_vcpu(i, vcpu, kvm) {
122 kvm_arch_vcpu_free(vcpu);
123 }
124
125 mutex_lock(&kvm->lock);
126
127 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
128 kvm->vcpus[i] = NULL;
129
130 atomic_set(&kvm->online_vcpus, 0);
131
132 mutex_unlock(&kvm->lock);
133}
134
James Hogan06c158c2015-05-01 13:50:18 +0100135static void kvm_mips_free_gpa_pt(struct kvm *kvm)
136{
137 /* It should always be safe to remove after flushing the whole range */
138 WARN_ON(!kvm_mips_flush_gpa_pt(kvm, 0, ~0));
139 pgd_free(NULL, kvm->arch.gpa_mm.pgd);
140}
141
Sanjay Lal669e8462012-11-21 18:34:02 -0800142void kvm_arch_destroy_vm(struct kvm *kvm)
143{
144 kvm_mips_free_vcpus(kvm);
James Hogan06c158c2015-05-01 13:50:18 +0100145 kvm_mips_free_gpa_pt(kvm);
Sanjay Lal669e8462012-11-21 18:34:02 -0800146}
147
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700148long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
149 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800150{
David Daneyed829852013-05-23 09:49:10 -0700151 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800152}
153
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530154int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
155 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800156{
157 return 0;
158}
159
James Hoganb6209112016-10-25 00:01:37 +0100160void kvm_arch_flush_shadow_all(struct kvm *kvm)
161{
162 /* Flush whole GPA */
163 kvm_mips_flush_gpa_pt(kvm, 0, ~0);
164
165 /* Let implementation do the rest */
166 kvm_mips_callbacks->flush_shadow_all(kvm);
167}
168
169void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
170 struct kvm_memory_slot *slot)
171{
172 /*
173 * The slot has been made invalid (ready for moving or deletion), so we
174 * need to ensure that it can no longer be accessed by any guest VCPUs.
175 */
176
177 spin_lock(&kvm->mmu_lock);
178 /* Flush slot from GPA */
179 kvm_mips_flush_gpa_pt(kvm, slot->base_gfn,
180 slot->base_gfn + slot->npages - 1);
181 /* Let implementation do the rest */
182 kvm_mips_callbacks->flush_shadow_memslot(kvm, slot);
183 spin_unlock(&kvm->mmu_lock);
184}
185
Sanjay Lal669e8462012-11-21 18:34:02 -0800186int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700187 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200188 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700189 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800190{
191 return 0;
192}
193
194void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200195 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700196 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200197 const struct kvm_memory_slot *new,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700198 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800199{
James Hogana1ac9e12016-12-06 14:56:20 +0000200 int needs_flush;
201
Sanjay Lal669e8462012-11-21 18:34:02 -0800202 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
203 __func__, kvm, mem->slot, mem->guest_phys_addr,
204 mem->memory_size, mem->userspace_addr);
James Hogana1ac9e12016-12-06 14:56:20 +0000205
206 /*
207 * If dirty page logging is enabled, write protect all pages in the slot
208 * ready for dirty logging.
209 *
210 * There is no need to do this in any of the following cases:
211 * CREATE: No dirty mappings will already exist.
212 * MOVE/DELETE: The old mappings will already have been cleaned up by
213 * kvm_arch_flush_shadow_memslot()
214 */
215 if (change == KVM_MR_FLAGS_ONLY &&
216 (!(old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
217 new->flags & KVM_MEM_LOG_DIRTY_PAGES)) {
218 spin_lock(&kvm->mmu_lock);
219 /* Write protect GPA page table entries */
220 needs_flush = kvm_mips_mkclean_gpa_pt(kvm, new->base_gfn,
221 new->base_gfn + new->npages - 1);
222 /* Let implementation do the rest */
223 if (needs_flush)
224 kvm_mips_callbacks->flush_shadow_memslot(kvm, new);
225 spin_unlock(&kvm->mmu_lock);
226 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800227}
228
James Hogand7b8f892016-06-23 17:34:40 +0100229static inline void dump_handler(const char *symbol, void *start, void *end)
230{
231 u32 *p;
232
233 pr_debug("LEAF(%s)\n", symbol);
234
235 pr_debug("\t.set push\n");
236 pr_debug("\t.set noreorder\n");
237
238 for (p = start; p < (u32 *)end; ++p)
239 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p);
240
241 pr_debug("\t.set\tpop\n");
242
243 pr_debug("\tEND(%s)\n", symbol);
244}
245
Sanjay Lal669e8462012-11-21 18:34:02 -0800246struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
247{
James Hogan90e93112016-06-23 17:34:39 +0100248 int err, size;
James Hogana7cfa7a2016-09-10 23:56:46 +0100249 void *gebase, *p, *handler, *refill_start, *refill_end;
Sanjay Lal669e8462012-11-21 18:34:02 -0800250 int i;
251
252 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
253
254 if (!vcpu) {
255 err = -ENOMEM;
256 goto out;
257 }
258
259 err = kvm_vcpu_init(vcpu, kvm, id);
260
261 if (err)
262 goto out_free_cpu;
263
James Hogan6e95bfd2014-05-29 10:16:43 +0100264 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800265
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700266 /*
267 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800268 * guest mode exits
269 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700270 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800271 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700272 else
James Hogan7006e2d2014-05-29 10:16:23 +0100273 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800274
Sanjay Lal669e8462012-11-21 18:34:02 -0800275 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
276
277 if (!gebase) {
278 err = -ENOMEM;
James Hogan585bb8f2015-11-11 14:21:20 +0000279 goto out_uninit_cpu;
Sanjay Lal669e8462012-11-21 18:34:02 -0800280 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100281 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
282 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800283
James Hogan2a06dab2016-07-08 11:53:26 +0100284 /*
285 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
286 * limits us to the low 512MB of physical address space. If the memory
287 * we allocate is out of range, just give up now.
288 */
289 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) {
290 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
291 gebase);
292 err = -ENOMEM;
293 goto out_free_gebase;
294 }
295
Sanjay Lal669e8462012-11-21 18:34:02 -0800296 /* Save new ebase */
297 vcpu->arch.guest_ebase = gebase;
298
James Hogan90e93112016-06-23 17:34:39 +0100299 /* Build guest exception vectors dynamically in unmapped memory */
James Hogan1f9ca622016-06-23 17:34:46 +0100300 handler = gebase + 0x2000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800301
James Hogana7cfa7a2016-09-10 23:56:46 +0100302 /* TLB refill */
303 refill_start = gebase;
304 refill_end = kvm_mips_build_tlb_refill_exception(refill_start, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800305
306 /* General Exception Entry point */
James Hogan1f9ca622016-06-23 17:34:46 +0100307 kvm_mips_build_exception(gebase + 0x180, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800308
309 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
310 for (i = 0; i < 8; i++) {
311 kvm_debug("L1 Vectored handler @ %p\n",
312 gebase + 0x200 + (i * VECTORSPACING));
James Hogan1f9ca622016-06-23 17:34:46 +0100313 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
314 handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800315 }
316
James Hogan90e93112016-06-23 17:34:39 +0100317 /* General exit handler */
James Hogan1f9ca622016-06-23 17:34:46 +0100318 p = handler;
James Hogan90e93112016-06-23 17:34:39 +0100319 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800320
James Hogan90e93112016-06-23 17:34:39 +0100321 /* Guest entry routine */
322 vcpu->arch.vcpu_run = p;
323 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100324
James Hogand7b8f892016-06-23 17:34:40 +0100325 /* Dump the generated code */
326 pr_debug("#include <asm/asm.h>\n");
327 pr_debug("#include <asm/regdef.h>\n");
328 pr_debug("\n");
329 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
James Hogana7cfa7a2016-09-10 23:56:46 +0100330 dump_handler("kvm_tlb_refill", refill_start, refill_end);
James Hogand7b8f892016-06-23 17:34:40 +0100331 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
332 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
333
Sanjay Lal669e8462012-11-21 18:34:02 -0800334 /* Invalidate the icache for these ranges */
James Hogan32eb12a2017-01-03 17:43:01 +0000335 flush_icache_range((unsigned long)gebase,
336 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800337
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700338 /*
339 * Allocate comm page for guest kernel, a TLB will be reserved for
340 * mapping GVA @ 0xFFFF8000 to this page
341 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800342 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
343
344 if (!vcpu->arch.kseg0_commpage) {
345 err = -ENOMEM;
346 goto out_free_gebase;
347 }
348
James Hogan6e95bfd2014-05-29 10:16:43 +0100349 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800350 kvm_mips_commpage_init(vcpu);
351
352 /* Init */
353 vcpu->arch.last_sched_cpu = -1;
354
355 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100356 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800357
358 return vcpu;
359
360out_free_gebase:
361 kfree(gebase);
362
James Hogan585bb8f2015-11-11 14:21:20 +0000363out_uninit_cpu:
364 kvm_vcpu_uninit(vcpu);
365
Sanjay Lal669e8462012-11-21 18:34:02 -0800366out_free_cpu:
367 kfree(vcpu);
368
369out:
370 return ERR_PTR(err);
371}
372
373void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
374{
375 hrtimer_cancel(&vcpu->arch.comparecount_timer);
376
377 kvm_vcpu_uninit(vcpu);
378
379 kvm_mips_dump_stats(vcpu);
380
James Hoganaba859292016-12-16 15:57:00 +0000381 kvm_mmu_free_memory_caches(vcpu);
James Hoganc6c0a662014-05-29 10:16:44 +0100382 kfree(vcpu->arch.guest_ebase);
383 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700384 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800385}
386
387void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
388{
389 kvm_arch_vcpu_free(vcpu);
390}
391
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700392int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
393 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800394{
David Daneyed829852013-05-23 09:49:10 -0700395 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800396}
397
398int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
399{
400 int r = 0;
401 sigset_t sigsaved;
402
403 if (vcpu->sigset_active)
404 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
405
406 if (vcpu->mmio_needed) {
407 if (!vcpu->mmio_is_write)
408 kvm_mips_complete_mmio_load(vcpu, run);
409 vcpu->mmio_needed = 0;
410 }
411
James Hoganf7982172015-02-04 17:06:37 +0000412 lose_fpu(1);
413
James Hogan044f0f02014-05-29 10:16:32 +0100414 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200415 guest_enter_irqoff();
James Hogan93258602016-06-14 09:40:14 +0100416 trace_kvm_enter(vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100417
James Hogan4841e0d2016-11-28 22:45:04 +0000418 /*
419 * Make sure the read of VCPU requests in vcpu_run() callback is not
420 * reordered ahead of the write to vcpu->mode, or we could miss a TLB
421 * flush request while the requester sees the VCPU as outside of guest
422 * mode and not needing an IPI.
423 */
424 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
425
James Hogana2c046e2016-11-18 13:14:37 +0000426 r = kvm_mips_callbacks->vcpu_run(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100427
James Hogan93258602016-06-14 09:40:14 +0100428 trace_kvm_out(vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200429 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800430 local_irq_enable();
431
432 if (vcpu->sigset_active)
433 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
434
435 return r;
436}
437
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700438int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
439 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800440{
441 int intr = (int)irq->irq;
442 struct kvm_vcpu *dvcpu = NULL;
443
444 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
445 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
446 (int)intr);
447
448 if (irq->cpu == -1)
449 dvcpu = vcpu;
450 else
451 dvcpu = vcpu->kvm->vcpus[irq->cpu];
452
453 if (intr == 2 || intr == 3 || intr == 4) {
454 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
455
456 } else if (intr == -2 || intr == -3 || intr == -4) {
457 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
458 } else {
459 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
460 irq->cpu, irq->irq);
461 return -EINVAL;
462 }
463
464 dvcpu->arch.wait = 0;
465
Marcelo Tosatti85773702016-02-19 09:46:39 +0100466 if (swait_active(&dvcpu->wq))
467 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800468
469 return 0;
470}
471
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700472int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
473 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800474{
David Daneyed829852013-05-23 09:49:10 -0700475 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800476}
477
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700478int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
479 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800480{
David Daneyed829852013-05-23 09:49:10 -0700481 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800482}
483
David Daney4c73fb22013-05-23 09:49:09 -0700484static u64 kvm_mips_get_one_regs[] = {
485 KVM_REG_MIPS_R0,
486 KVM_REG_MIPS_R1,
487 KVM_REG_MIPS_R2,
488 KVM_REG_MIPS_R3,
489 KVM_REG_MIPS_R4,
490 KVM_REG_MIPS_R5,
491 KVM_REG_MIPS_R6,
492 KVM_REG_MIPS_R7,
493 KVM_REG_MIPS_R8,
494 KVM_REG_MIPS_R9,
495 KVM_REG_MIPS_R10,
496 KVM_REG_MIPS_R11,
497 KVM_REG_MIPS_R12,
498 KVM_REG_MIPS_R13,
499 KVM_REG_MIPS_R14,
500 KVM_REG_MIPS_R15,
501 KVM_REG_MIPS_R16,
502 KVM_REG_MIPS_R17,
503 KVM_REG_MIPS_R18,
504 KVM_REG_MIPS_R19,
505 KVM_REG_MIPS_R20,
506 KVM_REG_MIPS_R21,
507 KVM_REG_MIPS_R22,
508 KVM_REG_MIPS_R23,
509 KVM_REG_MIPS_R24,
510 KVM_REG_MIPS_R25,
511 KVM_REG_MIPS_R26,
512 KVM_REG_MIPS_R27,
513 KVM_REG_MIPS_R28,
514 KVM_REG_MIPS_R29,
515 KVM_REG_MIPS_R30,
516 KVM_REG_MIPS_R31,
517
James Hogan70e92c7e2016-07-04 19:35:11 +0100518#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700519 KVM_REG_MIPS_HI,
520 KVM_REG_MIPS_LO,
James Hogan70e92c7e2016-07-04 19:35:11 +0100521#endif
David Daney4c73fb22013-05-23 09:49:09 -0700522 KVM_REG_MIPS_PC,
523
524 KVM_REG_MIPS_CP0_INDEX,
525 KVM_REG_MIPS_CP0_CONTEXT,
James Hogan7767b7d2014-05-29 10:16:30 +0100526 KVM_REG_MIPS_CP0_USERLOCAL,
David Daney4c73fb22013-05-23 09:49:09 -0700527 KVM_REG_MIPS_CP0_PAGEMASK,
528 KVM_REG_MIPS_CP0_WIRED,
James Hogan16fd5c12014-05-29 10:16:31 +0100529 KVM_REG_MIPS_CP0_HWRENA,
David Daney4c73fb22013-05-23 09:49:09 -0700530 KVM_REG_MIPS_CP0_BADVADDR,
James Hoganf8be02d2014-05-29 10:16:29 +0100531 KVM_REG_MIPS_CP0_COUNT,
David Daney4c73fb22013-05-23 09:49:09 -0700532 KVM_REG_MIPS_CP0_ENTRYHI,
James Hoganf8be02d2014-05-29 10:16:29 +0100533 KVM_REG_MIPS_CP0_COMPARE,
David Daney4c73fb22013-05-23 09:49:09 -0700534 KVM_REG_MIPS_CP0_STATUS,
535 KVM_REG_MIPS_CP0_CAUSE,
James Hoganfb6df0c2014-05-29 10:16:27 +0100536 KVM_REG_MIPS_CP0_EPC,
James Hogan1068eaa2014-06-26 13:56:52 +0100537 KVM_REG_MIPS_CP0_PRID,
David Daney4c73fb22013-05-23 09:49:09 -0700538 KVM_REG_MIPS_CP0_CONFIG,
539 KVM_REG_MIPS_CP0_CONFIG1,
540 KVM_REG_MIPS_CP0_CONFIG2,
541 KVM_REG_MIPS_CP0_CONFIG3,
James Hoganc7716072014-06-26 15:11:29 +0100542 KVM_REG_MIPS_CP0_CONFIG4,
543 KVM_REG_MIPS_CP0_CONFIG5,
David Daney4c73fb22013-05-23 09:49:09 -0700544 KVM_REG_MIPS_CP0_CONFIG7,
James Hoganf8239342014-05-29 10:16:37 +0100545 KVM_REG_MIPS_CP0_ERROREPC,
546
547 KVM_REG_MIPS_COUNT_CTL,
548 KVM_REG_MIPS_COUNT_RESUME,
James Hoganf74a8e22014-05-29 10:16:38 +0100549 KVM_REG_MIPS_COUNT_HZ,
David Daney4c73fb22013-05-23 09:49:09 -0700550};
551
James Hogane5775932016-06-15 19:29:51 +0100552static u64 kvm_mips_get_one_regs_fpu[] = {
553 KVM_REG_MIPS_FCR_IR,
554 KVM_REG_MIPS_FCR_CSR,
555};
556
557static u64 kvm_mips_get_one_regs_msa[] = {
558 KVM_REG_MIPS_MSA_IR,
559 KVM_REG_MIPS_MSA_CSR,
560};
561
James Hogan05108702016-06-15 19:29:56 +0100562static u64 kvm_mips_get_one_regs_kscratch[] = {
563 KVM_REG_MIPS_CP0_KSCRATCH1,
564 KVM_REG_MIPS_CP0_KSCRATCH2,
565 KVM_REG_MIPS_CP0_KSCRATCH3,
566 KVM_REG_MIPS_CP0_KSCRATCH4,
567 KVM_REG_MIPS_CP0_KSCRATCH5,
568 KVM_REG_MIPS_CP0_KSCRATCH6,
569};
570
James Hoganf5c43bd2016-06-15 19:29:49 +0100571static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
572{
573 unsigned long ret;
574
575 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100576 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
577 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
578 /* odd doubles */
579 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
580 ret += 16;
581 }
582 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
583 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hogan05108702016-06-15 19:29:56 +0100584 ret += __arch_hweight8(vcpu->arch.kscratch_enabled);
James Hoganf5c43bd2016-06-15 19:29:49 +0100585 ret += kvm_mips_callbacks->num_regs(vcpu);
586
587 return ret;
588}
589
590static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
591{
James Hogane5775932016-06-15 19:29:51 +0100592 u64 index;
593 unsigned int i;
594
James Hoganf5c43bd2016-06-15 19:29:49 +0100595 if (copy_to_user(indices, kvm_mips_get_one_regs,
596 sizeof(kvm_mips_get_one_regs)))
597 return -EFAULT;
598 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
599
James Hogane5775932016-06-15 19:29:51 +0100600 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
601 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
602 sizeof(kvm_mips_get_one_regs_fpu)))
603 return -EFAULT;
604 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
605
606 for (i = 0; i < 32; ++i) {
607 index = KVM_REG_MIPS_FPR_32(i);
608 if (copy_to_user(indices, &index, sizeof(index)))
609 return -EFAULT;
610 ++indices;
611
612 /* skip odd doubles if no F64 */
613 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
614 continue;
615
616 index = KVM_REG_MIPS_FPR_64(i);
617 if (copy_to_user(indices, &index, sizeof(index)))
618 return -EFAULT;
619 ++indices;
620 }
621 }
622
623 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
624 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
625 sizeof(kvm_mips_get_one_regs_msa)))
626 return -EFAULT;
627 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
628
629 for (i = 0; i < 32; ++i) {
630 index = KVM_REG_MIPS_VEC_128(i);
631 if (copy_to_user(indices, &index, sizeof(index)))
632 return -EFAULT;
633 ++indices;
634 }
635 }
636
James Hogan05108702016-06-15 19:29:56 +0100637 for (i = 0; i < 6; ++i) {
638 if (!(vcpu->arch.kscratch_enabled & BIT(i + 2)))
639 continue;
640
641 if (copy_to_user(indices, &kvm_mips_get_one_regs_kscratch[i],
642 sizeof(kvm_mips_get_one_regs_kscratch[i])))
643 return -EFAULT;
644 ++indices;
645 }
646
James Hoganf5c43bd2016-06-15 19:29:49 +0100647 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
648}
649
David Daney4c73fb22013-05-23 09:49:09 -0700650static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
651 const struct kvm_one_reg *reg)
652{
David Daney4c73fb22013-05-23 09:49:09 -0700653 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000654 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100655 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700656 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000657 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000658 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700659
660 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000661 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700662 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
663 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
664 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100665#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700666 case KVM_REG_MIPS_HI:
667 v = (long)vcpu->arch.hi;
668 break;
669 case KVM_REG_MIPS_LO:
670 v = (long)vcpu->arch.lo;
671 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100672#endif
David Daney4c73fb22013-05-23 09:49:09 -0700673 case KVM_REG_MIPS_PC:
674 v = (long)vcpu->arch.pc;
675 break;
676
James Hogan379245c2014-12-02 15:48:24 +0000677 /* Floating point registers */
678 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
679 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
680 return -EINVAL;
681 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
682 /* Odd singles in top of even double when FR=0 */
683 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
684 v = get_fpr32(&fpu->fpr[idx], 0);
685 else
686 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
687 break;
688 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
689 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
690 return -EINVAL;
691 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
692 /* Can't access odd doubles in FR=0 mode */
693 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
694 return -EINVAL;
695 v = get_fpr64(&fpu->fpr[idx], 0);
696 break;
697 case KVM_REG_MIPS_FCR_IR:
698 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
699 return -EINVAL;
700 v = boot_cpu_data.fpu_id;
701 break;
702 case KVM_REG_MIPS_FCR_CSR:
703 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
704 return -EINVAL;
705 v = fpu->fcr31;
706 break;
707
James Hoganab86bd62014-12-02 15:48:24 +0000708 /* MIPS SIMD Architecture (MSA) registers */
709 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
710 if (!kvm_mips_guest_has_msa(&vcpu->arch))
711 return -EINVAL;
712 /* Can't access MSA registers in FR=0 mode */
713 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
714 return -EINVAL;
715 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
716#ifdef CONFIG_CPU_LITTLE_ENDIAN
717 /* least significant byte first */
718 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
719 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
720#else
721 /* most significant byte first */
722 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
723 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
724#endif
725 break;
726 case KVM_REG_MIPS_MSA_IR:
727 if (!kvm_mips_guest_has_msa(&vcpu->arch))
728 return -EINVAL;
729 v = boot_cpu_data.msa_id;
730 break;
731 case KVM_REG_MIPS_MSA_CSR:
732 if (!kvm_mips_guest_has_msa(&vcpu->arch))
733 return -EINVAL;
734 v = fpu->msacsr;
735 break;
736
James Hogan379245c2014-12-02 15:48:24 +0000737 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700738 case KVM_REG_MIPS_CP0_INDEX:
739 v = (long)kvm_read_c0_guest_index(cop0);
740 break;
741 case KVM_REG_MIPS_CP0_CONTEXT:
742 v = (long)kvm_read_c0_guest_context(cop0);
743 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100744 case KVM_REG_MIPS_CP0_USERLOCAL:
745 v = (long)kvm_read_c0_guest_userlocal(cop0);
746 break;
David Daney4c73fb22013-05-23 09:49:09 -0700747 case KVM_REG_MIPS_CP0_PAGEMASK:
748 v = (long)kvm_read_c0_guest_pagemask(cop0);
749 break;
750 case KVM_REG_MIPS_CP0_WIRED:
751 v = (long)kvm_read_c0_guest_wired(cop0);
752 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100753 case KVM_REG_MIPS_CP0_HWRENA:
754 v = (long)kvm_read_c0_guest_hwrena(cop0);
755 break;
David Daney4c73fb22013-05-23 09:49:09 -0700756 case KVM_REG_MIPS_CP0_BADVADDR:
757 v = (long)kvm_read_c0_guest_badvaddr(cop0);
758 break;
759 case KVM_REG_MIPS_CP0_ENTRYHI:
760 v = (long)kvm_read_c0_guest_entryhi(cop0);
761 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100762 case KVM_REG_MIPS_CP0_COMPARE:
763 v = (long)kvm_read_c0_guest_compare(cop0);
764 break;
David Daney4c73fb22013-05-23 09:49:09 -0700765 case KVM_REG_MIPS_CP0_STATUS:
766 v = (long)kvm_read_c0_guest_status(cop0);
767 break;
768 case KVM_REG_MIPS_CP0_CAUSE:
769 v = (long)kvm_read_c0_guest_cause(cop0);
770 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100771 case KVM_REG_MIPS_CP0_EPC:
772 v = (long)kvm_read_c0_guest_epc(cop0);
773 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100774 case KVM_REG_MIPS_CP0_PRID:
775 v = (long)kvm_read_c0_guest_prid(cop0);
776 break;
David Daney4c73fb22013-05-23 09:49:09 -0700777 case KVM_REG_MIPS_CP0_CONFIG:
778 v = (long)kvm_read_c0_guest_config(cop0);
779 break;
780 case KVM_REG_MIPS_CP0_CONFIG1:
781 v = (long)kvm_read_c0_guest_config1(cop0);
782 break;
783 case KVM_REG_MIPS_CP0_CONFIG2:
784 v = (long)kvm_read_c0_guest_config2(cop0);
785 break;
786 case KVM_REG_MIPS_CP0_CONFIG3:
787 v = (long)kvm_read_c0_guest_config3(cop0);
788 break;
James Hoganc7716072014-06-26 15:11:29 +0100789 case KVM_REG_MIPS_CP0_CONFIG4:
790 v = (long)kvm_read_c0_guest_config4(cop0);
791 break;
792 case KVM_REG_MIPS_CP0_CONFIG5:
793 v = (long)kvm_read_c0_guest_config5(cop0);
794 break;
David Daney4c73fb22013-05-23 09:49:09 -0700795 case KVM_REG_MIPS_CP0_CONFIG7:
796 v = (long)kvm_read_c0_guest_config7(cop0);
797 break;
James Hogane93d4c12014-06-26 13:47:22 +0100798 case KVM_REG_MIPS_CP0_ERROREPC:
799 v = (long)kvm_read_c0_guest_errorepc(cop0);
800 break;
James Hogan05108702016-06-15 19:29:56 +0100801 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
802 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
803 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
804 return -EINVAL;
805 switch (idx) {
806 case 2:
807 v = (long)kvm_read_c0_guest_kscratch1(cop0);
808 break;
809 case 3:
810 v = (long)kvm_read_c0_guest_kscratch2(cop0);
811 break;
812 case 4:
813 v = (long)kvm_read_c0_guest_kscratch3(cop0);
814 break;
815 case 5:
816 v = (long)kvm_read_c0_guest_kscratch4(cop0);
817 break;
818 case 6:
819 v = (long)kvm_read_c0_guest_kscratch5(cop0);
820 break;
821 case 7:
822 v = (long)kvm_read_c0_guest_kscratch6(cop0);
823 break;
824 }
825 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100826 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100827 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100828 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
829 if (ret)
830 return ret;
831 break;
David Daney4c73fb22013-05-23 09:49:09 -0700832 }
David Daney681865d2013-06-10 12:33:48 -0700833 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
834 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700835
David Daney681865d2013-06-10 12:33:48 -0700836 return put_user(v, uaddr64);
837 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
838 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
839 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700840
David Daney681865d2013-06-10 12:33:48 -0700841 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000842 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
843 void __user *uaddr = (void __user *)(long)reg->addr;
844
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200845 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700846 } else {
847 return -EINVAL;
848 }
David Daney4c73fb22013-05-23 09:49:09 -0700849}
850
851static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
852 const struct kvm_one_reg *reg)
853{
David Daney4c73fb22013-05-23 09:49:09 -0700854 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000855 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
856 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000857 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000858 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700859
David Daney681865d2013-06-10 12:33:48 -0700860 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
861 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
862
863 if (get_user(v, uaddr64) != 0)
864 return -EFAULT;
865 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
866 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
867 s32 v32;
868
869 if (get_user(v32, uaddr32) != 0)
870 return -EFAULT;
871 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000872 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
873 void __user *uaddr = (void __user *)(long)reg->addr;
874
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200875 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700876 } else {
877 return -EINVAL;
878 }
David Daney4c73fb22013-05-23 09:49:09 -0700879
880 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000881 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700882 case KVM_REG_MIPS_R0:
883 /* Silently ignore requests to set $0 */
884 break;
885 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
886 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
887 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100888#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700889 case KVM_REG_MIPS_HI:
890 vcpu->arch.hi = v;
891 break;
892 case KVM_REG_MIPS_LO:
893 vcpu->arch.lo = v;
894 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100895#endif
David Daney4c73fb22013-05-23 09:49:09 -0700896 case KVM_REG_MIPS_PC:
897 vcpu->arch.pc = v;
898 break;
899
James Hogan379245c2014-12-02 15:48:24 +0000900 /* Floating point registers */
901 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
902 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
903 return -EINVAL;
904 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
905 /* Odd singles in top of even double when FR=0 */
906 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
907 set_fpr32(&fpu->fpr[idx], 0, v);
908 else
909 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
910 break;
911 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
912 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
913 return -EINVAL;
914 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
915 /* Can't access odd doubles in FR=0 mode */
916 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
917 return -EINVAL;
918 set_fpr64(&fpu->fpr[idx], 0, v);
919 break;
920 case KVM_REG_MIPS_FCR_IR:
921 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
922 return -EINVAL;
923 /* Read-only */
924 break;
925 case KVM_REG_MIPS_FCR_CSR:
926 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
927 return -EINVAL;
928 fpu->fcr31 = v;
929 break;
930
James Hoganab86bd62014-12-02 15:48:24 +0000931 /* MIPS SIMD Architecture (MSA) registers */
932 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
933 if (!kvm_mips_guest_has_msa(&vcpu->arch))
934 return -EINVAL;
935 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
936#ifdef CONFIG_CPU_LITTLE_ENDIAN
937 /* least significant byte first */
938 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
939 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
940#else
941 /* most significant byte first */
942 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
943 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
944#endif
945 break;
946 case KVM_REG_MIPS_MSA_IR:
947 if (!kvm_mips_guest_has_msa(&vcpu->arch))
948 return -EINVAL;
949 /* Read-only */
950 break;
951 case KVM_REG_MIPS_MSA_CSR:
952 if (!kvm_mips_guest_has_msa(&vcpu->arch))
953 return -EINVAL;
954 fpu->msacsr = v;
955 break;
956
James Hogan379245c2014-12-02 15:48:24 +0000957 /* Co-processor 0 registers */
David Daney4c73fb22013-05-23 09:49:09 -0700958 case KVM_REG_MIPS_CP0_INDEX:
959 kvm_write_c0_guest_index(cop0, v);
960 break;
961 case KVM_REG_MIPS_CP0_CONTEXT:
962 kvm_write_c0_guest_context(cop0, v);
963 break;
James Hogan7767b7d2014-05-29 10:16:30 +0100964 case KVM_REG_MIPS_CP0_USERLOCAL:
965 kvm_write_c0_guest_userlocal(cop0, v);
966 break;
David Daney4c73fb22013-05-23 09:49:09 -0700967 case KVM_REG_MIPS_CP0_PAGEMASK:
968 kvm_write_c0_guest_pagemask(cop0, v);
969 break;
970 case KVM_REG_MIPS_CP0_WIRED:
971 kvm_write_c0_guest_wired(cop0, v);
972 break;
James Hogan16fd5c12014-05-29 10:16:31 +0100973 case KVM_REG_MIPS_CP0_HWRENA:
974 kvm_write_c0_guest_hwrena(cop0, v);
975 break;
David Daney4c73fb22013-05-23 09:49:09 -0700976 case KVM_REG_MIPS_CP0_BADVADDR:
977 kvm_write_c0_guest_badvaddr(cop0, v);
978 break;
979 case KVM_REG_MIPS_CP0_ENTRYHI:
980 kvm_write_c0_guest_entryhi(cop0, v);
981 break;
982 case KVM_REG_MIPS_CP0_STATUS:
983 kvm_write_c0_guest_status(cop0, v);
984 break;
James Hoganfb6df0c2014-05-29 10:16:27 +0100985 case KVM_REG_MIPS_CP0_EPC:
986 kvm_write_c0_guest_epc(cop0, v);
987 break;
James Hogan1068eaa2014-06-26 13:56:52 +0100988 case KVM_REG_MIPS_CP0_PRID:
989 kvm_write_c0_guest_prid(cop0, v);
990 break;
David Daney4c73fb22013-05-23 09:49:09 -0700991 case KVM_REG_MIPS_CP0_ERROREPC:
992 kvm_write_c0_guest_errorepc(cop0, v);
993 break;
James Hogan05108702016-06-15 19:29:56 +0100994 case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
995 idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
996 if (!(vcpu->arch.kscratch_enabled & BIT(idx)))
997 return -EINVAL;
998 switch (idx) {
999 case 2:
1000 kvm_write_c0_guest_kscratch1(cop0, v);
1001 break;
1002 case 3:
1003 kvm_write_c0_guest_kscratch2(cop0, v);
1004 break;
1005 case 4:
1006 kvm_write_c0_guest_kscratch3(cop0, v);
1007 break;
1008 case 5:
1009 kvm_write_c0_guest_kscratch4(cop0, v);
1010 break;
1011 case 6:
1012 kvm_write_c0_guest_kscratch5(cop0, v);
1013 break;
1014 case 7:
1015 kvm_write_c0_guest_kscratch6(cop0, v);
1016 break;
1017 }
1018 break;
James Hoganf8be02d2014-05-29 10:16:29 +01001019 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -07001020 default:
James Hogancc68d222016-06-15 19:29:48 +01001021 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -07001022 }
1023 return 0;
1024}
1025
James Hogan5fafd8742014-12-08 23:07:56 +00001026static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1027 struct kvm_enable_cap *cap)
1028{
1029 int r = 0;
1030
1031 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
1032 return -EINVAL;
1033 if (cap->flags)
1034 return -EINVAL;
1035 if (cap->args[0])
1036 return -EINVAL;
1037
1038 switch (cap->cap) {
1039 case KVM_CAP_MIPS_FPU:
1040 vcpu->arch.fpu_enabled = true;
1041 break;
James Hogand952bd02014-12-08 23:07:56 +00001042 case KVM_CAP_MIPS_MSA:
1043 vcpu->arch.msa_enabled = true;
1044 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001045 default:
1046 r = -EINVAL;
1047 break;
1048 }
1049
1050 return r;
1051}
1052
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001053long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
1054 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -08001055{
1056 struct kvm_vcpu *vcpu = filp->private_data;
1057 void __user *argp = (void __user *)arg;
1058 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001059
1060 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -07001061 case KVM_SET_ONE_REG:
1062 case KVM_GET_ONE_REG: {
1063 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001064
David Daney4c73fb22013-05-23 09:49:09 -07001065 if (copy_from_user(&reg, argp, sizeof(reg)))
1066 return -EFAULT;
1067 if (ioctl == KVM_SET_ONE_REG)
1068 return kvm_mips_set_reg(vcpu, &reg);
1069 else
1070 return kvm_mips_get_reg(vcpu, &reg);
1071 }
1072 case KVM_GET_REG_LIST: {
1073 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -07001074 struct kvm_reg_list reg_list;
1075 unsigned n;
1076
1077 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
1078 return -EFAULT;
1079 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +01001080 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -07001081 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
1082 return -EFAULT;
1083 if (n < reg_list.n)
1084 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +01001085 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -07001086 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001087 case KVM_INTERRUPT:
1088 {
1089 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001090
Sanjay Lal669e8462012-11-21 18:34:02 -08001091 if (copy_from_user(&irq, argp, sizeof(irq)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +01001092 return -EFAULT;
Sanjay Lal669e8462012-11-21 18:34:02 -08001093 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
1094 irq.irq);
1095
1096 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1097 break;
1098 }
James Hogan5fafd8742014-12-08 23:07:56 +00001099 case KVM_ENABLE_CAP: {
1100 struct kvm_enable_cap cap;
1101
James Hogan5fafd8742014-12-08 23:07:56 +00001102 if (copy_from_user(&cap, argp, sizeof(cap)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +01001103 return -EFAULT;
James Hogan5fafd8742014-12-08 23:07:56 +00001104 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1105 break;
1106 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001107 default:
David Daney4c73fb22013-05-23 09:49:09 -07001108 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001109 }
Sanjay Lal669e8462012-11-21 18:34:02 -08001110 return r;
1111}
1112
James Hogane88643b2016-12-06 14:50:52 +00001113/**
1114 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
1115 * @kvm: kvm instance
1116 * @log: slot id and address to which we copy the log
1117 *
1118 * Steps 1-4 below provide general overview of dirty page logging. See
1119 * kvm_get_dirty_log_protect() function description for additional details.
1120 *
1121 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1122 * always flush the TLB (step 4) even if previous step failed and the dirty
1123 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1124 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1125 * writes will be marked dirty for next log read.
1126 *
1127 * 1. Take a snapshot of the bit and clear it if needed.
1128 * 2. Write protect the corresponding page.
1129 * 3. Copy the snapshot to the userspace.
1130 * 4. Flush TLB's if needed.
1131 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001132int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1133{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001134 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -08001135 struct kvm_memory_slot *memslot;
James Hogane88643b2016-12-06 14:50:52 +00001136 bool is_dirty = false;
Sanjay Lal669e8462012-11-21 18:34:02 -08001137 int r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001138
1139 mutex_lock(&kvm->slots_lock);
1140
James Hogane88643b2016-12-06 14:50:52 +00001141 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Sanjay Lal669e8462012-11-21 18:34:02 -08001142
Sanjay Lal669e8462012-11-21 18:34:02 -08001143 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001144 slots = kvm_memslots(kvm);
1145 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001146
James Hogane88643b2016-12-06 14:50:52 +00001147 /* Let implementation handle TLB/GVA invalidation */
1148 kvm_mips_callbacks->flush_shadow_memslot(kvm, memslot);
Sanjay Lal669e8462012-11-21 18:34:02 -08001149 }
1150
Sanjay Lal669e8462012-11-21 18:34:02 -08001151 mutex_unlock(&kvm->slots_lock);
1152 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001153}
1154
1155long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
1156{
1157 long r;
1158
1159 switch (ioctl) {
1160 default:
David Daneyed829852013-05-23 09:49:10 -07001161 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001162 }
1163
1164 return r;
1165}
1166
1167int kvm_arch_init(void *opaque)
1168{
Sanjay Lal669e8462012-11-21 18:34:02 -08001169 if (kvm_mips_callbacks) {
1170 kvm_err("kvm: module already exists\n");
1171 return -EEXIST;
1172 }
1173
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001174 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001175}
1176
1177void kvm_arch_exit(void)
1178{
1179 kvm_mips_callbacks = NULL;
1180}
1181
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001182int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1183 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001184{
David Daneyed829852013-05-23 09:49:10 -07001185 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001186}
1187
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001188int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1189 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001190{
David Daneyed829852013-05-23 09:49:10 -07001191 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001192}
1193
Dominik Dingel31928aa2014-12-04 15:47:07 +01001194void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001195{
Sanjay Lal669e8462012-11-21 18:34:02 -08001196}
1197
1198int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1199{
David Daneyed829852013-05-23 09:49:10 -07001200 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001201}
1202
1203int kvm_arch_vcpu_ioctl_set_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_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1209{
1210 return VM_FAULT_SIGBUS;
1211}
1212
Alexander Graf784aa3d2014-07-14 18:27:35 +02001213int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001214{
1215 int r;
1216
1217 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001218 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001219 case KVM_CAP_ENABLE_CAP:
David Daney4c73fb22013-05-23 09:49:09 -07001220 r = 1;
1221 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001222 case KVM_CAP_COALESCED_MMIO:
1223 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1224 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001225 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001226 /* We don't handle systems with inconsistent cpu_has_fpu */
1227 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001228 break;
James Hogand952bd02014-12-08 23:07:56 +00001229 case KVM_CAP_MIPS_MSA:
1230 /*
1231 * We don't support MSA vector partitioning yet:
1232 * 1) It would require explicit support which can't be tested
1233 * yet due to lack of support in current hardware.
1234 * 2) It extends the state that would need to be saved/restored
1235 * by e.g. QEMU for migration.
1236 *
1237 * When vector partitioning hardware becomes available, support
1238 * could be added by requiring a flag when enabling
1239 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1240 * to save/restore the appropriate extra state.
1241 */
1242 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1243 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001244 default:
1245 r = 0;
1246 break;
1247 }
1248 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001249}
1250
1251int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1252{
1253 return kvm_mips_pending_timer(vcpu);
1254}
1255
1256int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1257{
1258 int i;
1259 struct mips_coproc *cop0;
1260
1261 if (!vcpu)
1262 return -1;
1263
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001264 kvm_debug("VCPU Register Dump:\n");
1265 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1266 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001267
1268 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001269 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001270 vcpu->arch.gprs[i],
1271 vcpu->arch.gprs[i + 1],
1272 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1273 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001274 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1275 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001276
1277 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001278 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1279 kvm_read_c0_guest_status(cop0),
1280 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001281
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001282 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001283
1284 return 0;
1285}
1286
1287int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1288{
1289 int i;
1290
David Daney8d17dd02013-05-23 09:49:08 -07001291 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001292 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001293 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001294 vcpu->arch.hi = regs->hi;
1295 vcpu->arch.lo = regs->lo;
1296 vcpu->arch.pc = regs->pc;
1297
David Daney4c73fb22013-05-23 09:49:09 -07001298 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001299}
1300
1301int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1302{
1303 int i;
1304
David Daney8d17dd02013-05-23 09:49:08 -07001305 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001306 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001307
1308 regs->hi = vcpu->arch.hi;
1309 regs->lo = vcpu->arch.lo;
1310 regs->pc = vcpu->arch.pc;
1311
David Daney4c73fb22013-05-23 09:49:09 -07001312 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001313}
1314
James Hogan0fae34f2014-05-29 10:16:39 +01001315static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001316{
1317 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1318
1319 kvm_mips_callbacks->queue_timer_int(vcpu);
1320
1321 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001322 if (swait_active(&vcpu->wq))
1323 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001324}
1325
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001326/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001327static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001328{
1329 struct kvm_vcpu *vcpu;
1330
1331 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1332 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001333 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001334}
1335
1336int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1337{
James Hoganf7f14272016-09-08 22:57:03 +01001338 int err;
1339
1340 err = kvm_mips_callbacks->vcpu_init(vcpu);
1341 if (err)
1342 return err;
1343
Sanjay Lal669e8462012-11-21 18:34:02 -08001344 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1345 HRTIMER_MODE_REL);
1346 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001347 return 0;
1348}
1349
James Hogan630766b2016-09-08 23:00:24 +01001350void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1351{
1352 kvm_mips_callbacks->vcpu_uninit(vcpu);
1353}
1354
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001355int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1356 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001357{
1358 return 0;
1359}
1360
1361/* Initial guest state */
1362int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1363{
1364 return kvm_mips_callbacks->vcpu_setup(vcpu);
1365}
1366
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001367static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001368{
James Hogan8cffd192016-06-09 14:19:08 +01001369 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001370
Sanjay Lal669e8462012-11-21 18:34:02 -08001371 if (cpu_has_dsp)
1372 status |= (ST0_MX);
1373
1374 write_c0_status(status);
1375 ehb();
1376}
1377
1378/*
1379 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1380 */
1381int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1382{
James Hogan8cffd192016-06-09 14:19:08 +01001383 u32 cause = vcpu->arch.host_cp0_cause;
1384 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1385 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001386 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1387 enum emulation_result er = EMULATE_DONE;
James Hogan122e51d2016-11-28 17:23:14 +00001388 u32 inst;
Sanjay Lal669e8462012-11-21 18:34:02 -08001389 int ret = RESUME_GUEST;
1390
James Hogan4841e0d2016-11-28 22:45:04 +00001391 vcpu->mode = OUTSIDE_GUEST_MODE;
1392
James Hoganc4c6f2c2015-02-04 10:52:03 +00001393 /* re-enable HTW before enabling interrupts */
1394 htw_start();
1395
Sanjay Lal669e8462012-11-21 18:34:02 -08001396 /* Set a default exit reason */
1397 run->exit_reason = KVM_EXIT_UNKNOWN;
1398 run->ready_for_interrupt_injection = 1;
1399
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001400 /*
1401 * Set the appropriate status bits based on host CPU features,
1402 * before we hit the scheduler
1403 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001404 kvm_mips_set_c0_status();
1405
1406 local_irq_enable();
1407
1408 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1409 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001410 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001411
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001412 /*
1413 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001414 * causing an exception to be delivered to the Guest Kernel
1415 */
1416 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1417 if (er == EMULATE_PRIV_FAIL) {
1418 goto skip_emul;
1419 } else if (er == EMULATE_FAIL) {
1420 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1421 ret = RESUME_HOST;
1422 goto skip_emul;
1423 }
1424
1425 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001426 case EXCCODE_INT:
1427 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001428
1429 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001430
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001431 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001432 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001433
1434 ret = RESUME_GUEST;
1435 break;
1436
James Hogan16d100db2015-12-16 23:49:33 +00001437 case EXCCODE_CPU:
1438 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001439
1440 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001441 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1442 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001443 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001444 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001445 break;
1446
James Hogan16d100db2015-12-16 23:49:33 +00001447 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001448 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001449 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1450 break;
1451
James Hogan16d100db2015-12-16 23:49:33 +00001452 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001453 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1454 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1455 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001456
1457 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001458 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1459 break;
1460
James Hogan16d100db2015-12-16 23:49:33 +00001461 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001462 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1463 cause, opc, badvaddr);
1464
1465 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001466 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1467 break;
1468
James Hogan16d100db2015-12-16 23:49:33 +00001469 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001470 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001471 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1472 break;
1473
James Hogan16d100db2015-12-16 23:49:33 +00001474 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001475 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001476 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1477 break;
1478
James Hogan16d100db2015-12-16 23:49:33 +00001479 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001480 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001481 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1482 break;
1483
James Hogan16d100db2015-12-16 23:49:33 +00001484 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001485 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001486 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1487 break;
1488
James Hogan16d100db2015-12-16 23:49:33 +00001489 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001490 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001491 ret = kvm_mips_callbacks->handle_break(vcpu);
1492 break;
1493
James Hogan16d100db2015-12-16 23:49:33 +00001494 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001495 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001496 ret = kvm_mips_callbacks->handle_trap(vcpu);
1497 break;
1498
James Hogan16d100db2015-12-16 23:49:33 +00001499 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001500 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001501 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1502 break;
1503
James Hogan16d100db2015-12-16 23:49:33 +00001504 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001505 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001506 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1507 break;
1508
James Hogan16d100db2015-12-16 23:49:33 +00001509 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001510 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001511 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1512 break;
1513
Sanjay Lal669e8462012-11-21 18:34:02 -08001514 default:
James Hogan122e51d2016-11-28 17:23:14 +00001515 if (cause & CAUSEF_BD)
1516 opc += 1;
1517 inst = 0;
James Hogan6a97c772015-04-23 16:54:35 +01001518 kvm_get_badinstr(opc, vcpu, &inst);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001519 kvm_err("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n",
James Hogan122e51d2016-11-28 17:23:14 +00001520 exccode, opc, inst, badvaddr,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001521 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001522 kvm_arch_vcpu_dump_regs(vcpu);
1523 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1524 ret = RESUME_HOST;
1525 break;
1526
1527 }
1528
1529skip_emul:
1530 local_irq_disable();
1531
1532 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1533 kvm_mips_deliver_interrupts(vcpu, cause);
1534
1535 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001536 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001537 if (signal_pending(current)) {
1538 run->exit_reason = KVM_EXIT_INTR;
1539 ret = (-EINTR << 2) | RESUME_HOST;
1540 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001541 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001542 }
1543 }
1544
James Hogan98e91b82014-11-18 14:09:12 +00001545 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001546 trace_kvm_reenter(vcpu);
1547
James Hogan4841e0d2016-11-28 22:45:04 +00001548 /*
1549 * Make sure the read of VCPU requests in vcpu_reenter()
1550 * callback is not reordered ahead of the write to vcpu->mode,
1551 * or we could miss a TLB flush request while the requester sees
1552 * the VCPU as outside of guest mode and not needing an IPI.
1553 */
1554 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
1555
James Hogana2c046e2016-11-18 13:14:37 +00001556 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +01001557
James Hogan98e91b82014-11-18 14:09:12 +00001558 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001559 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1560 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001561 *
1562 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001563 * vector, as it may well cause an [MSA] FP exception if there
1564 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001565 * kvm_mips_csr_die_notifier() for how that is handled).
1566 */
1567 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1568 read_c0_status() & ST0_CU1)
1569 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001570
1571 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1572 read_c0_config5() & MIPS_CONF5_MSAEN)
1573 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001574 }
1575
James Hoganc4c6f2c2015-02-04 10:52:03 +00001576 /* Disable HTW before returning to guest or host */
1577 htw_stop();
1578
Sanjay Lal669e8462012-11-21 18:34:02 -08001579 return ret;
1580}
1581
James Hogan98e91b82014-11-18 14:09:12 +00001582/* Enable FPU for guest and restore context */
1583void kvm_own_fpu(struct kvm_vcpu *vcpu)
1584{
1585 struct mips_coproc *cop0 = vcpu->arch.cop0;
1586 unsigned int sr, cfg5;
1587
1588 preempt_disable();
1589
James Hogan539cb89fb2015-03-05 11:43:36 +00001590 sr = kvm_read_c0_guest_status(cop0);
1591
1592 /*
1593 * If MSA state is already live, it is undefined how it interacts with
1594 * FR=0 FPU state, and we don't want to hit reserved instruction
1595 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1596 * play it safe and save it first.
1597 *
1598 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1599 * get called when guest CU1 is set, however we can't trust the guest
1600 * not to clobber the status register directly via the commpage.
1601 */
1602 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001603 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001604 kvm_lose_fpu(vcpu);
1605
James Hogan98e91b82014-11-18 14:09:12 +00001606 /*
1607 * Enable FPU for guest
1608 * We set FR and FRE according to guest context
1609 */
James Hogan98e91b82014-11-18 14:09:12 +00001610 change_c0_status(ST0_CU1 | ST0_FR, sr);
1611 if (cpu_has_fre) {
1612 cfg5 = kvm_read_c0_guest_config5(cop0);
1613 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1614 }
1615 enable_fpu_hazard();
1616
1617 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001618 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001619 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001620 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001621 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1622 } else {
1623 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001624 }
1625
1626 preempt_enable();
1627}
1628
James Hogan539cb89fb2015-03-05 11:43:36 +00001629#ifdef CONFIG_CPU_HAS_MSA
1630/* Enable MSA for guest and restore context */
1631void kvm_own_msa(struct kvm_vcpu *vcpu)
1632{
1633 struct mips_coproc *cop0 = vcpu->arch.cop0;
1634 unsigned int sr, cfg5;
1635
1636 preempt_disable();
1637
1638 /*
1639 * Enable FPU if enabled in guest, since we're restoring FPU context
1640 * anyway. We set FR and FRE according to guest context.
1641 */
1642 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1643 sr = kvm_read_c0_guest_status(cop0);
1644
1645 /*
1646 * If FR=0 FPU state is already live, it is undefined how it
1647 * interacts with MSA state, so play it safe and save it first.
1648 */
1649 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001650 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1651 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001652 kvm_lose_fpu(vcpu);
1653
1654 change_c0_status(ST0_CU1 | ST0_FR, sr);
1655 if (sr & ST0_CU1 && cpu_has_fre) {
1656 cfg5 = kvm_read_c0_guest_config5(cop0);
1657 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1658 }
1659 }
1660
1661 /* Enable MSA for guest */
1662 set_c0_config5(MIPS_CONF5_MSAEN);
1663 enable_fpu_hazard();
1664
James Hoganf9431762016-06-14 09:40:10 +01001665 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1666 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001667 /*
1668 * Guest FPU state already loaded, only restore upper MSA state
1669 */
1670 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001671 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001672 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001673 break;
1674 case 0:
1675 /* Neither FPU or MSA already active, restore full MSA state */
1676 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001677 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001678 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001679 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001680 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1681 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001682 break;
1683 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001684 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001685 break;
1686 }
1687
1688 preempt_enable();
1689}
1690#endif
1691
1692/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001693void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1694{
1695 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001696 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001697 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001698 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001699 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001700 }
James Hoganf9431762016-06-14 09:40:10 +01001701 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001702 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001703 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001704 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001705 }
1706 preempt_enable();
1707}
1708
James Hogan539cb89fb2015-03-05 11:43:36 +00001709/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001710void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1711{
1712 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001713 * FPU & MSA get disabled in root context (hardware) when it is disabled
1714 * in guest context (software), but the register state in the hardware
1715 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001716 * before saving.
1717 */
1718
1719 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001720 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001721 set_c0_config5(MIPS_CONF5_MSAEN);
1722 enable_fpu_hazard();
1723
1724 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001725 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001726
1727 /* Disable MSA & FPU */
1728 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001729 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001730 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001731 disable_fpu_hazard();
1732 }
James Hoganf9431762016-06-14 09:40:10 +01001733 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1734 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001735 set_c0_status(ST0_CU1);
1736 enable_fpu_hazard();
1737
1738 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001739 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001740 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001741
1742 /* Disable FPU */
1743 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001744 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001745 }
1746 preempt_enable();
1747}
1748
1749/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001750 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1751 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1752 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001753 */
1754static int kvm_mips_csr_die_notify(struct notifier_block *self,
1755 unsigned long cmd, void *ptr)
1756{
1757 struct die_args *args = (struct die_args *)ptr;
1758 struct pt_regs *regs = args->regs;
1759 unsigned long pc;
1760
James Hogan539cb89fb2015-03-05 11:43:36 +00001761 /* Only interested in FPE and MSAFPE */
1762 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001763 return NOTIFY_DONE;
1764
1765 /* Return immediately if guest context isn't active */
1766 if (!(current->flags & PF_VCPU))
1767 return NOTIFY_DONE;
1768
1769 /* Should never get here from user mode */
1770 BUG_ON(user_mode(regs));
1771
1772 pc = instruction_pointer(regs);
1773 switch (cmd) {
1774 case DIE_FP:
1775 /* match 2nd instruction in __kvm_restore_fcsr */
1776 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1777 return NOTIFY_DONE;
1778 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001779 case DIE_MSAFP:
1780 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1781 if (!cpu_has_msa ||
1782 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1783 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1784 return NOTIFY_DONE;
1785 break;
James Hogan98e91b82014-11-18 14:09:12 +00001786 }
1787
1788 /* Move PC forward a little and continue executing */
1789 instruction_pointer(regs) += 4;
1790
1791 return NOTIFY_STOP;
1792}
1793
1794static struct notifier_block kvm_mips_csr_die_notifier = {
1795 .notifier_call = kvm_mips_csr_die_notify,
1796};
1797
James Hogan2db9d232015-12-16 23:49:32 +00001798static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001799{
1800 int ret;
1801
James Hogan1e5217f52016-06-23 17:34:45 +01001802 ret = kvm_mips_entry_setup();
1803 if (ret)
1804 return ret;
1805
Sanjay Lal669e8462012-11-21 18:34:02 -08001806 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1807
1808 if (ret)
1809 return ret;
1810
James Hogan98e91b82014-11-18 14:09:12 +00001811 register_die_notifier(&kvm_mips_csr_die_notifier);
1812
Sanjay Lal669e8462012-11-21 18:34:02 -08001813 return 0;
1814}
1815
James Hogan2db9d232015-12-16 23:49:32 +00001816static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001817{
1818 kvm_exit();
1819
James Hogan98e91b82014-11-18 14:09:12 +00001820 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001821}
1822
1823module_init(kvm_mips_init);
1824module_exit(kvm_mips_exit);
1825
1826EXPORT_TRACEPOINT_SYMBOL(kvm_exit);