From: Qiang Fu Subject: xen-detect fail. Date: Thu, 8 Apr 2010 13:56:38 +0800 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1713880807==" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org --===============1713880807== Content-Type: multipart/alternative; boundary=0016e64c24ac9176950483b35750 --0016e64c24ac9176950483b35750 Content-Type: text/plain; charset=ISO-8859-1 Hi, xen-detect may hang on some cases. I hit this issue on one of my box. And I found that it hang when executing "ud2a". Seems it not trap and sig the user. OS: RHEL 5.4 2.6.18-164.15.1.el5 glibc-2.5-42.el5_4.3.x86_64 /var/log/message kernel: xen-detect general protection rip:400665 rsp:7fff3a922888 error:0 On platform it work correctly, the message is kernel: xen-detect trap invalid opcode rip:40068d rsp:7fffa8acadc8 error:0 Any idea of this, why it behaves different? source codes: ------------------------ #include #include #include #include #include #include #include #include static void cpuid(uint32_t idx, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx, int pv_context) { asm volatile ( "test %1,%1 ; jz 1f ; ud2a ; .ascii \"xen\" ; 1: cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "0" (idx), "1" (pv_context) ); } static int check_for_xen(int pv_context) { uint32_t eax, ebx, ecx, edx; char signature[13]; uint32_t base; for ( base = 0x40000000; base < 0x40010000; base += 0x100 ) { cpuid(base, &eax, &ebx, &ecx, &edx, pv_context); *(uint32_t *)(signature + 0) = ebx; *(uint32_t *)(signature + 4) = ecx; *(uint32_t *)(signature + 8) = edx; signature[12] = '\0'; if ( !strcmp("XenVMMXenVMM", signature) && (eax >= (base + 2)) ) goto found; } return 0; found: cpuid(base + 1, &eax, &ebx, &ecx, &edx, pv_context); return 1; } static jmp_buf sigill_jmp; void sigill_handler(int sig) { longjmp(sigill_jmp, 1); } static void usage(void) { printf("Usage: xen_detect [options]\n"); printf("Options:\n"); printf(" -h, --help Display this information\n"); printf(" -q, --quiet Quiesce normal informational output\n"); printf(" -P, --pv Exit status 1 if not running as PV guest\n"); printf(" -H, --hvm Exit status 1 if not running as HVM guest.\n"); printf(" -N, --none Exit status 1 if running on Xen (PV or HVM)\n"); } int main(int argc, char **argv) { enum { XEN_PV = 1, XEN_HVM = 2, XEN_NONE = 3 } detected = 0, expected = 0; uint32_t version = 0; int ch, quiet = 0; const static char sopts[] = "hqPHN"; const static struct option lopts[] = { { "help", 0, NULL, 'h' }, { "quiet", 0, NULL, 'q' }, { "pv", 0, NULL, 'P' }, { "hvm", 0, NULL, 'H' }, { "none", 0, NULL, 'N' }, { 0, 0, 0, 0} }; while ( (ch = getopt_long(argc, argv, sopts, lopts, NULL)) != -1 ) { switch ( ch ) { case 'q': quiet = 1; break; case 'P': expected = XEN_PV; break; case 'H': expected = XEN_HVM; break; case 'N': expected = XEN_NONE; break; default: usage(); exit(1); } } /* Check for execution in HVM context. */ detected = XEN_HVM; if ( (version = check_for_xen(0)) != 0 ) goto out; /* * Set up a signal handler to test the paravirtualised CPUID instruction. * If executed outside Xen PV context, the extended opcode will fault, we * will longjmp via the signal handler, and print "Not running on Xen". */ detected = XEN_PV; if ( !setjmp(sigill_jmp) && (signal(SIGILL, sigill_handler) != SIG_ERR) && ((version = check_for_xen(1)) != 0) ) goto out; detected = XEN_NONE; out: if ( quiet ) /* nothing */; else if ( detected == XEN_NONE ) printf("Not running on Xen.\n"); else printf("Running in %s context on Xen v%d.%d.\n", (detected == XEN_PV) ? "PV" : "HVM", (uint16_t)(version >> 16), (uint16_t)version); return expected && (expected != detected); } --0016e64c24ac9176950483b35750 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi,

