ia64/xen-unstable

view tools/misc/xen-detect.c @ 13884:870370439fb9

Fix xen-detect build.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Thu Feb 08 17:07:36 2007 +0000 (2007-02-08)
parents 6a383beedf83
children fd720db48675
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];
54 cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
55 *(uint32_t *)(signature + 0) = ebx;
56 *(uint32_t *)(signature + 4) = ecx;
57 *(uint32_t *)(signature + 8) = edx;
58 signature[12] = '\0';
60 if ( strcmp("XenVMMXenVMM", signature) || (eax < 0x40000002) )
61 return 0;
63 cpuid(0x40000001, &eax, &ebx, &ecx, &edx);
64 printf("Running in %s context on Xen v%d.%d.\n",
65 pv_context ? "PV" : "HVM", (uint16_t)(eax >> 16), (uint16_t)eax);
66 return 1;
67 }
69 int main(void)
70 {
71 pid_t pid;
72 int status;
73 uint32_t dummy;
75 /* Check for execution in HVM context. */
76 if ( check_for_xen() )
77 return 0;
79 /* Now we check for execution in PV context. */
80 pv_context = 1;
82 /*
83 * Fork a child to test the paravirtualised CPUID instruction.
84 * If executed outside Xen PV context, the extended opcode will fault.
85 */
86 pid = fork();
87 switch ( pid )
88 {
89 case 0:
90 /* Child: test paravirtualised CPUID opcode and then exit cleanly. */
91 cpuid(0x40000000, &dummy, &dummy, &dummy, &dummy);
92 exit(0);
93 case -1:
94 fprintf(stderr, "Fork failed.\n");
95 return 0;
96 }
98 /*
99 * Parent waits for child to terminate and checks for clean exit.
100 * Only if the exit is clean is it safe for us to try the extended CPUID.
101 */
102 waitpid(pid, &status, 0);
103 if ( WIFEXITED(status) && check_for_xen() )
104 return 0;
106 printf("Not running on Xen.\n");
107 return 0;
108 }