1 /* virt-what-cpuid-helper: Are we running inside KVM or Xen HVM?
2 * Copyright (C) 2008-2019 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* This program was suggested by Gleb Natapov and written by Paolo
20 * Bonzini, with a few modifications by Richard W.M. Jones.
28 #if defined(__i386__) || defined(__x86_64__)
30 /* Known x86 hypervisor signatures. Note that if you add a new test
31 * to virt-what.in you may need to update this list. Note the
32 * signature is always 12 bytes long, plus we add \0 to the end to
36 known_signature (const char *sig)
39 strcmp (sig, "bhyve bhyve ") == 0 ||
40 memcmp (sig, "KVMKVMKVM\0\0\0", 12) == 0 ||
41 strcmp (sig, "LKVMLKVMLKVM") == 0 ||
42 strcmp (sig, "Microsoft Hv") == 0 ||
43 strcmp (sig, "OpenBSDVMM58") == 0 ||
44 strcmp (sig, "TCGTCGTCGTCG") == 0 ||
45 strcmp (sig, "VMwareVMware") == 0 ||
46 strcmp (sig, "XenVMMXenVMM") == 0 ||
51 cpuid (uint32_t eax, char *sig)
53 uint32_t *sig32 = (uint32_t *) sig;
56 "xchgl %%ebx,%1; xor %%ebx,%%ebx; cpuid; xchgl %%ebx,%1"
57 : "=a" (eax), "+r" (sig32[0]), "=c" (sig32[1]), "=d" (sig32[2])
68 const uint32_t base = 0x40000000;
71 /* Most hypervisors only have information in leaf 0x40000000.
73 * Some hypervisors have "Viridian [HyperV] extensions", and those
74 * must appear in slot 0x40000000, but they will also have the true
75 * hypervisor in a higher slot.
77 * CPUID is supposed to return the maximum leaf offset in %eax, but
78 * this is not reliable. Instead we check the returned signatures
79 * against a known list (the others will be empty or garbage) and
80 * only print the ones we know about. This is OK because if we add
81 * a new test in virt-what we can update the list.
83 * By searching backwards we only print the highest entry, thus
84 * ignoring Viridian for Xen (and Nutanix). If we ever encounter a
85 * hypervisor that has more than 2 entries we may need to revisit
88 for (leaf = base + 0xff00; leaf >= base; leaf -= 0x100) {
89 memset (sig, 0, sizeof sig);
91 if (known_signature (sig)) {
98 #else /* !i386, !x86_64 */
103 /* nothing for other architectures */