X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=virt-what-cpuid-helper.c;h=0cd4a6f2b168dcdb6d3025828a8d98170f5b68d8;hb=01ea13d9813df93e294b0d9776d4cefd54b1609b;hp=3024c375cb7b71142ded2a6a5836574d5b21f2f5;hpb=fcc68d36da2f3f27beddcc70c99c7a485b08afb4;p=virt-what.git diff --git a/virt-what-cpuid-helper.c b/virt-what-cpuid-helper.c index 3024c37..0cd4a6f 100644 --- a/virt-what-cpuid-helper.c +++ b/virt-what-cpuid-helper.c @@ -1,5 +1,5 @@ /* virt-what-cpuid-helper: Are we running inside KVM or Xen HVM? - * Copyright (C) 2008 Red Hat Inc. + * Copyright (C) 2008-2019 Red Hat 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 @@ -21,43 +21,92 @@ */ #include +#include +#include #include #if defined(__i386__) || defined(__x86_64__) -void -cpu_sig (char *sig) + +/* Known x86 hypervisor signatures. Note that if you add a new test + * to virt-what.in you may need to update this list. The signature is + * always 12 bytes except in the case of KVM. + */ +static int +known_signature (char *sig) +{ + return + strcmp (sig, "bhyve bhyve ") == 0 || + strcmp (sig, "KVMKVMKVM") == 0 || + strcmp (sig, "LKVMLKVMLKVM") == 0 || + strcmp (sig, "Microsoft Hv") == 0 || + strcmp (sig, "OpenBSDVMM58") == 0 || + strcmp (sig, "TCGTCGTCGTCG") == 0 || + strcmp (sig, "VMwareVMware") == 0 || + strcmp (sig, "XenVMMXenVMM") == 0 || + 0; +} + +static uint32_t +cpuid (uint32_t eax, char *sig) { - unsigned int eax = 0x40000000; - unsigned int *sig32 = (unsigned int *)sig; + uint32_t *sig32 = (uint32_t *) sig; + asm volatile ( - "xor %%ebx, %%ebx; cpuid" - : "=a" (eax), "=b" (sig32[0]), "=c" (sig32[1]), "=d" (sig32[2]) + "xchgl %%ebx,%1; xor %%ebx,%%ebx; cpuid; xchgl %%ebx,%1" + : "=a" (eax), "+r" (sig32[0]), "=c" (sig32[1]), "=d" (sig32[2]) : "0" (eax)); sig[12] = 0; + + return eax; +} + +static void +cpu_sig (void) +{ + char sig[13]; + const uint32_t base = 0x40000000; + uint32_t leaf; + + /* Most hypervisors only have information in leaf 0x40000000. + * + * Some hypervisors have "Viridian [HyperV] extensions", and those + * must appear in slot 0x40000000, but they will also have the true + * hypervisor in a higher slot. + * + * CPUID is supposed to return the maximum leaf offset in %eax, but + * this is not reliable. Instead we check the returned signatures + * against a known list (the others will be empty or garbage) and + * only print the ones we know about. This is OK because if we add + * a new test in virt-what we can update the list. + * + * By searching backwards we only print the highest entry, thus + * ignoring Viridian for Xen (and Nutanix). If we ever encounter a + * hypervisor that has more than 2 entries we may need to revisit + * this. + */ + for (leaf = base + 0xff00; leaf >= base; leaf -= 0x100) { + memset (sig, 0, sizeof sig); + cpuid (leaf, sig); + if (known_signature (sig)) { + puts (sig); + break; + } + } } -#else -void -cpu_sig (char *sig) + +#else /* !i386, !x86_64 */ + +static void +cpu_sig (void) { /* nothing for other architectures */ } + #endif int main() { - char sig[13]; - - memset (sig, 0, sizeof sig); - - cpu_sig (sig); - - puts (sig); - /* Possible values: - * KVMKVMKVM KVM guest - * XenVMMXenVMM Xen HVM guest - * VMwareVMware VMware guest - */ - + cpu_sig (); return 0; }