ia64/xen-unstable

view tools/misc/xen-detect.c @ 19648:f0e2df69a8eb

x86 hvm: Allow cross-vendor migration

Intercept #UD and emulate SYSCALL/SYSENTER/SYSEXIT as necessary.

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue May 26 15:01:36 2009 +0100 (2009-05-26)
parents fd720db48675
children
line source
1 /******************************************************************************
2 * xen_detect.c
3 *
4 * Simple GNU C / POSIX application to detect execution on Xen VMM platform.
5 *
6 * Copyright (c) 2007, XenSource Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to
10 * deal in the Software without restriction, including without limitation the
11 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 * sell copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <unistd.h>
35 static int pv_context;
37 static void cpuid(uint32_t idx,
38 uint32_t *eax,
39 uint32_t *ebx,
40 uint32_t *ecx,
41 uint32_t *edx)
42 {
43 asm volatile (
44 "test %1,%1 ; jz 1f ; ud2a ; .ascii \"xen\" ; 1: cpuid"
45 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
46 : "0" (idx), "1" (pv_context) );
47 }
49 static int check_for_xen(void)
50 {
51 uint32_t eax, ebx, ecx, edx;
52 char signature[13];
53 uint32_t base;
55 for ( base = 0x40000000; base < 0x40001000; base += 0x100 )
56 {
57 cpuid(base, &eax, &ebx, &ecx, &edx);
59 *(uint32_t *)(signature + 0) = ebx;
60 *(uint32_t *)(signature + 4) = ecx;
61 *(uint32_t *)(signature + 8) = edx;
62 signature[12] = '\0';
64 if ( !strcmp("XenVMMXenVMM", signature) && (eax >= (base + 2)) )
65 goto found;
66 }
68 return 0;
70 found:
71 cpuid(base + 1, &eax, &ebx, &ecx, &edx);
72 printf("Running in %s context on Xen v%d.%d.\n",
73 pv_context ? "PV" : "HVM", (uint16_t)(eax >> 16), (uint16_t)eax);
74 return 1;
75 }
77 int main(void)
78 {
79 pid_t pid;
80 int status;
81 uint32_t dummy;
83 /* Check for execution in HVM context. */
84 if ( check_for_xen() )
85 return 0;
87 /* Now we check for execution in PV context. */
88 pv_context = 1;
90 /*
91 * Fork a child to test the paravirtualised CPUID instruction.
92 * If executed outside Xen PV context, the extended opcode will fault.
93 */
94 pid = fork();
95 switch ( pid )
96 {
97 case 0:
98 /* Child: test paravirtualised CPUID opcode and then exit cleanly. */
99 cpuid(0x40000000, &dummy, &dummy, &dummy, &dummy);
100 exit(0);
101 case -1:
102 fprintf(stderr, "Fork failed.\n");
103 return 0;
104 }
106 /*
107 * Parent waits for child to terminate and checks for clean exit.
108 * Only if the exit is clean is it safe for us to try the extended CPUID.
109 */
110 waitpid(pid, &status, 0);
111 if ( WIFEXITED(status) && check_for_xen() )
112 return 0;
114 printf("Not running on Xen.\n");
115 return 0;
116 }