blob: fc82b73928fce9cc99cd831a45425e7c65b7a610 [file] [log] [blame]
nethercote288b7662004-01-20 09:24:53 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
sewardjc49ae752006-08-05 12:22:52 +00004#include <assert.h>
nethercote288b7662004-01-20 09:24:53 +00005
njn7fd6d382009-01-22 21:56:32 +00006// This file determines which architectures that this Valgrind installation
7// supports, which depends on the machine's architecture. It also depends
8// on the configuration options; for example, if Valgrind is installed on
9// an AMD64 machine but has been configured with --enable-only32bit then
10// this program will not match "amd64".
11//
nethercote362c0d22004-11-18 18:21:56 +000012// We return:
13// - 0 if the machine matches the asked-for cpu
14// - 1 if it didn't match, but did match the name of another arch
15// - 2 otherwise
16
njn7fd6d382009-01-22 21:56:32 +000017// Nb: When updating this file for a new architecture, add the name to
nethercote362c0d22004-11-18 18:21:56 +000018// 'all_archs' as well as adding go().
19
20#define False 0
21#define True 1
22typedef int Bool;
23
24char* all_archs[] = {
nethercote362c0d22004-11-18 18:21:56 +000025 "amd64",
njn10c122e2005-09-13 00:45:14 +000026 "ppc32",
sewardj0d4f8ac2006-01-02 16:24:03 +000027 "ppc64",
njnc6168192004-11-29 13:54:10 +000028 "x86",
nethercote362c0d22004-11-18 18:21:56 +000029 NULL
30};
nethercoteb625bcb2004-10-19 16:29:30 +000031
njn7fd6d382009-01-22 21:56:32 +000032//-----------------------------------------------------------------------------
33// ppc32-linux
34//---------------------------------------------------------------------------
35#if defined(VGP_ppc32_linux)
njnc6168192004-11-29 13:54:10 +000036static Bool go(char* cpu)
37{
sewardjfcbb9422005-11-10 23:30:21 +000038 if ( strcmp( cpu, "ppc32" ) == 0 )
njnc6168192004-11-29 13:54:10 +000039 return True;
sewardj87f7fdd2006-01-04 03:08:16 +000040 return False;
njnc6168192004-11-29 13:54:10 +000041}
njn7fd6d382009-01-22 21:56:32 +000042#endif // VGP_ppc32_linux
sewardj0d4f8ac2006-01-02 16:24:03 +000043
njn7fd6d382009-01-22 21:56:32 +000044//---------------------------------------------------------------------------
45// ppc64-linux
46//---------------------------------------------------------------------------
47#if defined(VGP_ppc64_linux)
sewardj0d4f8ac2006-01-02 16:24:03 +000048static Bool go(char* cpu)
49{
50 if ( strcmp( cpu, "ppc64" ) == 0 )
51 return True;
sewardj87f7fdd2006-01-04 03:08:16 +000052 if ( strcmp( cpu, "ppc32" ) == 0 )
53 return True;
54 return False;
sewardj0d4f8ac2006-01-02 16:24:03 +000055}
njn7fd6d382009-01-22 21:56:32 +000056#endif // VGP_ppc64_linux
njnc6168192004-11-29 13:54:10 +000057
njn7fd6d382009-01-22 21:56:32 +000058//---------------------------------------------------------------------------
59// ppc{32,64}-aix
60//---------------------------------------------------------------------------
61#if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5)
sewardjbe714ff2006-10-17 01:49:50 +000062static Bool go(char* cpu)
63{
64 if (sizeof(void*) == 8) {
65 /* cpu is in 64-bit mode */
66 if ( strcmp( cpu, "ppc64" ) == 0 )
67 return True;
68 if ( strcmp( cpu, "ppc32" ) == 0 )
69 return True;
70 } else {
71 if ( strcmp( cpu, "ppc32" ) == 0 )
72 return True;
73 }
74 return False;
75}
njn7fd6d382009-01-22 21:56:32 +000076#endif // VGP_ppc32_aix5 || VGP_ppc64_aix5
sewardjbe714ff2006-10-17 01:49:50 +000077
njn7fd6d382009-01-22 21:56:32 +000078//---------------------------------------------------------------------------
79// {x86,amd64}-linux (part 1 of 2)
80//---------------------------------------------------------------------------
81#if defined(VGP_x86_linux) || defined(VGP_amd64_linux)
sewardjb5f6f512005-03-10 23:59:00 +000082static void cpuid ( unsigned int n,
83 unsigned int* a, unsigned int* b,
84 unsigned int* c, unsigned int* d )
nethercote288b7662004-01-20 09:24:53 +000085{
86 __asm__ __volatile__ (
87 "cpuid"
88 : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) /* output */
89 : "0" (n) /* input */
90 );
91}
njn7fd6d382009-01-22 21:56:32 +000092#endif // VGP_x86_linux || VGP_amd64_linux
nethercote288b7662004-01-20 09:24:53 +000093
njn7fd6d382009-01-22 21:56:32 +000094//---------------------------------------------------------------------------
95// {x86,amd64}-{linux} (part 2 of 2)
96//---------------------------------------------------------------------------
97#if defined(VGP_x86_linux) || defined(VGP_amd64_linux)
nethercote362c0d22004-11-18 18:21:56 +000098static Bool go(char* cpu)
nethercoteb625bcb2004-10-19 16:29:30 +000099{
sewardjc49ae752006-08-05 12:22:52 +0000100 unsigned int level = 0, cmask = 0, dmask = 0, a, b, c, d;
nethercote288b7662004-01-20 09:24:53 +0000101
nethercoteb625bcb2004-10-19 16:29:30 +0000102 if ( strcmp( cpu, "x86" ) == 0 ) {
nethercote362c0d22004-11-18 18:21:56 +0000103 return True;
nethercoteb625bcb2004-10-19 16:29:30 +0000104 } else if ( strcmp( cpu, "x86-fpu" ) == 0 ) {
105 level = 1;
sewardjc49ae752006-08-05 12:22:52 +0000106 dmask = 1 << 0;
nethercoteb625bcb2004-10-19 16:29:30 +0000107 } else if ( strcmp( cpu, "x86-cmov" ) == 0 ) {
108 level = 1;
sewardjc49ae752006-08-05 12:22:52 +0000109 dmask = 1 << 15;
nethercoteb625bcb2004-10-19 16:29:30 +0000110 } else if ( strcmp( cpu, "x86-mmx" ) == 0 ) {
111 level = 1;
sewardjc49ae752006-08-05 12:22:52 +0000112 dmask = 1 << 23;
nethercoteb625bcb2004-10-19 16:29:30 +0000113 } else if ( strcmp( cpu, "x86-mmxext" ) == 0 ) {
114 level = 0x80000001;
sewardjc49ae752006-08-05 12:22:52 +0000115 dmask = 1 << 22;
nethercoteb625bcb2004-10-19 16:29:30 +0000116 } else if ( strcmp( cpu, "x86-sse" ) == 0 ) {
117 level = 1;
sewardjc49ae752006-08-05 12:22:52 +0000118 dmask = 1 << 25;
nethercoteb625bcb2004-10-19 16:29:30 +0000119 } else if ( strcmp( cpu, "x86-sse2" ) == 0 ) {
120 level = 1;
sewardjc49ae752006-08-05 12:22:52 +0000121 dmask = 1 << 26;
122 } else if ( strcmp( cpu, "x86-sse3" ) == 0 ) {
123 level = 1;
124 cmask = 1 << 0;
toma00febc2009-01-12 15:26:59 +0000125 } else if ( strcmp( cpu, "x86-ssse3" ) == 0 ) {
126 level = 1;
127 cmask = 1 << 9;
njn7fd6d382009-01-22 21:56:32 +0000128#if defined(VGA_amd64)
tomef567322005-11-11 14:33:43 +0000129 } else if ( strcmp( cpu, "amd64" ) == 0 ) {
130 return True;
sewardjc49ae752006-08-05 12:22:52 +0000131 } else if ( strcmp( cpu, "amd64-sse3" ) == 0 ) {
132 level = 1;
133 cmask = 1 << 0;
toma00febc2009-01-12 15:26:59 +0000134 } else if ( strcmp( cpu, "amd64-ssse3" ) == 0 ) {
135 level = 1;
136 cmask = 1 << 9;
tomef567322005-11-11 14:33:43 +0000137#endif
nethercoteb625bcb2004-10-19 16:29:30 +0000138 } else {
nethercote362c0d22004-11-18 18:21:56 +0000139 return False;
nethercote288b7662004-01-20 09:24:53 +0000140 }
141
sewardjc49ae752006-08-05 12:22:52 +0000142 assert( !(cmask != 0 && dmask != 0) );
143 assert( !(cmask == 0 && dmask == 0) );
144
nethercote1018bdd2004-02-11 23:33:29 +0000145 cpuid( level & 0x80000000, &a, &b, &c, &d );
nethercote288b7662004-01-20 09:24:53 +0000146
nethercote1018bdd2004-02-11 23:33:29 +0000147 if ( a >= level ) {
148 cpuid( level, &a, &b, &c, &d );
nethercote288b7662004-01-20 09:24:53 +0000149
sewardjc49ae752006-08-05 12:22:52 +0000150 if (dmask > 0 && (d & dmask) != 0) return True;
151 if (cmask > 0 && (c & cmask) != 0) return True;
nethercote288b7662004-01-20 09:24:53 +0000152 }
nethercote362c0d22004-11-18 18:21:56 +0000153 return False;
nethercoteb625bcb2004-10-19 16:29:30 +0000154}
njn7fd6d382009-01-22 21:56:32 +0000155#endif // VGP_x86_linux || VGP_amd64_linux
nethercote288b7662004-01-20 09:24:53 +0000156
nethercoteb625bcb2004-10-19 16:29:30 +0000157
njn7fd6d382009-01-22 21:56:32 +0000158//---------------------------------------------------------------------------
159// main
160//---------------------------------------------------------------------------
nethercoteb625bcb2004-10-19 16:29:30 +0000161int main(int argc, char **argv)
162{
nethercote362c0d22004-11-18 18:21:56 +0000163 int i;
nethercoteb625bcb2004-10-19 16:29:30 +0000164 if ( argc != 2 ) {
165 fprintf( stderr, "usage: cputest <cpu-type>\n" );
nethercote362c0d22004-11-18 18:21:56 +0000166 exit( 2 );
nethercoteb625bcb2004-10-19 16:29:30 +0000167 }
nethercote362c0d22004-11-18 18:21:56 +0000168 if (go( argv[1] )) {
169 return 0; // matched
170 }
171 for (i = 0; NULL != all_archs[i]; i++) {
172 if ( strcmp( argv[1], all_archs[i] ) == 0 )
173 return 1;
174 }
175 return 2;
nethercote288b7662004-01-20 09:24:53 +0000176}