blob: 78d58c2528a9b2029aa4df5dddc29e988b80d678 [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
Sanjay Lal669e8462012-11-21 18:34:02 -0800375 return vcpu;
376
377out_free_gebase:
378 kfree(gebase);
379
James Hogan585bb8f2015-11-11 14:21:20 +0000380out_uninit_cpu:
381 kvm_vcpu_uninit(vcpu);
382
Sanjay Lal669e8462012-11-21 18:34:02 -0800383out_free_cpu:
384 kfree(vcpu);
385
386out:
387 return ERR_PTR(err);
388}
389
390void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
391{
392 hrtimer_cancel(&vcpu->arch.comparecount_timer);
393
394 kvm_vcpu_uninit(vcpu);
395
396 kvm_mips_dump_stats(vcpu);
397
James Hoganaba859292016-12-16 15:57:00 +0000398 kvm_mmu_free_memory_caches(vcpu);
James Hoganc6c0a662014-05-29 10:16:44 +0100399 kfree(vcpu->arch.guest_ebase);
400 kfree(vcpu->arch.kseg0_commpage);
Deng-Cheng Zhu8c9eb042014-06-24 10:31:08 -0700401 kfree(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -0800402}
403
404void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
405{
406 kvm_arch_vcpu_free(vcpu);
407}
408
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700409int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
410 struct kvm_guest_debug *dbg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800411{
David Daneyed829852013-05-23 09:49:10 -0700412 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800413}
414
415int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
416{
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100417 int r = -EINTR;
Sanjay Lal669e8462012-11-21 18:34:02 -0800418 sigset_t sigsaved;
419
420 if (vcpu->sigset_active)
421 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
422
423 if (vcpu->mmio_needed) {
424 if (!vcpu->mmio_is_write)
425 kvm_mips_complete_mmio_load(vcpu, run);
426 vcpu->mmio_needed = 0;
427 }
428
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100429 if (run->immediate_exit)
430 goto out;
431
James Hoganf7982172015-02-04 17:06:37 +0000432 lose_fpu(1);
433
James Hogan044f0f02014-05-29 10:16:32 +0100434 local_irq_disable();
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200435 guest_enter_irqoff();
James Hogan93258602016-06-14 09:40:14 +0100436 trace_kvm_enter(vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100437
James Hogan4841e0d2016-11-28 22:45:04 +0000438 /*
439 * Make sure the read of VCPU requests in vcpu_run() callback is not
440 * reordered ahead of the write to vcpu->mode, or we could miss a TLB
441 * flush request while the requester sees the VCPU as outside of guest
442 * mode and not needing an IPI.
443 */
444 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
445
James Hogana2c046e2016-11-18 13:14:37 +0000446 r = kvm_mips_callbacks->vcpu_run(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +0100447
James Hogan93258602016-06-14 09:40:14 +0100448 trace_kvm_out(vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200449 guest_exit_irqoff();
Sanjay Lal669e8462012-11-21 18:34:02 -0800450 local_irq_enable();
451
Paolo Bonzini460df4c2017-02-08 11:50:15 +0100452out:
Sanjay Lal669e8462012-11-21 18:34:02 -0800453 if (vcpu->sigset_active)
454 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
455
456 return r;
457}
458
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700459int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
460 struct kvm_mips_interrupt *irq)
Sanjay Lal669e8462012-11-21 18:34:02 -0800461{
462 int intr = (int)irq->irq;
463 struct kvm_vcpu *dvcpu = NULL;
464
465 if (intr == 3 || intr == -3 || intr == 4 || intr == -4)
466 kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu,
467 (int)intr);
468
469 if (irq->cpu == -1)
470 dvcpu = vcpu;
471 else
472 dvcpu = vcpu->kvm->vcpus[irq->cpu];
473
474 if (intr == 2 || intr == 3 || intr == 4) {
475 kvm_mips_callbacks->queue_io_int(dvcpu, irq);
476
477 } else if (intr == -2 || intr == -3 || intr == -4) {
478 kvm_mips_callbacks->dequeue_io_int(dvcpu, irq);
479 } else {
480 kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
481 irq->cpu, irq->irq);
482 return -EINVAL;
483 }
484
485 dvcpu->arch.wait = 0;
486
Marcelo Tosatti85773702016-02-19 09:46:39 +0100487 if (swait_active(&dvcpu->wq))
488 swake_up(&dvcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -0800489
490 return 0;
491}
492
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700493int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
494 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800495{
David Daneyed829852013-05-23 09:49:10 -0700496 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800497}
498
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700499int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
500 struct kvm_mp_state *mp_state)
Sanjay Lal669e8462012-11-21 18:34:02 -0800501{
David Daneyed829852013-05-23 09:49:10 -0700502 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800503}
504
David Daney4c73fb22013-05-23 09:49:09 -0700505static u64 kvm_mips_get_one_regs[] = {
506 KVM_REG_MIPS_R0,
507 KVM_REG_MIPS_R1,
508 KVM_REG_MIPS_R2,
509 KVM_REG_MIPS_R3,
510 KVM_REG_MIPS_R4,
511 KVM_REG_MIPS_R5,
512 KVM_REG_MIPS_R6,
513 KVM_REG_MIPS_R7,
514 KVM_REG_MIPS_R8,
515 KVM_REG_MIPS_R9,
516 KVM_REG_MIPS_R10,
517 KVM_REG_MIPS_R11,
518 KVM_REG_MIPS_R12,
519 KVM_REG_MIPS_R13,
520 KVM_REG_MIPS_R14,
521 KVM_REG_MIPS_R15,
522 KVM_REG_MIPS_R16,
523 KVM_REG_MIPS_R17,
524 KVM_REG_MIPS_R18,
525 KVM_REG_MIPS_R19,
526 KVM_REG_MIPS_R20,
527 KVM_REG_MIPS_R21,
528 KVM_REG_MIPS_R22,
529 KVM_REG_MIPS_R23,
530 KVM_REG_MIPS_R24,
531 KVM_REG_MIPS_R25,
532 KVM_REG_MIPS_R26,
533 KVM_REG_MIPS_R27,
534 KVM_REG_MIPS_R28,
535 KVM_REG_MIPS_R29,
536 KVM_REG_MIPS_R30,
537 KVM_REG_MIPS_R31,
538
James Hogan70e92c7e2016-07-04 19:35:11 +0100539#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700540 KVM_REG_MIPS_HI,
541 KVM_REG_MIPS_LO,
James Hogan70e92c7e2016-07-04 19:35:11 +0100542#endif
David Daney4c73fb22013-05-23 09:49:09 -0700543 KVM_REG_MIPS_PC,
David Daney4c73fb22013-05-23 09:49:09 -0700544};
545
James Hogane5775932016-06-15 19:29:51 +0100546static u64 kvm_mips_get_one_regs_fpu[] = {
547 KVM_REG_MIPS_FCR_IR,
548 KVM_REG_MIPS_FCR_CSR,
549};
550
551static u64 kvm_mips_get_one_regs_msa[] = {
552 KVM_REG_MIPS_MSA_IR,
553 KVM_REG_MIPS_MSA_CSR,
554};
555
James Hoganf5c43bd2016-06-15 19:29:49 +0100556static unsigned long kvm_mips_num_regs(struct kvm_vcpu *vcpu)
557{
558 unsigned long ret;
559
560 ret = ARRAY_SIZE(kvm_mips_get_one_regs);
James Hogane5775932016-06-15 19:29:51 +0100561 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
562 ret += ARRAY_SIZE(kvm_mips_get_one_regs_fpu) + 48;
563 /* odd doubles */
564 if (boot_cpu_data.fpu_id & MIPS_FPIR_F64)
565 ret += 16;
566 }
567 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
568 ret += ARRAY_SIZE(kvm_mips_get_one_regs_msa) + 32;
James Hoganf5c43bd2016-06-15 19:29:49 +0100569 ret += kvm_mips_callbacks->num_regs(vcpu);
570
571 return ret;
572}
573
574static int kvm_mips_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
575{
James Hogane5775932016-06-15 19:29:51 +0100576 u64 index;
577 unsigned int i;
578
James Hoganf5c43bd2016-06-15 19:29:49 +0100579 if (copy_to_user(indices, kvm_mips_get_one_regs,
580 sizeof(kvm_mips_get_one_regs)))
581 return -EFAULT;
582 indices += ARRAY_SIZE(kvm_mips_get_one_regs);
583
James Hogane5775932016-06-15 19:29:51 +0100584 if (kvm_mips_guest_can_have_fpu(&vcpu->arch)) {
585 if (copy_to_user(indices, kvm_mips_get_one_regs_fpu,
586 sizeof(kvm_mips_get_one_regs_fpu)))
587 return -EFAULT;
588 indices += ARRAY_SIZE(kvm_mips_get_one_regs_fpu);
589
590 for (i = 0; i < 32; ++i) {
591 index = KVM_REG_MIPS_FPR_32(i);
592 if (copy_to_user(indices, &index, sizeof(index)))
593 return -EFAULT;
594 ++indices;
595
596 /* skip odd doubles if no F64 */
597 if (i & 1 && !(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
598 continue;
599
600 index = KVM_REG_MIPS_FPR_64(i);
601 if (copy_to_user(indices, &index, sizeof(index)))
602 return -EFAULT;
603 ++indices;
604 }
605 }
606
607 if (kvm_mips_guest_can_have_msa(&vcpu->arch)) {
608 if (copy_to_user(indices, kvm_mips_get_one_regs_msa,
609 sizeof(kvm_mips_get_one_regs_msa)))
610 return -EFAULT;
611 indices += ARRAY_SIZE(kvm_mips_get_one_regs_msa);
612
613 for (i = 0; i < 32; ++i) {
614 index = KVM_REG_MIPS_VEC_128(i);
615 if (copy_to_user(indices, &index, sizeof(index)))
616 return -EFAULT;
617 ++indices;
618 }
619 }
620
James Hoganf5c43bd2016-06-15 19:29:49 +0100621 return kvm_mips_callbacks->copy_reg_indices(vcpu, indices);
622}
623
David Daney4c73fb22013-05-23 09:49:09 -0700624static int kvm_mips_get_reg(struct kvm_vcpu *vcpu,
625 const struct kvm_one_reg *reg)
626{
David Daney4c73fb22013-05-23 09:49:09 -0700627 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000628 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
James Hoganf8be02d2014-05-29 10:16:29 +0100629 int ret;
David Daney4c73fb22013-05-23 09:49:09 -0700630 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000631 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000632 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700633
634 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000635 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700636 case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31:
637 v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0];
638 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100639#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700640 case KVM_REG_MIPS_HI:
641 v = (long)vcpu->arch.hi;
642 break;
643 case KVM_REG_MIPS_LO:
644 v = (long)vcpu->arch.lo;
645 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100646#endif
David Daney4c73fb22013-05-23 09:49:09 -0700647 case KVM_REG_MIPS_PC:
648 v = (long)vcpu->arch.pc;
649 break;
650
James Hogan379245c2014-12-02 15:48:24 +0000651 /* Floating point registers */
652 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
653 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
654 return -EINVAL;
655 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
656 /* Odd singles in top of even double when FR=0 */
657 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
658 v = get_fpr32(&fpu->fpr[idx], 0);
659 else
660 v = get_fpr32(&fpu->fpr[idx & ~1], idx & 1);
661 break;
662 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
663 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
664 return -EINVAL;
665 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
666 /* Can't access odd doubles in FR=0 mode */
667 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
668 return -EINVAL;
669 v = get_fpr64(&fpu->fpr[idx], 0);
670 break;
671 case KVM_REG_MIPS_FCR_IR:
672 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
673 return -EINVAL;
674 v = boot_cpu_data.fpu_id;
675 break;
676 case KVM_REG_MIPS_FCR_CSR:
677 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
678 return -EINVAL;
679 v = fpu->fcr31;
680 break;
681
James Hoganab86bd62014-12-02 15:48:24 +0000682 /* MIPS SIMD Architecture (MSA) registers */
683 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
684 if (!kvm_mips_guest_has_msa(&vcpu->arch))
685 return -EINVAL;
686 /* Can't access MSA registers in FR=0 mode */
687 if (!(kvm_read_c0_guest_status(cop0) & ST0_FR))
688 return -EINVAL;
689 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
690#ifdef CONFIG_CPU_LITTLE_ENDIAN
691 /* least significant byte first */
692 vs[0] = get_fpr64(&fpu->fpr[idx], 0);
693 vs[1] = get_fpr64(&fpu->fpr[idx], 1);
694#else
695 /* most significant byte first */
696 vs[0] = get_fpr64(&fpu->fpr[idx], 1);
697 vs[1] = get_fpr64(&fpu->fpr[idx], 0);
698#endif
699 break;
700 case KVM_REG_MIPS_MSA_IR:
701 if (!kvm_mips_guest_has_msa(&vcpu->arch))
702 return -EINVAL;
703 v = boot_cpu_data.msa_id;
704 break;
705 case KVM_REG_MIPS_MSA_CSR:
706 if (!kvm_mips_guest_has_msa(&vcpu->arch))
707 return -EINVAL;
708 v = fpu->msacsr;
709 break;
710
James Hoganf8be02d2014-05-29 10:16:29 +0100711 /* registers to be handled specially */
James Hogancc68d222016-06-15 19:29:48 +0100712 default:
James Hoganf8be02d2014-05-29 10:16:29 +0100713 ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v);
714 if (ret)
715 return ret;
716 break;
David Daney4c73fb22013-05-23 09:49:09 -0700717 }
David Daney681865d2013-06-10 12:33:48 -0700718 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
719 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700720
David Daney681865d2013-06-10 12:33:48 -0700721 return put_user(v, uaddr64);
722 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
723 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
724 u32 v32 = (u32)v;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700725
David Daney681865d2013-06-10 12:33:48 -0700726 return put_user(v32, uaddr32);
James Hoganab86bd62014-12-02 15:48:24 +0000727 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
728 void __user *uaddr = (void __user *)(long)reg->addr;
729
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200730 return copy_to_user(uaddr, vs, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700731 } else {
732 return -EINVAL;
733 }
David Daney4c73fb22013-05-23 09:49:09 -0700734}
735
736static int kvm_mips_set_reg(struct kvm_vcpu *vcpu,
737 const struct kvm_one_reg *reg)
738{
David Daney4c73fb22013-05-23 09:49:09 -0700739 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan379245c2014-12-02 15:48:24 +0000740 struct mips_fpu_struct *fpu = &vcpu->arch.fpu;
741 s64 v;
James Hoganab86bd62014-12-02 15:48:24 +0000742 s64 vs[2];
James Hogan379245c2014-12-02 15:48:24 +0000743 unsigned int idx;
David Daney4c73fb22013-05-23 09:49:09 -0700744
David Daney681865d2013-06-10 12:33:48 -0700745 if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) {
746 u64 __user *uaddr64 = (u64 __user *)(long)reg->addr;
747
748 if (get_user(v, uaddr64) != 0)
749 return -EFAULT;
750 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) {
751 u32 __user *uaddr32 = (u32 __user *)(long)reg->addr;
752 s32 v32;
753
754 if (get_user(v32, uaddr32) != 0)
755 return -EFAULT;
756 v = (s64)v32;
James Hoganab86bd62014-12-02 15:48:24 +0000757 } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U128) {
758 void __user *uaddr = (void __user *)(long)reg->addr;
759
Michael S. Tsirkin0178fd72016-02-28 17:35:59 +0200760 return copy_from_user(vs, uaddr, 16) ? -EFAULT : 0;
David Daney681865d2013-06-10 12:33:48 -0700761 } else {
762 return -EINVAL;
763 }
David Daney4c73fb22013-05-23 09:49:09 -0700764
765 switch (reg->id) {
James Hogan379245c2014-12-02 15:48:24 +0000766 /* General purpose registers */
David Daney4c73fb22013-05-23 09:49:09 -0700767 case KVM_REG_MIPS_R0:
768 /* Silently ignore requests to set $0 */
769 break;
770 case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31:
771 vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v;
772 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100773#ifndef CONFIG_CPU_MIPSR6
David Daney4c73fb22013-05-23 09:49:09 -0700774 case KVM_REG_MIPS_HI:
775 vcpu->arch.hi = v;
776 break;
777 case KVM_REG_MIPS_LO:
778 vcpu->arch.lo = v;
779 break;
James Hogan70e92c7e2016-07-04 19:35:11 +0100780#endif
David Daney4c73fb22013-05-23 09:49:09 -0700781 case KVM_REG_MIPS_PC:
782 vcpu->arch.pc = v;
783 break;
784
James Hogan379245c2014-12-02 15:48:24 +0000785 /* Floating point registers */
786 case KVM_REG_MIPS_FPR_32(0) ... KVM_REG_MIPS_FPR_32(31):
787 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
788 return -EINVAL;
789 idx = reg->id - KVM_REG_MIPS_FPR_32(0);
790 /* Odd singles in top of even double when FR=0 */
791 if (kvm_read_c0_guest_status(cop0) & ST0_FR)
792 set_fpr32(&fpu->fpr[idx], 0, v);
793 else
794 set_fpr32(&fpu->fpr[idx & ~1], idx & 1, v);
795 break;
796 case KVM_REG_MIPS_FPR_64(0) ... KVM_REG_MIPS_FPR_64(31):
797 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
798 return -EINVAL;
799 idx = reg->id - KVM_REG_MIPS_FPR_64(0);
800 /* Can't access odd doubles in FR=0 mode */
801 if (idx & 1 && !(kvm_read_c0_guest_status(cop0) & ST0_FR))
802 return -EINVAL;
803 set_fpr64(&fpu->fpr[idx], 0, v);
804 break;
805 case KVM_REG_MIPS_FCR_IR:
806 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
807 return -EINVAL;
808 /* Read-only */
809 break;
810 case KVM_REG_MIPS_FCR_CSR:
811 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
812 return -EINVAL;
813 fpu->fcr31 = v;
814 break;
815
James Hoganab86bd62014-12-02 15:48:24 +0000816 /* MIPS SIMD Architecture (MSA) registers */
817 case KVM_REG_MIPS_VEC_128(0) ... KVM_REG_MIPS_VEC_128(31):
818 if (!kvm_mips_guest_has_msa(&vcpu->arch))
819 return -EINVAL;
820 idx = reg->id - KVM_REG_MIPS_VEC_128(0);
821#ifdef CONFIG_CPU_LITTLE_ENDIAN
822 /* least significant byte first */
823 set_fpr64(&fpu->fpr[idx], 0, vs[0]);
824 set_fpr64(&fpu->fpr[idx], 1, vs[1]);
825#else
826 /* most significant byte first */
827 set_fpr64(&fpu->fpr[idx], 1, vs[0]);
828 set_fpr64(&fpu->fpr[idx], 0, vs[1]);
829#endif
830 break;
831 case KVM_REG_MIPS_MSA_IR:
832 if (!kvm_mips_guest_has_msa(&vcpu->arch))
833 return -EINVAL;
834 /* Read-only */
835 break;
836 case KVM_REG_MIPS_MSA_CSR:
837 if (!kvm_mips_guest_has_msa(&vcpu->arch))
838 return -EINVAL;
839 fpu->msacsr = v;
840 break;
841
James Hoganf8be02d2014-05-29 10:16:29 +0100842 /* registers to be handled specially */
David Daney4c73fb22013-05-23 09:49:09 -0700843 default:
James Hogancc68d222016-06-15 19:29:48 +0100844 return kvm_mips_callbacks->set_one_reg(vcpu, reg, v);
David Daney4c73fb22013-05-23 09:49:09 -0700845 }
846 return 0;
847}
848
James Hogan5fafd8742014-12-08 23:07:56 +0000849static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
850 struct kvm_enable_cap *cap)
851{
852 int r = 0;
853
854 if (!kvm_vm_ioctl_check_extension(vcpu->kvm, cap->cap))
855 return -EINVAL;
856 if (cap->flags)
857 return -EINVAL;
858 if (cap->args[0])
859 return -EINVAL;
860
861 switch (cap->cap) {
862 case KVM_CAP_MIPS_FPU:
863 vcpu->arch.fpu_enabled = true;
864 break;
James Hogand952bd02014-12-08 23:07:56 +0000865 case KVM_CAP_MIPS_MSA:
866 vcpu->arch.msa_enabled = true;
867 break;
James Hogan5fafd8742014-12-08 23:07:56 +0000868 default:
869 r = -EINVAL;
870 break;
871 }
872
873 return r;
874}
875
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700876long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
877 unsigned long arg)
Sanjay Lal669e8462012-11-21 18:34:02 -0800878{
879 struct kvm_vcpu *vcpu = filp->private_data;
880 void __user *argp = (void __user *)arg;
881 long r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800882
883 switch (ioctl) {
David Daney4c73fb22013-05-23 09:49:09 -0700884 case KVM_SET_ONE_REG:
885 case KVM_GET_ONE_REG: {
886 struct kvm_one_reg reg;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700887
David Daney4c73fb22013-05-23 09:49:09 -0700888 if (copy_from_user(&reg, argp, sizeof(reg)))
889 return -EFAULT;
890 if (ioctl == KVM_SET_ONE_REG)
891 return kvm_mips_set_reg(vcpu, &reg);
892 else
893 return kvm_mips_get_reg(vcpu, &reg);
894 }
895 case KVM_GET_REG_LIST: {
896 struct kvm_reg_list __user *user_list = argp;
David Daney4c73fb22013-05-23 09:49:09 -0700897 struct kvm_reg_list reg_list;
898 unsigned n;
899
900 if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
901 return -EFAULT;
902 n = reg_list.n;
James Hoganf5c43bd2016-06-15 19:29:49 +0100903 reg_list.n = kvm_mips_num_regs(vcpu);
David Daney4c73fb22013-05-23 09:49:09 -0700904 if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
905 return -EFAULT;
906 if (n < reg_list.n)
907 return -E2BIG;
James Hoganf5c43bd2016-06-15 19:29:49 +0100908 return kvm_mips_copy_reg_indices(vcpu, user_list->reg);
David Daney4c73fb22013-05-23 09:49:09 -0700909 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800910 case KVM_INTERRUPT:
911 {
912 struct kvm_mips_interrupt irq;
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700913
Sanjay Lal669e8462012-11-21 18:34:02 -0800914 if (copy_from_user(&irq, argp, sizeof(irq)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +0100915 return -EFAULT;
Sanjay Lal669e8462012-11-21 18:34:02 -0800916 kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
917 irq.irq);
918
919 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
920 break;
921 }
James Hogan5fafd8742014-12-08 23:07:56 +0000922 case KVM_ENABLE_CAP: {
923 struct kvm_enable_cap cap;
924
James Hogan5fafd8742014-12-08 23:07:56 +0000925 if (copy_from_user(&cap, argp, sizeof(cap)))
Markus Elfring5a6da5f2017-01-19 11:10:26 +0100926 return -EFAULT;
James Hogan5fafd8742014-12-08 23:07:56 +0000927 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
928 break;
929 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800930 default:
David Daney4c73fb22013-05-23 09:49:09 -0700931 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800932 }
Sanjay Lal669e8462012-11-21 18:34:02 -0800933 return r;
934}
935
James Hogane88643b2016-12-06 14:50:52 +0000936/**
937 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
938 * @kvm: kvm instance
939 * @log: slot id and address to which we copy the log
940 *
941 * Steps 1-4 below provide general overview of dirty page logging. See
942 * kvm_get_dirty_log_protect() function description for additional details.
943 *
944 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
945 * always flush the TLB (step 4) even if previous step failed and the dirty
946 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
947 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
948 * writes will be marked dirty for next log read.
949 *
950 * 1. Take a snapshot of the bit and clear it if needed.
951 * 2. Write protect the corresponding page.
952 * 3. Copy the snapshot to the userspace.
953 * 4. Flush TLB's if needed.
954 */
Sanjay Lal669e8462012-11-21 18:34:02 -0800955int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
956{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200957 struct kvm_memslots *slots;
Sanjay Lal669e8462012-11-21 18:34:02 -0800958 struct kvm_memory_slot *memslot;
James Hogane88643b2016-12-06 14:50:52 +0000959 bool is_dirty = false;
Sanjay Lal669e8462012-11-21 18:34:02 -0800960 int r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800961
962 mutex_lock(&kvm->slots_lock);
963
James Hogane88643b2016-12-06 14:50:52 +0000964 r = kvm_get_dirty_log_protect(kvm, log, &is_dirty);
Sanjay Lal669e8462012-11-21 18:34:02 -0800965
Sanjay Lal669e8462012-11-21 18:34:02 -0800966 if (is_dirty) {
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200967 slots = kvm_memslots(kvm);
968 memslot = id_to_memslot(slots, log->slot);
Sanjay Lal669e8462012-11-21 18:34:02 -0800969
James Hogane88643b2016-12-06 14:50:52 +0000970 /* Let implementation handle TLB/GVA invalidation */
971 kvm_mips_callbacks->flush_shadow_memslot(kvm, memslot);
Sanjay Lal669e8462012-11-21 18:34:02 -0800972 }
973
Sanjay Lal669e8462012-11-21 18:34:02 -0800974 mutex_unlock(&kvm->slots_lock);
975 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -0800976}
977
978long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
979{
980 long r;
981
982 switch (ioctl) {
983 default:
David Daneyed829852013-05-23 09:49:10 -0700984 r = -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -0800985 }
986
987 return r;
988}
989
990int kvm_arch_init(void *opaque)
991{
Sanjay Lal669e8462012-11-21 18:34:02 -0800992 if (kvm_mips_callbacks) {
993 kvm_err("kvm: module already exists\n");
994 return -EEXIST;
995 }
996
Deng-Cheng Zhud98403a2014-06-26 12:11:36 -0700997 return kvm_mips_emulation_init(&kvm_mips_callbacks);
Sanjay Lal669e8462012-11-21 18:34:02 -0800998}
999
1000void kvm_arch_exit(void)
1001{
1002 kvm_mips_callbacks = NULL;
1003}
1004
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001005int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1006 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001007{
David Daneyed829852013-05-23 09:49:10 -07001008 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001009}
1010
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001011int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1012 struct kvm_sregs *sregs)
Sanjay Lal669e8462012-11-21 18:34:02 -08001013{
David Daneyed829852013-05-23 09:49:10 -07001014 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001015}
1016
Dominik Dingel31928aa2014-12-04 15:47:07 +01001017void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Sanjay Lal669e8462012-11-21 18:34:02 -08001018{
Sanjay Lal669e8462012-11-21 18:34:02 -08001019}
1020
1021int kvm_arch_vcpu_ioctl_get_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_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1027{
David Daneyed829852013-05-23 09:49:10 -07001028 return -ENOIOCTLCMD;
Sanjay Lal669e8462012-11-21 18:34:02 -08001029}
1030
1031int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1032{
1033 return VM_FAULT_SIGBUS;
1034}
1035
Alexander Graf784aa3d2014-07-14 18:27:35 +02001036int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Sanjay Lal669e8462012-11-21 18:34:02 -08001037{
1038 int r;
1039
1040 switch (ext) {
David Daney4c73fb22013-05-23 09:49:09 -07001041 case KVM_CAP_ONE_REG:
James Hogan5fafd8742014-12-08 23:07:56 +00001042 case KVM_CAP_ENABLE_CAP:
James Hogan230c5722015-05-08 17:11:49 +01001043 case KVM_CAP_READONLY_MEM:
James Hogan411740f2016-12-13 16:32:39 +00001044 case KVM_CAP_SYNC_MMU:
Paolo Bonzini460df4c2017-02-08 11:50:15 +01001045 case KVM_CAP_IMMEDIATE_EXIT:
David Daney4c73fb22013-05-23 09:49:09 -07001046 r = 1;
1047 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001048 case KVM_CAP_COALESCED_MMIO:
1049 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1050 break;
James Hogan12ed1fa2016-12-13 22:39:39 +00001051 case KVM_CAP_NR_VCPUS:
1052 r = num_online_cpus();
1053 break;
1054 case KVM_CAP_MAX_VCPUS:
1055 r = KVM_MAX_VCPUS;
1056 break;
James Hogan5fafd8742014-12-08 23:07:56 +00001057 case KVM_CAP_MIPS_FPU:
James Hogan556f2a52016-04-22 10:38:48 +01001058 /* We don't handle systems with inconsistent cpu_has_fpu */
1059 r = !!raw_cpu_has_fpu;
James Hogan5fafd8742014-12-08 23:07:56 +00001060 break;
James Hogand952bd02014-12-08 23:07:56 +00001061 case KVM_CAP_MIPS_MSA:
1062 /*
1063 * We don't support MSA vector partitioning yet:
1064 * 1) It would require explicit support which can't be tested
1065 * yet due to lack of support in current hardware.
1066 * 2) It extends the state that would need to be saved/restored
1067 * by e.g. QEMU for migration.
1068 *
1069 * When vector partitioning hardware becomes available, support
1070 * could be added by requiring a flag when enabling
1071 * KVM_CAP_MIPS_MSA capability to indicate that userland knows
1072 * to save/restore the appropriate extra state.
1073 */
1074 r = cpu_has_msa && !(boot_cpu_data.msa_id & MSA_IR_WRPF);
1075 break;
Sanjay Lal669e8462012-11-21 18:34:02 -08001076 default:
James Hogan607ef2f2017-03-14 10:15:22 +00001077 r = kvm_mips_callbacks->check_extension(kvm, ext);
Sanjay Lal669e8462012-11-21 18:34:02 -08001078 break;
1079 }
1080 return r;
Sanjay Lal669e8462012-11-21 18:34:02 -08001081}
1082
1083int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1084{
1085 return kvm_mips_pending_timer(vcpu);
1086}
1087
1088int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu)
1089{
1090 int i;
1091 struct mips_coproc *cop0;
1092
1093 if (!vcpu)
1094 return -1;
1095
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001096 kvm_debug("VCPU Register Dump:\n");
1097 kvm_debug("\tpc = 0x%08lx\n", vcpu->arch.pc);
1098 kvm_debug("\texceptions: %08lx\n", vcpu->arch.pending_exceptions);
Sanjay Lal669e8462012-11-21 18:34:02 -08001099
1100 for (i = 0; i < 32; i += 4) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001101 kvm_debug("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i,
Sanjay Lal669e8462012-11-21 18:34:02 -08001102 vcpu->arch.gprs[i],
1103 vcpu->arch.gprs[i + 1],
1104 vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]);
1105 }
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001106 kvm_debug("\thi: 0x%08lx\n", vcpu->arch.hi);
1107 kvm_debug("\tlo: 0x%08lx\n", vcpu->arch.lo);
Sanjay Lal669e8462012-11-21 18:34:02 -08001108
1109 cop0 = vcpu->arch.cop0;
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001110 kvm_debug("\tStatus: 0x%08lx, Cause: 0x%08lx\n",
1111 kvm_read_c0_guest_status(cop0),
1112 kvm_read_c0_guest_cause(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001113
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -07001114 kvm_debug("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001115
1116 return 0;
1117}
1118
1119int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1120{
1121 int i;
1122
David Daney8d17dd02013-05-23 09:49:08 -07001123 for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001124 vcpu->arch.gprs[i] = regs->gpr[i];
David Daney8d17dd02013-05-23 09:49:08 -07001125 vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */
Sanjay Lal669e8462012-11-21 18:34:02 -08001126 vcpu->arch.hi = regs->hi;
1127 vcpu->arch.lo = regs->lo;
1128 vcpu->arch.pc = regs->pc;
1129
David Daney4c73fb22013-05-23 09:49:09 -07001130 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001131}
1132
1133int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1134{
1135 int i;
1136
David Daney8d17dd02013-05-23 09:49:08 -07001137 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++)
David Daneybf32ebf2013-05-23 09:49:07 -07001138 regs->gpr[i] = vcpu->arch.gprs[i];
Sanjay Lal669e8462012-11-21 18:34:02 -08001139
1140 regs->hi = vcpu->arch.hi;
1141 regs->lo = vcpu->arch.lo;
1142 regs->pc = vcpu->arch.pc;
1143
David Daney4c73fb22013-05-23 09:49:09 -07001144 return 0;
Sanjay Lal669e8462012-11-21 18:34:02 -08001145}
1146
James Hogan0fae34f2014-05-29 10:16:39 +01001147static void kvm_mips_comparecount_func(unsigned long data)
Sanjay Lal669e8462012-11-21 18:34:02 -08001148{
1149 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1150
1151 kvm_mips_callbacks->queue_timer_int(vcpu);
1152
1153 vcpu->arch.wait = 0;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001154 if (swait_active(&vcpu->wq))
1155 swake_up(&vcpu->wq);
Sanjay Lal669e8462012-11-21 18:34:02 -08001156}
1157
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001158/* low level hrtimer wake routine */
James Hogan0fae34f2014-05-29 10:16:39 +01001159static enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer)
Sanjay Lal669e8462012-11-21 18:34:02 -08001160{
1161 struct kvm_vcpu *vcpu;
1162
1163 vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer);
1164 kvm_mips_comparecount_func((unsigned long) vcpu);
James Hogane30492b2014-05-29 10:16:35 +01001165 return kvm_mips_count_timeout(vcpu);
Sanjay Lal669e8462012-11-21 18:34:02 -08001166}
1167
1168int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1169{
James Hoganf7f14272016-09-08 22:57:03 +01001170 int err;
1171
1172 err = kvm_mips_callbacks->vcpu_init(vcpu);
1173 if (err)
1174 return err;
1175
Sanjay Lal669e8462012-11-21 18:34:02 -08001176 hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC,
1177 HRTIMER_MODE_REL);
1178 vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup;
Sanjay Lal669e8462012-11-21 18:34:02 -08001179 return 0;
1180}
1181
James Hogan630766b2016-09-08 23:00:24 +01001182void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1183{
1184 kvm_mips_callbacks->vcpu_uninit(vcpu);
1185}
1186
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001187int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1188 struct kvm_translation *tr)
Sanjay Lal669e8462012-11-21 18:34:02 -08001189{
1190 return 0;
1191}
1192
1193/* Initial guest state */
1194int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1195{
1196 return kvm_mips_callbacks->vcpu_setup(vcpu);
1197}
1198
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001199static void kvm_mips_set_c0_status(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001200{
James Hogan8cffd192016-06-09 14:19:08 +01001201 u32 status = read_c0_status();
Sanjay Lal669e8462012-11-21 18:34:02 -08001202
Sanjay Lal669e8462012-11-21 18:34:02 -08001203 if (cpu_has_dsp)
1204 status |= (ST0_MX);
1205
1206 write_c0_status(status);
1207 ehb();
1208}
1209
1210/*
1211 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1212 */
1213int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
1214{
James Hogan8cffd192016-06-09 14:19:08 +01001215 u32 cause = vcpu->arch.host_cp0_cause;
1216 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
1217 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lal669e8462012-11-21 18:34:02 -08001218 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
1219 enum emulation_result er = EMULATE_DONE;
James Hogan122e51d2016-11-28 17:23:14 +00001220 u32 inst;
Sanjay Lal669e8462012-11-21 18:34:02 -08001221 int ret = RESUME_GUEST;
1222
James Hogan4841e0d2016-11-28 22:45:04 +00001223 vcpu->mode = OUTSIDE_GUEST_MODE;
1224
James Hoganc4c6f2c2015-02-04 10:52:03 +00001225 /* re-enable HTW before enabling interrupts */
1226 htw_start();
1227
Sanjay Lal669e8462012-11-21 18:34:02 -08001228 /* Set a default exit reason */
1229 run->exit_reason = KVM_EXIT_UNKNOWN;
1230 run->ready_for_interrupt_injection = 1;
1231
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001232 /*
1233 * Set the appropriate status bits based on host CPU features,
1234 * before we hit the scheduler
1235 */
Sanjay Lal669e8462012-11-21 18:34:02 -08001236 kvm_mips_set_c0_status();
1237
1238 local_irq_enable();
1239
1240 kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n",
1241 cause, opc, run, vcpu);
James Hogan1e09e862016-06-14 09:40:12 +01001242 trace_kvm_exit(vcpu, exccode);
Sanjay Lal669e8462012-11-21 18:34:02 -08001243
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001244 /*
1245 * Do a privilege check, if in UM most of these exit conditions end up
Sanjay Lal669e8462012-11-21 18:34:02 -08001246 * causing an exception to be delivered to the Guest Kernel
1247 */
1248 er = kvm_mips_check_privilege(cause, opc, run, vcpu);
1249 if (er == EMULATE_PRIV_FAIL) {
1250 goto skip_emul;
1251 } else if (er == EMULATE_FAIL) {
1252 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1253 ret = RESUME_HOST;
1254 goto skip_emul;
1255 }
1256
1257 switch (exccode) {
James Hogan16d100db2015-12-16 23:49:33 +00001258 case EXCCODE_INT:
1259 kvm_debug("[%d]EXCCODE_INT @ %p\n", vcpu->vcpu_id, opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001260
1261 ++vcpu->stat.int_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001262
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001263 if (need_resched())
Sanjay Lal669e8462012-11-21 18:34:02 -08001264 cond_resched();
Sanjay Lal669e8462012-11-21 18:34:02 -08001265
1266 ret = RESUME_GUEST;
1267 break;
1268
James Hogan16d100db2015-12-16 23:49:33 +00001269 case EXCCODE_CPU:
1270 kvm_debug("EXCCODE_CPU: @ PC: %p\n", opc);
Sanjay Lal669e8462012-11-21 18:34:02 -08001271
1272 ++vcpu->stat.cop_unusable_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001273 ret = kvm_mips_callbacks->handle_cop_unusable(vcpu);
1274 /* XXXKYMA: Might need to return to user space */
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001275 if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN)
Sanjay Lal669e8462012-11-21 18:34:02 -08001276 ret = RESUME_HOST;
Sanjay Lal669e8462012-11-21 18:34:02 -08001277 break;
1278
James Hogan16d100db2015-12-16 23:49:33 +00001279 case EXCCODE_MOD:
Sanjay Lal669e8462012-11-21 18:34:02 -08001280 ++vcpu->stat.tlbmod_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001281 ret = kvm_mips_callbacks->handle_tlb_mod(vcpu);
1282 break;
1283
James Hogan16d100db2015-12-16 23:49:33 +00001284 case EXCCODE_TLBS:
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001285 kvm_debug("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n",
1286 cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc,
1287 badvaddr);
Sanjay Lal669e8462012-11-21 18:34:02 -08001288
1289 ++vcpu->stat.tlbmiss_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001290 ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu);
1291 break;
1292
James Hogan16d100db2015-12-16 23:49:33 +00001293 case EXCCODE_TLBL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001294 kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
1295 cause, opc, badvaddr);
1296
1297 ++vcpu->stat.tlbmiss_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001298 ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu);
1299 break;
1300
James Hogan16d100db2015-12-16 23:49:33 +00001301 case EXCCODE_ADES:
Sanjay Lal669e8462012-11-21 18:34:02 -08001302 ++vcpu->stat.addrerr_st_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001303 ret = kvm_mips_callbacks->handle_addr_err_st(vcpu);
1304 break;
1305
James Hogan16d100db2015-12-16 23:49:33 +00001306 case EXCCODE_ADEL:
Sanjay Lal669e8462012-11-21 18:34:02 -08001307 ++vcpu->stat.addrerr_ld_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001308 ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu);
1309 break;
1310
James Hogan16d100db2015-12-16 23:49:33 +00001311 case EXCCODE_SYS:
Sanjay Lal669e8462012-11-21 18:34:02 -08001312 ++vcpu->stat.syscall_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001313 ret = kvm_mips_callbacks->handle_syscall(vcpu);
1314 break;
1315
James Hogan16d100db2015-12-16 23:49:33 +00001316 case EXCCODE_RI:
Sanjay Lal669e8462012-11-21 18:34:02 -08001317 ++vcpu->stat.resvd_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001318 ret = kvm_mips_callbacks->handle_res_inst(vcpu);
1319 break;
1320
James Hogan16d100db2015-12-16 23:49:33 +00001321 case EXCCODE_BP:
Sanjay Lal669e8462012-11-21 18:34:02 -08001322 ++vcpu->stat.break_inst_exits;
Sanjay Lal669e8462012-11-21 18:34:02 -08001323 ret = kvm_mips_callbacks->handle_break(vcpu);
1324 break;
1325
James Hogan16d100db2015-12-16 23:49:33 +00001326 case EXCCODE_TR:
James Hogan0a560422015-02-06 16:03:57 +00001327 ++vcpu->stat.trap_inst_exits;
James Hogan0a560422015-02-06 16:03:57 +00001328 ret = kvm_mips_callbacks->handle_trap(vcpu);
1329 break;
1330
James Hogan16d100db2015-12-16 23:49:33 +00001331 case EXCCODE_MSAFPE:
James Hoganc2537ed2015-02-06 10:56:27 +00001332 ++vcpu->stat.msa_fpe_exits;
James Hoganc2537ed2015-02-06 10:56:27 +00001333 ret = kvm_mips_callbacks->handle_msa_fpe(vcpu);
1334 break;
1335
James Hogan16d100db2015-12-16 23:49:33 +00001336 case EXCCODE_FPE:
James Hogan1c0cd662015-02-06 10:56:27 +00001337 ++vcpu->stat.fpe_exits;
James Hogan1c0cd662015-02-06 10:56:27 +00001338 ret = kvm_mips_callbacks->handle_fpe(vcpu);
1339 break;
1340
James Hogan16d100db2015-12-16 23:49:33 +00001341 case EXCCODE_MSADIS:
James Hoganc2537ed2015-02-06 10:56:27 +00001342 ++vcpu->stat.msa_disabled_exits;
James Hogan98119ad2015-02-06 11:11:56 +00001343 ret = kvm_mips_callbacks->handle_msa_disabled(vcpu);
1344 break;
1345
Sanjay Lal669e8462012-11-21 18:34:02 -08001346 default:
James Hogan122e51d2016-11-28 17:23:14 +00001347 if (cause & CAUSEF_BD)
1348 opc += 1;
1349 inst = 0;
James Hogan6a97c772015-04-23 16:54:35 +01001350 kvm_get_badinstr(opc, vcpu, &inst);
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001351 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 +00001352 exccode, opc, inst, badvaddr,
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001353 kvm_read_c0_guest_status(vcpu->arch.cop0));
Sanjay Lal669e8462012-11-21 18:34:02 -08001354 kvm_arch_vcpu_dump_regs(vcpu);
1355 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1356 ret = RESUME_HOST;
1357 break;
1358
1359 }
1360
1361skip_emul:
1362 local_irq_disable();
1363
1364 if (er == EMULATE_DONE && !(ret & RESUME_HOST))
1365 kvm_mips_deliver_interrupts(vcpu, cause);
1366
1367 if (!(ret & RESUME_HOST)) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07001368 /* Only check for signals if not already exiting to userspace */
Sanjay Lal669e8462012-11-21 18:34:02 -08001369 if (signal_pending(current)) {
1370 run->exit_reason = KVM_EXIT_INTR;
1371 ret = (-EINTR << 2) | RESUME_HOST;
1372 ++vcpu->stat.signal_exits;
James Hogan1e09e862016-06-14 09:40:12 +01001373 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_SIGNAL);
Sanjay Lal669e8462012-11-21 18:34:02 -08001374 }
1375 }
1376
James Hogan98e91b82014-11-18 14:09:12 +00001377 if (ret == RESUME_GUEST) {
James Hogan93258602016-06-14 09:40:14 +01001378 trace_kvm_reenter(vcpu);
1379
James Hogan4841e0d2016-11-28 22:45:04 +00001380 /*
1381 * Make sure the read of VCPU requests in vcpu_reenter()
1382 * callback is not reordered ahead of the write to vcpu->mode,
1383 * or we could miss a TLB flush request while the requester sees
1384 * the VCPU as outside of guest mode and not needing an IPI.
1385 */
1386 smp_store_mb(vcpu->mode, IN_GUEST_MODE);
1387
James Hogana2c046e2016-11-18 13:14:37 +00001388 kvm_mips_callbacks->vcpu_reenter(run, vcpu);
James Hogan25b08c72016-09-16 00:06:43 +01001389
James Hogan98e91b82014-11-18 14:09:12 +00001390 /*
James Hogan539cb89fb2015-03-05 11:43:36 +00001391 * If FPU / MSA are enabled (i.e. the guest's FPU / MSA context
1392 * is live), restore FCR31 / MSACSR.
James Hogan98e91b82014-11-18 14:09:12 +00001393 *
1394 * This should be before returning to the guest exception
James Hogan539cb89fb2015-03-05 11:43:36 +00001395 * vector, as it may well cause an [MSA] FP exception if there
1396 * are pending exception bits unmasked. (see
James Hogan98e91b82014-11-18 14:09:12 +00001397 * kvm_mips_csr_die_notifier() for how that is handled).
1398 */
1399 if (kvm_mips_guest_has_fpu(&vcpu->arch) &&
1400 read_c0_status() & ST0_CU1)
1401 __kvm_restore_fcsr(&vcpu->arch);
James Hogan539cb89fb2015-03-05 11:43:36 +00001402
1403 if (kvm_mips_guest_has_msa(&vcpu->arch) &&
1404 read_c0_config5() & MIPS_CONF5_MSAEN)
1405 __kvm_restore_msacsr(&vcpu->arch);
James Hogan98e91b82014-11-18 14:09:12 +00001406 }
1407
James Hoganc4c6f2c2015-02-04 10:52:03 +00001408 /* Disable HTW before returning to guest or host */
1409 htw_stop();
1410
Sanjay Lal669e8462012-11-21 18:34:02 -08001411 return ret;
1412}
1413
James Hogan98e91b82014-11-18 14:09:12 +00001414/* Enable FPU for guest and restore context */
1415void kvm_own_fpu(struct kvm_vcpu *vcpu)
1416{
1417 struct mips_coproc *cop0 = vcpu->arch.cop0;
1418 unsigned int sr, cfg5;
1419
1420 preempt_disable();
1421
James Hogan539cb89fb2015-03-05 11:43:36 +00001422 sr = kvm_read_c0_guest_status(cop0);
1423
1424 /*
1425 * If MSA state is already live, it is undefined how it interacts with
1426 * FR=0 FPU state, and we don't want to hit reserved instruction
1427 * exceptions trying to save the MSA state later when CU=1 && FR=1, so
1428 * play it safe and save it first.
1429 *
1430 * In theory we shouldn't ever hit this case since kvm_lose_fpu() should
1431 * get called when guest CU1 is set, however we can't trust the guest
1432 * not to clobber the status register directly via the commpage.
1433 */
1434 if (cpu_has_msa && sr & ST0_CU1 && !(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001435 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
James Hogan539cb89fb2015-03-05 11:43:36 +00001436 kvm_lose_fpu(vcpu);
1437
James Hogan98e91b82014-11-18 14:09:12 +00001438 /*
1439 * Enable FPU for guest
1440 * We set FR and FRE according to guest context
1441 */
James Hogan98e91b82014-11-18 14:09:12 +00001442 change_c0_status(ST0_CU1 | ST0_FR, sr);
1443 if (cpu_has_fre) {
1444 cfg5 = kvm_read_c0_guest_config5(cop0);
1445 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1446 }
1447 enable_fpu_hazard();
1448
1449 /* If guest FPU state not active, restore it now */
James Hoganf9431762016-06-14 09:40:10 +01001450 if (!(vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
James Hogan98e91b82014-11-18 14:09:12 +00001451 __kvm_restore_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001452 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001453 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_FPU);
1454 } else {
1455 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001456 }
1457
1458 preempt_enable();
1459}
1460
James Hogan539cb89fb2015-03-05 11:43:36 +00001461#ifdef CONFIG_CPU_HAS_MSA
1462/* Enable MSA for guest and restore context */
1463void kvm_own_msa(struct kvm_vcpu *vcpu)
1464{
1465 struct mips_coproc *cop0 = vcpu->arch.cop0;
1466 unsigned int sr, cfg5;
1467
1468 preempt_disable();
1469
1470 /*
1471 * Enable FPU if enabled in guest, since we're restoring FPU context
1472 * anyway. We set FR and FRE according to guest context.
1473 */
1474 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1475 sr = kvm_read_c0_guest_status(cop0);
1476
1477 /*
1478 * If FR=0 FPU state is already live, it is undefined how it
1479 * interacts with MSA state, so play it safe and save it first.
1480 */
1481 if (!(sr & ST0_FR) &&
James Hoganf9431762016-06-14 09:40:10 +01001482 (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU |
1483 KVM_MIPS_AUX_MSA)) == KVM_MIPS_AUX_FPU)
James Hogan539cb89fb2015-03-05 11:43:36 +00001484 kvm_lose_fpu(vcpu);
1485
1486 change_c0_status(ST0_CU1 | ST0_FR, sr);
1487 if (sr & ST0_CU1 && cpu_has_fre) {
1488 cfg5 = kvm_read_c0_guest_config5(cop0);
1489 change_c0_config5(MIPS_CONF5_FRE, cfg5);
1490 }
1491 }
1492
1493 /* Enable MSA for guest */
1494 set_c0_config5(MIPS_CONF5_MSAEN);
1495 enable_fpu_hazard();
1496
James Hoganf9431762016-06-14 09:40:10 +01001497 switch (vcpu->arch.aux_inuse & (KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA)) {
1498 case KVM_MIPS_AUX_FPU:
James Hogan539cb89fb2015-03-05 11:43:36 +00001499 /*
1500 * Guest FPU state already loaded, only restore upper MSA state
1501 */
1502 __kvm_restore_msa_upper(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001503 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan04ebebf2016-06-14 09:40:11 +01001504 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001505 break;
1506 case 0:
1507 /* Neither FPU or MSA already active, restore full MSA state */
1508 __kvm_restore_msa(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001509 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001510 if (kvm_mips_guest_has_fpu(&vcpu->arch))
James Hoganf9431762016-06-14 09:40:10 +01001511 vcpu->arch.aux_inuse |= KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001512 trace_kvm_aux(vcpu, KVM_TRACE_AUX_RESTORE,
1513 KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001514 break;
1515 default:
James Hogan04ebebf2016-06-14 09:40:11 +01001516 trace_kvm_aux(vcpu, KVM_TRACE_AUX_ENABLE, KVM_TRACE_AUX_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001517 break;
1518 }
1519
1520 preempt_enable();
1521}
1522#endif
1523
1524/* Drop FPU & MSA without saving it */
James Hogan98e91b82014-11-18 14:09:12 +00001525void kvm_drop_fpu(struct kvm_vcpu *vcpu)
1526{
1527 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001528 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001529 disable_msa();
James Hogan04ebebf2016-06-14 09:40:11 +01001530 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_MSA);
James Hoganf9431762016-06-14 09:40:10 +01001531 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_MSA;
James Hogan539cb89fb2015-03-05 11:43:36 +00001532 }
James Hoganf9431762016-06-14 09:40:10 +01001533 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan98e91b82014-11-18 14:09:12 +00001534 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan04ebebf2016-06-14 09:40:11 +01001535 trace_kvm_aux(vcpu, KVM_TRACE_AUX_DISCARD, KVM_TRACE_AUX_FPU);
James Hoganf9431762016-06-14 09:40:10 +01001536 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan98e91b82014-11-18 14:09:12 +00001537 }
1538 preempt_enable();
1539}
1540
James Hogan539cb89fb2015-03-05 11:43:36 +00001541/* Save and disable FPU & MSA */
James Hogan98e91b82014-11-18 14:09:12 +00001542void kvm_lose_fpu(struct kvm_vcpu *vcpu)
1543{
1544 /*
James Hoganc58cf742017-03-14 10:15:17 +00001545 * With T&E, FPU & MSA get disabled in root context (hardware) when it
1546 * is disabled in guest context (software), but the register state in
1547 * the hardware may still be in use.
1548 * This is why we explicitly re-enable the hardware before saving.
James Hogan98e91b82014-11-18 14:09:12 +00001549 */
1550
1551 preempt_disable();
James Hoganf9431762016-06-14 09:40:10 +01001552 if (cpu_has_msa && vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
James Hoganc58cf742017-03-14 10:15:17 +00001553 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) {
1554 set_c0_config5(MIPS_CONF5_MSAEN);
1555 enable_fpu_hazard();
1556 }
James Hogan539cb89fb2015-03-05 11:43:36 +00001557
1558 __kvm_save_msa(&vcpu->arch);
James Hogan04ebebf2016-06-14 09:40:11 +01001559 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU_MSA);
James Hogan539cb89fb2015-03-05 11:43:36 +00001560
1561 /* Disable MSA & FPU */
1562 disable_msa();
James Hoganf9431762016-06-14 09:40:10 +01001563 if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hogan539cb89fb2015-03-05 11:43:36 +00001564 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001565 disable_fpu_hazard();
1566 }
James Hoganf9431762016-06-14 09:40:10 +01001567 vcpu->arch.aux_inuse &= ~(KVM_MIPS_AUX_FPU | KVM_MIPS_AUX_MSA);
1568 } else if (vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU) {
James Hoganc58cf742017-03-14 10:15:17 +00001569 if (!IS_ENABLED(CONFIG_KVM_MIPS_VZ)) {
1570 set_c0_status(ST0_CU1);
1571 enable_fpu_hazard();
1572 }
James Hogan98e91b82014-11-18 14:09:12 +00001573
1574 __kvm_save_fpu(&vcpu->arch);
James Hoganf9431762016-06-14 09:40:10 +01001575 vcpu->arch.aux_inuse &= ~KVM_MIPS_AUX_FPU;
James Hogan04ebebf2016-06-14 09:40:11 +01001576 trace_kvm_aux(vcpu, KVM_TRACE_AUX_SAVE, KVM_TRACE_AUX_FPU);
James Hogan98e91b82014-11-18 14:09:12 +00001577
1578 /* Disable FPU */
1579 clear_c0_status(ST0_CU1 | ST0_FR);
James Hogan4ac33422016-04-22 10:38:49 +01001580 disable_fpu_hazard();
James Hogan98e91b82014-11-18 14:09:12 +00001581 }
1582 preempt_enable();
1583}
1584
1585/*
James Hogan539cb89fb2015-03-05 11:43:36 +00001586 * Step over a specific ctc1 to FCSR and a specific ctcmsa to MSACSR which are
1587 * used to restore guest FCSR/MSACSR state and may trigger a "harmless" FP/MSAFP
1588 * exception if cause bits are set in the value being written.
James Hogan98e91b82014-11-18 14:09:12 +00001589 */
1590static int kvm_mips_csr_die_notify(struct notifier_block *self,
1591 unsigned long cmd, void *ptr)
1592{
1593 struct die_args *args = (struct die_args *)ptr;
1594 struct pt_regs *regs = args->regs;
1595 unsigned long pc;
1596
James Hogan539cb89fb2015-03-05 11:43:36 +00001597 /* Only interested in FPE and MSAFPE */
1598 if (cmd != DIE_FP && cmd != DIE_MSAFP)
James Hogan98e91b82014-11-18 14:09:12 +00001599 return NOTIFY_DONE;
1600
1601 /* Return immediately if guest context isn't active */
1602 if (!(current->flags & PF_VCPU))
1603 return NOTIFY_DONE;
1604
1605 /* Should never get here from user mode */
1606 BUG_ON(user_mode(regs));
1607
1608 pc = instruction_pointer(regs);
1609 switch (cmd) {
1610 case DIE_FP:
1611 /* match 2nd instruction in __kvm_restore_fcsr */
1612 if (pc != (unsigned long)&__kvm_restore_fcsr + 4)
1613 return NOTIFY_DONE;
1614 break;
James Hogan539cb89fb2015-03-05 11:43:36 +00001615 case DIE_MSAFP:
1616 /* match 2nd/3rd instruction in __kvm_restore_msacsr */
1617 if (!cpu_has_msa ||
1618 pc < (unsigned long)&__kvm_restore_msacsr + 4 ||
1619 pc > (unsigned long)&__kvm_restore_msacsr + 8)
1620 return NOTIFY_DONE;
1621 break;
James Hogan98e91b82014-11-18 14:09:12 +00001622 }
1623
1624 /* Move PC forward a little and continue executing */
1625 instruction_pointer(regs) += 4;
1626
1627 return NOTIFY_STOP;
1628}
1629
1630static struct notifier_block kvm_mips_csr_die_notifier = {
1631 .notifier_call = kvm_mips_csr_die_notify,
1632};
1633
James Hogan2db9d232015-12-16 23:49:32 +00001634static int __init kvm_mips_init(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001635{
1636 int ret;
1637
James Hogan1e5217f52016-06-23 17:34:45 +01001638 ret = kvm_mips_entry_setup();
1639 if (ret)
1640 return ret;
1641
Sanjay Lal669e8462012-11-21 18:34:02 -08001642 ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1643
1644 if (ret)
1645 return ret;
1646
James Hogan98e91b82014-11-18 14:09:12 +00001647 register_die_notifier(&kvm_mips_csr_die_notifier);
1648
Sanjay Lal669e8462012-11-21 18:34:02 -08001649 return 0;
1650}
1651
James Hogan2db9d232015-12-16 23:49:32 +00001652static void __exit kvm_mips_exit(void)
Sanjay Lal669e8462012-11-21 18:34:02 -08001653{
1654 kvm_exit();
1655
James Hogan98e91b82014-11-18 14:09:12 +00001656 unregister_die_notifier(&kvm_mips_csr_die_notifier);
Sanjay Lal669e8462012-11-21 18:34:02 -08001657}
1658
1659module_init(kvm_mips_init);
1660module_exit(kvm_mips_exit);
1661
1662EXPORT_TRACEPOINT_SYMBOL(kvm_exit);