From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, Sean Christopherson <seanjc@google.com>,
Mathias Krause <minipli@grsecurity.net>,
Andrew Jones <andrew.jones@linux.dev>
Subject: [kvm-unit-tests PATCH v3 15/20] x86/kvmclock: Skip kvmclock test when not running on KVM with CLOCKSOURCE2
Date: Thu, 14 May 2026 14:04:55 -0700 [thread overview]
Message-ID: <20260514210500.1626871-16-seanjc@google.com> (raw)
In-Reply-To: <20260514210500.1626871-1-seanjc@google.com>
Skip the kvmclock test if the (virtual) CPU isn't running on KVM and/or
doesn't have CLOCKSOURCE2. Presumably non-KVM environments simply don't
run the test, but checking for kvmclock support is easy enough. E.g. with
-cpu host,-kvmclock,kvm-pv-enforce-cpuid
the test will die on WRMSR #GPs without the checks, but generate
SKIP: CPU not running on KVM with CLOCKSOURCE2
SUMMARY: 1 tests, 1 skipped
with the appropriate checks.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
lib/x86/processor.h | 15 +++++++++++++++
x86/kvmclock.h | 2 ++
x86/kvmclock_test.c | 9 ++++++++-
3 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/lib/x86/processor.h b/lib/x86/processor.h
index 32ce08e2..ba7065f7 100644
--- a/lib/x86/processor.h
+++ b/lib/x86/processor.h
@@ -317,6 +317,7 @@ struct x86_cpu_feature {
#define X86_FEATURE_XSAVE X86_CPU_FEATURE(0x1, 0, ECX, 26)
#define X86_FEATURE_OSXSAVE X86_CPU_FEATURE(0x1, 0, ECX, 27)
#define X86_FEATURE_RDRAND X86_CPU_FEATURE(0x1, 0, ECX, 30)
+#define X86_FEATURE_HYPERVISOR X86_CPU_FEATURE(0x1, 0, ECX, 31)
#define X86_FEATURE_MCE X86_CPU_FEATURE(0x1, 0, EDX, 7)
#define X86_FEATURE_APIC X86_CPU_FEATURE(0x1, 0, EDX, 9)
#define X86_FEATURE_CLFLUSH X86_CPU_FEATURE(0x1, 0, EDX, 19)
@@ -351,6 +352,7 @@ struct x86_cpu_feature {
/*
* KVM defined leafs
*/
+#define KVM_FEATURE_CLOCKSOURCE2 X86_CPU_FEATURE(0x40000001, 0, EAX, 3)
#define KVM_FEATURE_ASYNC_PF X86_CPU_FEATURE(0x40000001, 0, EAX, 4)
#define KVM_FEATURE_ASYNC_PF_INT X86_CPU_FEATURE(0x40000001, 0, EAX, 14)
@@ -449,6 +451,7 @@ struct x86_cpu_property {
#define X86_PROPERTY_AMX_NR_TILE_REGS X86_CPU_PROPERTY(0x1d, 1, EBX, 16, 31)
#define X86_PROPERTY_AMX_MAX_ROWS X86_CPU_PROPERTY(0x1d, 1, ECX, 0, 15)
+#define KVM_SIGNATURE "KVMKVMKVM\0\0\0"
#define X86_PROPERTY_MAX_KVM_LEAF X86_CPU_PROPERTY(0x40000000, 0, EAX, 0, 31)
#define X86_PROPERTY_MAX_EXT_LEAF X86_CPU_PROPERTY(0x80000000, 0, EAX, 0, 31)
@@ -506,6 +509,18 @@ static __always_inline bool this_cpu_has_p(struct x86_cpu_property property)
return max_leaf >= property.function;
}
+static inline bool this_cpu_has_kvm(void)
+{
+ struct cpuid signature;
+
+ if (!this_cpu_has(X86_FEATURE_HYPERVISOR) ||
+ !this_cpu_has_p(X86_PROPERTY_MAX_KVM_LEAF))
+ return false;
+
+ signature = cpuid(X86_PROPERTY_MAX_KVM_LEAF.function);
+ return !memcmp(KVM_SIGNATURE, &signature.b, 12);
+}
+
static inline u8 cpuid_maxphyaddr(void)
{
if (!this_cpu_has_p(X86_PROPERTY_MAX_PHY_ADDR))
diff --git a/x86/kvmclock.h b/x86/kvmclock.h
index 1a40a7c0..bde9a21f 100644
--- a/x86/kvmclock.h
+++ b/x86/kvmclock.h
@@ -1,6 +1,8 @@
#ifndef X86_KVMCLOCK_H
#define X86_KVMCLOCK_H
+#include "libcflat.h"
+
#define MSR_KVM_WALL_CLOCK_NEW 0x4b564d00
#define MSR_KVM_SYSTEM_TIME_NEW 0x4b564d01
diff --git a/x86/kvmclock_test.c b/x86/kvmclock_test.c
index d21c6c72..659be870 100644
--- a/x86/kvmclock_test.c
+++ b/x86/kvmclock_test.c
@@ -108,6 +108,11 @@ int main(int ac, char **av)
int ncpus;
int i;
+ if (!this_cpu_has_kvm() || !this_cpu_has(KVM_FEATURE_CLOCKSOURCE2)) {
+ report_skip("CPU not running on KVM with CLOCKSOURCE2");
+ goto out;
+ }
+
if (ac > 1)
loops = atol(av[1]);
if (ac > 2)
@@ -151,5 +156,7 @@ int main(int ac, char **av)
on_cpus(kvm_clock_clear, NULL);
- return nerr > 0 ? 1 : 0;
+ report(!nerr, "%u time warps detected", nerr);
+out:
+ return report_summary();
}
--
2.54.0.563.g4f69b47b94-goog
next prev parent reply other threads:[~2026-05-14 21:05 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-14 21:04 [kvm-unit-tests PATCH v3 00/20] x86: Better backtraces for leaf functions Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 01/20] x86/vmx: Drop unused SYSENTER "support" in nested VMX infrastructure Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 02/20] x86/vmx: Drop unused guest_regs " Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 03/20] x86/svm: Sort (and swap) GPRs by their index, not alphabetically Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 04/20] x86: Dedup guest/host context switch of registers across SVM and VMX Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 05/20] x86/virt: Use macro shenanigans to get reg offsets when swapping guest/host regs Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 06/20] x86/virt: Track "guest regs" using per-CPU variable Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 07/20] x86/svm: Don't VMLOAD/VMSAVE "guest" state around VMRUN Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 08/20] x86/vmx: Use separate VMCSes for BSP vs. AP in INIT test Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 09/20] x86/vmx: Swap GPRs after checking "launched" status Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 10/20] x86/vmx: Track VMCS "launched" state per-CPU Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 11/20] x86/vmx: Track "is this CPU in guest mode" per-CPU Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 12/20] x86/vmx: Communicate hypercalls via RAX, not a global field Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 13/20] x86/vmx: Initialize test stage in SIPI test *before* launching AP thread Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 14/20] x86/kvmclock: Replace spaces with tabs Sean Christopherson
2026-05-14 21:04 ` Sean Christopherson [this message]
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 16/20] x86/vmx: Tag "struct vmx_msr_entry" as needing to be 16-byte aligned Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 17/20] x86/smp: Align the stack to a 16-byte boundary when invoking SMP function calls Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 18/20] x86/vmx: Write to KVM's WALL_CLOCK MSR via VM-Entry load list sync in SIPI test Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 19/20] x86: Better backtraces for leaf functions Sean Christopherson
2026-05-14 21:05 ` [kvm-unit-tests PATCH v3 20/20] x86: Prevent realmode test code instrumentation with nop-mcount Sean Christopherson
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=20260514210500.1626871-16-seanjc@google.com \
--to=seanjc@google.com \
--cc=andrew.jones@linux.dev \
--cc=kvm@vger.kernel.org \
--cc=minipli@grsecurity.net \
--cc=pbonzini@redhat.com \
/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