-
Notifications
You must be signed in to change notification settings - Fork 8
/
xen-detect.c
114 lines (97 loc) · 3.2 KB
/
xen-detect.c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/******************************************************************************
* xen-detect.c
*
* Simple GNU C / POSIX application to detect execution on Xen VMM platform.
*
* Copyright (c) 2007, XenSource Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
static int xen_pv_context;
static uint32_t xen_base = 0x40000000;
static jmp_buf xen_sigill_jmp;
static void xen_sigill_handler(int sig)
{
longjmp(xen_sigill_jmp, 1);
}
static void xen_cpuid(uint32_t idx,
uint32_t *eax,
uint32_t *ebx,
uint32_t *ecx,
uint32_t *edx)
{
asm volatile (
"test %1,%1 ; jz 1f ; ud2a ; .ascii \"xen\" ; 1: cpuid"
: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
: "0" (idx), "1" (xen_pv_context) );
}
static bool xen_check_inner(void)
{
uint32_t eax, ebx, ecx, edx;
char signature[13];
for (xen_base = 0x40000000; xen_base < 0x40010000; xen_base += 0x100)
{
xen_cpuid(xen_base, &eax, &ebx, &ecx, &edx);
*(uint32_t *)(signature + 0) = ebx;
*(uint32_t *)(signature + 4) = ecx;
*(uint32_t *)(signature + 8) = edx;
signature[12] = '\0';
if (!strcmp("XenVMMXenVMM", signature) && eax >= (xen_base + 2))
return true;
}
return false;
}
static void xen_explain(void)
{
uint32_t eax, ebx, ecx, edx;
xen_cpuid(xen_base + 1, &eax, &ebx, &ecx, &edx);
printf("Hypervisor: Xen %s\n", xen_pv_context ? "PV" : "HVM");
printf("Version: %d.%d\n", (uint16_t)(eax >> 16), (uint16_t)eax);
}
static bool xen_check_outer(void)
{
struct sigaction act, oldact;
bool ret;
/* first check for HVM... */
xen_pv_context = 0;
if (xen_check_inner())
return true;
/* not HVM... catch SIGILL in case we're running outside Xen */
if (setjmp(xen_sigill_jmp))
{
xen_pv_context = 0;
return false;
}
memset(&act, 0, sizeof act);
act.sa_handler = xen_sigill_handler;
sigemptyset(&act.sa_mask);
if (sigaction(SIGILL, &act, &oldact))
return false;
/* actually do the check now */
xen_pv_context = 1;
ret = xen_check_inner();
/* restore old SIGILL handler */
sigaction(SIGILL, &oldact, NULL);
return ret;
}
static struct test_impl xen_impl = {
.hv_name = "Xen",
.check = xen_check_outer,
.explain = xen_explain,
};