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 /* Copied from the Linux kernel definition in
51  * arch/x86/include/asm/processor.h
52  */
53 static inline void
54 cpuid (uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
55 {
56   asm volatile ("cpuid"
57                 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
58                 : "0" (*eax), "2" (*ecx)
59                 : "memory");
60 }
61
62 static uint32_t
63 cpuid_leaf (uint32_t eax, char *sig)
64 {
65   uint32_t *sig32 = (uint32_t *) sig;
66
67   cpuid (&eax, &sig32[0], &sig32[1], &sig32[2]);
68   sig[12] = 0; /* \0-terminate the string to make string comparison possible */
69   return eax;
70 }
71
72 static void
73 cpu_sig (void)
74 {
75   char sig[13];
76   const uint32_t base = 0x40000000;
77   uint32_t leaf;
78
79   /* Most hypervisors only have information in leaf 0x40000000.
80    *
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.
84    *
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.
90    *
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
94    * this.
95    */
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)) {
100       puts (sig);
101       break;
102     }
103   }
104 }
105
106 #else /* !i386, !x86_64 */
107
108 static void
109 cpu_sig (void)
110 {
111   /* nothing for other architectures */
112 }
113
114 #endif
115
116 int
117 main()
118 {
119   cpu_sig ();
120   return 0;
121 }