From patchwork Tue Jan 14 09:22:17 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vadim Rozenfeld X-Patchwork-Id: 3484841 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 14FD1C02DC for ; Tue, 14 Jan 2014 09:23:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AB0D320179 for ; Tue, 14 Jan 2014 09:23:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1C743201CE for ; Tue, 14 Jan 2014 09:23:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751310AbaANJW6 (ORCPT ); Tue, 14 Jan 2014 04:22:58 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60932 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751207AbaANJW4 (ORCPT ); Tue, 14 Jan 2014 04:22:56 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s0E9MobP018026 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 14 Jan 2014 04:22:53 -0500 Received: from localhost.localdomain (vpn-201-49.tlv.redhat.com [10.35.201.49]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s0E9MVB4012645; Tue, 14 Jan 2014 04:22:45 -0500 From: Vadim Rozenfeld To: kvm@vger.kernel.org Cc: mtosatti@redhat.com, pbonzini@redhat.com, pl@dlhnet.de, Vadim Rozenfeld Subject: [RFC PATCH v4 2/2] add support for Hyper-V partition reference time enlightenment Date: Tue, 14 Jan 2014 20:22:17 +1100 Message-Id: <1389691337-12050-3-git-send-email-vrozenfe@redhat.com> In-Reply-To: <1389691337-12050-1-git-send-email-vrozenfe@redhat.com> References: <1389691337-12050-1-git-send-email-vrozenfe@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-7.0 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 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: {