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(__FreeBSD_kernel__) || defined(__DragonFly__)
24 #include <sys/types.h>
25 #include <sys/sysctl.h>
37 #include <proto/exec.h>
40 /* Thanks to the FreeBSD project for some of this cpuid code, and
41 * help understanding how to use it. Thanks to the Mesa
42 * team for SSE support detection and more cpu detect code.
45 /* I believe this code works. However, it has only been used on a PII and PIII */
47 static void check_os_katmai_support( void );
50 // return TRUE if cpuid supported
51 static int has_cpuid(void)
55 // code from libavcodec:
56 __asm__
__volatile__ (
57 /* See if CPUID instruction is supported ... */
58 /* ... Get copies of EFLAGS into eax and ecx */
63 /* ... Toggle the ID bit in one copy and store */
64 /* to the EFLAGS reg */
65 "xor $0x200000, %0\n\t"
69 /* ... Get the (hopefully modified) EFLAGS */
82 do_cpuid(unsigned int ax
, unsigned int *p
)
87 : "=a" (p
[0]), "=b" (p
[1]), "=c" (p
[2]), "=d" (p
[3])
91 // code from libavcodec:
93 ("mov %%"REG_b
", %%"REG_S
"\n\t"
95 "xchg %%"REG_b
", %%"REG_S
96 : "=a" (p
[0]), "=S" (p
[1]),
97 "=c" (p
[2]), "=d" (p
[3])
103 void GetCpuCaps( CpuCaps
*caps
)
105 unsigned int regs
[4];
106 unsigned int regs2
[4];
108 memset(caps
, 0, sizeof(*caps
));
110 caps
->cl_size
=32; /* default */
112 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"CPUID not supported!??? (maybe an old 486?)\n");
115 do_cpuid(0x00000000, regs
); // get _max_ cpuid level and vendor name
116 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU vendor name: %.4s%.4s%.4s max cpuid level: %d\n",
117 (char*) (regs
+1),(char*) (regs
+3),(char*) (regs
+2), regs
[0]);
118 if (regs
[0]>=0x00000001)
120 char *tmpstr
, *ptmpstr
;
123 do_cpuid(0x00000001, regs2
);
125 caps
->cpuType
=(regs2
[0] >> 8)&0xf;
126 caps
->cpuModel
=(regs2
[0] >> 4)&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 if(caps
->cpuType
==0xf || caps
->cpuType
==6)
135 caps
->cpuModel
|= ((regs2
[0]>>16)&0xf) << 4;
137 caps
->cpuStepping
=regs2
[0] & 0xf;
139 // general feature flags:
140 caps
->hasTSC
= (regs2
[3] & (1 << 8 )) >> 8; // 0x0000010
141 caps
->hasMMX
= (regs2
[3] & (1 << 23 )) >> 23; // 0x0800000
142 caps
->hasSSE
= (regs2
[3] & (1 << 25 )) >> 25; // 0x2000000
143 caps
->hasSSE2
= (regs2
[3] & (1 << 26 )) >> 26; // 0x4000000
144 caps
->hasMMX2
= caps
->hasSSE
; // SSE cpus supports mmxext too
145 cl_size
= ((regs2
[1] >> 8) & 0xFF)*8;
146 if(cl_size
) caps
->cl_size
= cl_size
;
148 ptmpstr
=tmpstr
=GetCpuFriendlyName(regs
, regs2
);
149 while(*ptmpstr
== ' ') // strip leading spaces
151 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: %s ", ptmpstr
);
153 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"(Family: %d, Model: %d, Stepping: %d)\n",
154 caps
->cpuType
, caps
->cpuModel
, caps
->cpuStepping
);
157 do_cpuid(0x80000000, regs
);
158 if (regs
[0]>=0x80000001) {
159 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"extended cpuid-level: %d\n",regs
[0]&0x7FFFFFFF);
160 do_cpuid(0x80000001, regs2
);
161 caps
->hasMMX
|= (regs2
[3] & (1 << 23 )) >> 23; // 0x0800000
162 caps
->hasMMX2
|= (regs2
[3] & (1 << 22 )) >> 22; // 0x400000
163 caps
->has3DNow
= (regs2
[3] & (1 << 31 )) >> 31; //0x80000000
164 caps
->has3DNowExt
= (regs2
[3] & (1 << 30 )) >> 30;
166 if(regs
[0]>=0x80000006)
168 do_cpuid(0x80000006, regs2
);
169 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"extended cache-info: %d\n",regs2
[2]&0x7FFFFFFF);
170 caps
->cl_size
= regs2
[2] & 0xFF;
172 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"Detected cache-line size is %u bytes\n",caps
->cl_size
);
174 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"cpudetect: MMX=%d MMX2=%d SSE=%d SSE2=%d 3DNow=%d 3DNowExt=%d\n",
180 gCpuCaps
.has3DNowExt
);
183 /* FIXME: Does SSE2 need more OS support, too? */
184 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__CYGWIN__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) || defined(__MINGW32__)
186 check_os_katmai_support();
194 // caps->hasMMX2 = 0;
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");
218 if(caps
->has3DNowExt
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"3DNowExt supported but disabled\n");
224 #define CPUID_EXTFAMILY ((regs2[0] >> 20)&0xFF) /* 27..20 */
225 #define CPUID_EXTMODEL ((regs2[0] >> 16)&0x0F) /* 19..16 */
226 #define CPUID_TYPE ((regs2[0] >> 12)&0x04) /* 13..12 */
227 #define CPUID_FAMILY ((regs2[0] >> 8)&0x0F) /* 11..08 */
228 #define CPUID_MODEL ((regs2[0] >> 4)&0x0F) /* 07..04 */
229 #define CPUID_STEPPING ((regs2[0] >> 0)&0x0F) /* 03..00 */
231 char *GetCpuFriendlyName(unsigned int regs
[], unsigned int regs2
[]){
232 #include "cputable.h" /* get cpuname and cpuvendors */
237 if (NULL
==(retname
=malloc(256))) {
238 mp_msg(MSGT_CPUDETECT
,MSGL_FATAL
,"Error: GetCpuFriendlyName() not enough memory\n");
242 sprintf(vendor
,"%.4s%.4s%.4s",(char*)(regs
+1),(char*)(regs
+3),(char*)(regs
+2));
244 do_cpuid(0x80000000,regs
);
245 if (regs
[0] >= 0x80000004)
247 // CPU has built-in namestring
249 for (i
= 0x80000002; i
<= 0x80000004; i
++)
252 strncat(retname
, (char*)regs
, 16);
257 for(i
=0; i
<MAX_VENDORS
; i
++){
258 if(!strcmp(cpuvendors
[i
].string
,vendor
)){
259 if(cpuname
[i
][CPUID_FAMILY
][CPUID_MODEL
]){
260 snprintf(retname
,255,"%s %s",cpuvendors
[i
].name
,cpuname
[i
][CPUID_FAMILY
][CPUID_MODEL
]);
262 snprintf(retname
,255,"unknown %s %d. Generation CPU",cpuvendors
[i
].name
,CPUID_FAMILY
);
263 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"unknown %s CPU:\n",cpuvendors
[i
].name
);
264 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Vendor: %s\n",cpuvendors
[i
].string
);
265 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Type: %d\n",CPUID_TYPE
);
266 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Family: %d (ext: %d)\n",CPUID_FAMILY
,CPUID_EXTFAMILY
);
267 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Model: %d (ext: %d)\n",CPUID_MODEL
,CPUID_EXTMODEL
);
268 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Stepping: %d\n",CPUID_STEPPING
);
269 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Please send the above info along with the exact CPU name"
270 "to the MPlayer-Developers, so we can add it to the list!\n");
276 //printf("Detected CPU: %s\n", retname);
280 #undef CPUID_EXTFAMILY
281 #undef CPUID_EXTMODEL
285 #undef CPUID_STEPPING
288 #if defined(__linux__) && defined(_POSIX_SOURCE) && !defined(ARCH_X86_64)
289 static void sigill_handler_sse( int signal
, struct sigcontext sc
)
291 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "SIGILL, " );
293 /* Both the "xorps %%xmm0,%%xmm0" and "divps %xmm0,%%xmm1"
294 * instructions are 3 bytes long. We must increment the instruction
295 * pointer manually to avoid repeated execution of the offending
298 * If the SIGILL is caused by a divide-by-zero when unmasked
299 * exceptions aren't supported, the SIMD FPU status and control
300 * word will be restored at the end of the test, so we don't need
301 * to worry about doing it here. Besides, we may not be able to...
307 #endif /* __linux__ && _POSIX_SOURCE */
310 LONG CALLBACK
win32_sig_handler_sse(EXCEPTION_POINTERS
* ep
)
312 if(ep
->ExceptionRecord
->ExceptionCode
==EXCEPTION_ILLEGAL_INSTRUCTION
){
313 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "SIGILL, " );
314 ep
->ContextRecord
->Eip
+=3;
316 return EXCEPTION_CONTINUE_EXECUTION
;
318 return EXCEPTION_CONTINUE_SEARCH
;
322 /* If we're running on a processor that can do SSE, let's see if we
323 * are allowed to or not. This will catch 2.4.0 or later kernels that
324 * haven't been configured for a Pentium III but are running on one,
325 * and RedHat patched 2.2 kernels that have broken exception handling
326 * support for user space apps that do SSE.
329 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
330 #define SSE_SYSCTL_NAME "hw.instruction_sse"
331 #elif defined(__APPLE__)
332 #define SSE_SYSCTL_NAME "hw.optional.sse"
335 static void check_os_katmai_support( void )
340 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
342 size_t len
=sizeof(has_sse
);
344 ret
= sysctlbyname(SSE_SYSCTL_NAME
, &has_sse
, &len
, NULL
, 0);
348 #elif defined(__NetBSD__) || defined (__OpenBSD__)
349 #if __NetBSD_Version__ >= 105250000 || (defined __OpenBSD__)
350 int has_sse
, has_sse2
, ret
, mib
[2];
353 mib
[0] = CTL_MACHDEP
;
355 varlen
= sizeof(has_sse
);
357 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
358 ret
= sysctl(mib
, 2, &has_sse
, &varlen
, NULL
, 0);
359 if (ret
< 0 || !has_sse
) {
361 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "no!\n" );
364 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "yes!\n" );
368 varlen
= sizeof(has_sse2
);
369 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE2... " );
370 ret
= sysctl(mib
, 2, &has_sse2
, &varlen
, NULL
, 0);
371 if (ret
< 0 || !has_sse2
) {
373 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "no!\n" );
376 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "yes!\n" );
380 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "No OS support for SSE, disabling to be safe.\n" );
383 LPTOP_LEVEL_EXCEPTION_FILTER exc_fil
;
384 if ( gCpuCaps
.hasSSE
) {
385 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
386 exc_fil
= SetUnhandledExceptionFilter(win32_sig_handler_sse
);
387 __asm
__volatile ("xorps %xmm0, %xmm0");
388 SetUnhandledExceptionFilter(exc_fil
);
389 if ( gCpuCaps
.hasSSE
) mp_msg(MSGT_CPUDETECT
,MSGL_V
, "yes.\n" );
390 else mp_msg(MSGT_CPUDETECT
,MSGL_V
, "no!\n" );
392 #elif defined(__linux__)
393 #if defined(_POSIX_SOURCE)
394 struct sigaction saved_sigill
;
396 /* Save the original signal handlers.
398 sigaction( SIGILL
, NULL
, &saved_sigill
);
400 signal( SIGILL
, (void (*)(int))sigill_handler_sse
);
402 /* Emulate test for OSFXSR in CR4. The OS will set this bit if it
403 * supports the extended FPU save and restore required for SSE. If
404 * we execute an SSE instruction on a PIII and get a SIGILL, the OS
405 * doesn't support Streaming SIMD Exceptions, even if the processor
408 if ( gCpuCaps
.hasSSE
) {
409 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
411 // __asm __volatile ("xorps %%xmm0, %%xmm0");
412 __asm
__volatile ("xorps %xmm0, %xmm0");
414 if ( gCpuCaps
.hasSSE
) {
415 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "yes.\n" );
417 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "no!\n" );
421 /* Restore the original signal handlers.
423 sigaction( SIGILL
, &saved_sigill
, NULL
);
425 /* If we've gotten to here and the XMM CPUID bit is still set, we're
426 * safe to go ahead and hook out the SSE code throughout Mesa.
428 if ( gCpuCaps
.hasSSE
) {
429 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Tests of OS support for SSE passed.\n" );
431 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Tests of OS support for SSE failed!\n" );
434 /* We can't use POSIX signal handling to test the availability of
435 * SSE, so we disable it by default.
437 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "Cannot test OS support for SSE, disabling to be safe.\n" );
439 #endif /* _POSIX_SOURCE */
441 /* Do nothing on other platforms for now.
443 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "Cannot test OS support for SSE, leaving disabled.\n" );
445 #endif /* __linux__ */
450 #include <sys/sysctl.h>
456 static sigjmp_buf jmpbuf
;
457 static volatile sig_atomic_t canjump
= 0;
459 static void sigill_handler (int sig
)
462 signal (sig
, SIG_DFL
);
467 siglongjmp (jmpbuf
, 1);
469 #endif //__AMIGAOS4__
472 void GetCpuCaps( CpuCaps
*caps
)
484 caps
->hasAltiVec
= 0;
488 rip-off from ffmpeg altivec detection code.
489 this code also appears on Apple's AltiVec pages.
492 int sels
[2] = {CTL_HW
, HW_VECTORUNIT
};
494 size_t len
= sizeof(has_vu
);
497 err
= sysctl(sels
, 2, &has_vu
, &len
, NULL
, 0);
501 caps
->hasAltiVec
= 1;
503 #else /* SYS_DARWIN */
507 GetCPUInfoTags(GCIT_VectorUnit
, &result
, TAG_DONE
);
508 if (result
== VECTORTYPE_ALTIVEC
)
509 caps
->hasAltiVec
= 1;
511 /* no Darwin, do it the brute-force way */
512 /* this is borrowed from the libmpeg2 library */
514 signal (SIGILL
, sigill_handler
);
515 if (sigsetjmp (jmpbuf
, 1)) {
516 signal (SIGILL
, SIG_DFL
);
520 asm volatile ("mtspr 256, %0\n\t"
521 "vand %%v0, %%v0, %%v0"
525 signal (SIGILL
, SIG_DFL
);
526 caps
->hasAltiVec
= 1;
529 #endif //__AMIGAOS4__
530 #endif /* SYS_DARWIN */
531 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"AltiVec %sfound\n", (caps
->hasAltiVec
? "" : "not "));
532 #endif /* HAVE_ALTIVEC */
535 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Intel Itanium\n");
539 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Sun Sparc\n");
543 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: ARM\n");
547 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: PowerPC\n");
551 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Digital Alpha\n");
555 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: SGI MIPS\n");
559 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Hewlett-Packard PA-RISC\n");
563 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: IBM S/390\n");
567 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: IBM S/390X\n");
571 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
, "CPU: Digital VAX\n" );
574 #endif /* !ARCH_X86 */