blob: 476ece99bf3be4a049312707bb651229ea7f4508 [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 Hogana8a3c422017-03-14 10:15:19 +0000110 switch (type) {
111 case KVM_VM_MIPS_TE:
112 break;
113 default:
114 /* Unsupported KVM type */
115 return -EINVAL;
116 };
117
James Hogan06c158c2015-05-01 13:50:18 +0100118 /* Allocate page table to map GPA -> RPA */
119 kvm->arch.gpa_mm.pgd = kvm_pgd_alloc();
120 if (!kvm->arch.gpa_mm.pgd)
121 return -ENOMEM;
122
Sanjay Lal669e8462012-11-21 18:34:02 -0800123 return 0;
124}
125
Luiz Capitulino235539b2016-09-07 14:47:23 -0400126bool kvm_arch_has_vcpu_debugfs(void)
127{
128 return false;
129}
130
131int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
132{
133 return 0;
134}
135
Sanjay Lal669e8462012-11-21 18:34:02 -0800136void kvm_mips_free_vcpus(struct kvm *kvm)
137{
138 unsigned int i;
139 struct kvm_vcpu *vcpu;
140
Sanjay Lal669e8462012-11-21 18:34:02 -0800141 kvm_for_each_vcpu(i, vcpu, kvm) {
142 kvm_arch_vcpu_free(vcpu);
143 }
144
145 mutex_lock(&kvm->lock);
146
147 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
148 kvm->vcpus[i] = NULL;
149
150 atomic_set(&kvm->online_vcpus, 0);
151
152 mutex_unlock(&kvm->lock);
153}
154
James Hogan06c158c2015-05-01 13:50:18 +0100155static void kvm_mips_free_gpa_pt(struct kvm *kvm)
156{
157 /* It should always be safe to remove after flushing the whole range */
158 WARN_ON(!kvm_mips_flush_gpa_pt(kvm, 0, ~0));
159 pgd_free(NULL, kvm->arch.gpa_mm.pgd);
160}
161
Sanjay Lal669e8462012-11-21 18:34:02 -0800162void kvm_arch_destroy_vm(struct kvm *kvm)
163{
164 kvm_mips_free_vcpus(kvm);
James Hogan06c158c2015-05-01 13:50:18 +0100165 kvm_mips_free_gpa_pt(kvm);
Sanjay Lal669e8462012-11-21 18:34:02 -0800166}
167
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700168long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl,
169 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800170{
David Daneyed829852013-05-23 09:49:10 -0700171 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800172}
173
Aneesh Kumar K.V55870272013-10-07 22:18:00 +0530174int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
175 unsigned long npages)
Sanjay Lal669e8462012-11-21 18:34:02 -0800176{
177 return 0;
178}
179
James Hoganb6209112016-10-25 00:01:37 +0100180void kvm_arch_flush_shadow_all(struct kvm *kvm)
181{
182 /* Flush whole GPA */
183 kvm_mips_flush_gpa_pt(kvm, 0, ~0);
184
185 /* Let implementation do the rest */
186 kvm_mips_callbacks->flush_shadow_all(kvm);
187}
188
189void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
190 struct kvm_memory_slot *slot)
191{
192 /*
193 * The slot has been made invalid (ready for moving or deletion), so we
194 * need to ensure that it can no longer be accessed by any guest VCPUs.
195 */
196
197 spin_lock(&kvm->mmu_lock);
198 /* Flush slot from GPA */
199 kvm_mips_flush_gpa_pt(kvm, slot->base_gfn,
200 slot->base_gfn + slot->npages - 1);
201 /* Let implementation do the rest */
202 kvm_mips_callbacks->flush_shadow_memslot(kvm, slot);
203 spin_unlock(&kvm->mmu_lock);
204}
205
Sanjay Lal669e8462012-11-21 18:34:02 -0800206int kvm_arch_prepare_memory_region(struct kvm *kvm,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700207 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200208 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700209 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800210{
211 return 0;
212}
213
214void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +0200215 const struct kvm_userspace_memory_region *mem,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700216 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +0200217 const struct kvm_memory_slot *new,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700218 enum kvm_mr_change change)
Sanjay Lal669e8462012-11-21 18:34:02 -0800219{
James Hogana1ac9e12016-12-06 14:56:20 +0000220 int needs_flush;
221
Sanjay Lal669e8462012-11-21 18:34:02 -0800222 kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n",
223 __func__, kvm, mem->slot, mem->guest_phys_addr,
224 mem->memory_size, mem->userspace_addr);
James Hogana1ac9e12016-12-06 14:56:20 +0000225
226 /*
227 * If dirty page logging is enabled, write protect all pages in the slot
228 * ready for dirty logging.
229 *
230 * There is no need to do this in any of the following cases:
231 * CREATE: No dirty mappings will already exist.
232 * MOVE/DELETE: The old mappings will already have been cleaned up by
233 * kvm_arch_flush_shadow_memslot()
234 */
235 if (change == KVM_MR_FLAGS_ONLY &&
236 (!(old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
237 new->flags & KVM_MEM_LOG_DIRTY_PAGES)) {
238 spin_lock(&kvm->mmu_lock);
239 /* Write protect GPA page table entries */
240 needs_flush = kvm_mips_mkclean_gpa_pt(kvm, new->base_gfn,
241 new->base_gfn + new->npages - 1);
242 /* Let implementation do the rest */
243 if (needs_flush)
244 kvm_mips_callbacks->flush_shadow_memslot(kvm, new);
245 spin_unlock(&kvm->mmu_lock);
246 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800247}
248
James Hogand7b8f892016-06-23 17:34:40 +0100249static inline void dump_handler(const char *symbol, void *start, void *end)
250{
251 u32 *p;
252
253 pr_debug("LEAF(%s)\n", symbol);
254
255 pr_debug("\t.set push\n");
256 pr_debug("\t.set noreorder\n");
257
258 for (p = start; p < (u32 *)end; ++p)
259 pr_debug("\t.word\t0x%08x\t\t# %p\n", *p, p);
260
261 pr_debug("\t.set\tpop\n");
262
263 pr_debug("\tEND(%s)\n", symbol);
264}
265
Sanjay Lal669e8462012-11-21 18:34:02 -0800266struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
267{
James Hogan90e93112016-06-23 17:34:39 +0100268 int err, size;
James Hogana7cfa7a2016-09-10 23:56:46 +0100269 void *gebase, *p, *handler, *refill_start, *refill_end;
Sanjay Lal669e8462012-11-21 18:34:02 -0800270 int i;
271
272 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
273
274 if (!vcpu) {
275 err = -ENOMEM;
276 goto out;
277 }
278
279 err = kvm_vcpu_init(vcpu, kvm, id);
280
281 if (err)
282 goto out_free_cpu;
283
James Hogan6e95bfd2014-05-29 10:16:43 +0100284 kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800285
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700286 /*
287 * Allocate space for host mode exception handlers that handle
Sanjay Lal669e8462012-11-21 18:34:02 -0800288 * guest mode exits
289 */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700290 if (cpu_has_veic || cpu_has_vint)
Sanjay Lal669e8462012-11-21 18:34:02 -0800291 size = 0x200 + VECTORSPACING * 64;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700292 else
James Hogan7006e2d2014-05-29 10:16:23 +0100293 size = 0x4000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800294
Sanjay Lal669e8462012-11-21 18:34:02 -0800295 gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
296
297 if (!gebase) {
298 err = -ENOMEM;
James Hogan585bb8f2015-11-11 14:21:20 +0000299 goto out_uninit_cpu;
Sanjay Lal669e8462012-11-21 18:34:02 -0800300 }
James Hogan6e95bfd2014-05-29 10:16:43 +0100301 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
302 ALIGN(size, PAGE_SIZE), gebase);
Sanjay Lal669e8462012-11-21 18:34:02 -0800303
James Hogan2a06dab2016-07-08 11:53:26 +0100304 /*
305 * Check new ebase actually fits in CP0_EBase. The lack of a write gate
306 * limits us to the low 512MB of physical address space. If the memory
307 * we allocate is out of range, just give up now.
308 */
309 if (!cpu_has_ebase_wg && virt_to_phys(gebase) >= 0x20000000) {
310 kvm_err("CP0_EBase.WG required for guest exception base %pK\n",
311 gebase);
312 err = -ENOMEM;
313 goto out_free_gebase;
314 }
315
Sanjay Lal669e8462012-11-21 18:34:02 -0800316 /* Save new ebase */
317 vcpu->arch.guest_ebase = gebase;
318
James Hogan90e93112016-06-23 17:34:39 +0100319 /* Build guest exception vectors dynamically in unmapped memory */
James Hogan1f9ca622016-06-23 17:34:46 +0100320 handler = gebase + 0x2000;
Sanjay Lal669e8462012-11-21 18:34:02 -0800321
James Hogana7cfa7a2016-09-10 23:56:46 +0100322 /* TLB refill */
323 refill_start = gebase;
324 refill_end = kvm_mips_build_tlb_refill_exception(refill_start, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800325
326 /* General Exception Entry point */
James Hogan1f9ca622016-06-23 17:34:46 +0100327 kvm_mips_build_exception(gebase + 0x180, handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800328
329 /* For vectored interrupts poke the exception code @ all offsets 0-7 */
330 for (i = 0; i < 8; i++) {
331 kvm_debug("L1 Vectored handler @ %p\n",
332 gebase + 0x200 + (i * VECTORSPACING));
James Hogan1f9ca622016-06-23 17:34:46 +0100333 kvm_mips_build_exception(gebase + 0x200 + i * VECTORSPACING,
334 handler);
Sanjay Lal669e8462012-11-21 18:34:02 -0800335 }
336
James Hogan90e93112016-06-23 17:34:39 +0100337 /* General exit handler */
James Hogan1f9ca622016-06-23 17:34:46 +0100338 p = handler;
James Hogan90e93112016-06-23 17:34:39 +0100339 p = kvm_mips_build_exit(p);
Sanjay Lal669e8462012-11-21 18:34:02 -0800340
James Hogan90e93112016-06-23 17:34:39 +0100341 /* Guest entry routine */
342 vcpu->arch.vcpu_run = p;
343 p = kvm_mips_build_vcpu_run(p);
James Hogan797179b2016-06-09 10:50:43 +0100344
James Hogand7b8f892016-06-23 17:34:40 +0100345 /* Dump the generated code */
346 pr_debug("#include <asm/asm.h>\n");
347 pr_debug("#include <asm/regdef.h>\n");
348 pr_debug("\n");
349 dump_handler("kvm_vcpu_run", vcpu->arch.vcpu_run, p);
James Hogana7cfa7a2016-09-10 23:56:46 +0100350 dump_handler("kvm_tlb_refill", refill_start, refill_end);
James Hogand7b8f892016-06-23 17:34:40 +0100351 dump_handler("kvm_gen_exc", gebase + 0x180, gebase + 0x200);
352 dump_handler("kvm_exit", gebase + 0x2000, vcpu->arch.vcpu_run);
353
Sanjay Lal669e8462012-11-21 18:34:02 -0800354 /* Invalidate the icache for these ranges */
James Hogan32eb12a2017-01-03 17:43:01 +0000355 flush_icache_range((unsigned long)gebase,
356 (unsigned long)gebase + ALIGN(size, PAGE_SIZE));
Sanjay Lal669e8462012-11-21 18:34:02 -0800357
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700358 /*
359 * Allocate comm page for guest kernel, a TLB will be reserved for
360 * mapping GVA @ 0xFFFF8000 to this page
361 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800362 vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL);
363
364 if (!vcpu->arch.kseg0_commpage) {
365 err = -ENOMEM;
366 goto out_free_gebase;
367 }
368
James Hogan6e95bfd2014-05-29 10:16:43 +0100369 kvm_debug("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage);
Sanjay Lal669e8462012-11-21 18:34:02 -0800370 kvm_mips_commpage_init(vcpu);
371
372 /* Init */
373 vcpu->arch.last_sched_cpu = -1;
374
375 /* Start off the timer */
James Hogane30492b2014-05-29 10:16:35 +0100376 kvm_mips_init_count(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800377
378 return vcpu;
379
380out_free_gebase:
381 kfree(gebase);
382
James Hogan585bb8f2015-11-11 14:21:20 +0000383out_uninit_cpu:
384 kvm_vcpu_uninit(vcpu);
385
Sanjay Lal669e8462012-11-21 18:34:02 -0800386out_free_cpu:
387 kfree(vcpu);
388
389out:
390 return ERR_PTR(err);
391}
392
393void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
394{
395 hrtimer_cancel(&vcpu->arch.comparecount_timer);
396
397 kvm_vcpu_uninit(vcpu);
398
399 kvm_mips_dump_stats(vcpu);
400
James Hoganaba85922016-12-16 15:57:00 +0000401 kvm_mmu_free_memory_caches(vcpu);
James Hoganc6c0a662014-05-29 10:16:44 +0100402 kfree(vcpu->arch.guest_ebase);
403 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700404 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800405}
406
407void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
408{
409 kvm_arch_vcpu_free(vcpu);
410}
411
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700412int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
413 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800414{
David Daneyed829852013-05-23 09:49:10 -0700415 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800416}
417
418int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
419{
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100420 int r = -EINTR;
Sanjay Lal669e8462012-11-21 18:34:02 -0800421 sigset_t sigsaved;
422
423 if (vcpu->sigset_active)
424 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
425
426 if (vcpu->mmio_needed) {
427 if (!vcpu->mmio_is_write)
428 kvm_mips_complete_mmio_load(vcpu, run);
429 vcpu->mmio_needed = 0;
430 }
431
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100432 if (run->immediate_exit)
433 goto out;
434
James Hoganf7982172015-02-04 17:06:37 +0000435 lose_fpu(1);
436
James Hogan044f0f02014-05-29 10:16:32 +0100437 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200438 guest_enter_irqoff();
James Hogan93258602016-06-14 09:40:14 +0100439 trace_kvm_enter(vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100440
James Hogan4841e0d2016-11-28 22:45:04 +0000441 /*
442 * Make sure the read of VCPU requests in vcpu_run() callback is not
443 * reordered ahead of the write to vcpu->mode, or we could miss a TLB
444 * flush request while the requester sees the VCPU as outside of guest
445 * mode and not needing an IPI.
446 */
447 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
448
James Hogana2c046e2016-11-18 13:14:37 +0000449 r = kvm_mips_callbacks->vcpu_run(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100450
James Hogan93258602016-06-14 09:40:14 +0100451 trace_kvm_out(vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200452 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800453 local_irq_enable();
454
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100455out:
Sanjay Lal669e8462012-11-21 18:34:02 -0800456 if (vcpu->sigset_active)
457 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
458
459 return r;
460}
461
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700462int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
463 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800464{
465 int intr = (int)irq->irq;
466 struct kvm_vcpu *dvcpu = NULL;
467
468 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
469 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
470 (int)intr);
471
472 if (irq->cpu == -1)
473 dvcpu = vcpu;
474 else
475 dvcpu = vcpu->kvm->vcpus[irq->cpu];
476
477 if (intr == 2 || intr == 3 || intr == 4) {
478 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
479
480 } else if (intr == -2 || intr == -3 || intr == -4) {
481 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
482 } else {
483 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
484 irq->cpu, irq->irq);
485 return -EINVAL;
486 }
487
488 dvcpu->arch.wait = 0;
489
Marcelo Tosatti85773702016-02-19 09:46:39 +0100490 if (swait_active(&dvcpu->wq))
491 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800492
493 return 0;
494}
495
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700496int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
497 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800498{
David Daneyed829852013-05-23 09:49:10 -0700499 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800500}
501
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700502int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
503 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800504{
David Daneyed829852013-05-23 09:49:10 -0700505 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800506}
507
David Daney4c73fb22013-05-23 09:49:09 -0700508static u64 kvm_mips_get_one_regs[] = {
509 KVM_REG_MIPS_R0,
510 KVM_REG_MIPS_R1,
511 KVM_REG_MIPS_R2,
512 KVM_REG_MIPS_R3,
513 KVM_REG_MIPS_R4,
514 KVM_REG_MIPS_R5,
515 KVM_REG_MIPS_R6,
516 KVM_REG_MIPS_R7,
517 KVM_REG_MIPS_R8,
518 KVM_REG_MIPS_R9,
519 KVM_REG_MIPS_R10,
520 KVM_REG_MIPS_R11,
521 KVM_REG_MIPS_R12,
522 KVM_REG_MIPS_R13,
523 KVM_REG_MIPS_R14,
524 KVM_REG_MIPS_R15,
525 KVM_REG_MIPS_R16,
526 KVM_REG_MIPS_R17,
527 KVM_REG_MIPS_R18,
528 KVM_REG_MIPS_R19,
529 KVM_REG_MIPS_R20,
530 KVM_REG_MIPS_R21,
531 KVM_REG_MIPS_R22,
532 KVM_REG_MIPS_R23,
533 KVM_REG_MIPS_R24,
534 KVM_REG_MIPS_R25,
535 KVM_REG_MIPS_R26,
536 KVM_REG_MIPS_R27,
537 KVM_REG_MIPS_R28,
538 KVM_REG_MIPS_R29,
539 KVM_REG_MIPS_R30,
540 KVM_REG_MIPS_R31,
541
James Hogan70e92c7e2016-07-04 19:35:11 +0100542#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700543 KVM_REG_MIPS_HI,
544 KVM_REG_MIPS_LO,
James Hogan70e92c7e2016-07-04 19:35:11 +0100545#endif
David Daney4c73fb22013-05-23 09:49:09 -0700546 KVM_REG_MIPS_PC,
David Daney4c73fb22013-05-23 09:49:09 -0700547};
548
James Hogane5775932016-06-15 19:29:51 +0100549static u64 kvm_mips_get_one_regs_fpu[] = {
550 KVM_REG_MIPS_FCR_IR,
551 KVM_REG_MIPS_FCR_CSR,
552};
553
554static u64 kvm_mips_get_one_regs_msa[] = {
555 KVM_REG_MIPS_MSA_IR,
556 KVM_REG_MIPS_MSA_CSR,
557};
558
James Hoganf5c43bd2016-06-15 19:29:49 +0100559static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
560{
561 unsigned long ret;
562
563 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100564 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
565 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
566 /* odd doubles */
567 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
568 ret += 16;
569 }
570 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
571 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hoganf5c43bd2016-06-15 19:29:49 +0100572 ret += kvm_mips_callbacks->num_regs(vcpu);
573
574 return ret;
575}
576
577static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
578{
James Hogane5775932016-06-15 19:29:51 +0100579 u64 index;
580 unsigned int i;
581
James Hoganf5c43bd2016-06-15 19:29:49 +0100582 if (copy_to_user(indices, kvm_mips_get_one_regs,
583 sizeof(kvm_mips_get_one_regs)))
584 return -EFAULT;
585 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
586
James Hogane5775932016-06-15 19:29:51 +0100587 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
588 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
589 sizeof(kvm_mips_get_one_regs_fpu)))
590 return -EFAULT;
591 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
592
593 for (i = 0; i < 32; ++i) {
594 index = KVM_REG_MIPS_FPR_32(i);
595 if (copy_to_user(indices, &index, sizeof(index)))
596 return -EFAULT;
597 ++indices;
598
599 /* skip odd doubles if no F64 */
600 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
601 continue;
602
603 index = KVM_REG_MIPS_FPR_64(i);
604 if (copy_to_user(indices, &index, sizeof(index)))
605 return -EFAULT;
606 ++indices;
607 }
608 }
609
610 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
611 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
612 sizeof(kvm_mips_get_one_regs_msa)))
613 return -EFAULT;
614 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
615
616 for (i = 0; i < 32; ++i) {
617 index = KVM_REG_MIPS_VEC_128(i);
618 if (copy_to_user(indices, &index, sizeof(index)))
619 return -EFAULT;
620 ++indices;
621 }
622 }
623
James Hoganf5c43bd2016-06-15 19:29:49 +0100624 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
625}
626
David Daney4c73fb22013-05-23 09:49:09 -0700627static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
628 const struct kvm_one_reg *reg)
629{
David Daney4c73fb22013-05-23 09:49:09 -0700630 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000631 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100632 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700633 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000634 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000635 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700636
637 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000638 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700639 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
640 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
641 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100642#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700643 case KVM_REG_MIPS_HI:
644 v = (long)vcpu->arch.hi;
645 break;
646 case KVM_REG_MIPS_LO:
647 v = (long)vcpu->arch.lo;
648 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100649#endif
David Daney4c73fb22013-05-23 09:49:09 -0700650 case KVM_REG_MIPS_PC:
651 v = (long)vcpu->arch.pc;
652 break;
653
James Hogan379245c2014-12-02 15:48:24 +0000654 /* Floating point registers */
655 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
656 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
657 return -EINVAL;
658 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
659 /* Odd singles in top of even double when FR=0 */
660 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
661 v = get_fpr32(&fpu->fpr[idx], 0);
662 else
663 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
664 break;
665 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
666 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
667 return -EINVAL;
668 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
669 /* Can't access odd doubles in FR=0 mode */
670 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
671 return -EINVAL;
672 v = get_fpr64(&fpu->fpr[idx], 0);
673 break;
674 case KVM_REG_MIPS_FCR_IR:
675 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
676 return -EINVAL;
677 v = boot_cpu_data.fpu_id;
678 break;
679 case KVM_REG_MIPS_FCR_CSR:
680 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
681 return -EINVAL;
682 v = fpu->fcr31;
683 break;
684
James Hoganab86bd62014-12-02 15:48:24 +0000685 /* MIPS SIMD Architecture (MSA) registers */
686 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
687 if (!kvm_mips_guest_has_msa(&vcpu->arch))
688 return -EINVAL;
689 /* Can't access MSA registers in FR=0 mode */
690 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
691 return -EINVAL;
692 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
693#ifdef CONFIG_CPU_LITTLE_ENDIAN
694 /* least significant byte first */
695 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
696 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
697#else
698 /* most significant byte first */
699 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
700 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
701#endif
702 break;
703 case KVM_REG_MIPS_MSA_IR:
704 if (!kvm_mips_guest_has_msa(&vcpu->arch))
705 return -EINVAL;
706 v = boot_cpu_data.msa_id;
707 break;
708 case KVM_REG_MIPS_MSA_CSR:
709 if (!kvm_mips_guest_has_msa(&vcpu->arch))
710 return -EINVAL;
711 v = fpu->msacsr;
712 break;
713
James Hoganf8be02d2014-05-29 10:16:29 +0100714 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100715 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100716 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
717 if (ret)
718 return ret;
719 break;
David Daney4c73fb22013-05-23 09:49:09 -0700720 }
David Daney681865d2013-06-10 12:33:48 -0700721 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
722 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700723
David Daney681865d2013-06-10 12:33:48 -0700724 return put_user(v, uaddr64);
725 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
726 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
727 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700728
David Daney681865d2013-06-10 12:33:48 -0700729 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000730 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
731 void __user *uaddr = (void __user *)(long)reg->addr;
732
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200733 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700734 } else {
735 return -EINVAL;
736 }
David Daney4c73fb22013-05-23 09:49:09 -0700737}
738
739static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
740 const struct kvm_one_reg *reg)
741{
David Daney4c73fb22013-05-23 09:49:09 -0700742 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000743 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
744 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000745 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000746 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700747
David Daney681865d2013-06-10 12:33:48 -0700748 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
749 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
750
751 if (get_user(v, uaddr64) != 0)
752 return -EFAULT;
753 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
754 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
755 s32 v32;
756
757 if (get_user(v32, uaddr32) != 0)
758 return -EFAULT;
759 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000760 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
761 void __user *uaddr = (void __user *)(long)reg->addr;
762
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200763 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700764 } else {
765 return -EINVAL;
766 }
David Daney4c73fb22013-05-23 09:49:09 -0700767
768 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000769 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700770 case KVM_REG_MIPS_R0:
771 /* Silently ignore requests to set $0 */
772 break;
773 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
774 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
775 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100776#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700777 case KVM_REG_MIPS_HI:
778 vcpu->arch.hi = v;
779 break;
780 case KVM_REG_MIPS_LO:
781 vcpu->arch.lo = v;
782 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100783#endif
David Daney4c73fb22013-05-23 09:49:09 -0700784 case KVM_REG_MIPS_PC:
785 vcpu->arch.pc = v;
786 break;
787
James Hogan379245c2014-12-02 15:48:24 +0000788 /* Floating point registers */
789 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
790 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
791 return -EINVAL;
792 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
793 /* Odd singles in top of even double when FR=0 */
794 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
795 set_fpr32(&fpu->fpr[idx], 0, v);
796 else
797 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
798 break;
799 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
800 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
801 return -EINVAL;
802 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
803 /* Can't access odd doubles in FR=0 mode */
804 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
805 return -EINVAL;
806 set_fpr64(&fpu->fpr[idx], 0, v);
807 break;
808 case KVM_REG_MIPS_FCR_IR:
809 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
810 return -EINVAL;
811 /* Read-only */
812 break;
813 case KVM_REG_MIPS_FCR_CSR:
814 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
815 return -EINVAL;
816 fpu->fcr31 = v;
817 break;
818
James Hoganab86bd62014-12-02 15:48:24 +0000819 /* MIPS SIMD Architecture (MSA) registers */
820 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
821 if (!kvm_mips_guest_has_msa(&vcpu->arch))
822 return -EINVAL;
823 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
824#ifdef CONFIG_CPU_LITTLE_ENDIAN
825 /* least significant byte first */
826 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
827 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
828#else
829 /* most significant byte first */
830 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
831 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
832#endif
833 break;
834 case KVM_REG_MIPS_MSA_IR:
835 if (!kvm_mips_guest_has_msa(&vcpu->arch))
836 return -EINVAL;
837 /* Read-only */
838 break;
839 case KVM_REG_MIPS_MSA_CSR:
840 if (!kvm_mips_guest_has_msa(&vcpu->arch))
841 return -EINVAL;
842 fpu->msacsr = v;
843 break;
844
James Hoganf8be02d2014-05-29 10:16:29 +0100845 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -0700846 default:
James Hogancc68d222016-06-15 19:29:48 +0100847 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700848 }
849 return 0;
850}
851
James Hogan5fafd8742014-12-08 23:07:56 +0000852static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
853 struct kvm_enable_cap *cap)
854{
855 int r = 0;
856
857 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
858 return -EINVAL;
859 if (cap->flags)
860 return -EINVAL;
861 if (cap->args[0])
862 return -EINVAL;
863
864 switch (cap->cap) {
865 case KVM_CAP_MIPS_FPU:
866 vcpu->arch.fpu_enabled = true;
867 break;
James Hogand952bd02014-12-08 23:07:56 +0000868 case KVM_CAP_MIPS_MSA:
869 vcpu->arch.msa_enabled = true;
870 break;
James Hogan5fafd8742014-12-08 23:07:56 +0000871 default:
872 r = -EINVAL;
873 break;
874 }
875
876 return r;
877}
878
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700879long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
880 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800881{
882 struct kvm_vcpu *vcpu = filp->private_data;
883 void __user *argp = (void __user *)arg;
884 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800885
886 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -0700887 case KVM_SET_ONE_REG:
888 case KVM_GET_ONE_REG: {
889 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700890
David Daney4c73fb22013-05-23 09:49:09 -0700891 if (copy_from_user(&reg, argp, sizeof(reg)))
892 return -EFAULT;
893 if (ioctl == KVM_SET_ONE_REG)
894 return kvm_mips_set_reg(vcpu, &reg);
895 else
896 return kvm_mips_get_reg(vcpu, &reg);
897 }
898 case KVM_GET_REG_LIST: {
899 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -0700900 struct kvm_reg_list reg_list;
901 unsigned n;
902
903 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
904 return -EFAULT;
905 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +0100906 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -0700907 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
908 return -EFAULT;
909 if (n < reg_list.n)
910 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +0100911 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -0700912 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800913 case KVM_INTERRUPT:
914 {
915 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700916
Sanjay Lal669e8462012-11-21 18:34:02 -0800917 if (copy_from_user(&irq, argp, sizeof(irq)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +0100918 return -EFAULT;
Sanjay Lal669e8462012-11-21 18:34:02 -0800919 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
920 irq.irq);
921
922 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
923 break;
924 }
James Hogan5fafd8742014-12-08 23:07:56 +0000925 case KVM_ENABLE_CAP: {
926 struct kvm_enable_cap cap;
927
James Hogan5fafd8742014-12-08 23:07:56 +0000928 if (copy_from_user(&cap, argp, sizeof(cap)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +0100929 return -EFAULT;
James Hogan5fafd8742014-12-08 23:07:56 +0000930 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
931 break;
932 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800933 default:
David Daney4c73fb22013-05-23 09:49:09 -0700934 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800935 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800936 return r;
937}
938
James Hogane88643b2016-12-06 14:50:52 +0000939/**
940 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
941 * @kvm: kvm instance
942 * @log: slot id and address to which we copy the log
943 *
944 * Steps 1-4 below provide general overview of dirty page logging. See
945 * kvm_get_dirty_log_protect() function description for additional details.
946 *
947 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
948 * always flush the TLB (step 4) even if previous step failed and the dirty
949 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
950 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
951 * writes will be marked dirty for next log read.
952 *
953 * 1. Take a snapshot of the bit and clear it if needed.
954 * 2. Write protect the corresponding page.
955 * 3. Copy the snapshot to the userspace.
956 * 4. Flush TLB's if needed.
957 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800958int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
959{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200960 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -0800961 struct kvm_memory_slot *memslot;
James Hogane88643b2016-12-06 14:50:52 +0000962 bool is_dirty = false;
Sanjay Lal669e8462012-11-21 18:34:02 -0800963 int r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800964
965 mutex_lock(&kvm->slots_lock);
966
James Hogane88643b2016-12-06 14:50:52 +0000967 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Sanjay Lal669e8462012-11-21 18:34:02 -0800968
Sanjay Lal669e8462012-11-21 18:34:02 -0800969 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200970 slots = kvm_memslots(kvm);
971 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -0800972
James Hogane88643b2016-12-06 14:50:52 +0000973 /* Let implementation handle TLB/GVA invalidation */
974 kvm_mips_callbacks->flush_shadow_memslot(kvm, memslot);
Sanjay Lal669e8462012-11-21 18:34:02 -0800975 }
976
Sanjay Lal669e8462012-11-21 18:34:02 -0800977 mutex_unlock(&kvm->slots_lock);
978 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800979}
980
981long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
982{
983 long r;
984
985 switch (ioctl) {
986 default:
David Daneyed829852013-05-23 09:49:10 -0700987 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800988 }
989
990 return r;
991}
992
993int kvm_arch_init(void *opaque)
994{
Sanjay Lal669e8462012-11-21 18:34:02 -0800995 if (kvm_mips_callbacks) {
996 kvm_err("kvm: module already exists\n");
997 return -EEXIST;
998 }
999
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -07001000 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -08001001}
1002
1003void kvm_arch_exit(void)
1004{
1005 kvm_mips_callbacks = NULL;
1006}
1007
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001008int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1009 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001010{
David Daneyed829852013-05-23 09:49:10 -07001011 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001012}
1013
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001014int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1015 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001016{
David Daneyed829852013-05-23 09:49:10 -07001017 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001018}
1019
Dominik Dingel31928aa2014-12-04 15:47:07 +01001020void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001021{
Sanjay Lal669e8462012-11-21 18:34:02 -08001022}
1023
1024int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1025{
David Daneyed829852013-05-23 09:49:10 -07001026 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001027}
1028
1029int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1030{
David Daneyed829852013-05-23 09:49:10 -07001031 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001032}
1033
1034int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1035{
1036 return VM_FAULT_SIGBUS;
1037}
1038
Alexander Graf784aa3d2014-07-14 18:27:35 +02001039int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001040{
1041 int r;
1042
1043 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001044 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001045 case KVM_CAP_ENABLE_CAP:
James Hogan230c5722015-05-08 17:11:49 +01001046 case KVM_CAP_READONLY_MEM:
James Hogan411740f2016-12-13 16:32:39 +00001047 case KVM_CAP_SYNC_MMU:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01001048 case KVM_CAP_IMMEDIATE_EXIT:
James Hogana8a3c422017-03-14 10:15:19 +00001049 case KVM_CAP_MIPS_TE:
David Daney4c73fb22013-05-23 09:49:09 -07001050 r = 1;
1051 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001052 case KVM_CAP_COALESCED_MMIO:
1053 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1054 break;
James Hogan12ed1fa2016-12-13 22:39:39 +00001055 case KVM_CAP_NR_VCPUS:
1056 r = num_online_cpus();
1057 break;
1058 case KVM_CAP_MAX_VCPUS:
1059 r = KVM_MAX_VCPUS;
1060 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001061 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001062 /* We don't handle systems with inconsistent cpu_has_fpu */
1063 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001064 break;
James Hogand952bd02014-12-08 23:07:56 +00001065 case KVM_CAP_MIPS_MSA:
1066 /*
1067 * We don't support MSA vector partitioning yet:
1068 * 1) It would require explicit support which can't be tested
1069 * yet due to lack of support in current hardware.
1070 * 2) It extends the state that would need to be saved/restored
1071 * by e.g. QEMU for migration.
1072 *
1073 * When vector partitioning hardware becomes available, support
1074 * could be added by requiring a flag when enabling
1075 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1076 * to save/restore the appropriate extra state.
1077 */
1078 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1079 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001080 default:
1081 r = 0;
1082 break;
1083 }
1084 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001085}
1086
1087int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1088{
1089 return kvm_mips_pending_timer(vcpu);
1090}
1091
1092int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1093{
1094 int i;
1095 struct mips_coproc *cop0;
1096
1097 if (!vcpu)
1098 return -1;
1099
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001100 kvm_debug("VCPU Register Dump:\n");
1101 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1102 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001103
1104 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001105 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001106 vcpu->arch.gprs[i],
1107 vcpu->arch.gprs[i + 1],
1108 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1109 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001110 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1111 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001112
1113 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001114 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1115 kvm_read_c0_guest_status(cop0),
1116 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001117
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001118 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001119
1120 return 0;
1121}
1122
1123int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1124{
1125 int i;
1126
David Daney8d17dd02013-05-23 09:49:08 -07001127 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001128 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001129 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001130 vcpu->arch.hi = regs->hi;
1131 vcpu->arch.lo = regs->lo;
1132 vcpu->arch.pc = regs->pc;
1133
David Daney4c73fb22013-05-23 09:49:09 -07001134 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001135}
1136
1137int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1138{
1139 int i;
1140
David Daney8d17dd02013-05-23 09:49:08 -07001141 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001142 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001143
1144 regs->hi = vcpu->arch.hi;
1145 regs->lo = vcpu->arch.lo;
1146 regs->pc = vcpu->arch.pc;
1147
David Daney4c73fb22013-05-23 09:49:09 -07001148 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001149}
1150
James Hogan0fae34f2014-05-29 10:16:39 +01001151static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001152{
1153 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1154
1155 kvm_mips_callbacks->queue_timer_int(vcpu);
1156
1157 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001158 if (swait_active(&vcpu->wq))
1159 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001160}
1161
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001162/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001163static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001164{
1165 struct kvm_vcpu *vcpu;
1166
1167 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1168 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001169 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001170}
1171
1172int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1173{
James Hoganf7f14272016-09-08 22:57:03 +01001174 int err;
1175
1176 err = kvm_mips_callbacks->vcpu_init(vcpu);
1177 if (err)
1178 return err;
1179
Sanjay Lal669e8462012-11-21 18:34:02 -08001180 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1181 HRTIMER_MODE_REL);
1182 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001183 return 0;
1184}
1185
James Hogan630766b32016-09-08 23:00:24 +01001186void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1187{
1188 kvm_mips_callbacks->vcpu_uninit(vcpu);
1189}
1190
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001191int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1192 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001193{
1194 return 0;
1195}
1196
1197/* Initial guest state */
1198int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1199{
1200 return kvm_mips_callbacks->vcpu_setup(vcpu);
1201}
1202
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001203static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001204{
James Hogan8cffd192016-06-09 14:19:08 +01001205 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001206
Sanjay Lal669e8462012-11-21 18:34:02 -08001207 if (cpu_has_dsp)
1208 status |= (ST0_MX);
1209
1210 write_c0_status(status);
1211 ehb();
1212}
1213
1214/*
1215 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1216 */
1217int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1218{
James Hogan8cffd192016-06-09 14:19:08 +01001219 u32 cause = vcpu->arch.host_cp0_cause;
1220 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1221 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001222 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1223 enum emulation_result er = EMULATE_DONE;
James Hogan122e51d2016-11-28 17:23:14 +00001224 u32 inst;
Sanjay Lal669e8462012-11-21 18:34:02 -08001225 int ret = RESUME_GUEST;
1226
James Hogan4841e0d2016-11-28 22:45:04 +00001227 vcpu->mode = OUTSIDE_GUEST_MODE;
1228
James Hoganc4c6f2c2015-02-04 10:52:03 +00001229 /* re-enable HTW before enabling interrupts */
1230 htw_start();
1231
Sanjay Lal669e8462012-11-21 18:34:02 -08001232 /* Set a default exit reason */
1233 run->exit_reason = KVM_EXIT_UNKNOWN;
1234 run->ready_for_interrupt_injection = 1;
1235
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001236 /*
1237 * Set the appropriate status bits based on host CPU features,
1238 * before we hit the scheduler
1239 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001240 kvm_mips_set_c0_status();
1241
1242 local_irq_enable();
1243
1244 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1245 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001246 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001247
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001248 /*
1249 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001250 * causing an exception to be delivered to the Guest Kernel
1251 */
1252 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1253 if (er == EMULATE_PRIV_FAIL) {
1254 goto skip_emul;
1255 } else if (er == EMULATE_FAIL) {
1256 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1257 ret = RESUME_HOST;
1258 goto skip_emul;
1259 }
1260
1261 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001262 case EXCCODE_INT:
1263 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001264
1265 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001266
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001267 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001268 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001269
1270 ret = RESUME_GUEST;
1271 break;
1272
James Hogan16d100db2015-12-16 23:49:33 +00001273 case EXCCODE_CPU:
1274 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001275
1276 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001277 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1278 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001279 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001280 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001281 break;
1282
James Hogan16d100db2015-12-16 23:49:33 +00001283 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001284 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001285 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1286 break;
1287
James Hogan16d100db2015-12-16 23:49:33 +00001288 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001289 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1290 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1291 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001292
1293 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001294 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1295 break;
1296
James Hogan16d100db2015-12-16 23:49:33 +00001297 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001298 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1299 cause, opc, badvaddr);
1300
1301 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001302 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1303 break;
1304
James Hogan16d100db2015-12-16 23:49:33 +00001305 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001306 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001307 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1308 break;
1309
James Hogan16d100db2015-12-16 23:49:33 +00001310 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001311 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001312 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1313 break;
1314
James Hogan16d100db2015-12-16 23:49:33 +00001315 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001316 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001317 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1318 break;
1319
James Hogan16d100db2015-12-16 23:49:33 +00001320 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001321 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001322 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1323 break;
1324
James Hogan16d100db2015-12-16 23:49:33 +00001325 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001326 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001327 ret = kvm_mips_callbacks->handle_break(vcpu);
1328 break;
1329
James Hogan16d100db2015-12-16 23:49:33 +00001330 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001331 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001332 ret = kvm_mips_callbacks->handle_trap(vcpu);
1333 break;
1334
James Hogan16d100db2015-12-16 23:49:33 +00001335 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001336 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001337 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1338 break;
1339
James Hogan16d100db2015-12-16 23:49:33 +00001340 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001341 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001342 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1343 break;
1344
James Hogan16d100db2015-12-16 23:49:33 +00001345 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001346 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001347 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1348 break;
1349
Sanjay Lal669e8462012-11-21 18:34:02 -08001350 default:
James Hogan122e51d2016-11-28 17:23:14 +00001351 if (cause & CAUSEF_BD)
1352 opc += 1;
1353 inst = 0;
James Hogan6a97c772015-04-23 16:54:35 +01001354 kvm_get_badinstr(opc, vcpu, &inst);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001355 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 +00001356 exccode, opc, inst, badvaddr,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001357 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001358 kvm_arch_vcpu_dump_regs(vcpu);
1359 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1360 ret = RESUME_HOST;
1361 break;
1362
1363 }
1364
1365skip_emul:
1366 local_irq_disable();
1367
1368 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1369 kvm_mips_deliver_interrupts(vcpu, cause);
1370
1371 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001372 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001373 if (signal_pending(current)) {
1374 run->exit_reason = KVM_EXIT_INTR;
1375 ret = (-EINTR << 2) | RESUME_HOST;
1376 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001377 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001378 }
1379 }
1380
James Hogan98e91b82014-11-18 14:09:12 +00001381 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001382 trace_kvm_reenter(vcpu);
1383
James Hogan4841e0d2016-11-28 22:45:04 +00001384 /*
1385 * Make sure the read of VCPU requests in vcpu_reenter()
1386 * callback is not reordered ahead of the write to vcpu->mode,
1387 * or we could miss a TLB flush request while the requester sees
1388 * the VCPU as outside of guest mode and not needing an IPI.
1389 */
1390 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
1391
James Hogana2c046e2016-11-18 13:14:37 +00001392 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +01001393
James Hogan98e91b82014-11-18 14:09:12 +00001394 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001395 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1396 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001397 *
1398 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001399 * vector, as it may well cause an [MSA] FP exception if there
1400 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001401 * kvm_mips_csr_die_notifier() for how that is handled).
1402 */
1403 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1404 read_c0_status() & ST0_CU1)
1405 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001406
1407 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1408 read_c0_config5() & MIPS_CONF5_MSAEN)
1409 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001410 }
1411
James Hoganc4c6f2c2015-02-04 10:52:03 +00001412 /* Disable HTW before returning to guest or host */
1413 htw_stop();
1414
Sanjay Lal669e8462012-11-21 18:34:02 -08001415 return ret;
1416}
1417
James Hogan98e91b82014-11-18 14:09:12 +00001418/* Enable FPU for guest and restore context */
1419void kvm_own_fpu(struct kvm_vcpu *vcpu)
1420{
1421 struct mips_coproc *cop0 = vcpu->arch.cop0;
1422 unsigned int sr, cfg5;
1423
1424 preempt_disable();
1425
James Hogan539cb89fb2015-03-05 11:43:36 +00001426 sr = kvm_read_c0_guest_status(cop0);
1427
1428 /*
1429 * If MSA state is already live, it is undefined how it interacts with
1430 * FR=0 FPU state, and we don't want to hit reserved instruction
1431 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1432 * play it safe and save it first.
1433 *
1434 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1435 * get called when guest CU1 is set, however we can't trust the guest
1436 * not to clobber the status register directly via the commpage.
1437 */
1438 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001439 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001440 kvm_lose_fpu(vcpu);
1441
James Hogan98e91b82014-11-18 14:09:12 +00001442 /*
1443 * Enable FPU for guest
1444 * We set FR and FRE according to guest context
1445 */
James Hogan98e91b82014-11-18 14:09:12 +00001446 change_c0_status(ST0_CU1 | ST0_FR, sr);
1447 if (cpu_has_fre) {
1448 cfg5 = kvm_read_c0_guest_config5(cop0);
1449 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1450 }
1451 enable_fpu_hazard();
1452
1453 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001454 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001455 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001456 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001457 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1458 } else {
1459 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001460 }
1461
1462 preempt_enable();
1463}
1464
James Hogan539cb89fb2015-03-05 11:43:36 +00001465#ifdef CONFIG_CPU_HAS_MSA
1466/* Enable MSA for guest and restore context */
1467void kvm_own_msa(struct kvm_vcpu *vcpu)
1468{
1469 struct mips_coproc *cop0 = vcpu->arch.cop0;
1470 unsigned int sr, cfg5;
1471
1472 preempt_disable();
1473
1474 /*
1475 * Enable FPU if enabled in guest, since we're restoring FPU context
1476 * anyway. We set FR and FRE according to guest context.
1477 */
1478 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1479 sr = kvm_read_c0_guest_status(cop0);
1480
1481 /*
1482 * If FR=0 FPU state is already live, it is undefined how it
1483 * interacts with MSA state, so play it safe and save it first.
1484 */
1485 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001486 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1487 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001488 kvm_lose_fpu(vcpu);
1489
1490 change_c0_status(ST0_CU1 | ST0_FR, sr);
1491 if (sr & ST0_CU1 && cpu_has_fre) {
1492 cfg5 = kvm_read_c0_guest_config5(cop0);
1493 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1494 }
1495 }
1496
1497 /* Enable MSA for guest */
1498 set_c0_config5(MIPS_CONF5_MSAEN);
1499 enable_fpu_hazard();
1500
James Hoganf9431762016-06-14 09:40:10 +01001501 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1502 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001503 /*
1504 * Guest FPU state already loaded, only restore upper MSA state
1505 */
1506 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001507 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001508 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001509 break;
1510 case 0:
1511 /* Neither FPU or MSA already active, restore full MSA state */
1512 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001513 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001514 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001515 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001516 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1517 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001518 break;
1519 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001520 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001521 break;
1522 }
1523
1524 preempt_enable();
1525}
1526#endif
1527
1528/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001529void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1530{
1531 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001532 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001533 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001534 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001535 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001536 }
James Hoganf9431762016-06-14 09:40:10 +01001537 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001538 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001539 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001540 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001541 }
1542 preempt_enable();
1543}
1544
James Hogan539cb89fb2015-03-05 11:43:36 +00001545/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001546void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1547{
1548 /*
James Hoganc58cf742017-03-14 10:15:17 +00001549 * With T&E, FPU & MSA get disabled in root context (hardware) when it
1550 * is disabled in guest context (software), but the register state in
1551 * the hardware may still be in use.
1552 * This is why we explicitly re-enable the hardware before saving.
James Hogan98e91b82014-11-18 14:09:12 +00001553 */
1554
1555 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001556 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hoganc58cf742017-03-14 10:15:17 +00001557 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) {
1558 set_c0_config5(MIPS_CONF5_MSAEN);
1559 enable_fpu_hazard();
1560 }
James Hogan539cb89fb2015-03-05 11:43:36 +00001561
1562 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001563 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001564
1565 /* Disable MSA & FPU */
1566 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001567 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001568 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001569 disable_fpu_hazard();
1570 }
James Hoganf9431762016-06-14 09:40:10 +01001571 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1572 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hoganc58cf742017-03-14 10:15:17 +00001573 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) {
1574 set_c0_status(ST0_CU1);
1575 enable_fpu_hazard();
1576 }
James Hogan98e91b82014-11-18 14:09:12 +00001577
1578 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001579 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001580 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001581
1582 /* Disable FPU */
1583 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001584 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001585 }
1586 preempt_enable();
1587}
1588
1589/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001590 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1591 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1592 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001593 */
1594static int kvm_mips_csr_die_notify(struct notifier_block *self,
1595 unsigned long cmd, void *ptr)
1596{
1597 struct die_args *args = (struct die_args *)ptr;
1598 struct pt_regs *regs = args->regs;
1599 unsigned long pc;
1600
James Hogan539cb89fb2015-03-05 11:43:36 +00001601 /* Only interested in FPE and MSAFPE */
1602 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001603 return NOTIFY_DONE;
1604
1605 /* Return immediately if guest context isn't active */
1606 if (!(current->flags & PF_VCPU))
1607 return NOTIFY_DONE;
1608
1609 /* Should never get here from user mode */
1610 BUG_ON(user_mode(regs));
1611
1612 pc = instruction_pointer(regs);
1613 switch (cmd) {
1614 case DIE_FP:
1615 /* match 2nd instruction in __kvm_restore_fcsr */
1616 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1617 return NOTIFY_DONE;
1618 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001619 case DIE_MSAFP:
1620 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1621 if (!cpu_has_msa ||
1622 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1623 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1624 return NOTIFY_DONE;
1625 break;
James Hogan98e91b82014-11-18 14:09:12 +00001626 }
1627
1628 /* Move PC forward a little and continue executing */
1629 instruction_pointer(regs) += 4;
1630
1631 return NOTIFY_STOP;
1632}
1633
1634static struct notifier_block kvm_mips_csr_die_notifier = {
1635 .notifier_call = kvm_mips_csr_die_notify,
1636};
1637
James Hogan2db9d232015-12-16 23:49:32 +00001638static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001639{
1640 int ret;
1641
James Hogan1e5217f52016-06-23 17:34:45 +01001642 ret = kvm_mips_entry_setup();
1643 if (ret)
1644 return ret;
1645
Sanjay Lal669e8462012-11-21 18:34:02 -08001646 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1647
1648 if (ret)
1649 return ret;
1650
James Hogan98e91b82014-11-18 14:09:12 +00001651 register_die_notifier(&kvm_mips_csr_die_notifier);
1652
Sanjay Lal669e8462012-11-21 18:34:02 -08001653 return 0;
1654}
1655
James Hogan2db9d232015-12-16 23:49:32 +00001656static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001657{
1658 kvm_exit();
1659
James Hogan98e91b82014-11-18 14:09:12 +00001660 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001661}
1662
1663module_init(kvm_mips_init);
1664module_exit(kvm_mips_exit);
1665
1666EXPORT_TRACEPOINT_SYMBOL(kvm_exit);