blob: c507533ef6ea97b2b335b8cd81e5793ad69b070b [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010019#include <linux/sched/signal.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080020#include <linux/fs.h>
21#include <linux/bootmem.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010022
James Hoganf7982172015-02-04 17:06:37 +000023#include <asm/fpu.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080024#include <asm/page.h>
25#include <asm/cacheflush.h>
26#include <asm/mmu_context.h>
James Hogan06c158c2015-05-01 13:50:18 +010027#include <asm/pgalloc.h>
James Hoganc4c6f2c2015-02-04 10:52:03 +000028#include <asm/pgtable.h>
Sanjay Lal669e8462012-11-21 18:34:02 -080029
30#include <linux/kvm_host.h>
31
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070032#include "interrupt.h"
33#include "commpage.h"
Sanjay Lal669e8462012-11-21 18:34:02 -080034
35#define CREATE_TRACE_POINTS
36#include "trace.h"
37
38#ifndef VECTORSPACING
39#define VECTORSPACING 0x100 /* for EI/VI mode */
40#endif
41
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070042#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x)
Sanjay Lal669e8462012-11-21 18:34:02 -080043struct kvm_stats_debugfs_item debugfs_entries[] = {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070044 { "wait", VCPU_STAT(wait_exits), KVM_STAT_VCPU },
45 { "cache", VCPU_STAT(cache_exits), KVM_STAT_VCPU },
46 { "signal", VCPU_STAT(signal_exits), KVM_STAT_VCPU },
47 { "interrupt", VCPU_STAT(int_exits), KVM_STAT_VCPU },
48 { "cop_unsuable", VCPU_STAT(cop_unusable_exits), KVM_STAT_VCPU },
49 { "tlbmod", VCPU_STAT(tlbmod_exits), KVM_STAT_VCPU },
50 { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits), KVM_STAT_VCPU },
51 { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits), KVM_STAT_VCPU },
52 { "addrerr_st", VCPU_STAT(addrerr_st_exits), KVM_STAT_VCPU },
53 { "addrerr_ld", VCPU_STAT(addrerr_ld_exits), KVM_STAT_VCPU },
54 { "syscall", VCPU_STAT(syscall_exits), KVM_STAT_VCPU },
55 { "resvd_inst", VCPU_STAT(resvd_inst_exits), KVM_STAT_VCPU },
56 { "break_inst", VCPU_STAT(break_inst_exits), KVM_STAT_VCPU },
James Hogan0a560422015-02-06 16:03:57 +000057 { "trap_inst", VCPU_STAT(trap_inst_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000058 { "msa_fpe", VCPU_STAT(msa_fpe_exits), KVM_STAT_VCPU },
James Hogan1c0cd662015-02-06 10:56:27 +000059 { "fpe", VCPU_STAT(fpe_exits), KVM_STAT_VCPU },
James Hoganc2537ed2015-02-06 10:56:27 +000060 { "msa_disabled", VCPU_STAT(msa_disabled_exits), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070061 { "flush_dcache", VCPU_STAT(flush_dcache_exits), KVM_STAT_VCPU },
James Hogana7244922017-03-14 10:15:18 +000062#ifdef CONFIG_KVM_MIPS_VZ
63 { "vz_gpsi", VCPU_STAT(vz_gpsi_exits), KVM_STAT_VCPU },
64 { "vz_gsfc", VCPU_STAT(vz_gsfc_exits), KVM_STAT_VCPU },
65 { "vz_hc", VCPU_STAT(vz_hc_exits), KVM_STAT_VCPU },
66 { "vz_grr", VCPU_STAT(vz_grr_exits), KVM_STAT_VCPU },
67 { "vz_gva", VCPU_STAT(vz_gva_exits), KVM_STAT_VCPU },
68 { "vz_ghfc", VCPU_STAT(vz_ghfc_exits), KVM_STAT_VCPU },
69 { "vz_gpa", VCPU_STAT(vz_gpa_exits), KVM_STAT_VCPU },
70 { "vz_resvd", VCPU_STAT(vz_resvd_exits), KVM_STAT_VCPU },
71#endif
Paolo Bonzinif7819512015-02-04 18:20:58 +010072 { "halt_successful_poll", VCPU_STAT(halt_successful_poll), KVM_STAT_VCPU },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +020073 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), KVM_STAT_VCPU },
Christian Borntraeger3491caf2016-05-13 12:16:35 +020074 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid), KVM_STAT_VCPU },
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070075 { "halt_wakeup", VCPU_STAT(halt_wakeup), KVM_STAT_VCPU },
Sanjay Lal669e8462012-11-21 18:34:02 -080076 {NULL}
77};
78
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070079/*
80 * XXXKYMA: We are simulatoring a processor that has the WII bit set in
81 * Config7, so we are "runnable" if interrupts are pending
Sanjay Lal669e8462012-11-21 18:34:02 -080082 */
83int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
84{
85 return !!(vcpu->arch.pending_exceptions);
86}
87
88int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
89{
90 return 1;
91}
92
Radim Krčmář13a34e02014-08-28 15:13:03 +020093int kvm_arch_hardware_enable(void)
Sanjay Lal669e8462012-11-21 18:34:02 -080094{
95 return 0;
96}
97
Sanjay Lal669e8462012-11-21 18:34:02 -080098int kvm_arch_hardware_setup(void)
99{
100 return 0;
101}
102
Sanjay Lal669e8462012-11-21 18:34:02 -0800103void kvm_arch_check_processor_compat(void *rtn)
104{
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700105 *(int *)rtn = 0;
Sanjay Lal669e8462012-11-21 18:34:02 -0800106}
107
Sanjay Lal669e8462012-11-21 18:34:02 -0800108int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
109{
James Hogan06c158c2015-05-01 13:50:18 +0100110 /* Allocate page table to map GPA -> RPA */
111 kvm->arch.gpa_mm.pgd = kvm_pgd_alloc();
112 if (!kvm->arch.gpa_mm.pgd)
113 return -ENOMEM;
114
Sanjay Lal669e8462012-11-21 18:34:02 -0800115 return 0;
116}
117
Luiz Capitulino235539b2016-09-07 14:47:23 -0400118bool kvm_arch_has_vcpu_debugfs(void)
119{
120 return false;
121}
122
123int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
124{
125 return 0;
126}
127
Sanjay Lal669e8462012-11-21 18:34:02 -0800128void kvm_mips_free_vcpus(struct kvm *kvm)
129{
130 unsigned int i;
131 struct kvm_vcpu *vcpu;
132
Sanjay Lal669e8462012-11-21 18:34:02 -0800133 kvm_for_each_vcpu(i, vcpu, kvm) {
134 kvm_arch_vcpu_free(vcpu);
135 }
136
137 mutex_lock(&kvm->lock);
138
139 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
140 kvm->vcpus[i] = NULL;
141
142 atomic_set(&kvm->online_vcpus, 0);
143
144 mutex_unlock(&kvm->lock);
145}
146
James Hogan06c158c2015-05-01 13:50:18 +0100147static void kvm_mips_free_gpa_pt(struct kvm *kvm)
148{
149 /* It should always be safe to remove after flushing the whole range */
150 WARN_ON(!kvm_mips_flush_gpa_pt(kvm, 0, ~0));
151 pgd_free(NULL, kvm->arch.gpa_mm.pgd);
152}
153
Sanjay Lal669e8462012-11-21 18:34:02 -0800154void kvm_arch_destroy_vm(struct kvm *kvm)
155{
156 kvm_mips_free_vcpus(kvm);
James Hogan06c158c2015-05-01 13:50:18 +0100157 kvm_mips_free_gpa_pt(kvm);
Sanjay Lal669e8462012-11-21 18:34:02 -0800158}
159
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700160long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
161 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800162{
David Daneyed829852013-05-23 09:49:10 -0700163 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800164}
165
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530166int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
167 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800168{
169 return 0;
170}
171
James Hoganb6209112016-10-25 00:01:37 +0100172void kvm_arch_flush_shadow_all(struct kvm *kvm)
173{
174 /* Flush whole GPA */
175 kvm_mips_flush_gpa_pt(kvm, 0, ~0);
176
177 /* Let implementation do the rest */
178 kvm_mips_callbacks->flush_shadow_all(kvm);
179}
180
181void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
182 struct kvm_memory_slot *slot)
183{
184 /*
185 * The slot has been made invalid (ready for moving or deletion), so we
186 * need to ensure that it can no longer be accessed by any guest VCPUs.
187 */
188
189 spin_lock(&kvm->mmu_lock);
190 /* Flush slot from GPA */
191 kvm_mips_flush_gpa_pt(kvm, slot->base_gfn,
192 slot->base_gfn + slot->npages - 1);
193 /* Let implementation do the rest */
194 kvm_mips_callbacks->flush_shadow_memslot(kvm, slot);
195 spin_unlock(&kvm->mmu_lock);
196}
197
Sanjay Lal669e8462012-11-21 18:34:02 -0800198int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700199 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200200 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700201 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800202{
203 return 0;
204}
205
206void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200207 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700208 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200209 const struct kvm_memory_slot *new,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700210 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800211{
James Hogana1ac9e12016-12-06 14:56:20 +0000212 int needs_flush;
213
Sanjay Lal669e8462012-11-21 18:34:02 -0800214 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
215 __func__, kvm, mem->slot, mem->guest_phys_addr,
216 mem->memory_size, mem->userspace_addr);
James Hogana1ac9e12016-12-06 14:56:20 +0000217
218 /*
219 * If dirty page logging is enabled, write protect all pages in the slot
220 * ready for dirty logging.
221 *
222 * There is no need to do this in any of the following cases:
223 * CREATE: No dirty mappings will already exist.
224 * MOVE/DELETE: The old mappings will already have been cleaned up by
225 * kvm_arch_flush_shadow_memslot()
226 */
227 if (change == KVM_MR_FLAGS_ONLY &&
228 (!(old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
229 new->flags & KVM_MEM_LOG_DIRTY_PAGES)) {
230 spin_lock(&kvm->mmu_lock);
231 /* Write protect GPA page table entries */
232 needs_flush = kvm_mips_mkclean_gpa_pt(kvm, new->base_gfn,
233 new->base_gfn + new->npages - 1);
234 /* Let implementation do the rest */
235 if (needs_flush)
236 kvm_mips_callbacks->flush_shadow_memslot(kvm, new);
237 spin_unlock(&kvm->mmu_lock);
238 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800239}
240
James Hogand7b8f892016-06-23 17:34:40 +0100241static inline void dump_handler(const char *symbol, void *start, void *end)
242{
243 u32 *p;
244
245 pr_debug("LEAF(%s)\n", symbol);
246
247 pr_debug("\t.set push\n");
248 pr_debug("\t.set noreorder\n");
249
250 for (p = start; p < (u32 *)end; ++p)
251 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p);
252
253 pr_debug("\t.set\tpop\n");
254
255 pr_debug("\tEND(%s)\n", symbol);
256}
257
Sanjay Lal669e8462012-11-21 18:34:02 -0800258struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
259{
James Hogan90e93112016-06-23 17:34:39 +0100260 int err, size;
James Hogana7cfa7a2016-09-10 23:56:46 +0100261 void *gebase, *p, *handler, *refill_start, *refill_end;
Sanjay Lal669e8462012-11-21 18:34:02 -0800262 int i;
263
264 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
265
266 if (!vcpu) {
267 err = -ENOMEM;
268 goto out;
269 }
270
271 err = kvm_vcpu_init(vcpu, kvm, id);
272
273 if (err)
274 goto out_free_cpu;
275
James Hogan6e95bfd2014-05-29 10:16:43 +0100276 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800277
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700278 /*
279 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800280 * guest mode exits
281 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700282 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800283 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700284 else
James Hogan7006e2d2014-05-29 10:16:23 +0100285 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800286
Sanjay Lal669e8462012-11-21 18:34:02 -0800287 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
288
289 if (!gebase) {
290 err = -ENOMEM;
James Hogan585bb8f2015-11-11 14:21:20 +0000291 goto out_uninit_cpu;
Sanjay Lal669e8462012-11-21 18:34:02 -0800292 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100293 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
294 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800295
James Hogan2a06dab2016-07-08 11:53:26 +0100296 /*
297 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
298 * limits us to the low 512MB of physical address space. If the memory
299 * we allocate is out of range, just give up now.
300 */
301 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) {
302 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
303 gebase);
304 err = -ENOMEM;
305 goto out_free_gebase;
306 }
307
Sanjay Lal669e8462012-11-21 18:34:02 -0800308 /* Save new ebase */
309 vcpu->arch.guest_ebase = gebase;
310
James Hogan90e93112016-06-23 17:34:39 +0100311 /* Build guest exception vectors dynamically in unmapped memory */
James Hogan1f9ca622016-06-23 17:34:46 +0100312 handler = gebase + 0x2000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800313
James Hogana7cfa7a2016-09-10 23:56:46 +0100314 /* TLB refill */
315 refill_start = gebase;
316 refill_end = kvm_mips_build_tlb_refill_exception(refill_start, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800317
318 /* General Exception Entry point */
James Hogan1f9ca622016-06-23 17:34:46 +0100319 kvm_mips_build_exception(gebase + 0x180, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800320
321 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
322 for (i = 0; i < 8; i++) {
323 kvm_debug("L1 Vectored handler @ %p\n",
324 gebase + 0x200 + (i * VECTORSPACING));
James Hogan1f9ca622016-06-23 17:34:46 +0100325 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
326 handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800327 }
328
James Hogan90e93112016-06-23 17:34:39 +0100329 /* General exit handler */
James Hogan1f9ca622016-06-23 17:34:46 +0100330 p = handler;
James Hogan90e93112016-06-23 17:34:39 +0100331 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800332
James Hogan90e93112016-06-23 17:34:39 +0100333 /* Guest entry routine */
334 vcpu->arch.vcpu_run = p;
335 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100336
James Hogand7b8f892016-06-23 17:34:40 +0100337 /* Dump the generated code */
338 pr_debug("#include <asm/asm.h>\n");
339 pr_debug("#include <asm/regdef.h>\n");
340 pr_debug("\n");
341 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
James Hogana7cfa7a2016-09-10 23:56:46 +0100342 dump_handler("kvm_tlb_refill", refill_start, refill_end);
James Hogand7b8f892016-06-23 17:34:40 +0100343 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
344 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
345
Sanjay Lal669e8462012-11-21 18:34:02 -0800346 /* Invalidate the icache for these ranges */
James Hogan32eb12a2017-01-03 17:43:01 +0000347 flush_icache_range((unsigned long)gebase,
348 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800349
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700350 /*
351 * Allocate comm page for guest kernel, a TLB will be reserved for
352 * mapping GVA @ 0xFFFF8000 to this page
353 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800354 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
355
356 if (!vcpu->arch.kseg0_commpage) {
357 err = -ENOMEM;
358 goto out_free_gebase;
359 }
360
James Hogan6e95bfd2014-05-29 10:16:43 +0100361 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800362 kvm_mips_commpage_init(vcpu);
363
364 /* Init */
365 vcpu->arch.last_sched_cpu = -1;
366
367 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100368 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800369
370 return vcpu;
371
372out_free_gebase:
373 kfree(gebase);
374
James Hogan585bb8f2015-11-11 14:21:20 +0000375out_uninit_cpu:
376 kvm_vcpu_uninit(vcpu);
377
Sanjay Lal669e8462012-11-21 18:34:02 -0800378out_free_cpu:
379 kfree(vcpu);
380
381out:
382 return ERR_PTR(err);
383}
384
385void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
386{
387 hrtimer_cancel(&vcpu->arch.comparecount_timer);
388
389 kvm_vcpu_uninit(vcpu);
390
391 kvm_mips_dump_stats(vcpu);
392
James Hoganaba859292016-12-16 15:57:00 +0000393 kvm_mmu_free_memory_caches(vcpu);
James Hoganc6c0a662014-05-29 10:16:44 +0100394 kfree(vcpu->arch.guest_ebase);
395 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700396 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800397}
398
399void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
400{
401 kvm_arch_vcpu_free(vcpu);
402}
403
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700404int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
405 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800406{
David Daneyed829852013-05-23 09:49:10 -0700407 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800408}
409
410int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
411{
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100412 int r = -EINTR;
Sanjay Lal669e8462012-11-21 18:34:02 -0800413 sigset_t sigsaved;
414
415 if (vcpu->sigset_active)
416 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
417
418 if (vcpu->mmio_needed) {
419 if (!vcpu->mmio_is_write)
420 kvm_mips_complete_mmio_load(vcpu, run);
421 vcpu->mmio_needed = 0;
422 }
423
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100424 if (run->immediate_exit)
425 goto out;
426
James Hoganf7982172015-02-04 17:06:37 +0000427 lose_fpu(1);
428
James Hogan044f0f02014-05-29 10:16:32 +0100429 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200430 guest_enter_irqoff();
James Hogan93258602016-06-14 09:40:14 +0100431 trace_kvm_enter(vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100432
James Hogan4841e0d2016-11-28 22:45:04 +0000433 /*
434 * Make sure the read of VCPU requests in vcpu_run() callback is not
435 * reordered ahead of the write to vcpu->mode, or we could miss a TLB
436 * flush request while the requester sees the VCPU as outside of guest
437 * mode and not needing an IPI.
438 */
439 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
440
James Hogana2c046e2016-11-18 13:14:37 +0000441 r = kvm_mips_callbacks->vcpu_run(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100442
James Hogan93258602016-06-14 09:40:14 +0100443 trace_kvm_out(vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200444 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800445 local_irq_enable();
446
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100447out:
Sanjay Lal669e8462012-11-21 18:34:02 -0800448 if (vcpu->sigset_active)
449 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
450
451 return r;
452}
453
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700454int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
455 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800456{
457 int intr = (int)irq->irq;
458 struct kvm_vcpu *dvcpu = NULL;
459
460 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
461 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
462 (int)intr);
463
464 if (irq->cpu == -1)
465 dvcpu = vcpu;
466 else
467 dvcpu = vcpu->kvm->vcpus[irq->cpu];
468
469 if (intr == 2 || intr == 3 || intr == 4) {
470 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
471
472 } else if (intr == -2 || intr == -3 || intr == -4) {
473 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
474 } else {
475 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
476 irq->cpu, irq->irq);
477 return -EINVAL;
478 }
479
480 dvcpu->arch.wait = 0;
481
Marcelo Tosatti85773702016-02-19 09:46:39 +0100482 if (swait_active(&dvcpu->wq))
483 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800484
485 return 0;
486}
487
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700488int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
489 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800490{
David Daneyed829852013-05-23 09:49:10 -0700491 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800492}
493
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700494int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
495 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800496{
David Daneyed829852013-05-23 09:49:10 -0700497 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800498}
499
David Daney4c73fb22013-05-23 09:49:09 -0700500static u64 kvm_mips_get_one_regs[] = {
501 KVM_REG_MIPS_R0,
502 KVM_REG_MIPS_R1,
503 KVM_REG_MIPS_R2,
504 KVM_REG_MIPS_R3,
505 KVM_REG_MIPS_R4,
506 KVM_REG_MIPS_R5,
507 KVM_REG_MIPS_R6,
508 KVM_REG_MIPS_R7,
509 KVM_REG_MIPS_R8,
510 KVM_REG_MIPS_R9,
511 KVM_REG_MIPS_R10,
512 KVM_REG_MIPS_R11,
513 KVM_REG_MIPS_R12,
514 KVM_REG_MIPS_R13,
515 KVM_REG_MIPS_R14,
516 KVM_REG_MIPS_R15,
517 KVM_REG_MIPS_R16,
518 KVM_REG_MIPS_R17,
519 KVM_REG_MIPS_R18,
520 KVM_REG_MIPS_R19,
521 KVM_REG_MIPS_R20,
522 KVM_REG_MIPS_R21,
523 KVM_REG_MIPS_R22,
524 KVM_REG_MIPS_R23,
525 KVM_REG_MIPS_R24,
526 KVM_REG_MIPS_R25,
527 KVM_REG_MIPS_R26,
528 KVM_REG_MIPS_R27,
529 KVM_REG_MIPS_R28,
530 KVM_REG_MIPS_R29,
531 KVM_REG_MIPS_R30,
532 KVM_REG_MIPS_R31,
533
James Hogan70e92c7e2016-07-04 19:35:11 +0100534#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700535 KVM_REG_MIPS_HI,
536 KVM_REG_MIPS_LO,
James Hogan70e92c7e2016-07-04 19:35:11 +0100537#endif
David Daney4c73fb22013-05-23 09:49:09 -0700538 KVM_REG_MIPS_PC,
David Daney4c73fb22013-05-23 09:49:09 -0700539};
540
James Hogane5775932016-06-15 19:29:51 +0100541static u64 kvm_mips_get_one_regs_fpu[] = {
542 KVM_REG_MIPS_FCR_IR,
543 KVM_REG_MIPS_FCR_CSR,
544};
545
546static u64 kvm_mips_get_one_regs_msa[] = {
547 KVM_REG_MIPS_MSA_IR,
548 KVM_REG_MIPS_MSA_CSR,
549};
550
James Hoganf5c43bd2016-06-15 19:29:49 +0100551static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
552{
553 unsigned long ret;
554
555 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100556 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
557 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
558 /* odd doubles */
559 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
560 ret += 16;
561 }
562 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
563 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hoganf5c43bd2016-06-15 19:29:49 +0100564 ret += kvm_mips_callbacks->num_regs(vcpu);
565
566 return ret;
567}
568
569static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
570{
James Hogane5775932016-06-15 19:29:51 +0100571 u64 index;
572 unsigned int i;
573
James Hoganf5c43bd2016-06-15 19:29:49 +0100574 if (copy_to_user(indices, kvm_mips_get_one_regs,
575 sizeof(kvm_mips_get_one_regs)))
576 return -EFAULT;
577 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
578
James Hogane5775932016-06-15 19:29:51 +0100579 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
580 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
581 sizeof(kvm_mips_get_one_regs_fpu)))
582 return -EFAULT;
583 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
584
585 for (i = 0; i < 32; ++i) {
586 index = KVM_REG_MIPS_FPR_32(i);
587 if (copy_to_user(indices, &index, sizeof(index)))
588 return -EFAULT;
589 ++indices;
590
591 /* skip odd doubles if no F64 */
592 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
593 continue;
594
595 index = KVM_REG_MIPS_FPR_64(i);
596 if (copy_to_user(indices, &index, sizeof(index)))
597 return -EFAULT;
598 ++indices;
599 }
600 }
601
602 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
603 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
604 sizeof(kvm_mips_get_one_regs_msa)))
605 return -EFAULT;
606 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
607
608 for (i = 0; i < 32; ++i) {
609 index = KVM_REG_MIPS_VEC_128(i);
610 if (copy_to_user(indices, &index, sizeof(index)))
611 return -EFAULT;
612 ++indices;
613 }
614 }
615
James Hoganf5c43bd2016-06-15 19:29:49 +0100616 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
617}
618
David Daney4c73fb22013-05-23 09:49:09 -0700619static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
620 const struct kvm_one_reg *reg)
621{
David Daney4c73fb22013-05-23 09:49:09 -0700622 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000623 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100624 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700625 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000626 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000627 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700628
629 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000630 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700631 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
632 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
633 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100634#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700635 case KVM_REG_MIPS_HI:
636 v = (long)vcpu->arch.hi;
637 break;
638 case KVM_REG_MIPS_LO:
639 v = (long)vcpu->arch.lo;
640 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100641#endif
David Daney4c73fb22013-05-23 09:49:09 -0700642 case KVM_REG_MIPS_PC:
643 v = (long)vcpu->arch.pc;
644 break;
645
James Hogan379245c2014-12-02 15:48:24 +0000646 /* Floating point registers */
647 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
648 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
649 return -EINVAL;
650 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
651 /* Odd singles in top of even double when FR=0 */
652 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
653 v = get_fpr32(&fpu->fpr[idx], 0);
654 else
655 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
656 break;
657 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
658 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
659 return -EINVAL;
660 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
661 /* Can't access odd doubles in FR=0 mode */
662 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
663 return -EINVAL;
664 v = get_fpr64(&fpu->fpr[idx], 0);
665 break;
666 case KVM_REG_MIPS_FCR_IR:
667 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
668 return -EINVAL;
669 v = boot_cpu_data.fpu_id;
670 break;
671 case KVM_REG_MIPS_FCR_CSR:
672 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
673 return -EINVAL;
674 v = fpu->fcr31;
675 break;
676
James Hoganab86bd62014-12-02 15:48:24 +0000677 /* MIPS SIMD Architecture (MSA) registers */
678 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
679 if (!kvm_mips_guest_has_msa(&vcpu->arch))
680 return -EINVAL;
681 /* Can't access MSA registers in FR=0 mode */
682 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
683 return -EINVAL;
684 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
685#ifdef CONFIG_CPU_LITTLE_ENDIAN
686 /* least significant byte first */
687 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
688 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
689#else
690 /* most significant byte first */
691 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
692 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
693#endif
694 break;
695 case KVM_REG_MIPS_MSA_IR:
696 if (!kvm_mips_guest_has_msa(&vcpu->arch))
697 return -EINVAL;
698 v = boot_cpu_data.msa_id;
699 break;
700 case KVM_REG_MIPS_MSA_CSR:
701 if (!kvm_mips_guest_has_msa(&vcpu->arch))
702 return -EINVAL;
703 v = fpu->msacsr;
704 break;
705
James Hoganf8be02d2014-05-29 10:16:29 +0100706 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100707 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100708 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
709 if (ret)
710 return ret;
711 break;
David Daney4c73fb22013-05-23 09:49:09 -0700712 }
David Daney681865d2013-06-10 12:33:48 -0700713 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
714 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700715
David Daney681865d2013-06-10 12:33:48 -0700716 return put_user(v, uaddr64);
717 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
718 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
719 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700720
David Daney681865d2013-06-10 12:33:48 -0700721 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000722 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
723 void __user *uaddr = (void __user *)(long)reg->addr;
724
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200725 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700726 } else {
727 return -EINVAL;
728 }
David Daney4c73fb22013-05-23 09:49:09 -0700729}
730
731static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
732 const struct kvm_one_reg *reg)
733{
David Daney4c73fb22013-05-23 09:49:09 -0700734 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000735 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
736 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000737 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000738 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700739
David Daney681865d2013-06-10 12:33:48 -0700740 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
741 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
742
743 if (get_user(v, uaddr64) != 0)
744 return -EFAULT;
745 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
746 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
747 s32 v32;
748
749 if (get_user(v32, uaddr32) != 0)
750 return -EFAULT;
751 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000752 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
753 void __user *uaddr = (void __user *)(long)reg->addr;
754
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200755 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700756 } else {
757 return -EINVAL;
758 }
David Daney4c73fb22013-05-23 09:49:09 -0700759
760 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000761 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700762 case KVM_REG_MIPS_R0:
763 /* Silently ignore requests to set $0 */
764 break;
765 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
766 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
767 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100768#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700769 case KVM_REG_MIPS_HI:
770 vcpu->arch.hi = v;
771 break;
772 case KVM_REG_MIPS_LO:
773 vcpu->arch.lo = v;
774 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100775#endif
David Daney4c73fb22013-05-23 09:49:09 -0700776 case KVM_REG_MIPS_PC:
777 vcpu->arch.pc = v;
778 break;
779
James Hogan379245c2014-12-02 15:48:24 +0000780 /* Floating point registers */
781 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
782 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
783 return -EINVAL;
784 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
785 /* Odd singles in top of even double when FR=0 */
786 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
787 set_fpr32(&fpu->fpr[idx], 0, v);
788 else
789 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
790 break;
791 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
792 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
793 return -EINVAL;
794 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
795 /* Can't access odd doubles in FR=0 mode */
796 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
797 return -EINVAL;
798 set_fpr64(&fpu->fpr[idx], 0, v);
799 break;
800 case KVM_REG_MIPS_FCR_IR:
801 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
802 return -EINVAL;
803 /* Read-only */
804 break;
805 case KVM_REG_MIPS_FCR_CSR:
806 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
807 return -EINVAL;
808 fpu->fcr31 = v;
809 break;
810
James Hoganab86bd62014-12-02 15:48:24 +0000811 /* MIPS SIMD Architecture (MSA) registers */
812 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
813 if (!kvm_mips_guest_has_msa(&vcpu->arch))
814 return -EINVAL;
815 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
816#ifdef CONFIG_CPU_LITTLE_ENDIAN
817 /* least significant byte first */
818 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
819 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
820#else
821 /* most significant byte first */
822 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
823 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
824#endif
825 break;
826 case KVM_REG_MIPS_MSA_IR:
827 if (!kvm_mips_guest_has_msa(&vcpu->arch))
828 return -EINVAL;
829 /* Read-only */
830 break;
831 case KVM_REG_MIPS_MSA_CSR:
832 if (!kvm_mips_guest_has_msa(&vcpu->arch))
833 return -EINVAL;
834 fpu->msacsr = v;
835 break;
836
James Hoganf8be02d2014-05-29 10:16:29 +0100837 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -0700838 default:
James Hogancc68d222016-06-15 19:29:48 +0100839 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700840 }
841 return 0;
842}
843
James Hogan5fafd8742014-12-08 23:07:56 +0000844static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
845 struct kvm_enable_cap *cap)
846{
847 int r = 0;
848
849 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
850 return -EINVAL;
851 if (cap->flags)
852 return -EINVAL;
853 if (cap->args[0])
854 return -EINVAL;
855
856 switch (cap->cap) {
857 case KVM_CAP_MIPS_FPU:
858 vcpu->arch.fpu_enabled = true;
859 break;
James Hogand952bd02014-12-08 23:07:56 +0000860 case KVM_CAP_MIPS_MSA:
861 vcpu->arch.msa_enabled = true;
862 break;
James Hogan5fafd8742014-12-08 23:07:56 +0000863 default:
864 r = -EINVAL;
865 break;
866 }
867
868 return r;
869}
870
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700871long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
872 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800873{
874 struct kvm_vcpu *vcpu = filp->private_data;
875 void __user *argp = (void __user *)arg;
876 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800877
878 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -0700879 case KVM_SET_ONE_REG:
880 case KVM_GET_ONE_REG: {
881 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700882
David Daney4c73fb22013-05-23 09:49:09 -0700883 if (copy_from_user(&reg, argp, sizeof(reg)))
884 return -EFAULT;
885 if (ioctl == KVM_SET_ONE_REG)
886 return kvm_mips_set_reg(vcpu, &reg);
887 else
888 return kvm_mips_get_reg(vcpu, &reg);
889 }
890 case KVM_GET_REG_LIST: {
891 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -0700892 struct kvm_reg_list reg_list;
893 unsigned n;
894
895 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
896 return -EFAULT;
897 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +0100898 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -0700899 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
900 return -EFAULT;
901 if (n < reg_list.n)
902 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +0100903 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -0700904 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800905 case KVM_INTERRUPT:
906 {
907 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700908
Sanjay Lal669e8462012-11-21 18:34:02 -0800909 if (copy_from_user(&irq, argp, sizeof(irq)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +0100910 return -EFAULT;
Sanjay Lal669e8462012-11-21 18:34:02 -0800911 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
912 irq.irq);
913
914 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
915 break;
916 }
James Hogan5fafd8742014-12-08 23:07:56 +0000917 case KVM_ENABLE_CAP: {
918 struct kvm_enable_cap cap;
919
James Hogan5fafd8742014-12-08 23:07:56 +0000920 if (copy_from_user(&cap, argp, sizeof(cap)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +0100921 return -EFAULT;
James Hogan5fafd8742014-12-08 23:07:56 +0000922 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
923 break;
924 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800925 default:
David Daney4c73fb22013-05-23 09:49:09 -0700926 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800927 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800928 return r;
929}
930
James Hogane88643b2016-12-06 14:50:52 +0000931/**
932 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
933 * @kvm: kvm instance
934 * @log: slot id and address to which we copy the log
935 *
936 * Steps 1-4 below provide general overview of dirty page logging. See
937 * kvm_get_dirty_log_protect() function description for additional details.
938 *
939 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
940 * always flush the TLB (step 4) even if previous step failed and the dirty
941 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
942 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
943 * writes will be marked dirty for next log read.
944 *
945 * 1. Take a snapshot of the bit and clear it if needed.
946 * 2. Write protect the corresponding page.
947 * 3. Copy the snapshot to the userspace.
948 * 4. Flush TLB's if needed.
949 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800950int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
951{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200952 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -0800953 struct kvm_memory_slot *memslot;
James Hogane88643b2016-12-06 14:50:52 +0000954 bool is_dirty = false;
Sanjay Lal669e8462012-11-21 18:34:02 -0800955 int r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800956
957 mutex_lock(&kvm->slots_lock);
958
James Hogane88643b2016-12-06 14:50:52 +0000959 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Sanjay Lal669e8462012-11-21 18:34:02 -0800960
Sanjay Lal669e8462012-11-21 18:34:02 -0800961 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200962 slots = kvm_memslots(kvm);
963 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -0800964
James Hogane88643b2016-12-06 14:50:52 +0000965 /* Let implementation handle TLB/GVA invalidation */
966 kvm_mips_callbacks->flush_shadow_memslot(kvm, memslot);
Sanjay Lal669e8462012-11-21 18:34:02 -0800967 }
968
Sanjay Lal669e8462012-11-21 18:34:02 -0800969 mutex_unlock(&kvm->slots_lock);
970 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800971}
972
973long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
974{
975 long r;
976
977 switch (ioctl) {
978 default:
David Daneyed829852013-05-23 09:49:10 -0700979 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800980 }
981
982 return r;
983}
984
985int kvm_arch_init(void *opaque)
986{
Sanjay Lal669e8462012-11-21 18:34:02 -0800987 if (kvm_mips_callbacks) {
988 kvm_err("kvm: module already exists\n");
989 return -EEXIST;
990 }
991
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700992 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -0800993}
994
995void kvm_arch_exit(void)
996{
997 kvm_mips_callbacks = NULL;
998}
999
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001000int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1001 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001002{
David Daneyed829852013-05-23 09:49:10 -07001003 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001004}
1005
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001006int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1007 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001008{
David Daneyed829852013-05-23 09:49:10 -07001009 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001010}
1011
Dominik Dingel31928aa2014-12-04 15:47:07 +01001012void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001013{
Sanjay Lal669e8462012-11-21 18:34:02 -08001014}
1015
1016int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1017{
David Daneyed829852013-05-23 09:49:10 -07001018 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001019}
1020
1021int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1022{
David Daneyed829852013-05-23 09:49:10 -07001023 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001024}
1025
1026int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1027{
1028 return VM_FAULT_SIGBUS;
1029}
1030
Alexander Graf784aa3d2014-07-14 18:27:35 +02001031int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001032{
1033 int r;
1034
1035 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001036 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001037 case KVM_CAP_ENABLE_CAP:
James Hogan230c5722015-05-08 17:11:49 +01001038 case KVM_CAP_READONLY_MEM:
James Hogan411740f2016-12-13 16:32:39 +00001039 case KVM_CAP_SYNC_MMU:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01001040 case KVM_CAP_IMMEDIATE_EXIT:
David Daney4c73fb22013-05-23 09:49:09 -07001041 r = 1;
1042 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001043 case KVM_CAP_COALESCED_MMIO:
1044 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1045 break;
James Hogan12ed1fa2016-12-13 22:39:39 +00001046 case KVM_CAP_NR_VCPUS:
1047 r = num_online_cpus();
1048 break;
1049 case KVM_CAP_MAX_VCPUS:
1050 r = KVM_MAX_VCPUS;
1051 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001052 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001053 /* We don't handle systems with inconsistent cpu_has_fpu */
1054 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001055 break;
James Hogand952bd02014-12-08 23:07:56 +00001056 case KVM_CAP_MIPS_MSA:
1057 /*
1058 * We don't support MSA vector partitioning yet:
1059 * 1) It would require explicit support which can't be tested
1060 * yet due to lack of support in current hardware.
1061 * 2) It extends the state that would need to be saved/restored
1062 * by e.g. QEMU for migration.
1063 *
1064 * When vector partitioning hardware becomes available, support
1065 * could be added by requiring a flag when enabling
1066 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1067 * to save/restore the appropriate extra state.
1068 */
1069 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1070 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001071 default:
1072 r = 0;
1073 break;
1074 }
1075 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001076}
1077
1078int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1079{
1080 return kvm_mips_pending_timer(vcpu);
1081}
1082
1083int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1084{
1085 int i;
1086 struct mips_coproc *cop0;
1087
1088 if (!vcpu)
1089 return -1;
1090
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001091 kvm_debug("VCPU Register Dump:\n");
1092 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1093 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001094
1095 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001096 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001097 vcpu->arch.gprs[i],
1098 vcpu->arch.gprs[i + 1],
1099 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1100 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001101 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1102 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001103
1104 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001105 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1106 kvm_read_c0_guest_status(cop0),
1107 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001108
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001109 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001110
1111 return 0;
1112}
1113
1114int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1115{
1116 int i;
1117
David Daney8d17dd02013-05-23 09:49:08 -07001118 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001119 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001120 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001121 vcpu->arch.hi = regs->hi;
1122 vcpu->arch.lo = regs->lo;
1123 vcpu->arch.pc = regs->pc;
1124
David Daney4c73fb22013-05-23 09:49:09 -07001125 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001126}
1127
1128int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1129{
1130 int i;
1131
David Daney8d17dd02013-05-23 09:49:08 -07001132 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001133 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001134
1135 regs->hi = vcpu->arch.hi;
1136 regs->lo = vcpu->arch.lo;
1137 regs->pc = vcpu->arch.pc;
1138
David Daney4c73fb22013-05-23 09:49:09 -07001139 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001140}
1141
James Hogan0fae34f2014-05-29 10:16:39 +01001142static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001143{
1144 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1145
1146 kvm_mips_callbacks->queue_timer_int(vcpu);
1147
1148 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001149 if (swait_active(&vcpu->wq))
1150 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001151}
1152
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001153/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001154static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001155{
1156 struct kvm_vcpu *vcpu;
1157
1158 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1159 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001160 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001161}
1162
1163int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1164{
James Hoganf7f14272016-09-08 22:57:03 +01001165 int err;
1166
1167 err = kvm_mips_callbacks->vcpu_init(vcpu);
1168 if (err)
1169 return err;
1170
Sanjay Lal669e8462012-11-21 18:34:02 -08001171 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1172 HRTIMER_MODE_REL);
1173 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001174 return 0;
1175}
1176
James Hogan630766b2016-09-08 23:00:24 +01001177void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1178{
1179 kvm_mips_callbacks->vcpu_uninit(vcpu);
1180}
1181
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001182int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1183 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001184{
1185 return 0;
1186}
1187
1188/* Initial guest state */
1189int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1190{
1191 return kvm_mips_callbacks->vcpu_setup(vcpu);
1192}
1193
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001194static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001195{
James Hogan8cffd192016-06-09 14:19:08 +01001196 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001197
Sanjay Lal669e8462012-11-21 18:34:02 -08001198 if (cpu_has_dsp)
1199 status |= (ST0_MX);
1200
1201 write_c0_status(status);
1202 ehb();
1203}
1204
1205/*
1206 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1207 */
1208int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1209{
James Hogan8cffd192016-06-09 14:19:08 +01001210 u32 cause = vcpu->arch.host_cp0_cause;
1211 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1212 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001213 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1214 enum emulation_result er = EMULATE_DONE;
James Hogan122e51d2016-11-28 17:23:14 +00001215 u32 inst;
Sanjay Lal669e8462012-11-21 18:34:02 -08001216 int ret = RESUME_GUEST;
1217
James Hogan4841e0d2016-11-28 22:45:04 +00001218 vcpu->mode = OUTSIDE_GUEST_MODE;
1219
James Hoganc4c6f2c2015-02-04 10:52:03 +00001220 /* re-enable HTW before enabling interrupts */
1221 htw_start();
1222
Sanjay Lal669e8462012-11-21 18:34:02 -08001223 /* Set a default exit reason */
1224 run->exit_reason = KVM_EXIT_UNKNOWN;
1225 run->ready_for_interrupt_injection = 1;
1226
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001227 /*
1228 * Set the appropriate status bits based on host CPU features,
1229 * before we hit the scheduler
1230 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001231 kvm_mips_set_c0_status();
1232
1233 local_irq_enable();
1234
1235 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1236 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001237 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001238
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001239 /*
1240 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001241 * causing an exception to be delivered to the Guest Kernel
1242 */
1243 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1244 if (er == EMULATE_PRIV_FAIL) {
1245 goto skip_emul;
1246 } else if (er == EMULATE_FAIL) {
1247 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1248 ret = RESUME_HOST;
1249 goto skip_emul;
1250 }
1251
1252 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001253 case EXCCODE_INT:
1254 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001255
1256 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001257
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001258 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001259 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001260
1261 ret = RESUME_GUEST;
1262 break;
1263
James Hogan16d100db2015-12-16 23:49:33 +00001264 case EXCCODE_CPU:
1265 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001266
1267 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001268 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1269 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001270 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001271 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001272 break;
1273
James Hogan16d100db2015-12-16 23:49:33 +00001274 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001275 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001276 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1277 break;
1278
James Hogan16d100db2015-12-16 23:49:33 +00001279 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001280 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1281 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1282 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001283
1284 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001285 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1286 break;
1287
James Hogan16d100db2015-12-16 23:49:33 +00001288 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001289 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1290 cause, opc, badvaddr);
1291
1292 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001293 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1294 break;
1295
James Hogan16d100db2015-12-16 23:49:33 +00001296 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001297 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001298 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1299 break;
1300
James Hogan16d100db2015-12-16 23:49:33 +00001301 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001302 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001303 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1304 break;
1305
James Hogan16d100db2015-12-16 23:49:33 +00001306 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001307 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001308 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1309 break;
1310
James Hogan16d100db2015-12-16 23:49:33 +00001311 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001312 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001313 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1314 break;
1315
James Hogan16d100db2015-12-16 23:49:33 +00001316 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001317 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001318 ret = kvm_mips_callbacks->handle_break(vcpu);
1319 break;
1320
James Hogan16d100db2015-12-16 23:49:33 +00001321 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001322 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001323 ret = kvm_mips_callbacks->handle_trap(vcpu);
1324 break;
1325
James Hogan16d100db2015-12-16 23:49:33 +00001326 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001327 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001328 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1329 break;
1330
James Hogan16d100db2015-12-16 23:49:33 +00001331 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001332 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001333 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1334 break;
1335
James Hogan16d100db2015-12-16 23:49:33 +00001336 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001337 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001338 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1339 break;
1340
Sanjay Lal669e8462012-11-21 18:34:02 -08001341 default:
James Hogan122e51d2016-11-28 17:23:14 +00001342 if (cause & CAUSEF_BD)
1343 opc += 1;
1344 inst = 0;
James Hogan6a97c772015-04-23 16:54:35 +01001345 kvm_get_badinstr(opc, vcpu, &inst);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001346 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 +00001347 exccode, opc, inst, badvaddr,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001348 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001349 kvm_arch_vcpu_dump_regs(vcpu);
1350 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1351 ret = RESUME_HOST;
1352 break;
1353
1354 }
1355
1356skip_emul:
1357 local_irq_disable();
1358
1359 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1360 kvm_mips_deliver_interrupts(vcpu, cause);
1361
1362 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001363 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001364 if (signal_pending(current)) {
1365 run->exit_reason = KVM_EXIT_INTR;
1366 ret = (-EINTR << 2) | RESUME_HOST;
1367 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001368 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001369 }
1370 }
1371
James Hogan98e91b82014-11-18 14:09:12 +00001372 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001373 trace_kvm_reenter(vcpu);
1374
James Hogan4841e0d2016-11-28 22:45:04 +00001375 /*
1376 * Make sure the read of VCPU requests in vcpu_reenter()
1377 * callback is not reordered ahead of the write to vcpu->mode,
1378 * or we could miss a TLB flush request while the requester sees
1379 * the VCPU as outside of guest mode and not needing an IPI.
1380 */
1381 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
1382
James Hogana2c046e2016-11-18 13:14:37 +00001383 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +01001384
James Hogan98e91b82014-11-18 14:09:12 +00001385 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001386 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1387 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001388 *
1389 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001390 * vector, as it may well cause an [MSA] FP exception if there
1391 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001392 * kvm_mips_csr_die_notifier() for how that is handled).
1393 */
1394 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1395 read_c0_status() & ST0_CU1)
1396 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001397
1398 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1399 read_c0_config5() & MIPS_CONF5_MSAEN)
1400 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001401 }
1402
James Hoganc4c6f2c2015-02-04 10:52:03 +00001403 /* Disable HTW before returning to guest or host */
1404 htw_stop();
1405
Sanjay Lal669e8462012-11-21 18:34:02 -08001406 return ret;
1407}
1408
James Hogan98e91b82014-11-18 14:09:12 +00001409/* Enable FPU for guest and restore context */
1410void kvm_own_fpu(struct kvm_vcpu *vcpu)
1411{
1412 struct mips_coproc *cop0 = vcpu->arch.cop0;
1413 unsigned int sr, cfg5;
1414
1415 preempt_disable();
1416
James Hogan539cb89fb2015-03-05 11:43:36 +00001417 sr = kvm_read_c0_guest_status(cop0);
1418
1419 /*
1420 * If MSA state is already live, it is undefined how it interacts with
1421 * FR=0 FPU state, and we don't want to hit reserved instruction
1422 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1423 * play it safe and save it first.
1424 *
1425 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1426 * get called when guest CU1 is set, however we can't trust the guest
1427 * not to clobber the status register directly via the commpage.
1428 */
1429 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001430 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001431 kvm_lose_fpu(vcpu);
1432
James Hogan98e91b82014-11-18 14:09:12 +00001433 /*
1434 * Enable FPU for guest
1435 * We set FR and FRE according to guest context
1436 */
James Hogan98e91b82014-11-18 14:09:12 +00001437 change_c0_status(ST0_CU1 | ST0_FR, sr);
1438 if (cpu_has_fre) {
1439 cfg5 = kvm_read_c0_guest_config5(cop0);
1440 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1441 }
1442 enable_fpu_hazard();
1443
1444 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001445 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001446 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001447 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001448 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1449 } else {
1450 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001451 }
1452
1453 preempt_enable();
1454}
1455
James Hogan539cb89fb2015-03-05 11:43:36 +00001456#ifdef CONFIG_CPU_HAS_MSA
1457/* Enable MSA for guest and restore context */
1458void kvm_own_msa(struct kvm_vcpu *vcpu)
1459{
1460 struct mips_coproc *cop0 = vcpu->arch.cop0;
1461 unsigned int sr, cfg5;
1462
1463 preempt_disable();
1464
1465 /*
1466 * Enable FPU if enabled in guest, since we're restoring FPU context
1467 * anyway. We set FR and FRE according to guest context.
1468 */
1469 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1470 sr = kvm_read_c0_guest_status(cop0);
1471
1472 /*
1473 * If FR=0 FPU state is already live, it is undefined how it
1474 * interacts with MSA state, so play it safe and save it first.
1475 */
1476 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001477 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1478 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001479 kvm_lose_fpu(vcpu);
1480
1481 change_c0_status(ST0_CU1 | ST0_FR, sr);
1482 if (sr & ST0_CU1 && cpu_has_fre) {
1483 cfg5 = kvm_read_c0_guest_config5(cop0);
1484 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1485 }
1486 }
1487
1488 /* Enable MSA for guest */
1489 set_c0_config5(MIPS_CONF5_MSAEN);
1490 enable_fpu_hazard();
1491
James Hoganf9431762016-06-14 09:40:10 +01001492 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1493 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001494 /*
1495 * Guest FPU state already loaded, only restore upper MSA state
1496 */
1497 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001498 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001499 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001500 break;
1501 case 0:
1502 /* Neither FPU or MSA already active, restore full MSA state */
1503 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001504 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001505 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001506 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001507 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1508 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001509 break;
1510 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001511 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001512 break;
1513 }
1514
1515 preempt_enable();
1516}
1517#endif
1518
1519/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001520void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1521{
1522 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001523 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001524 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001525 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001526 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001527 }
James Hoganf9431762016-06-14 09:40:10 +01001528 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001529 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001530 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001531 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001532 }
1533 preempt_enable();
1534}
1535
James Hogan539cb89fb2015-03-05 11:43:36 +00001536/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001537void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1538{
1539 /*
James Hoganc58cf742017-03-14 10:15:17 +00001540 * With T&E, FPU & MSA get disabled in root context (hardware) when it
1541 * is disabled in guest context (software), but the register state in
1542 * the hardware may still be in use.
1543 * This is why we explicitly re-enable the hardware before saving.
James Hogan98e91b82014-11-18 14:09:12 +00001544 */
1545
1546 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001547 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hoganc58cf742017-03-14 10:15:17 +00001548 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) {
1549 set_c0_config5(MIPS_CONF5_MSAEN);
1550 enable_fpu_hazard();
1551 }
James Hogan539cb89fb2015-03-05 11:43:36 +00001552
1553 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001554 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001555
1556 /* Disable MSA & FPU */
1557 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001558 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001559 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001560 disable_fpu_hazard();
1561 }
James Hoganf9431762016-06-14 09:40:10 +01001562 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1563 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hoganc58cf742017-03-14 10:15:17 +00001564 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) {
1565 set_c0_status(ST0_CU1);
1566 enable_fpu_hazard();
1567 }
James Hogan98e91b82014-11-18 14:09:12 +00001568
1569 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001570 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001571 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001572
1573 /* Disable FPU */
1574 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001575 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001576 }
1577 preempt_enable();
1578}
1579
1580/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001581 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1582 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1583 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001584 */
1585static int kvm_mips_csr_die_notify(struct notifier_block *self,
1586 unsigned long cmd, void *ptr)
1587{
1588 struct die_args *args = (struct die_args *)ptr;
1589 struct pt_regs *regs = args->regs;
1590 unsigned long pc;
1591
James Hogan539cb89fb2015-03-05 11:43:36 +00001592 /* Only interested in FPE and MSAFPE */
1593 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001594 return NOTIFY_DONE;
1595
1596 /* Return immediately if guest context isn't active */
1597 if (!(current->flags & PF_VCPU))
1598 return NOTIFY_DONE;
1599
1600 /* Should never get here from user mode */
1601 BUG_ON(user_mode(regs));
1602
1603 pc = instruction_pointer(regs);
1604 switch (cmd) {
1605 case DIE_FP:
1606 /* match 2nd instruction in __kvm_restore_fcsr */
1607 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1608 return NOTIFY_DONE;
1609 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001610 case DIE_MSAFP:
1611 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1612 if (!cpu_has_msa ||
1613 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1614 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1615 return NOTIFY_DONE;
1616 break;
James Hogan98e91b82014-11-18 14:09:12 +00001617 }
1618
1619 /* Move PC forward a little and continue executing */
1620 instruction_pointer(regs) += 4;
1621
1622 return NOTIFY_STOP;
1623}
1624
1625static struct notifier_block kvm_mips_csr_die_notifier = {
1626 .notifier_call = kvm_mips_csr_die_notify,
1627};
1628
James Hogan2db9d232015-12-16 23:49:32 +00001629static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001630{
1631 int ret;
1632
James Hogan1e5217f52016-06-23 17:34:45 +01001633 ret = kvm_mips_entry_setup();
1634 if (ret)
1635 return ret;
1636
Sanjay Lal669e8462012-11-21 18:34:02 -08001637 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1638
1639 if (ret)
1640 return ret;
1641
James Hogan98e91b82014-11-18 14:09:12 +00001642 register_die_notifier(&kvm_mips_csr_die_notifier);
1643
Sanjay Lal669e8462012-11-21 18:34:02 -08001644 return 0;
1645}
1646
James Hogan2db9d232015-12-16 23:49:32 +00001647static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001648{
1649 kvm_exit();
1650
James Hogan98e91b82014-11-18 14:09:12 +00001651 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001652}
1653
1654module_init(kvm_mips_init);
1655module_exit(kvm_mips_exit);
1656
1657EXPORT_TRACEPOINT_SYMBOL(kvm_exit);