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: Instruction/Exception emulation
8 * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
12 #include <linux/errno.h>
13 #include <linux/err.h>
14 #include <linux/ktime.h>
15 #include <linux/kvm_host.h>
16 #include <linux/module.h>
17 #include <linux/vmalloc.h>
19 #include <linux/bootmem.h>
20 #include <linux/random.h>
22 #include <asm/cacheflush.h>
23 #include <asm/cacheops.h>
24 #include <asm/cpu-info.h>
25 #include <asm/mmu_context.h>
26 #include <asm/tlbflush.h>
30 #include <asm/r4kcache.h>
31 #define CONFIG_MIPS_MT
33 #include "interrupt.h"
39 * Compute the return address and do emulate branch simulation, if required.
40 * This function should be called only in branch delay slot active.
42 unsigned long kvm_compute_return_epc(struct kvm_vcpu *vcpu,
45 unsigned int dspcontrol;
46 union mips_instruction insn;
47 struct kvm_vcpu_arch *arch = &vcpu->arch;
49 long nextpc = KVM_INVALID_INST;
54 /* Read the instruction */
55 insn.word = kvm_get_inst((u32 *) epc, vcpu);
57 if (insn.word == KVM_INVALID_INST)
58 return KVM_INVALID_INST;
60 switch (insn.i_format.opcode) {
61 /* jr and jalr are in r_format format. */
63 switch (insn.r_format.func) {
65 arch->gprs[insn.r_format.rd] = epc + 8;
68 nextpc = arch->gprs[insn.r_format.rs];
74 * This group contains:
75 * bltz_op, bgez_op, bltzl_op, bgezl_op,
76 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
79 switch (insn.i_format.rt) {
82 if ((long)arch->gprs[insn.i_format.rs] < 0)
83 epc = epc + 4 + (insn.i_format.simmediate << 2);
91 if ((long)arch->gprs[insn.i_format.rs] >= 0)
92 epc = epc + 4 + (insn.i_format.simmediate << 2);
100 arch->gprs[31] = epc + 8;
101 if ((long)arch->gprs[insn.i_format.rs] < 0)
102 epc = epc + 4 + (insn.i_format.simmediate << 2);
110 arch->gprs[31] = epc + 8;
111 if ((long)arch->gprs[insn.i_format.rs] >= 0)
112 epc = epc + 4 + (insn.i_format.simmediate << 2);
121 dspcontrol = rddsp(0x01);
123 if (dspcontrol >= 32)
124 epc = epc + 4 + (insn.i_format.simmediate << 2);
132 /* These are unconditional and in j_format. */
134 arch->gprs[31] = instpc + 8;
139 epc |= (insn.j_format.target << 2);
143 /* These are conditional and in i_format. */
146 if (arch->gprs[insn.i_format.rs] ==
147 arch->gprs[insn.i_format.rt])
148 epc = epc + 4 + (insn.i_format.simmediate << 2);
156 if (arch->gprs[insn.i_format.rs] !=
157 arch->gprs[insn.i_format.rt])
158 epc = epc + 4 + (insn.i_format.simmediate << 2);
164 case blez_op: /* POP06 */
165 #ifndef CONFIG_CPU_MIPSR6
166 case blezl_op: /* removed in R6 */
168 if (insn.i_format.rt != 0)
170 if ((long)arch->gprs[insn.i_format.rs] <= 0)
171 epc = epc + 4 + (insn.i_format.simmediate << 2);
177 case bgtz_op: /* POP07 */
178 #ifndef CONFIG_CPU_MIPSR6
179 case bgtzl_op: /* removed in R6 */
181 if (insn.i_format.rt != 0)
183 if ((long)arch->gprs[insn.i_format.rs] > 0)
184 epc = epc + 4 + (insn.i_format.simmediate << 2);
190 /* And now the FPA/cp1 branch instructions. */
192 kvm_err("%s: unsupported cop1_op\n", __func__);
195 #ifdef CONFIG_CPU_MIPSR6
196 /* R6 added the following compact branches with forbidden slots */
197 case blezl_op: /* POP26 */
198 case bgtzl_op: /* POP27 */
199 /* only rt == 0 isn't compact branch */
200 if (insn.i_format.rt != 0)
205 /* only rs == rt == 0 is reserved, rest are compact branches */
206 if (insn.i_format.rs != 0 || insn.i_format.rt != 0)
211 /* only rs == 0 isn't compact branch */
212 if (insn.i_format.rs != 0)
217 * If we've hit an exception on the forbidden slot, then
218 * the branch must not have been taken.
225 /* Compact branches not supported before R6 */
233 kvm_err("%s: unaligned epc\n", __func__);
237 kvm_err("%s: DSP branch but not DSP ASE\n", __func__);
241 enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause)
243 unsigned long branch_pc;
244 enum emulation_result er = EMULATE_DONE;
246 if (cause & CAUSEF_BD) {
247 branch_pc = kvm_compute_return_epc(vcpu, vcpu->arch.pc);
248 if (branch_pc == KVM_INVALID_INST) {
251 vcpu->arch.pc = branch_pc;
252 kvm_debug("BD update_pc(): New PC: %#lx\n",
258 kvm_debug("update_pc(): New PC: %#lx\n", vcpu->arch.pc);
264 * kvm_mips_count_disabled() - Find whether the CP0_Count timer is disabled.
265 * @vcpu: Virtual CPU.
267 * Returns: 1 if the CP0_Count timer is disabled by either the guest
268 * CP0_Cause.DC bit or the count_ctl.DC bit.
269 * 0 otherwise (in which case CP0_Count timer is running).
271 static inline int kvm_mips_count_disabled(struct kvm_vcpu *vcpu)
273 struct mips_coproc *cop0 = vcpu->arch.cop0;
275 return (vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) ||
276 (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC);
280 * kvm_mips_ktime_to_count() - Scale ktime_t to a 32-bit count.
282 * Caches the dynamic nanosecond bias in vcpu->arch.count_dyn_bias.
284 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
286 static u32 kvm_mips_ktime_to_count(struct kvm_vcpu *vcpu, ktime_t now)
291 now_ns = ktime_to_ns(now);
292 delta = now_ns + vcpu->arch.count_dyn_bias;
294 if (delta >= vcpu->arch.count_period) {
295 /* If delta is out of safe range the bias needs adjusting */
296 periods = div64_s64(now_ns, vcpu->arch.count_period);
297 vcpu->arch.count_dyn_bias = -periods * vcpu->arch.count_period;
298 /* Recalculate delta with new bias */
299 delta = now_ns + vcpu->arch.count_dyn_bias;
303 * We've ensured that:
304 * delta < count_period
306 * Therefore the intermediate delta*count_hz will never overflow since
307 * at the boundary condition:
308 * delta = count_period
309 * delta = NSEC_PER_SEC * 2^32 / count_hz
310 * delta * count_hz = NSEC_PER_SEC * 2^32
312 return div_u64(delta * vcpu->arch.count_hz, NSEC_PER_SEC);
316 * kvm_mips_count_time() - Get effective current time.
317 * @vcpu: Virtual CPU.
319 * Get effective monotonic ktime. This is usually a straightforward ktime_get(),
320 * except when the master disable bit is set in count_ctl, in which case it is
321 * count_resume, i.e. the time that the count was disabled.
323 * Returns: Effective monotonic ktime for CP0_Count.
325 static inline ktime_t kvm_mips_count_time(struct kvm_vcpu *vcpu)
327 if (unlikely(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
328 return vcpu->arch.count_resume;
334 * kvm_mips_read_count_running() - Read the current count value as if running.
335 * @vcpu: Virtual CPU.
336 * @now: Kernel time to read CP0_Count at.
338 * Returns the current guest CP0_Count register at time @now and handles if the
339 * timer interrupt is pending and hasn't been handled yet.
341 * Returns: The current value of the guest CP0_Count register.
343 static u32 kvm_mips_read_count_running(struct kvm_vcpu *vcpu, ktime_t now)
345 struct mips_coproc *cop0 = vcpu->arch.cop0;
346 ktime_t expires, threshold;
350 /* Calculate the biased and scaled guest CP0_Count */
351 count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
352 compare = kvm_read_c0_guest_compare(cop0);
355 * Find whether CP0_Count has reached the closest timer interrupt. If
356 * not, we shouldn't inject it.
358 if ((s32)(count - compare) < 0)
362 * The CP0_Count we're going to return has already reached the closest
363 * timer interrupt. Quickly check if it really is a new interrupt by
364 * looking at whether the interval until the hrtimer expiry time is
365 * less than 1/4 of the timer period.
367 expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer);
368 threshold = ktime_add_ns(now, vcpu->arch.count_period / 4);
369 if (ktime_before(expires, threshold)) {
371 * Cancel it while we handle it so there's no chance of
372 * interference with the timeout handler.
374 running = hrtimer_cancel(&vcpu->arch.comparecount_timer);
376 /* Nothing should be waiting on the timeout */
377 kvm_mips_callbacks->queue_timer_int(vcpu);
380 * Restart the timer if it was running based on the expiry time
381 * we read, so that we don't push it back 2 periods.
384 expires = ktime_add_ns(expires,
385 vcpu->arch.count_period);
386 hrtimer_start(&vcpu->arch.comparecount_timer, expires,
395 * kvm_mips_read_count() - Read the current count value.
396 * @vcpu: Virtual CPU.
398 * Read the current guest CP0_Count value, taking into account whether the timer
401 * Returns: The current guest CP0_Count value.
403 u32 kvm_mips_read_count(struct kvm_vcpu *vcpu)
405 struct mips_coproc *cop0 = vcpu->arch.cop0;
407 /* If count disabled just read static copy of count */
408 if (kvm_mips_count_disabled(vcpu))
409 return kvm_read_c0_guest_count(cop0);
411 return kvm_mips_read_count_running(vcpu, ktime_get());
415 * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer.
416 * @vcpu: Virtual CPU.
417 * @count: Output pointer for CP0_Count value at point of freeze.
419 * Freeze the hrtimer safely and return both the ktime and the CP0_Count value
420 * at the point it was frozen. It is guaranteed that any pending interrupts at
421 * the point it was frozen are handled, and none after that point.
423 * This is useful where the time/CP0_Count is needed in the calculation of the
426 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
428 * Returns: The ktime at the point of freeze.
430 static ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu, u32 *count)
434 /* stop hrtimer before finding time */
435 hrtimer_cancel(&vcpu->arch.comparecount_timer);
438 /* find count at this point and handle pending hrtimer */
439 *count = kvm_mips_read_count_running(vcpu, now);
445 * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry.
446 * @vcpu: Virtual CPU.
447 * @now: ktime at point of resume.
448 * @count: CP0_Count at point of resume.
450 * Resumes the timer and updates the timer expiry based on @now and @count.
451 * This can be used in conjunction with kvm_mips_freeze_timer() when timer
452 * parameters need to be changed.
454 * It is guaranteed that a timer interrupt immediately after resume will be
455 * handled, but not if CP_Compare is exactly at @count. That case is already
456 * handled by kvm_mips_freeze_timer().
458 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
460 static void kvm_mips_resume_hrtimer(struct kvm_vcpu *vcpu,
461 ktime_t now, u32 count)
463 struct mips_coproc *cop0 = vcpu->arch.cop0;
468 /* Calculate timeout (wrap 0 to 2^32) */
469 compare = kvm_read_c0_guest_compare(cop0);
470 delta = (u64)(u32)(compare - count - 1) + 1;
471 delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
472 expire = ktime_add_ns(now, delta);
474 /* Update hrtimer to use new timeout */
475 hrtimer_cancel(&vcpu->arch.comparecount_timer);
476 hrtimer_start(&vcpu->arch.comparecount_timer, expire, HRTIMER_MODE_ABS);
480 * kvm_mips_write_count() - Modify the count and update timer.
481 * @vcpu: Virtual CPU.
482 * @count: Guest CP0_Count value to set.
484 * Sets the CP0_Count value and updates the timer accordingly.
486 void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count)
488 struct mips_coproc *cop0 = vcpu->arch.cop0;
492 now = kvm_mips_count_time(vcpu);
493 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
495 if (kvm_mips_count_disabled(vcpu))
496 /* The timer's disabled, adjust the static count */
497 kvm_write_c0_guest_count(cop0, count);
500 kvm_mips_resume_hrtimer(vcpu, now, count);
504 * kvm_mips_init_count() - Initialise timer.
505 * @vcpu: Virtual CPU.
507 * Initialise the timer to a sensible frequency, namely 100MHz, zero it, and set
508 * it going if it's enabled.
510 void kvm_mips_init_count(struct kvm_vcpu *vcpu)
513 vcpu->arch.count_hz = 100*1000*1000;
514 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32,
515 vcpu->arch.count_hz);
516 vcpu->arch.count_dyn_bias = 0;
519 kvm_mips_write_count(vcpu, 0);
523 * kvm_mips_set_count_hz() - Update the frequency of the timer.
524 * @vcpu: Virtual CPU.
525 * @count_hz: Frequency of CP0_Count timer in Hz.
527 * Change the frequency of the CP0_Count timer. This is done atomically so that
528 * CP0_Count is continuous and no timer interrupt is lost.
530 * Returns: -EINVAL if @count_hz is out of range.
533 int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
535 struct mips_coproc *cop0 = vcpu->arch.cop0;
540 /* ensure the frequency is in a sensible range... */
541 if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
543 /* ... and has actually changed */
544 if (vcpu->arch.count_hz == count_hz)
547 /* Safely freeze timer so we can keep it continuous */
548 dc = kvm_mips_count_disabled(vcpu);
550 now = kvm_mips_count_time(vcpu);
551 count = kvm_read_c0_guest_count(cop0);
553 now = kvm_mips_freeze_hrtimer(vcpu, &count);
556 /* Update the frequency */
557 vcpu->arch.count_hz = count_hz;
558 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
559 vcpu->arch.count_dyn_bias = 0;
561 /* Calculate adjusted bias so dynamic count is unchanged */
562 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
564 /* Update and resume hrtimer */
566 kvm_mips_resume_hrtimer(vcpu, now, count);
571 * kvm_mips_write_compare() - Modify compare and update timer.
572 * @vcpu: Virtual CPU.
573 * @compare: New CP0_Compare value.
574 * @ack: Whether to acknowledge timer interrupt.
576 * Update CP0_Compare to a new value and update the timeout.
577 * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure
578 * any pending timer interrupt is preserved.
580 void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack)
582 struct mips_coproc *cop0 = vcpu->arch.cop0;
584 u32 old_compare = kvm_read_c0_guest_compare(cop0);
588 /* if unchanged, must just be an ack */
589 if (old_compare == compare) {
592 kvm_mips_callbacks->dequeue_timer_int(vcpu);
593 kvm_write_c0_guest_compare(cop0, compare);
597 /* freeze_hrtimer() takes care of timer interrupts <= count */
598 dc = kvm_mips_count_disabled(vcpu);
600 now = kvm_mips_freeze_hrtimer(vcpu, &count);
603 kvm_mips_callbacks->dequeue_timer_int(vcpu);
605 kvm_write_c0_guest_compare(cop0, compare);
607 /* resume_hrtimer() takes care of timer interrupts > count */
609 kvm_mips_resume_hrtimer(vcpu, now, count);
613 * kvm_mips_count_disable() - Disable count.
614 * @vcpu: Virtual CPU.
616 * Disable the CP0_Count timer. A timer interrupt on or before the final stop
617 * time will be handled but not after.
619 * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
620 * count_ctl.DC has been set (count disabled).
622 * Returns: The time that the timer was stopped.
624 static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu)
626 struct mips_coproc *cop0 = vcpu->arch.cop0;
631 hrtimer_cancel(&vcpu->arch.comparecount_timer);
633 /* Set the static count from the dynamic count, handling pending TI */
635 count = kvm_mips_read_count_running(vcpu, now);
636 kvm_write_c0_guest_count(cop0, count);
642 * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
643 * @vcpu: Virtual CPU.
645 * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
646 * before the final stop time will be handled if the timer isn't disabled by
647 * count_ctl.DC, but not after.
649 * Assumes CP0_Cause.DC is clear (count enabled).
651 void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu)
653 struct mips_coproc *cop0 = vcpu->arch.cop0;
655 kvm_set_c0_guest_cause(cop0, CAUSEF_DC);
656 if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
657 kvm_mips_count_disable(vcpu);
661 * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
662 * @vcpu: Virtual CPU.
664 * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
665 * the start time will be handled if the timer isn't disabled by count_ctl.DC,
666 * potentially before even returning, so the caller should be careful with
667 * ordering of CP0_Cause modifications so as not to lose it.
669 * Assumes CP0_Cause.DC is set (count disabled).
671 void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu)
673 struct mips_coproc *cop0 = vcpu->arch.cop0;
676 kvm_clear_c0_guest_cause(cop0, CAUSEF_DC);
679 * Set the dynamic count to match the static count.
680 * This starts the hrtimer if count_ctl.DC allows it.
681 * Otherwise it conveniently updates the biases.
683 count = kvm_read_c0_guest_count(cop0);
684 kvm_mips_write_count(vcpu, count);
688 * kvm_mips_set_count_ctl() - Update the count control KVM register.
689 * @vcpu: Virtual CPU.
690 * @count_ctl: Count control register new value.
692 * Set the count control KVM register. The timer is updated accordingly.
694 * Returns: -EINVAL if reserved bits are set.
697 int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl)
699 struct mips_coproc *cop0 = vcpu->arch.cop0;
700 s64 changed = count_ctl ^ vcpu->arch.count_ctl;
705 /* Only allow defined bits to be changed */
706 if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC))
709 /* Apply new value */
710 vcpu->arch.count_ctl = count_ctl;
712 /* Master CP0_Count disable */
713 if (changed & KVM_REG_MIPS_COUNT_CTL_DC) {
714 /* Is CP0_Cause.DC already disabling CP0_Count? */
715 if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) {
716 if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)
717 /* Just record the current time */
718 vcpu->arch.count_resume = ktime_get();
719 } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) {
720 /* disable timer and record current time */
721 vcpu->arch.count_resume = kvm_mips_count_disable(vcpu);
724 * Calculate timeout relative to static count at resume
725 * time (wrap 0 to 2^32).
727 count = kvm_read_c0_guest_count(cop0);
728 compare = kvm_read_c0_guest_compare(cop0);
729 delta = (u64)(u32)(compare - count - 1) + 1;
730 delta = div_u64(delta * NSEC_PER_SEC,
731 vcpu->arch.count_hz);
732 expire = ktime_add_ns(vcpu->arch.count_resume, delta);
734 /* Handle pending interrupt */
736 if (ktime_compare(now, expire) >= 0)
737 /* Nothing should be waiting on the timeout */
738 kvm_mips_callbacks->queue_timer_int(vcpu);
740 /* Resume hrtimer without changing bias */
741 count = kvm_mips_read_count_running(vcpu, now);
742 kvm_mips_resume_hrtimer(vcpu, now, count);
750 * kvm_mips_set_count_resume() - Update the count resume KVM register.
751 * @vcpu: Virtual CPU.
752 * @count_resume: Count resume register new value.
754 * Set the count resume KVM register.
756 * Returns: -EINVAL if out of valid range (0..now).
759 int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume)
762 * It doesn't make sense for the resume time to be in the future, as it
763 * would be possible for the next interrupt to be more than a full
764 * period in the future.
766 if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get()))
769 vcpu->arch.count_resume = ns_to_ktime(count_resume);
774 * kvm_mips_count_timeout() - Push timer forward on timeout.
775 * @vcpu: Virtual CPU.
777 * Handle an hrtimer event by push the hrtimer forward a period.
779 * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
781 enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu)
783 /* Add the Count period to the current expiry time */
784 hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer,
785 vcpu->arch.count_period);
786 return HRTIMER_RESTART;
789 enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
791 struct mips_coproc *cop0 = vcpu->arch.cop0;
792 enum emulation_result er = EMULATE_DONE;
794 if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
795 kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
796 kvm_read_c0_guest_epc(cop0));
797 kvm_clear_c0_guest_status(cop0, ST0_EXL);
798 vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
800 } else if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
801 kvm_clear_c0_guest_status(cop0, ST0_ERL);
802 vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
804 kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
812 enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
814 kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
815 vcpu->arch.pending_exceptions);
817 ++vcpu->stat.wait_exits;
818 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_WAIT);
819 if (!vcpu->arch.pending_exceptions) {
821 kvm_vcpu_block(vcpu);
824 * We we are runnable, then definitely go off to user space to
825 * check if any I/O interrupts are pending.
827 if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
828 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
829 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
837 * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that
838 * we can catch this, if things ever change
840 enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
842 struct mips_coproc *cop0 = vcpu->arch.cop0;
843 unsigned long pc = vcpu->arch.pc;
845 kvm_err("[%#lx] COP0_TLBR [%ld]\n", pc, kvm_read_c0_guest_index(cop0));
849 /* Write Guest TLB Entry @ Index */
850 enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
852 struct mips_coproc *cop0 = vcpu->arch.cop0;
853 int index = kvm_read_c0_guest_index(cop0);
854 struct kvm_mips_tlb *tlb = NULL;
855 unsigned long pc = vcpu->arch.pc;
857 if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
858 kvm_debug("%s: illegal index: %d\n", __func__, index);
859 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
860 pc, index, kvm_read_c0_guest_entryhi(cop0),
861 kvm_read_c0_guest_entrylo0(cop0),
862 kvm_read_c0_guest_entrylo1(cop0),
863 kvm_read_c0_guest_pagemask(cop0));
864 index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
867 tlb = &vcpu->arch.guest_tlb[index];
869 * Probe the shadow host TLB for the entry being overwritten, if one
870 * matches, invalidate it
872 kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
874 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
875 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
876 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
877 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
879 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
880 pc, index, kvm_read_c0_guest_entryhi(cop0),
881 kvm_read_c0_guest_entrylo0(cop0),
882 kvm_read_c0_guest_entrylo1(cop0),
883 kvm_read_c0_guest_pagemask(cop0));
888 /* Write Guest TLB Entry @ Random Index */
889 enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
891 struct mips_coproc *cop0 = vcpu->arch.cop0;
892 struct kvm_mips_tlb *tlb = NULL;
893 unsigned long pc = vcpu->arch.pc;
896 get_random_bytes(&index, sizeof(index));
897 index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
899 tlb = &vcpu->arch.guest_tlb[index];
902 * Probe the shadow host TLB for the entry being overwritten, if one
903 * matches, invalidate it
905 kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
907 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
908 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
909 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
910 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
912 kvm_debug("[%#lx] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
913 pc, index, kvm_read_c0_guest_entryhi(cop0),
914 kvm_read_c0_guest_entrylo0(cop0),
915 kvm_read_c0_guest_entrylo1(cop0));
920 enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
922 struct mips_coproc *cop0 = vcpu->arch.cop0;
923 long entryhi = kvm_read_c0_guest_entryhi(cop0);
924 unsigned long pc = vcpu->arch.pc;
927 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
929 kvm_write_c0_guest_index(cop0, index);
931 kvm_debug("[%#lx] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
938 * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1
939 * @vcpu: Virtual CPU.
941 * Finds the mask of bits which are writable in the guest's Config1 CP0
942 * register, by userland (currently read-only to the guest).
944 unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu)
946 unsigned int mask = 0;
948 /* Permit FPU to be present if FPU is supported */
949 if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
950 mask |= MIPS_CONF1_FP;
956 * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3
957 * @vcpu: Virtual CPU.
959 * Finds the mask of bits which are writable in the guest's Config3 CP0
960 * register, by userland (currently read-only to the guest).
962 unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu)
964 /* Config4 and ULRI are optional */
965 unsigned int mask = MIPS_CONF_M | MIPS_CONF3_ULRI;
967 /* Permit MSA to be present if MSA is supported */
968 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
969 mask |= MIPS_CONF3_MSA;
975 * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4
976 * @vcpu: Virtual CPU.
978 * Finds the mask of bits which are writable in the guest's Config4 CP0
979 * register, by userland (currently read-only to the guest).
981 unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu)
983 /* Config5 is optional */
984 unsigned int mask = MIPS_CONF_M;
987 mask |= (unsigned int)vcpu->arch.kscratch_enabled << 16;
993 * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5
994 * @vcpu: Virtual CPU.
996 * Finds the mask of bits which are writable in the guest's Config5 CP0
997 * register, by the guest itself.
999 unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu)
1001 unsigned int mask = 0;
1003 /* Permit MSAEn changes if MSA supported and enabled */
1004 if (kvm_mips_guest_has_msa(&vcpu->arch))
1005 mask |= MIPS_CONF5_MSAEN;
1008 * Permit guest FPU mode changes if FPU is enabled and the relevant
1009 * feature exists according to FIR register.
1011 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1013 mask |= MIPS_CONF5_FRE;
1014 /* We don't support UFR or UFE */
1020 enum emulation_result kvm_mips_emulate_CP0(union mips_instruction inst,
1021 u32 *opc, u32 cause,
1022 struct kvm_run *run,
1023 struct kvm_vcpu *vcpu)
1025 struct mips_coproc *cop0 = vcpu->arch.cop0;
1026 enum emulation_result er = EMULATE_DONE;
1028 unsigned long curr_pc;
1031 * Update PC and hold onto current PC in case there is
1032 * an error and we want to rollback the PC
1034 curr_pc = vcpu->arch.pc;
1035 er = update_pc(vcpu, cause);
1036 if (er == EMULATE_FAIL)
1039 if (inst.co_format.co) {
1040 switch (inst.co_format.func) {
1041 case tlbr_op: /* Read indexed TLB entry */
1042 er = kvm_mips_emul_tlbr(vcpu);
1044 case tlbwi_op: /* Write indexed */
1045 er = kvm_mips_emul_tlbwi(vcpu);
1047 case tlbwr_op: /* Write random */
1048 er = kvm_mips_emul_tlbwr(vcpu);
1050 case tlbp_op: /* TLB Probe */
1051 er = kvm_mips_emul_tlbp(vcpu);
1054 kvm_err("!!!COP0_RFE!!!\n");
1057 er = kvm_mips_emul_eret(vcpu);
1058 goto dont_update_pc;
1060 er = kvm_mips_emul_wait(vcpu);
1064 rt = inst.c0r_format.rt;
1065 rd = inst.c0r_format.rd;
1066 sel = inst.c0r_format.sel;
1068 switch (inst.c0r_format.rs) {
1070 #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1071 cop0->stat[rd][sel]++;
1074 if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
1075 vcpu->arch.gprs[rt] = kvm_mips_read_count(vcpu);
1076 } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
1077 vcpu->arch.gprs[rt] = 0x0;
1078 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1079 kvm_mips_trans_mfc0(inst, opc, vcpu);
1082 vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
1084 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1085 kvm_mips_trans_mfc0(inst, opc, vcpu);
1089 trace_kvm_hwr(vcpu, KVM_TRACE_MFC0,
1090 KVM_TRACE_COP0(rd, sel),
1091 vcpu->arch.gprs[rt]);
1095 vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
1097 trace_kvm_hwr(vcpu, KVM_TRACE_DMFC0,
1098 KVM_TRACE_COP0(rd, sel),
1099 vcpu->arch.gprs[rt]);
1103 #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1104 cop0->stat[rd][sel]++;
1106 trace_kvm_hwr(vcpu, KVM_TRACE_MTC0,
1107 KVM_TRACE_COP0(rd, sel),
1108 vcpu->arch.gprs[rt]);
1110 if ((rd == MIPS_CP0_TLB_INDEX)
1111 && (vcpu->arch.gprs[rt] >=
1112 KVM_MIPS_GUEST_TLB_SIZE)) {
1113 kvm_err("Invalid TLB Index: %ld",
1114 vcpu->arch.gprs[rt]);
1118 #define C0_EBASE_CORE_MASK 0xff
1119 if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
1120 /* Preserve CORE number */
1121 kvm_change_c0_guest_ebase(cop0,
1122 ~(C0_EBASE_CORE_MASK),
1123 vcpu->arch.gprs[rt]);
1124 kvm_err("MTCz, cop0->reg[EBASE]: %#lx\n",
1125 kvm_read_c0_guest_ebase(cop0));
1126 } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
1128 vcpu->arch.gprs[rt] & KVM_ENTRYHI_ASID;
1129 if ((KSEGX(vcpu->arch.gprs[rt]) != CKSEG0) &&
1130 ((kvm_read_c0_guest_entryhi(cop0) &
1131 KVM_ENTRYHI_ASID) != nasid)) {
1132 trace_kvm_asid_change(vcpu,
1133 kvm_read_c0_guest_entryhi(cop0)
1137 /* Blow away the shadow host TLBs */
1138 kvm_mips_flush_host_tlb(1);
1140 kvm_write_c0_guest_entryhi(cop0,
1141 vcpu->arch.gprs[rt]);
1143 /* Are we writing to COUNT */
1144 else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
1145 kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
1147 } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
1148 /* If we are writing to COMPARE */
1149 /* Clear pending timer interrupt, if any */
1150 kvm_mips_write_compare(vcpu,
1151 vcpu->arch.gprs[rt],
1153 } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
1154 unsigned int old_val, val, change;
1156 old_val = kvm_read_c0_guest_status(cop0);
1157 val = vcpu->arch.gprs[rt];
1158 change = val ^ old_val;
1160 /* Make sure that the NMI bit is never set */
1164 * Don't allow CU1 or FR to be set unless FPU
1165 * capability enabled and exists in guest
1168 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1169 val &= ~(ST0_CU1 | ST0_FR);
1172 * Also don't allow FR to be set if host doesn't
1175 if (!(current_cpu_data.fpu_id & MIPS_FPIR_F64))
1179 /* Handle changes in FPU mode */
1183 * FPU and Vector register state is made
1184 * UNPREDICTABLE by a change of FR, so don't
1185 * even bother saving it.
1187 if (change & ST0_FR)
1191 * If MSA state is already live, it is undefined
1192 * how it interacts with FR=0 FPU state, and we
1193 * don't want to hit reserved instruction
1194 * exceptions trying to save the MSA state later
1195 * when CU=1 && FR=1, so play it safe and save
1198 if (change & ST0_CU1 && !(val & ST0_FR) &&
1199 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
1203 * Propagate CU1 (FPU enable) changes
1204 * immediately if the FPU context is already
1205 * loaded. When disabling we leave the context
1206 * loaded so it can be quickly enabled again in
1209 if (change & ST0_CU1 &&
1210 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
1211 change_c0_status(ST0_CU1, val);
1215 kvm_write_c0_guest_status(cop0, val);
1217 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1219 * If FPU present, we need CU1/FR bits to take
1220 * effect fairly soon.
1222 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1223 kvm_mips_trans_mtc0(inst, opc, vcpu);
1225 } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
1226 unsigned int old_val, val, change, wrmask;
1228 old_val = kvm_read_c0_guest_config5(cop0);
1229 val = vcpu->arch.gprs[rt];
1231 /* Only a few bits are writable in Config5 */
1232 wrmask = kvm_mips_config5_wrmask(vcpu);
1233 change = (val ^ old_val) & wrmask;
1234 val = old_val ^ change;
1237 /* Handle changes in FPU/MSA modes */
1241 * Propagate FRE changes immediately if the FPU
1242 * context is already loaded.
1244 if (change & MIPS_CONF5_FRE &&
1245 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
1246 change_c0_config5(MIPS_CONF5_FRE, val);
1249 * Propagate MSAEn changes immediately if the
1250 * MSA context is already loaded. When disabling
1251 * we leave the context loaded so it can be
1252 * quickly enabled again in the near future.
1254 if (change & MIPS_CONF5_MSAEN &&
1255 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
1256 change_c0_config5(MIPS_CONF5_MSAEN,
1261 kvm_write_c0_guest_config5(cop0, val);
1262 } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
1263 u32 old_cause, new_cause;
1265 old_cause = kvm_read_c0_guest_cause(cop0);
1266 new_cause = vcpu->arch.gprs[rt];
1267 /* Update R/W bits */
1268 kvm_change_c0_guest_cause(cop0, 0x08800300,
1270 /* DC bit enabling/disabling timer? */
1271 if ((old_cause ^ new_cause) & CAUSEF_DC) {
1272 if (new_cause & CAUSEF_DC)
1273 kvm_mips_count_disable_cause(vcpu);
1275 kvm_mips_count_enable_cause(vcpu);
1277 } else if ((rd == MIPS_CP0_HWRENA) && (sel == 0)) {
1278 u32 mask = MIPS_HWRENA_CPUNUM |
1279 MIPS_HWRENA_SYNCISTEP |
1283 if (kvm_read_c0_guest_config3(cop0) &
1285 mask |= MIPS_HWRENA_ULR;
1286 cop0->reg[rd][sel] = vcpu->arch.gprs[rt] & mask;
1288 cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
1289 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1290 kvm_mips_trans_mtc0(inst, opc, vcpu);
1296 kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
1297 vcpu->arch.pc, rt, rd, sel);
1298 trace_kvm_hwr(vcpu, KVM_TRACE_DMTC0,
1299 KVM_TRACE_COP0(rd, sel),
1300 vcpu->arch.gprs[rt]);
1305 #ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
1306 cop0->stat[MIPS_CP0_STATUS][0]++;
1309 vcpu->arch.gprs[rt] =
1310 kvm_read_c0_guest_status(cop0);
1312 if (inst.mfmc0_format.sc) {
1313 kvm_debug("[%#lx] mfmc0_op: EI\n",
1315 kvm_set_c0_guest_status(cop0, ST0_IE);
1317 kvm_debug("[%#lx] mfmc0_op: DI\n",
1319 kvm_clear_c0_guest_status(cop0, ST0_IE);
1326 u32 css = cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
1328 (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
1330 * We don't support any shadow register sets, so
1331 * SRSCtl[PSS] == SRSCtl[CSS] = 0
1337 kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
1338 vcpu->arch.gprs[rt]);
1339 vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
1343 kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
1344 vcpu->arch.pc, inst.c0r_format.rs);
1351 /* Rollback PC only if emulation was unsuccessful */
1352 if (er == EMULATE_FAIL)
1353 vcpu->arch.pc = curr_pc;
1357 * This is for special instructions whose emulation
1358 * updates the PC, so do not overwrite the PC under
1365 enum emulation_result kvm_mips_emulate_store(union mips_instruction inst,
1367 struct kvm_run *run,
1368 struct kvm_vcpu *vcpu)
1370 enum emulation_result er = EMULATE_DO_MMIO;
1373 void *data = run->mmio.data;
1374 unsigned long curr_pc;
1377 * Update PC and hold onto current PC in case there is
1378 * an error and we want to rollback the PC
1380 curr_pc = vcpu->arch.pc;
1381 er = update_pc(vcpu, cause);
1382 if (er == EMULATE_FAIL)
1385 rt = inst.i_format.rt;
1387 switch (inst.i_format.opcode) {
1390 if (bytes > sizeof(run->mmio.data)) {
1391 kvm_err("%s: bad MMIO length: %d\n", __func__,
1394 run->mmio.phys_addr =
1395 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1397 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1401 run->mmio.len = bytes;
1402 run->mmio.is_write = 1;
1403 vcpu->mmio_needed = 1;
1404 vcpu->mmio_is_write = 1;
1405 *(u8 *) data = vcpu->arch.gprs[rt];
1406 kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1407 vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt],
1414 if (bytes > sizeof(run->mmio.data)) {
1415 kvm_err("%s: bad MMIO length: %d\n", __func__,
1418 run->mmio.phys_addr =
1419 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1421 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1426 run->mmio.len = bytes;
1427 run->mmio.is_write = 1;
1428 vcpu->mmio_needed = 1;
1429 vcpu->mmio_is_write = 1;
1430 *(u32 *) data = vcpu->arch.gprs[rt];
1432 kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1433 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
1434 vcpu->arch.gprs[rt], *(u32 *) data);
1439 if (bytes > sizeof(run->mmio.data)) {
1440 kvm_err("%s: bad MMIO length: %d\n", __func__,
1443 run->mmio.phys_addr =
1444 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1446 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1451 run->mmio.len = bytes;
1452 run->mmio.is_write = 1;
1453 vcpu->mmio_needed = 1;
1454 vcpu->mmio_is_write = 1;
1455 *(u16 *) data = vcpu->arch.gprs[rt];
1457 kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1458 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
1459 vcpu->arch.gprs[rt], *(u32 *) data);
1463 kvm_err("Store not yet supported (inst=0x%08x)\n",
1469 /* Rollback PC if emulation was unsuccessful */
1470 if (er == EMULATE_FAIL)
1471 vcpu->arch.pc = curr_pc;
1476 enum emulation_result kvm_mips_emulate_load(union mips_instruction inst,
1477 u32 cause, struct kvm_run *run,
1478 struct kvm_vcpu *vcpu)
1480 enum emulation_result er = EMULATE_DO_MMIO;
1484 rt = inst.i_format.rt;
1485 op = inst.i_format.opcode;
1487 vcpu->arch.pending_load_cause = cause;
1488 vcpu->arch.io_gpr = rt;
1493 if (bytes > sizeof(run->mmio.data)) {
1494 kvm_err("%s: bad MMIO length: %d\n", __func__,
1499 run->mmio.phys_addr =
1500 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1502 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1507 run->mmio.len = bytes;
1508 run->mmio.is_write = 0;
1509 vcpu->mmio_needed = 1;
1510 vcpu->mmio_is_write = 0;
1516 if (bytes > sizeof(run->mmio.data)) {
1517 kvm_err("%s: bad MMIO length: %d\n", __func__,
1522 run->mmio.phys_addr =
1523 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1525 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1530 run->mmio.len = bytes;
1531 run->mmio.is_write = 0;
1532 vcpu->mmio_needed = 1;
1533 vcpu->mmio_is_write = 0;
1536 vcpu->mmio_needed = 2;
1538 vcpu->mmio_needed = 1;
1545 if (bytes > sizeof(run->mmio.data)) {
1546 kvm_err("%s: bad MMIO length: %d\n", __func__,
1551 run->mmio.phys_addr =
1552 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1554 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1559 run->mmio.len = bytes;
1560 run->mmio.is_write = 0;
1561 vcpu->mmio_is_write = 0;
1564 vcpu->mmio_needed = 2;
1566 vcpu->mmio_needed = 1;
1571 kvm_err("Load not yet supported (inst=0x%08x)\n",
1580 enum emulation_result kvm_mips_emulate_cache(union mips_instruction inst,
1581 u32 *opc, u32 cause,
1582 struct kvm_run *run,
1583 struct kvm_vcpu *vcpu)
1585 struct mips_coproc *cop0 = vcpu->arch.cop0;
1586 enum emulation_result er = EMULATE_DONE;
1587 u32 cache, op_inst, op, base;
1589 struct kvm_vcpu_arch *arch = &vcpu->arch;
1591 unsigned long curr_pc;
1594 * Update PC and hold onto current PC in case there is
1595 * an error and we want to rollback the PC
1597 curr_pc = vcpu->arch.pc;
1598 er = update_pc(vcpu, cause);
1599 if (er == EMULATE_FAIL)
1602 base = inst.i_format.rs;
1603 op_inst = inst.i_format.rt;
1604 offset = inst.i_format.simmediate;
1605 cache = op_inst & CacheOp_Cache;
1606 op = op_inst & CacheOp_Op;
1608 va = arch->gprs[base] + offset;
1610 kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1611 cache, op, base, arch->gprs[base], offset);
1614 * Treat INDEX_INV as a nop, basically issued by Linux on startup to
1615 * invalidate the caches entirely by stepping through all the
1618 if (op == Index_Writeback_Inv) {
1619 kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1620 vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
1621 arch->gprs[base], offset);
1623 if (cache == Cache_D)
1625 else if (cache == Cache_I)
1628 kvm_err("%s: unsupported CACHE INDEX operation\n",
1630 return EMULATE_FAIL;
1633 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1634 kvm_mips_trans_cache_index(inst, opc, vcpu);
1640 if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
1641 if (kvm_mips_host_tlb_lookup(vcpu, va) < 0)
1642 kvm_mips_handle_kseg0_tlb_fault(va, vcpu);
1643 } else if ((KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0) ||
1644 KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) {
1647 /* If an entry already exists then skip */
1648 if (kvm_mips_host_tlb_lookup(vcpu, va) >= 0)
1652 * If address not in the guest TLB, then give the guest a fault,
1653 * the resulting handler will do the right thing
1655 index = kvm_mips_guest_tlb_lookup(vcpu, (va & VPN2_MASK) |
1656 (kvm_read_c0_guest_entryhi
1657 (cop0) & KVM_ENTRYHI_ASID));
1660 vcpu->arch.host_cp0_badvaddr = va;
1661 vcpu->arch.pc = curr_pc;
1662 er = kvm_mips_emulate_tlbmiss_ld(cause, NULL, run,
1665 goto dont_update_pc;
1667 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
1669 * Check if the entry is valid, if not then setup a TLB
1670 * invalid exception to the guest
1672 if (!TLB_IS_VALID(*tlb, va)) {
1673 vcpu->arch.host_cp0_badvaddr = va;
1674 vcpu->arch.pc = curr_pc;
1675 er = kvm_mips_emulate_tlbinv_ld(cause, NULL,
1678 goto dont_update_pc;
1681 * We fault an entry from the guest tlb to the
1684 kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb);
1688 kvm_err("INVALID CACHE INDEX/ADDRESS (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1689 cache, op, base, arch->gprs[base], offset);
1697 /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
1698 if (op_inst == Hit_Writeback_Inv_D || op_inst == Hit_Invalidate_D) {
1699 flush_dcache_line(va);
1701 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1703 * Replace the CACHE instruction, with a SYNCI, not the same,
1706 kvm_mips_trans_cache_va(inst, opc, vcpu);
1708 } else if (op_inst == Hit_Invalidate_I) {
1709 flush_dcache_line(va);
1710 flush_icache_line(va);
1712 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1713 /* Replace the CACHE instruction, with a SYNCI */
1714 kvm_mips_trans_cache_va(inst, opc, vcpu);
1717 kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1718 cache, op, base, arch->gprs[base], offset);
1724 /* Rollback PC only if emulation was unsuccessful */
1725 if (er == EMULATE_FAIL)
1726 vcpu->arch.pc = curr_pc;
1730 * This is for exceptions whose emulation updates the PC, so do not
1731 * overwrite the PC under any circumstances
1737 enum emulation_result kvm_mips_emulate_inst(u32 cause, u32 *opc,
1738 struct kvm_run *run,
1739 struct kvm_vcpu *vcpu)
1741 union mips_instruction inst;
1742 enum emulation_result er = EMULATE_DONE;
1744 /* Fetch the instruction. */
1745 if (cause & CAUSEF_BD)
1748 inst.word = kvm_get_inst(opc, vcpu);
1750 switch (inst.r_format.opcode) {
1752 er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
1757 er = kvm_mips_emulate_store(inst, cause, run, vcpu);
1764 er = kvm_mips_emulate_load(inst, cause, run, vcpu);
1768 ++vcpu->stat.cache_exits;
1769 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
1770 er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
1774 kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
1776 kvm_arch_vcpu_dump_regs(vcpu);
1784 enum emulation_result kvm_mips_emulate_syscall(u32 cause,
1786 struct kvm_run *run,
1787 struct kvm_vcpu *vcpu)
1789 struct mips_coproc *cop0 = vcpu->arch.cop0;
1790 struct kvm_vcpu_arch *arch = &vcpu->arch;
1791 enum emulation_result er = EMULATE_DONE;
1793 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1795 kvm_write_c0_guest_epc(cop0, arch->pc);
1796 kvm_set_c0_guest_status(cop0, ST0_EXL);
1798 if (cause & CAUSEF_BD)
1799 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1801 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1803 kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
1805 kvm_change_c0_guest_cause(cop0, (0xff),
1806 (EXCCODE_SYS << CAUSEB_EXCCODE));
1808 /* Set PC to the exception entry point */
1809 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1812 kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
1819 enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause,
1821 struct kvm_run *run,
1822 struct kvm_vcpu *vcpu)
1824 struct mips_coproc *cop0 = vcpu->arch.cop0;
1825 struct kvm_vcpu_arch *arch = &vcpu->arch;
1826 unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
1827 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
1829 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1831 kvm_write_c0_guest_epc(cop0, arch->pc);
1832 kvm_set_c0_guest_status(cop0, ST0_EXL);
1834 if (cause & CAUSEF_BD)
1835 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1837 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1839 kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
1842 /* set pc to the exception entry point */
1843 arch->pc = KVM_GUEST_KSEG0 + 0x0;
1846 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
1849 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1852 kvm_change_c0_guest_cause(cop0, (0xff),
1853 (EXCCODE_TLBL << CAUSEB_EXCCODE));
1855 /* setup badvaddr, context and entryhi registers for the guest */
1856 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1857 /* XXXKYMA: is the context register used by linux??? */
1858 kvm_write_c0_guest_entryhi(cop0, entryhi);
1859 /* Blow away the shadow host TLBs */
1860 kvm_mips_flush_host_tlb(1);
1862 return EMULATE_DONE;
1865 enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause,
1867 struct kvm_run *run,
1868 struct kvm_vcpu *vcpu)
1870 struct mips_coproc *cop0 = vcpu->arch.cop0;
1871 struct kvm_vcpu_arch *arch = &vcpu->arch;
1872 unsigned long entryhi =
1873 (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
1874 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
1876 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1878 kvm_write_c0_guest_epc(cop0, arch->pc);
1879 kvm_set_c0_guest_status(cop0, ST0_EXL);
1881 if (cause & CAUSEF_BD)
1882 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1884 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1886 kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
1889 /* set pc to the exception entry point */
1890 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1893 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
1895 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1898 kvm_change_c0_guest_cause(cop0, (0xff),
1899 (EXCCODE_TLBL << CAUSEB_EXCCODE));
1901 /* setup badvaddr, context and entryhi registers for the guest */
1902 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1903 /* XXXKYMA: is the context register used by linux??? */
1904 kvm_write_c0_guest_entryhi(cop0, entryhi);
1905 /* Blow away the shadow host TLBs */
1906 kvm_mips_flush_host_tlb(1);
1908 return EMULATE_DONE;
1911 enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause,
1913 struct kvm_run *run,
1914 struct kvm_vcpu *vcpu)
1916 struct mips_coproc *cop0 = vcpu->arch.cop0;
1917 struct kvm_vcpu_arch *arch = &vcpu->arch;
1918 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
1919 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
1921 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1923 kvm_write_c0_guest_epc(cop0, arch->pc);
1924 kvm_set_c0_guest_status(cop0, ST0_EXL);
1926 if (cause & CAUSEF_BD)
1927 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1929 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1931 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
1934 /* Set PC to the exception entry point */
1935 arch->pc = KVM_GUEST_KSEG0 + 0x0;
1937 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
1939 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1942 kvm_change_c0_guest_cause(cop0, (0xff),
1943 (EXCCODE_TLBS << CAUSEB_EXCCODE));
1945 /* setup badvaddr, context and entryhi registers for the guest */
1946 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1947 /* XXXKYMA: is the context register used by linux??? */
1948 kvm_write_c0_guest_entryhi(cop0, entryhi);
1949 /* Blow away the shadow host TLBs */
1950 kvm_mips_flush_host_tlb(1);
1952 return EMULATE_DONE;
1955 enum emulation_result kvm_mips_emulate_tlbinv_st(u32 cause,
1957 struct kvm_run *run,
1958 struct kvm_vcpu *vcpu)
1960 struct mips_coproc *cop0 = vcpu->arch.cop0;
1961 struct kvm_vcpu_arch *arch = &vcpu->arch;
1962 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
1963 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
1965 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1967 kvm_write_c0_guest_epc(cop0, arch->pc);
1968 kvm_set_c0_guest_status(cop0, ST0_EXL);
1970 if (cause & CAUSEF_BD)
1971 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1973 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1975 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
1978 /* Set PC to the exception entry point */
1979 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1981 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
1983 arch->pc = KVM_GUEST_KSEG0 + 0x180;
1986 kvm_change_c0_guest_cause(cop0, (0xff),
1987 (EXCCODE_TLBS << CAUSEB_EXCCODE));
1989 /* setup badvaddr, context and entryhi registers for the guest */
1990 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
1991 /* XXXKYMA: is the context register used by linux??? */
1992 kvm_write_c0_guest_entryhi(cop0, entryhi);
1993 /* Blow away the shadow host TLBs */
1994 kvm_mips_flush_host_tlb(1);
1996 return EMULATE_DONE;
1999 /* TLBMOD: store into address matching TLB with Dirty bit off */
2000 enum emulation_result kvm_mips_handle_tlbmod(u32 cause, u32 *opc,
2001 struct kvm_run *run,
2002 struct kvm_vcpu *vcpu)
2004 enum emulation_result er = EMULATE_DONE;
2006 struct mips_coproc *cop0 = vcpu->arch.cop0;
2007 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
2008 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
2011 /* If address not in the guest TLB, then we are in trouble */
2012 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
2014 /* XXXKYMA Invalidate and retry */
2015 kvm_mips_host_tlb_inv(vcpu, vcpu->arch.host_cp0_badvaddr);
2016 kvm_err("%s: host got TLBMOD for %#lx but entry not present in Guest TLB\n",
2018 kvm_mips_dump_guest_tlbs(vcpu);
2019 kvm_mips_dump_host_tlbs();
2020 return EMULATE_FAIL;
2024 er = kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
2028 enum emulation_result kvm_mips_emulate_tlbmod(u32 cause,
2030 struct kvm_run *run,
2031 struct kvm_vcpu *vcpu)
2033 struct mips_coproc *cop0 = vcpu->arch.cop0;
2034 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
2035 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
2036 struct kvm_vcpu_arch *arch = &vcpu->arch;
2038 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2040 kvm_write_c0_guest_epc(cop0, arch->pc);
2041 kvm_set_c0_guest_status(cop0, ST0_EXL);
2043 if (cause & CAUSEF_BD)
2044 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2046 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2048 kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
2051 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2053 kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
2055 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2058 kvm_change_c0_guest_cause(cop0, (0xff),
2059 (EXCCODE_MOD << CAUSEB_EXCCODE));
2061 /* setup badvaddr, context and entryhi registers for the guest */
2062 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2063 /* XXXKYMA: is the context register used by linux??? */
2064 kvm_write_c0_guest_entryhi(cop0, entryhi);
2065 /* Blow away the shadow host TLBs */
2066 kvm_mips_flush_host_tlb(1);
2068 return EMULATE_DONE;
2071 enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause,
2073 struct kvm_run *run,
2074 struct kvm_vcpu *vcpu)
2076 struct mips_coproc *cop0 = vcpu->arch.cop0;
2077 struct kvm_vcpu_arch *arch = &vcpu->arch;
2079 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2081 kvm_write_c0_guest_epc(cop0, arch->pc);
2082 kvm_set_c0_guest_status(cop0, ST0_EXL);
2084 if (cause & CAUSEF_BD)
2085 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2087 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2091 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2093 kvm_change_c0_guest_cause(cop0, (0xff),
2094 (EXCCODE_CPU << CAUSEB_EXCCODE));
2095 kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
2097 return EMULATE_DONE;
2100 enum emulation_result kvm_mips_emulate_ri_exc(u32 cause,
2102 struct kvm_run *run,
2103 struct kvm_vcpu *vcpu)
2105 struct mips_coproc *cop0 = vcpu->arch.cop0;
2106 struct kvm_vcpu_arch *arch = &vcpu->arch;
2107 enum emulation_result er = EMULATE_DONE;
2109 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2111 kvm_write_c0_guest_epc(cop0, arch->pc);
2112 kvm_set_c0_guest_status(cop0, ST0_EXL);
2114 if (cause & CAUSEF_BD)
2115 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2117 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2119 kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
2121 kvm_change_c0_guest_cause(cop0, (0xff),
2122 (EXCCODE_RI << CAUSEB_EXCCODE));
2124 /* Set PC to the exception entry point */
2125 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2128 kvm_err("Trying to deliver RI when EXL is already set\n");
2135 enum emulation_result kvm_mips_emulate_bp_exc(u32 cause,
2137 struct kvm_run *run,
2138 struct kvm_vcpu *vcpu)
2140 struct mips_coproc *cop0 = vcpu->arch.cop0;
2141 struct kvm_vcpu_arch *arch = &vcpu->arch;
2142 enum emulation_result er = EMULATE_DONE;
2144 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2146 kvm_write_c0_guest_epc(cop0, arch->pc);
2147 kvm_set_c0_guest_status(cop0, ST0_EXL);
2149 if (cause & CAUSEF_BD)
2150 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2152 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2154 kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
2156 kvm_change_c0_guest_cause(cop0, (0xff),
2157 (EXCCODE_BP << CAUSEB_EXCCODE));
2159 /* Set PC to the exception entry point */
2160 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2163 kvm_err("Trying to deliver BP when EXL is already set\n");
2170 enum emulation_result kvm_mips_emulate_trap_exc(u32 cause,
2172 struct kvm_run *run,
2173 struct kvm_vcpu *vcpu)
2175 struct mips_coproc *cop0 = vcpu->arch.cop0;
2176 struct kvm_vcpu_arch *arch = &vcpu->arch;
2177 enum emulation_result er = EMULATE_DONE;
2179 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2181 kvm_write_c0_guest_epc(cop0, arch->pc);
2182 kvm_set_c0_guest_status(cop0, ST0_EXL);
2184 if (cause & CAUSEF_BD)
2185 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2187 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2189 kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc);
2191 kvm_change_c0_guest_cause(cop0, (0xff),
2192 (EXCCODE_TR << CAUSEB_EXCCODE));
2194 /* Set PC to the exception entry point */
2195 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2198 kvm_err("Trying to deliver TRAP when EXL is already set\n");
2205 enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause,
2207 struct kvm_run *run,
2208 struct kvm_vcpu *vcpu)
2210 struct mips_coproc *cop0 = vcpu->arch.cop0;
2211 struct kvm_vcpu_arch *arch = &vcpu->arch;
2212 enum emulation_result er = EMULATE_DONE;
2214 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2216 kvm_write_c0_guest_epc(cop0, arch->pc);
2217 kvm_set_c0_guest_status(cop0, ST0_EXL);
2219 if (cause & CAUSEF_BD)
2220 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2222 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2224 kvm_debug("Delivering MSAFPE @ pc %#lx\n", arch->pc);
2226 kvm_change_c0_guest_cause(cop0, (0xff),
2227 (EXCCODE_MSAFPE << CAUSEB_EXCCODE));
2229 /* Set PC to the exception entry point */
2230 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2233 kvm_err("Trying to deliver MSAFPE when EXL is already set\n");
2240 enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause,
2242 struct kvm_run *run,
2243 struct kvm_vcpu *vcpu)
2245 struct mips_coproc *cop0 = vcpu->arch.cop0;
2246 struct kvm_vcpu_arch *arch = &vcpu->arch;
2247 enum emulation_result er = EMULATE_DONE;
2249 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2251 kvm_write_c0_guest_epc(cop0, arch->pc);
2252 kvm_set_c0_guest_status(cop0, ST0_EXL);
2254 if (cause & CAUSEF_BD)
2255 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2257 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2259 kvm_debug("Delivering FPE @ pc %#lx\n", arch->pc);
2261 kvm_change_c0_guest_cause(cop0, (0xff),
2262 (EXCCODE_FPE << CAUSEB_EXCCODE));
2264 /* Set PC to the exception entry point */
2265 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2268 kvm_err("Trying to deliver FPE when EXL is already set\n");
2275 enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause,
2277 struct kvm_run *run,
2278 struct kvm_vcpu *vcpu)
2280 struct mips_coproc *cop0 = vcpu->arch.cop0;
2281 struct kvm_vcpu_arch *arch = &vcpu->arch;
2282 enum emulation_result er = EMULATE_DONE;
2284 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2286 kvm_write_c0_guest_epc(cop0, arch->pc);
2287 kvm_set_c0_guest_status(cop0, ST0_EXL);
2289 if (cause & CAUSEF_BD)
2290 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2292 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2294 kvm_debug("Delivering MSADIS @ pc %#lx\n", arch->pc);
2296 kvm_change_c0_guest_cause(cop0, (0xff),
2297 (EXCCODE_MSADIS << CAUSEB_EXCCODE));
2299 /* Set PC to the exception entry point */
2300 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2303 kvm_err("Trying to deliver MSADIS when EXL is already set\n");
2310 enum emulation_result kvm_mips_handle_ri(u32 cause, u32 *opc,
2311 struct kvm_run *run,
2312 struct kvm_vcpu *vcpu)
2314 struct mips_coproc *cop0 = vcpu->arch.cop0;
2315 struct kvm_vcpu_arch *arch = &vcpu->arch;
2316 enum emulation_result er = EMULATE_DONE;
2317 unsigned long curr_pc;
2318 union mips_instruction inst;
2321 * Update PC and hold onto current PC in case there is
2322 * an error and we want to rollback the PC
2324 curr_pc = vcpu->arch.pc;
2325 er = update_pc(vcpu, cause);
2326 if (er == EMULATE_FAIL)
2329 /* Fetch the instruction. */
2330 if (cause & CAUSEF_BD)
2333 inst.word = kvm_get_inst(opc, vcpu);
2335 if (inst.word == KVM_INVALID_INST) {
2336 kvm_err("%s: Cannot get inst @ %p\n", __func__, opc);
2337 return EMULATE_FAIL;
2340 if (inst.r_format.opcode == spec3_op &&
2341 inst.r_format.func == rdhwr_op) {
2342 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
2343 int rd = inst.r_format.rd;
2344 int rt = inst.r_format.rt;
2345 int sel = inst.r_format.re & 0x7;
2347 /* If usermode, check RDHWR rd is allowed by guest HWREna */
2348 if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
2349 kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
2354 case MIPS_HWR_CPUNUM: /* CPU number */
2355 arch->gprs[rt] = vcpu->vcpu_id;
2357 case MIPS_HWR_SYNCISTEP: /* SYNCI length */
2358 arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
2359 current_cpu_data.icache.linesz);
2361 case MIPS_HWR_CC: /* Read count register */
2362 arch->gprs[rt] = kvm_mips_read_count(vcpu);
2364 case MIPS_HWR_CCRES: /* Count register resolution */
2365 switch (current_cpu_data.cputype) {
2374 case MIPS_HWR_ULR: /* Read UserLocal register */
2375 arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
2379 kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
2383 trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR, KVM_TRACE_HWR(rd, sel),
2384 vcpu->arch.gprs[rt]);
2386 kvm_debug("Emulate RI not supported @ %p: %#x\n",
2391 return EMULATE_DONE;
2395 * Rollback PC (if in branch delay slot then the PC already points to
2396 * branch target), and pass the RI exception to the guest OS.
2398 vcpu->arch.pc = curr_pc;
2399 return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
2402 enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
2403 struct kvm_run *run)
2405 unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
2406 enum emulation_result er = EMULATE_DONE;
2408 if (run->mmio.len > sizeof(*gpr)) {
2409 kvm_err("Bad MMIO length: %d", run->mmio.len);
2414 er = update_pc(vcpu, vcpu->arch.pending_load_cause);
2415 if (er == EMULATE_FAIL)
2418 switch (run->mmio.len) {
2420 *gpr = *(s32 *) run->mmio.data;
2424 if (vcpu->mmio_needed == 2)
2425 *gpr = *(s16 *) run->mmio.data;
2427 *gpr = *(u16 *)run->mmio.data;
2431 if (vcpu->mmio_needed == 2)
2432 *gpr = *(s8 *) run->mmio.data;
2434 *gpr = *(u8 *) run->mmio.data;
2438 if (vcpu->arch.pending_load_cause & CAUSEF_BD)
2439 kvm_debug("[%#lx] Completing %d byte BD Load to gpr %d (0x%08lx) type %d\n",
2440 vcpu->arch.pc, run->mmio.len, vcpu->arch.io_gpr, *gpr,
2447 static enum emulation_result kvm_mips_emulate_exc(u32 cause,
2449 struct kvm_run *run,
2450 struct kvm_vcpu *vcpu)
2452 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
2453 struct mips_coproc *cop0 = vcpu->arch.cop0;
2454 struct kvm_vcpu_arch *arch = &vcpu->arch;
2455 enum emulation_result er = EMULATE_DONE;
2457 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2459 kvm_write_c0_guest_epc(cop0, arch->pc);
2460 kvm_set_c0_guest_status(cop0, ST0_EXL);
2462 if (cause & CAUSEF_BD)
2463 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2465 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2467 kvm_change_c0_guest_cause(cop0, (0xff),
2468 (exccode << CAUSEB_EXCCODE));
2470 /* Set PC to the exception entry point */
2471 arch->pc = KVM_GUEST_KSEG0 + 0x180;
2472 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2474 kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
2475 exccode, kvm_read_c0_guest_epc(cop0),
2476 kvm_read_c0_guest_badvaddr(cop0));
2478 kvm_err("Trying to deliver EXC when EXL is already set\n");
2485 enum emulation_result kvm_mips_check_privilege(u32 cause,
2487 struct kvm_run *run,
2488 struct kvm_vcpu *vcpu)
2490 enum emulation_result er = EMULATE_DONE;
2491 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
2492 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
2494 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
2503 case EXCCODE_MSAFPE:
2505 case EXCCODE_MSADIS:
2509 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
2510 er = EMULATE_PRIV_FAIL;
2518 * We we are accessing Guest kernel space, then send an
2519 * address error exception to the guest
2521 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
2522 kvm_debug("%s: LD MISS @ %#lx\n", __func__,
2525 cause |= (EXCCODE_ADEL << CAUSEB_EXCCODE);
2526 er = EMULATE_PRIV_FAIL;
2532 * We we are accessing Guest kernel space, then send an
2533 * address error exception to the guest
2535 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
2536 kvm_debug("%s: ST MISS @ %#lx\n", __func__,
2539 cause |= (EXCCODE_ADES << CAUSEB_EXCCODE);
2540 er = EMULATE_PRIV_FAIL;
2545 kvm_debug("%s: address error ST @ %#lx\n", __func__,
2547 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2549 cause |= (EXCCODE_TLBS << CAUSEB_EXCCODE);
2551 er = EMULATE_PRIV_FAIL;
2554 kvm_debug("%s: address error LD @ %#lx\n", __func__,
2556 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2558 cause |= (EXCCODE_TLBL << CAUSEB_EXCCODE);
2560 er = EMULATE_PRIV_FAIL;
2563 er = EMULATE_PRIV_FAIL;
2568 if (er == EMULATE_PRIV_FAIL)
2569 kvm_mips_emulate_exc(cause, opc, run, vcpu);
2575 * User Address (UA) fault, this could happen if
2576 * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
2577 * case we pass on the fault to the guest kernel and let it handle it.
2578 * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
2579 * case we inject the TLB from the Guest TLB into the shadow host TLB
2581 enum emulation_result kvm_mips_handle_tlbmiss(u32 cause,
2583 struct kvm_run *run,
2584 struct kvm_vcpu *vcpu)
2586 enum emulation_result er = EMULATE_DONE;
2587 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
2588 unsigned long va = vcpu->arch.host_cp0_badvaddr;
2591 kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx\n",
2592 vcpu->arch.host_cp0_badvaddr);
2595 * KVM would not have got the exception if this entry was valid in the
2596 * shadow host TLB. Check the Guest TLB, if the entry is not there then
2597 * send the guest an exception. The guest exc handler should then inject
2598 * an entry into the guest TLB.
2600 index = kvm_mips_guest_tlb_lookup(vcpu,
2602 (kvm_read_c0_guest_entryhi(vcpu->arch.cop0) &
2605 if (exccode == EXCCODE_TLBL) {
2606 er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
2607 } else if (exccode == EXCCODE_TLBS) {
2608 er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
2610 kvm_err("%s: invalid exc code: %d\n", __func__,
2615 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
2618 * Check if the entry is valid, if not then setup a TLB invalid
2619 * exception to the guest
2621 if (!TLB_IS_VALID(*tlb, va)) {
2622 if (exccode == EXCCODE_TLBL) {
2623 er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
2625 } else if (exccode == EXCCODE_TLBS) {
2626 er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
2629 kvm_err("%s: invalid exc code: %d\n", __func__,
2634 kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
2635 tlb->tlb_hi, tlb->tlb_lo[0], tlb->tlb_lo[1]);
2637 * OK we have a Guest TLB entry, now inject it into the
2640 kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb);