blob: 6a0d7040d8820c695ac926b02e849e9074bc54c0 [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 Hogan867f4da2017-03-14 10:25:47 +000015#include <linux/log2.h>
James Hogandacc3ed2016-08-19 15:27:22 +010016#include <linux/uaccess.h>
James Hogan1581ff32016-11-16 23:48:56 +000017#include <linux/vmalloc.h>
18#include <asm/mmu_context.h>
James Hoganf7f14272016-09-08 22:57:03 +010019#include <asm/pgalloc.h>
Sanjay Lalf5c236d2012-11-21 18:34:09 -080020
Deng-Cheng Zhud7d5b052014-06-26 12:11:38 -070021#include "interrupt.h"
Sanjay Lalf5c236d2012-11-21 18:34:09 -080022
23static gpa_t kvm_trap_emul_gva_to_gpa_cb(gva_t gva)
24{
25 gpa_t gpa;
James Hogan8cffd192016-06-09 14:19:08 +010026 gva_t kseg = KSEGX(gva);
James Hoganb8f79dd2015-05-11 23:31:45 +010027 gva_t gkseg = KVM_GUEST_KSEGX(gva);
Sanjay Lalf5c236d2012-11-21 18:34:09 -080028
29 if ((kseg == CKSEG0) || (kseg == CKSEG1))
30 gpa = CPHYSADDR(gva);
James Hoganb8f79dd2015-05-11 23:31:45 +010031 else if (gkseg == KVM_GUEST_KSEG0)
32 gpa = KVM_GUEST_CPHYSADDR(gva);
Sanjay Lalf5c236d2012-11-21 18:34:09 -080033 else {
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -070034 kvm_err("%s: cannot find GPA for GVA: %#lx\n", __func__, gva);
Sanjay Lalf5c236d2012-11-21 18:34:09 -080035 kvm_mips_dump_host_tlbs();
36 gpa = KVM_INVALID_ADDR;
37 }
38
Sanjay Lalf5c236d2012-11-21 18:34:09 -080039 kvm_debug("%s: gva %#lx, gpa: %#llx\n", __func__, gva, gpa);
Sanjay Lalf5c236d2012-11-21 18:34:09 -080040
41 return gpa;
42}
43
James Hogan28c1e762017-03-14 10:15:24 +000044static int kvm_trap_emul_no_handler(struct kvm_vcpu *vcpu)
45{
46 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
47 u32 cause = vcpu->arch.host_cp0_cause;
48 u32 exccode = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
49 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
50 u32 inst = 0;
51
52 /*
53 * Fetch the instruction.
54 */
55 if (cause & CAUSEF_BD)
56 opc += 1;
57 kvm_get_badinstr(opc, vcpu, &inst);
58
James Hogana27660f2017-03-14 10:15:25 +000059 kvm_err("Exception Code: %d not handled @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#x\n",
James Hogan28c1e762017-03-14 10:15:24 +000060 exccode, opc, inst, badvaddr,
61 kvm_read_c0_guest_status(vcpu->arch.cop0));
62 kvm_arch_vcpu_dump_regs(vcpu);
63 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
64 return RESUME_HOST;
65}
66
Sanjay Lalf5c236d2012-11-21 18:34:09 -080067static int kvm_trap_emul_handle_cop_unusable(struct kvm_vcpu *vcpu)
68{
James Hogan1c0cd662015-02-06 10:56:27 +000069 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lalf5c236d2012-11-21 18:34:09 -080070 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +010071 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +010072 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -080073 enum emulation_result er = EMULATE_DONE;
74 int ret = RESUME_GUEST;
75
James Hogan1c0cd662015-02-06 10:56:27 +000076 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) {
77 /* FPU Unusable */
78 if (!kvm_mips_guest_has_fpu(&vcpu->arch) ||
79 (kvm_read_c0_guest_status(cop0) & ST0_CU1) == 0) {
80 /*
81 * Unusable/no FPU in guest:
82 * deliver guest COP1 Unusable Exception
83 */
84 er = kvm_mips_emulate_fpu_exc(cause, opc, run, vcpu);
85 } else {
86 /* Restore FPU state */
87 kvm_own_fpu(vcpu);
88 er = EMULATE_DONE;
89 }
90 } else {
Sanjay Lalf5c236d2012-11-21 18:34:09 -080091 er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
James Hogan1c0cd662015-02-06 10:56:27 +000092 }
Sanjay Lalf5c236d2012-11-21 18:34:09 -080093
94 switch (er) {
95 case EMULATE_DONE:
96 ret = RESUME_GUEST;
97 break;
98
99 case EMULATE_FAIL:
100 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
101 ret = RESUME_HOST;
102 break;
103
104 case EMULATE_WAIT:
105 run->exit_reason = KVM_EXIT_INTR;
106 ret = RESUME_HOST;
107 break;
108
James Hogan955d8dc2017-03-14 10:15:14 +0000109 case EMULATE_HYPERCALL:
110 ret = kvm_mips_handle_hypcall(vcpu);
111 break;
112
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800113 default:
114 BUG();
115 }
116 return ret;
117}
118
James Hogan420ea092016-12-06 19:27:18 +0000119static int kvm_mips_bad_load(u32 cause, u32 *opc, struct kvm_run *run,
120 struct kvm_vcpu *vcpu)
121{
122 enum emulation_result er;
123 union mips_instruction inst;
124 int err;
125
126 /* A code fetch fault doesn't count as an MMIO */
127 if (kvm_is_ifetch_fault(&vcpu->arch)) {
128 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
129 return RESUME_HOST;
130 }
131
132 /* Fetch the instruction. */
133 if (cause & CAUSEF_BD)
134 opc += 1;
135 err = kvm_get_badinstr(opc, vcpu, &inst.word);
136 if (err) {
137 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
138 return RESUME_HOST;
139 }
140
141 /* Emulate the load */
142 er = kvm_mips_emulate_load(inst, cause, run, vcpu);
143 if (er == EMULATE_FAIL) {
144 kvm_err("Emulate load from MMIO space failed\n");
145 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
146 } else {
147 run->exit_reason = KVM_EXIT_MMIO;
148 }
149 return RESUME_HOST;
150}
151
152static int kvm_mips_bad_store(u32 cause, u32 *opc, struct kvm_run *run,
153 struct kvm_vcpu *vcpu)
154{
155 enum emulation_result er;
156 union mips_instruction inst;
157 int err;
158
159 /* Fetch the instruction. */
160 if (cause & CAUSEF_BD)
161 opc += 1;
162 err = kvm_get_badinstr(opc, vcpu, &inst.word);
163 if (err) {
164 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
165 return RESUME_HOST;
166 }
167
168 /* Emulate the store */
169 er = kvm_mips_emulate_store(inst, cause, run, vcpu);
170 if (er == EMULATE_FAIL) {
171 kvm_err("Emulate store to MMIO space failed\n");
172 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
173 } else {
174 run->exit_reason = KVM_EXIT_MMIO;
175 }
176 return RESUME_HOST;
177}
178
179static int kvm_mips_bad_access(u32 cause, u32 *opc, struct kvm_run *run,
180 struct kvm_vcpu *vcpu, bool store)
181{
182 if (store)
183 return kvm_mips_bad_store(cause, opc, run, vcpu);
184 else
185 return kvm_mips_bad_load(cause, opc, run, vcpu);
186}
187
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800188static int kvm_trap_emul_handle_tlb_mod(struct kvm_vcpu *vcpu)
189{
James Hogan64ebc9e2016-12-13 13:02:36 +0000190 struct mips_coproc *cop0 = vcpu->arch.cop0;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800191 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100192 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800193 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
James Hogan31cf7492016-06-09 14:19:09 +0100194 u32 cause = vcpu->arch.host_cp0_cause;
James Hogan64ebc9e2016-12-13 13:02:36 +0000195 struct kvm_mips_tlb *tlb;
196 unsigned long entryhi;
197 int index;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800198
199 if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
200 || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700201 /*
James Hogan64ebc9e2016-12-13 13:02:36 +0000202 * First find the mapping in the guest TLB. If the failure to
203 * write was due to the guest TLB, it should be up to the guest
204 * to handle it.
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800205 */
James Hogan64ebc9e2016-12-13 13:02:36 +0000206 entryhi = (badvaddr & VPN2_MASK) |
207 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
208 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
209
210 /*
211 * These should never happen.
212 * They would indicate stale host TLB entries.
213 */
214 if (unlikely(index < 0)) {
215 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
216 return RESUME_HOST;
217 }
218 tlb = vcpu->arch.guest_tlb + index;
219 if (unlikely(!TLB_IS_VALID(*tlb, badvaddr))) {
220 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
221 return RESUME_HOST;
222 }
223
224 /*
225 * Guest entry not dirty? That would explain the TLB modified
226 * exception. Relay that on to the guest so it can handle it.
227 */
228 if (!TLB_IS_DIRTY(*tlb, badvaddr)) {
229 kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
230 return RESUME_GUEST;
231 }
232
233 if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, badvaddr,
234 true))
235 /* Not writable, needs handling as MMIO */
236 return kvm_mips_bad_store(cause, opc, run, vcpu);
237 return RESUME_GUEST;
238 } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
239 if (kvm_mips_handle_kseg0_tlb_fault(badvaddr, vcpu, true) < 0)
240 /* Not writable, needs handling as MMIO */
241 return kvm_mips_bad_store(cause, opc, run, vcpu);
242 return RESUME_GUEST;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800243 } else {
James Hogan64ebc9e2016-12-13 13:02:36 +0000244 /* host kernel addresses are all handled as MMIO */
245 return kvm_mips_bad_store(cause, opc, run, vcpu);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800246 }
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800247}
248
James Hogan3b08aec2016-06-09 14:19:20 +0100249static int kvm_trap_emul_handle_tlb_miss(struct kvm_vcpu *vcpu, bool store)
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800250{
251 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100252 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800253 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
James Hogan31cf7492016-06-09 14:19:09 +0100254 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800255 enum emulation_result er = EMULATE_DONE;
256 int ret = RESUME_GUEST;
257
258 if (((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR)
259 && KVM_GUEST_KERNEL_MODE(vcpu)) {
260 if (kvm_mips_handle_commpage_tlb_fault(badvaddr, vcpu) < 0) {
261 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
262 ret = RESUME_HOST;
263 }
264 } else if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
265 || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
James Hogan3b08aec2016-06-09 14:19:20 +0100266 kvm_debug("USER ADDR TLB %s fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
267 store ? "ST" : "LD", cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800268
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700269 /*
270 * User Address (UA) fault, this could happen if
271 * (1) TLB entry not present/valid in both Guest and shadow host
272 * TLBs, in this case we pass on the fault to the guest
273 * kernel and let it handle it.
274 * (2) TLB entry is present in the Guest TLB but not in the
275 * shadow, in this case we inject the TLB from the Guest TLB
276 * into the shadow host TLB
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800277 */
278
James Hogan577ed7f2015-05-01 14:56:31 +0100279 er = kvm_mips_handle_tlbmiss(cause, opc, run, vcpu, store);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800280 if (er == EMULATE_DONE)
281 ret = RESUME_GUEST;
282 else {
283 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
284 ret = RESUME_HOST;
285 }
286 } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
James Hogan3b08aec2016-06-09 14:19:20 +0100287 /*
288 * All KSEG0 faults are handled by KVM, as the guest kernel does
289 * not expect to ever get them
290 */
James Hoganb8f79dd2015-05-11 23:31:45 +0100291 if (kvm_mips_handle_kseg0_tlb_fault(badvaddr, vcpu, store) < 0)
292 ret = kvm_mips_bad_access(cause, opc, run, vcpu, store);
James Hogand5888472016-08-19 15:09:47 +0100293 } else if (KVM_GUEST_KERNEL_MODE(vcpu)
294 && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
295 /*
296 * With EVA we may get a TLB exception instead of an address
297 * error when the guest performs MMIO to KSeg1 addresses.
298 */
James Hogan420ea092016-12-06 19:27:18 +0000299 ret = kvm_mips_bad_access(cause, opc, run, vcpu, store);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800300 } else {
James Hogan3b08aec2016-06-09 14:19:20 +0100301 kvm_err("Illegal TLB %s fault address , cause %#x, PC: %p, BadVaddr: %#lx\n",
302 store ? "ST" : "LD", cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800303 kvm_mips_dump_host_tlbs();
304 kvm_arch_vcpu_dump_regs(vcpu);
305 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
306 ret = RESUME_HOST;
307 }
308 return ret;
309}
310
James Hogan3b08aec2016-06-09 14:19:20 +0100311static int kvm_trap_emul_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
312{
313 return kvm_trap_emul_handle_tlb_miss(vcpu, true);
314}
315
316static int kvm_trap_emul_handle_tlb_ld_miss(struct kvm_vcpu *vcpu)
317{
318 return kvm_trap_emul_handle_tlb_miss(vcpu, false);
319}
320
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800321static int kvm_trap_emul_handle_addr_err_st(struct kvm_vcpu *vcpu)
322{
323 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100324 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800325 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
James Hogan31cf7492016-06-09 14:19:09 +0100326 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800327 int ret = RESUME_GUEST;
328
329 if (KVM_GUEST_KERNEL_MODE(vcpu)
330 && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
James Hogan420ea092016-12-06 19:27:18 +0000331 ret = kvm_mips_bad_store(cause, opc, run, vcpu);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800332 } else {
James Hogan31cf7492016-06-09 14:19:09 +0100333 kvm_err("Address Error (STORE): cause %#x, PC: %p, BadVaddr: %#lx\n",
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700334 cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800335 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
336 ret = RESUME_HOST;
337 }
338 return ret;
339}
340
341static int kvm_trap_emul_handle_addr_err_ld(struct kvm_vcpu *vcpu)
342{
343 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100344 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800345 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
James Hogan31cf7492016-06-09 14:19:09 +0100346 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800347 int ret = RESUME_GUEST;
348
349 if (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1) {
James Hogan420ea092016-12-06 19:27:18 +0000350 ret = kvm_mips_bad_load(cause, opc, run, vcpu);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800351 } else {
James Hogan31cf7492016-06-09 14:19:09 +0100352 kvm_err("Address Error (LOAD): cause %#x, PC: %p, BadVaddr: %#lx\n",
Deng-Cheng Zhu6ad78a52014-06-26 12:11:35 -0700353 cause, opc, badvaddr);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800354 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
355 ret = RESUME_HOST;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800356 }
357 return ret;
358}
359
360static int kvm_trap_emul_handle_syscall(struct kvm_vcpu *vcpu)
361{
362 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100363 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100364 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800365 enum emulation_result er = EMULATE_DONE;
366 int ret = RESUME_GUEST;
367
368 er = kvm_mips_emulate_syscall(cause, opc, run, vcpu);
369 if (er == EMULATE_DONE)
370 ret = RESUME_GUEST;
371 else {
372 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
373 ret = RESUME_HOST;
374 }
375 return ret;
376}
377
378static int kvm_trap_emul_handle_res_inst(struct kvm_vcpu *vcpu)
379{
380 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100381 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100382 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800383 enum emulation_result er = EMULATE_DONE;
384 int ret = RESUME_GUEST;
385
386 er = kvm_mips_handle_ri(cause, opc, run, vcpu);
387 if (er == EMULATE_DONE)
388 ret = RESUME_GUEST;
389 else {
390 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
391 ret = RESUME_HOST;
392 }
393 return ret;
394}
395
396static int kvm_trap_emul_handle_break(struct kvm_vcpu *vcpu)
397{
398 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100399 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100400 u32 cause = vcpu->arch.host_cp0_cause;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800401 enum emulation_result er = EMULATE_DONE;
402 int ret = RESUME_GUEST;
403
404 er = kvm_mips_emulate_bp_exc(cause, opc, run, vcpu);
405 if (er == EMULATE_DONE)
406 ret = RESUME_GUEST;
407 else {
408 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
409 ret = RESUME_HOST;
410 }
411 return ret;
412}
413
James Hogan0a560422015-02-06 16:03:57 +0000414static int kvm_trap_emul_handle_trap(struct kvm_vcpu *vcpu)
415{
416 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100417 u32 __user *opc = (u32 __user *)vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100418 u32 cause = vcpu->arch.host_cp0_cause;
James Hogan0a560422015-02-06 16:03:57 +0000419 enum emulation_result er = EMULATE_DONE;
420 int ret = RESUME_GUEST;
421
422 er = kvm_mips_emulate_trap_exc(cause, opc, run, vcpu);
423 if (er == EMULATE_DONE) {
424 ret = RESUME_GUEST;
425 } else {
426 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
427 ret = RESUME_HOST;
428 }
429 return ret;
430}
431
James Hoganc2537ed2015-02-06 10:56:27 +0000432static int kvm_trap_emul_handle_msa_fpe(struct kvm_vcpu *vcpu)
433{
434 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100435 u32 __user *opc = (u32 __user *)vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100436 u32 cause = vcpu->arch.host_cp0_cause;
James Hoganc2537ed2015-02-06 10:56:27 +0000437 enum emulation_result er = EMULATE_DONE;
438 int ret = RESUME_GUEST;
439
440 er = kvm_mips_emulate_msafpe_exc(cause, opc, run, vcpu);
441 if (er == EMULATE_DONE) {
442 ret = RESUME_GUEST;
443 } else {
444 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
445 ret = RESUME_HOST;
446 }
447 return ret;
448}
449
James Hogan1c0cd662015-02-06 10:56:27 +0000450static int kvm_trap_emul_handle_fpe(struct kvm_vcpu *vcpu)
451{
452 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100453 u32 __user *opc = (u32 __user *)vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100454 u32 cause = vcpu->arch.host_cp0_cause;
James Hogan1c0cd662015-02-06 10:56:27 +0000455 enum emulation_result er = EMULATE_DONE;
456 int ret = RESUME_GUEST;
457
458 er = kvm_mips_emulate_fpe_exc(cause, opc, run, vcpu);
459 if (er == EMULATE_DONE) {
460 ret = RESUME_GUEST;
461 } else {
462 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
463 ret = RESUME_HOST;
464 }
465 return ret;
466}
467
James Hoganc2537ed2015-02-06 10:56:27 +0000468/**
469 * kvm_trap_emul_handle_msa_disabled() - Guest used MSA while disabled in root.
470 * @vcpu: Virtual CPU context.
471 *
472 * Handle when the guest attempts to use MSA when it is disabled.
473 */
James Hogan98119ad2015-02-06 11:11:56 +0000474static int kvm_trap_emul_handle_msa_disabled(struct kvm_vcpu *vcpu)
475{
James Hoganc2537ed2015-02-06 10:56:27 +0000476 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogan98119ad2015-02-06 11:11:56 +0000477 struct kvm_run *run = vcpu->run;
James Hogan8cffd192016-06-09 14:19:08 +0100478 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
James Hogan31cf7492016-06-09 14:19:09 +0100479 u32 cause = vcpu->arch.host_cp0_cause;
James Hogan98119ad2015-02-06 11:11:56 +0000480 enum emulation_result er = EMULATE_DONE;
481 int ret = RESUME_GUEST;
482
James Hoganc2537ed2015-02-06 10:56:27 +0000483 if (!kvm_mips_guest_has_msa(&vcpu->arch) ||
484 (kvm_read_c0_guest_status(cop0) & (ST0_CU1 | ST0_FR)) == ST0_CU1) {
485 /*
486 * No MSA in guest, or FPU enabled and not in FR=1 mode,
487 * guest reserved instruction exception
488 */
489 er = kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
490 } else if (!(kvm_read_c0_guest_config5(cop0) & MIPS_CONF5_MSAEN)) {
491 /* MSA disabled by guest, guest MSA disabled exception */
492 er = kvm_mips_emulate_msadis_exc(cause, opc, run, vcpu);
493 } else {
494 /* Restore MSA/FPU state */
495 kvm_own_msa(vcpu);
496 er = EMULATE_DONE;
497 }
James Hogan98119ad2015-02-06 11:11:56 +0000498
499 switch (er) {
500 case EMULATE_DONE:
501 ret = RESUME_GUEST;
502 break;
503
504 case EMULATE_FAIL:
505 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
506 ret = RESUME_HOST;
507 break;
508
509 default:
510 BUG();
511 }
512 return ret;
513}
514
James Hoganedab4fe2017-03-14 10:15:23 +0000515static int kvm_trap_emul_hardware_enable(void)
516{
517 return 0;
518}
519
520static void kvm_trap_emul_hardware_disable(void)
521{
522}
523
James Hogan607ef2f2017-03-14 10:15:22 +0000524static int kvm_trap_emul_check_extension(struct kvm *kvm, long ext)
525{
526 int r;
527
528 switch (ext) {
529 case KVM_CAP_MIPS_TE:
530 r = 1;
531 break;
532 default:
533 r = 0;
534 break;
535 }
536
537 return r;
538}
539
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800540static int kvm_trap_emul_vcpu_init(struct kvm_vcpu *vcpu)
541{
James Hoganf7f14272016-09-08 22:57:03 +0100542 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
543 struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
544
James Hoganf7f14272016-09-08 22:57:03 +0100545 /*
546 * Allocate GVA -> HPA page tables.
547 * MIPS doesn't use the mm_struct pointer argument.
548 */
549 kern_mm->pgd = pgd_alloc(kern_mm);
550 if (!kern_mm->pgd)
551 return -ENOMEM;
552
553 user_mm->pgd = pgd_alloc(user_mm);
554 if (!user_mm->pgd) {
555 pgd_free(kern_mm, kern_mm->pgd);
556 return -ENOMEM;
557 }
558
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800559 return 0;
560}
561
James Hoganf7f14272016-09-08 22:57:03 +0100562static void kvm_mips_emul_free_gva_pt(pgd_t *pgd)
563{
564 /* Don't free host kernel page tables copied from init_mm.pgd */
565 const unsigned long end = 0x80000000;
566 unsigned long pgd_va, pud_va, pmd_va;
567 pud_t *pud;
568 pmd_t *pmd;
569 pte_t *pte;
570 int i, j, k;
571
572 for (i = 0; i < USER_PTRS_PER_PGD; i++) {
573 if (pgd_none(pgd[i]))
574 continue;
575
576 pgd_va = (unsigned long)i << PGDIR_SHIFT;
577 if (pgd_va >= end)
578 break;
579 pud = pud_offset(pgd + i, 0);
580 for (j = 0; j < PTRS_PER_PUD; j++) {
581 if (pud_none(pud[j]))
582 continue;
583
584 pud_va = pgd_va | ((unsigned long)j << PUD_SHIFT);
585 if (pud_va >= end)
586 break;
587 pmd = pmd_offset(pud + j, 0);
588 for (k = 0; k < PTRS_PER_PMD; k++) {
589 if (pmd_none(pmd[k]))
590 continue;
591
592 pmd_va = pud_va | (k << PMD_SHIFT);
593 if (pmd_va >= end)
594 break;
595 pte = pte_offset(pmd + k, 0);
596 pte_free_kernel(NULL, pte);
597 }
598 pmd_free(NULL, pmd);
599 }
600 pud_free(NULL, pud);
601 }
602 pgd_free(NULL, pgd);
603}
604
James Hogan630766b2016-09-08 23:00:24 +0100605static void kvm_trap_emul_vcpu_uninit(struct kvm_vcpu *vcpu)
606{
James Hoganf7f14272016-09-08 22:57:03 +0100607 kvm_mips_emul_free_gva_pt(vcpu->arch.guest_kernel_mm.pgd);
608 kvm_mips_emul_free_gva_pt(vcpu->arch.guest_user_mm.pgd);
James Hogan630766b2016-09-08 23:00:24 +0100609}
610
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800611static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu)
612{
613 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hogane3429252016-06-15 19:30:00 +0100614 u32 config, config1;
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800615 int vcpu_id = vcpu->vcpu_id;
616
James Hogana517c1a2017-03-14 10:15:21 +0000617 /* Start off the timer at 100 MHz */
618 kvm_mips_init_count(vcpu, 100*1000*1000);
619
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700620 /*
621 * Arch specific stuff, set up config registers properly so that the
James Hogan84260972016-07-04 19:35:15 +0100622 * guest will come up as expected
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800623 */
James Hogan84260972016-07-04 19:35:15 +0100624#ifndef CONFIG_CPU_MIPSR6
625 /* r2-r5, simulate a MIPS 24kc */
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800626 kvm_write_c0_guest_prid(cop0, 0x00019300);
James Hogan84260972016-07-04 19:35:15 +0100627#else
628 /* r6+, simulate a generic QEMU machine */
629 kvm_write_c0_guest_prid(cop0, 0x00010000);
630#endif
James Hogane3429252016-06-15 19:30:00 +0100631 /*
632 * Have config1, Cacheable, noncoherent, write-back, write allocate.
633 * Endianness, arch revision & virtually tagged icache should match
634 * host.
635 */
636 config = read_c0_config() & MIPS_CONF_AR;
James Hogan4e10b762016-06-15 19:30:01 +0100637 config |= MIPS_CONF_M | CONF_CM_CACHABLE_NONCOHERENT | MIPS_CONF_MT_TLB;
James Hogane3429252016-06-15 19:30:00 +0100638#ifdef CONFIG_CPU_BIG_ENDIAN
639 config |= CONF_BE;
640#endif
641 if (cpu_has_vtag_icache)
642 config |= MIPS_CONF_VI;
643 kvm_write_c0_guest_config(cop0, config);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800644
645 /* Read the cache characteristics from the host Config1 Register */
646 config1 = (read_c0_config1() & ~0x7f);
647
James Hogan867f4da2017-03-14 10:25:47 +0000648 /* DCache line size not correctly reported in Config1 on Octeon CPUs */
649 if (cpu_dcache_line_size()) {
650 config1 &= ~MIPS_CONF1_DL;
651 config1 |= ((ilog2(cpu_dcache_line_size()) - 1) <<
652 MIPS_CONF1_DL_SHF) & MIPS_CONF1_DL;
653 }
654
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800655 /* Set up MMU size */
656 config1 &= ~(0x3f << 25);
657 config1 |= ((KVM_MIPS_GUEST_TLB_SIZE - 1) << 25);
658
659 /* We unset some bits that we aren't emulating */
James Hogan4e10b762016-06-15 19:30:01 +0100660 config1 &= ~(MIPS_CONF1_C2 | MIPS_CONF1_MD | MIPS_CONF1_PC |
661 MIPS_CONF1_WR | MIPS_CONF1_CA);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800662 kvm_write_c0_guest_config1(cop0, config1);
663
James Hogan2211ee82015-03-04 15:56:47 +0000664 /* Have config3, no tertiary/secondary caches implemented */
665 kvm_write_c0_guest_config2(cop0, MIPS_CONF_M);
666 /* MIPS_CONF_M | (read_c0_config2() & 0xfff) */
667
James Hoganc7716072014-06-26 15:11:29 +0100668 /* Have config4, UserLocal */
669 kvm_write_c0_guest_config3(cop0, MIPS_CONF_M | MIPS_CONF3_ULRI);
670
671 /* Have config5 */
672 kvm_write_c0_guest_config4(cop0, MIPS_CONF_M);
673
674 /* No config6 */
675 kvm_write_c0_guest_config5(cop0, 0);
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800676
677 /* Set Wait IE/IXMT Ignore in Config7, IAR, AR */
678 kvm_write_c0_guest_config7(cop0, (MIPS_CONF7_WII) | (1 << 10));
679
James Hoganbe67a0b2017-01-18 16:20:31 +0000680 /* Status */
681 kvm_write_c0_guest_status(cop0, ST0_BEV | ST0_ERL);
682
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700683 /*
Adam Buchbinder92a76f62016-02-25 00:44:58 -0800684 * Setup IntCtl defaults, compatibility mode for timer interrupts (HW5)
Deng-Cheng Zhud116e812014-06-26 12:11:34 -0700685 */
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800686 kvm_write_c0_guest_intctl(cop0, 0xFC000000);
687
688 /* Put in vcpu id as CPUNum into Ebase Reg to handle SMP Guests */
James Hogan37af2f32016-05-11 13:50:49 +0100689 kvm_write_c0_guest_ebase(cop0, KVM_GUEST_KSEG0 |
690 (vcpu_id & MIPS_EBASE_CPUNUM));
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800691
James Hoganbe67a0b2017-01-18 16:20:31 +0000692 /* Put PC at guest reset vector */
693 vcpu->arch.pc = KVM_GUEST_CKSEG1ADDR(0x1fc00000);
694
Sanjay Lalf5c236d2012-11-21 18:34:09 -0800695 return 0;
696}
697
James Hoganb6209112016-10-25 00:01:37 +0100698static void kvm_trap_emul_flush_shadow_all(struct kvm *kvm)
699{
700 /* Flush GVA page tables and invalidate GVA ASIDs on all VCPUs */
701 kvm_flush_remote_tlbs(kvm);
702}
703
704static void kvm_trap_emul_flush_shadow_memslot(struct kvm *kvm,
705 const struct kvm_memory_slot *slot)
706{
707 kvm_trap_emul_flush_shadow_all(kvm);
708}
709
James Hogan654229a2016-12-08 22:46:41 +0000710static u64 kvm_trap_emul_get_one_regs[] = {
711 KVM_REG_MIPS_CP0_INDEX,
James Hogan013044c2016-12-07 17:16:37 +0000712 KVM_REG_MIPS_CP0_ENTRYLO0,
713 KVM_REG_MIPS_CP0_ENTRYLO1,
James Hogan654229a2016-12-08 22:46:41 +0000714 KVM_REG_MIPS_CP0_CONTEXT,
715 KVM_REG_MIPS_CP0_USERLOCAL,
716 KVM_REG_MIPS_CP0_PAGEMASK,
717 KVM_REG_MIPS_CP0_WIRED,
718 KVM_REG_MIPS_CP0_HWRENA,
719 KVM_REG_MIPS_CP0_BADVADDR,
720 KVM_REG_MIPS_CP0_COUNT,
721 KVM_REG_MIPS_CP0_ENTRYHI,
722 KVM_REG_MIPS_CP0_COMPARE,
723 KVM_REG_MIPS_CP0_STATUS,
James Hoganad58d4d2015-02-02 22:55:17 +0000724 KVM_REG_MIPS_CP0_INTCTL,
James Hogan654229a2016-12-08 22:46:41 +0000725 KVM_REG_MIPS_CP0_CAUSE,
726 KVM_REG_MIPS_CP0_EPC,
727 KVM_REG_MIPS_CP0_PRID,
James Hogan7801bbe2016-11-14 23:59:27 +0000728 KVM_REG_MIPS_CP0_EBASE,
James Hogan654229a2016-12-08 22:46:41 +0000729 KVM_REG_MIPS_CP0_CONFIG,
730 KVM_REG_MIPS_CP0_CONFIG1,
731 KVM_REG_MIPS_CP0_CONFIG2,
732 KVM_REG_MIPS_CP0_CONFIG3,
733 KVM_REG_MIPS_CP0_CONFIG4,
734 KVM_REG_MIPS_CP0_CONFIG5,
735 KVM_REG_MIPS_CP0_CONFIG7,
736 KVM_REG_MIPS_CP0_ERROREPC,
737 KVM_REG_MIPS_CP0_KSCRATCH1,
738 KVM_REG_MIPS_CP0_KSCRATCH2,
739 KVM_REG_MIPS_CP0_KSCRATCH3,
740 KVM_REG_MIPS_CP0_KSCRATCH4,
741 KVM_REG_MIPS_CP0_KSCRATCH5,
742 KVM_REG_MIPS_CP0_KSCRATCH6,
743
744 KVM_REG_MIPS_COUNT_CTL,
745 KVM_REG_MIPS_COUNT_RESUME,
746 KVM_REG_MIPS_COUNT_HZ,
747};
748
James Hoganf5c43bd2016-06-15 19:29:49 +0100749static unsigned long kvm_trap_emul_num_regs(struct kvm_vcpu *vcpu)
750{
James Hogan654229a2016-12-08 22:46:41 +0000751 return ARRAY_SIZE(kvm_trap_emul_get_one_regs);
James Hoganf5c43bd2016-06-15 19:29:49 +0100752}
753
754static int kvm_trap_emul_copy_reg_indices(struct kvm_vcpu *vcpu,
755 u64 __user *indices)
756{
James Hogan654229a2016-12-08 22:46:41 +0000757 if (copy_to_user(indices, kvm_trap_emul_get_one_regs,
758 sizeof(kvm_trap_emul_get_one_regs)))
759 return -EFAULT;
760 indices += ARRAY_SIZE(kvm_trap_emul_get_one_regs);
761
James Hoganf5c43bd2016-06-15 19:29:49 +0100762 return 0;
763}
764
James Hoganf8be02d2014-05-29 10:16:29 +0100765static int kvm_trap_emul_get_one_reg(struct kvm_vcpu *vcpu,
766 const struct kvm_one_reg *reg,
767 s64 *v)
768{
James Hogan654229a2016-12-08 22:46:41 +0000769 struct mips_coproc *cop0 = vcpu->arch.cop0;
770
James Hoganf8be02d2014-05-29 10:16:29 +0100771 switch (reg->id) {
James Hogan654229a2016-12-08 22:46:41 +0000772 case KVM_REG_MIPS_CP0_INDEX:
773 *v = (long)kvm_read_c0_guest_index(cop0);
774 break;
James Hogan013044c2016-12-07 17:16:37 +0000775 case KVM_REG_MIPS_CP0_ENTRYLO0:
776 *v = kvm_read_c0_guest_entrylo0(cop0);
777 break;
778 case KVM_REG_MIPS_CP0_ENTRYLO1:
779 *v = kvm_read_c0_guest_entrylo1(cop0);
780 break;
James Hogan654229a2016-12-08 22:46:41 +0000781 case KVM_REG_MIPS_CP0_CONTEXT:
782 *v = (long)kvm_read_c0_guest_context(cop0);
783 break;
784 case KVM_REG_MIPS_CP0_USERLOCAL:
785 *v = (long)kvm_read_c0_guest_userlocal(cop0);
786 break;
787 case KVM_REG_MIPS_CP0_PAGEMASK:
788 *v = (long)kvm_read_c0_guest_pagemask(cop0);
789 break;
790 case KVM_REG_MIPS_CP0_WIRED:
791 *v = (long)kvm_read_c0_guest_wired(cop0);
792 break;
793 case KVM_REG_MIPS_CP0_HWRENA:
794 *v = (long)kvm_read_c0_guest_hwrena(cop0);
795 break;
796 case KVM_REG_MIPS_CP0_BADVADDR:
797 *v = (long)kvm_read_c0_guest_badvaddr(cop0);
798 break;
799 case KVM_REG_MIPS_CP0_ENTRYHI:
800 *v = (long)kvm_read_c0_guest_entryhi(cop0);
801 break;
802 case KVM_REG_MIPS_CP0_COMPARE:
803 *v = (long)kvm_read_c0_guest_compare(cop0);
804 break;
805 case KVM_REG_MIPS_CP0_STATUS:
806 *v = (long)kvm_read_c0_guest_status(cop0);
807 break;
James Hoganad58d4d2015-02-02 22:55:17 +0000808 case KVM_REG_MIPS_CP0_INTCTL:
809 *v = (long)kvm_read_c0_guest_intctl(cop0);
810 break;
James Hogan654229a2016-12-08 22:46:41 +0000811 case KVM_REG_MIPS_CP0_CAUSE:
812 *v = (long)kvm_read_c0_guest_cause(cop0);
813 break;
814 case KVM_REG_MIPS_CP0_EPC:
815 *v = (long)kvm_read_c0_guest_epc(cop0);
816 break;
817 case KVM_REG_MIPS_CP0_PRID:
818 *v = (long)kvm_read_c0_guest_prid(cop0);
819 break;
James Hogan7801bbe2016-11-14 23:59:27 +0000820 case KVM_REG_MIPS_CP0_EBASE:
821 *v = (long)kvm_read_c0_guest_ebase(cop0);
822 break;
James Hogan654229a2016-12-08 22:46:41 +0000823 case KVM_REG_MIPS_CP0_CONFIG:
824 *v = (long)kvm_read_c0_guest_config(cop0);
825 break;
826 case KVM_REG_MIPS_CP0_CONFIG1:
827 *v = (long)kvm_read_c0_guest_config1(cop0);
828 break;
829 case KVM_REG_MIPS_CP0_CONFIG2:
830 *v = (long)kvm_read_c0_guest_config2(cop0);
831 break;
832 case KVM_REG_MIPS_CP0_CONFIG3:
833 *v = (long)kvm_read_c0_guest_config3(cop0);
834 break;
835 case KVM_REG_MIPS_CP0_CONFIG4:
836 *v = (long)kvm_read_c0_guest_config4(cop0);
837 break;
838 case KVM_REG_MIPS_CP0_CONFIG5:
839 *v = (long)kvm_read_c0_guest_config5(cop0);
840 break;
841 case KVM_REG_MIPS_CP0_CONFIG7:
842 *v = (long)kvm_read_c0_guest_config7(cop0);
843 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100844 case KVM_REG_MIPS_CP0_COUNT:
James Hogane30492b2014-05-29 10:16:35 +0100845 *v = kvm_mips_read_count(vcpu);
James Hoganf8be02d2014-05-29 10:16:29 +0100846 break;
James Hoganf8239342014-05-29 10:16:37 +0100847 case KVM_REG_MIPS_COUNT_CTL:
848 *v = vcpu->arch.count_ctl;
849 break;
850 case KVM_REG_MIPS_COUNT_RESUME:
851 *v = ktime_to_ns(vcpu->arch.count_resume);
852 break;
James Hoganf74a8e22014-05-29 10:16:38 +0100853 case KVM_REG_MIPS_COUNT_HZ:
854 *v = vcpu->arch.count_hz;
855 break;
James Hogan654229a2016-12-08 22:46:41 +0000856 case KVM_REG_MIPS_CP0_ERROREPC:
857 *v = (long)kvm_read_c0_guest_errorepc(cop0);
858 break;
859 case KVM_REG_MIPS_CP0_KSCRATCH1:
860 *v = (long)kvm_read_c0_guest_kscratch1(cop0);
861 break;
862 case KVM_REG_MIPS_CP0_KSCRATCH2:
863 *v = (long)kvm_read_c0_guest_kscratch2(cop0);
864 break;
865 case KVM_REG_MIPS_CP0_KSCRATCH3:
866 *v = (long)kvm_read_c0_guest_kscratch3(cop0);
867 break;
868 case KVM_REG_MIPS_CP0_KSCRATCH4:
869 *v = (long)kvm_read_c0_guest_kscratch4(cop0);
870 break;
871 case KVM_REG_MIPS_CP0_KSCRATCH5:
872 *v = (long)kvm_read_c0_guest_kscratch5(cop0);
873 break;
874 case KVM_REG_MIPS_CP0_KSCRATCH6:
875 *v = (long)kvm_read_c0_guest_kscratch6(cop0);
876 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100877 default:
878 return -EINVAL;
879 }
880 return 0;
881}
882
883static int kvm_trap_emul_set_one_reg(struct kvm_vcpu *vcpu,
884 const struct kvm_one_reg *reg,
885 s64 v)
886{
887 struct mips_coproc *cop0 = vcpu->arch.cop0;
James Hoganf8239342014-05-29 10:16:37 +0100888 int ret = 0;
James Hoganc7716072014-06-26 15:11:29 +0100889 unsigned int cur, change;
James Hoganf8be02d2014-05-29 10:16:29 +0100890
891 switch (reg->id) {
James Hogan654229a2016-12-08 22:46:41 +0000892 case KVM_REG_MIPS_CP0_INDEX:
893 kvm_write_c0_guest_index(cop0, v);
894 break;
James Hogan013044c2016-12-07 17:16:37 +0000895 case KVM_REG_MIPS_CP0_ENTRYLO0:
896 kvm_write_c0_guest_entrylo0(cop0, v);
897 break;
898 case KVM_REG_MIPS_CP0_ENTRYLO1:
899 kvm_write_c0_guest_entrylo1(cop0, v);
900 break;
James Hogan654229a2016-12-08 22:46:41 +0000901 case KVM_REG_MIPS_CP0_CONTEXT:
902 kvm_write_c0_guest_context(cop0, v);
903 break;
904 case KVM_REG_MIPS_CP0_USERLOCAL:
905 kvm_write_c0_guest_userlocal(cop0, v);
906 break;
907 case KVM_REG_MIPS_CP0_PAGEMASK:
908 kvm_write_c0_guest_pagemask(cop0, v);
909 break;
910 case KVM_REG_MIPS_CP0_WIRED:
911 kvm_write_c0_guest_wired(cop0, v);
912 break;
913 case KVM_REG_MIPS_CP0_HWRENA:
914 kvm_write_c0_guest_hwrena(cop0, v);
915 break;
916 case KVM_REG_MIPS_CP0_BADVADDR:
917 kvm_write_c0_guest_badvaddr(cop0, v);
918 break;
919 case KVM_REG_MIPS_CP0_ENTRYHI:
920 kvm_write_c0_guest_entryhi(cop0, v);
921 break;
922 case KVM_REG_MIPS_CP0_STATUS:
923 kvm_write_c0_guest_status(cop0, v);
924 break;
James Hoganad58d4d2015-02-02 22:55:17 +0000925 case KVM_REG_MIPS_CP0_INTCTL:
926 /* No VInt, so no VS, read-only for now */
927 break;
James Hogan654229a2016-12-08 22:46:41 +0000928 case KVM_REG_MIPS_CP0_EPC:
929 kvm_write_c0_guest_epc(cop0, v);
930 break;
931 case KVM_REG_MIPS_CP0_PRID:
932 kvm_write_c0_guest_prid(cop0, v);
933 break;
James Hogan7801bbe2016-11-14 23:59:27 +0000934 case KVM_REG_MIPS_CP0_EBASE:
935 /*
936 * Allow core number to be written, but the exception base must
937 * remain in guest KSeg0.
938 */
939 kvm_change_c0_guest_ebase(cop0, 0x1ffff000 | MIPS_EBASE_CPUNUM,
940 v);
941 break;
James Hoganf8be02d2014-05-29 10:16:29 +0100942 case KVM_REG_MIPS_CP0_COUNT:
James Hogane30492b2014-05-29 10:16:35 +0100943 kvm_mips_write_count(vcpu, v);
James Hoganf8be02d2014-05-29 10:16:29 +0100944 break;
945 case KVM_REG_MIPS_CP0_COMPARE:
James Hoganb45bacd2016-04-22 10:38:46 +0100946 kvm_mips_write_compare(vcpu, v, false);
James Hogane30492b2014-05-29 10:16:35 +0100947 break;
948 case KVM_REG_MIPS_CP0_CAUSE:
949 /*
950 * If the timer is stopped or started (DC bit) it must look
951 * atomic with changes to the interrupt pending bits (TI, IRQ5).
952 * A timer interrupt should not happen in between.
953 */
954 if ((kvm_read_c0_guest_cause(cop0) ^ v) & CAUSEF_DC) {
955 if (v & CAUSEF_DC) {
956 /* disable timer first */
957 kvm_mips_count_disable_cause(vcpu);
James Hogana27660f2017-03-14 10:15:25 +0000958 kvm_change_c0_guest_cause(cop0, (u32)~CAUSEF_DC,
959 v);
James Hogane30492b2014-05-29 10:16:35 +0100960 } else {
961 /* enable timer last */
James Hogana27660f2017-03-14 10:15:25 +0000962 kvm_change_c0_guest_cause(cop0, (u32)~CAUSEF_DC,
963 v);
James Hogane30492b2014-05-29 10:16:35 +0100964 kvm_mips_count_enable_cause(vcpu);
965 }
966 } else {
967 kvm_write_c0_guest_cause(cop0, v);
968 }
James Hoganf8be02d2014-05-29 10:16:29 +0100969 break;
James Hoganc7716072014-06-26 15:11:29 +0100970 case KVM_REG_MIPS_CP0_CONFIG:
971 /* read-only for now */
972 break;
973 case KVM_REG_MIPS_CP0_CONFIG1:
974 cur = kvm_read_c0_guest_config1(cop0);
975 change = (cur ^ v) & kvm_mips_config1_wrmask(vcpu);
976 if (change) {
977 v = cur ^ change;
978 kvm_write_c0_guest_config1(cop0, v);
979 }
980 break;
981 case KVM_REG_MIPS_CP0_CONFIG2:
982 /* read-only for now */
983 break;
984 case KVM_REG_MIPS_CP0_CONFIG3:
985 cur = kvm_read_c0_guest_config3(cop0);
986 change = (cur ^ v) & kvm_mips_config3_wrmask(vcpu);
987 if (change) {
988 v = cur ^ change;
989 kvm_write_c0_guest_config3(cop0, v);
990 }
991 break;
992 case KVM_REG_MIPS_CP0_CONFIG4:
993 cur = kvm_read_c0_guest_config4(cop0);
994 change = (cur ^ v) & kvm_mips_config4_wrmask(vcpu);
995 if (change) {
996 v = cur ^ change;
997 kvm_write_c0_guest_config4(cop0, v);
998 }
999 break;
1000 case KVM_REG_MIPS_CP0_CONFIG5:
1001 cur = kvm_read_c0_guest_config5(cop0);
1002 change = (cur ^ v) & kvm_mips_config5_wrmask(vcpu);
1003 if (change) {
1004 v = cur ^ change;
1005 kvm_write_c0_guest_config5(cop0, v);
1006 }
1007 break;
James Hogan89d6ad82016-12-14 01:58:44 +00001008 case KVM_REG_MIPS_CP0_CONFIG7:
1009 /* writes ignored */
1010 break;
James Hoganf8239342014-05-29 10:16:37 +01001011 case KVM_REG_MIPS_COUNT_CTL:
1012 ret = kvm_mips_set_count_ctl(vcpu, v);
1013 break;
1014 case KVM_REG_MIPS_COUNT_RESUME:
1015 ret = kvm_mips_set_count_resume(vcpu, v);
1016 break;
James Hoganf74a8e22014-05-29 10:16:38 +01001017 case KVM_REG_MIPS_COUNT_HZ:
1018 ret = kvm_mips_set_count_hz(vcpu, v);
1019 break;
James Hogan654229a2016-12-08 22:46:41 +00001020 case KVM_REG_MIPS_CP0_ERROREPC:
1021 kvm_write_c0_guest_errorepc(cop0, v);
1022 break;
1023 case KVM_REG_MIPS_CP0_KSCRATCH1:
1024 kvm_write_c0_guest_kscratch1(cop0, v);
1025 break;
1026 case KVM_REG_MIPS_CP0_KSCRATCH2:
1027 kvm_write_c0_guest_kscratch2(cop0, v);
1028 break;
1029 case KVM_REG_MIPS_CP0_KSCRATCH3:
1030 kvm_write_c0_guest_kscratch3(cop0, v);
1031 break;
1032 case KVM_REG_MIPS_CP0_KSCRATCH4:
1033 kvm_write_c0_guest_kscratch4(cop0, v);
1034 break;
1035 case KVM_REG_MIPS_CP0_KSCRATCH5:
1036 kvm_write_c0_guest_kscratch5(cop0, v);
1037 break;
1038 case KVM_REG_MIPS_CP0_KSCRATCH6:
1039 kvm_write_c0_guest_kscratch6(cop0, v);
1040 break;
James Hoganf8be02d2014-05-29 10:16:29 +01001041 default:
1042 return -EINVAL;
1043 }
James Hoganf8239342014-05-29 10:16:37 +01001044 return ret;
James Hoganf8be02d2014-05-29 10:16:29 +01001045}
1046
James Hogana60b8432016-11-12 00:00:13 +00001047static int kvm_trap_emul_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
James Hoganb86ecb32015-02-09 16:35:20 +00001048{
James Hoganc550d532016-10-11 23:14:39 +01001049 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
1050 struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
James Hogan7faa6ee2016-10-07 23:58:53 +01001051 struct mm_struct *mm;
James Hogan1581ff32016-11-16 23:48:56 +00001052
James Hogan1581ff32016-11-16 23:48:56 +00001053 /*
James Hogan91737ea2016-12-02 23:40:52 +00001054 * Were we in guest context? If so, restore the appropriate ASID based
1055 * on the mode of the Guest (Kernel/User).
James Hogan1581ff32016-11-16 23:48:56 +00001056 */
1057 if (current->flags & PF_VCPU) {
James Hogan7faa6ee2016-10-07 23:58:53 +01001058 mm = KVM_GUEST_KERNEL_MODE(vcpu) ? kern_mm : user_mm;
James Hogan91737ea2016-12-02 23:40:52 +00001059 if ((cpu_context(cpu, mm) ^ asid_cache(cpu)) &
1060 asid_version_mask(cpu))
1061 get_new_mmu_context(mm, cpu);
James Hogan7faa6ee2016-10-07 23:58:53 +01001062 write_c0_entryhi(cpu_asid(cpu, mm));
1063 TLBMISS_HANDLER_SETUP_PGD(mm->pgd);
James Hogana7ebb2e2016-11-15 00:06:05 +00001064 kvm_mips_suspend_mm(cpu);
James Hogan1581ff32016-11-16 23:48:56 +00001065 ehb();
1066 }
1067
James Hoganb86ecb32015-02-09 16:35:20 +00001068 return 0;
1069}
1070
James Hogana60b8432016-11-12 00:00:13 +00001071static int kvm_trap_emul_vcpu_put(struct kvm_vcpu *vcpu, int cpu)
James Hoganb86ecb32015-02-09 16:35:20 +00001072{
James Hogana60b8432016-11-12 00:00:13 +00001073 kvm_lose_fpu(vcpu);
1074
James Hogan91cdee52016-11-18 13:25:24 +00001075 if (current->flags & PF_VCPU) {
1076 /* Restore normal Linux process memory map */
1077 if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) &
James Hogan91737ea2016-12-02 23:40:52 +00001078 asid_version_mask(cpu)))
James Hogan91cdee52016-11-18 13:25:24 +00001079 get_new_mmu_context(current->mm, cpu);
James Hogan91cdee52016-11-18 13:25:24 +00001080 write_c0_entryhi(cpu_asid(cpu, current->mm));
James Hogan7faa6ee2016-10-07 23:58:53 +01001081 TLBMISS_HANDLER_SETUP_PGD(current->mm->pgd);
James Hogana7ebb2e2016-11-15 00:06:05 +00001082 kvm_mips_resume_mm(cpu);
James Hogan91cdee52016-11-18 13:25:24 +00001083 ehb();
James Hogan1581ff32016-11-16 23:48:56 +00001084 }
James Hogan1581ff32016-11-16 23:48:56 +00001085
James Hoganb86ecb32015-02-09 16:35:20 +00001086 return 0;
1087}
1088
James Hoganb29e1152016-11-28 23:19:32 +00001089static void kvm_trap_emul_check_requests(struct kvm_vcpu *vcpu, int cpu,
1090 bool reload_asid)
1091{
1092 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
1093 struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
1094 struct mm_struct *mm;
1095 int i;
1096
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +02001097 if (likely(!kvm_request_pending(vcpu)))
James Hoganb29e1152016-11-28 23:19:32 +00001098 return;
1099
1100 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
1101 /*
1102 * Both kernel & user GVA mappings must be invalidated. The
1103 * caller is just about to check whether the ASID is stale
1104 * anyway so no need to reload it here.
1105 */
1106 kvm_mips_flush_gva_pt(kern_mm->pgd, KMF_GPA | KMF_KERN);
1107 kvm_mips_flush_gva_pt(user_mm->pgd, KMF_GPA | KMF_USER);
1108 for_each_possible_cpu(i) {
1109 cpu_context(i, kern_mm) = 0;
1110 cpu_context(i, user_mm) = 0;
1111 }
1112
1113 /* Generate new ASID for current mode */
1114 if (reload_asid) {
1115 mm = KVM_GUEST_KERNEL_MODE(vcpu) ? kern_mm : user_mm;
1116 get_new_mmu_context(mm, cpu);
1117 htw_stop();
1118 write_c0_entryhi(cpu_asid(cpu, mm));
1119 TLBMISS_HANDLER_SETUP_PGD(mm->pgd);
1120 htw_start();
1121 }
1122 }
1123}
1124
James Hogan1880afd2016-11-28 23:04:52 +00001125/**
1126 * kvm_trap_emul_gva_lockless_begin() - Begin lockless access to GVA space.
1127 * @vcpu: VCPU pointer.
1128 *
1129 * Call before a GVA space access outside of guest mode, to ensure that
1130 * asynchronous TLB flush requests are handled or delayed until completion of
1131 * the GVA access (as indicated by a matching kvm_trap_emul_gva_lockless_end()).
1132 *
1133 * Should be called with IRQs already enabled.
1134 */
1135void kvm_trap_emul_gva_lockless_begin(struct kvm_vcpu *vcpu)
1136{
1137 /* We re-enable IRQs in kvm_trap_emul_gva_lockless_end() */
1138 WARN_ON_ONCE(irqs_disabled());
1139
1140 /*
1141 * The caller is about to access the GVA space, so we set the mode to
1142 * force TLB flush requests to send an IPI, and also disable IRQs to
1143 * delay IPI handling until kvm_trap_emul_gva_lockless_end().
1144 */
1145 local_irq_disable();
1146
1147 /*
1148 * Make sure the read of VCPU requests is not reordered ahead of the
1149 * write to vcpu->mode, or we could miss a TLB flush request while
1150 * the requester sees the VCPU as outside of guest mode and not needing
1151 * an IPI.
1152 */
1153 smp_store_mb(vcpu->mode, READING_SHADOW_PAGE_TABLES);
1154
1155 /*
1156 * If a TLB flush has been requested (potentially while
1157 * OUTSIDE_GUEST_MODE and assumed immediately effective), perform it
1158 * before accessing the GVA space, and be sure to reload the ASID if
1159 * necessary as it'll be immediately used.
1160 *
1161 * TLB flush requests after this check will trigger an IPI due to the
1162 * mode change above, which will be delayed due to IRQs disabled.
1163 */
1164 kvm_trap_emul_check_requests(vcpu, smp_processor_id(), true);
1165}
1166
1167/**
1168 * kvm_trap_emul_gva_lockless_end() - End lockless access to GVA space.
1169 * @vcpu: VCPU pointer.
1170 *
1171 * Called after a GVA space access outside of guest mode. Should have a matching
1172 * call to kvm_trap_emul_gva_lockless_begin().
1173 */
1174void kvm_trap_emul_gva_lockless_end(struct kvm_vcpu *vcpu)
1175{
1176 /*
1177 * Make sure the write to vcpu->mode is not reordered in front of GVA
1178 * accesses, or a TLB flush requester may not think it necessary to send
1179 * an IPI.
1180 */
1181 smp_store_release(&vcpu->mode, OUTSIDE_GUEST_MODE);
1182
1183 /*
1184 * Now that the access to GVA space is complete, its safe for pending
1185 * TLB flush request IPIs to be handled (which indicates completion).
1186 */
1187 local_irq_enable();
1188}
1189
James Hogana2c046e2016-11-18 13:14:37 +00001190static void kvm_trap_emul_vcpu_reenter(struct kvm_run *run,
1191 struct kvm_vcpu *vcpu)
1192{
James Hoganb29e1152016-11-28 23:19:32 +00001193 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
James Hogana2c046e2016-11-18 13:14:37 +00001194 struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
James Hoganb29e1152016-11-28 23:19:32 +00001195 struct mm_struct *mm;
James Hogana2c046e2016-11-18 13:14:37 +00001196 struct mips_coproc *cop0 = vcpu->arch.cop0;
1197 int i, cpu = smp_processor_id();
1198 unsigned int gasid;
1199
1200 /*
James Hoganb29e1152016-11-28 23:19:32 +00001201 * No need to reload ASID, IRQs are disabled already so there's no rush,
1202 * and we'll check if we need to regenerate below anyway before
1203 * re-entering the guest.
James Hogana2c046e2016-11-18 13:14:37 +00001204 */
James Hoganb29e1152016-11-28 23:19:32 +00001205 kvm_trap_emul_check_requests(vcpu, cpu, false);
1206
1207 if (KVM_GUEST_KERNEL_MODE(vcpu)) {
1208 mm = kern_mm;
1209 } else {
1210 mm = user_mm;
1211
1212 /*
1213 * Lazy host ASID regeneration / PT flush for guest user mode.
1214 * If the guest ASID has changed since the last guest usermode
1215 * execution, invalidate the stale TLB entries and flush GVA PT
1216 * entries too.
1217 */
James Hogana2c046e2016-11-18 13:14:37 +00001218 gasid = kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID;
1219 if (gasid != vcpu->arch.last_user_gasid) {
James Hogana31b50d2016-12-16 15:57:00 +00001220 kvm_mips_flush_gva_pt(user_mm->pgd, KMF_USER);
James Hogana2c046e2016-11-18 13:14:37 +00001221 for_each_possible_cpu(i)
James Hoganb29e1152016-11-28 23:19:32 +00001222 cpu_context(i, user_mm) = 0;
James Hogana2c046e2016-11-18 13:14:37 +00001223 vcpu->arch.last_user_gasid = gasid;
1224 }
1225 }
James Hoganb29e1152016-11-28 23:19:32 +00001226
1227 /*
1228 * Check if ASID is stale. This may happen due to a TLB flush request or
1229 * a lazy user MM invalidation.
1230 */
1231 if ((cpu_context(cpu, mm) ^ asid_cache(cpu)) &
1232 asid_version_mask(cpu))
1233 get_new_mmu_context(mm, cpu);
James Hogana2c046e2016-11-18 13:14:37 +00001234}
1235
1236static int kvm_trap_emul_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
1237{
James Hogana7ebb2e2016-11-15 00:06:05 +00001238 int cpu = smp_processor_id();
James Hogana2c046e2016-11-18 13:14:37 +00001239 int r;
1240
1241 /* Check if we have any exceptions/interrupts pending */
1242 kvm_mips_deliver_interrupts(vcpu,
1243 kvm_read_c0_guest_cause(vcpu->arch.cop0));
1244
1245 kvm_trap_emul_vcpu_reenter(run, vcpu);
1246
James Hogandacc3ed2016-08-19 15:27:22 +01001247 /*
1248 * We use user accessors to access guest memory, but we don't want to
1249 * invoke Linux page faulting.
1250 */
1251 pagefault_disable();
1252
James Hogana2c046e2016-11-18 13:14:37 +00001253 /* Disable hardware page table walking while in guest */
1254 htw_stop();
1255
James Hogana7ebb2e2016-11-15 00:06:05 +00001256 /*
1257 * While in guest context we're in the guest's address space, not the
1258 * host process address space, so we need to be careful not to confuse
1259 * e.g. cache management IPIs.
1260 */
1261 kvm_mips_suspend_mm(cpu);
1262
James Hogana2c046e2016-11-18 13:14:37 +00001263 r = vcpu->arch.vcpu_run(run, vcpu);
1264
James Hogan91cdee52016-11-18 13:25:24 +00001265 /* We may have migrated while handling guest exits */
1266 cpu = smp_processor_id();
1267
1268 /* Restore normal Linux process memory map */
1269 if (((cpu_context(cpu, current->mm) ^ asid_cache(cpu)) &
1270 asid_version_mask(cpu)))
1271 get_new_mmu_context(current->mm, cpu);
1272 write_c0_entryhi(cpu_asid(cpu, current->mm));
James Hogan7faa6ee2016-10-07 23:58:53 +01001273 TLBMISS_HANDLER_SETUP_PGD(current->mm->pgd);
James Hogana7ebb2e2016-11-15 00:06:05 +00001274 kvm_mips_resume_mm(cpu);
James Hogan91cdee52016-11-18 13:25:24 +00001275
James Hogana2c046e2016-11-18 13:14:37 +00001276 htw_start();
1277
James Hogandacc3ed2016-08-19 15:27:22 +01001278 pagefault_enable();
1279
James Hogana2c046e2016-11-18 13:14:37 +00001280 return r;
1281}
1282
Sanjay Lalf5c236d2012-11-21 18:34:09 -08001283static struct kvm_mips_callbacks kvm_trap_emul_callbacks = {
1284 /* exit handlers */
1285 .handle_cop_unusable = kvm_trap_emul_handle_cop_unusable,
1286 .handle_tlb_mod = kvm_trap_emul_handle_tlb_mod,
1287 .handle_tlb_st_miss = kvm_trap_emul_handle_tlb_st_miss,
1288 .handle_tlb_ld_miss = kvm_trap_emul_handle_tlb_ld_miss,
1289 .handle_addr_err_st = kvm_trap_emul_handle_addr_err_st,
1290 .handle_addr_err_ld = kvm_trap_emul_handle_addr_err_ld,
1291 .handle_syscall = kvm_trap_emul_handle_syscall,
1292 .handle_res_inst = kvm_trap_emul_handle_res_inst,
1293 .handle_break = kvm_trap_emul_handle_break,
James Hogan0a560422015-02-06 16:03:57 +00001294 .handle_trap = kvm_trap_emul_handle_trap,
James Hoganc2537ed2015-02-06 10:56:27 +00001295 .handle_msa_fpe = kvm_trap_emul_handle_msa_fpe,
James Hogan1c0cd662015-02-06 10:56:27 +00001296 .handle_fpe = kvm_trap_emul_handle_fpe,
James Hogan98119ad2015-02-06 11:11:56 +00001297 .handle_msa_disabled = kvm_trap_emul_handle_msa_disabled,
James Hogan28c1e762017-03-14 10:15:24 +00001298 .handle_guest_exit = kvm_trap_emul_no_handler,
Sanjay Lalf5c236d2012-11-21 18:34:09 -08001299
James Hoganedab4fe2017-03-14 10:15:23 +00001300 .hardware_enable = kvm_trap_emul_hardware_enable,
1301 .hardware_disable = kvm_trap_emul_hardware_disable,
James Hogan607ef2f2017-03-14 10:15:22 +00001302 .check_extension = kvm_trap_emul_check_extension,
Sanjay Lalf5c236d2012-11-21 18:34:09 -08001303 .vcpu_init = kvm_trap_emul_vcpu_init,
James Hogan630766b2016-09-08 23:00:24 +01001304 .vcpu_uninit = kvm_trap_emul_vcpu_uninit,
Sanjay Lalf5c236d2012-11-21 18:34:09 -08001305 .vcpu_setup = kvm_trap_emul_vcpu_setup,
James Hoganb6209112016-10-25 00:01:37 +01001306 .flush_shadow_all = kvm_trap_emul_flush_shadow_all,
1307 .flush_shadow_memslot = kvm_trap_emul_flush_shadow_memslot,
Sanjay Lalf5c236d2012-11-21 18:34:09 -08001308 .gva_to_gpa = kvm_trap_emul_gva_to_gpa_cb,
1309 .queue_timer_int = kvm_mips_queue_timer_int_cb,
1310 .dequeue_timer_int = kvm_mips_dequeue_timer_int_cb,
1311 .queue_io_int = kvm_mips_queue_io_int_cb,
1312 .dequeue_io_int = kvm_mips_dequeue_io_int_cb,
1313 .irq_deliver = kvm_mips_irq_deliver_cb,
1314 .irq_clear = kvm_mips_irq_clear_cb,
James Hoganf5c43bd2016-06-15 19:29:49 +01001315 .num_regs = kvm_trap_emul_num_regs,
1316 .copy_reg_indices = kvm_trap_emul_copy_reg_indices,
James Hoganf8be02d2014-05-29 10:16:29 +01001317 .get_one_reg = kvm_trap_emul_get_one_reg,
1318 .set_one_reg = kvm_trap_emul_set_one_reg,
James Hogana60b8432016-11-12 00:00:13 +00001319 .vcpu_load = kvm_trap_emul_vcpu_load,
1320 .vcpu_put = kvm_trap_emul_vcpu_put,
James Hogana2c046e2016-11-18 13:14:37 +00001321 .vcpu_run = kvm_trap_emul_vcpu_run,
1322 .vcpu_reenter = kvm_trap_emul_vcpu_reenter,
Sanjay Lalf5c236d2012-11-21 18:34:09 -08001323};
1324
1325int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks)
1326{
1327 *install_callbacks = &kvm_trap_emul_callbacks;
1328 return 0;
1329}