Index: sys/conf/files.amd64 =================================================================== --- sys/conf/files.amd64 +++ sys/conf/files.amd64 @@ -559,6 +559,7 @@ x86/x86/busdma_machdep.c standard x86/x86/dump_machdep.c standard x86/x86/fdt_machdep.c optional fdt +x86/x86/hypervisor.c standard x86/x86/identcpu.c standard x86/x86/intr_machdep.c standard x86/x86/io_apic.c standard Index: sys/conf/files.i386 =================================================================== --- sys/conf/files.i386 +++ sys/conf/files.i386 @@ -577,6 +577,7 @@ x86/x86/busdma_machdep.c standard x86/x86/dump_machdep.c standard x86/x86/fdt_machdep.c optional fdt +x86/x86/hypervisor.c standard x86/x86/identcpu.c standard x86/x86/intr_machdep.c standard x86/x86/io_apic.c optional apic Index: sys/x86/include/hypervisor.h =================================================================== --- /dev/null +++ sys/x86/include/hypervisor.h @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2014 Bryan Venteicher + * 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_HYPERVISOR_H_ +#define _X86_HYPERVISOR_H_ + +#include +#include + +typedef void hypervisor_init_func_t(void); + +/* + * The guest hypervisor support may provide paravirtualized or have special + * requirements for various operations. The callback functions are provided + * when a hypervisor is detected and registered. + */ +struct hypervisor_ops { +}; + +void hypervisor_sysinit(void *func); +void hypervisor_register(const char *vendor, enum VM_GUEST guest, + struct hypervisor_ops *ops); +int hypervisor_cpuid_base(const char *signature, int leaves, + uint32_t *base, uint32_t *high); +void hypervisor_print_info(void); + +#define HYPERVISOR_SYSINIT(name, func) \ + SYSINIT(name ## _hypervisor_sysinit, SI_SUB_HYPERVISOR, \ + SI_ORDER_FIRST, hypervisor_sysinit, func) + +#endif /* !_X86_HYPERVISOR_H_ */ Index: sys/x86/x86/hypervisor.c =================================================================== --- /dev/null +++ sys/x86/x86/hypervisor.c @@ -0,0 +1,99 @@ +/*- + * Copyright (c) 2014 Bryan Venteicher + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +#include +#include +#include +#include + +#include + +char hv_vendor[16]; +SYSCTL_STRING(_hw, OID_AUTO, hv_vendor, CTLFLAG_RD, hv_vendor, 0, + "Hypervisor vendor"); + +void +hypervisor_sysinit(void *func) +{ + hypervisor_init_func_t *init; + + init = func; + + /* + * Call the init function if we have not already identified the + * hypervisor yet. We assume the detectable hypervisors will + * announce its presence via the CPUID bit. + */ + if (vm_guest == VM_GUEST_VM && cpu_feature2 & CPUID2_HV) + (*init)(); +} + +void +hypervisor_register(const char *vendor, enum VM_GUEST guest, + struct hypervisor_ops *ops) +{ + + strlcpy(hv_vendor, vendor, sizeof(hv_vendor)); + vm_guest = guest; +} + +/* + * [RFC] CPUID usage for interaction between Hypervisors and Linux. + * http://lkml.org/lkml/2008/10/1/246 + */ +int +hypervisor_cpuid_base(const char *signature, int leaves, uint32_t *base, + uint32_t *high) +{ + uint32_t leaf, regs[4]; + + for (leaf = 0x40000000; leaf < 0x40010000; leaf += 0x100) { + do_cpuid(leaf, regs); + if (!memcmp(signature, ®s[1], 12) && + (leaves == 0 || (regs[0] - leaf >= leaves))) { + *base = leaf; + *high = regs[0]; + return (0); + } + } + + return (1); +} + +void +hypervisor_print_info(void) +{ + + if (*hv_vendor) + printf("Hypervisor: Origin = \"%s\"\n", hv_vendor); +} Index: sys/x86/x86/identcpu.c =================================================================== --- sys/x86/x86/identcpu.c +++ sys/x86/x86/identcpu.c @@ -64,6 +64,7 @@ #include #include +#include #include #ifdef __i386__ @@ -78,7 +79,6 @@ static void print_AMD_info(void); static void print_INTEL_info(void); static void print_INTEL_TLB(u_int data); -static void print_hypervisor_info(void); static void print_svm_info(void); static void print_via_padlock_info(void); static void print_vmx_info(void); @@ -123,11 +123,6 @@ SYSCTL_INT(_hw, OID_AUTO, clockrate, CTLFLAG_RD, &hw_clockrate, 0, "CPU instruction clock rate"); -u_int hv_high; -char hv_vendor[16]; -SYSCTL_STRING(_hw, OID_AUTO, hv_vendor, CTLFLAG_RD, hv_vendor, 0, - "Hypervisor vendor"); - static eventhandler_tag tsc_post_tag; static char cpu_brand[48]; @@ -985,7 +980,7 @@ #endif } - print_hypervisor_info(); + hypervisor_print_info(); } void @@ -1218,25 +1213,12 @@ int i; /* - * [RFC] CPUID usage for interaction between Hypervisors and Linux. - * http://lkml.org/lkml/2008/10/1/246 - * - * KB1009458: Mechanisms to determine if software is running in - * a VMware virtual machine - * http://kb.vmware.com/kb/1009458 + * Modern hypervisors set the HV present feature bit and are then + * identifiable through a special CPUID leaf. Hypervisors we know + * about are later detected via the SI_SUB_HYPERVISOR SYSINIT(). */ if (cpu_feature2 & CPUID2_HV) { vm_guest = VM_GUEST_VM; - do_cpuid(0x40000000, regs); - if (regs[0] >= 0x40000000) { - hv_high = regs[0]; - ((u_int *)&hv_vendor)[0] = regs[1]; - ((u_int *)&hv_vendor)[1] = regs[2]; - ((u_int *)&hv_vendor)[2] = regs[3]; - hv_vendor[12] = '\0'; - if (strcmp(hv_vendor, "VMwareVMware") == 0) - vm_guest = VM_GUEST_VMWARE; - } return; } @@ -2150,11 +2132,3 @@ ); } } - -static void -print_hypervisor_info(void) -{ - - if (*hv_vendor) - printf("Hypervisor: Origin = \"%s\"\n", hv_vendor); -}