The following patch allows to activate a partition reference
time enlightenment that is based on the host platform's support
for an Invariant Time Stamp Counter (iTSC).

v2 -> v3
    Handle TSC sequence, scale, and offest changing during migration.

v3 -> v4
    1. Wrap access to iTSC page with kvm_read_guest/kvm_write_guest
    suggested by Andrew Honig <[email protected]> and Marcelo
    2. iTSC data calculation/access fixes suggested by Marcelo and Paolo

---
 arch/x86/kvm/x86.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 496bdb1..6e6debf 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1873,6 +1873,7 @@ static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 
msr, u64 data)
        case HV_X64_MSR_REFERENCE_TSC: {
                u64 gfn;
                unsigned long addr;
+               struct kvm_arch *ka = &kvm->arch;
                HV_REFERENCE_TSC_PAGE tsc_ref;
                memset(&tsc_ref, 0, sizeof(tsc_ref));
                if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE)) {
@@ -1883,6 +1884,13 @@ static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 
msr, u64 data)
                addr = gfn_to_hva(kvm, gfn);
                if (kvm_is_error_hva(addr))
                        return 1;
+               if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC)
+                   && ka->use_master_clock) {
+                       tsc_ref.tsc_sequence = 1;
+                       tsc_ref.tsc_scale = ((10000LL << 32) /
+                                            vcpu->arch.virtual_tsc_khz) << 32;
+                       tsc_ref.tsc_offset = kvm->arch.kvmclock_offset;
+               }
                if (__copy_to_user((void __user *)addr, &tsc_ref, 
sizeof(tsc_ref)))
                        return 1;
                mark_page_dirty(kvm, gfn);
@@ -3871,6 +3879,27 @@ long kvm_arch_vm_ioctl(struct file *filp,
                local_irq_enable();
                kvm->arch.kvmclock_offset = delta;
                kvm_gen_update_masterclock(kvm);
+               if (kvm->arch.hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) {
+                       HV_REFERENCE_TSC_PAGE tsc_ref;
+                       struct kvm_arch *ka = &kvm->arch;
+                       r = kvm_read_guest(kvm, kvm->arch.hv_tsc_page,
+                                          &tsc_ref, sizeof(tsc_ref));
+                       if (r)
+                               goto out;
+                       if (tsc_ref.tsc_sequence
+                           && boot_cpu_has(X86_FEATURE_CONSTANT_TSC)
+                           && ka->use_master_clock) {
+                               tsc_ref.tsc_sequence++;
+                               tsc_ref.tsc_scale = ((10000LL << 32) /
+                                       __get_cpu_var(cpu_tsc_khz)) << 32;
+                               tsc_ref.tsc_offset = kvm->arch.kvmclock_offset;
+                       } else
+                               tsc_ref.tsc_sequence = 0;
+                       r = kvm_write_guest(kvm, kvm->arch.hv_tsc_page,
+                                           &tsc_ref, sizeof(tsc_ref));
+                       if (r)
+                               goto out;
+               }
                break;
        }
        case KVM_GET_CLOCK: {
-- 
1.8.1.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to