From: Chris Lalancette <clalance@redhat.com> Date: Fri, 20 Mar 2009 10:22:05 +0100 Subject: [x86] hypervisor: detection and get tsc_freq Message-id: 49C3603D.50808@redhat.com O-Subject: [RHEL5.4 PATCH 3/14]: x86: Hypervisor detection and get tsc_freq from hypervisor Bugzilla: 463573 RH-Acked-by: Rik van Riel <riel@redhat.com> RH-Acked-by: Brian Maly <bmaly@redhat.com> RH-Acked-by: Justin M. Forbes <jforbes@redhat.com> Impact: Changes timebase calibration on Vmware. v3->v2 : Abstract the hypervisor detection and feature (tsc_freq) request behind a hypervisor.c file v2->v1 : Add a x86_hyper_vendor field to the cpuinfo_x86 structure. This avoids multiple calls to the hypervisor detection function. This patch adds function to detect if we are running under VMware. The current way to check if we are on VMware is following, - check if "hypervisor present bit" is set, if so read the 0x40000000 cpuid leaf and check for "VMwareVMware" signature. - if the above fails, check the DMI vendors name for "VMware" string if we find one we query the VMware hypervisor port to check if we are under VMware. The DMI + "VMware hypervisor port check" is needed for older VMware products which don't implement the hypervisor signature cpuid leaf. Also note that since we are checking for the DMI signature the hypervisor port should never be accessed on native hardware. This patch also adds a hypervisor_get_tsc_freq function, instead of calibrating the frequency which can be error prone in virtualized environment, we ask the hypervisor for it. We get the frequency from the hypervisor by accessing the hypervisor port if we are running on VMware. Other hypervisors too can add code to the generic routine to get frequency o their platform. upstream commit 88b094fb8d4fe43b7025ea8d487059e8813e02cd Fixes BZ 463573 diff --git a/arch/i386/kernel/cpu/Makefile b/arch/i386/kernel/cpu/Makefile index 9a56914..fdf7e00 100644 --- a/arch/i386/kernel/cpu/Makefile +++ b/arch/i386/kernel/cpu/Makefile @@ -13,6 +13,10 @@ obj-y += rise.o obj-y += nexgen.o obj-y += umc.o +ifndef CONFIG_XEN +obj-y += vmware.o hypervisor.o +endif + obj-$(CONFIG_X86_MCE) += mcheck/ obj-$(CONFIG_MTRR) += mtrr/ diff --git a/arch/i386/kernel/cpu/common.c b/arch/i386/kernel/cpu/common.c index f279008..447afef 100644 --- a/arch/i386/kernel/cpu/common.c +++ b/arch/i386/kernel/cpu/common.c @@ -19,6 +19,8 @@ #include <mach_apic.h> #endif +#include <asm/generic-hypervisor.h> + #include "cpu.h" DEFINE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr); @@ -456,6 +458,7 @@ void __cpuinit identify_cpu(struct cpuinfo_x86 *c) printk(" %08lx", c->x86_capability[i]); printk("\n"); + init_hypervisor(c); /* * On SMP, boot_cpu_data holds the common feature set between * all CPUs; so make sure that we indicate which features are diff --git a/arch/i386/kernel/cpu/hypervisor.c b/arch/i386/kernel/cpu/hypervisor.c new file mode 100644 index 0000000..ba55eff --- /dev/null +++ b/arch/i386/kernel/cpu/hypervisor.c @@ -0,0 +1,49 @@ +/* + * Common hypervisor code + * + * Copyright (C) 2008, VMware, Inc. + * Author : Alok N Kataria <akataria@vmware.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or + * NON INFRINGEMENT. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include <linux/init.h> +#include <asm/processor.h> +#include <asm/vmware.h> +#include <asm/generic-hypervisor.h> + +static inline void __cpuinit +detect_hypervisor_vendor(struct cpuinfo_x86 *c) +{ + if (vmware_platform()) { + c->x86_hyper_vendor = X86_HYPER_VENDOR_VMWARE; + } else { + c->x86_hyper_vendor = X86_HYPER_VENDOR_NONE; + } +} + +unsigned long get_hypervisor_tsc_freq(void) +{ + if (boot_cpu_data.x86_hyper_vendor == X86_HYPER_VENDOR_VMWARE) + return vmware_get_tsc_khz(); + return 0; +} + +void __cpuinit init_hypervisor(struct cpuinfo_x86 *c) +{ + detect_hypervisor_vendor(c); +} diff --git a/arch/i386/kernel/cpu/vmware.c b/arch/i386/kernel/cpu/vmware.c new file mode 100644 index 0000000..dbcf0dd --- /dev/null +++ b/arch/i386/kernel/cpu/vmware.c @@ -0,0 +1,90 @@ +/* + * VMware Detection code. + * + * Copyright (C) 2008, VMware, Inc. + * Author : Alok N Kataria <akataria@vmware.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or + * NON INFRINGEMENT. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include <linux/dmi.h> +#include <linux/init.h> +#include <asm/div64.h> +#include <asm/vmware.h> + +#define CPUID_VMWARE_INFO_LEAF 0x40000000 +#define VMWARE_HYPERVISOR_MAGIC 0x564D5868 +#define VMWARE_HYPERVISOR_PORT 0x5658 + +#define VMWARE_PORT_CMD_GETVERSION 10 +#define VMWARE_PORT_CMD_GETHZ 45 + +#define VMWARE_PORT(cmd, eax, ebx, ecx, edx) \ + __asm__("inl (%%dx)" : \ + "=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \ + "0"(VMWARE_HYPERVISOR_MAGIC), \ + "1"(VMWARE_PORT_CMD_##cmd), \ + "2"(VMWARE_HYPERVISOR_PORT), "3"(0) : \ + "memory"); + +static inline int __vmware_platform(void) +{ + uint32_t eax, ebx, ecx, edx; + VMWARE_PORT(GETVERSION, eax, ebx, ecx, edx); + return eax != (uint32_t)-1 && ebx == VMWARE_HYPERVISOR_MAGIC; +} + +static unsigned long __vmware_get_tsc_khz(void) +{ + uint64_t tsc_hz; + uint32_t eax, ebx, ecx, edx; + + VMWARE_PORT(GETHZ, eax, ebx, ecx, edx); + + if (ebx == (uint32_t)-1) + return 0; + tsc_hz = eax | (((uint64_t)ebx) << 32); + do_div(tsc_hz, 1000); + BUG_ON(tsc_hz >> 32); + return tsc_hz; +} + +int vmware_platform(void) +{ + if (cpu_has_hypervisor) { + unsigned int eax, ebx, ecx, edx; + char hyper_vendor_id[13]; + + cpuid(CPUID_VMWARE_INFO_LEAF, &eax, &ebx, &ecx, &edx); + memcpy(hyper_vendor_id + 0, &ebx, 4); + memcpy(hyper_vendor_id + 4, &ecx, 4); + memcpy(hyper_vendor_id + 8, &edx, 4); + hyper_vendor_id[12] = '\0'; + if (!strcmp(hyper_vendor_id, "VMwareVMware")) + return 1; + } else if (dmi_name_in_vendors("VMware") && + __vmware_platform()) + return 1; + + return 0; +} + +unsigned long vmware_get_tsc_khz(void) +{ + BUG_ON(!vmware_platform()); + return __vmware_get_tsc_khz(); +} diff --git a/arch/i386/kernel/setup.c b/arch/i386/kernel/setup.c index 0fec4c9..7d00734 100644 --- a/arch/i386/kernel/setup.c +++ b/arch/i386/kernel/setup.c @@ -63,6 +63,8 @@ #include <setup_arch.h> #include <bios_ebda.h> +#include <asm/generic-hypervisor.h> + /* Forward Declaration. */ void __init find_max_pfn(void); @@ -1595,6 +1597,12 @@ void __init setup_arch(char **cmdline_p) dmi_scan_machine(); + /* + * VMware detection requires dmi to be available, so this + * needs to be done after dmi_scan_machine, for the BP. + */ + init_hypervisor(&boot_cpu_data); + #ifdef CONFIG_X86_GENERICARCH generic_apic_probe(*cmdline_p); #endif diff --git a/arch/i386/kernel/tsc.c b/arch/i386/kernel/tsc.c index 87c757c..9789209 100644 --- a/arch/i386/kernel/tsc.c +++ b/arch/i386/kernel/tsc.c @@ -15,6 +15,7 @@ #include <asm/tsc.h> #include <asm/delay.h> #include <asm/io.h> +#include <asm/generic-hypervisor.h> #include "mach_timer.h" @@ -135,6 +136,14 @@ static unsigned long calculate_cpu_khz(void) int i; unsigned long flags; +#ifndef CONFIG_XEN + tsc_khz = get_hypervisor_tsc_freq(); + if (tsc_khz) { + printk(KERN_INFO "TSC: Frequency read from the hypervisor\n"); + return tsc_khz; + } +#endif + local_irq_save(flags); /* run 3 times to ensure the cache is warm */ diff --git a/arch/x86_64/kernel/Makefile b/arch/x86_64/kernel/Makefile index 5f427b4..9782a3f 100644 --- a/arch/x86_64/kernel/Makefile +++ b/arch/x86_64/kernel/Makefile @@ -48,6 +48,10 @@ obj-$(CONFIG_AMD_IOMMU) += amd_iommu_init.o amd_iommu.o obj-y += topology.o obj-y += intel_cacheinfo.o obj-y += addon_cpuid_features.o +ifndef CONFIG_XEN +obj-y += vmware.o +obj-y += hypervisor.o +endif CFLAGS_vsyscall.o := $(PROFILING) -g0 @@ -58,6 +62,8 @@ topology-y += ../../i386/kernel/topology.o microcode-$(subst m,y,$(CONFIG_MICROCODE)) += ../../i386/kernel/microcode.o intel_cacheinfo-y += ../../i386/kernel/cpu/intel_cacheinfo.o addon_cpuid_features-y += ../../i386/kernel/cpu/addon_cpuid_features.o +vmware-y += ../../i386/kernel/cpu/vmware.o +hypervisor-y += ../../i386/kernel/cpu/hypervisor.o quirks-y += ../../i386/kernel/quirks.o i8237-y += ../../i386/kernel/i8237.o msr-$(subst m,y,$(CONFIG_X86_MSR)) += ../../i386/kernel/msr.o diff --git a/arch/x86_64/kernel/setup.c b/arch/x86_64/kernel/setup.c index f1c873d..8509983 100644 --- a/arch/x86_64/kernel/setup.c +++ b/arch/x86_64/kernel/setup.c @@ -66,6 +66,7 @@ #include <asm/numa.h> #include <asm/sections.h> #include <asm/dmi.h> +#include <asm/generic-hypervisor.h> /* * Machine setup.. @@ -567,6 +568,12 @@ void __init setup_arch(char **cmdline_p) dmi_scan_machine(); + /* + * VMware detection requires dmi to be available, so this + * needs to be done after dmi_scan_machine, for the BP. + */ + init_hypervisor(&boot_cpu_data); + #ifdef CONFIG_ACPI /* * Initialize the ACPI boot-time table parser (gets the RSDP and SDT). diff --git a/arch/x86_64/kernel/time.c b/arch/x86_64/kernel/time.c index 6efcc7f..fb9fcd9 100644 --- a/arch/x86_64/kernel/time.c +++ b/arch/x86_64/kernel/time.c @@ -47,6 +47,7 @@ #include <asm/hpet.h> #include <asm/sections.h> #include <asm/nmi.h> +#include <asm/generic-hypervisor.h> #include <linux/cpufreq.h> #include <linux/hpet.h> #ifdef CONFIG_X86_LOCAL_APIC @@ -987,6 +988,8 @@ time_cpu_notifier(struct notifier_block *nb, unsigned long action, void *hcpu) void __init time_init(void) { + unsigned int hypervisor_khz; + if (nohpet) vxtime.hpet_address = 0; @@ -1025,6 +1028,13 @@ void __init time_init(void) boot_cpu_data.x86 == 16) cpu_khz = tsc_calibrate_cpu_khz(); + /* Should we get tsc_khz from the hypervisor? */ + hypervisor_khz = get_hypervisor_tsc_freq(); + if (hypervisor_khz) { + tsc_khz = hypervisor_khz; + cpu_khz = tsc_khz; + } + vxtime.mode = VXTIME_TSC; vxtime.quot = (NSEC_PER_SEC << NS_SCALE) / vxtime_hz; vxtime.tsc_quot = (NSEC_PER_MSEC << NS_SCALE) / cpu_khz; diff --git a/include/asm-i386/generic-hypervisor.h b/include/asm-i386/generic-hypervisor.h new file mode 100644 index 0000000..369f5c5 --- /dev/null +++ b/include/asm-i386/generic-hypervisor.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2008, VMware, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or + * NON INFRINGEMENT. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ +#ifndef ASM_X86__HYPERVISOR_H +#define ASM_X86__HYPERVISOR_H + +extern unsigned long get_hypervisor_tsc_freq(void); +extern void init_hypervisor(struct cpuinfo_x86 *c); + +#endif diff --git a/include/asm-i386/mach-xen/asm/processor.h b/include/asm-i386/mach-xen/asm/processor.h index d8a4b7b..25fe40f 100644 --- a/include/asm-i386/mach-xen/asm/processor.h +++ b/include/asm-i386/mach-xen/asm/processor.h @@ -65,7 +65,11 @@ struct cpuinfo_x86 { char fdiv_bug; char f00f_bug; char coma_bug; +#ifdef __GENKSYMS__ char pad0; +#else + __u8 x86_hyper_vendor; +#endif int x86_power; unsigned long loops_per_jiffy; #ifdef CONFIG_SMP @@ -92,6 +96,9 @@ struct cpuinfo_x86 { #define X86_VENDOR_NUM 9 #define X86_VENDOR_UNKNOWN 0xff +#define X86_HYPER_VENDOR_NONE 0 +#define X86_HYPER_VENDOR_VMWARE 1 + /* * capabilities of CPUs */ diff --git a/include/asm-i386/processor.h b/include/asm-i386/processor.h index c108225..39e1e7c 100644 --- a/include/asm-i386/processor.h +++ b/include/asm-i386/processor.h @@ -64,7 +64,11 @@ struct cpuinfo_x86 { char fdiv_bug; char f00f_bug; char coma_bug; +#ifdef __GENKSYMS__ char pad0; +#else + __u8 x86_hyper_vendor; +#endif int x86_power; unsigned long loops_per_jiffy; #ifdef CONFIG_SMP @@ -91,6 +95,9 @@ struct cpuinfo_x86 { #define X86_VENDOR_NUM 9 #define X86_VENDOR_UNKNOWN 0xff +#define X86_HYPER_VENDOR_NONE 0 +#define X86_HYPER_VENDOR_VMWARE 1 + /* * capabilities of CPUs */ diff --git a/include/asm-i386/vmware.h b/include/asm-i386/vmware.h new file mode 100644 index 0000000..02dfea5 --- /dev/null +++ b/include/asm-i386/vmware.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2008, VMware, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or + * NON INFRINGEMENT. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ +#ifndef ASM_X86__VMWARE_H +#define ASM_X86__VMWARE_H + +extern unsigned long vmware_get_tsc_khz(void); +extern int vmware_platform(void); + +#endif diff --git a/include/asm-x86_64/generic-hypervisor.h b/include/asm-x86_64/generic-hypervisor.h new file mode 100644 index 0000000..369f5c5 --- /dev/null +++ b/include/asm-x86_64/generic-hypervisor.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2008, VMware, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or + * NON INFRINGEMENT. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ +#ifndef ASM_X86__HYPERVISOR_H +#define ASM_X86__HYPERVISOR_H + +extern unsigned long get_hypervisor_tsc_freq(void); +extern void init_hypervisor(struct cpuinfo_x86 *c); + +#endif diff --git a/include/asm-x86_64/mach-xen/asm/processor.h b/include/asm-x86_64/mach-xen/asm/processor.h index 2ca8c14..f82dfc6 100644 --- a/include/asm-x86_64/mach-xen/asm/processor.h +++ b/include/asm-x86_64/mach-xen/asm/processor.h @@ -62,6 +62,9 @@ struct cpuinfo_x86 { int x86_tlbsize; /* number of 4K pages in DTLB/ITLB combined(in pages)*/ __u8 x86_virt_bits, x86_phys_bits; __u8 x86_max_cores; /* cpuid returned max cores value */ +#ifndef __GENKSYMS__ + __u8 x86_hyper_vendor; +#endif __u32 x86_power; __u32 extended_cpuid_level; /* Max extended CPUID function supported */ unsigned long loops_per_jiffy; @@ -87,6 +90,9 @@ struct cpuinfo_x86 { #define X86_VENDOR_NUM 8 #define X86_VENDOR_UNKNOWN 0xff +#define X86_HYPER_VENDOR_NONE 0 +#define X86_HYPER_VENDOR_VMWARE 1 + #ifdef CONFIG_SMP extern struct cpuinfo_x86 cpu_data[]; #define current_cpu_data cpu_data[smp_processor_id()] diff --git a/include/asm-x86_64/processor.h b/include/asm-x86_64/processor.h index 2dd19a7..8923857 100644 --- a/include/asm-x86_64/processor.h +++ b/include/asm-x86_64/processor.h @@ -62,6 +62,9 @@ struct cpuinfo_x86 { int x86_tlbsize; /* number of 4K pages in DTLB/ITLB combined(in pages)*/ __u8 x86_virt_bits, x86_phys_bits; __u8 x86_max_cores; /* cpuid returned max cores value */ +#ifndef __GENKSYMS__ + __u8 x86_hyper_vendor; +#endif __u32 x86_power; __u32 extended_cpuid_level; /* Max extended CPUID function supported */ unsigned long loops_per_jiffy; @@ -87,6 +90,9 @@ struct cpuinfo_x86 { #define X86_VENDOR_NUM 8 #define X86_VENDOR_UNKNOWN 0xff +#define X86_HYPER_VENDOR_NONE 0 +#define X86_HYPER_VENDOR_VMWARE 1 + #ifdef CONFIG_SMP extern struct cpuinfo_x86 cpu_data[]; #define current_cpu_data cpu_data[smp_processor_id()] diff --git a/include/asm-x86_64/vmware.h b/include/asm-x86_64/vmware.h new file mode 100644 index 0000000..02dfea5 --- /dev/null +++ b/include/asm-x86_64/vmware.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2008, VMware, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or + * NON INFRINGEMENT. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ +#ifndef ASM_X86__VMWARE_H +#define ASM_X86__VMWARE_H + +extern unsigned long vmware_get_tsc_khz(void); +extern int vmware_platform(void); + +#endif