12 #if defined(ARCH_X86) || defined(ARCH_X86_64)
17 #if defined (__NetBSD__) || defined(__OpenBSD__)
18 #include <sys/param.h>
19 #include <sys/sysctl.h>
20 #include <machine/cpu.h>
23 #if defined(__FreeBSD__) || defined(__DragonFly__)
24 #include <sys/types.h>
25 #include <sys/sysctl.h>
37 #include <proto/exec.h>
40 //#define X86_FXSR_MAGIC
41 /* Thanks to the FreeBSD project for some of this cpuid code, and
42 * help understanding how to use it. Thanks to the Mesa
43 * team for SSE support detection and more cpu detect code.
46 /* I believe this code works. However, it has only been used on a PII and PIII */
48 static void check_os_katmai_support( void );
51 // return TRUE if cpuid supported
52 static int has_cpuid(void)
56 // code from libavcodec:
57 __asm__
__volatile__ (
58 /* See if CPUID instruction is supported ... */
59 /* ... Get copies of EFLAGS into eax and ecx */
64 /* ... Toggle the ID bit in one copy and store */
65 /* to the EFLAGS reg */
66 "xor $0x200000, %0\n\t"
70 /* ... Get the (hopefully modified) EFLAGS */
83 do_cpuid(unsigned int ax
, unsigned int *p
)
88 : "=a" (p
[0]), "=b" (p
[1]), "=c" (p
[2]), "=d" (p
[3])
92 // code from libavcodec:
94 ("mov %%"REG_b
", %%"REG_S
"\n\t"
96 "xchg %%"REG_b
", %%"REG_S
97 : "=a" (p
[0]), "=S" (p
[1]),
98 "=c" (p
[2]), "=d" (p
[3])
104 void GetCpuCaps( CpuCaps
*caps
)
106 unsigned int regs
[4];
107 unsigned int regs2
[4];
109 memset(caps
, 0, sizeof(*caps
));
111 caps
->cl_size
=32; /* default */
113 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"CPUID not supported!??? (maybe an old 486?)\n");
116 do_cpuid(0x00000000, regs
); // get _max_ cpuid level and vendor name
117 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU vendor name: %.4s%.4s%.4s max cpuid level: %d\n",
118 (char*) (regs
+1),(char*) (regs
+3),(char*) (regs
+2), regs
[0]);
119 if (regs
[0]>=0x00000001)
124 do_cpuid(0x00000001, regs2
);
126 caps
->cpuType
=(regs2
[0] >> 8)&0xf;
128 // see AMD64 Architecture Programmer's Manual, Volume 3: General-purpose and
129 // System Instructions, Table 3-2: Effective family computation, page 120.
130 if(caps
->cpuType
==0xf){
131 // use extended family (P4, IA64, K8)
132 caps
->cpuType
=0xf+((regs2
[0]>>20)&255);
134 caps
->cpuStepping
=regs2
[0] & 0xf;
136 // general feature flags:
137 caps
->hasTSC
= (regs2
[3] & (1 << 8 )) >> 8; // 0x0000010
138 caps
->hasMMX
= (regs2
[3] & (1 << 23 )) >> 23; // 0x0800000
139 caps
->hasSSE
= (regs2
[3] & (1 << 25 )) >> 25; // 0x2000000
140 caps
->hasSSE2
= (regs2
[3] & (1 << 26 )) >> 26; // 0x4000000
141 caps
->hasMMX2
= caps
->hasSSE
; // SSE cpus supports mmxext too
142 cl_size
= ((regs2
[1] >> 8) & 0xFF)*8;
143 if(cl_size
) caps
->cl_size
= cl_size
;
145 tmpstr
=GetCpuFriendlyName(regs
, regs2
);
146 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: %s ",tmpstr
);
148 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"(Family: %d, Stepping: %d)\n",
149 caps
->cpuType
, caps
->cpuStepping
);
152 do_cpuid(0x80000000, regs
);
153 if (regs
[0]>=0x80000001) {
154 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"extended cpuid-level: %d\n",regs
[0]&0x7FFFFFFF);
155 do_cpuid(0x80000001, regs2
);
156 caps
->hasMMX
|= (regs2
[3] & (1 << 23 )) >> 23; // 0x0800000
157 caps
->hasMMX2
|= (regs2
[3] & (1 << 22 )) >> 22; // 0x400000
158 caps
->has3DNow
= (regs2
[3] & (1 << 31 )) >> 31; //0x80000000
159 caps
->has3DNowExt
= (regs2
[3] & (1 << 30 )) >> 30;
161 if(regs
[0]>=0x80000006)
163 do_cpuid(0x80000006, regs2
);
164 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"extended cache-info: %d\n",regs2
[2]&0x7FFFFFFF);
165 caps
->cl_size
= regs2
[2] & 0xFF;
167 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"Detected cache-line size is %u bytes\n",caps
->cl_size
);
169 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"cpudetect: MMX=%d MMX2=%d SSE=%d SSE2=%d 3DNow=%d 3DNowExt=%d\n",
175 gCpuCaps
.has3DNowExt
);
178 /* FIXME: Does SSE2 need more OS support, too? */
179 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__) || defined(__OpenBSD__) || defined(__DragonFly__)
181 check_os_katmai_support();
189 // caps->hasMMX2 = 0;
193 if(caps
->hasMMX
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"MMX supported but disabled\n");
197 if(caps
->hasMMX2
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"MMX2 supported but disabled\n");
201 if(caps
->hasSSE
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"SSE supported but disabled\n");
205 if(caps
->hasSSE2
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"SSE2 supported but disabled\n");
209 if(caps
->has3DNow
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"3DNow supported but disabled\n");
213 if(caps
->has3DNowExt
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"3DNowExt supported but disabled\n");
219 #define CPUID_EXTFAMILY ((regs2[0] >> 20)&0xFF) /* 27..20 */
220 #define CPUID_EXTMODEL ((regs2[0] >> 16)&0x0F) /* 19..16 */
221 #define CPUID_TYPE ((regs2[0] >> 12)&0x04) /* 13..12 */
222 #define CPUID_FAMILY ((regs2[0] >> 8)&0x0F) /* 11..08 */
223 #define CPUID_MODEL ((regs2[0] >> 4)&0x0F) /* 07..04 */
224 #define CPUID_STEPPING ((regs2[0] >> 0)&0x0F) /* 03..00 */
226 char *GetCpuFriendlyName(unsigned int regs
[], unsigned int regs2
[]){
227 #include "cputable.h" /* get cpuname and cpuvendors */
232 if (NULL
==(retname
=(char*)malloc(256))) {
233 mp_msg(MSGT_CPUDETECT
,MSGL_FATAL
,"Error: GetCpuFriendlyName() not enough memory\n");
237 sprintf(vendor
,"%.4s%.4s%.4s",(char*)(regs
+1),(char*)(regs
+3),(char*)(regs
+2));
239 for(i
=0; i
<MAX_VENDORS
; i
++){
240 if(!strcmp(cpuvendors
[i
].string
,vendor
)){
241 if(cpuname
[i
][CPUID_FAMILY
][CPUID_MODEL
]){
242 snprintf(retname
,255,"%s %s",cpuvendors
[i
].name
,cpuname
[i
][CPUID_FAMILY
][CPUID_MODEL
]);
244 snprintf(retname
,255,"unknown %s %d. Generation CPU",cpuvendors
[i
].name
,CPUID_FAMILY
);
245 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"unknown %s CPU:\n",cpuvendors
[i
].name
);
246 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Vendor: %s\n",cpuvendors
[i
].string
);
247 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Type: %d\n",CPUID_TYPE
);
248 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Family: %d (ext: %d)\n",CPUID_FAMILY
,CPUID_EXTFAMILY
);
249 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Model: %d (ext: %d)\n",CPUID_MODEL
,CPUID_EXTMODEL
);
250 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Stepping: %d\n",CPUID_STEPPING
);
251 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Please send the above info along with the exact CPU name"
252 "to the MPlayer-Developers, so we can add it to the list!\n");
258 //printf("Detected CPU: %s\n", retname);
262 #undef CPUID_EXTFAMILY
263 #undef CPUID_EXTMODEL
267 #undef CPUID_STEPPING
270 #if defined(__linux__) && defined(_POSIX_SOURCE) && defined(X86_FXSR_MAGIC)
271 static void sigill_handler_sse( int signal
, struct sigcontext sc
)
273 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "SIGILL, " );
275 /* Both the "xorps %%xmm0,%%xmm0" and "divps %xmm0,%%xmm1"
276 * instructions are 3 bytes long. We must increment the instruction
277 * pointer manually to avoid repeated execution of the offending
280 * If the SIGILL is caused by a divide-by-zero when unmasked
281 * exceptions aren't supported, the SIMD FPU status and control
282 * word will be restored at the end of the test, so we don't need
283 * to worry about doing it here. Besides, we may not be able to...
290 static void sigfpe_handler_sse( int signal
, struct sigcontext sc
)
292 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "SIGFPE, " );
294 if ( sc
.fpstate
->magic
!= 0xffff ) {
295 /* Our signal context has the extended FPU state, so reset the
296 * divide-by-zero exception mask and clear the divide-by-zero
299 sc
.fpstate
->mxcsr
|= 0x00000200;
300 sc
.fpstate
->mxcsr
&= 0xfffffffb;
302 /* If we ever get here, we're completely hosed.
304 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "\n\n" );
305 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "SSE enabling test failed badly!" );
308 #endif /* __linux__ && _POSIX_SOURCE && X86_FXSR_MAGIC */
311 LONG CALLBACK
win32_sig_handler_sse(EXCEPTION_POINTERS
* ep
)
313 if(ep
->ExceptionRecord
->ExceptionCode
==EXCEPTION_ILLEGAL_INSTRUCTION
){
314 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "SIGILL, " );
315 ep
->ContextRecord
->Eip
+=3;
317 return EXCEPTION_CONTINUE_EXECUTION
;
319 return EXCEPTION_CONTINUE_SEARCH
;
323 /* If we're running on a processor that can do SSE, let's see if we
324 * are allowed to or not. This will catch 2.4.0 or later kernels that
325 * haven't been configured for a Pentium III but are running on one,
326 * and RedHat patched 2.2 kernels that have broken exception handling
327 * support for user space apps that do SSE.
329 static void check_os_katmai_support( void )
334 #elif defined(__FreeBSD__) || defined(__DragonFly__)
336 size_t len
=sizeof(has_sse
);
338 ret
= sysctlbyname("hw.instruction_sse", &has_sse
, &len
, NULL
, 0);
342 #elif defined(__NetBSD__) || defined (__OpenBSD__)
343 #if __NetBSD_Version__ >= 105250000 || (defined __OpenBSD__)
344 int has_sse
, has_sse2
, ret
, mib
[2];
347 mib
[0] = CTL_MACHDEP
;
349 varlen
= sizeof(has_sse
);
351 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
352 ret
= sysctl(mib
, 2, &has_sse
, &varlen
, NULL
, 0);
353 if (ret
< 0 || !has_sse
) {
355 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "no!\n" );
358 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "yes!\n" );
362 varlen
= sizeof(has_sse2
);
363 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE2... " );
364 ret
= sysctl(mib
, 2, &has_sse2
, &varlen
, NULL
, 0);
365 if (ret
< 0 || !has_sse2
) {
367 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "no!\n" );
370 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "yes!\n" );
374 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "No OS support for SSE, disabling to be safe.\n" );
377 LPTOP_LEVEL_EXCEPTION_FILTER exc_fil
;
378 if ( gCpuCaps
.hasSSE
) {
379 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
380 exc_fil
= SetUnhandledExceptionFilter(win32_sig_handler_sse
);
381 __asm
__volatile ("xorps %xmm0, %xmm0");
382 SetUnhandledExceptionFilter(exc_fil
);
383 if ( gCpuCaps
.hasSSE
) mp_msg(MSGT_CPUDETECT
,MSGL_V
, "yes.\n" );
384 else mp_msg(MSGT_CPUDETECT
,MSGL_V
, "no!\n" );
386 #elif defined(__linux__)
387 #if defined(_POSIX_SOURCE) && defined(X86_FXSR_MAGIC)
388 struct sigaction saved_sigill
;
389 struct sigaction saved_sigfpe
;
391 /* Save the original signal handlers.
393 sigaction( SIGILL
, NULL
, &saved_sigill
);
394 sigaction( SIGFPE
, NULL
, &saved_sigfpe
);
396 signal( SIGILL
, (void (*)(int))sigill_handler_sse
);
397 signal( SIGFPE
, (void (*)(int))sigfpe_handler_sse
);
399 /* Emulate test for OSFXSR in CR4. The OS will set this bit if it
400 * supports the extended FPU save and restore required for SSE. If
401 * we execute an SSE instruction on a PIII and get a SIGILL, the OS
402 * doesn't support Streaming SIMD Exceptions, even if the processor
405 if ( gCpuCaps
.hasSSE
) {
406 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
408 // __asm __volatile ("xorps %%xmm0, %%xmm0");
409 __asm
__volatile ("xorps %xmm0, %xmm0");
411 if ( gCpuCaps
.hasSSE
) {
412 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "yes.\n" );
414 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "no!\n" );
418 /* Emulate test for OSXMMEXCPT in CR4. The OS will set this bit if
419 * it supports unmasked SIMD FPU exceptions. If we unmask the
420 * exceptions, do a SIMD divide-by-zero and get a SIGILL, the OS
421 * doesn't support unmasked SIMD FPU exceptions. If we get a SIGFPE
422 * as expected, we're okay but we need to clean up after it.
424 * Are we being too stringent in our requirement that the OS support
425 * unmasked exceptions? Certain RedHat 2.2 kernels enable SSE by
426 * setting CR4.OSFXSR but don't support unmasked exceptions. Win98
427 * doesn't even support them. We at least know the user-space SSE
428 * support is good in kernels that do support unmasked exceptions,
429 * and therefore to be safe I'm going to leave this test in here.
431 if ( gCpuCaps
.hasSSE
) {
432 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE unmasked exceptions... " );
434 // test_os_katmai_exception_support();
436 if ( gCpuCaps
.hasSSE
) {
437 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "yes.\n" );
439 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "no!\n" );
443 /* Restore the original signal handlers.
445 sigaction( SIGILL
, &saved_sigill
, NULL
);
446 sigaction( SIGFPE
, &saved_sigfpe
, NULL
);
448 /* If we've gotten to here and the XMM CPUID bit is still set, we're
449 * safe to go ahead and hook out the SSE code throughout Mesa.
451 if ( gCpuCaps
.hasSSE
) {
452 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Tests of OS support for SSE passed.\n" );
454 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Tests of OS support for SSE failed!\n" );
457 /* We can't use POSIX signal handling to test the availability of
458 * SSE, so we disable it by default.
460 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "Cannot test OS support for SSE, disabling to be safe.\n" );
462 #endif /* _POSIX_SOURCE && X86_FXSR_MAGIC */
464 /* Do nothing on other platforms for now.
466 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "Cannot test OS support for SSE, leaving disabled.\n" );
468 #endif /* __linux__ */
470 #else /* ARCH_X86 || ARCH_X86_64 */
473 #include <sys/sysctl.h>
479 static sigjmp_buf jmpbuf
;
480 static volatile sig_atomic_t canjump
= 0;
482 static void sigill_handler (int sig
)
485 signal (sig
, SIG_DFL
);
490 siglongjmp (jmpbuf
, 1);
492 #endif //__AMIGAOS4__
495 void GetCpuCaps( CpuCaps
*caps
)
506 caps
->hasAltiVec
= 0;
510 rip-off from ffmpeg altivec detection code.
511 this code also appears on Apple's AltiVec pages.
514 int sels
[2] = {CTL_HW
, HW_VECTORUNIT
};
516 size_t len
= sizeof(has_vu
);
519 err
= sysctl(sels
, 2, &has_vu
, &len
, NULL
, 0);
523 caps
->hasAltiVec
= 1;
525 #else /* SYS_DARWIN */
529 GetCPUInfoTags(GCIT_VectorUnit
, &result
, TAG_DONE
);
530 if (result
== VECTORTYPE_ALTIVEC
)
531 caps
->hasAltiVec
= 1;
533 /* no Darwin, do it the brute-force way */
534 /* this is borrowed from the libmpeg2 library */
536 signal (SIGILL
, sigill_handler
);
537 if (sigsetjmp (jmpbuf
, 1)) {
538 signal (SIGILL
, SIG_DFL
);
542 asm volatile ("mtspr 256, %0\n\t"
543 "vand %%v0, %%v0, %%v0"
547 signal (SIGILL
, SIG_DFL
);
548 caps
->hasAltiVec
= 1;
551 #endif //__AMIGAOS4__
552 #endif /* SYS_DARWIN */
553 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"AltiVec %sfound\n", (caps
->hasAltiVec
? "" : "not "));
554 #endif /* HAVE_ALTIVEC */
557 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Intel Itanium\n");
561 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Sun Sparc\n");
565 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: ARM\n");
569 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: PowerPC\n");
573 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Digital Alpha\n");
577 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: SGI MIPS\n");
581 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Hewlett-Packard PA-RISC\n");
585 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: IBM S/390\n");
589 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: IBM S/390X\n");
593 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
, "CPU: Digital VAX\n" );
596 #endif /* !ARCH_X86 */