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
6 * KVM/MIPS: Deliver/Emulate exceptions to the guest kernel
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
12 #include <linux/errno.h>
13 #include <linux/err.h>
14 #include <linux/vmalloc.h>
16 #include <linux/kvm_host.h>
18 #include "interrupt.h"
20 static gpa_t kvm_trap_emul_gva_to_gpa_cb(gva_t gva)
23 gva_t kseg = KSEGX(gva);
25 if ((kseg == CKSEG0) || (kseg == CKSEG1))
28 kvm_err("%s: cannot find GPA for GVA: %#lx\n", __func__, gva);
29 kvm_mips_dump_host_tlbs();
30 gpa = KVM_INVALID_ADDR;
33 kvm_debug("%s: gva %#lx, gpa: %#llx\n", __func__, gva, gpa);
38 static int kvm_trap_emul_handle_cop_unusable(struct kvm_vcpu *vcpu)
40 struct mips_coproc *cop0 = vcpu->arch.cop0;
41 struct kvm_run *run = vcpu->run;
42 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
43 u32 cause = vcpu->arch.host_cp0_cause;
44 enum emulation_result er = EMULATE_DONE;
45 int ret = RESUME_GUEST;
47 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) {
49 if (!kvm_mips_guest_has_fpu(&vcpu->arch) ||
50 (kvm_read_c0_guest_status(cop0) & ST0_CU1) == 0) {
52 * Unusable/no FPU in guest:
53 * deliver guest COP1 Unusable Exception
55 er = kvm_mips_emulate_fpu_exc(cause, opc, run, vcpu);
57 /* Restore FPU state */
62 er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
71 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
76 run->exit_reason = KVM_EXIT_INTR;
86 static int kvm_trap_emul_handle_tlb_mod(struct kvm_vcpu *vcpu)
88 struct kvm_run *run = vcpu->run;
89 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
90 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
91 u32 cause = vcpu->arch.host_cp0_cause;
92 enum emulation_result er = EMULATE_DONE;
93 int ret = RESUME_GUEST;
95 if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
96 || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
97 kvm_debug("USER/KSEG23 ADDR TLB MOD fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
98 cause, opc, badvaddr);
99 er = kvm_mips_handle_tlbmod(cause, opc, run, vcpu);
101 if (er == EMULATE_DONE)
104 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
107 } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
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
113 kvm_err("TLB MOD fault not handled, cause %#x, PC: %p, BadVaddr: %#lx\n",
114 cause, opc, badvaddr);
115 kvm_mips_dump_host_tlbs();
116 kvm_arch_vcpu_dump_regs(vcpu);
117 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
120 kvm_err("Illegal TLB Mod fault address , cause %#x, PC: %p, BadVaddr: %#lx\n",
121 cause, opc, badvaddr);
122 kvm_mips_dump_host_tlbs();
123 kvm_arch_vcpu_dump_regs(vcpu);
124 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
130 static int kvm_trap_emul_handle_tlb_miss(struct kvm_vcpu *vcpu, bool store)
132 struct kvm_run *run = vcpu->run;
133 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
134 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
135 u32 cause = vcpu->arch.host_cp0_cause;
136 enum emulation_result er = EMULATE_DONE;
137 int ret = RESUME_GUEST;
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;
145 } else if (KVM_GUEST_KSEGX(badvaddr) < KVM_GUEST_KSEG0
146 || KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG23) {
147 kvm_debug("USER ADDR TLB %s fault: cause %#x, PC: %p, BadVaddr: %#lx\n",
148 store ? "ST" : "LD", cause, opc, badvaddr);
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
160 er = kvm_mips_handle_tlbmiss(cause, opc, run, vcpu);
161 if (er == EMULATE_DONE)
164 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
167 } else if (KVM_GUEST_KSEGX(badvaddr) == KVM_GUEST_KSEG0) {
169 * All KSEG0 faults are handled by KVM, as the guest kernel does
170 * not expect to ever get them
172 if (kvm_mips_handle_kseg0_tlb_fault
173 (vcpu->arch.host_cp0_badvaddr, vcpu) < 0) {
174 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
177 } else if (KVM_GUEST_KERNEL_MODE(vcpu)
178 && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
180 * With EVA we may get a TLB exception instead of an address
181 * error when the guest performs MMIO to KSeg1 addresses.
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;
192 run->exit_reason = KVM_EXIT_MMIO;
196 kvm_err("Illegal TLB %s fault address , cause %#x, PC: %p, BadVaddr: %#lx\n",
197 store ? "ST" : "LD", cause, opc, badvaddr);
198 kvm_mips_dump_host_tlbs();
199 kvm_arch_vcpu_dump_regs(vcpu);
200 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
206 static int kvm_trap_emul_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
208 return kvm_trap_emul_handle_tlb_miss(vcpu, true);
211 static int kvm_trap_emul_handle_tlb_ld_miss(struct kvm_vcpu *vcpu)
213 return kvm_trap_emul_handle_tlb_miss(vcpu, false);
216 static int kvm_trap_emul_handle_addr_err_st(struct kvm_vcpu *vcpu)
218 struct kvm_run *run = vcpu->run;
219 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
220 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
221 u32 cause = vcpu->arch.host_cp0_cause;
222 enum emulation_result er = EMULATE_DONE;
223 int ret = RESUME_GUEST;
225 if (KVM_GUEST_KERNEL_MODE(vcpu)
226 && (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1)) {
227 kvm_debug("Emulate Store to MMIO space\n");
228 er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
229 if (er == EMULATE_FAIL) {
230 kvm_err("Emulate Store to MMIO space failed\n");
231 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
234 run->exit_reason = KVM_EXIT_MMIO;
238 kvm_err("Address Error (STORE): cause %#x, PC: %p, BadVaddr: %#lx\n",
239 cause, opc, badvaddr);
240 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
246 static int kvm_trap_emul_handle_addr_err_ld(struct kvm_vcpu *vcpu)
248 struct kvm_run *run = vcpu->run;
249 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
250 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
251 u32 cause = vcpu->arch.host_cp0_cause;
252 enum emulation_result er = EMULATE_DONE;
253 int ret = RESUME_GUEST;
255 if (KSEGX(badvaddr) == CKSEG0 || KSEGX(badvaddr) == CKSEG1) {
256 kvm_debug("Emulate Load from MMIO space @ %#lx\n", badvaddr);
257 er = kvm_mips_emulate_inst(cause, opc, run, vcpu);
258 if (er == EMULATE_FAIL) {
259 kvm_err("Emulate Load from MMIO space failed\n");
260 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
263 run->exit_reason = KVM_EXIT_MMIO;
267 kvm_err("Address Error (LOAD): cause %#x, PC: %p, BadVaddr: %#lx\n",
268 cause, opc, badvaddr);
269 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
276 static int kvm_trap_emul_handle_syscall(struct kvm_vcpu *vcpu)
278 struct kvm_run *run = vcpu->run;
279 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
280 u32 cause = vcpu->arch.host_cp0_cause;
281 enum emulation_result er = EMULATE_DONE;
282 int ret = RESUME_GUEST;
284 er = kvm_mips_emulate_syscall(cause, opc, run, vcpu);
285 if (er == EMULATE_DONE)
288 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
294 static int kvm_trap_emul_handle_res_inst(struct kvm_vcpu *vcpu)
296 struct kvm_run *run = vcpu->run;
297 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
298 u32 cause = vcpu->arch.host_cp0_cause;
299 enum emulation_result er = EMULATE_DONE;
300 int ret = RESUME_GUEST;
302 er = kvm_mips_handle_ri(cause, opc, run, vcpu);
303 if (er == EMULATE_DONE)
306 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
312 static int kvm_trap_emul_handle_break(struct kvm_vcpu *vcpu)
314 struct kvm_run *run = vcpu->run;
315 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
316 u32 cause = vcpu->arch.host_cp0_cause;
317 enum emulation_result er = EMULATE_DONE;
318 int ret = RESUME_GUEST;
320 er = kvm_mips_emulate_bp_exc(cause, opc, run, vcpu);
321 if (er == EMULATE_DONE)
324 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
330 static int kvm_trap_emul_handle_trap(struct kvm_vcpu *vcpu)
332 struct kvm_run *run = vcpu->run;
333 u32 __user *opc = (u32 __user *)vcpu->arch.pc;
334 u32 cause = vcpu->arch.host_cp0_cause;
335 enum emulation_result er = EMULATE_DONE;
336 int ret = RESUME_GUEST;
338 er = kvm_mips_emulate_trap_exc(cause, opc, run, vcpu);
339 if (er == EMULATE_DONE) {
342 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
348 static int kvm_trap_emul_handle_msa_fpe(struct kvm_vcpu *vcpu)
350 struct kvm_run *run = vcpu->run;
351 u32 __user *opc = (u32 __user *)vcpu->arch.pc;
352 u32 cause = vcpu->arch.host_cp0_cause;
353 enum emulation_result er = EMULATE_DONE;
354 int ret = RESUME_GUEST;
356 er = kvm_mips_emulate_msafpe_exc(cause, opc, run, vcpu);
357 if (er == EMULATE_DONE) {
360 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
366 static int kvm_trap_emul_handle_fpe(struct kvm_vcpu *vcpu)
368 struct kvm_run *run = vcpu->run;
369 u32 __user *opc = (u32 __user *)vcpu->arch.pc;
370 u32 cause = vcpu->arch.host_cp0_cause;
371 enum emulation_result er = EMULATE_DONE;
372 int ret = RESUME_GUEST;
374 er = kvm_mips_emulate_fpe_exc(cause, opc, run, vcpu);
375 if (er == EMULATE_DONE) {
378 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
385 * kvm_trap_emul_handle_msa_disabled() - Guest used MSA while disabled in root.
386 * @vcpu: Virtual CPU context.
388 * Handle when the guest attempts to use MSA when it is disabled.
390 static int kvm_trap_emul_handle_msa_disabled(struct kvm_vcpu *vcpu)
392 struct mips_coproc *cop0 = vcpu->arch.cop0;
393 struct kvm_run *run = vcpu->run;
394 u32 __user *opc = (u32 __user *) vcpu->arch.pc;
395 u32 cause = vcpu->arch.host_cp0_cause;
396 enum emulation_result er = EMULATE_DONE;
397 int ret = RESUME_GUEST;
399 if (!kvm_mips_guest_has_msa(&vcpu->arch) ||
400 (kvm_read_c0_guest_status(cop0) & (ST0_CU1 | ST0_FR)) == ST0_CU1) {
402 * No MSA in guest, or FPU enabled and not in FR=1 mode,
403 * guest reserved instruction exception
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);
410 /* Restore MSA/FPU state */
421 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
431 static int kvm_trap_emul_vm_init(struct kvm *kvm)
436 static int kvm_trap_emul_vcpu_init(struct kvm_vcpu *vcpu)
438 vcpu->arch.kscratch_enabled = 0xfc;
443 static int kvm_trap_emul_vcpu_setup(struct kvm_vcpu *vcpu)
445 struct mips_coproc *cop0 = vcpu->arch.cop0;
447 int vcpu_id = vcpu->vcpu_id;
450 * Arch specific stuff, set up config registers properly so that the
451 * guest will come up as expected
453 #ifndef CONFIG_CPU_MIPSR6
454 /* r2-r5, simulate a MIPS 24kc */
455 kvm_write_c0_guest_prid(cop0, 0x00019300);
457 /* r6+, simulate a generic QEMU machine */
458 kvm_write_c0_guest_prid(cop0, 0x00010000);
461 * Have config1, Cacheable, noncoherent, write-back, write allocate.
462 * Endianness, arch revision & virtually tagged icache should match
465 config = read_c0_config() & MIPS_CONF_AR;
466 config |= MIPS_CONF_M | CONF_CM_CACHABLE_NONCOHERENT | MIPS_CONF_MT_TLB;
467 #ifdef CONFIG_CPU_BIG_ENDIAN
470 if (cpu_has_vtag_icache)
471 config |= MIPS_CONF_VI;
472 kvm_write_c0_guest_config(cop0, config);
474 /* Read the cache characteristics from the host Config1 Register */
475 config1 = (read_c0_config1() & ~0x7f);
477 /* Set up MMU size */
478 config1 &= ~(0x3f << 25);
479 config1 |= ((KVM_MIPS_GUEST_TLB_SIZE - 1) << 25);
481 /* We unset some bits that we aren't emulating */
482 config1 &= ~(MIPS_CONF1_C2 | MIPS_CONF1_MD | MIPS_CONF1_PC |
483 MIPS_CONF1_WR | MIPS_CONF1_CA);
484 kvm_write_c0_guest_config1(cop0, config1);
486 /* 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) */
490 /* Have config4, UserLocal */
491 kvm_write_c0_guest_config3(cop0, MIPS_CONF_M | MIPS_CONF3_ULRI);
494 kvm_write_c0_guest_config4(cop0, MIPS_CONF_M);
497 kvm_write_c0_guest_config5(cop0, 0);
499 /* Set Wait IE/IXMT Ignore in Config7, IAR, AR */
500 kvm_write_c0_guest_config7(cop0, (MIPS_CONF7_WII) | (1 << 10));
503 * Setup IntCtl defaults, compatibility mode for timer interrupts (HW5)
505 kvm_write_c0_guest_intctl(cop0, 0xFC000000);
507 /* Put in vcpu id as CPUNum into Ebase Reg to handle SMP Guests */
508 kvm_write_c0_guest_ebase(cop0, KVM_GUEST_KSEG0 |
509 (vcpu_id & MIPS_EBASE_CPUNUM));
514 static unsigned long kvm_trap_emul_num_regs(struct kvm_vcpu *vcpu)
519 static int kvm_trap_emul_copy_reg_indices(struct kvm_vcpu *vcpu,
525 static int kvm_trap_emul_get_one_reg(struct kvm_vcpu *vcpu,
526 const struct kvm_one_reg *reg,
530 case KVM_REG_MIPS_CP0_COUNT:
531 *v = kvm_mips_read_count(vcpu);
533 case KVM_REG_MIPS_COUNT_CTL:
534 *v = vcpu->arch.count_ctl;
536 case KVM_REG_MIPS_COUNT_RESUME:
537 *v = ktime_to_ns(vcpu->arch.count_resume);
539 case KVM_REG_MIPS_COUNT_HZ:
540 *v = vcpu->arch.count_hz;
548 static int kvm_trap_emul_set_one_reg(struct kvm_vcpu *vcpu,
549 const struct kvm_one_reg *reg,
552 struct mips_coproc *cop0 = vcpu->arch.cop0;
554 unsigned int cur, change;
557 case KVM_REG_MIPS_CP0_COUNT:
558 kvm_mips_write_count(vcpu, v);
560 case KVM_REG_MIPS_CP0_COMPARE:
561 kvm_mips_write_compare(vcpu, v, false);
563 case KVM_REG_MIPS_CP0_CAUSE:
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.
569 if ((kvm_read_c0_guest_cause(cop0) ^ v) & CAUSEF_DC) {
571 /* disable timer first */
572 kvm_mips_count_disable_cause(vcpu);
573 kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v);
575 /* enable timer last */
576 kvm_change_c0_guest_cause(cop0, ~CAUSEF_DC, v);
577 kvm_mips_count_enable_cause(vcpu);
580 kvm_write_c0_guest_cause(cop0, v);
583 case KVM_REG_MIPS_CP0_CONFIG:
584 /* read-only for now */
586 case KVM_REG_MIPS_CP0_CONFIG1:
587 cur = kvm_read_c0_guest_config1(cop0);
588 change = (cur ^ v) & kvm_mips_config1_wrmask(vcpu);
591 kvm_write_c0_guest_config1(cop0, v);
594 case KVM_REG_MIPS_CP0_CONFIG2:
595 /* read-only for now */
597 case KVM_REG_MIPS_CP0_CONFIG3:
598 cur = kvm_read_c0_guest_config3(cop0);
599 change = (cur ^ v) & kvm_mips_config3_wrmask(vcpu);
602 kvm_write_c0_guest_config3(cop0, v);
605 case KVM_REG_MIPS_CP0_CONFIG4:
606 cur = kvm_read_c0_guest_config4(cop0);
607 change = (cur ^ v) & kvm_mips_config4_wrmask(vcpu);
610 kvm_write_c0_guest_config4(cop0, v);
613 case KVM_REG_MIPS_CP0_CONFIG5:
614 cur = kvm_read_c0_guest_config5(cop0);
615 change = (cur ^ v) & kvm_mips_config5_wrmask(vcpu);
618 kvm_write_c0_guest_config5(cop0, v);
621 case KVM_REG_MIPS_COUNT_CTL:
622 ret = kvm_mips_set_count_ctl(vcpu, v);
624 case KVM_REG_MIPS_COUNT_RESUME:
625 ret = kvm_mips_set_count_resume(vcpu, v);
627 case KVM_REG_MIPS_COUNT_HZ:
628 ret = kvm_mips_set_count_hz(vcpu, v);
636 static int kvm_trap_emul_vcpu_get_regs(struct kvm_vcpu *vcpu)
643 static int kvm_trap_emul_vcpu_set_regs(struct kvm_vcpu *vcpu)
648 static struct kvm_mips_callbacks kvm_trap_emul_callbacks = {
650 .handle_cop_unusable = kvm_trap_emul_handle_cop_unusable,
651 .handle_tlb_mod = kvm_trap_emul_handle_tlb_mod,
652 .handle_tlb_st_miss = kvm_trap_emul_handle_tlb_st_miss,
653 .handle_tlb_ld_miss = kvm_trap_emul_handle_tlb_ld_miss,
654 .handle_addr_err_st = kvm_trap_emul_handle_addr_err_st,
655 .handle_addr_err_ld = kvm_trap_emul_handle_addr_err_ld,
656 .handle_syscall = kvm_trap_emul_handle_syscall,
657 .handle_res_inst = kvm_trap_emul_handle_res_inst,
658 .handle_break = kvm_trap_emul_handle_break,
659 .handle_trap = kvm_trap_emul_handle_trap,
660 .handle_msa_fpe = kvm_trap_emul_handle_msa_fpe,
661 .handle_fpe = kvm_trap_emul_handle_fpe,
662 .handle_msa_disabled = kvm_trap_emul_handle_msa_disabled,
664 .vm_init = kvm_trap_emul_vm_init,
665 .vcpu_init = kvm_trap_emul_vcpu_init,
666 .vcpu_setup = kvm_trap_emul_vcpu_setup,
667 .gva_to_gpa = kvm_trap_emul_gva_to_gpa_cb,
668 .queue_timer_int = kvm_mips_queue_timer_int_cb,
669 .dequeue_timer_int = kvm_mips_dequeue_timer_int_cb,
670 .queue_io_int = kvm_mips_queue_io_int_cb,
671 .dequeue_io_int = kvm_mips_dequeue_io_int_cb,
672 .irq_deliver = kvm_mips_irq_deliver_cb,
673 .irq_clear = kvm_mips_irq_clear_cb,
674 .num_regs = kvm_trap_emul_num_regs,
675 .copy_reg_indices = kvm_trap_emul_copy_reg_indices,
676 .get_one_reg = kvm_trap_emul_get_one_reg,
677 .set_one_reg = kvm_trap_emul_set_one_reg,
678 .vcpu_get_regs = kvm_trap_emul_vcpu_get_regs,
679 .vcpu_set_regs = kvm_trap_emul_vcpu_set_regs,
682 int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks)
684 *install_callbacks = &kvm_trap_emul_callbacks;