Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: MIPS/Emulate: Update CP0_Compare emulation for VZ

Update emulation of guest writes to CP0_Compare for VZ. There are two
main differences compared to trap & emulate:

- Writing to CP0_Compare in the VZ hardware guest context acks any
pending timer, clearing CP0_Cause.TI. If we don't want an ack to take
place we must carefully restore the TI bit if it was previously set.

- Even with guest timer access disabled in CP0_GuestCtl0.GT, if the
guest CP0_Count reaches the guest CP0_Compare the timer interrupt
will assert. To prevent this we must set CP0_GTOffset to move the
guest CP0_Count out of the way of the new guest CP0_Compare, either
before or after depending on whether it is a forwards or backwards
change.

Signed-off-by: James Hogan <james.hogan@imgtec.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Radim Krčmář" <rkrcmar@redhat.com>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: linux-mips@linux-mips.org
Cc: kvm@vger.kernel.org

+42 -1
+42 -1
arch/mips/kvm/emulate.c
··· 621 621 struct mips_coproc *cop0 = vcpu->arch.cop0; 622 622 int dc; 623 623 u32 old_compare = kvm_read_c0_guest_compare(cop0); 624 - ktime_t now; 624 + s32 delta = compare - old_compare; 625 + u32 cause; 626 + ktime_t now = ktime_set(0, 0); /* silence bogus GCC warning */ 625 627 u32 count; 626 628 627 629 /* if unchanged, must just be an ack */ ··· 635 633 return; 636 634 } 637 635 636 + /* 637 + * If guest CP0_Compare moves forward, CP0_GTOffset should be adjusted 638 + * too to prevent guest CP0_Count hitting guest CP0_Compare. 639 + * 640 + * The new GTOffset corresponds to the new value of CP0_Compare, and is 641 + * set prior to it being written into the guest context. We disable 642 + * preemption until the new value is written to prevent restore of a 643 + * GTOffset corresponding to the old CP0_Compare value. 644 + */ 645 + if (IS_ENABLED(CONFIG_KVM_MIPS_VZ) && delta > 0) { 646 + preempt_disable(); 647 + write_c0_gtoffset(compare - read_c0_count()); 648 + back_to_back_c0_hazard(); 649 + } 650 + 638 651 /* freeze_hrtimer() takes care of timer interrupts <= count */ 639 652 dc = kvm_mips_count_disabled(vcpu); 640 653 if (!dc) ··· 657 640 658 641 if (ack) 659 642 kvm_mips_callbacks->dequeue_timer_int(vcpu); 643 + else if (IS_ENABLED(CONFIG_KVM_MIPS_VZ)) 644 + /* 645 + * With VZ, writing CP0_Compare acks (clears) CP0_Cause.TI, so 646 + * preserve guest CP0_Cause.TI if we don't want to ack it. 647 + */ 648 + cause = kvm_read_c0_guest_cause(cop0); 660 649 661 650 kvm_write_c0_guest_compare(cop0, compare); 651 + 652 + if (IS_ENABLED(CONFIG_KVM_MIPS_VZ)) { 653 + if (delta > 0) 654 + preempt_enable(); 655 + 656 + back_to_back_c0_hazard(); 657 + 658 + if (!ack && cause & CAUSEF_TI) 659 + kvm_write_c0_guest_cause(cop0, cause); 660 + } 662 661 663 662 /* resume_hrtimer() takes care of timer interrupts > count */ 664 663 if (!dc) 665 664 kvm_mips_resume_hrtimer(vcpu, now, count); 665 + 666 + /* 667 + * If guest CP0_Compare is moving backward, we delay CP0_GTOffset change 668 + * until after the new CP0_Compare is written, otherwise new guest 669 + * CP0_Count could hit new guest CP0_Compare. 670 + */ 671 + if (IS_ENABLED(CONFIG_KVM_MIPS_VZ) && delta <= 0) 672 + write_c0_gtoffset(compare - read_c0_count()); 666 673 } 667 674 668 675 /**