Date: Sun, 21 Dec 2014 23:26:43 +0000 (UTC) From: Bryan Venteicher <bryanv@FreeBSD.org> To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r276043 - in projects/paravirt/sys: conf x86/include x86/x86 Message-ID: <201412212326.sBLNQh9P036545@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: bryanv Date: Sun Dec 21 23:26:42 2014 New Revision: 276043 URL: https://svnweb.freebsd.org/changeset/base/276043 Log: Add detection of VMware and KVM to the hypervisor interface While here, relocate the code for the VMware TSC frequency into the VMware implementation. Eventually we should probably grow a hypervisor interface for this. Added: projects/paravirt/sys/x86/include/kvm.h (contents, props changed) projects/paravirt/sys/x86/x86/kvm.c (contents, props changed) projects/paravirt/sys/x86/x86/vmware.c (contents, props changed) Modified: projects/paravirt/sys/conf/files.amd64 projects/paravirt/sys/conf/files.i386 projects/paravirt/sys/x86/include/vmware.h projects/paravirt/sys/x86/x86/hypervisor.c projects/paravirt/sys/x86/x86/tsc.c Modified: projects/paravirt/sys/conf/files.amd64 ============================================================================== --- projects/paravirt/sys/conf/files.amd64 Sun Dec 21 23:23:02 2014 (r276042) +++ projects/paravirt/sys/conf/files.amd64 Sun Dec 21 23:26:42 2014 (r276043) @@ -563,6 +563,7 @@ x86/x86/hypervisor.c standard x86/x86/identcpu.c standard x86/x86/intr_machdep.c standard x86/x86/io_apic.c standard +x86/x86/kvm.c standard x86/x86/legacy.c standard x86/x86/local_apic.c standard x86/x86/mca.c standard @@ -573,6 +574,7 @@ x86/x86/nexus.c standard x86/x86/pvclock.c standard x86/x86/tsc.c standard x86/x86/delay.c standard +x86/x86/vmware.c standard x86/xen/hvm.c optional xenhvm x86/xen/xen_intr.c optional xen | xenhvm x86/xen/pv.c optional xenhvm Modified: projects/paravirt/sys/conf/files.i386 ============================================================================== --- projects/paravirt/sys/conf/files.i386 Sun Dec 21 23:23:02 2014 (r276042) +++ projects/paravirt/sys/conf/files.i386 Sun Dec 21 23:26:42 2014 (r276043) @@ -581,6 +581,7 @@ x86/x86/hypervisor.c standard x86/x86/identcpu.c standard x86/x86/intr_machdep.c standard x86/x86/io_apic.c optional apic +x86/x86/kvm.c standard x86/x86/legacy.c optional native x86/x86/local_apic.c optional apic x86/x86/mca.c standard @@ -591,6 +592,7 @@ x86/x86/nexus.c standard x86/x86/tsc.c standard x86/x86/pvclock.c standard x86/x86/delay.c standard +x86/x86/vmware.c standard x86/xen/hvm.c optional xenhvm x86/xen/xen_intr.c optional xen | xenhvm x86/xen/xen_apic.c optional xenhvm Added: projects/paravirt/sys/x86/include/kvm.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ projects/paravirt/sys/x86/include/kvm.h Sun Dec 21 23:26:42 2014 (r276043) @@ -0,0 +1,37 @@ +/*- + * Copyright (c) 2014 Bryan Venteicher <bryanv@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _X86_KVM_H_ +#define _X86_KVM_H_ + +#define KVM_CPUID_FEATURES_LEAF 0x40000001 + +int kvm_paravirt_supported(void); +uint32_t kvm_get_features(void); + +#endif /* !_X86_KVM_H_ */ Modified: projects/paravirt/sys/x86/include/vmware.h ============================================================================== --- projects/paravirt/sys/x86/include/vmware.h Sun Dec 21 23:23:02 2014 (r276042) +++ projects/paravirt/sys/x86/include/vmware.h Sun Dec 21 23:26:42 2014 (r276043) @@ -44,4 +44,6 @@ vmware_hvcall(u_int cmd, u_int *p) : "memory"); } +uint64_t vmware_tsc_freq(void); + #endif /* !_X86_VMWARE_H_ */ Modified: projects/paravirt/sys/x86/x86/hypervisor.c ============================================================================== --- projects/paravirt/sys/x86/x86/hypervisor.c Sun Dec 21 23:23:02 2014 (r276042) +++ projects/paravirt/sys/x86/x86/hypervisor.c Sun Dec 21 23:26:42 2014 (r276043) @@ -44,7 +44,12 @@ char hv_vendor[16]; SYSCTL_STRING(_hw, OID_AUTO, hv_vendor, CTLFLAG_RD, hv_vendor, 0, "Hypervisor vendor"); +extern const struct hypervisor_info kvm_hypervisor_info; +extern const struct hypervisor_info vmware_hypervisor_info; + static const struct hypervisor_info *hypervisor_infos[] = { + &kvm_hypervisor_info, + &vmware_hypervisor_info, }; static const struct hypervisor_info *hv_info; Added: projects/paravirt/sys/x86/x86/kvm.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ projects/paravirt/sys/x86/x86/kvm.c Sun Dec 21 23:26:42 2014 (r276043) @@ -0,0 +1,82 @@ +/*- + * Copyright (c) 2014 Bryan Venteicher <bryanv@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/systm.h> + +#include <x86/hypervisor.h> +#include <x86/kvm.h> + +static int kvm_identify(void); +static uint32_t kvm_cpuid_identify(void); + +const struct hypervisor_info kvm_hypervisor_info = { + .hvi_name = "KVM", + .hvi_type = VM_GUEST_KVM, + .hvi_identify = kvm_identify, +}; + +static uint32_t kvm_cpuid_base = -1; +static uint32_t kvm_cpuid_high = -1; + +static uint32_t +kvm_cpuid_identify(void) +{ + + if (kvm_cpuid_base == -1) { + hypervisor_cpuid_base("KVMKVMKVM\0\0", 0, &kvm_cpuid_base, + &kvm_cpuid_high); + } + + return (kvm_cpuid_base); +} + +static int +kvm_identify(void) +{ + + return (kvm_cpuid_identify() != 0); +} + +int +kvm_paravirt_supported(void) +{ + + return (kvm_cpuid_base != -1); +} + +uint32_t +kvm_get_features(void) +{ + u_int regs[4]; + + do_cpuid(kvm_cpuid_identify() | KVM_CPUID_FEATURES_LEAF, regs); + + return (regs[0]); +} Modified: projects/paravirt/sys/x86/x86/tsc.c ============================================================================== --- projects/paravirt/sys/x86/x86/tsc.c Sun Dec 21 23:23:02 2014 (r276042) +++ projects/paravirt/sys/x86/x86/tsc.c Sun Dec 21 23:26:42 2014 (r276043) @@ -104,22 +104,6 @@ static struct timecounter tsc_timecounte }; static void -tsc_freq_vmware(void) -{ - u_int regs[4]; - - if (hv_high >= 0x40000010) { - do_cpuid(0x40000010, regs); - tsc_freq = regs[0] * 1000; - } else { - vmware_hvcall(VMW_HVCMD_GETHZ, regs); - if (regs[1] != UINT_MAX) - tsc_freq = regs[0] | ((uint64_t)regs[1] << 32); - } - tsc_is_invariant = 1; -} - -static void tsc_freq_intel(void) { char brand[48]; @@ -201,7 +185,8 @@ probe_tsc_freq(void) } if (vm_guest == VM_GUEST_VMWARE) { - tsc_freq_vmware(); + tsc_freq = vmware_tsc_freq(); + tsc_is_invariant = 1; return; } Added: projects/paravirt/sys/x86/x86/vmware.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ projects/paravirt/sys/x86/x86/vmware.c Sun Dec 21 23:26:42 2014 (r276043) @@ -0,0 +1,90 @@ +/*- + * Copyright (c) 2014 Bryan Venteicher <bryanv@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/limits.h> + +#include <x86/hypervisor.h> +#include <x86/vmware.h> + +static int vmware_identify(void); +static uint32_t vmware_cpuid_identify(void); + +const struct hypervisor_info vmware_hypervisor_info = { + .hvi_name = "VMware", + .hvi_type = VM_GUEST_VMWARE, + .hvi_identify = vmware_identify, +}; + +static uint32_t vmware_cpuid_base = -1; +static uint32_t vmware_cpuid_high = -1; + +static uint32_t +vmware_cpuid_identify(void) +{ + + if (vmware_cpuid_base == -1) { + hypervisor_cpuid_base("VMwareVMware", 0, &vmware_cpuid_base, + &vmware_cpuid_high); + } + + return (vmware_cpuid_base); +} + +/* + * KB1009458: Mechanisms to determine if software is running in a VMware + * virtual machine: http://kb.vmware.com/kb/1009458 + */ +static int +vmware_identify(void) +{ + + return (vmware_cpuid_identify() != 0); +} + +uint64_t +vmware_tsc_freq(void) +{ + uint64_t freq; + u_int regs[4]; + + if (vmware_cpuid_high >= 0x40000010) { + do_cpuid(0x40000010, regs); + freq = regs[0] * 1000; + } else { + vmware_hvcall(VMW_HVCMD_GETHZ, regs); + if (regs[1] != UINT_MAX) + freq = regs[0] | ((uint64_t)regs[1] << 32); + else + freq = 0; + } + + return (freq); +}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201412212326.sBLNQh9P036545>