Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86: Correctly detect hypervisor

We try to handle the hypervisor compatibility mode by detecting hypervisor
through a specific order. This is not robust, since hypervisors may implement
each others features.

This patch tries to handle this situation by always choosing the last one in the
CPUID leaves. This is done by letting .detect() return a priority instead of
true/false and just re-using the CPUID leaf where the signature were found as
the priority (or 1 if it was found by DMI). Then we can just pick hypervisor who
has the highest priority. Other sophisticated detection method could also be
implemented on top.

Suggested by H. Peter Anvin and Paolo Bonzini.

Acked-by: K. Y. Srinivasan <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Jeremy Fitzhardinge <jeremy@goop.org>
Cc: Doug Covelli <dcovelli@vmware.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Dan Hecht <dhecht@vmware.com>
Cc: Paul Gortmaker <paul.gortmaker@windriver.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
Link: http://lkml.kernel.org/r/1374742475-2485-4-git-send-email-jasowang@redhat.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>

authored by

Jason Wang and committed by
H. Peter Anvin
9df56f19 1085ba7f

+25 -28
+1 -1
arch/x86/include/asm/hypervisor.h
··· 33 33 const char *name; 34 34 35 35 /* Detection routine */ 36 - bool (*detect)(void); 36 + uint32_t (*detect)(void); 37 37 38 38 /* Adjust CPU feature bits (run once per CPU) */ 39 39 void (*set_cpu_features)(struct cpuinfo_x86 *);
+7 -8
arch/x86/kernel/cpu/hypervisor.c
··· 25 25 #include <asm/processor.h> 26 26 #include <asm/hypervisor.h> 27 27 28 - /* 29 - * Hypervisor detect order. This is specified explicitly here because 30 - * some hypervisors might implement compatibility modes for other 31 - * hypervisors and therefore need to be detected in specific sequence. 32 - */ 33 28 static const __initconst struct hypervisor_x86 * const hypervisors[] = 34 29 { 35 30 #ifdef CONFIG_XEN_PVHVM ··· 44 49 detect_hypervisor_vendor(void) 45 50 { 46 51 const struct hypervisor_x86 *h, * const *p; 52 + uint32_t pri, max_pri = 0; 47 53 48 54 for (p = hypervisors; p < hypervisors + ARRAY_SIZE(hypervisors); p++) { 49 55 h = *p; 50 - if (h->detect()) { 56 + pri = h->detect(); 57 + if (pri != 0 && pri > max_pri) { 58 + max_pri = pri; 51 59 x86_hyper = h; 52 - printk(KERN_INFO "Hypervisor detected: %s\n", h->name); 53 - break; 54 60 } 55 61 } 62 + 63 + if (max_pri) 64 + printk(KERN_INFO "Hypervisor detected: %s\n", x86_hyper->name); 56 65 } 57 66 58 67 void init_hypervisor(struct cpuinfo_x86 *c)
+8 -5
arch/x86/kernel/cpu/mshyperv.c
··· 27 27 struct ms_hyperv_info ms_hyperv; 28 28 EXPORT_SYMBOL_GPL(ms_hyperv); 29 29 30 - static bool __init ms_hyperv_platform(void) 30 + static uint32_t __init ms_hyperv_platform(void) 31 31 { 32 32 u32 eax; 33 33 u32 hyp_signature[3]; 34 34 35 35 if (!boot_cpu_has(X86_FEATURE_HYPERVISOR)) 36 - return false; 36 + return 0; 37 37 38 38 cpuid(HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS, 39 39 &eax, &hyp_signature[0], &hyp_signature[1], &hyp_signature[2]); 40 40 41 - return eax >= HYPERV_CPUID_MIN && 42 - eax <= HYPERV_CPUID_MAX && 43 - !memcmp("Microsoft Hv", hyp_signature, 12); 41 + if (eax >= HYPERV_CPUID_MIN && 42 + eax <= HYPERV_CPUID_MAX && 43 + !memcmp("Microsoft Hv", hyp_signature, 12)) 44 + return HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS; 45 + 46 + return 0; 44 47 } 45 48 46 49 static cycle_t read_hv_clock(struct clocksource *arg)
+4 -4
arch/x86/kernel/cpu/vmware.c
··· 93 93 * serial key should be enough, as this will always have a VMware 94 94 * specific string when running under VMware hypervisor. 95 95 */ 96 - static bool __init vmware_platform(void) 96 + static uint32_t __init vmware_platform(void) 97 97 { 98 98 if (cpu_has_hypervisor) { 99 99 unsigned int eax; ··· 102 102 cpuid(CPUID_VMWARE_INFO_LEAF, &eax, &hyper_vendor_id[0], 103 103 &hyper_vendor_id[1], &hyper_vendor_id[2]); 104 104 if (!memcmp(hyper_vendor_id, "VMwareVMware", 12)) 105 - return true; 105 + return CPUID_VMWARE_INFO_LEAF; 106 106 } else if (dmi_available && dmi_name_in_serial("VMware") && 107 107 __vmware_platform()) 108 - return true; 108 + return 1; 109 109 110 - return false; 110 + return 0; 111 111 } 112 112 113 113 /*
+2 -4
arch/x86/kernel/kvm.c
··· 498 498 #endif 499 499 } 500 500 501 - static bool __init kvm_detect(void) 501 + static uint32_t __init kvm_detect(void) 502 502 { 503 - if (!kvm_para_available()) 504 - return false; 505 - return true; 503 + return kvm_cpuid_base(); 506 504 } 507 505 508 506 const struct hypervisor_x86 x86_hyper_kvm __refconst = {
+3 -6
arch/x86/xen/enlighten.c
··· 1720 1720 xen_hvm_init_mmu_ops(); 1721 1721 } 1722 1722 1723 - static bool __init xen_hvm_platform(void) 1723 + static uint32_t __init xen_hvm_platform(void) 1724 1724 { 1725 1725 if (xen_pv_domain()) 1726 - return false; 1726 + return 0; 1727 1727 1728 - if (!xen_cpuid_base()) 1729 - return false; 1730 - 1731 - return true; 1728 + return xen_cpuid_base(); 1732 1729 } 1733 1730 1734 1731 bool xen_hvm_need_lapic(void)