From nobody Sun Feb 8 14:59:25 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1559564920; cv=none; d=zoho.com; s=zohoarc; b=IXVIyOQuUadW80b+nYvrdiLcdMnjb3kwzTCKr9iaLUR9DGDh9iHxmTRYUm7sCPmHeBZXrxHXDbECiCl3huHnG51xp1+uFWvzGSqFRZq2/7DlCw5nBTerb1Ml+qkjJAPVPl4fx9mgknBt5USks/wUmfgLVg2e2uy4tRjxX8BYvTQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1559564920; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=KKwR/BlPtjHM4IkS3t6d+UFz7FeT0PBdc/4kkevSkHc=; b=ALd36F+OWUbmX5KxoJ+0XwsHOQ0cq2R1YLCvZ/DszfxbP2u1eb+kIkePWd5Ig2S+FcpJ/ta07V3ESCylWmtSXY1y8FQ8jEhvj1lTJybI86pqfDCl8UkrEjeL1yK5+uOOooiljIkg9L2gqRIJNkaz4Kc/lyr7ViKrnpChg/zPvyY= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1559564920992287.07975124676466; Mon, 3 Jun 2019 05:28:40 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 25C693107B0F; Mon, 3 Jun 2019 12:28:39 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EFE352E056; Mon, 3 Jun 2019 12:28:38 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B15E519729; Mon, 3 Jun 2019 12:28:36 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x53CS4iq003609 for ; Mon, 3 Jun 2019 08:28:04 -0400 Received: by smtp.corp.redhat.com (Postfix) id 196A72AABD; Mon, 3 Jun 2019 12:28:04 +0000 (UTC) Received: from orkuz.int.mamuti.net (unknown [10.40.205.163]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B68534522 for ; Mon, 3 Jun 2019 12:28:01 +0000 (UTC) Received: by orkuz.int.mamuti.net (Postfix, from userid 500) id 827442A5DF4; Mon, 3 Jun 2019 14:27:59 +0200 (CEST) From: Jiri Denemark To: libvir-list@redhat.com Date: Mon, 3 Jun 2019 14:27:54 +0200 Message-Id: <53f953283efc457e3c80b746d55fcaee4531a4c8.1559564736.git.jdenemar@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 3/7] util: Add virHostCPUGetTscInfo X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Mon, 03 Jun 2019 12:28:39 +0000 (UTC) On a KVM x86_64 host which supports invariant TSC this function can be used to detect the TSC frequency and the availability of TSC scaling. The magic MSR numbers required to check if VMX scaling is supported on the host are documented in Volume 3 of the Intel=C2=AE 64 and IA-32 Architectures Software Developer=E2=80=99s Manual. Signed-off-by: Jiri Denemark Reviewed-by: J=C3=A1n Tomko --- src/util/virhostcpu.c | 71 +++++++++++++++++++++++++++++++++++++++++++ src/util/virhostcpu.h | 11 +++++++ 2 files changed, 82 insertions(+) diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index f4a62c74fa..8c00804b0e 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -1324,6 +1324,69 @@ virHostCPUGetMSR(unsigned long index, return virHostCPUGetMSRFromKVM(index, msr); } =20 + +# define VMX_PROCBASED_CTLS2_MSR 0x48b +# define VMX_USE_TSC_SCALING (1 << 25) + +/* + * This function should only be called when the host CPU supports invarian= t TSC + * (invtsc CPUID feature). + * + * Returns pointer to the TSC info structure on success, + * NULL when TSC cannot be probed otherwise. + */ +virHostCPUTscInfoPtr +virHostCPUGetTscInfo(void) +{ + virHostCPUTscInfoPtr info; + VIR_AUTOCLOSE kvmFd =3D -1; + VIR_AUTOCLOSE vmFd =3D -1; + VIR_AUTOCLOSE vcpuFd =3D -1; + uint64_t msr =3D 0; + int rc; + + if ((kvmFd =3D open(KVM_DEVICE, O_RDONLY)) < 0) { + virReportSystemError(errno, _("Unable to open %s"), KVM_DEVICE); + return NULL; + } + + if ((vmFd =3D ioctl(kvmFd, KVM_CREATE_VM, 0)) < 0) { + virReportSystemError(errno, "%s", + _("Unable to create KVM VM for TSC probing")); + return NULL; + } + + if ((vcpuFd =3D ioctl(vmFd, KVM_CREATE_VCPU, 0)) < 0) { + virReportSystemError(errno, "%s", + _("Unable to create KVM vCPU for TSC probing"= )); + return NULL; + } + + if ((rc =3D ioctl(vcpuFd, KVM_GET_TSC_KHZ)) < 0) { + virReportSystemError(errno, "%s", + _("Unable to probe TSC frequency")); + return NULL; + } + + if (VIR_ALLOC(info) < 0) + return NULL; + + info->frequency =3D rc * 1000ULL; + + if (virHostCPUGetMSR(VMX_PROCBASED_CTLS2_MSR, &msr) =3D=3D 0) { + /* High 32 bits of the MSR value indicate whether specific control + * can be set to 1. */ + msr >>=3D 32; + + info->scaling =3D virTristateBoolFromBool(!!(msr & VMX_USE_TSC_SCA= LING)); + } + + VIR_DEBUG("Detected TSC frequency %llu Hz, scaling %s", + info->frequency, virTristateBoolTypeToString(info->scaling)); + + return info; +} + #else =20 int @@ -1335,6 +1398,14 @@ virHostCPUGetMSR(unsigned long index ATTRIBUTE_UNUSE= D, return -1; } =20 +virHostCPUTscInfoPtr +virHostCPUGetTscInfo(void) +{ + virReportSystemError(ENOSYS, "%s", + _("Probing TSC is not supported on this platform"= )); + return NULL; +} + #endif /* HAVE_LINUX_KVM_H && defined(KVM_GET_MSRS) && \ (defined(__i386__) || defined(__x86_64__)) && \ (defined(__linux__) || defined(__FreeBSD__)) */ diff --git a/src/util/virhostcpu.h b/src/util/virhostcpu.h index 0d20dbef61..b822bc11a8 100644 --- a/src/util/virhostcpu.h +++ b/src/util/virhostcpu.h @@ -25,6 +25,15 @@ # include "internal.h" # include "virarch.h" # include "virbitmap.h" +# include "virenum.h" + + +typedef struct _virHostCPUTscInfo virHostCPUTscInfo; +typedef virHostCPUTscInfo *virHostCPUTscInfoPtr; +struct _virHostCPUTscInfo { + unsigned long long frequency; + virTristateBool scaling; +}; =20 =20 int virHostCPUGetStats(int cpuNum, @@ -69,4 +78,6 @@ unsigned int virHostCPUGetMicrocodeVersion(void); int virHostCPUGetMSR(unsigned long index, uint64_t *msr); =20 +virHostCPUTscInfoPtr virHostCPUGetTscInfo(void); + #endif /* LIBVIRT_VIRHOSTCPU_H */ --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list