xen-detect may hang on some cases.
I hit this issue on one of= my box. And I found that
it hang when executing "ud2a". Seems= it not trap
and sig the user.

OS: RHEL 5.4=A0=A0=A0=A0=A0=A0 2.= 6.18-164.15.1.el5
glibc-2.5-42.el5_4.3.x86_64

/var/log/message
kernel: xen-detect g= eneral protection rip:400665 rsp:7fff3a922888 error:0

On platform it= work correctly, the message is
kernel: xen-detect trap invalid opcode r= ip:40068d rsp:7fffa8acadc8 error:0

Any idea of this, why it behaves different?

source codes:
---= ---------------------
#include <stdint.h>
#include <stdlib.h= >
#include <stdio.h>
#include <string.h>
#include &= lt;setjmp.h>
#include <signal.h>
#include <unistd.h>
#include <geto= pt.h>

static void cpuid(uint32_t idx,
=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0 uint32_t *eax,
=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0 uint32_t *ebx,
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0 uint32_t *ecx,
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 uint32_t *edx,
=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 int pv_context)
{
= =A0=A0=A0 asm volatile (
=A0=A0=A0=A0=A0=A0=A0 "test %1,%1 ; jz 1f = ; ud2a ; .ascii \"xen\" ; 1: cpuid"
=A0=A0=A0=A0=A0=A0=A0= : "=3Da" (*eax), "=3Db" (*ebx), "=3Dc" (*ecx= ), "=3Dd" (*edx)
=A0=A0=A0=A0=A0=A0=A0 : "0" (idx), "1" (pv_context) );<= br>}

static int check_for_xen(int pv_context)
{
=A0=A0=A0 uint= 32_t eax, ebx, ecx, edx;
=A0=A0=A0 char signature[13];
=A0=A0=A0 uint= 32_t base;

=A0=A0=A0 for ( base =3D 0x40000000; base < 0x40010000= ; base +=3D 0x100 )
=A0=A0=A0 {
=A0=A0=A0=A0=A0=A0=A0 cpuid(base, &eax, &ebx, &e= cx, &edx, pv_context);

=A0=A0=A0=A0=A0=A0=A0 *(uint32_t *)(signa= ture + 0) =3D ebx;
=A0=A0=A0=A0=A0=A0=A0 *(uint32_t *)(signature + 4) = =3D ecx;
=A0=A0=A0=A0=A0=A0=A0 *(uint32_t *)(signature + 8) =3D edx;
=A0=A0=A0=A0=A0=A0=A0 signature[12] =3D '\0';

=A0=A0=A0=A0= =A0=A0=A0 if ( !strcmp("XenVMMXenVMM", signature) && (eax= >=3D (base + 2)) )
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 goto found;
= =A0=A0=A0 }

=A0=A0=A0 return 0;

=A0found:
=A0=A0=A0 cpuid(= base + 1, &eax, &ebx, &ecx, &edx, pv_context);
=A0=A0=A0 return 1;
}

static jmp_buf sigill_jmp;
void sigill_h= andler(int sig)
{
=A0=A0=A0 longjmp(sigill_jmp, 1);
}

