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 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 <string.h>
25
26 #if defined(__i386__) || defined(__x86_64__)
27
28 static unsigned int
29 cpuid (unsigned int eax, char *sig)
30 {
31   unsigned int *sig32 = (unsigned int *) sig;
32
33   asm volatile (
34         "xchgl %%ebx,%1; xor %%ebx,%%ebx; cpuid; xchgl %%ebx,%1"
35         : "=a" (eax), "+r" (sig32[0]), "=c" (sig32[1]), "=d" (sig32[2])
36         : "0" (eax));
37   sig[12] = 0;
38
39   return eax;
40 }
41
42 static void
43 cpu_sig (void)
44 {
45   char sig[13];
46   unsigned int base = 0x40000000, leaf = base;
47   unsigned int max_entries;
48
49   memset (sig, 0, sizeof sig);
50   max_entries = cpuid (leaf, sig);
51   puts (sig);
52
53   /* Most hypervisors only have information in leaf 0x40000000, but
54    * upstream Xen contains further leaf entries (in particular when
55    * used with Viridian [HyperV] extensions).  CPUID is supposed to
56    * return the maximum leaf offset in %eax, so that's what we use,
57    * but only if it looks sensible.
58    */
59   if (max_entries > 3 && max_entries < 0x10000) {
60     for (leaf = base + 0x100; leaf <= base + max_entries; leaf += 0x100) {
61       memset (sig, 0, sizeof sig);
62       cpuid (leaf, sig);
63       puts (sig);
64     }
65   }
66 }
67
68 #else /* !i386, !x86_64 */
69
70 static void
71 cpu_sig (void)
72 {
73   /* nothing for other architectures */
74 }
75
76 #endif
77
78 int
79 main()
80 {
81   cpu_sig ();
82   return 0;
83 }