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 ||
50 /* Copied from the Linux kernel definition in
51 * arch/x86/include/asm/processor.h
54 cpuid (uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
57 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
58 : "0" (*eax), "2" (*ecx)
63 cpuid_leaf (uint32_t eax, char *sig)
65 uint32_t *sig32 = (uint32_t *) sig;
67 cpuid (&eax, &sig32[0], &sig32[1], &sig32[2]);
68 sig[12] = 0; /* \0-terminate the string to make string comparison possible */
76 const uint32_t base = 0x40000000;
79 /* Most hypervisors only have information in leaf 0x40000000.
81 * Some hypervisors have "Viridian [HyperV] extensions", and those
82 * must appear in slot 0x40000000, but they will also have the true
83 * hypervisor in a higher slot.
85 * CPUID is supposed to return the maximum leaf offset in %eax, but
86 * this is not reliable. Instead we check the returned signatures
87 * against a known list (the others will be empty or garbage) and
88 * only print the ones we know about. This is OK because if we add
89 * a new test in virt-what we can update the list.
91 * By searching backwards we only print the highest entry, thus
92 * ignoring Viridian for Xen (and Nutanix). If we ever encounter a
93 * hypervisor that has more than 2 entries we may need to revisit
96 for (leaf = base + 0xff00; leaf >= base; leaf -= 0x100) {
97 memset (sig, 0, sizeof sig);
98 cpuid_leaf (leaf, sig);
99 if (known_signature (sig)) {
106 #else /* !i386, !x86_64 */
111 /* nothing for other architectures */