14 #if defined (__NetBSD__) || defined(__OpenBSD__)
15 #include <sys/param.h>
16 #include <sys/sysctl.h>
17 #include <machine/cpu.h>
18 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
19 #include <sys/types.h>
20 #include <sys/sysctl.h>
21 #elif defined(__linux__)
23 #elif defined(__MINGW32__) || defined(__CYGWIN__)
25 #elif defined(__OS2__)
28 #elif defined(__AMIGAOS4__)
29 #include <proto/exec.h>
32 /* Thanks to the FreeBSD project for some of this cpuid code, and
33 * help understanding how to use it. Thanks to the Mesa
34 * team for SSE support detection and more cpu detect code.
37 /* I believe this code works. However, it has only been used on a PII and PIII */
39 static void check_os_katmai_support( void );
41 // return TRUE if cpuid supported
42 static int has_cpuid(void)
44 // code from libavcodec:
50 /* See if CPUID instruction is supported ... */
51 /* ... Get copies of EFLAGS into eax and ecx */
56 /* ... Toggle the ID bit in one copy and store */
57 /* to the EFLAGS reg */
58 "xor $0x200000, %0\n\t"
62 /* ... Get the (hopefully modified) EFLAGS */
75 do_cpuid(unsigned int ax
, unsigned int *p
)
80 : "=a" (p
[0]), "=b" (p
[1]), "=c" (p
[2]), "=d" (p
[3])
84 // code from libavcodec:
86 ("mov %%"REG_b
", %%"REG_S
"\n\t"
88 "xchg %%"REG_b
", %%"REG_S
89 : "=a" (p
[0]), "=S" (p
[1]),
90 "=c" (p
[2]), "=d" (p
[3])
96 void GetCpuCaps( CpuCaps
*caps
)
99 unsigned int regs2
[4];
101 memset(caps
, 0, sizeof(*caps
));
103 caps
->cl_size
=32; /* default */
105 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"CPUID not supported!??? (maybe an old 486?)\n");
108 do_cpuid(0x00000000, regs
); // get _max_ cpuid level and vendor name
109 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU vendor name: %.4s%.4s%.4s max cpuid level: %d\n",
110 (char*) (regs
+1),(char*) (regs
+3),(char*) (regs
+2), regs
[0]);
111 if (regs
[0]>=0x00000001)
113 char *tmpstr
, *ptmpstr
;
116 do_cpuid(0x00000001, regs2
);
118 caps
->cpuType
=(regs2
[0] >> 8)&0xf;
119 caps
->cpuModel
=(regs2
[0] >> 4)&0xf;
121 // see AMD64 Architecture Programmer's Manual, Volume 3: General-purpose and
122 // System Instructions, Table 3-2: Effective family computation, page 120.
123 if(caps
->cpuType
==0xf){
124 // use extended family (P4, IA64, K8)
125 caps
->cpuType
=0xf+((regs2
[0]>>20)&255);
127 if(caps
->cpuType
==0xf || caps
->cpuType
==6)
128 caps
->cpuModel
|= ((regs2
[0]>>16)&0xf) << 4;
130 caps
->cpuStepping
=regs2
[0] & 0xf;
132 // general feature flags:
133 caps
->hasTSC
= (regs2
[3] & (1 << 8 )) >> 8; // 0x0000010
134 caps
->hasMMX
= (regs2
[3] & (1 << 23 )) >> 23; // 0x0800000
135 caps
->hasSSE
= (regs2
[3] & (1 << 25 )) >> 25; // 0x2000000
136 caps
->hasSSE2
= (regs2
[3] & (1 << 26 )) >> 26; // 0x4000000
137 caps
->hasSSE3
= (regs2
[2] & 1); // 0x0000001
138 caps
->hasSSSE3
= (regs2
[2] & (1 << 9 )) >> 9; // 0x0000200
139 caps
->hasMMX2
= caps
->hasSSE
; // SSE cpus supports mmxext too
140 cl_size
= ((regs2
[1] >> 8) & 0xFF)*8;
141 if(cl_size
) caps
->cl_size
= cl_size
;
143 ptmpstr
=tmpstr
=GetCpuFriendlyName(regs
, regs2
);
144 while(*ptmpstr
== ' ') // strip leading spaces
146 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU: %s ", ptmpstr
);
148 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"(Family: %d, Model: %d, Stepping: %d)\n",
149 caps
->cpuType
, caps
->cpuModel
, 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;
160 caps
->hasSSE4a
= (regs2
[2] & (1 << 6 )) >> 6; // 0x0000040
162 if(regs
[0]>=0x80000006)
164 do_cpuid(0x80000006, regs2
);
165 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"extended cache-info: %d\n",regs2
[2]&0x7FFFFFFF);
166 caps
->cl_size
= regs2
[2] & 0xFF;
168 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"Detected cache-line size is %u bytes\n",caps
->cl_size
);
170 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"cpudetect: MMX=%d MMX2=%d SSE=%d SSE2=%d 3DNow=%d 3DNowExt=%d\n",
176 gCpuCaps
.has3DNowExt
);
179 /* FIXME: Does SSE2 need more OS support, too? */
180 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
181 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
182 || defined(__APPLE__) || defined(__CYGWIN__) || defined(__MINGW32__) \
185 check_os_katmai_support();
193 // caps->hasMMX2 = 0;
196 #if !CONFIG_RUNTIME_CPUDETECT
198 if(caps
->hasMMX
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"MMX supported but disabled\n");
202 if(caps
->hasMMX2
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"MMX2 supported but disabled\n");
206 if(caps
->hasSSE
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"SSE supported but disabled\n");
210 if(caps
->hasSSE2
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"SSE2 supported but disabled\n");
214 if(caps
->has3DNow
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"3DNow supported but disabled\n");
217 #if !HAVE_AMD3DNOWEXT
218 if(caps
->has3DNowExt
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"3DNowExt supported but disabled\n");
221 #endif // CONFIG_RUNTIME_CPUDETECT
224 char *GetCpuFriendlyName(unsigned int regs
[], unsigned int regs2
[]){
229 if (NULL
==(retname
=malloc(256))) {
230 mp_msg(MSGT_CPUDETECT
,MSGL_FATAL
,"Error: GetCpuFriendlyName() not enough memory\n");
235 sprintf(vendor
,"%.4s%.4s%.4s",(char*)(regs
+1),(char*)(regs
+3),(char*)(regs
+2));
237 do_cpuid(0x80000000,regs
);
238 if (regs
[0] >= 0x80000004)
240 // CPU has built-in namestring
241 for (i
= 0x80000002; i
<= 0x80000004; i
++)
244 strncat(retname
, (char*)regs
, 16);
250 #if defined(__linux__) && defined(_POSIX_SOURCE) && !ARCH_X86_64
251 static void sigill_handler_sse( int signal
, struct sigcontext sc
)
253 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "SIGILL, " );
255 /* Both the "xorps %%xmm0,%%xmm0" and "divps %xmm0,%%xmm1"
256 * instructions are 3 bytes long. We must increment the instruction
257 * pointer manually to avoid repeated execution of the offending
260 * If the SIGILL is caused by a divide-by-zero when unmasked
261 * exceptions aren't supported, the SIMD FPU status and control
262 * word will be restored at the end of the test, so we don't need
263 * to worry about doing it here. Besides, we may not be able to...
269 #endif /* __linux__ && _POSIX_SOURCE */
271 #if (defined(__MINGW32__) || defined(__CYGWIN__)) && !ARCH_X86_64
272 LONG CALLBACK
win32_sig_handler_sse(EXCEPTION_POINTERS
* ep
)
274 if(ep
->ExceptionRecord
->ExceptionCode
==EXCEPTION_ILLEGAL_INSTRUCTION
){
275 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "SIGILL, " );
276 ep
->ContextRecord
->Eip
+=3;
278 return EXCEPTION_CONTINUE_EXECUTION
;
280 return EXCEPTION_CONTINUE_SEARCH
;
282 #endif /* defined(__MINGW32__) || defined(__CYGWIN__) */
285 ULONG _System
os2_sig_handler_sse( PEXCEPTIONREPORTRECORD p1
,
286 PEXCEPTIONREGISTRATIONRECORD p2
,
290 if(p1
->ExceptionNum
== XCPT_ILLEGAL_INSTRUCTION
){
291 mp_msg(MSGT_CPUDETECT
, MSGL_V
, "SIGILL, ");
296 return XCPT_CONTINUE_EXECUTION
;
298 return XCPT_CONTINUE_SEARCH
;
302 /* If we're running on a processor that can do SSE, let's see if we
303 * are allowed to or not. This will catch 2.4.0 or later kernels that
304 * haven't been configured for a Pentium III but are running on one,
305 * and RedHat patched 2.2 kernels that have broken exception handling
306 * support for user space apps that do SSE.
309 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
310 #define SSE_SYSCTL_NAME "hw.instruction_sse"
311 #elif defined(__APPLE__)
312 #define SSE_SYSCTL_NAME "hw.optional.sse"
315 static void check_os_katmai_support( void )
320 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
322 size_t len
=sizeof(has_sse
);
324 ret
= sysctlbyname(SSE_SYSCTL_NAME
, &has_sse
, &len
, NULL
, 0);
328 #elif defined(__NetBSD__) || defined (__OpenBSD__)
329 #if __NetBSD_Version__ >= 105250000 || (defined __OpenBSD__)
330 int has_sse
, has_sse2
, ret
, mib
[2];
333 mib
[0] = CTL_MACHDEP
;
335 varlen
= sizeof(has_sse
);
337 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
338 ret
= sysctl(mib
, 2, &has_sse
, &varlen
, NULL
, 0);
339 gCpuCaps
.hasSSE
= ret
>= 0 && has_sse
;
340 mp_msg(MSGT_CPUDETECT
,MSGL_V
, gCpuCaps
.hasSSE
? "yes.\n" : "no!\n" );
343 varlen
= sizeof(has_sse2
);
344 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE2... " );
345 ret
= sysctl(mib
, 2, &has_sse2
, &varlen
, NULL
, 0);
346 gCpuCaps
.hasSSE2
= ret
>= 0 && has_sse2
;
347 mp_msg(MSGT_CPUDETECT
,MSGL_V
, gCpuCaps
.hasSSE2
? "yes.\n" : "no!\n" );
350 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "No OS support for SSE, disabling to be safe.\n" );
352 #elif defined(__MINGW32__) || defined(__CYGWIN__)
353 LPTOP_LEVEL_EXCEPTION_FILTER exc_fil
;
354 if ( gCpuCaps
.hasSSE
) {
355 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
356 exc_fil
= SetUnhandledExceptionFilter(win32_sig_handler_sse
);
357 __asm__
volatile ("xorps %xmm0, %xmm0");
358 SetUnhandledExceptionFilter(exc_fil
);
359 mp_msg(MSGT_CPUDETECT
,MSGL_V
, gCpuCaps
.hasSSE
? "yes.\n" : "no!\n" );
361 #elif defined(__OS2__)
362 EXCEPTIONREGISTRATIONRECORD RegRec
= { 0, &os2_sig_handler_sse
};
363 if ( gCpuCaps
.hasSSE
) {
364 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
365 DosSetExceptionHandler( &RegRec
);
366 __asm__
volatile ("xorps %xmm0, %xmm0");
367 DosUnsetExceptionHandler( &RegRec
);
368 mp_msg(MSGT_CPUDETECT
,MSGL_V
, gCpuCaps
.hasSSE
? "yes.\n" : "no!\n" );
370 #elif defined(__linux__)
371 #if defined(_POSIX_SOURCE)
372 struct sigaction saved_sigill
;
374 /* Save the original signal handlers.
376 sigaction( SIGILL
, NULL
, &saved_sigill
);
378 signal( SIGILL
, (void (*)(int))sigill_handler_sse
);
380 /* Emulate test for OSFXSR in CR4. The OS will set this bit if it
381 * supports the extended FPU save and restore required for SSE. If
382 * we execute an SSE instruction on a PIII and get a SIGILL, the OS
383 * doesn't support Streaming SIMD Exceptions, even if the processor
386 if ( gCpuCaps
.hasSSE
) {
387 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
389 // __asm__ volatile ("xorps %%xmm0, %%xmm0");
390 __asm__
volatile ("xorps %xmm0, %xmm0");
392 mp_msg(MSGT_CPUDETECT
,MSGL_V
, gCpuCaps
.hasSSE
? "yes.\n" : "no!\n" );
395 /* Restore the original signal handlers.
397 sigaction( SIGILL
, &saved_sigill
, NULL
);
399 /* If we've gotten to here and the XMM CPUID bit is still set, we're
400 * safe to go ahead and hook out the SSE code throughout Mesa.
402 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Tests of OS support for SSE %s\n", gCpuCaps
.hasSSE
? "passed." : "failed!" );
404 /* We can't use POSIX signal handling to test the availability of
405 * SSE, so we disable it by default.
407 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "Cannot test OS support for SSE, disabling to be safe.\n" );
409 #endif /* _POSIX_SOURCE */
411 /* Do nothing on other platforms for now.
413 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "Cannot test OS support for SSE, leaving disabled.\n" );
415 #endif /* __linux__ */
420 #include <sys/sysctl.h>
421 #elif defined(__AMIGAOS4__)
427 static sigjmp_buf jmpbuf
;
428 static volatile sig_atomic_t canjump
= 0;
430 static void sigill_handler (int sig
)
433 signal (sig
, SIG_DFL
);
438 siglongjmp (jmpbuf
, 1);
440 #endif /* __APPLE__ */
442 void GetCpuCaps( CpuCaps
*caps
)
457 caps
->hasAltiVec
= 0;
461 rip-off from ffmpeg altivec detection code.
462 this code also appears on Apple's AltiVec pages.
465 int sels
[2] = {CTL_HW
, HW_VECTORUNIT
};
467 size_t len
= sizeof(has_vu
);
470 err
= sysctl(sels
, 2, &has_vu
, &len
, NULL
, 0);
474 caps
->hasAltiVec
= 1;
476 #elif defined(__AMIGAOS4__)
479 GetCPUInfoTags(GCIT_VectorUnit
, &result
, TAG_DONE
);
480 if (result
== VECTORTYPE_ALTIVEC
)
481 caps
->hasAltiVec
= 1;
483 /* no Darwin, do it the brute-force way */
484 /* this is borrowed from the libmpeg2 library */
486 signal (SIGILL
, sigill_handler
);
487 if (sigsetjmp (jmpbuf
, 1)) {
488 signal (SIGILL
, SIG_DFL
);
492 __asm__
volatile ("mtspr 256, %0\n\t"
493 "vand %%v0, %%v0, %%v0"
497 signal (SIGILL
, SIG_DFL
);
498 caps
->hasAltiVec
= 1;
501 #endif /* __APPLE__ */
502 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"AltiVec %sfound\n", (caps
->hasAltiVec
? "" : "not "));
503 #endif /* HAVE_ALTIVEC */
506 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU: Intel Itanium\n");
509 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU: Sun Sparc\n");
512 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU: ARM\n");
515 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU: PowerPC\n");
518 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU: Digital Alpha\n");
521 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU: SGI MIPS\n");
524 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU: Hewlett-Packard PA-RISC\n");
527 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU: IBM S/390\n");
530 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU: IBM S/390X\n");
533 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "CPU: Digital VAX\n" );
536 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "CPU: Tensilica Xtensa\n" );
538 #endif /* !ARCH_X86 */