stati= c void usage(void)
{
=A0=A0=A0 printf("Usage: xen_detect [option= s]\n");
=A0=A0=A0 printf("Options:\n");
=A0=A0=A0 printf("=A0 -h,= --help=A0=A0=A0 Display this information\n");
=A0=A0=A0 printf(&qu= ot;=A0 -q, --quiet=A0=A0 Quiesce normal informational output\n");
= =A0=A0=A0 printf("=A0 -P, --pv=A0=A0=A0=A0=A0 Exit status 1 if not run= ning as PV guest\n");
=A0=A0=A0 printf("=A0 -H, --hvm=A0=A0=A0=A0 Exit status 1 if not runni= ng as HVM guest.\n");
=A0=A0=A0 printf("=A0 -N, --none=A0=A0= =A0 Exit status 1 if running on Xen (PV or HVM)\n");
}

int m= ain(int argc, char **argv)
{
=A0=A0=A0 enum { XEN_PV =3D 1, XEN_HVM =3D 2, XEN_NONE =3D 3 } detecte= d =3D 0, expected =3D 0;
=A0=A0=A0 uint32_t version =3D 0;
=A0=A0=A0 = int ch, quiet =3D 0;

=A0=A0=A0 const static char sopts[] =3D "h= qPHN";
=A0=A0=A0 const static struct option lopts[] =3D {
=A0=A0=A0=A0=A0=A0=A0 { "help",=A0 0, NULL, 'h' },
=A0= =A0=A0=A0=A0=A0=A0 { "quiet", 0, NULL, 'q' },
=A0=A0= =A0=A0=A0=A0=A0 { "pv",=A0=A0=A0 0, NULL, 'P' },
=A0= =A0=A0=A0=A0=A0=A0 { "hvm",=A0=A0 0, NULL, 'H' },
=A0=A0=A0=A0=A0=A0=A0 { "none",=A0 0, NULL, 'N' },
=A0= =A0=A0=A0=A0=A0=A0 { 0, 0, 0, 0}
=A0=A0=A0 };

=A0=A0=A0 while ( (= ch =3D getopt_long(argc, argv, sopts, lopts, NULL)) !=3D -1 )
=A0=A0=A0 = {
=A0=A0=A0=A0=A0=A0=A0 switch ( ch )
=A0=A0=A0=A0=A0=A0=A0 {
=A0= =A0=A0=A0=A0=A0=A0 case 'q':
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 quiet =3D 1;
=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0 break;
=A0=A0=A0=A0=A0=A0=A0 case 'P':
=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0 expected =3D XEN_PV;
=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 break;
=A0=A0=A0=A0=A0=A0=A0 case 'H':
=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0 expected =3D XEN_HVM;
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0 break;
=A0=A0=A0=A0=A0=A0=A0 case 'N':
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 expected =3D XEN_NONE;
=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0 break;
=A0=A0=A0=A0=A0=A0=A0 default:
=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0 usage();
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 exit(1)= ;
=A0=A0=A0=A0=A0=A0=A0 }
=A0=A0=A0 }

=A0=A0=A0 /* Check for e= xecution in HVM context. */
=A0=A0=A0 detected =3D XEN_HVM;
=A0=A0=A0 if ( (version =3D check_for_xen(0)) !=3D 0 )
=A0=A0=A0=A0=A0= =A0=A0 goto out;

=A0=A0=A0 /*
=A0=A0=A0=A0 * Set up a signal hand= ler to test the paravirtualised CPUID instruction.
=A0=A0=A0=A0 * If exe= cuted outside Xen PV context, the extended opcode will fault, we
=A0=A0=A0=A0 * will longjmp via the signal handler, and print "Not run= ning on Xen".
=A0=A0=A0=A0 */
=A0=A0=A0 detected =3D XEN_PV;
= =A0=A0=A0 if ( !setjmp(sigill_jmp)
=A0=A0=A0=A0=A0=A0=A0=A0 && (= signal(SIGILL, sigill_handler) !=3D SIG_ERR)
=A0=A0=A0=A0=A0=A0=A0=A0 && ((version =3D check_for_xen(1)) !=3D 0)= )
=A0=A0=A0=A0=A0=A0=A0 goto out;

=A0=A0=A0 detected =3D XEN_NON= E;

=A0out:
=A0=A0=A0 if ( quiet )
=A0=A0=A0=A0=A0=A0=A0 /* not= hing */;
=A0=A0=A0 else if ( detected =3D=3D XEN_NONE )
=A0=A0=A0=A0= =A0=A0=A0 printf("Not running on Xen.\n");
=A0=A0=A0 else
=A0=A0=A0=A0=A0=A0=A0 printf("Running in %s context = on Xen v%d.%d.\n",
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (dete= cted =3D=3D XEN_PV) ? "PV" : "HVM",
=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0 (uint16_t)(version >> 16), (uint16_t)vers= ion);

=A0=A0=A0 return expected && (expected !=3D detected);
}
=
--0016e64c24ac9176950483b35750-- --===============1713880807== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --===============1713880807==--