blob: 494a90221b5e4b190965f675f4b3d7fa94beede7 [file] [log] [blame]
Sanjay Lalf5c236d2012-11-21 18:34:09 -08001/*
Deng-Cheng Zhud116e812014-06-26 12:11:34 -07002 * 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: Deliver/Emulate exceptions to the guest kernel
7 *
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
10 */
Sanjay Lalf5c236d2012-11-21 18:34:09 -080011
12#include <linux/errno.h>
13#include <linux/err.h>
Sanjay Lalf5c236d2012-11-21 18:34:09 -080014#include <linux/kvm_host.h>
James Hogan1581ff32016-11-16 23:48:56 +000015#include <linux/vmalloc.h>
16#include <asm/mmu_context.h>
Sanjay Lalf5c236d2012-11-21 18:34:09 -080017
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070018#include "interrupt.h"
Sanjay Lalf5c236d2012-11-21 18:34:09 -080019
20static gpa_t kvm_trap_emul_gva_to_gpa_cb(gva_t gva)
21{
22 gpa_t gpa;
James Hogan8cffd192016-06-09 14:19:08 +010023 gva_t kseg = KSEGX(gva);
Sanjay Lalf5c236d2012-11-21 18:34:09 -080024
25 if ((kseg == CKSEG0) || (kseg == CKSEG1))
26 gpa = CPHYSADDR(gva);
27 else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -070028 kvm_err("%s: cannot find GPA for GVA: %#lx\n", __func__, gva);
Sanjay Lalf5c236d2012-11-21 18:34:09 -080029 kvm_mips_dump_host_tlbs();
30 gpa = KVM_INVALID_ADDR;
31 }
32
Sanjay Lalf5c236d2012-11-21 18:34:09 -080033 kvm_debug("%s: gva %#lx, gpa: %#llx\n", __func__, gva, gpa);
Sanjay Lalf5c236d2012-11-21 18:34:09 -080034
35 return gpa;
36}
37
Sanjay Lalf5c236d2012-11-21 18:34:09 -080038static int kvm_trap_emul_handle_cop_unusable(struct kvm_vcpu *vcpu)
39{
James Hogan1c0cd662015-02-06 10:56:27 +000040 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lalf5c236d2012-11-21 18:34:09 -080041 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +010042 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +010043 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -080044 enum emulation_result er = EMULATE_DONE;
45 int ret = RESUME_GUEST;
46
James Hogan1c0cd662015-02-06 10:56:27 +000047 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) {
48 /* FPU Unusable */
49 if (!kvm_mips_guest_has_fpu(&vcpu->arch) ||
50 (kvm_read_c0_guest_status(cop0) & ST0_CU1) == 0) {
51 /*
52 * Unusable/no FPU in guest:
53 * deliver guest COP1 Unusable Exception
54 */
55 er = kvm_mips_emulate_fpu_exc(cause, opc, run, vcpu);
56 } else {
57 /* Restore FPU state */
58 kvm_own_fpu(vcpu);
59 er = EMULATE_DONE;
60 }
61 } else {
Sanjay Lalf5c236d2012-11-21 18:34:09 -080062 er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
James Hogan1c0cd662015-02-06 10:56:27 +000063 }
Sanjay Lalf5c236d2012-11-21 18:34:09 -080064
65 switch (er) {
66 case EMULATE_DONE:
67 ret = RESUME_GUEST;
68 break;
69
70 case EMULATE_FAIL:
71 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
72 ret = RESUME_HOST;
73 break;
74
75 case EMULATE_WAIT:
76 run->exit_reason = KVM_EXIT_INTR;
77 ret = RESUME_HOST;
78 break;
79
80 default:
81 BUG();
82 }
83 return ret;
84}
85
86static int kvm_trap_emul_handle_tlb_mod(struct kvm_vcpu *vcpu)
87{
88 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +010089 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lalf5c236d2012-11-21 18:34:09 -080090 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
James Hogan31cf7492016-06-09 14:19:09 +010091 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -080092 enum emulation_result er = EMULATE_DONE;
93 int ret = RESUME_GUEST;
94
95 if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
96 || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
James Hogan31cf7492016-06-09 14:19:09 +010097 kvm_debug("USER/KSEG23 ADDR TLB MOD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
Deng-Cheng Zhud116e812014-06-26 12:11:34 -070098 cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -080099 er = kvm_mips_handle_tlbmod(cause, opc, run, vcpu);
100
101 if (er == EMULATE_DONE)
102 ret = RESUME_GUEST;
103 else {
104 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
105 ret = RESUME_HOST;
106 }
107 } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700108 /*
109 * XXXKYMA: The guest kernel does not expect to get this fault
110 * when we are not using HIGHMEM. Need to address this in a
111 * HIGHMEM kernel
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800112 */
James Hogan31cf7492016-06-09 14:19:09 +0100113 kvm_err("TLB MOD fault not handled, cause %#x, PC: %p, BadVaddr: %#lx\n",
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700114 cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800115 kvm_mips_dump_host_tlbs();
116 kvm_arch_vcpu_dump_regs(vcpu);
117 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
118 ret = RESUME_HOST;
119 } else {
James Hogan31cf7492016-06-09 14:19:09 +0100120 kvm_err("Illegal TLB Mod fault address , cause %#x, PC: %p, BadVaddr: %#lx\n",
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700121 cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800122 kvm_mips_dump_host_tlbs();
123 kvm_arch_vcpu_dump_regs(vcpu);
124 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
125 ret = RESUME_HOST;
126 }
127 return ret;
128}
129
James Hogan3b08aec2016-06-09 14:19:20 +0100130static int kvm_trap_emul_handle_tlb_miss(struct kvm_vcpu *vcpu, bool store)
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800131{
132 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100133 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800134 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
James Hogan31cf7492016-06-09 14:19:09 +0100135 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800136 enum emulation_result er = EMULATE_DONE;
137 int ret = RESUME_GUEST;
138
139 if (((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR)
140 && KVM_GUEST_KERNEL_MODE(vcpu)) {
141 if (kvm_mips_handle_commpage_tlb_fault(badvaddr, vcpu) < 0) {
142 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
143 ret = RESUME_HOST;
144 }
145 } else if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
146 || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
James Hogan3b08aec2016-06-09 14:19:20 +0100147 kvm_debug("USER ADDR TLB %s fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
148 store ? "ST" : "LD", cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800149
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700150 /*
151 * User Address (UA) fault, this could happen if
152 * (1) TLB entry not present/valid in both Guest and shadow host
153 * TLBs, in this case we pass on the fault to the guest
154 * kernel and let it handle it.
155 * (2) TLB entry is present in the Guest TLB but not in the
156 * shadow, in this case we inject the TLB from the Guest TLB
157 * into the shadow host TLB
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800158 */
159
160 er = kvm_mips_handle_tlbmiss(cause, opc, run, vcpu);
161 if (er == EMULATE_DONE)
162 ret = RESUME_GUEST;
163 else {
164 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
165 ret = RESUME_HOST;
166 }
167 } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
James Hogan3b08aec2016-06-09 14:19:20 +0100168 /*
169 * All KSEG0 faults are handled by KVM, as the guest kernel does
170 * not expect to ever get them
171 */
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800172 if (kvm_mips_handle_kseg0_tlb_fault
173 (vcpu->arch.host_cp0_badvaddr, vcpu) < 0) {
174 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
175 ret = RESUME_HOST;
176 }
James Hogand5888472016-08-19 15:09:47 +0100177 } else if (KVM_GUEST_KERNEL_MODE(vcpu)
178 && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
179 /*
180 * With EVA we may get a TLB exception instead of an address
181 * error when the guest performs MMIO to KSeg1 addresses.
182 */
183 kvm_debug("Emulate %s MMIO space\n",
184 store ? "Store to" : "Load from");
185 er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
186 if (er == EMULATE_FAIL) {
187 kvm_err("Emulate %s MMIO space failed\n",
188 store ? "Store to" : "Load from");
189 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
190 ret = RESUME_HOST;
191 } else {
192 run->exit_reason = KVM_EXIT_MMIO;
193 ret = RESUME_HOST;
194 }
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800195 } else {
James Hogan3b08aec2016-06-09 14:19:20 +0100196 kvm_err("Illegal TLB %s fault address , cause %#x, PC: %p, BadVaddr: %#lx\n",
197 store ? "ST" : "LD", cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800198 kvm_mips_dump_host_tlbs();
199 kvm_arch_vcpu_dump_regs(vcpu);
200 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
201 ret = RESUME_HOST;
202 }
203 return ret;
204}
205
James Hogan3b08aec2016-06-09 14:19:20 +0100206static int kvm_trap_emul_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
207{
208 return kvm_trap_emul_handle_tlb_miss(vcpu, true);
209}
210
211static int kvm_trap_emul_handle_tlb_ld_miss(struct kvm_vcpu *vcpu)
212{
213 return kvm_trap_emul_handle_tlb_miss(vcpu, false);
214}
215
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800216static int kvm_trap_emul_handle_addr_err_st(struct kvm_vcpu *vcpu)
217{
218 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100219 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800220 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
James Hogan31cf7492016-06-09 14:19:09 +0100221 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800222 enum emulation_result er = EMULATE_DONE;
223 int ret = RESUME_GUEST;
224
225 if (KVM_GUEST_KERNEL_MODE(vcpu)
226 && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800227 kvm_debug("Emulate Store to MMIO space\n");
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800228 er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
229 if (er == EMULATE_FAIL) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700230 kvm_err("Emulate Store to MMIO space failed\n");
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800231 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
232 ret = RESUME_HOST;
233 } else {
234 run->exit_reason = KVM_EXIT_MMIO;
235 ret = RESUME_HOST;
236 }
237 } else {
James Hogan31cf7492016-06-09 14:19:09 +0100238 kvm_err("Address Error (STORE): cause %#x, PC: %p, BadVaddr: %#lx\n",
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700239 cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800240 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
241 ret = RESUME_HOST;
242 }
243 return ret;
244}
245
246static int kvm_trap_emul_handle_addr_err_ld(struct kvm_vcpu *vcpu)
247{
248 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100249 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800250 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
James Hogan31cf7492016-06-09 14:19:09 +0100251 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800252 enum emulation_result er = EMULATE_DONE;
253 int ret = RESUME_GUEST;
254
255 if (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1) {
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800256 kvm_debug("Emulate Load from MMIO space @ %#lx\n", badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800257 er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
258 if (er == EMULATE_FAIL) {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700259 kvm_err("Emulate Load from MMIO space failed\n");
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800260 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
261 ret = RESUME_HOST;
262 } else {
263 run->exit_reason = KVM_EXIT_MMIO;
264 ret = RESUME_HOST;
265 }
266 } else {
James Hogan31cf7492016-06-09 14:19:09 +0100267 kvm_err("Address Error (LOAD): cause %#x, PC: %p, BadVaddr: %#lx\n",
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700268 cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800269 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
270 ret = RESUME_HOST;
271 er = EMULATE_FAIL;
272 }
273 return ret;
274}
275
276static int kvm_trap_emul_handle_syscall(struct kvm_vcpu *vcpu)
277{
278 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100279 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100280 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800281 enum emulation_result er = EMULATE_DONE;
282 int ret = RESUME_GUEST;
283
284 er = kvm_mips_emulate_syscall(cause, opc, run, vcpu);
285 if (er == EMULATE_DONE)
286 ret = RESUME_GUEST;
287 else {
288 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
289 ret = RESUME_HOST;
290 }
291 return ret;
292}
293
294static int kvm_trap_emul_handle_res_inst(struct kvm_vcpu *vcpu)
295{
296 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100297 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100298 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800299 enum emulation_result er = EMULATE_DONE;
300 int ret = RESUME_GUEST;
301
302 er = kvm_mips_handle_ri(cause, opc, run, vcpu);
303 if (er == EMULATE_DONE)
304 ret = RESUME_GUEST;
305 else {
306 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
307 ret = RESUME_HOST;
308 }
309 return ret;
310}
311
312static int kvm_trap_emul_handle_break(struct kvm_vcpu *vcpu)
313{
314 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100315 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100316 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800317 enum emulation_result er = EMULATE_DONE;
318 int ret = RESUME_GUEST;
319
320 er = kvm_mips_emulate_bp_exc(cause, opc, run, vcpu);
321 if (er == EMULATE_DONE)
322 ret = RESUME_GUEST;
323 else {
324 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
325 ret = RESUME_HOST;
326 }
327 return ret;
328}
329
James Hogan0a560422015-02-06 16:03:57 +0000330static int kvm_trap_emul_handle_trap(struct kvm_vcpu *vcpu)
331{
332 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100333 u32 __user *opc = (u32 __user *)vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100334 u32 cause = vcpu->arch.host_cp0_cause;
James Hogan0a560422015-02-06 16:03:57 +0000335 enum emulation_result er = EMULATE_DONE;
336 int ret = RESUME_GUEST;
337
338 er = kvm_mips_emulate_trap_exc(cause, opc, run, vcpu);
339 if (er == EMULATE_DONE) {
340 ret = RESUME_GUEST;
341 } else {
342 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
343 ret = RESUME_HOST;
344 }
345 return ret;
346}
347
James Hoganc2537ed2015-02-06 10:56:27 +0000348static int kvm_trap_emul_handle_msa_fpe(struct kvm_vcpu *vcpu)
349{
350 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100351 u32 __user *opc = (u32 __user *)vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100352 u32 cause = vcpu->arch.host_cp0_cause;
James Hoganc2537ed2015-02-06 10:56:27 +0000353 enum emulation_result er = EMULATE_DONE;
354 int ret = RESUME_GUEST;
355
356 er = kvm_mips_emulate_msafpe_exc(cause, opc, run, vcpu);
357 if (er == EMULATE_DONE) {
358 ret = RESUME_GUEST;
359 } else {
360 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
361 ret = RESUME_HOST;
362 }
363 return ret;
364}
365
James Hogan1c0cd662015-02-06 10:56:27 +0000366static int kvm_trap_emul_handle_fpe(struct kvm_vcpu *vcpu)
367{
368 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100369 u32 __user *opc = (u32 __user *)vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100370 u32 cause = vcpu->arch.host_cp0_cause;
James Hogan1c0cd662015-02-06 10:56:27 +0000371 enum emulation_result er = EMULATE_DONE;
372 int ret = RESUME_GUEST;
373
374 er = kvm_mips_emulate_fpe_exc(cause, opc, run, vcpu);
375 if (er == EMULATE_DONE) {
376 ret = RESUME_GUEST;
377 } else {
378 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
379 ret = RESUME_HOST;
380 }
381 return ret;
382}
383
James Hoganc2537ed2015-02-06 10:56:27 +0000384/**
385 * kvm_trap_emul_handle_msa_disabled() - Guest used MSA while disabled in root.
386 * @vcpu: Virtual CPU context.
387 *
388 * Handle when the guest attempts to use MSA when it is disabled.
389 */
James Hogan98119ad2015-02-06 11:11:56 +0000390static int kvm_trap_emul_handle_msa_disabled(struct kvm_vcpu *vcpu)
391{
James Hoganc2537ed2015-02-06 10:56:27 +0000392 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan98119ad2015-02-06 11:11:56 +0000393 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100394 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100395 u32 cause = vcpu->arch.host_cp0_cause;
James Hogan98119ad2015-02-06 11:11:56 +0000396 enum emulation_result er = EMULATE_DONE;
397 int ret = RESUME_GUEST;
398
James Hoganc2537ed2015-02-06 10:56:27 +0000399 if (!kvm_mips_guest_has_msa(&vcpu->arch) ||
400 (kvm_read_c0_guest_status(cop0) & (ST0_CU1 | ST0_FR)) == ST0_CU1) {
401 /*
402 * No MSA in guest, or FPU enabled and not in FR=1 mode,
403 * guest reserved instruction exception
404 */
405 er = kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
406 } else if (!(kvm_read_c0_guest_config5(cop0) & MIPS_CONF5_MSAEN)) {
407 /* MSA disabled by guest, guest MSA disabled exception */
408 er = kvm_mips_emulate_msadis_exc(cause, opc, run, vcpu);
409 } else {
410 /* Restore MSA/FPU state */
411 kvm_own_msa(vcpu);
412 er = EMULATE_DONE;
413 }
James Hogan98119ad2015-02-06 11:11:56 +0000414
415 switch (er) {
416 case EMULATE_DONE:
417 ret = RESUME_GUEST;
418 break;
419
420 case EMULATE_FAIL:
421 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
422 ret = RESUME_HOST;
423 break;
424
425 default:
426 BUG();
427 }
428 return ret;
429}
430
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800431static int kvm_trap_emul_vm_init(struct kvm *kvm)
432{
433 return 0;
434}
435
436static int kvm_trap_emul_vcpu_init(struct kvm_vcpu *vcpu)
437{
James Hogan05108702016-06-15 19:29:56 +0100438 vcpu->arch.kscratch_enabled = 0xfc;
439
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800440 return 0;
441}
442
443static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu)
444{
445 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogane3429252016-06-15 19:30:00 +0100446 u32 config, config1;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800447 int vcpu_id = vcpu->vcpu_id;
448
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700449 /*
450 * Arch specific stuff, set up config registers properly so that the
James Hogan84260972016-07-04 19:35:15 +0100451 * guest will come up as expected
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800452 */
James Hogan84260972016-07-04 19:35:15 +0100453#ifndef CONFIG_CPU_MIPSR6
454 /* r2-r5, simulate a MIPS 24kc */
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800455 kvm_write_c0_guest_prid(cop0, 0x00019300);
James Hogan84260972016-07-04 19:35:15 +0100456#else
457 /* r6+, simulate a generic QEMU machine */
458 kvm_write_c0_guest_prid(cop0, 0x00010000);
459#endif
James Hogane3429252016-06-15 19:30:00 +0100460 /*
461 * Have config1, Cacheable, noncoherent, write-back, write allocate.
462 * Endianness, arch revision & virtually tagged icache should match
463 * host.
464 */
465 config = read_c0_config() & MIPS_CONF_AR;
James Hogan4e10b762016-06-15 19:30:01 +0100466 config |= MIPS_CONF_M | CONF_CM_CACHABLE_NONCOHERENT | MIPS_CONF_MT_TLB;
James Hogane3429252016-06-15 19:30:00 +0100467#ifdef CONFIG_CPU_BIG_ENDIAN
468 config |= CONF_BE;
469#endif
470 if (cpu_has_vtag_icache)
471 config |= MIPS_CONF_VI;
472 kvm_write_c0_guest_config(cop0, config);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800473
474 /* Read the cache characteristics from the host Config1 Register */
475 config1 = (read_c0_config1() & ~0x7f);
476
477 /* Set up MMU size */
478 config1 &= ~(0x3f << 25);
479 config1 |= ((KVM_MIPS_GUEST_TLB_SIZE - 1) << 25);
480
481 /* We unset some bits that we aren't emulating */
James Hogan4e10b762016-06-15 19:30:01 +0100482 config1 &= ~(MIPS_CONF1_C2 | MIPS_CONF1_MD | MIPS_CONF1_PC |
483 MIPS_CONF1_WR | MIPS_CONF1_CA);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800484 kvm_write_c0_guest_config1(cop0, config1);
485
James Hogan2211ee82015-03-04 15:56:47 +0000486 /* Have config3, no tertiary/secondary caches implemented */
487 kvm_write_c0_guest_config2(cop0, MIPS_CONF_M);
488 /* MIPS_CONF_M | (read_c0_config2() & 0xfff) */
489
James Hoganc7716072014-06-26 15:11:29 +0100490 /* Have config4, UserLocal */
491 kvm_write_c0_guest_config3(cop0, MIPS_CONF_M | MIPS_CONF3_ULRI);
492
493 /* Have config5 */
494 kvm_write_c0_guest_config4(cop0, MIPS_CONF_M);
495
496 /* No config6 */
497 kvm_write_c0_guest_config5(cop0, 0);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800498
499 /* Set Wait IE/IXMT Ignore in Config7, IAR, AR */
500 kvm_write_c0_guest_config7(cop0, (MIPS_CONF7_WII) | (1 << 10));
501
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700502 /*
Adam Buchbinder92a76f62016-02-25 00:44:58 -0800503 * Setup IntCtl defaults, compatibility mode for timer interrupts (HW5)
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700504 */
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800505 kvm_write_c0_guest_intctl(cop0, 0xFC000000);
506
507 /* Put in vcpu id as CPUNum into Ebase Reg to handle SMP Guests */
James Hogan37af2f32016-05-11 13:50:49 +0100508 kvm_write_c0_guest_ebase(cop0, KVM_GUEST_KSEG0 |
509 (vcpu_id & MIPS_EBASE_CPUNUM));
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800510
511 return 0;
512}
513
James Hoganf5c43bd2016-06-15 19:29:49 +0100514static unsigned long kvm_trap_emul_num_regs(struct kvm_vcpu *vcpu)
515{
516 return 0;
517}
518
519static int kvm_trap_emul_copy_reg_indices(struct kvm_vcpu *vcpu,
520 u64 __user *indices)
521{
522 return 0;
523}
524
James Hoganf8be02d2014-05-29 10:16:29 +0100525static int kvm_trap_emul_get_one_reg(struct kvm_vcpu *vcpu,
526 const struct kvm_one_reg *reg,
527 s64 *v)
528{
529 switch (reg->id) {
530 case KVM_REG_MIPS_CP0_COUNT:
James Hogane30492b2014-05-29 10:16:35 +0100531 *v = kvm_mips_read_count(vcpu);
James Hoganf8be02d2014-05-29 10:16:29 +0100532 break;
James Hoganf8239342014-05-29 10:16:37 +0100533 case KVM_REG_MIPS_COUNT_CTL:
534 *v = vcpu->arch.count_ctl;
535 break;
536 case KVM_REG_MIPS_COUNT_RESUME:
537 *v = ktime_to_ns(vcpu->arch.count_resume);
538 break;
James Hoganf74a8e22014-05-29 10:16:38 +0100539 case KVM_REG_MIPS_COUNT_HZ:
540 *v = vcpu->arch.count_hz;
541 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100542 default:
543 return -EINVAL;
544 }
545 return 0;
546}
547
548static int kvm_trap_emul_set_one_reg(struct kvm_vcpu *vcpu,
549 const struct kvm_one_reg *reg,
550 s64 v)
551{
552 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganf8239342014-05-29 10:16:37 +0100553 int ret = 0;
James Hoganc7716072014-06-26 15:11:29 +0100554 unsigned int cur, change;
James Hoganf8be02d2014-05-29 10:16:29 +0100555
556 switch (reg->id) {
557 case KVM_REG_MIPS_CP0_COUNT:
James Hogane30492b2014-05-29 10:16:35 +0100558 kvm_mips_write_count(vcpu, v);
James Hoganf8be02d2014-05-29 10:16:29 +0100559 break;
560 case KVM_REG_MIPS_CP0_COMPARE:
James Hoganb45bacd2016-04-22 10:38:46 +0100561 kvm_mips_write_compare(vcpu, v, false);
James Hogane30492b2014-05-29 10:16:35 +0100562 break;
563 case KVM_REG_MIPS_CP0_CAUSE:
564 /*
565 * If the timer is stopped or started (DC bit) it must look
566 * atomic with changes to the interrupt pending bits (TI, IRQ5).
567 * A timer interrupt should not happen in between.
568 */
569 if ((kvm_read_c0_guest_cause(cop0) ^ v) & CAUSEF_DC) {
570 if (v & CAUSEF_DC) {
571 /* disable timer first */
572 kvm_mips_count_disable_cause(vcpu);
573 kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v);
574 } else {
575 /* enable timer last */
576 kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v);
577 kvm_mips_count_enable_cause(vcpu);
578 }
579 } else {
580 kvm_write_c0_guest_cause(cop0, v);
581 }
James Hoganf8be02d2014-05-29 10:16:29 +0100582 break;
James Hoganc7716072014-06-26 15:11:29 +0100583 case KVM_REG_MIPS_CP0_CONFIG:
584 /* read-only for now */
585 break;
586 case KVM_REG_MIPS_CP0_CONFIG1:
587 cur = kvm_read_c0_guest_config1(cop0);
588 change = (cur ^ v) & kvm_mips_config1_wrmask(vcpu);
589 if (change) {
590 v = cur ^ change;
591 kvm_write_c0_guest_config1(cop0, v);
592 }
593 break;
594 case KVM_REG_MIPS_CP0_CONFIG2:
595 /* read-only for now */
596 break;
597 case KVM_REG_MIPS_CP0_CONFIG3:
598 cur = kvm_read_c0_guest_config3(cop0);
599 change = (cur ^ v) & kvm_mips_config3_wrmask(vcpu);
600 if (change) {
601 v = cur ^ change;
602 kvm_write_c0_guest_config3(cop0, v);
603 }
604 break;
605 case KVM_REG_MIPS_CP0_CONFIG4:
606 cur = kvm_read_c0_guest_config4(cop0);
607 change = (cur ^ v) & kvm_mips_config4_wrmask(vcpu);
608 if (change) {
609 v = cur ^ change;
610 kvm_write_c0_guest_config4(cop0, v);
611 }
612 break;
613 case KVM_REG_MIPS_CP0_CONFIG5:
614 cur = kvm_read_c0_guest_config5(cop0);
615 change = (cur ^ v) & kvm_mips_config5_wrmask(vcpu);
616 if (change) {
617 v = cur ^ change;
618 kvm_write_c0_guest_config5(cop0, v);
619 }
620 break;
James Hoganf8239342014-05-29 10:16:37 +0100621 case KVM_REG_MIPS_COUNT_CTL:
622 ret = kvm_mips_set_count_ctl(vcpu, v);
623 break;
624 case KVM_REG_MIPS_COUNT_RESUME:
625 ret = kvm_mips_set_count_resume(vcpu, v);
626 break;
James Hoganf74a8e22014-05-29 10:16:38 +0100627 case KVM_REG_MIPS_COUNT_HZ:
628 ret = kvm_mips_set_count_hz(vcpu, v);
629 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100630 default:
631 return -EINVAL;
632 }
James Hoganf8239342014-05-29 10:16:37 +0100633 return ret;
James Hoganf8be02d2014-05-29 10:16:29 +0100634}
635
James Hogana60b8432016-11-12 00:00:13 +0000636static int kvm_trap_emul_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
James Hoganb86ecb32015-02-09 16:35:20 +0000637{
James Hogan1581ff32016-11-16 23:48:56 +0000638 unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
639
640 /* Allocate new kernel and user ASIDs if needed */
641
642 if ((vcpu->arch.guest_kernel_asid[cpu] ^ asid_cache(cpu)) &
643 asid_version_mask(cpu)) {
644 kvm_get_new_mmu_context(&vcpu->arch.guest_kernel_mm, cpu, vcpu);
645 vcpu->arch.guest_kernel_asid[cpu] =
646 vcpu->arch.guest_kernel_mm.context.asid[cpu];
647
648 kvm_debug("[%d]: cpu_context: %#lx\n", cpu,
649 cpu_context(cpu, current->mm));
650 kvm_debug("[%d]: Allocated new ASID for Guest Kernel: %#x\n",
651 cpu, vcpu->arch.guest_kernel_asid[cpu]);
652 }
653
654 if ((vcpu->arch.guest_user_asid[cpu] ^ asid_cache(cpu)) &
655 asid_version_mask(cpu)) {
656 kvm_get_new_mmu_context(&vcpu->arch.guest_user_mm, cpu, vcpu);
657 vcpu->arch.guest_user_asid[cpu] =
658 vcpu->arch.guest_user_mm.context.asid[cpu];
659
660 kvm_debug("[%d]: cpu_context: %#lx\n", cpu,
661 cpu_context(cpu, current->mm));
662 kvm_debug("[%d]: Allocated new ASID for Guest User: %#x\n", cpu,
663 vcpu->arch.guest_user_asid[cpu]);
664 }
665
666 /*
667 * Were we in guest context? If so then the pre-empted ASID is
668 * no longer valid, we need to set it to what it should be based
669 * on the mode of the Guest (Kernel/User)
670 */
671 if (current->flags & PF_VCPU) {
672 if (KVM_GUEST_KERNEL_MODE(vcpu))
673 write_c0_entryhi(vcpu->arch.guest_kernel_asid[cpu] &
674 asid_mask);
675 else
676 write_c0_entryhi(vcpu->arch.guest_user_asid[cpu] &
677 asid_mask);
678 ehb();
679 }
680
James Hoganb86ecb32015-02-09 16:35:20 +0000681 return 0;
682}
683
James Hogana60b8432016-11-12 00:00:13 +0000684static int kvm_trap_emul_vcpu_put(struct kvm_vcpu *vcpu, int cpu)
James Hoganb86ecb32015-02-09 16:35:20 +0000685{
James Hogana60b8432016-11-12 00:00:13 +0000686 kvm_lose_fpu(vcpu);
687
James Hogan1581ff32016-11-16 23:48:56 +0000688 if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) &
689 asid_version_mask(cpu))) {
690 kvm_debug("%s: Dropping MMU Context: %#lx\n", __func__,
691 cpu_context(cpu, current->mm));
692 drop_mmu_context(current->mm, cpu);
693 }
694 write_c0_entryhi(cpu_asid(cpu, current->mm));
695 ehb();
696
James Hoganb86ecb32015-02-09 16:35:20 +0000697 return 0;
698}
699
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800700static struct kvm_mips_callbacks kvm_trap_emul_callbacks = {
701 /* exit handlers */
702 .handle_cop_unusable = kvm_trap_emul_handle_cop_unusable,
703 .handle_tlb_mod = kvm_trap_emul_handle_tlb_mod,
704 .handle_tlb_st_miss = kvm_trap_emul_handle_tlb_st_miss,
705 .handle_tlb_ld_miss = kvm_trap_emul_handle_tlb_ld_miss,
706 .handle_addr_err_st = kvm_trap_emul_handle_addr_err_st,
707 .handle_addr_err_ld = kvm_trap_emul_handle_addr_err_ld,
708 .handle_syscall = kvm_trap_emul_handle_syscall,
709 .handle_res_inst = kvm_trap_emul_handle_res_inst,
710 .handle_break = kvm_trap_emul_handle_break,
James Hogan0a560422015-02-06 16:03:57 +0000711 .handle_trap = kvm_trap_emul_handle_trap,
James Hoganc2537ed2015-02-06 10:56:27 +0000712 .handle_msa_fpe = kvm_trap_emul_handle_msa_fpe,
James Hogan1c0cd662015-02-06 10:56:27 +0000713 .handle_fpe = kvm_trap_emul_handle_fpe,
James Hogan98119ad2015-02-06 11:11:56 +0000714 .handle_msa_disabled = kvm_trap_emul_handle_msa_disabled,
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800715
716 .vm_init = kvm_trap_emul_vm_init,
717 .vcpu_init = kvm_trap_emul_vcpu_init,
718 .vcpu_setup = kvm_trap_emul_vcpu_setup,
719 .gva_to_gpa = kvm_trap_emul_gva_to_gpa_cb,
720 .queue_timer_int = kvm_mips_queue_timer_int_cb,
721 .dequeue_timer_int = kvm_mips_dequeue_timer_int_cb,
722 .queue_io_int = kvm_mips_queue_io_int_cb,
723 .dequeue_io_int = kvm_mips_dequeue_io_int_cb,
724 .irq_deliver = kvm_mips_irq_deliver_cb,
725 .irq_clear = kvm_mips_irq_clear_cb,
James Hoganf5c43bd2016-06-15 19:29:49 +0100726 .num_regs = kvm_trap_emul_num_regs,
727 .copy_reg_indices = kvm_trap_emul_copy_reg_indices,
James Hoganf8be02d2014-05-29 10:16:29 +0100728 .get_one_reg = kvm_trap_emul_get_one_reg,
729 .set_one_reg = kvm_trap_emul_set_one_reg,
James Hogana60b8432016-11-12 00:00:13 +0000730 .vcpu_load = kvm_trap_emul_vcpu_load,
731 .vcpu_put = kvm_trap_emul_vcpu_put,
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800732};
733
734int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks)
735{
736 *install_callbacks = &kvm_trap_emul_callbacks;
737 return 0;
738}