1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
/*
* Identify hypervisor vendor
*
* based on code from util-linux lscpu
*/
#include <stdio.h>
#include <string.h>
/*
* This CPUID leaf returns the information about the hypervisor.
* EAX : maximum input value for CPUID supported by the hypervisor.
* EBX, ECX, EDX : Hypervisor vendor ID signature. E.g. VMwareVMware.
*/
#define HYPERVISOR_INFO_LEAF 0x40000000
static inline void
cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
__asm__(
#if defined(__PIC__) && defined(__i386__)
/* x86 PIC cannot clobber ebx -- gcc bitches */
"pushl %%ebx;"
"cpuid;"
"movl %%ebx, %%esi;"
"popl %%ebx;"
: "=S" (*ebx),
#else
"cpuid;"
: "=b" (*ebx),
#endif
"=a" (*eax),
"=c" (*ecx),
"=d" (*edx)
: "1" (op), "c"(0));
}
static const char *get_hypervisor(void)
{
unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
char hyper_vendor_id[13];
memset(hyper_vendor_id, 0, sizeof(hyper_vendor_id));
cpuid(HYPERVISOR_INFO_LEAF, &eax, &ebx, &ecx, &edx);
memcpy(hyper_vendor_id + 0, &ebx, 4);
memcpy(hyper_vendor_id + 4, &ecx, 4);
memcpy(hyper_vendor_id + 8, &edx, 4);
hyper_vendor_id[12] = '\0';
if (!hyper_vendor_id[0])
return NULL;
else if (!strncmp("XenVMMXenVMM", hyper_vendor_id, 12))
return "Xen";
else if (!strncmp("KVMKVMKVM", hyper_vendor_id, 9))
return "KVM";
else if (!strncmp("Microsoft Hv", hyper_vendor_id, 12))
return "Microsoft HyperV";
else if (!strncmp("VMwareVMware", hyper_vendor_id, 12))
return "VMware";
else
return "Unknown";
}
int main(int argc, char **argv)
{
const char *vm = get_hypervisor();
if (vm) {
printf("%s\n", vm);
return 0;
} else
return 1;
}
|