]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
CPU: fprintf is not safe inside a signal handler...
[vlc] / src / misc / cpu.c
1 /*****************************************************************************
2  * cpu.c: CPU detection code
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Eugenio Jarosiewicz <ej0@cise.ufl.eduEujenio>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34
35 #include <signal.h>                            /* SIGHUP, SIGINT, SIGKILL */
36 #include <setjmp.h>                                    /* longjmp, setjmp */
37
38 #include "libvlc.h"
39
40 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
41 #include <sys/sysctl.h>
42 #endif
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static void SigHandler   ( int );
48
49 /*****************************************************************************
50  * Global variables - they're needed for signal handling
51  *****************************************************************************/
52 static jmp_buf env;
53 static int     i_illegal;
54
55 #if defined( __i386__ ) || defined( __x86_64__ )
56 static void warn_cap( const char *psz_capability )
57 {
58     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
59                      "operating system.\n", psz_capability );
60     fprintf( stderr, "         some optimizations will be disabled unless "
61                      "you upgrade your OS\n" );
62 }
63 #endif
64
65 /*****************************************************************************
66  * CPUCapabilities: get the CPU capabilities
67  *****************************************************************************
68  * This function is called to list extensions the CPU may have.
69  *****************************************************************************/
70 uint32_t CPUCapabilities( void )
71 {
72     volatile uint32_t i_capabilities = CPU_CAPABILITY_NONE;
73
74 #if defined( __i386__ ) || defined( __x86_64__ )
75     volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
76     volatile bool    b_amd;
77
78     /* Needed for x86 CPU capabilities detection */
79 #   if defined( __x86_64__ )
80 #       define cpuid( reg )                    \
81             asm volatile ( "cpuid\n\t"         \
82                            "movl %%ebx,%1\n\t" \
83                          : "=a" ( i_eax ),     \
84                            "=b" ( i_ebx ),     \
85                            "=c" ( i_ecx ),     \
86                            "=d" ( i_edx )      \
87                          : "a"  ( reg )        \
88                          : "cc" );
89 #   else
90 #       define cpuid( reg )                    \
91             asm volatile ( "push %%ebx\n\t"    \
92                            "cpuid\n\t"         \
93                            "movl %%ebx,%1\n\t" \
94                            "pop %%ebx\n\t"     \
95                          : "=a" ( i_eax ),     \
96                            "=r" ( i_ebx ),     \
97                            "=c" ( i_ecx ),     \
98                            "=d" ( i_edx )      \
99                          : "a"  ( reg )        \
100                          : "cc" );
101 #   endif
102
103 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
104     void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
105 #   endif
106
107     i_capabilities |= CPU_CAPABILITY_FPU;
108
109 #   if defined( __i386__ )
110     /* check if cpuid instruction is supported */
111     asm volatile ( "push %%ebx\n\t"
112                    "pushf\n\t"
113                    "pop %%eax\n\t"
114                    "movl %%eax, %%ebx\n\t"
115                    "xorl $0x200000, %%eax\n\t"
116                    "push %%eax\n\t"
117                    "popf\n\t"
118                    "pushf\n\t"
119                    "pop %%eax\n\t"
120                    "movl %%ebx,%1\n\t"
121                    "pop %%ebx\n\t"
122                  : "=a" ( i_eax ),
123                    "=r" ( i_ebx )
124                  :
125                  : "cc" );
126
127     if( i_eax == i_ebx )
128         goto out;
129 #   else
130     /* x86_64 supports cpuid instruction, so we dont need to check it */
131 #   endif
132
133     i_capabilities |= CPU_CAPABILITY_486;
134
135     /* the CPU supports the CPUID instruction - get its level */
136     cpuid( 0x00000000 );
137
138     if( !i_eax )
139         goto out;
140
141     /* FIXME: this isn't correct, since some 486s have cpuid */
142     i_capabilities |= CPU_CAPABILITY_586;
143
144     /* borrowed from mpeg2dec */
145     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
146                     && ( i_edx == 0x69746e65 );
147
148     /* test for the MMX flag */
149     cpuid( 0x00000001 );
150
151     if( ! (i_edx & 0x00800000) )
152         goto out;
153
154     i_capabilities |= CPU_CAPABILITY_MMX;
155
156     if( i_edx & 0x02000000 )
157     {
158         i_capabilities |= CPU_CAPABILITY_MMXEXT;
159
160 #   ifdef CAN_COMPILE_SSE
161         /* We test if OS supports the SSE instructions */
162         i_illegal = 0;
163
164         if( setjmp( env ) == 0 )
165         {
166             /* Test a SSE instruction */
167             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
168         }
169
170         if( i_illegal == 0 )
171             i_capabilities |= CPU_CAPABILITY_SSE;
172         else
173             warn_cap( "SSE" );
174 #   endif
175     }
176
177     if( i_edx & 0x04000000 )
178     {
179 #   if defined(CAN_COMPILE_SSE)
180         /* We test if OS supports the SSE instructions */
181         i_illegal = 0;
182
183         if( setjmp( env ) == 0 )
184         {
185             /* Test a SSE2 instruction */
186             __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
187         }
188
189         if( i_illegal == 0 )
190             i_capabilities |= CPU_CAPABILITY_SSE2;
191         else
192             warn_cap( "SSE2" );
193 #   endif
194     }
195
196     /* test for additional capabilities */
197     cpuid( 0x80000000 );
198
199     if( i_eax < 0x80000001 )
200         goto out;
201
202     /* list these additional capabilities */
203     cpuid( 0x80000001 );
204
205 #   ifdef CAN_COMPILE_3DNOW
206     if( i_edx & 0x80000000 )
207     {
208         i_illegal = 0;
209
210         if( setjmp( env ) == 0 )
211         {
212             /* Test a 3D Now! instruction */
213             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
214         }
215
216         if( i_illegal == 0 )
217             i_capabilities |= CPU_CAPABILITY_3DNOW;
218         else
219             warn_cap( "3D Now!" );
220     }
221 #   endif
222
223     if( b_amd && ( i_edx & 0x00400000 ) )
224     {
225         i_capabilities |= CPU_CAPABILITY_MMXEXT;
226     }
227
228 out:
229 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
230     signal( SIGILL, pf_sigill );
231 #   endif
232
233 #elif defined( __arm__ )
234 #   if defined( __ARM_EABI__ ) && !defined( __SOFTFP__ )
235     i_capabilities |= CPU_CAPABILITY_FPU;
236 #   endif
237
238 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
239
240     i_capabilities |= CPU_CAPABILITY_FPU;
241
242 #   if defined(__APPLE__)
243     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
244     int i_has_altivec = 0;
245     size_t i_length = sizeof( i_has_altivec );
246     int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
247
248     if( i_error == 0 && i_has_altivec != 0 )
249         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
250
251 #   elif defined( CAN_COMPILE_ALTIVEC )
252     void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
253
254     i_illegal = 0;
255
256     if( setjmp( env ) == 0 )
257     {
258         asm volatile ("mtspr 256, %0\n\t"
259                       "vand %%v0, %%v0, %%v0"
260                       :
261                       : "r" (-1));
262     }
263
264     if( i_illegal == 0 )
265         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
266
267     signal( SIGILL, pf_sigill );
268 #   else
269     (void)SigHandler; /* Don't complain about dead code here */
270 #   endif
271
272 #elif defined( __sparc__ )
273     i_capabilities |= CPU_CAPABILITY_FPU;
274
275 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
276     i_capabilities |= CPU_CAPABILITY_FPU;
277
278 #endif
279     return i_capabilities;
280 }
281
282 /*****************************************************************************
283  * SigHandler: system signal handler
284  *****************************************************************************
285  * This function is called when an illegal instruction signal is received by
286  * the program. We use this function to test OS and CPU capabilities
287  *****************************************************************************/
288 static void SigHandler( int i_signal )
289 {
290     /* Acknowledge the signal received */
291     i_illegal = 1;
292
293 #ifdef HAVE_SIGRELSE
294     sigrelse( i_signal );
295 #else
296     VLC_UNUSED( i_signal );
297 #endif
298     longjmp( env, 1 );
299 }
300
301
302 uint32_t cpu_flags = 0;
303
304
305 /*****************************************************************************
306  * vlc_CPU: get pre-computed CPU capability flags
307  ****************************************************************************/
308 unsigned vlc_CPU (void)
309 {
310     return cpu_flags;
311 }
312
313 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
314 static vlc_memset_t pf_vlc_memset = memset;
315
316 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
317 {
318     if (cpy)
319         pf_vlc_memcpy = cpy;
320     if (set)
321         pf_vlc_memset = set;
322 }
323
324 /**
325  * vlc_memcpy: fast CPU-dependent memcpy
326  */
327 void *vlc_memcpy (void *tgt, const void *src, size_t n)
328 {
329     return pf_vlc_memcpy (tgt, src, n);
330 }
331
332 /**
333  * vlc_memset: fast CPU-dependent memset
334  */
335 void *vlc_memset (void *tgt, int c, size_t n)
336 {
337     return pf_vlc_memset (tgt, c, n);
338 }