From: David Woodhouse <dwmw2@infradead.org>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
graf@amazon.de, Ajay Kaher <ajay.kaher@broadcom.com>,
Alexey Makhalov <alexey.makhalov@broadcom.com>,
Alok N Kataria <akataria@vmware.com>
Subject: [PATCH 2/3] KVM: x86: Provide TSC frequency in "generic" timing infomation CPUID leaf
Date: Thu, 14 Aug 2025 12:56:04 +0100 [thread overview]
Message-ID: <20250814120237.2469583-3-dwmw2@infradead.org> (raw)
In-Reply-To: <20250814120237.2469583-1-dwmw2@infradead.org>
From: David Woodhouse <dwmw@amazon.co.uk>
In https://lkml.org/lkml/2008/10/1/246 a proposal was made for generic
CPUID leaves, of which only 0x40000010 was defined, to contain the TSC
and local APIC frequencies. The proposal from VMware was mostly shot
down in flames, *but* XNU does unconditionally assume that this leaf
contains the frequency information, if it's present on any hypervisor:
https://github.com/apple/darwin-xnu/blob/main/osfmk/i386/cpuid.c
So does FreeBSD: https://github.com/freebsd/freebsd-src/commit/4a432614f68
So at this point it would be daft for a hypervisor to expose 0x40000010
for any *other* content. KVM might as well adopt it, and fill in the
accurate TSC frequency just as it does for the Xen TSC leaf.
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
arch/x86/include/uapi/asm/kvm_para.h | 11 +++++++++++
arch/x86/kvm/cpuid.c | 7 +++++++
2 files changed, 18 insertions(+)
diff --git a/arch/x86/include/uapi/asm/kvm_para.h b/arch/x86/include/uapi/asm/kvm_para.h
index a1efa7907a0b..1597c4a2a24a 100644
--- a/arch/x86/include/uapi/asm/kvm_para.h
+++ b/arch/x86/include/uapi/asm/kvm_para.h
@@ -44,6 +44,17 @@
*/
#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24
+
+/*
+ * Proposed by VMware in https://lkml.org/lkml/2008/10/1/246 the timing
+ * information leaf provides the TSC and local APIC timer frequencies:
+ *
+ * # EAX: (Virtual) TSC frequency in kHz.
+ * # EBX: (Virtual) Bus (local apic timer) frequency in kHz.
+ * # ECX, EDX: RESERVED (reserved fields are set to zero).
+ */
+#define KVM_CPUID_TIMING_INFO 0x40000010
+
#define MSR_KVM_WALL_CLOCK 0x11
#define MSR_KVM_SYSTEM_TIME 0x12
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index bcce3a75c3f2..1bd69d9c86b7 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -2029,6 +2029,13 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
} else if (index == 2) {
*eax = vcpu->arch.hw_tsc_khz;
}
+ } else if (vcpu->arch.kvm_cpuid.base &&
+ function <= vcpu->arch.kvm_cpuid.limit &&
+ function == (vcpu->arch.kvm_cpuid.base | KVM_CPUID_TIMING_INFO)) {
+ if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu))
+ kvm_guest_time_update(vcpu);
+
+ *eax = vcpu->arch.hw_tsc_khz;
}
} else {
*eax = *ebx = *ecx = *edx = 0;
--
2.49.0
next prev parent reply other threads:[~2025-08-14 12:03 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-14 11:56 [PATCH 0/3] Support "generic" CPUID timing leaf as KVM guest and host David Woodhouse
2025-08-14 11:56 ` [PATCH 1/3] KVM: x86: Restore caching of KVM CPUID base David Woodhouse
2025-08-14 11:56 ` David Woodhouse [this message]
2025-08-14 11:56 ` [PATCH 3/3] x86/kvm: Obtain TSC frequency from CPUID if present David Woodhouse
2025-08-15 15:24 ` [syzbot ci] Re: Support "generic" CPUID timing leaf as KVM guest and host syzbot ci
2025-08-15 15:36 ` David Woodhouse
2025-08-15 15:38 ` syzbot ci
2025-08-15 15:43 ` Aleksandr Nogikh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250814120237.2469583-3-dwmw2@infradead.org \
--to=dwmw2@infradead.org \
--cc=ajay.kaher@broadcom.com \
--cc=akataria@vmware.com \
--cc=alexey.makhalov@broadcom.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=graf@amazon.de \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--cc=vkuznets@redhat.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).