10000l : fix a crash on x86 due to an horrible mistake in my x86_64 patch
[mplayer.git] / cpudetect.c
blob65d7e5ed6da85b74ff3295c98536c047fdd53a38
1 #include "config.h"
2 #include "cpudetect.h"
3 #include "mp_msg.h"
5 CpuCaps gCpuCaps;
7 #ifdef HAVE_MALLOC_H
8 #include <malloc.h>
9 #endif
10 #include <stdlib.h>
12 #if defined(ARCH_X86) || defined(ARCH_X86_64)
14 #include <stdio.h>
15 #include <string.h>
17 #if defined (__NetBSD__) || defined(__OpenBSD__)
18 #include <sys/param.h>
19 #include <sys/sysctl.h>
20 #include <machine/cpu.h>
21 #endif
23 #ifdef __FreeBSD__
24 #include <sys/types.h>
25 #include <sys/sysctl.h>
26 #endif
28 #ifdef __linux__
29 #include <signal.h>
30 #endif
32 #ifdef WIN32
33 #include <windows.h>
34 #endif
36 //#define X86_FXSR_MAGIC
37 /* Thanks to the FreeBSD project for some of this cpuid code, and
38 * help understanding how to use it. Thanks to the Mesa
39 * team for SSE support detection and more cpu detect code.
42 /* I believe this code works. However, it has only been used on a PII and PIII */
44 static void check_os_katmai_support( void );
46 #if 1
47 // return TRUE if cpuid supported
48 static int has_cpuid()
50 long a, c;
52 // code from libavcodec:
53 __asm__ __volatile__ (
54 /* See if CPUID instruction is supported ... */
55 /* ... Get copies of EFLAGS into eax and ecx */
56 "pushf\n\t"
57 "pop %0\n\t"
58 "mov %0, %1\n\t"
60 /* ... Toggle the ID bit in one copy and store */
61 /* to the EFLAGS reg */
62 "xor $0x200000, %0\n\t"
63 "push %0\n\t"
64 "popf\n\t"
66 /* ... Get the (hopefully modified) EFLAGS */
67 "pushf\n\t"
68 "pop %0\n\t"
69 : "=a" (a), "=c" (c)
71 : "cc"
74 return (a!=c);
76 #endif
78 static void
79 do_cpuid(unsigned int ax, unsigned int *p)
81 #if 0
82 __asm __volatile(
83 "cpuid;"
84 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
85 : "0" (ax)
87 #else
88 // code from libavcodec:
89 __asm __volatile
90 ("mov %%"REG_b", %%"REG_S"\n\t"
91 "cpuid\n\t"
92 "xchg %%"REG_b", %%"REG_S
93 : "=a" (p[0]), "=S" (p[1]),
94 "=c" (p[2]), "=d" (p[3])
95 : "0" (ax));
96 #endif
100 void GetCpuCaps( CpuCaps *caps)
102 unsigned int regs[4];
103 unsigned int regs2[4];
105 memset(caps, 0, sizeof(*caps));
106 caps->isX86=1;
107 caps->cl_size=32; /* default */
108 if (!has_cpuid()) {
109 mp_msg(MSGT_CPUDETECT,MSGL_WARN,"CPUID not supported!??? (maybe an old 486?)\n");
110 return;
112 do_cpuid(0x00000000, regs); // get _max_ cpuid level and vendor name
113 mp_msg(MSGT_CPUDETECT,MSGL_V,"CPU vendor name: %.4s%.4s%.4s max cpuid level: %d\n",
114 (char*) (regs+1),(char*) (regs+3),(char*) (regs+2), regs[0]);
115 if (regs[0]>=0x00000001)
117 char *tmpstr;
118 unsigned cl_size;
120 do_cpuid(0x00000001, regs2);
122 caps->cpuType=(regs2[0] >> 8)&0xf;
123 if(caps->cpuType==0xf){
124 // use extended family (P4, IA64)
125 caps->cpuType=8+((regs2[0]>>20)&255);
127 caps->cpuStepping=regs2[0] & 0xf;
129 // general feature flags:
130 caps->hasTSC = (regs2[3] & (1 << 8 )) >> 8; // 0x0000010
131 caps->hasMMX = (regs2[3] & (1 << 23 )) >> 23; // 0x0800000
132 caps->hasSSE = (regs2[3] & (1 << 25 )) >> 25; // 0x2000000
133 caps->hasSSE2 = (regs2[3] & (1 << 26 )) >> 26; // 0x4000000
134 caps->hasMMX2 = caps->hasSSE; // SSE cpus supports mmxext too
135 cl_size = ((regs2[1] >> 8) & 0xFF)*8;
136 if(cl_size) caps->cl_size = cl_size;
138 tmpstr=GetCpuFriendlyName(regs, regs2);
139 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"CPU: %s ",tmpstr);
140 free(tmpstr);
141 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"(Family: %d, Stepping: %d)\n",
142 caps->cpuType, caps->cpuStepping);
145 do_cpuid(0x80000000, regs);
146 if (regs[0]>=0x80000001) {
147 mp_msg(MSGT_CPUDETECT,MSGL_V,"extended cpuid-level: %d\n",regs[0]&0x7FFFFFFF);
148 do_cpuid(0x80000001, regs2);
149 caps->hasMMX |= (regs2[3] & (1 << 23 )) >> 23; // 0x0800000
150 caps->hasMMX2 |= (regs2[3] & (1 << 22 )) >> 22; // 0x400000
151 caps->has3DNow = (regs2[3] & (1 << 31 )) >> 31; //0x80000000
152 caps->has3DNowExt = (regs2[3] & (1 << 30 )) >> 30;
154 if(regs[0]>=0x80000006)
156 do_cpuid(0x80000006, regs2);
157 mp_msg(MSGT_CPUDETECT,MSGL_V,"extended cache-info: %d\n",regs2[2]&0x7FFFFFFF);
158 caps->cl_size = regs2[2] & 0xFF;
160 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"Detected cache-line size is %u bytes\n",caps->cl_size);
161 #if 0
162 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"cpudetect: MMX=%d MMX2=%d SSE=%d SSE2=%d 3DNow=%d 3DNowExt=%d\n",
163 gCpuCaps.hasMMX,
164 gCpuCaps.hasMMX2,
165 gCpuCaps.hasSSE,
166 gCpuCaps.hasSSE2,
167 gCpuCaps.has3DNow,
168 gCpuCaps.has3DNowExt );
169 #endif
171 /* FIXME: Does SSE2 need more OS support, too? */
172 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__) || defined(__OpenBSD__)
173 if (caps->hasSSE)
174 check_os_katmai_support();
175 if (!caps->hasSSE)
176 caps->hasSSE2 = 0;
177 #else
178 caps->hasSSE=0;
179 caps->hasSSE2 = 0;
180 #endif
181 // caps->has3DNow=1;
182 // caps->hasMMX2 = 0;
183 // caps->hasMMX = 0;
185 #ifndef HAVE_MMX
186 if(caps->hasMMX) mp_msg(MSGT_CPUDETECT,MSGL_WARN,"MMX supported but disabled\n");
187 caps->hasMMX=0;
188 #endif
189 #ifndef HAVE_MMX2
190 if(caps->hasMMX2) mp_msg(MSGT_CPUDETECT,MSGL_WARN,"MMX2 supported but disabled\n");
191 caps->hasMMX2=0;
192 #endif
193 #ifndef HAVE_SSE
194 if(caps->hasSSE) mp_msg(MSGT_CPUDETECT,MSGL_WARN,"SSE supported but disabled\n");
195 caps->hasSSE=0;
196 #endif
197 #ifndef HAVE_SSE2
198 if(caps->hasSSE2) mp_msg(MSGT_CPUDETECT,MSGL_WARN,"SSE2 supported but disabled\n");
199 caps->hasSSE2=0;
200 #endif
201 #ifndef HAVE_3DNOW
202 if(caps->has3DNow) mp_msg(MSGT_CPUDETECT,MSGL_WARN,"3DNow supported but disabled\n");
203 caps->has3DNow=0;
204 #endif
205 #ifndef HAVE_3DNOWEX
206 if(caps->has3DNowExt) mp_msg(MSGT_CPUDETECT,MSGL_WARN,"3DNowExt supported but disabled\n");
207 caps->has3DNowExt=0;
208 #endif
212 #define CPUID_EXTFAMILY ((regs2[0] >> 20)&0xFF) /* 27..20 */
213 #define CPUID_EXTMODEL ((regs2[0] >> 16)&0x0F) /* 19..16 */
214 #define CPUID_TYPE ((regs2[0] >> 12)&0x04) /* 13..12 */
215 #define CPUID_FAMILY ((regs2[0] >> 8)&0x0F) /* 11..08 */
216 #define CPUID_MODEL ((regs2[0] >> 4)&0x0F) /* 07..04 */
217 #define CPUID_STEPPING ((regs2[0] >> 0)&0x0F) /* 03..00 */
219 char *GetCpuFriendlyName(unsigned int regs[], unsigned int regs2[]){
220 #include "cputable.h" /* get cpuname and cpuvendors */
221 char vendor[17];
222 char *retname;
223 int i;
225 if (NULL==(retname=(char*)malloc(256))) {
226 mp_msg(MSGT_CPUDETECT,MSGL_FATAL,"Error: GetCpuFriendlyName() not enough memory\n");
227 exit(1);
230 sprintf(vendor,"%.4s%.4s%.4s",(char*)(regs+1),(char*)(regs+3),(char*)(regs+2));
232 for(i=0; i<MAX_VENDORS; i++){
233 if(!strcmp(cpuvendors[i].string,vendor)){
234 if(cpuname[i][CPUID_FAMILY][CPUID_MODEL]){
235 snprintf(retname,255,"%s %s",cpuvendors[i].name,cpuname[i][CPUID_FAMILY][CPUID_MODEL]);
236 } else {
237 snprintf(retname,255,"unknown %s %d. Generation CPU",cpuvendors[i].name,CPUID_FAMILY);
238 mp_msg(MSGT_CPUDETECT,MSGL_WARN,"unknown %s CPU:\n",cpuvendors[i].name);
239 mp_msg(MSGT_CPUDETECT,MSGL_WARN,"Vendor: %s\n",cpuvendors[i].string);
240 mp_msg(MSGT_CPUDETECT,MSGL_WARN,"Type: %d\n",CPUID_TYPE);
241 mp_msg(MSGT_CPUDETECT,MSGL_WARN,"Family: %d (ext: %d)\n",CPUID_FAMILY,CPUID_EXTFAMILY);
242 mp_msg(MSGT_CPUDETECT,MSGL_WARN,"Model: %d (ext: %d)\n",CPUID_MODEL,CPUID_EXTMODEL);
243 mp_msg(MSGT_CPUDETECT,MSGL_WARN,"Stepping: %d\n",CPUID_STEPPING);
244 mp_msg(MSGT_CPUDETECT,MSGL_WARN,"Please send the above info along with the exact CPU name"
245 "to the MPlayer-Developers, so we can add it to the list!\n");
250 //printf("Detected CPU: %s\n", retname);
251 return retname;
254 #undef CPUID_EXTFAMILY
255 #undef CPUID_EXTMODEL
256 #undef CPUID_TYPE
257 #undef CPUID_FAMILY
258 #undef CPUID_MODEL
259 #undef CPUID_STEPPING
262 #if defined(__linux__) && defined(_POSIX_SOURCE) && defined(X86_FXSR_MAGIC)
263 static void sigill_handler_sse( int signal, struct sigcontext sc )
265 mp_msg(MSGT_CPUDETECT,MSGL_V, "SIGILL, " );
267 /* Both the "xorps %%xmm0,%%xmm0" and "divps %xmm0,%%xmm1"
268 * instructions are 3 bytes long. We must increment the instruction
269 * pointer manually to avoid repeated execution of the offending
270 * instruction.
272 * If the SIGILL is caused by a divide-by-zero when unmasked
273 * exceptions aren't supported, the SIMD FPU status and control
274 * word will be restored at the end of the test, so we don't need
275 * to worry about doing it here. Besides, we may not be able to...
277 sc.eip += 3;
279 gCpuCaps.hasSSE=0;
282 static void sigfpe_handler_sse( int signal, struct sigcontext sc )
284 mp_msg(MSGT_CPUDETECT,MSGL_V, "SIGFPE, " );
286 if ( sc.fpstate->magic != 0xffff ) {
287 /* Our signal context has the extended FPU state, so reset the
288 * divide-by-zero exception mask and clear the divide-by-zero
289 * exception bit.
291 sc.fpstate->mxcsr |= 0x00000200;
292 sc.fpstate->mxcsr &= 0xfffffffb;
293 } else {
294 /* If we ever get here, we're completely hosed.
296 mp_msg(MSGT_CPUDETECT,MSGL_V, "\n\n" );
297 mp_msg(MSGT_CPUDETECT,MSGL_V, "SSE enabling test failed badly!" );
300 #endif /* __linux__ && _POSIX_SOURCE && X86_FXSR_MAGIC */
302 #ifdef WIN32
303 LONG CALLBACK win32_sig_handler_sse(EXCEPTION_POINTERS* ep)
305 if(ep->ExceptionRecord->ExceptionCode==EXCEPTION_ILLEGAL_INSTRUCTION){
306 mp_msg(MSGT_CPUDETECT,MSGL_V, "SIGILL, " );
307 ep->ContextRecord->Eip +=3;
308 gCpuCaps.hasSSE=0;
309 return EXCEPTION_CONTINUE_EXECUTION;
311 return EXCEPTION_CONTINUE_SEARCH;
313 #endif /* WIN32 */
315 /* If we're running on a processor that can do SSE, let's see if we
316 * are allowed to or not. This will catch 2.4.0 or later kernels that
317 * haven't been configured for a Pentium III but are running on one,
318 * and RedHat patched 2.2 kernels that have broken exception handling
319 * support for user space apps that do SSE.
321 static void check_os_katmai_support( void )
323 #if defined(__FreeBSD__)
324 int has_sse=0, ret;
325 size_t len=sizeof(has_sse);
327 ret = sysctlbyname("hw.instruction_sse", &has_sse, &len, NULL, 0);
328 if (ret || !has_sse)
329 gCpuCaps.hasSSE=0;
331 #elif defined(__NetBSD__) || defined (__OpenBSD__)
332 #if __NetBSD_Version__ >= 105250000 || (defined __OpenBSD__)
333 int has_sse, has_sse2, ret, mib[2];
334 size_t varlen;
336 mib[0] = CTL_MACHDEP;
337 mib[1] = CPU_SSE;
338 varlen = sizeof(has_sse);
340 mp_msg(MSGT_CPUDETECT,MSGL_V, "Testing OS support for SSE... " );
341 ret = sysctl(mib, 2, &has_sse, &varlen, NULL, 0);
342 if (ret < 0 || !has_sse) {
343 gCpuCaps.hasSSE=0;
344 mp_msg(MSGT_CPUDETECT,MSGL_V, "no!\n" );
345 } else {
346 gCpuCaps.hasSSE=1;
347 mp_msg(MSGT_CPUDETECT,MSGL_V, "yes!\n" );
350 mib[1] = CPU_SSE2;
351 varlen = sizeof(has_sse2);
352 mp_msg(MSGT_CPUDETECT,MSGL_V, "Testing OS support for SSE2... " );
353 ret = sysctl(mib, 2, &has_sse2, &varlen, NULL, 0);
354 if (ret < 0 || !has_sse2) {
355 gCpuCaps.hasSSE2=0;
356 mp_msg(MSGT_CPUDETECT,MSGL_V, "no!\n" );
357 } else {
358 gCpuCaps.hasSSE2=1;
359 mp_msg(MSGT_CPUDETECT,MSGL_V, "yes!\n" );
361 #else
362 gCpuCaps.hasSSE = 0;
363 mp_msg(MSGT_CPUDETECT,MSGL_WARN, "No OS support for SSE, disabling to be safe.\n" );
364 #endif
365 #elif defined(WIN32)
366 LPTOP_LEVEL_EXCEPTION_FILTER exc_fil;
367 if ( gCpuCaps.hasSSE ) {
368 mp_msg(MSGT_CPUDETECT,MSGL_V, "Testing OS support for SSE... " );
369 exc_fil = SetUnhandledExceptionFilter(win32_sig_handler_sse);
370 __asm __volatile ("xorps %xmm0, %xmm0");
371 SetUnhandledExceptionFilter(exc_fil);
372 if ( gCpuCaps.hasSSE ) mp_msg(MSGT_CPUDETECT,MSGL_V, "yes.\n" );
373 else mp_msg(MSGT_CPUDETECT,MSGL_V, "no!\n" );
375 #elif defined(__linux__)
376 #if defined(_POSIX_SOURCE) && defined(X86_FXSR_MAGIC)
377 struct sigaction saved_sigill;
378 struct sigaction saved_sigfpe;
380 /* Save the original signal handlers.
382 sigaction( SIGILL, NULL, &saved_sigill );
383 sigaction( SIGFPE, NULL, &saved_sigfpe );
385 signal( SIGILL, (void (*)(int))sigill_handler_sse );
386 signal( SIGFPE, (void (*)(int))sigfpe_handler_sse );
388 /* Emulate test for OSFXSR in CR4. The OS will set this bit if it
389 * supports the extended FPU save and restore required for SSE. If
390 * we execute an SSE instruction on a PIII and get a SIGILL, the OS
391 * doesn't support Streaming SIMD Exceptions, even if the processor
392 * does.
394 if ( gCpuCaps.hasSSE ) {
395 mp_msg(MSGT_CPUDETECT,MSGL_V, "Testing OS support for SSE... " );
397 // __asm __volatile ("xorps %%xmm0, %%xmm0");
398 __asm __volatile ("xorps %xmm0, %xmm0");
400 if ( gCpuCaps.hasSSE ) {
401 mp_msg(MSGT_CPUDETECT,MSGL_V, "yes.\n" );
402 } else {
403 mp_msg(MSGT_CPUDETECT,MSGL_V, "no!\n" );
407 /* Emulate test for OSXMMEXCPT in CR4. The OS will set this bit if
408 * it supports unmasked SIMD FPU exceptions. If we unmask the
409 * exceptions, do a SIMD divide-by-zero and get a SIGILL, the OS
410 * doesn't support unmasked SIMD FPU exceptions. If we get a SIGFPE
411 * as expected, we're okay but we need to clean up after it.
413 * Are we being too stringent in our requirement that the OS support
414 * unmasked exceptions? Certain RedHat 2.2 kernels enable SSE by
415 * setting CR4.OSFXSR but don't support unmasked exceptions. Win98
416 * doesn't even support them. We at least know the user-space SSE
417 * support is good in kernels that do support unmasked exceptions,
418 * and therefore to be safe I'm going to leave this test in here.
420 if ( gCpuCaps.hasSSE ) {
421 mp_msg(MSGT_CPUDETECT,MSGL_V, "Testing OS support for SSE unmasked exceptions... " );
423 // test_os_katmai_exception_support();
425 if ( gCpuCaps.hasSSE ) {
426 mp_msg(MSGT_CPUDETECT,MSGL_V, "yes.\n" );
427 } else {
428 mp_msg(MSGT_CPUDETECT,MSGL_V, "no!\n" );
432 /* Restore the original signal handlers.
434 sigaction( SIGILL, &saved_sigill, NULL );
435 sigaction( SIGFPE, &saved_sigfpe, NULL );
437 /* If we've gotten to here and the XMM CPUID bit is still set, we're
438 * safe to go ahead and hook out the SSE code throughout Mesa.
440 if ( gCpuCaps.hasSSE ) {
441 mp_msg(MSGT_CPUDETECT,MSGL_V, "Tests of OS support for SSE passed.\n" );
442 } else {
443 mp_msg(MSGT_CPUDETECT,MSGL_V, "Tests of OS support for SSE failed!\n" );
445 #else
446 /* We can't use POSIX signal handling to test the availability of
447 * SSE, so we disable it by default.
449 mp_msg(MSGT_CPUDETECT,MSGL_WARN, "Cannot test OS support for SSE, disabling to be safe.\n" );
450 gCpuCaps.hasSSE=0;
451 #endif /* _POSIX_SOURCE && X86_FXSR_MAGIC */
452 #else
453 /* Do nothing on other platforms for now.
455 mp_msg(MSGT_CPUDETECT,MSGL_WARN, "Cannot test OS support for SSE, leaving disabled.\n" );
456 gCpuCaps.hasSSE=0;
457 #endif /* __linux__ */
459 #else /* ARCH_X86 || ARCH_X86_64 */
461 #ifdef SYS_DARWIN
462 #include <sys/sysctl.h>
463 #else
464 #include <signal.h>
465 #include <setjmp.h>
467 static sigjmp_buf jmpbuf;
468 static volatile sig_atomic_t canjump = 0;
470 static void sigill_handler (int sig)
472 if (!canjump) {
473 signal (sig, SIG_DFL);
474 raise (sig);
477 canjump = 0;
478 siglongjmp (jmpbuf, 1);
480 #endif
482 void GetCpuCaps( CpuCaps *caps)
484 caps->cpuType=0;
485 caps->cpuStepping=0;
486 caps->hasMMX=0;
487 caps->hasMMX2=0;
488 caps->has3DNow=0;
489 caps->has3DNowExt=0;
490 caps->hasSSE=0;
491 caps->hasSSE2=0;
492 caps->isX86=0;
493 caps->hasAltiVec = 0;
494 #ifdef HAVE_ALTIVEC
495 #ifdef SYS_DARWIN
497 rip-off from ffmpeg altivec detection code.
498 this code also appears on Apple's AltiVec pages.
501 int sels[2] = {CTL_HW, HW_VECTORUNIT};
502 int has_vu = 0;
503 size_t len = sizeof(has_vu);
504 int err;
506 err = sysctl(sels, 2, &has_vu, &len, NULL, 0);
508 if (err == 0)
509 if (has_vu != 0)
510 caps->hasAltiVec = 1;
512 #else /* SYS_DARWIN */
513 /* no Darwin, do it the brute-force way */
514 /* this is borrowed from the libmpeg2 library */
516 signal (SIGILL, sigill_handler);
517 if (sigsetjmp (jmpbuf, 1)) {
518 signal (SIGILL, SIG_DFL);
519 } else {
520 canjump = 1;
522 asm volatile ("mtspr 256, %0\n\t"
523 "vand %%v0, %%v0, %%v0"
525 : "r" (-1));
527 signal (SIGILL, SIG_DFL);
528 caps->hasAltiVec = 1;
531 #endif /* SYS_DARWIN */
532 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"AltiVec %sfound\n", (caps->hasAltiVec ? "" : "not "));
533 #endif /* HAVE_ALTIVEC */
535 #ifdef ARCH_IA64
536 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"CPU: Intel Itanium\n");
537 #endif
539 #ifdef ARCH_SPARC
540 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"CPU: Sun Sparc\n");
541 #endif
543 #ifdef ARCH_ARMV4L
544 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"CPU: ARM\n");
545 #endif
547 #ifdef ARCH_POWERPC
548 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"CPU: PowerPC\n");
549 #endif
551 #ifdef ARCH_ALPHA
552 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"CPU: Digital Alpha\n");
553 #endif
555 #ifdef ARCH_SGI_MIPS
556 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"CPU: SGI MIPS\n");
557 #endif
559 #ifdef ARCH_PA_RISC
560 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"CPU: Hewlett-Packard PA-RISC\n");
561 #endif
563 #ifdef ARCH_S390
564 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"CPU: IBM S/390\n");
565 #endif
567 #ifdef ARCH_S390X
568 mp_msg(MSGT_CPUDETECT,MSGL_INFO,"CPU: IBM S/390X\n");
569 #endif
571 #ifdef ARCH_VAX
572 mp_msg(MSGT_CPUDETECT,MSGL_INFO, "CPU: Digital VAX\n" );
573 #endif
575 #endif /* !ARCH_X86 */