blob: 31ee5ee0010befcb1071421ef616a05d27a123b5 [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,
David Daney4c73fb22013-05-23 09:49:09 -0700523};
524
James Hogane5775932016-06-15 19:29:51 +0100525static u64 kvm_mips_get_one_regs_fpu[] = {
526 KVM_REG_MIPS_FCR_IR,
527 KVM_REG_MIPS_FCR_CSR,
528};
529
530static u64 kvm_mips_get_one_regs_msa[] = {
531 KVM_REG_MIPS_MSA_IR,
532 KVM_REG_MIPS_MSA_CSR,
533};
534
James Hoganf5c43bd2016-06-15 19:29:49 +0100535static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
536{
537 unsigned long ret;
538
539 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100540 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
541 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
542 /* odd doubles */
543 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
544 ret += 16;
545 }
546 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
547 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hoganf5c43bd2016-06-15 19:29:49 +0100548 ret += kvm_mips_callbacks->num_regs(vcpu);
549
550 return ret;
551}
552
553static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
554{
James Hogane5775932016-06-15 19:29:51 +0100555 u64 index;
556 unsigned int i;
557
James Hoganf5c43bd2016-06-15 19:29:49 +0100558 if (copy_to_user(indices, kvm_mips_get_one_regs,
559 sizeof(kvm_mips_get_one_regs)))
560 return -EFAULT;
561 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
562
James Hogane5775932016-06-15 19:29:51 +0100563 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
564 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
565 sizeof(kvm_mips_get_one_regs_fpu)))
566 return -EFAULT;
567 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
568
569 for (i = 0; i < 32; ++i) {
570 index = KVM_REG_MIPS_FPR_32(i);
571 if (copy_to_user(indices, &index, sizeof(index)))
572 return -EFAULT;
573 ++indices;
574
575 /* skip odd doubles if no F64 */
576 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
577 continue;
578
579 index = KVM_REG_MIPS_FPR_64(i);
580 if (copy_to_user(indices, &index, sizeof(index)))
581 return -EFAULT;
582 ++indices;
583 }
584 }
585
586 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
587 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
588 sizeof(kvm_mips_get_one_regs_msa)))
589 return -EFAULT;
590 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
591
592 for (i = 0; i < 32; ++i) {
593 index = KVM_REG_MIPS_VEC_128(i);
594 if (copy_to_user(indices, &index, sizeof(index)))
595 return -EFAULT;
596 ++indices;
597 }
598 }
599
James Hoganf5c43bd2016-06-15 19:29:49 +0100600 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
601}
602
David Daney4c73fb22013-05-23 09:49:09 -0700603static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
604 const struct kvm_one_reg *reg)
605{
David Daney4c73fb22013-05-23 09:49:09 -0700606 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000607 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100608 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700609 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000610 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000611 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700612
613 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000614 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700615 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
616 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
617 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100618#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700619 case KVM_REG_MIPS_HI:
620 v = (long)vcpu->arch.hi;
621 break;
622 case KVM_REG_MIPS_LO:
623 v = (long)vcpu->arch.lo;
624 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100625#endif
David Daney4c73fb22013-05-23 09:49:09 -0700626 case KVM_REG_MIPS_PC:
627 v = (long)vcpu->arch.pc;
628 break;
629
James Hogan379245c2014-12-02 15:48:24 +0000630 /* Floating point registers */
631 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
632 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
633 return -EINVAL;
634 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
635 /* Odd singles in top of even double when FR=0 */
636 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
637 v = get_fpr32(&fpu->fpr[idx], 0);
638 else
639 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
640 break;
641 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
642 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
643 return -EINVAL;
644 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
645 /* Can't access odd doubles in FR=0 mode */
646 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
647 return -EINVAL;
648 v = get_fpr64(&fpu->fpr[idx], 0);
649 break;
650 case KVM_REG_MIPS_FCR_IR:
651 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
652 return -EINVAL;
653 v = boot_cpu_data.fpu_id;
654 break;
655 case KVM_REG_MIPS_FCR_CSR:
656 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
657 return -EINVAL;
658 v = fpu->fcr31;
659 break;
660
James Hoganab86bd62014-12-02 15:48:24 +0000661 /* MIPS SIMD Architecture (MSA) registers */
662 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
663 if (!kvm_mips_guest_has_msa(&vcpu->arch))
664 return -EINVAL;
665 /* Can't access MSA registers in FR=0 mode */
666 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
667 return -EINVAL;
668 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
669#ifdef CONFIG_CPU_LITTLE_ENDIAN
670 /* least significant byte first */
671 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
672 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
673#else
674 /* most significant byte first */
675 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
676 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
677#endif
678 break;
679 case KVM_REG_MIPS_MSA_IR:
680 if (!kvm_mips_guest_has_msa(&vcpu->arch))
681 return -EINVAL;
682 v = boot_cpu_data.msa_id;
683 break;
684 case KVM_REG_MIPS_MSA_CSR:
685 if (!kvm_mips_guest_has_msa(&vcpu->arch))
686 return -EINVAL;
687 v = fpu->msacsr;
688 break;
689
James Hoganf8be02d2014-05-29 10:16:29 +0100690 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100691 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100692 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
693 if (ret)
694 return ret;
695 break;
David Daney4c73fb22013-05-23 09:49:09 -0700696 }
David Daney681865d2013-06-10 12:33:48 -0700697 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
698 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700699
David Daney681865d2013-06-10 12:33:48 -0700700 return put_user(v, uaddr64);
701 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
702 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
703 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700704
David Daney681865d2013-06-10 12:33:48 -0700705 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000706 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
707 void __user *uaddr = (void __user *)(long)reg->addr;
708
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200709 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700710 } else {
711 return -EINVAL;
712 }
David Daney4c73fb22013-05-23 09:49:09 -0700713}
714
715static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
716 const struct kvm_one_reg *reg)
717{
David Daney4c73fb22013-05-23 09:49:09 -0700718 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000719 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
720 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000721 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000722 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700723
David Daney681865d2013-06-10 12:33:48 -0700724 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
725 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
726
727 if (get_user(v, uaddr64) != 0)
728 return -EFAULT;
729 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
730 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
731 s32 v32;
732
733 if (get_user(v32, uaddr32) != 0)
734 return -EFAULT;
735 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000736 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
737 void __user *uaddr = (void __user *)(long)reg->addr;
738
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200739 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700740 } else {
741 return -EINVAL;
742 }
David Daney4c73fb22013-05-23 09:49:09 -0700743
744 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000745 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700746 case KVM_REG_MIPS_R0:
747 /* Silently ignore requests to set $0 */
748 break;
749 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
750 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
751 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100752#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700753 case KVM_REG_MIPS_HI:
754 vcpu->arch.hi = v;
755 break;
756 case KVM_REG_MIPS_LO:
757 vcpu->arch.lo = v;
758 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100759#endif
David Daney4c73fb22013-05-23 09:49:09 -0700760 case KVM_REG_MIPS_PC:
761 vcpu->arch.pc = v;
762 break;
763
James Hogan379245c2014-12-02 15:48:24 +0000764 /* Floating point registers */
765 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
766 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
767 return -EINVAL;
768 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
769 /* Odd singles in top of even double when FR=0 */
770 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
771 set_fpr32(&fpu->fpr[idx], 0, v);
772 else
773 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
774 break;
775 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
776 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
777 return -EINVAL;
778 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
779 /* Can't access odd doubles in FR=0 mode */
780 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
781 return -EINVAL;
782 set_fpr64(&fpu->fpr[idx], 0, v);
783 break;
784 case KVM_REG_MIPS_FCR_IR:
785 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
786 return -EINVAL;
787 /* Read-only */
788 break;
789 case KVM_REG_MIPS_FCR_CSR:
790 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
791 return -EINVAL;
792 fpu->fcr31 = v;
793 break;
794
James Hoganab86bd62014-12-02 15:48:24 +0000795 /* MIPS SIMD Architecture (MSA) registers */
796 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
797 if (!kvm_mips_guest_has_msa(&vcpu->arch))
798 return -EINVAL;
799 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
800#ifdef CONFIG_CPU_LITTLE_ENDIAN
801 /* least significant byte first */
802 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
803 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
804#else
805 /* most significant byte first */
806 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
807 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
808#endif
809 break;
810 case KVM_REG_MIPS_MSA_IR:
811 if (!kvm_mips_guest_has_msa(&vcpu->arch))
812 return -EINVAL;
813 /* Read-only */
814 break;
815 case KVM_REG_MIPS_MSA_CSR:
816 if (!kvm_mips_guest_has_msa(&vcpu->arch))
817 return -EINVAL;
818 fpu->msacsr = v;
819 break;
820
James Hoganf8be02d2014-05-29 10:16:29 +0100821 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -0700822 default:
James Hogancc68d222016-06-15 19:29:48 +0100823 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700824 }
825 return 0;
826}
827
James Hogan5fafd8742014-12-08 23:07:56 +0000828static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
829 struct kvm_enable_cap *cap)
830{
831 int r = 0;
832
833 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
834 return -EINVAL;
835 if (cap->flags)
836 return -EINVAL;
837 if (cap->args[0])
838 return -EINVAL;
839
840 switch (cap->cap) {
841 case KVM_CAP_MIPS_FPU:
842 vcpu->arch.fpu_enabled = true;
843 break;
James Hogand952bd02014-12-08 23:07:56 +0000844 case KVM_CAP_MIPS_MSA:
845 vcpu->arch.msa_enabled = true;
846 break;
James Hogan5fafd8742014-12-08 23:07:56 +0000847 default:
848 r = -EINVAL;
849 break;
850 }
851
852 return r;
853}
854
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700855long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
856 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800857{
858 struct kvm_vcpu *vcpu = filp->private_data;
859 void __user *argp = (void __user *)arg;
860 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800861
862 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -0700863 case KVM_SET_ONE_REG:
864 case KVM_GET_ONE_REG: {
865 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700866
David Daney4c73fb22013-05-23 09:49:09 -0700867 if (copy_from_user(&reg, argp, sizeof(reg)))
868 return -EFAULT;
869 if (ioctl == KVM_SET_ONE_REG)
870 return kvm_mips_set_reg(vcpu, &reg);
871 else
872 return kvm_mips_get_reg(vcpu, &reg);
873 }
874 case KVM_GET_REG_LIST: {
875 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -0700876 struct kvm_reg_list reg_list;
877 unsigned n;
878
879 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
880 return -EFAULT;
881 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +0100882 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -0700883 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
884 return -EFAULT;
885 if (n < reg_list.n)
886 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +0100887 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -0700888 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800889 case KVM_INTERRUPT:
890 {
891 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700892
Sanjay Lal669e8462012-11-21 18:34:02 -0800893 if (copy_from_user(&irq, argp, sizeof(irq)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +0100894 return -EFAULT;
Sanjay Lal669e8462012-11-21 18:34:02 -0800895 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
896 irq.irq);
897
898 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
899 break;
900 }
James Hogan5fafd8742014-12-08 23:07:56 +0000901 case KVM_ENABLE_CAP: {
902 struct kvm_enable_cap cap;
903
James Hogan5fafd8742014-12-08 23:07:56 +0000904 if (copy_from_user(&cap, argp, sizeof(cap)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +0100905 return -EFAULT;
James Hogan5fafd8742014-12-08 23:07:56 +0000906 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
907 break;
908 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800909 default:
David Daney4c73fb22013-05-23 09:49:09 -0700910 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800911 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800912 return r;
913}
914
James Hogane88643b2016-12-06 14:50:52 +0000915/**
916 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
917 * @kvm: kvm instance
918 * @log: slot id and address to which we copy the log
919 *
920 * Steps 1-4 below provide general overview of dirty page logging. See
921 * kvm_get_dirty_log_protect() function description for additional details.
922 *
923 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
924 * always flush the TLB (step 4) even if previous step failed and the dirty
925 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
926 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
927 * writes will be marked dirty for next log read.
928 *
929 * 1. Take a snapshot of the bit and clear it if needed.
930 * 2. Write protect the corresponding page.
931 * 3. Copy the snapshot to the userspace.
932 * 4. Flush TLB's if needed.
933 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800934int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
935{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200936 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -0800937 struct kvm_memory_slot *memslot;
James Hogane88643b2016-12-06 14:50:52 +0000938 bool is_dirty = false;
Sanjay Lal669e8462012-11-21 18:34:02 -0800939 int r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800940
941 mutex_lock(&kvm->slots_lock);
942
James Hogane88643b2016-12-06 14:50:52 +0000943 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Sanjay Lal669e8462012-11-21 18:34:02 -0800944
Sanjay Lal669e8462012-11-21 18:34:02 -0800945 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200946 slots = kvm_memslots(kvm);
947 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -0800948
James Hogane88643b2016-12-06 14:50:52 +0000949 /* Let implementation handle TLB/GVA invalidation */
950 kvm_mips_callbacks->flush_shadow_memslot(kvm, memslot);
Sanjay Lal669e8462012-11-21 18:34:02 -0800951 }
952
Sanjay Lal669e8462012-11-21 18:34:02 -0800953 mutex_unlock(&kvm->slots_lock);
954 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800955}
956
957long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
958{
959 long r;
960
961 switch (ioctl) {
962 default:
David Daneyed829852013-05-23 09:49:10 -0700963 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800964 }
965
966 return r;
967}
968
969int kvm_arch_init(void *opaque)
970{
Sanjay Lal669e8462012-11-21 18:34:02 -0800971 if (kvm_mips_callbacks) {
972 kvm_err("kvm: module already exists\n");
973 return -EEXIST;
974 }
975
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700976 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -0800977}
978
979void kvm_arch_exit(void)
980{
981 kvm_mips_callbacks = NULL;
982}
983
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700984int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
985 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -0800986{
David Daneyed829852013-05-23 09:49:10 -0700987 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800988}
989
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700990int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
991 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -0800992{
David Daneyed829852013-05-23 09:49:10 -0700993 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800994}
995
Dominik Dingel31928aa2014-12-04 15:47:07 +0100996void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -0800997{
Sanjay Lal669e8462012-11-21 18:34:02 -0800998}
999
1000int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1001{
David Daneyed829852013-05-23 09:49:10 -07001002 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001003}
1004
1005int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1006{
David Daneyed829852013-05-23 09:49:10 -07001007 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001008}
1009
1010int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1011{
1012 return VM_FAULT_SIGBUS;
1013}
1014
Alexander Graf784aa3d2014-07-14 18:27:35 +02001015int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001016{
1017 int r;
1018
1019 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001020 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001021 case KVM_CAP_ENABLE_CAP:
James Hogan230c5722015-05-08 17:11:49 +01001022 case KVM_CAP_READONLY_MEM:
James Hogan411740f2016-12-13 16:32:39 +00001023 case KVM_CAP_SYNC_MMU:
David Daney4c73fb22013-05-23 09:49:09 -07001024 r = 1;
1025 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001026 case KVM_CAP_COALESCED_MMIO:
1027 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1028 break;
James Hogan12ed1fa2016-12-13 22:39:39 +00001029 case KVM_CAP_NR_VCPUS:
1030 r = num_online_cpus();
1031 break;
1032 case KVM_CAP_MAX_VCPUS:
1033 r = KVM_MAX_VCPUS;
1034 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001035 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001036 /* We don't handle systems with inconsistent cpu_has_fpu */
1037 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001038 break;
James Hogand952bd02014-12-08 23:07:56 +00001039 case KVM_CAP_MIPS_MSA:
1040 /*
1041 * We don't support MSA vector partitioning yet:
1042 * 1) It would require explicit support which can't be tested
1043 * yet due to lack of support in current hardware.
1044 * 2) It extends the state that would need to be saved/restored
1045 * by e.g. QEMU for migration.
1046 *
1047 * When vector partitioning hardware becomes available, support
1048 * could be added by requiring a flag when enabling
1049 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1050 * to save/restore the appropriate extra state.
1051 */
1052 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1053 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001054 default:
1055 r = 0;
1056 break;
1057 }
1058 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001059}
1060
1061int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1062{
1063 return kvm_mips_pending_timer(vcpu);
1064}
1065
1066int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1067{
1068 int i;
1069 struct mips_coproc *cop0;
1070
1071 if (!vcpu)
1072 return -1;
1073
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001074 kvm_debug("VCPU Register Dump:\n");
1075 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1076 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001077
1078 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001079 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001080 vcpu->arch.gprs[i],
1081 vcpu->arch.gprs[i + 1],
1082 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1083 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001084 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1085 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001086
1087 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001088 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1089 kvm_read_c0_guest_status(cop0),
1090 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001091
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001092 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001093
1094 return 0;
1095}
1096
1097int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1098{
1099 int i;
1100
David Daney8d17dd02013-05-23 09:49:08 -07001101 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001102 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001103 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001104 vcpu->arch.hi = regs->hi;
1105 vcpu->arch.lo = regs->lo;
1106 vcpu->arch.pc = regs->pc;
1107
David Daney4c73fb22013-05-23 09:49:09 -07001108 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001109}
1110
1111int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1112{
1113 int i;
1114
David Daney8d17dd02013-05-23 09:49:08 -07001115 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001116 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001117
1118 regs->hi = vcpu->arch.hi;
1119 regs->lo = vcpu->arch.lo;
1120 regs->pc = vcpu->arch.pc;
1121
David Daney4c73fb22013-05-23 09:49:09 -07001122 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001123}
1124
James Hogan0fae34f2014-05-29 10:16:39 +01001125static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001126{
1127 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1128
1129 kvm_mips_callbacks->queue_timer_int(vcpu);
1130
1131 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001132 if (swait_active(&vcpu->wq))
1133 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001134}
1135
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001136/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001137static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001138{
1139 struct kvm_vcpu *vcpu;
1140
1141 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1142 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001143 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001144}
1145
1146int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1147{
James Hoganf7f14272016-09-08 22:57:03 +01001148 int err;
1149
1150 err = kvm_mips_callbacks->vcpu_init(vcpu);
1151 if (err)
1152 return err;
1153
Sanjay Lal669e8462012-11-21 18:34:02 -08001154 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1155 HRTIMER_MODE_REL);
1156 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001157 return 0;
1158}
1159
James Hogan630766b2016-09-08 23:00:24 +01001160void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1161{
1162 kvm_mips_callbacks->vcpu_uninit(vcpu);
1163}
1164
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001165int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1166 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001167{
1168 return 0;
1169}
1170
1171/* Initial guest state */
1172int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1173{
1174 return kvm_mips_callbacks->vcpu_setup(vcpu);
1175}
1176
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001177static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001178{
James Hogan8cffd192016-06-09 14:19:08 +01001179 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001180
Sanjay Lal669e8462012-11-21 18:34:02 -08001181 if (cpu_has_dsp)
1182 status |= (ST0_MX);
1183
1184 write_c0_status(status);
1185 ehb();
1186}
1187
1188/*
1189 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1190 */
1191int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1192{
James Hogan8cffd192016-06-09 14:19:08 +01001193 u32 cause = vcpu->arch.host_cp0_cause;
1194 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1195 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001196 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1197 enum emulation_result er = EMULATE_DONE;
James Hogan122e51d2016-11-28 17:23:14 +00001198 u32 inst;
Sanjay Lal669e8462012-11-21 18:34:02 -08001199 int ret = RESUME_GUEST;
1200
James Hogan4841e0d2016-11-28 22:45:04 +00001201 vcpu->mode = OUTSIDE_GUEST_MODE;
1202
James Hoganc4c6f2c2015-02-04 10:52:03 +00001203 /* re-enable HTW before enabling interrupts */
1204 htw_start();
1205
Sanjay Lal669e8462012-11-21 18:34:02 -08001206 /* Set a default exit reason */
1207 run->exit_reason = KVM_EXIT_UNKNOWN;
1208 run->ready_for_interrupt_injection = 1;
1209
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001210 /*
1211 * Set the appropriate status bits based on host CPU features,
1212 * before we hit the scheduler
1213 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001214 kvm_mips_set_c0_status();
1215
1216 local_irq_enable();
1217
1218 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1219 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001220 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001221
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001222 /*
1223 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001224 * causing an exception to be delivered to the Guest Kernel
1225 */
1226 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1227 if (er == EMULATE_PRIV_FAIL) {
1228 goto skip_emul;
1229 } else if (er == EMULATE_FAIL) {
1230 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1231 ret = RESUME_HOST;
1232 goto skip_emul;
1233 }
1234
1235 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001236 case EXCCODE_INT:
1237 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001238
1239 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001240
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001241 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001242 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001243
1244 ret = RESUME_GUEST;
1245 break;
1246
James Hogan16d100db2015-12-16 23:49:33 +00001247 case EXCCODE_CPU:
1248 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001249
1250 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001251 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1252 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001253 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001254 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001255 break;
1256
James Hogan16d100db2015-12-16 23:49:33 +00001257 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001258 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001259 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1260 break;
1261
James Hogan16d100db2015-12-16 23:49:33 +00001262 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001263 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1264 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1265 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001266
1267 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001268 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1269 break;
1270
James Hogan16d100db2015-12-16 23:49:33 +00001271 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001272 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1273 cause, opc, badvaddr);
1274
1275 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001276 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1277 break;
1278
James Hogan16d100db2015-12-16 23:49:33 +00001279 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001280 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001281 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1282 break;
1283
James Hogan16d100db2015-12-16 23:49:33 +00001284 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001285 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001286 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1287 break;
1288
James Hogan16d100db2015-12-16 23:49:33 +00001289 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001290 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001291 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1292 break;
1293
James Hogan16d100db2015-12-16 23:49:33 +00001294 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001295 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001296 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1297 break;
1298
James Hogan16d100db2015-12-16 23:49:33 +00001299 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001300 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001301 ret = kvm_mips_callbacks->handle_break(vcpu);
1302 break;
1303
James Hogan16d100db2015-12-16 23:49:33 +00001304 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001305 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001306 ret = kvm_mips_callbacks->handle_trap(vcpu);
1307 break;
1308
James Hogan16d100db2015-12-16 23:49:33 +00001309 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001310 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001311 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1312 break;
1313
James Hogan16d100db2015-12-16 23:49:33 +00001314 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001315 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001316 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1317 break;
1318
James Hogan16d100db2015-12-16 23:49:33 +00001319 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001320 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001321 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1322 break;
1323
Sanjay Lal669e8462012-11-21 18:34:02 -08001324 default:
James Hogan122e51d2016-11-28 17:23:14 +00001325 if (cause & CAUSEF_BD)
1326 opc += 1;
1327 inst = 0;
James Hogan6a97c772015-04-23 16:54:35 +01001328 kvm_get_badinstr(opc, vcpu, &inst);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001329 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 +00001330 exccode, opc, inst, badvaddr,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001331 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001332 kvm_arch_vcpu_dump_regs(vcpu);
1333 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1334 ret = RESUME_HOST;
1335 break;
1336
1337 }
1338
1339skip_emul:
1340 local_irq_disable();
1341
1342 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1343 kvm_mips_deliver_interrupts(vcpu, cause);
1344
1345 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001346 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001347 if (signal_pending(current)) {
1348 run->exit_reason = KVM_EXIT_INTR;
1349 ret = (-EINTR << 2) | RESUME_HOST;
1350 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001351 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001352 }
1353 }
1354
James Hogan98e91b82014-11-18 14:09:12 +00001355 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001356 trace_kvm_reenter(vcpu);
1357
James Hogan4841e0d2016-11-28 22:45:04 +00001358 /*
1359 * Make sure the read of VCPU requests in vcpu_reenter()
1360 * callback is not reordered ahead of the write to vcpu->mode,
1361 * or we could miss a TLB flush request while the requester sees
1362 * the VCPU as outside of guest mode and not needing an IPI.
1363 */
1364 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
1365
James Hogana2c046e2016-11-18 13:14:37 +00001366 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +01001367
James Hogan98e91b82014-11-18 14:09:12 +00001368 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001369 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1370 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001371 *
1372 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001373 * vector, as it may well cause an [MSA] FP exception if there
1374 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001375 * kvm_mips_csr_die_notifier() for how that is handled).
1376 */
1377 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1378 read_c0_status() & ST0_CU1)
1379 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001380
1381 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1382 read_c0_config5() & MIPS_CONF5_MSAEN)
1383 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001384 }
1385
James Hoganc4c6f2c2015-02-04 10:52:03 +00001386 /* Disable HTW before returning to guest or host */
1387 htw_stop();
1388
Sanjay Lal669e8462012-11-21 18:34:02 -08001389 return ret;
1390}
1391
James Hogan98e91b82014-11-18 14:09:12 +00001392/* Enable FPU for guest and restore context */
1393void kvm_own_fpu(struct kvm_vcpu *vcpu)
1394{
1395 struct mips_coproc *cop0 = vcpu->arch.cop0;
1396 unsigned int sr, cfg5;
1397
1398 preempt_disable();
1399
James Hogan539cb89fb2015-03-05 11:43:36 +00001400 sr = kvm_read_c0_guest_status(cop0);
1401
1402 /*
1403 * If MSA state is already live, it is undefined how it interacts with
1404 * FR=0 FPU state, and we don't want to hit reserved instruction
1405 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1406 * play it safe and save it first.
1407 *
1408 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1409 * get called when guest CU1 is set, however we can't trust the guest
1410 * not to clobber the status register directly via the commpage.
1411 */
1412 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001413 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001414 kvm_lose_fpu(vcpu);
1415
James Hogan98e91b82014-11-18 14:09:12 +00001416 /*
1417 * Enable FPU for guest
1418 * We set FR and FRE according to guest context
1419 */
James Hogan98e91b82014-11-18 14:09:12 +00001420 change_c0_status(ST0_CU1 | ST0_FR, sr);
1421 if (cpu_has_fre) {
1422 cfg5 = kvm_read_c0_guest_config5(cop0);
1423 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1424 }
1425 enable_fpu_hazard();
1426
1427 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001428 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001429 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001430 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001431 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1432 } else {
1433 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001434 }
1435
1436 preempt_enable();
1437}
1438
James Hogan539cb89fb2015-03-05 11:43:36 +00001439#ifdef CONFIG_CPU_HAS_MSA
1440/* Enable MSA for guest and restore context */
1441void kvm_own_msa(struct kvm_vcpu *vcpu)
1442{
1443 struct mips_coproc *cop0 = vcpu->arch.cop0;
1444 unsigned int sr, cfg5;
1445
1446 preempt_disable();
1447
1448 /*
1449 * Enable FPU if enabled in guest, since we're restoring FPU context
1450 * anyway. We set FR and FRE according to guest context.
1451 */
1452 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1453 sr = kvm_read_c0_guest_status(cop0);
1454
1455 /*
1456 * If FR=0 FPU state is already live, it is undefined how it
1457 * interacts with MSA state, so play it safe and save it first.
1458 */
1459 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001460 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1461 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001462 kvm_lose_fpu(vcpu);
1463
1464 change_c0_status(ST0_CU1 | ST0_FR, sr);
1465 if (sr & ST0_CU1 && cpu_has_fre) {
1466 cfg5 = kvm_read_c0_guest_config5(cop0);
1467 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1468 }
1469 }
1470
1471 /* Enable MSA for guest */
1472 set_c0_config5(MIPS_CONF5_MSAEN);
1473 enable_fpu_hazard();
1474
James Hoganf9431762016-06-14 09:40:10 +01001475 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1476 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001477 /*
1478 * Guest FPU state already loaded, only restore upper MSA state
1479 */
1480 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001481 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001482 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001483 break;
1484 case 0:
1485 /* Neither FPU or MSA already active, restore full MSA state */
1486 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001487 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001488 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001489 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001490 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1491 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001492 break;
1493 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001494 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001495 break;
1496 }
1497
1498 preempt_enable();
1499}
1500#endif
1501
1502/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001503void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1504{
1505 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001506 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001507 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001508 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001509 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001510 }
James Hoganf9431762016-06-14 09:40:10 +01001511 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001512 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001513 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001514 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001515 }
1516 preempt_enable();
1517}
1518
James Hogan539cb89fb2015-03-05 11:43:36 +00001519/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001520void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1521{
1522 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001523 * FPU & MSA get disabled in root context (hardware) when it is disabled
1524 * in guest context (software), but the register state in the hardware
1525 * may still be in use. This is why we explicitly re-enable the hardware
James Hogan98e91b82014-11-18 14:09:12 +00001526 * before saving.
1527 */
1528
1529 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001530 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001531 set_c0_config5(MIPS_CONF5_MSAEN);
1532 enable_fpu_hazard();
1533
1534 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001535 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001536
1537 /* Disable MSA & FPU */
1538 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001539 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001540 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001541 disable_fpu_hazard();
1542 }
James Hoganf9431762016-06-14 09:40:10 +01001543 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1544 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001545 set_c0_status(ST0_CU1);
1546 enable_fpu_hazard();
1547
1548 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001549 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001550 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001551
1552 /* Disable FPU */
1553 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001554 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001555 }
1556 preempt_enable();
1557}
1558
1559/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001560 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1561 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1562 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001563 */
1564static int kvm_mips_csr_die_notify(struct notifier_block *self,
1565 unsigned long cmd, void *ptr)
1566{
1567 struct die_args *args = (struct die_args *)ptr;
1568 struct pt_regs *regs = args->regs;
1569 unsigned long pc;
1570
James Hogan539cb89fb2015-03-05 11:43:36 +00001571 /* Only interested in FPE and MSAFPE */
1572 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001573 return NOTIFY_DONE;
1574
1575 /* Return immediately if guest context isn't active */
1576 if (!(current->flags & PF_VCPU))
1577 return NOTIFY_DONE;
1578
1579 /* Should never get here from user mode */
1580 BUG_ON(user_mode(regs));
1581
1582 pc = instruction_pointer(regs);
1583 switch (cmd) {
1584 case DIE_FP:
1585 /* match 2nd instruction in __kvm_restore_fcsr */
1586 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1587 return NOTIFY_DONE;
1588 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001589 case DIE_MSAFP:
1590 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1591 if (!cpu_has_msa ||
1592 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1593 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1594 return NOTIFY_DONE;
1595 break;
James Hogan98e91b82014-11-18 14:09:12 +00001596 }
1597
1598 /* Move PC forward a little and continue executing */
1599 instruction_pointer(regs) += 4;
1600
1601 return NOTIFY_STOP;
1602}
1603
1604static struct notifier_block kvm_mips_csr_die_notifier = {
1605 .notifier_call = kvm_mips_csr_die_notify,
1606};
1607
James Hogan2db9d232015-12-16 23:49:32 +00001608static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001609{
1610 int ret;
1611
James Hogan1e5217f52016-06-23 17:34:45 +01001612 ret = kvm_mips_entry_setup();
1613 if (ret)
1614 return ret;
1615
Sanjay Lal669e8462012-11-21 18:34:02 -08001616 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1617
1618 if (ret)
1619 return ret;
1620
James Hogan98e91b82014-11-18 14:09:12 +00001621 register_die_notifier(&kvm_mips_csr_die_notifier);
1622
Sanjay Lal669e8462012-11-21 18:34:02 -08001623 return 0;
1624}
1625
James Hogan2db9d232015-12-16 23:49:32 +00001626static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001627{
1628 kvm_exit();
1629
James Hogan98e91b82014-11-18 14:09:12 +00001630 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001631}
1632
1633module_init(kvm_mips_init);
1634module_exit(kvm_mips_exit);
1635
1636EXPORT_TRACEPOINT_SYMBOL(kvm_exit);