git: 493676d3b271 - stable/15 - kvm: Support non-default CPUID leaf

ShengYi Hung <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a64b743.27035.4c5559dc__13831.8979110167$1784985429$gmane$org@gitrepo.freebsd.org>
The branch stable/15 has been updated by aokblast:

URL: https://cgit.FreeBSD.org/src/commit/?id=493676d3b2717c12ee09d84a5b73758f75a66278

commit 493676d3b2717c12ee09d84a5b73758f75a66278
Author:     ShengYi Hung <[email protected]>
AuthorDate: 2026-07-10 09:21:11 +0000
Commit:     ShengYi Hung <[email protected]>
CommitDate: 2026-07-25 13:13:24 +0000

    kvm: Support non-default CPUID leaf
    
    KVM does not always use 0x40000000 as its CPUID base. For example, QEMU
    adds a 0x100 offset when nested virtualization is detected and the host
    exposes Hyper-V enlightenment hints. To accommodate this behavior,
    switch the detection logic to use the CPUID leaf returned by do_cpuid(),
    making the implementation more flexible.
    
    See:
    https://github.com/qemu/qemu/blob/master/target/i386/kvm/kvm.c#L2300
    
    Reviewed by:    kib
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D58146
    
    (cherry picked from commit 86691d52a6d3796ad36ba474cf0a9493f6d99202)
---
 sys/x86/include/kvm.h | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/sys/x86/include/kvm.h b/sys/x86/include/kvm.h
index 83dd20fa8d23..5414075f89d6 100644
--- a/sys/x86/include/kvm.h
+++ b/sys/x86/include/kvm.h
@@ -45,7 +45,7 @@
 #include <machine/md_var.h>
 
 #define	KVM_CPUID_SIGNATURE			0x40000000
-#define	KVM_CPUID_FEATURES_LEAF			0x40000001
+#define	KVM_CPUID_FEATURES_LEAF(base)		((base) + 1)
 
 #define	KVM_FEATURE_CLOCKSOURCE			0x00000001
 #define	KVM_FEATURE_CLOCKSOURCE2		0x00000008
@@ -63,8 +63,7 @@ static inline bool
 kvm_cpuid_features_leaf_supported(void)
 {
 	return (vm_guest == VM_GUEST_KVM &&
-	    KVM_CPUID_FEATURES_LEAF > hv_base &&
-	    KVM_CPUID_FEATURES_LEAF <= hv_high);
+	    KVM_CPUID_FEATURES_LEAF(hv_base) <= hv_high);
 }
 
 static inline void
@@ -73,7 +72,7 @@ kvm_cpuid_get_features(u_int *regs)
 	if (!kvm_cpuid_features_leaf_supported())
 		regs[0] = regs[1] = regs[2] = regs[3] = 0;
 	else
-		do_cpuid(KVM_CPUID_FEATURES_LEAF, regs);
+		do_cpuid(KVM_CPUID_FEATURES_LEAF(hv_base), regs);
 }
 
 #endif /* !_X86_KVM_H_ */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.