virt-what-cvm: rename 'azure-hcl' fact to 'hyperv-hcl'
[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.  Note the
32  * signature is always 12 bytes long, plus we add \0 to the end to
33  * make it 13 bytes.
34  */
35 static int
36 known_signature (const char *sig)
37 {
38   return
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 ||
47     0;
48 }
49
50 static uint32_t
51 cpuid (uint32_t eax, char *sig)
52 {
53   uint32_t *sig32 = (uint32_t *) sig;
54
55   asm volatile (
56         "xchgl %%ebx,%1; xor %%ebx,%%ebx; cpuid; xchgl %%ebx,%1"
57         : "=a" (eax), "+r" (sig32[0]), "=c" (sig32[1]), "=d" (sig32[2])
58         : "0" (eax));
59   sig[12] = 0;
60
61   return eax;
62 }
63
64 static void
65 cpu_sig (void)
66 {
67   char sig[13];
68   const uint32_t base = 0x40000000;
69   uint32_t leaf;
70
71   /* Most hypervisors only have information in leaf 0x40000000.
72    *
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.
76    *
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.
82    *
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
86    * this.
87    */
88   for (leaf = base + 0xff00; leaf >= base; leaf -= 0x100) {
89     memset (sig, 0, sizeof sig);
90     cpuid (leaf, sig);
91     if (known_signature (sig)) {
92       puts (sig);
93       break;
94     }
95   }
96 }
97
98 #else /* !i386, !x86_64 */
99
100 static void
101 cpu_sig (void)
102 {
103   /* nothing for other architectures */
104 }
105
106 #endif
107
108 int
109 main()
110 {
111   cpu_sig ();
112   return 0;
113 }