Add support for Nutanix Acropolis Hypervisor (AHV) (RHBZ#1756381).
[virt-what.git] / virt-what-cpuid-helper.c
1 /* virt-what-cpuid-helper: Are we running inside KVM or Xen HVM?
2  * Copyright (C) 2008-2019 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 /* This program was suggested by Gleb Natapov and written by Paolo
20  * Bonzini, with a few modifications by Richard W.M. Jones.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <string.h>
27
28 #if defined(__i386__) || defined(__x86_64__)
29
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.  The signature is
32  * always 12 bytes except in the case of KVM.
33  */
34 static int
35 known_signature (char *sig)
36 {
37   return
38     strcmp (sig, "bhyve bhyve ") == 0 ||
39     strcmp (sig, "KVMKVMKVM") == 0 ||
40     strcmp (sig, "LKVMLKVMLKVM") == 0 ||
41     strcmp (sig, "Microsoft Hv") == 0 ||
42     strcmp (sig, "OpenBSDVMM58") == 0 ||
43     strcmp (sig, "TCGTCGTCGTCG") == 0 ||
44     strcmp (sig, "VMwareVMware") == 0 ||
45     strcmp (sig, "XenVMMXenVMM") == 0 ||
46     0;
47 }
48
49 static uint32_t
50 cpuid (uint32_t eax, char *sig)
51 {
52   uint32_t *sig32 = (uint32_t *) sig;
53
54   asm volatile (
55         "xchgl %%ebx,%1; xor %%ebx,%%ebx; cpuid; xchgl %%ebx,%1"
56         : "=a" (eax), "+r" (sig32[0]), "=c" (sig32[1]), "=d" (sig32[2])
57         : "0" (eax));
58   sig[12] = 0;
59
60   return eax;
61 }
62
63 static void
64 cpu_sig (void)
65 {
66   char sig[13];
67   const uint32_t base = 0x40000000;
68   uint32_t leaf;
69
70   /* Most hypervisors only have information in leaf 0x40000000.
71    *
72    * Some hypervisors have "Viridian [HyperV] extensions", and those
73    * must appear in slot 0x40000000, but they will also have the true
74    * hypervisor in a higher slot.
75    *
76    * CPUID is supposed to return the maximum leaf offset in %eax, but
77    * this is not reliable.  Instead we check the returned signatures
78    * against a known list (the others will be empty or garbage) and
79    * only print the ones we know about.  This is OK because if we add
80    * a new test in virt-what we can update the list.
81    *
82    * By searching backwards we only print the highest entry, thus
83    * ignoring Viridian for Xen (and Nutanix).  If we ever encounter a
84    * hypervisor that has more than 2 entries we may need to revisit
85    * this.
86    */
87   for (leaf = base + 0xff00; leaf >= base; leaf -= 0x100) {
88     memset (sig, 0, sizeof sig);
89     cpuid (leaf, sig);
90     if (known_signature (sig)) {
91       puts (sig);
92       break;
93     }
94   }
95 }
96
97 #else /* !i386, !x86_64 */
98
99 static void
100 cpu_sig (void)
101 {
102   /* nothing for other architectures */
103 }
104
105 #endif
106
107 int
108 main()
109 {
110   cpu_sig ();
111   return 0;
112 }