设为首页 收藏本站
查看: 1284|回复: 0

[经验分享] xen detect

[复制链接]

尚未签到

发表于 2015-10-11 14:18:43 | 显示全部楼层 |阅读模式
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 */
26
27#include <stdint.h>
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <setjmp.h>
32#include <signal.h>
33#include <unistd.h>
34#include <getopt.h>
35
36static void cpuid(uint32_t idx,
37 uint32_t *eax,
38 uint32_t *ebx,
39 uint32_t *ecx,
40 uint32_t *edx,
41 int pv_context)
42{
43 asm volatile (
44 &quot;test %1,%1 ; jz 1f ; ud2a ; .ascii \&quot;xen\&quot; ; 1: cpuid&quot;
45 : &quot;=a&quot; (*eax), &quot;=b&quot; (*ebx), &quot;=c&quot; (*ecx), &quot;=d&quot; (*edx)
46 : &quot;0&quot; (idx), &quot;1&quot; (pv_context) );
47}
48
49static int check_for_xen(int pv_context)
50{
51 uint32_t eax, ebx, ecx, edx;
52 char signature[13];
53 uint32_t base;
54
55 for ( base = 0x40000000; base < 0x40010000; base &#43;= 0x100 )
56 {
57 cpuid(base, &eax, &ebx, &ecx, &edx, pv_context);
58
59 *(uint32_t *)(signature &#43; 0) = ebx;
60 *(uint32_t *)(signature &#43; 4) = ecx;
61 *(uint32_t *)(signature &#43; 8) = edx;
62 signature[12] = '\0';
63
64 if ( !strcmp(&quot;XenVMMXenVMM&quot;, signature) && (eax >= (base &#43; 2)) )
65 goto found;
66 }
67
68 return 0;
69
70 found:
71 cpuid(base &#43; 1, &eax, &ebx, &ecx, &edx, pv_context);
72 return 1;
73}
74
75static jmp_buf sigill_jmp;
76void sigill_handler(int sig)
77{
78 longjmp(sigill_jmp, 1);
79}
80
81static void usage(void)
82{
83 printf(&quot;Usage: xen_detect [options]\n&quot;);
84 printf(&quot;Options:\n&quot;);
85 printf(&quot; -h, --help Display this information\n&quot;);
86 printf(&quot; -q, --quiet Quiesce normal informational output\n&quot;);
87 printf(&quot; -P, --pv Exit status 1 if not running as PV guest\n&quot;);
88 printf(&quot; -H, --hvm Exit status 1 if not running as HVM guest.\n&quot;);
89 printf(&quot; -N, --none Exit status 1 if running on Xen (PV or HVM)\n&quot;);
90}
91
92int main(int argc, char **argv)
93{
94 enum { XEN_PV = 1, XEN_HVM = 2, XEN_NONE = 3 } detected = 0, expected = 0;
95 uint32_t version = 0;
96 int ch, quiet = 0;
97
98 const static char sopts[] = &quot;hqPHN&quot;;
99 const static struct option lopts[] = {
100 { &quot;help&quot;, 0, NULL, 'h' },
101 { &quot;quiet&quot;, 0, NULL, 'q' },
102 { &quot;pv&quot;, 0, NULL, 'P' },
103 { &quot;hvm&quot;, 0, NULL, 'H' },
104 { &quot;none&quot;, 0, NULL, 'N' },
105 { 0, 0, 0, 0}
106 };
107
108 while ( (ch = getopt_long(argc, argv, sopts, lopts, NULL)) != -1 )
109 {
110 switch ( ch )
111 {
112 case 'q':
113 quiet = 1;
114 break;
115 case 'P':
116 expected = XEN_PV;
117 break;
118 case 'H':
119 expected = XEN_HVM;
120 break;
121 case 'N':
122 expected = XEN_NONE;
123 break;
124 default:
125 usage();
126 exit(1);
127 }
128 }
129
130 /* Check for execution in HVM context. */
131 detected = XEN_HVM;
132 if ( (version = check_for_xen(0)) != 0 )
133 goto out;
134
135 /*
136 * Set up a signal handler to test the paravirtualised CPUID instruction.
137 * If executed outside Xen PV context, the extended opcode will fault, we
138 * will longjmp via the signal handler, and print &quot;Not running on Xen&quot;.
139 */
140 detected = XEN_PV;
141 if ( !setjmp(sigill_jmp)
142 && (signal(SIGILL, sigill_handler) != SIG_ERR)
143 && ((version = check_for_xen(1)) != 0) )
144 goto out;
145
146 detected = XEN_NONE;
147
148 out:
149 if ( quiet )
150 /* nothing */;
151 else if ( detected == XEN_NONE )
152 printf(&quot;Not running on Xen.\n&quot;);
153 else
154 printf(&quot;Running in %s context on Xen v%d.%d.\n&quot;,
155 (detected == XEN_PV) ? &quot;PV&quot; : &quot;HVM&quot;,
156 (uint16_t)(version >> 16), (uint16_t)version);
157
158 return expected && (expected != detected);
159}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-125501-1-1.html 上篇帖子: Ubuntu安装Xen4(可用) 下篇帖子: xen ubuntu8.04 指南
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表