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/vmalloc.h>
18 #include <linux/bootmem.h>
19 #include <linux/random.h>
21 #include <asm/cacheflush.h>
22 #include <asm/cacheops.h>
23 #include <asm/cpu-info.h>
24 #include <asm/mmu_context.h>
25 #include <asm/tlbflush.h>
29 #include <asm/r4kcache.h>
30 #define CONFIG_MIPS_MT
32 #include "interrupt.h"
38 * Compute the return address and do emulate branch simulation, if required.
39 * This function should be called only in branch delay slot active.
41 static int kvm_compute_return_epc(struct kvm_vcpu *vcpu, unsigned long instpc,
44 unsigned int dspcontrol;
45 union mips_instruction insn;
46 struct kvm_vcpu_arch *arch = &vcpu->arch;
52 kvm_err("%s: unaligned epc\n", __func__);
56 /* Read the instruction */
57 err = kvm_get_badinstrp((u32 *)epc, vcpu, &insn.word);
61 switch (insn.i_format.opcode) {
62 /* jr and jalr are in r_format format. */
64 switch (insn.r_format.func) {
66 arch->gprs[insn.r_format.rd] = epc + 8;
69 nextpc = arch->gprs[insn.r_format.rs];
77 * This group contains:
78 * bltz_op, bgez_op, bltzl_op, bgezl_op,
79 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
82 switch (insn.i_format.rt) {
85 if ((long)arch->gprs[insn.i_format.rs] < 0)
86 epc = epc + 4 + (insn.i_format.simmediate << 2);
94 if ((long)arch->gprs[insn.i_format.rs] >= 0)
95 epc = epc + 4 + (insn.i_format.simmediate << 2);
103 arch->gprs[31] = epc + 8;
104 if ((long)arch->gprs[insn.i_format.rs] < 0)
105 epc = epc + 4 + (insn.i_format.simmediate << 2);
113 arch->gprs[31] = epc + 8;
114 if ((long)arch->gprs[insn.i_format.rs] >= 0)
115 epc = epc + 4 + (insn.i_format.simmediate << 2);
122 kvm_err("%s: DSP branch but not DSP ASE\n",
127 dspcontrol = rddsp(0x01);
129 if (dspcontrol >= 32)
130 epc = epc + 4 + (insn.i_format.simmediate << 2);
140 /* These are unconditional and in j_format. */
142 arch->gprs[31] = instpc + 8;
147 epc |= (insn.j_format.target << 2);
151 /* These are conditional and in i_format. */
154 if (arch->gprs[insn.i_format.rs] ==
155 arch->gprs[insn.i_format.rt])
156 epc = epc + 4 + (insn.i_format.simmediate << 2);
164 if (arch->gprs[insn.i_format.rs] !=
165 arch->gprs[insn.i_format.rt])
166 epc = epc + 4 + (insn.i_format.simmediate << 2);
172 case blez_op: /* POP06 */
173 #ifndef CONFIG_CPU_MIPSR6
174 case blezl_op: /* removed in R6 */
176 if (insn.i_format.rt != 0)
178 if ((long)arch->gprs[insn.i_format.rs] <= 0)
179 epc = epc + 4 + (insn.i_format.simmediate << 2);
185 case bgtz_op: /* POP07 */
186 #ifndef CONFIG_CPU_MIPSR6
187 case bgtzl_op: /* removed in R6 */
189 if (insn.i_format.rt != 0)
191 if ((long)arch->gprs[insn.i_format.rs] > 0)
192 epc = epc + 4 + (insn.i_format.simmediate << 2);
198 /* And now the FPA/cp1 branch instructions. */
200 kvm_err("%s: unsupported cop1_op\n", __func__);
203 #ifdef CONFIG_CPU_MIPSR6
204 /* R6 added the following compact branches with forbidden slots */
205 case blezl_op: /* POP26 */
206 case bgtzl_op: /* POP27 */
207 /* only rt == 0 isn't compact branch */
208 if (insn.i_format.rt != 0)
213 /* only rs == rt == 0 is reserved, rest are compact branches */
214 if (insn.i_format.rs != 0 || insn.i_format.rt != 0)
219 /* only rs == 0 isn't compact branch */
220 if (insn.i_format.rs != 0)
225 * If we've hit an exception on the forbidden slot, then
226 * the branch must not have been taken.
233 /* Fall through - Compact branches not supported before R6 */
243 enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause)
247 if (cause & CAUSEF_BD) {
248 err = kvm_compute_return_epc(vcpu, vcpu->arch.pc,
256 kvm_debug("update_pc(): New PC: %#lx\n", vcpu->arch.pc);
262 * kvm_get_badinstr() - Get bad instruction encoding.
263 * @opc: Guest pointer to faulting instruction.
264 * @vcpu: KVM VCPU information.
266 * Gets the instruction encoding of the faulting instruction, using the saved
267 * BadInstr register value if it exists, otherwise falling back to reading guest
270 * Returns: The instruction encoding of the faulting instruction.
272 int kvm_get_badinstr(u32 *opc, struct kvm_vcpu *vcpu, u32 *out)
274 if (cpu_has_badinstr) {
275 *out = vcpu->arch.host_cp0_badinstr;
278 return kvm_get_inst(opc, vcpu, out);
283 * kvm_get_badinstrp() - Get bad prior instruction encoding.
284 * @opc: Guest pointer to prior faulting instruction.
285 * @vcpu: KVM VCPU information.
287 * Gets the instruction encoding of the prior faulting instruction (the branch
288 * containing the delay slot which faulted), using the saved BadInstrP register
289 * value if it exists, otherwise falling back to reading guest memory at @opc.
291 * Returns: The instruction encoding of the prior faulting instruction.
293 int kvm_get_badinstrp(u32 *opc, struct kvm_vcpu *vcpu, u32 *out)
295 if (cpu_has_badinstrp) {
296 *out = vcpu->arch.host_cp0_badinstrp;
299 return kvm_get_inst(opc, vcpu, out);
304 * kvm_mips_count_disabled() - Find whether the CP0_Count timer is disabled.
305 * @vcpu: Virtual CPU.
307 * Returns: 1 if the CP0_Count timer is disabled by either the guest
308 * CP0_Cause.DC bit or the count_ctl.DC bit.
309 * 0 otherwise (in which case CP0_Count timer is running).
311 static inline int kvm_mips_count_disabled(struct kvm_vcpu *vcpu)
313 struct mips_coproc *cop0 = vcpu->arch.cop0;
315 return (vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) ||
316 (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC);
320 * kvm_mips_ktime_to_count() - Scale ktime_t to a 32-bit count.
322 * Caches the dynamic nanosecond bias in vcpu->arch.count_dyn_bias.
324 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
326 static u32 kvm_mips_ktime_to_count(struct kvm_vcpu *vcpu, ktime_t now)
331 now_ns = ktime_to_ns(now);
332 delta = now_ns + vcpu->arch.count_dyn_bias;
334 if (delta >= vcpu->arch.count_period) {
335 /* If delta is out of safe range the bias needs adjusting */
336 periods = div64_s64(now_ns, vcpu->arch.count_period);
337 vcpu->arch.count_dyn_bias = -periods * vcpu->arch.count_period;
338 /* Recalculate delta with new bias */
339 delta = now_ns + vcpu->arch.count_dyn_bias;
343 * We've ensured that:
344 * delta < count_period
346 * Therefore the intermediate delta*count_hz will never overflow since
347 * at the boundary condition:
348 * delta = count_period
349 * delta = NSEC_PER_SEC * 2^32 / count_hz
350 * delta * count_hz = NSEC_PER_SEC * 2^32
352 return div_u64(delta * vcpu->arch.count_hz, NSEC_PER_SEC);
356 * kvm_mips_count_time() - Get effective current time.
357 * @vcpu: Virtual CPU.
359 * Get effective monotonic ktime. This is usually a straightforward ktime_get(),
360 * except when the master disable bit is set in count_ctl, in which case it is
361 * count_resume, i.e. the time that the count was disabled.
363 * Returns: Effective monotonic ktime for CP0_Count.
365 static inline ktime_t kvm_mips_count_time(struct kvm_vcpu *vcpu)
367 if (unlikely(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
368 return vcpu->arch.count_resume;
374 * kvm_mips_read_count_running() - Read the current count value as if running.
375 * @vcpu: Virtual CPU.
376 * @now: Kernel time to read CP0_Count at.
378 * Returns the current guest CP0_Count register at time @now and handles if the
379 * timer interrupt is pending and hasn't been handled yet.
381 * Returns: The current value of the guest CP0_Count register.
383 static u32 kvm_mips_read_count_running(struct kvm_vcpu *vcpu, ktime_t now)
385 struct mips_coproc *cop0 = vcpu->arch.cop0;
386 ktime_t expires, threshold;
390 /* Calculate the biased and scaled guest CP0_Count */
391 count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
392 compare = kvm_read_c0_guest_compare(cop0);
395 * Find whether CP0_Count has reached the closest timer interrupt. If
396 * not, we shouldn't inject it.
398 if ((s32)(count - compare) < 0)
402 * The CP0_Count we're going to return has already reached the closest
403 * timer interrupt. Quickly check if it really is a new interrupt by
404 * looking at whether the interval until the hrtimer expiry time is
405 * less than 1/4 of the timer period.
407 expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer);
408 threshold = ktime_add_ns(now, vcpu->arch.count_period / 4);
409 if (ktime_before(expires, threshold)) {
411 * Cancel it while we handle it so there's no chance of
412 * interference with the timeout handler.
414 running = hrtimer_cancel(&vcpu->arch.comparecount_timer);
416 /* Nothing should be waiting on the timeout */
417 kvm_mips_callbacks->queue_timer_int(vcpu);
420 * Restart the timer if it was running based on the expiry time
421 * we read, so that we don't push it back 2 periods.
424 expires = ktime_add_ns(expires,
425 vcpu->arch.count_period);
426 hrtimer_start(&vcpu->arch.comparecount_timer, expires,
435 * kvm_mips_read_count() - Read the current count value.
436 * @vcpu: Virtual CPU.
438 * Read the current guest CP0_Count value, taking into account whether the timer
441 * Returns: The current guest CP0_Count value.
443 u32 kvm_mips_read_count(struct kvm_vcpu *vcpu)
445 struct mips_coproc *cop0 = vcpu->arch.cop0;
447 /* If count disabled just read static copy of count */
448 if (kvm_mips_count_disabled(vcpu))
449 return kvm_read_c0_guest_count(cop0);
451 return kvm_mips_read_count_running(vcpu, ktime_get());
455 * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer.
456 * @vcpu: Virtual CPU.
457 * @count: Output pointer for CP0_Count value at point of freeze.
459 * Freeze the hrtimer safely and return both the ktime and the CP0_Count value
460 * at the point it was frozen. It is guaranteed that any pending interrupts at
461 * the point it was frozen are handled, and none after that point.
463 * This is useful where the time/CP0_Count is needed in the calculation of the
466 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
468 * Returns: The ktime at the point of freeze.
470 static ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu, u32 *count)
474 /* stop hrtimer before finding time */
475 hrtimer_cancel(&vcpu->arch.comparecount_timer);
478 /* find count at this point and handle pending hrtimer */
479 *count = kvm_mips_read_count_running(vcpu, now);
485 * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry.
486 * @vcpu: Virtual CPU.
487 * @now: ktime at point of resume.
488 * @count: CP0_Count at point of resume.
490 * Resumes the timer and updates the timer expiry based on @now and @count.
491 * This can be used in conjunction with kvm_mips_freeze_timer() when timer
492 * parameters need to be changed.
494 * It is guaranteed that a timer interrupt immediately after resume will be
495 * handled, but not if CP_Compare is exactly at @count. That case is already
496 * handled by kvm_mips_freeze_timer().
498 * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
500 static void kvm_mips_resume_hrtimer(struct kvm_vcpu *vcpu,
501 ktime_t now, u32 count)
503 struct mips_coproc *cop0 = vcpu->arch.cop0;
508 /* Calculate timeout (wrap 0 to 2^32) */
509 compare = kvm_read_c0_guest_compare(cop0);
510 delta = (u64)(u32)(compare - count - 1) + 1;
511 delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
512 expire = ktime_add_ns(now, delta);
514 /* Update hrtimer to use new timeout */
515 hrtimer_cancel(&vcpu->arch.comparecount_timer);
516 hrtimer_start(&vcpu->arch.comparecount_timer, expire, HRTIMER_MODE_ABS);
520 * kvm_mips_write_count() - Modify the count and update timer.
521 * @vcpu: Virtual CPU.
522 * @count: Guest CP0_Count value to set.
524 * Sets the CP0_Count value and updates the timer accordingly.
526 void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count)
528 struct mips_coproc *cop0 = vcpu->arch.cop0;
532 now = kvm_mips_count_time(vcpu);
533 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
535 if (kvm_mips_count_disabled(vcpu))
536 /* The timer's disabled, adjust the static count */
537 kvm_write_c0_guest_count(cop0, count);
540 kvm_mips_resume_hrtimer(vcpu, now, count);
544 * kvm_mips_init_count() - Initialise timer.
545 * @vcpu: Virtual CPU.
547 * Initialise the timer to a sensible frequency, namely 100MHz, zero it, and set
548 * it going if it's enabled.
550 void kvm_mips_init_count(struct kvm_vcpu *vcpu)
553 vcpu->arch.count_hz = 100*1000*1000;
554 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32,
555 vcpu->arch.count_hz);
556 vcpu->arch.count_dyn_bias = 0;
559 kvm_mips_write_count(vcpu, 0);
563 * kvm_mips_set_count_hz() - Update the frequency of the timer.
564 * @vcpu: Virtual CPU.
565 * @count_hz: Frequency of CP0_Count timer in Hz.
567 * Change the frequency of the CP0_Count timer. This is done atomically so that
568 * CP0_Count is continuous and no timer interrupt is lost.
570 * Returns: -EINVAL if @count_hz is out of range.
573 int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
575 struct mips_coproc *cop0 = vcpu->arch.cop0;
580 /* ensure the frequency is in a sensible range... */
581 if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
583 /* ... and has actually changed */
584 if (vcpu->arch.count_hz == count_hz)
587 /* Safely freeze timer so we can keep it continuous */
588 dc = kvm_mips_count_disabled(vcpu);
590 now = kvm_mips_count_time(vcpu);
591 count = kvm_read_c0_guest_count(cop0);
593 now = kvm_mips_freeze_hrtimer(vcpu, &count);
596 /* Update the frequency */
597 vcpu->arch.count_hz = count_hz;
598 vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
599 vcpu->arch.count_dyn_bias = 0;
601 /* Calculate adjusted bias so dynamic count is unchanged */
602 vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
604 /* Update and resume hrtimer */
606 kvm_mips_resume_hrtimer(vcpu, now, count);
611 * kvm_mips_write_compare() - Modify compare and update timer.
612 * @vcpu: Virtual CPU.
613 * @compare: New CP0_Compare value.
614 * @ack: Whether to acknowledge timer interrupt.
616 * Update CP0_Compare to a new value and update the timeout.
617 * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure
618 * any pending timer interrupt is preserved.
620 void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack)
622 struct mips_coproc *cop0 = vcpu->arch.cop0;
624 u32 old_compare = kvm_read_c0_guest_compare(cop0);
628 /* if unchanged, must just be an ack */
629 if (old_compare == compare) {
632 kvm_mips_callbacks->dequeue_timer_int(vcpu);
633 kvm_write_c0_guest_compare(cop0, compare);
637 /* freeze_hrtimer() takes care of timer interrupts <= count */
638 dc = kvm_mips_count_disabled(vcpu);
640 now = kvm_mips_freeze_hrtimer(vcpu, &count);
643 kvm_mips_callbacks->dequeue_timer_int(vcpu);
645 kvm_write_c0_guest_compare(cop0, compare);
647 /* resume_hrtimer() takes care of timer interrupts > count */
649 kvm_mips_resume_hrtimer(vcpu, now, count);
653 * kvm_mips_count_disable() - Disable count.
654 * @vcpu: Virtual CPU.
656 * Disable the CP0_Count timer. A timer interrupt on or before the final stop
657 * time will be handled but not after.
659 * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
660 * count_ctl.DC has been set (count disabled).
662 * Returns: The time that the timer was stopped.
664 static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu)
666 struct mips_coproc *cop0 = vcpu->arch.cop0;
671 hrtimer_cancel(&vcpu->arch.comparecount_timer);
673 /* Set the static count from the dynamic count, handling pending TI */
675 count = kvm_mips_read_count_running(vcpu, now);
676 kvm_write_c0_guest_count(cop0, count);
682 * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
683 * @vcpu: Virtual CPU.
685 * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
686 * before the final stop time will be handled if the timer isn't disabled by
687 * count_ctl.DC, but not after.
689 * Assumes CP0_Cause.DC is clear (count enabled).
691 void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu)
693 struct mips_coproc *cop0 = vcpu->arch.cop0;
695 kvm_set_c0_guest_cause(cop0, CAUSEF_DC);
696 if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
697 kvm_mips_count_disable(vcpu);
701 * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
702 * @vcpu: Virtual CPU.
704 * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
705 * the start time will be handled if the timer isn't disabled by count_ctl.DC,
706 * potentially before even returning, so the caller should be careful with
707 * ordering of CP0_Cause modifications so as not to lose it.
709 * Assumes CP0_Cause.DC is set (count disabled).
711 void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu)
713 struct mips_coproc *cop0 = vcpu->arch.cop0;
716 kvm_clear_c0_guest_cause(cop0, CAUSEF_DC);
719 * Set the dynamic count to match the static count.
720 * This starts the hrtimer if count_ctl.DC allows it.
721 * Otherwise it conveniently updates the biases.
723 count = kvm_read_c0_guest_count(cop0);
724 kvm_mips_write_count(vcpu, count);
728 * kvm_mips_set_count_ctl() - Update the count control KVM register.
729 * @vcpu: Virtual CPU.
730 * @count_ctl: Count control register new value.
732 * Set the count control KVM register. The timer is updated accordingly.
734 * Returns: -EINVAL if reserved bits are set.
737 int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl)
739 struct mips_coproc *cop0 = vcpu->arch.cop0;
740 s64 changed = count_ctl ^ vcpu->arch.count_ctl;
745 /* Only allow defined bits to be changed */
746 if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC))
749 /* Apply new value */
750 vcpu->arch.count_ctl = count_ctl;
752 /* Master CP0_Count disable */
753 if (changed & KVM_REG_MIPS_COUNT_CTL_DC) {
754 /* Is CP0_Cause.DC already disabling CP0_Count? */
755 if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) {
756 if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)
757 /* Just record the current time */
758 vcpu->arch.count_resume = ktime_get();
759 } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) {
760 /* disable timer and record current time */
761 vcpu->arch.count_resume = kvm_mips_count_disable(vcpu);
764 * Calculate timeout relative to static count at resume
765 * time (wrap 0 to 2^32).
767 count = kvm_read_c0_guest_count(cop0);
768 compare = kvm_read_c0_guest_compare(cop0);
769 delta = (u64)(u32)(compare - count - 1) + 1;
770 delta = div_u64(delta * NSEC_PER_SEC,
771 vcpu->arch.count_hz);
772 expire = ktime_add_ns(vcpu->arch.count_resume, delta);
774 /* Handle pending interrupt */
776 if (ktime_compare(now, expire) >= 0)
777 /* Nothing should be waiting on the timeout */
778 kvm_mips_callbacks->queue_timer_int(vcpu);
780 /* Resume hrtimer without changing bias */
781 count = kvm_mips_read_count_running(vcpu, now);
782 kvm_mips_resume_hrtimer(vcpu, now, count);
790 * kvm_mips_set_count_resume() - Update the count resume KVM register.
791 * @vcpu: Virtual CPU.
792 * @count_resume: Count resume register new value.
794 * Set the count resume KVM register.
796 * Returns: -EINVAL if out of valid range (0..now).
799 int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume)
802 * It doesn't make sense for the resume time to be in the future, as it
803 * would be possible for the next interrupt to be more than a full
804 * period in the future.
806 if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get()))
809 vcpu->arch.count_resume = ns_to_ktime(count_resume);
814 * kvm_mips_count_timeout() - Push timer forward on timeout.
815 * @vcpu: Virtual CPU.
817 * Handle an hrtimer event by push the hrtimer forward a period.
819 * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
821 enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu)
823 /* Add the Count period to the current expiry time */
824 hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer,
825 vcpu->arch.count_period);
826 return HRTIMER_RESTART;
829 enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
831 struct mips_coproc *cop0 = vcpu->arch.cop0;
832 enum emulation_result er = EMULATE_DONE;
834 if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
835 kvm_clear_c0_guest_status(cop0, ST0_ERL);
836 vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
837 } else if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
838 kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
839 kvm_read_c0_guest_epc(cop0));
840 kvm_clear_c0_guest_status(cop0, ST0_EXL);
841 vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
844 kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
852 enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
854 kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
855 vcpu->arch.pending_exceptions);
857 ++vcpu->stat.wait_exits;
858 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_WAIT);
859 if (!vcpu->arch.pending_exceptions) {
861 kvm_vcpu_block(vcpu);
864 * We we are runnable, then definitely go off to user space to
865 * check if any I/O interrupts are pending.
867 if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
868 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
869 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
877 * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that
878 * we can catch this, if things ever change
880 enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
882 struct mips_coproc *cop0 = vcpu->arch.cop0;
883 unsigned long pc = vcpu->arch.pc;
885 kvm_err("[%#lx] COP0_TLBR [%ld]\n", pc, kvm_read_c0_guest_index(cop0));
890 * kvm_mips_invalidate_guest_tlb() - Indicates a change in guest MMU map.
891 * @vcpu: VCPU with changed mappings.
892 * @tlb: TLB entry being removed.
894 * This is called to indicate a single change in guest MMU mappings, so that we
895 * can arrange TLB flushes on this and other CPUs.
897 static void kvm_mips_invalidate_guest_tlb(struct kvm_vcpu *vcpu,
898 struct kvm_mips_tlb *tlb)
900 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
901 struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
905 /* No need to flush for entries which are already invalid */
906 if (!((tlb->tlb_lo[0] | tlb->tlb_lo[1]) & ENTRYLO_V))
908 /* Don't touch host kernel page tables or TLB mappings */
909 if ((unsigned long)tlb->tlb_hi > 0x7fffffff)
911 /* User address space doesn't need flushing for KSeg2/3 changes */
912 user = tlb->tlb_hi < KVM_GUEST_KSEG0;
916 /* Invalidate page table entries */
917 kvm_trap_emul_invalidate_gva(vcpu, tlb->tlb_hi & VPN2_MASK, user);
920 * Probe the shadow host TLB for the entry being overwritten, if one
921 * matches, invalidate it
923 kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi, user, true);
925 /* Invalidate the whole ASID on other CPUs */
926 cpu = smp_processor_id();
927 for_each_possible_cpu(i) {
931 cpu_context(i, user_mm) = 0;
932 cpu_context(i, kern_mm) = 0;
938 /* Write Guest TLB Entry @ Index */
939 enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
941 struct mips_coproc *cop0 = vcpu->arch.cop0;
942 int index = kvm_read_c0_guest_index(cop0);
943 struct kvm_mips_tlb *tlb = NULL;
944 unsigned long pc = vcpu->arch.pc;
946 if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
947 kvm_debug("%s: illegal index: %d\n", __func__, index);
948 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
949 pc, index, kvm_read_c0_guest_entryhi(cop0),
950 kvm_read_c0_guest_entrylo0(cop0),
951 kvm_read_c0_guest_entrylo1(cop0),
952 kvm_read_c0_guest_pagemask(cop0));
953 index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
956 tlb = &vcpu->arch.guest_tlb[index];
958 kvm_mips_invalidate_guest_tlb(vcpu, tlb);
960 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
961 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
962 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
963 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
965 kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
966 pc, index, kvm_read_c0_guest_entryhi(cop0),
967 kvm_read_c0_guest_entrylo0(cop0),
968 kvm_read_c0_guest_entrylo1(cop0),
969 kvm_read_c0_guest_pagemask(cop0));
974 /* Write Guest TLB Entry @ Random Index */
975 enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
977 struct mips_coproc *cop0 = vcpu->arch.cop0;
978 struct kvm_mips_tlb *tlb = NULL;
979 unsigned long pc = vcpu->arch.pc;
982 get_random_bytes(&index, sizeof(index));
983 index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
985 tlb = &vcpu->arch.guest_tlb[index];
987 kvm_mips_invalidate_guest_tlb(vcpu, tlb);
989 tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
990 tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
991 tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
992 tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
994 kvm_debug("[%#lx] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
995 pc, index, kvm_read_c0_guest_entryhi(cop0),
996 kvm_read_c0_guest_entrylo0(cop0),
997 kvm_read_c0_guest_entrylo1(cop0));
1002 enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
1004 struct mips_coproc *cop0 = vcpu->arch.cop0;
1005 long entryhi = kvm_read_c0_guest_entryhi(cop0);
1006 unsigned long pc = vcpu->arch.pc;
1009 index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
1011 kvm_write_c0_guest_index(cop0, index);
1013 kvm_debug("[%#lx] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
1016 return EMULATE_DONE;
1020 * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1
1021 * @vcpu: Virtual CPU.
1023 * Finds the mask of bits which are writable in the guest's Config1 CP0
1024 * register, by userland (currently read-only to the guest).
1026 unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu)
1028 unsigned int mask = 0;
1030 /* Permit FPU to be present if FPU is supported */
1031 if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
1032 mask |= MIPS_CONF1_FP;
1038 * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3
1039 * @vcpu: Virtual CPU.
1041 * Finds the mask of bits which are writable in the guest's Config3 CP0
1042 * register, by userland (currently read-only to the guest).
1044 unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu)
1046 /* Config4 and ULRI are optional */
1047 unsigned int mask = MIPS_CONF_M | MIPS_CONF3_ULRI;
1049 /* Permit MSA to be present if MSA is supported */
1050 if (kvm_mips_guest_can_have_msa(&vcpu->arch))
1051 mask |= MIPS_CONF3_MSA;
1057 * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4
1058 * @vcpu: Virtual CPU.
1060 * Finds the mask of bits which are writable in the guest's Config4 CP0
1061 * register, by userland (currently read-only to the guest).
1063 unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu)
1065 /* Config5 is optional */
1066 unsigned int mask = MIPS_CONF_M;
1069 mask |= 0xfc << MIPS_CONF4_KSCREXIST_SHIFT;
1075 * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5
1076 * @vcpu: Virtual CPU.
1078 * Finds the mask of bits which are writable in the guest's Config5 CP0
1079 * register, by the guest itself.
1081 unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu)
1083 unsigned int mask = 0;
1085 /* Permit MSAEn changes if MSA supported and enabled */
1086 if (kvm_mips_guest_has_msa(&vcpu->arch))
1087 mask |= MIPS_CONF5_MSAEN;
1090 * Permit guest FPU mode changes if FPU is enabled and the relevant
1091 * feature exists according to FIR register.
1093 if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
1095 mask |= MIPS_CONF5_FRE;
1096 /* We don't support UFR or UFE */
1102 enum emulation_result kvm_mips_emulate_CP0(union mips_instruction inst,
1103 u32 *opc, u32 cause,
1104 struct kvm_run *run,
1105 struct kvm_vcpu *vcpu)
1107 struct mips_coproc *cop0 = vcpu->arch.cop0;
1108 struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
1109 enum emulation_result er = EMULATE_DONE;
1111 unsigned long curr_pc;
1115 * Update PC and hold onto current PC in case there is
1116 * an error and we want to rollback the PC
1118 curr_pc = vcpu->arch.pc;
1119 er = update_pc(vcpu, cause);
1120 if (er == EMULATE_FAIL)
1123 if (inst.co_format.co) {
1124 switch (inst.co_format.func) {
1125 case tlbr_op: /* Read indexed TLB entry */
1126 er = kvm_mips_emul_tlbr(vcpu);
1128 case tlbwi_op: /* Write indexed */
1129 er = kvm_mips_emul_tlbwi(vcpu);
1131 case tlbwr_op: /* Write random */
1132 er = kvm_mips_emul_tlbwr(vcpu);
1134 case tlbp_op: /* TLB Probe */
1135 er = kvm_mips_emul_tlbp(vcpu);
1138 kvm_err("!!!COP0_RFE!!!\n");
1141 er = kvm_mips_emul_eret(vcpu);
1142 goto dont_update_pc;
1144 er = kvm_mips_emul_wait(vcpu);
1148 rt = inst.c0r_format.rt;
1149 rd = inst.c0r_format.rd;
1150 sel = inst.c0r_format.sel;
1152 switch (inst.c0r_format.rs) {
1154 #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1155 cop0->stat[rd][sel]++;
1158 if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
1159 vcpu->arch.gprs[rt] =
1160 (s32)kvm_mips_read_count(vcpu);
1161 } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
1162 vcpu->arch.gprs[rt] = 0x0;
1163 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1164 kvm_mips_trans_mfc0(inst, opc, vcpu);
1167 vcpu->arch.gprs[rt] = (s32)cop0->reg[rd][sel];
1169 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1170 kvm_mips_trans_mfc0(inst, opc, vcpu);
1174 trace_kvm_hwr(vcpu, KVM_TRACE_MFC0,
1175 KVM_TRACE_COP0(rd, sel),
1176 vcpu->arch.gprs[rt]);
1180 vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
1182 trace_kvm_hwr(vcpu, KVM_TRACE_DMFC0,
1183 KVM_TRACE_COP0(rd, sel),
1184 vcpu->arch.gprs[rt]);
1188 #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
1189 cop0->stat[rd][sel]++;
1191 trace_kvm_hwr(vcpu, KVM_TRACE_MTC0,
1192 KVM_TRACE_COP0(rd, sel),
1193 vcpu->arch.gprs[rt]);
1195 if ((rd == MIPS_CP0_TLB_INDEX)
1196 && (vcpu->arch.gprs[rt] >=
1197 KVM_MIPS_GUEST_TLB_SIZE)) {
1198 kvm_err("Invalid TLB Index: %ld",
1199 vcpu->arch.gprs[rt]);
1203 if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
1205 * Preserve core number, and keep the exception
1206 * base in guest KSeg0.
1208 kvm_change_c0_guest_ebase(cop0, 0x1ffff000,
1209 vcpu->arch.gprs[rt]);
1210 } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
1212 vcpu->arch.gprs[rt] & KVM_ENTRYHI_ASID;
1213 if (((kvm_read_c0_guest_entryhi(cop0) &
1214 KVM_ENTRYHI_ASID) != nasid)) {
1215 trace_kvm_asid_change(vcpu,
1216 kvm_read_c0_guest_entryhi(cop0)
1221 * Flush entries from the GVA page
1223 * Guest user page table will get
1224 * flushed lazily on re-entry to guest
1225 * user if the guest ASID actually
1228 kvm_mips_flush_gva_pt(kern_mm->pgd,
1232 * Regenerate/invalidate kernel MMU
1234 * The user MMU context will be
1235 * regenerated lazily on re-entry to
1236 * guest user if the guest ASID actually
1240 cpu = smp_processor_id();
1241 get_new_mmu_context(kern_mm, cpu);
1242 for_each_possible_cpu(i)
1244 cpu_context(i, kern_mm) = 0;
1247 kvm_write_c0_guest_entryhi(cop0,
1248 vcpu->arch.gprs[rt]);
1250 /* Are we writing to COUNT */
1251 else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
1252 kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
1254 } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
1255 /* If we are writing to COMPARE */
1256 /* Clear pending timer interrupt, if any */
1257 kvm_mips_write_compare(vcpu,
1258 vcpu->arch.gprs[rt],
1260 } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
1261 unsigned int old_val, val, change;
1263 old_val = kvm_read_c0_guest_status(cop0);
1264 val = vcpu->arch.gprs[rt];
1265 change = val ^ old_val;
1267 /* Make sure that the NMI bit is never set */
1271 * Don't allow CU1 or FR to be set unless FPU
1272 * capability enabled and exists in guest
1275 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1276 val &= ~(ST0_CU1 | ST0_FR);
1279 * Also don't allow FR to be set if host doesn't
1282 if (!(current_cpu_data.fpu_id & MIPS_FPIR_F64))
1286 /* Handle changes in FPU mode */
1290 * FPU and Vector register state is made
1291 * UNPREDICTABLE by a change of FR, so don't
1292 * even bother saving it.
1294 if (change & ST0_FR)
1298 * If MSA state is already live, it is undefined
1299 * how it interacts with FR=0 FPU state, and we
1300 * don't want to hit reserved instruction
1301 * exceptions trying to save the MSA state later
1302 * when CU=1 && FR=1, so play it safe and save
1305 if (change & ST0_CU1 && !(val & ST0_FR) &&
1306 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
1310 * Propagate CU1 (FPU enable) changes
1311 * immediately if the FPU context is already
1312 * loaded. When disabling we leave the context
1313 * loaded so it can be quickly enabled again in
1316 if (change & ST0_CU1 &&
1317 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
1318 change_c0_status(ST0_CU1, val);
1322 kvm_write_c0_guest_status(cop0, val);
1324 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1326 * If FPU present, we need CU1/FR bits to take
1327 * effect fairly soon.
1329 if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1330 kvm_mips_trans_mtc0(inst, opc, vcpu);
1332 } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
1333 unsigned int old_val, val, change, wrmask;
1335 old_val = kvm_read_c0_guest_config5(cop0);
1336 val = vcpu->arch.gprs[rt];
1338 /* Only a few bits are writable in Config5 */
1339 wrmask = kvm_mips_config5_wrmask(vcpu);
1340 change = (val ^ old_val) & wrmask;
1341 val = old_val ^ change;
1344 /* Handle changes in FPU/MSA modes */
1348 * Propagate FRE changes immediately if the FPU
1349 * context is already loaded.
1351 if (change & MIPS_CONF5_FRE &&
1352 vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
1353 change_c0_config5(MIPS_CONF5_FRE, val);
1356 * Propagate MSAEn changes immediately if the
1357 * MSA context is already loaded. When disabling
1358 * we leave the context loaded so it can be
1359 * quickly enabled again in the near future.
1361 if (change & MIPS_CONF5_MSAEN &&
1362 vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
1363 change_c0_config5(MIPS_CONF5_MSAEN,
1368 kvm_write_c0_guest_config5(cop0, val);
1369 } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
1370 u32 old_cause, new_cause;
1372 old_cause = kvm_read_c0_guest_cause(cop0);
1373 new_cause = vcpu->arch.gprs[rt];
1374 /* Update R/W bits */
1375 kvm_change_c0_guest_cause(cop0, 0x08800300,
1377 /* DC bit enabling/disabling timer? */
1378 if ((old_cause ^ new_cause) & CAUSEF_DC) {
1379 if (new_cause & CAUSEF_DC)
1380 kvm_mips_count_disable_cause(vcpu);
1382 kvm_mips_count_enable_cause(vcpu);
1384 } else if ((rd == MIPS_CP0_HWRENA) && (sel == 0)) {
1385 u32 mask = MIPS_HWRENA_CPUNUM |
1386 MIPS_HWRENA_SYNCISTEP |
1390 if (kvm_read_c0_guest_config3(cop0) &
1392 mask |= MIPS_HWRENA_ULR;
1393 cop0->reg[rd][sel] = vcpu->arch.gprs[rt] & mask;
1395 cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
1396 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1397 kvm_mips_trans_mtc0(inst, opc, vcpu);
1403 kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
1404 vcpu->arch.pc, rt, rd, sel);
1405 trace_kvm_hwr(vcpu, KVM_TRACE_DMTC0,
1406 KVM_TRACE_COP0(rd, sel),
1407 vcpu->arch.gprs[rt]);
1412 #ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
1413 cop0->stat[MIPS_CP0_STATUS][0]++;
1416 vcpu->arch.gprs[rt] =
1417 kvm_read_c0_guest_status(cop0);
1419 if (inst.mfmc0_format.sc) {
1420 kvm_debug("[%#lx] mfmc0_op: EI\n",
1422 kvm_set_c0_guest_status(cop0, ST0_IE);
1424 kvm_debug("[%#lx] mfmc0_op: DI\n",
1426 kvm_clear_c0_guest_status(cop0, ST0_IE);
1433 u32 css = cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
1435 (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
1437 * We don't support any shadow register sets, so
1438 * SRSCtl[PSS] == SRSCtl[CSS] = 0
1444 kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
1445 vcpu->arch.gprs[rt]);
1446 vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
1450 kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
1451 vcpu->arch.pc, inst.c0r_format.rs);
1458 /* Rollback PC only if emulation was unsuccessful */
1459 if (er == EMULATE_FAIL)
1460 vcpu->arch.pc = curr_pc;
1464 * This is for special instructions whose emulation
1465 * updates the PC, so do not overwrite the PC under
1472 enum emulation_result kvm_mips_emulate_store(union mips_instruction inst,
1474 struct kvm_run *run,
1475 struct kvm_vcpu *vcpu)
1477 enum emulation_result er = EMULATE_DO_MMIO;
1480 void *data = run->mmio.data;
1481 unsigned long curr_pc;
1484 * Update PC and hold onto current PC in case there is
1485 * an error and we want to rollback the PC
1487 curr_pc = vcpu->arch.pc;
1488 er = update_pc(vcpu, cause);
1489 if (er == EMULATE_FAIL)
1492 rt = inst.i_format.rt;
1494 switch (inst.i_format.opcode) {
1497 if (bytes > sizeof(run->mmio.data)) {
1498 kvm_err("%s: bad MMIO length: %d\n", __func__,
1501 run->mmio.phys_addr =
1502 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1504 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1508 run->mmio.len = bytes;
1509 run->mmio.is_write = 1;
1510 vcpu->mmio_needed = 1;
1511 vcpu->mmio_is_write = 1;
1512 *(u8 *) data = vcpu->arch.gprs[rt];
1513 kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1514 vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt],
1521 if (bytes > sizeof(run->mmio.data)) {
1522 kvm_err("%s: bad MMIO length: %d\n", __func__,
1525 run->mmio.phys_addr =
1526 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1528 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1533 run->mmio.len = bytes;
1534 run->mmio.is_write = 1;
1535 vcpu->mmio_needed = 1;
1536 vcpu->mmio_is_write = 1;
1537 *(u32 *) data = vcpu->arch.gprs[rt];
1539 kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1540 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
1541 vcpu->arch.gprs[rt], *(u32 *) data);
1546 if (bytes > sizeof(run->mmio.data)) {
1547 kvm_err("%s: bad MMIO length: %d\n", __func__,
1550 run->mmio.phys_addr =
1551 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1553 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1558 run->mmio.len = bytes;
1559 run->mmio.is_write = 1;
1560 vcpu->mmio_needed = 1;
1561 vcpu->mmio_is_write = 1;
1562 *(u16 *) data = vcpu->arch.gprs[rt];
1564 kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
1565 vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
1566 vcpu->arch.gprs[rt], *(u32 *) data);
1570 kvm_err("Store not yet supported (inst=0x%08x)\n",
1576 /* Rollback PC if emulation was unsuccessful */
1577 if (er == EMULATE_FAIL)
1578 vcpu->arch.pc = curr_pc;
1583 enum emulation_result kvm_mips_emulate_load(union mips_instruction inst,
1584 u32 cause, struct kvm_run *run,
1585 struct kvm_vcpu *vcpu)
1587 enum emulation_result er = EMULATE_DO_MMIO;
1588 unsigned long curr_pc;
1592 rt = inst.i_format.rt;
1593 op = inst.i_format.opcode;
1596 * Find the resume PC now while we have safe and easy access to the
1597 * prior branch instruction, and save it for
1598 * kvm_mips_complete_mmio_load() to restore later.
1600 curr_pc = vcpu->arch.pc;
1601 er = update_pc(vcpu, cause);
1602 if (er == EMULATE_FAIL)
1604 vcpu->arch.io_pc = vcpu->arch.pc;
1605 vcpu->arch.pc = curr_pc;
1607 vcpu->arch.io_gpr = rt;
1612 if (bytes > sizeof(run->mmio.data)) {
1613 kvm_err("%s: bad MMIO length: %d\n", __func__,
1618 run->mmio.phys_addr =
1619 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1621 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1626 run->mmio.len = bytes;
1627 run->mmio.is_write = 0;
1628 vcpu->mmio_needed = 1;
1629 vcpu->mmio_is_write = 0;
1635 if (bytes > sizeof(run->mmio.data)) {
1636 kvm_err("%s: bad MMIO length: %d\n", __func__,
1641 run->mmio.phys_addr =
1642 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1644 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1649 run->mmio.len = bytes;
1650 run->mmio.is_write = 0;
1651 vcpu->mmio_needed = 1;
1652 vcpu->mmio_is_write = 0;
1655 vcpu->mmio_needed = 2;
1657 vcpu->mmio_needed = 1;
1664 if (bytes > sizeof(run->mmio.data)) {
1665 kvm_err("%s: bad MMIO length: %d\n", __func__,
1670 run->mmio.phys_addr =
1671 kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
1673 if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
1678 run->mmio.len = bytes;
1679 run->mmio.is_write = 0;
1680 vcpu->mmio_is_write = 0;
1683 vcpu->mmio_needed = 2;
1685 vcpu->mmio_needed = 1;
1690 kvm_err("Load not yet supported (inst=0x%08x)\n",
1699 static enum emulation_result kvm_mips_guest_cache_op(int (*fn)(unsigned long),
1700 unsigned long curr_pc,
1702 struct kvm_run *run,
1703 struct kvm_vcpu *vcpu,
1709 /* Carefully attempt the cache operation */
1710 kvm_trap_emul_gva_lockless_begin(vcpu);
1712 kvm_trap_emul_gva_lockless_end(vcpu);
1715 return EMULATE_DONE;
1718 * Try to handle the fault and retry, maybe we just raced with a
1721 switch (kvm_trap_emul_gva_fault(vcpu, addr, false)) {
1724 /* bad virtual or physical address */
1725 return EMULATE_FAIL;
1727 /* no matching guest TLB */
1728 vcpu->arch.host_cp0_badvaddr = addr;
1729 vcpu->arch.pc = curr_pc;
1730 kvm_mips_emulate_tlbmiss_ld(cause, NULL, run, vcpu);
1731 return EMULATE_EXCEPT;
1732 case KVM_MIPS_TLBINV:
1733 /* invalid matching guest TLB */
1734 vcpu->arch.host_cp0_badvaddr = addr;
1735 vcpu->arch.pc = curr_pc;
1736 kvm_mips_emulate_tlbinv_ld(cause, NULL, run, vcpu);
1737 return EMULATE_EXCEPT;
1744 enum emulation_result kvm_mips_emulate_cache(union mips_instruction inst,
1745 u32 *opc, u32 cause,
1746 struct kvm_run *run,
1747 struct kvm_vcpu *vcpu)
1749 enum emulation_result er = EMULATE_DONE;
1750 u32 cache, op_inst, op, base;
1752 struct kvm_vcpu_arch *arch = &vcpu->arch;
1754 unsigned long curr_pc;
1757 * Update PC and hold onto current PC in case there is
1758 * an error and we want to rollback the PC
1760 curr_pc = vcpu->arch.pc;
1761 er = update_pc(vcpu, cause);
1762 if (er == EMULATE_FAIL)
1765 base = inst.i_format.rs;
1766 op_inst = inst.i_format.rt;
1767 if (cpu_has_mips_r6)
1768 offset = inst.spec3_format.simmediate;
1770 offset = inst.i_format.simmediate;
1771 cache = op_inst & CacheOp_Cache;
1772 op = op_inst & CacheOp_Op;
1774 va = arch->gprs[base] + offset;
1776 kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1777 cache, op, base, arch->gprs[base], offset);
1780 * Treat INDEX_INV as a nop, basically issued by Linux on startup to
1781 * invalidate the caches entirely by stepping through all the
1784 if (op == Index_Writeback_Inv) {
1785 kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1786 vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
1787 arch->gprs[base], offset);
1789 if (cache == Cache_D)
1791 else if (cache == Cache_I)
1794 kvm_err("%s: unsupported CACHE INDEX operation\n",
1796 return EMULATE_FAIL;
1799 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1800 kvm_mips_trans_cache_index(inst, opc, vcpu);
1805 /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
1806 if (op_inst == Hit_Writeback_Inv_D || op_inst == Hit_Invalidate_D) {
1808 * Perform the dcache part of icache synchronisation on the
1811 er = kvm_mips_guest_cache_op(protected_writeback_dcache_line,
1812 curr_pc, va, run, vcpu, cause);
1813 if (er != EMULATE_DONE)
1815 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1817 * Replace the CACHE instruction, with a SYNCI, not the same,
1820 kvm_mips_trans_cache_va(inst, opc, vcpu);
1822 } else if (op_inst == Hit_Invalidate_I) {
1823 /* Perform the icache synchronisation on the guest's behalf */
1824 er = kvm_mips_guest_cache_op(protected_writeback_dcache_line,
1825 curr_pc, va, run, vcpu, cause);
1826 if (er != EMULATE_DONE)
1828 er = kvm_mips_guest_cache_op(protected_flush_icache_line,
1829 curr_pc, va, run, vcpu, cause);
1830 if (er != EMULATE_DONE)
1833 #ifdef CONFIG_KVM_MIPS_DYN_TRANS
1834 /* Replace the CACHE instruction, with a SYNCI */
1835 kvm_mips_trans_cache_va(inst, opc, vcpu);
1838 kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1839 cache, op, base, arch->gprs[base], offset);
1844 /* Rollback PC only if emulation was unsuccessful */
1845 if (er == EMULATE_FAIL)
1846 vcpu->arch.pc = curr_pc;
1847 /* Guest exception needs guest to resume */
1848 if (er == EMULATE_EXCEPT)
1854 enum emulation_result kvm_mips_emulate_inst(u32 cause, u32 *opc,
1855 struct kvm_run *run,
1856 struct kvm_vcpu *vcpu)
1858 union mips_instruction inst;
1859 enum emulation_result er = EMULATE_DONE;
1862 /* Fetch the instruction. */
1863 if (cause & CAUSEF_BD)
1865 err = kvm_get_badinstr(opc, vcpu, &inst.word);
1867 return EMULATE_FAIL;
1869 switch (inst.r_format.opcode) {
1871 er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
1876 er = kvm_mips_emulate_store(inst, cause, run, vcpu);
1883 er = kvm_mips_emulate_load(inst, cause, run, vcpu);
1886 #ifndef CONFIG_CPU_MIPSR6
1888 ++vcpu->stat.cache_exits;
1889 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
1890 er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
1894 switch (inst.spec3_format.func) {
1896 ++vcpu->stat.cache_exits;
1897 trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
1898 er = kvm_mips_emulate_cache(inst, opc, cause, run,
1909 kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
1911 kvm_arch_vcpu_dump_regs(vcpu);
1920 * kvm_mips_guest_exception_base() - Find guest exception vector base address.
1922 * Returns: The base address of the current guest exception vector, taking
1923 * both Guest.CP0_Status.BEV and Guest.CP0_EBase into account.
1925 long kvm_mips_guest_exception_base(struct kvm_vcpu *vcpu)
1927 struct mips_coproc *cop0 = vcpu->arch.cop0;
1929 if (kvm_read_c0_guest_status(cop0) & ST0_BEV)
1930 return KVM_GUEST_CKSEG1ADDR(0x1fc00200);
1932 return kvm_read_c0_guest_ebase(cop0) & MIPS_EBASE_BASE;
1935 enum emulation_result kvm_mips_emulate_syscall(u32 cause,
1937 struct kvm_run *run,
1938 struct kvm_vcpu *vcpu)
1940 struct mips_coproc *cop0 = vcpu->arch.cop0;
1941 struct kvm_vcpu_arch *arch = &vcpu->arch;
1942 enum emulation_result er = EMULATE_DONE;
1944 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1946 kvm_write_c0_guest_epc(cop0, arch->pc);
1947 kvm_set_c0_guest_status(cop0, ST0_EXL);
1949 if (cause & CAUSEF_BD)
1950 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1952 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1954 kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
1956 kvm_change_c0_guest_cause(cop0, (0xff),
1957 (EXCCODE_SYS << CAUSEB_EXCCODE));
1959 /* Set PC to the exception entry point */
1960 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
1963 kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
1970 enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause,
1972 struct kvm_run *run,
1973 struct kvm_vcpu *vcpu)
1975 struct mips_coproc *cop0 = vcpu->arch.cop0;
1976 struct kvm_vcpu_arch *arch = &vcpu->arch;
1977 unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
1978 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
1980 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
1982 kvm_write_c0_guest_epc(cop0, arch->pc);
1983 kvm_set_c0_guest_status(cop0, ST0_EXL);
1985 if (cause & CAUSEF_BD)
1986 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
1988 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
1990 kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
1993 /* set pc to the exception entry point */
1994 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x0;
1997 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
2000 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2003 kvm_change_c0_guest_cause(cop0, (0xff),
2004 (EXCCODE_TLBL << CAUSEB_EXCCODE));
2006 /* setup badvaddr, context and entryhi registers for the guest */
2007 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2008 /* XXXKYMA: is the context register used by linux??? */
2009 kvm_write_c0_guest_entryhi(cop0, entryhi);
2011 return EMULATE_DONE;
2014 enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause,
2016 struct kvm_run *run,
2017 struct kvm_vcpu *vcpu)
2019 struct mips_coproc *cop0 = vcpu->arch.cop0;
2020 struct kvm_vcpu_arch *arch = &vcpu->arch;
2021 unsigned long entryhi =
2022 (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
2023 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
2025 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2027 kvm_write_c0_guest_epc(cop0, arch->pc);
2028 kvm_set_c0_guest_status(cop0, ST0_EXL);
2030 if (cause & CAUSEF_BD)
2031 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2033 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2035 kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
2038 kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
2042 /* set pc to the exception entry point */
2043 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2045 kvm_change_c0_guest_cause(cop0, (0xff),
2046 (EXCCODE_TLBL << CAUSEB_EXCCODE));
2048 /* setup badvaddr, context and entryhi registers for the guest */
2049 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2050 /* XXXKYMA: is the context register used by linux??? */
2051 kvm_write_c0_guest_entryhi(cop0, entryhi);
2053 return EMULATE_DONE;
2056 enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause,
2058 struct kvm_run *run,
2059 struct kvm_vcpu *vcpu)
2061 struct mips_coproc *cop0 = vcpu->arch.cop0;
2062 struct kvm_vcpu_arch *arch = &vcpu->arch;
2063 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
2064 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
2066 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2068 kvm_write_c0_guest_epc(cop0, arch->pc);
2069 kvm_set_c0_guest_status(cop0, ST0_EXL);
2071 if (cause & CAUSEF_BD)
2072 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2074 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2076 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
2079 /* Set PC to the exception entry point */
2080 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x0;
2082 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
2084 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2087 kvm_change_c0_guest_cause(cop0, (0xff),
2088 (EXCCODE_TLBS << CAUSEB_EXCCODE));
2090 /* setup badvaddr, context and entryhi registers for the guest */
2091 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2092 /* XXXKYMA: is the context register used by linux??? */
2093 kvm_write_c0_guest_entryhi(cop0, entryhi);
2095 return EMULATE_DONE;
2098 enum emulation_result kvm_mips_emulate_tlbinv_st(u32 cause,
2100 struct kvm_run *run,
2101 struct kvm_vcpu *vcpu)
2103 struct mips_coproc *cop0 = vcpu->arch.cop0;
2104 struct kvm_vcpu_arch *arch = &vcpu->arch;
2105 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
2106 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
2108 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2110 kvm_write_c0_guest_epc(cop0, arch->pc);
2111 kvm_set_c0_guest_status(cop0, ST0_EXL);
2113 if (cause & CAUSEF_BD)
2114 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2116 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2118 kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
2121 kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
2125 /* Set PC to the exception entry point */
2126 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2128 kvm_change_c0_guest_cause(cop0, (0xff),
2129 (EXCCODE_TLBS << CAUSEB_EXCCODE));
2131 /* setup badvaddr, context and entryhi registers for the guest */
2132 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2133 /* XXXKYMA: is the context register used by linux??? */
2134 kvm_write_c0_guest_entryhi(cop0, entryhi);
2136 return EMULATE_DONE;
2139 enum emulation_result kvm_mips_emulate_tlbmod(u32 cause,
2141 struct kvm_run *run,
2142 struct kvm_vcpu *vcpu)
2144 struct mips_coproc *cop0 = vcpu->arch.cop0;
2145 unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
2146 (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
2147 struct kvm_vcpu_arch *arch = &vcpu->arch;
2149 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2151 kvm_write_c0_guest_epc(cop0, arch->pc);
2152 kvm_set_c0_guest_status(cop0, ST0_EXL);
2154 if (cause & CAUSEF_BD)
2155 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2157 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2159 kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
2162 kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
2166 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2168 kvm_change_c0_guest_cause(cop0, (0xff),
2169 (EXCCODE_MOD << CAUSEB_EXCCODE));
2171 /* setup badvaddr, context and entryhi registers for the guest */
2172 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2173 /* XXXKYMA: is the context register used by linux??? */
2174 kvm_write_c0_guest_entryhi(cop0, entryhi);
2176 return EMULATE_DONE;
2179 enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause,
2181 struct kvm_run *run,
2182 struct kvm_vcpu *vcpu)
2184 struct mips_coproc *cop0 = vcpu->arch.cop0;
2185 struct kvm_vcpu_arch *arch = &vcpu->arch;
2187 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2189 kvm_write_c0_guest_epc(cop0, arch->pc);
2190 kvm_set_c0_guest_status(cop0, ST0_EXL);
2192 if (cause & CAUSEF_BD)
2193 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2195 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2199 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2201 kvm_change_c0_guest_cause(cop0, (0xff),
2202 (EXCCODE_CPU << CAUSEB_EXCCODE));
2203 kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
2205 return EMULATE_DONE;
2208 enum emulation_result kvm_mips_emulate_ri_exc(u32 cause,
2210 struct kvm_run *run,
2211 struct kvm_vcpu *vcpu)
2213 struct mips_coproc *cop0 = vcpu->arch.cop0;
2214 struct kvm_vcpu_arch *arch = &vcpu->arch;
2215 enum emulation_result er = EMULATE_DONE;
2217 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2219 kvm_write_c0_guest_epc(cop0, arch->pc);
2220 kvm_set_c0_guest_status(cop0, ST0_EXL);
2222 if (cause & CAUSEF_BD)
2223 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2225 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2227 kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
2229 kvm_change_c0_guest_cause(cop0, (0xff),
2230 (EXCCODE_RI << CAUSEB_EXCCODE));
2232 /* Set PC to the exception entry point */
2233 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2236 kvm_err("Trying to deliver RI when EXL is already set\n");
2243 enum emulation_result kvm_mips_emulate_bp_exc(u32 cause,
2245 struct kvm_run *run,
2246 struct kvm_vcpu *vcpu)
2248 struct mips_coproc *cop0 = vcpu->arch.cop0;
2249 struct kvm_vcpu_arch *arch = &vcpu->arch;
2250 enum emulation_result er = EMULATE_DONE;
2252 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2254 kvm_write_c0_guest_epc(cop0, arch->pc);
2255 kvm_set_c0_guest_status(cop0, ST0_EXL);
2257 if (cause & CAUSEF_BD)
2258 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2260 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2262 kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
2264 kvm_change_c0_guest_cause(cop0, (0xff),
2265 (EXCCODE_BP << CAUSEB_EXCCODE));
2267 /* Set PC to the exception entry point */
2268 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2271 kvm_err("Trying to deliver BP when EXL is already set\n");
2278 enum emulation_result kvm_mips_emulate_trap_exc(u32 cause,
2280 struct kvm_run *run,
2281 struct kvm_vcpu *vcpu)
2283 struct mips_coproc *cop0 = vcpu->arch.cop0;
2284 struct kvm_vcpu_arch *arch = &vcpu->arch;
2285 enum emulation_result er = EMULATE_DONE;
2287 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2289 kvm_write_c0_guest_epc(cop0, arch->pc);
2290 kvm_set_c0_guest_status(cop0, ST0_EXL);
2292 if (cause & CAUSEF_BD)
2293 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2295 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2297 kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc);
2299 kvm_change_c0_guest_cause(cop0, (0xff),
2300 (EXCCODE_TR << CAUSEB_EXCCODE));
2302 /* Set PC to the exception entry point */
2303 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2306 kvm_err("Trying to deliver TRAP when EXL is already set\n");
2313 enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause,
2315 struct kvm_run *run,
2316 struct kvm_vcpu *vcpu)
2318 struct mips_coproc *cop0 = vcpu->arch.cop0;
2319 struct kvm_vcpu_arch *arch = &vcpu->arch;
2320 enum emulation_result er = EMULATE_DONE;
2322 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2324 kvm_write_c0_guest_epc(cop0, arch->pc);
2325 kvm_set_c0_guest_status(cop0, ST0_EXL);
2327 if (cause & CAUSEF_BD)
2328 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2330 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2332 kvm_debug("Delivering MSAFPE @ pc %#lx\n", arch->pc);
2334 kvm_change_c0_guest_cause(cop0, (0xff),
2335 (EXCCODE_MSAFPE << CAUSEB_EXCCODE));
2337 /* Set PC to the exception entry point */
2338 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2341 kvm_err("Trying to deliver MSAFPE when EXL is already set\n");
2348 enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause,
2350 struct kvm_run *run,
2351 struct kvm_vcpu *vcpu)
2353 struct mips_coproc *cop0 = vcpu->arch.cop0;
2354 struct kvm_vcpu_arch *arch = &vcpu->arch;
2355 enum emulation_result er = EMULATE_DONE;
2357 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2359 kvm_write_c0_guest_epc(cop0, arch->pc);
2360 kvm_set_c0_guest_status(cop0, ST0_EXL);
2362 if (cause & CAUSEF_BD)
2363 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2365 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2367 kvm_debug("Delivering FPE @ pc %#lx\n", arch->pc);
2369 kvm_change_c0_guest_cause(cop0, (0xff),
2370 (EXCCODE_FPE << CAUSEB_EXCCODE));
2372 /* Set PC to the exception entry point */
2373 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2376 kvm_err("Trying to deliver FPE when EXL is already set\n");
2383 enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause,
2385 struct kvm_run *run,
2386 struct kvm_vcpu *vcpu)
2388 struct mips_coproc *cop0 = vcpu->arch.cop0;
2389 struct kvm_vcpu_arch *arch = &vcpu->arch;
2390 enum emulation_result er = EMULATE_DONE;
2392 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2394 kvm_write_c0_guest_epc(cop0, arch->pc);
2395 kvm_set_c0_guest_status(cop0, ST0_EXL);
2397 if (cause & CAUSEF_BD)
2398 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2400 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2402 kvm_debug("Delivering MSADIS @ pc %#lx\n", arch->pc);
2404 kvm_change_c0_guest_cause(cop0, (0xff),
2405 (EXCCODE_MSADIS << CAUSEB_EXCCODE));
2407 /* Set PC to the exception entry point */
2408 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2411 kvm_err("Trying to deliver MSADIS when EXL is already set\n");
2418 enum emulation_result kvm_mips_handle_ri(u32 cause, u32 *opc,
2419 struct kvm_run *run,
2420 struct kvm_vcpu *vcpu)
2422 struct mips_coproc *cop0 = vcpu->arch.cop0;
2423 struct kvm_vcpu_arch *arch = &vcpu->arch;
2424 enum emulation_result er = EMULATE_DONE;
2425 unsigned long curr_pc;
2426 union mips_instruction inst;
2430 * Update PC and hold onto current PC in case there is
2431 * an error and we want to rollback the PC
2433 curr_pc = vcpu->arch.pc;
2434 er = update_pc(vcpu, cause);
2435 if (er == EMULATE_FAIL)
2438 /* Fetch the instruction. */
2439 if (cause & CAUSEF_BD)
2441 err = kvm_get_badinstr(opc, vcpu, &inst.word);
2443 kvm_err("%s: Cannot get inst @ %p (%d)\n", __func__, opc, err);
2444 return EMULATE_FAIL;
2447 if (inst.r_format.opcode == spec3_op &&
2448 inst.r_format.func == rdhwr_op &&
2449 inst.r_format.rs == 0 &&
2450 (inst.r_format.re >> 3) == 0) {
2451 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
2452 int rd = inst.r_format.rd;
2453 int rt = inst.r_format.rt;
2454 int sel = inst.r_format.re & 0x7;
2456 /* If usermode, check RDHWR rd is allowed by guest HWREna */
2457 if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
2458 kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
2463 case MIPS_HWR_CPUNUM: /* CPU number */
2464 arch->gprs[rt] = vcpu->vcpu_id;
2466 case MIPS_HWR_SYNCISTEP: /* SYNCI length */
2467 arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
2468 current_cpu_data.icache.linesz);
2470 case MIPS_HWR_CC: /* Read count register */
2471 arch->gprs[rt] = (s32)kvm_mips_read_count(vcpu);
2473 case MIPS_HWR_CCRES: /* Count register resolution */
2474 switch (current_cpu_data.cputype) {
2483 case MIPS_HWR_ULR: /* Read UserLocal register */
2484 arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
2488 kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
2492 trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR, KVM_TRACE_HWR(rd, sel),
2493 vcpu->arch.gprs[rt]);
2495 kvm_debug("Emulate RI not supported @ %p: %#x\n",
2500 return EMULATE_DONE;
2504 * Rollback PC (if in branch delay slot then the PC already points to
2505 * branch target), and pass the RI exception to the guest OS.
2507 vcpu->arch.pc = curr_pc;
2508 return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
2511 enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
2512 struct kvm_run *run)
2514 unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
2515 enum emulation_result er = EMULATE_DONE;
2517 if (run->mmio.len > sizeof(*gpr)) {
2518 kvm_err("Bad MMIO length: %d", run->mmio.len);
2523 /* Restore saved resume PC */
2524 vcpu->arch.pc = vcpu->arch.io_pc;
2526 switch (run->mmio.len) {
2528 *gpr = *(s32 *) run->mmio.data;
2532 if (vcpu->mmio_needed == 2)
2533 *gpr = *(s16 *) run->mmio.data;
2535 *gpr = *(u16 *)run->mmio.data;
2539 if (vcpu->mmio_needed == 2)
2540 *gpr = *(s8 *) run->mmio.data;
2542 *gpr = *(u8 *) run->mmio.data;
2550 static enum emulation_result kvm_mips_emulate_exc(u32 cause,
2552 struct kvm_run *run,
2553 struct kvm_vcpu *vcpu)
2555 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
2556 struct mips_coproc *cop0 = vcpu->arch.cop0;
2557 struct kvm_vcpu_arch *arch = &vcpu->arch;
2558 enum emulation_result er = EMULATE_DONE;
2560 if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
2562 kvm_write_c0_guest_epc(cop0, arch->pc);
2563 kvm_set_c0_guest_status(cop0, ST0_EXL);
2565 if (cause & CAUSEF_BD)
2566 kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
2568 kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
2570 kvm_change_c0_guest_cause(cop0, (0xff),
2571 (exccode << CAUSEB_EXCCODE));
2573 /* Set PC to the exception entry point */
2574 arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
2575 kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
2577 kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
2578 exccode, kvm_read_c0_guest_epc(cop0),
2579 kvm_read_c0_guest_badvaddr(cop0));
2581 kvm_err("Trying to deliver EXC when EXL is already set\n");
2588 enum emulation_result kvm_mips_check_privilege(u32 cause,
2590 struct kvm_run *run,
2591 struct kvm_vcpu *vcpu)
2593 enum emulation_result er = EMULATE_DONE;
2594 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
2595 unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
2597 int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
2606 case EXCCODE_MSAFPE:
2608 case EXCCODE_MSADIS:
2612 if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
2613 er = EMULATE_PRIV_FAIL;
2621 * We we are accessing Guest kernel space, then send an
2622 * address error exception to the guest
2624 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
2625 kvm_debug("%s: LD MISS @ %#lx\n", __func__,
2628 cause |= (EXCCODE_ADEL << CAUSEB_EXCCODE);
2629 er = EMULATE_PRIV_FAIL;
2635 * We we are accessing Guest kernel space, then send an
2636 * address error exception to the guest
2638 if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
2639 kvm_debug("%s: ST MISS @ %#lx\n", __func__,
2642 cause |= (EXCCODE_ADES << CAUSEB_EXCCODE);
2643 er = EMULATE_PRIV_FAIL;
2648 kvm_debug("%s: address error ST @ %#lx\n", __func__,
2650 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2652 cause |= (EXCCODE_TLBS << CAUSEB_EXCCODE);
2654 er = EMULATE_PRIV_FAIL;
2657 kvm_debug("%s: address error LD @ %#lx\n", __func__,
2659 if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
2661 cause |= (EXCCODE_TLBL << CAUSEB_EXCCODE);
2663 er = EMULATE_PRIV_FAIL;
2666 er = EMULATE_PRIV_FAIL;
2671 if (er == EMULATE_PRIV_FAIL)
2672 kvm_mips_emulate_exc(cause, opc, run, vcpu);
2678 * User Address (UA) fault, this could happen if
2679 * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
2680 * case we pass on the fault to the guest kernel and let it handle it.
2681 * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
2682 * case we inject the TLB from the Guest TLB into the shadow host TLB
2684 enum emulation_result kvm_mips_handle_tlbmiss(u32 cause,
2686 struct kvm_run *run,
2687 struct kvm_vcpu *vcpu,
2690 enum emulation_result er = EMULATE_DONE;
2691 u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
2692 unsigned long va = vcpu->arch.host_cp0_badvaddr;
2695 kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx\n",
2696 vcpu->arch.host_cp0_badvaddr);
2699 * KVM would not have got the exception if this entry was valid in the
2700 * shadow host TLB. Check the Guest TLB, if the entry is not there then
2701 * send the guest an exception. The guest exc handler should then inject
2702 * an entry into the guest TLB.
2704 index = kvm_mips_guest_tlb_lookup(vcpu,
2706 (kvm_read_c0_guest_entryhi(vcpu->arch.cop0) &
2709 if (exccode == EXCCODE_TLBL) {
2710 er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
2711 } else if (exccode == EXCCODE_TLBS) {
2712 er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
2714 kvm_err("%s: invalid exc code: %d\n", __func__,
2719 struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
2722 * Check if the entry is valid, if not then setup a TLB invalid
2723 * exception to the guest
2725 if (!TLB_IS_VALID(*tlb, va)) {
2726 if (exccode == EXCCODE_TLBL) {
2727 er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
2729 } else if (exccode == EXCCODE_TLBS) {
2730 er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
2733 kvm_err("%s: invalid exc code: %d\n", __func__,
2738 kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
2739 tlb->tlb_hi, tlb->tlb_lo[0], tlb->tlb_lo[1]);
2741 * OK we have a Guest TLB entry, now inject it into the
2744 if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, va,
2746 kvm_err("%s: handling mapped seg tlb fault for %lx, index: %u, vcpu: %p, ASID: %#lx\n",
2747 __func__, va, index, vcpu,