]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
Check plugins directory names for unsupported capability
[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 #include <vlc_cpu.h>
35
36 #include <sys/types.h>
37 #ifndef WIN32
38 #include <unistd.h>
39 #include <sys/wait.h>
40 #endif
41
42 #include "libvlc.h"
43
44 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
45 #include <sys/sysctl.h>
46 #endif
47
48 #if defined( __i386__ ) || defined( __x86_64__ ) || defined( __powerpc__ ) \
49  || defined( __ppc__ ) || defined( __ppc64__ ) || defined( __powerpc64__ )
50 static bool check_OS_capability( const char *psz_capability, pid_t pid )
51 {
52 #ifndef WIN32
53     int status;
54
55     if( pid == -1 )
56         return false; /* fail safe :-/ */
57
58     while( waitpid( pid, &status, 0 ) == -1 );
59
60     if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
61         return true;
62
63     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
64                      "operating system.\n", psz_capability );
65     fprintf( stderr, "         some optimizations will be disabled unless "
66                      "you upgrade your OS\n" );
67     return false;
68 #else
69 # warning FIXME!
70 # define fork() (errno = ENOSYS, -1)
71     (void)pid;
72     (void)psz_capability;
73     return true;
74 #endif
75 }
76 #endif
77
78 /*****************************************************************************
79  * CPUCapabilities: get the CPU capabilities
80  *****************************************************************************
81  * This function is called to list extensions the CPU may have.
82  *****************************************************************************/
83 uint32_t CPUCapabilities( void )
84 {
85     uint32_t i_capabilities = 0;
86
87 #if defined( __i386__ ) || defined( __x86_64__ )
88      unsigned int i_eax, i_ebx, i_ecx, i_edx;
89      bool b_amd;
90
91     /* Needed for x86 CPU capabilities detection */
92 #   if defined( __x86_64__ )
93 #       define cpuid( reg )                    \
94             asm volatile ( "cpuid\n\t"         \
95                            "movl %%ebx,%1\n\t" \
96                          : "=a" ( i_eax ),     \
97                            "=b" ( i_ebx ),     \
98                            "=c" ( i_ecx ),     \
99                            "=d" ( i_edx )      \
100                          : "a"  ( reg )        \
101                          : "cc" );
102 #   else
103 #       define cpuid( reg )                    \
104             asm volatile ( "push %%ebx\n\t"    \
105                            "cpuid\n\t"         \
106                            "movl %%ebx,%1\n\t" \
107                            "pop %%ebx\n\t"     \
108                          : "=a" ( i_eax ),     \
109                            "=r" ( i_ebx ),     \
110                            "=c" ( i_ecx ),     \
111                            "=d" ( i_edx )      \
112                          : "a"  ( reg )        \
113                          : "cc" );
114 #   endif
115      /* Check if the OS really supports the requested instructions */
116 #   define check_capability(name, flag, code)  \
117      do {                                      \
118         pid_t pid = fork();                    \
119         if( pid == 0 )                         \
120         {                                      \
121             __asm__ __volatile__ ( code : : ); \
122             _exit(0);                           \
123         }                                      \
124         if( check_OS_capability((name), pid )) \
125             i_capabilities |= (flag);          \
126      } while(0)
127
128 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
129   && !defined (__i686__) && !defined (__pentium4__) \
130   && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
131     /* check if cpuid instruction is supported */
132     asm volatile ( "push %%ebx\n\t"
133                    "pushf\n\t"
134                    "pop %%eax\n\t"
135                    "movl %%eax, %%ebx\n\t"
136                    "xorl $0x200000, %%eax\n\t"
137                    "push %%eax\n\t"
138                    "popf\n\t"
139                    "pushf\n\t"
140                    "pop %%eax\n\t"
141                    "movl %%ebx,%1\n\t"
142                    "pop %%ebx\n\t"
143                  : "=a" ( i_eax ),
144                    "=r" ( i_ebx )
145                  :
146                  : "cc" );
147
148     if( i_eax == i_ebx )
149         goto out;
150 # endif
151
152     /* the CPU supports the CPUID instruction - get its level */
153     cpuid( 0x00000000 );
154
155 # if defined (__i386__) && !defined (__i586__) \
156   && !defined (__i686__) && !defined (__pentium4__) \
157   && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
158     if( !i_eax )
159         goto out;
160 #endif
161
162     /* borrowed from mpeg2dec */
163     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
164                     && ( i_edx == 0x69746e65 );
165
166     /* test for the MMX flag */
167     cpuid( 0x00000001 );
168 # if !defined (__MMX__)
169     if( ! (i_edx & 0x00800000) )
170         goto out;
171 # endif
172     i_capabilities |= CPU_CAPABILITY_MMX;
173
174 # if defined (__SSE__)
175     i_capabilities |= CPU_CAPABILITY_MMXEXT | CPU_CAPABILITY_SSE;
176 # else
177     if( i_edx & 0x02000000 )
178     {
179         i_capabilities |= CPU_CAPABILITY_MMXEXT;
180
181 #   ifdef CAN_COMPILE_SSE
182         check_capability( "SSE", CPU_CAPABILITY_SSE,
183                           "xorps %%xmm0,%%xmm0\n" );
184 #   endif
185     }
186 # endif
187
188 # if defined (__SSE2__)
189     i_capabilities |= CPU_CAPABILITY_SSE2;
190 # elif defined (CAN_COMPILE_SSE2)
191     if( i_edx & 0x04000000 )
192         check_capability( "SSE2", CPU_CAPABILITY_SSE2,
193                           "movupd %%xmm0, %%xmm0\n" );
194 # endif
195
196 # if defined (__SSE3__)
197     i_capabilities |= CPU_CAPABILITY_SSE3;
198 # elif defined (CAN_COMPILE_SSE3)
199     if( i_ecx & 0x00000001 )
200         check_capability( "SSE3", CPU_CAPABILITY_SSE3,
201                           "movsldup %%xmm1, %%xmm0\n" );
202 # endif
203
204 # if defined (__SSSE3__)
205     i_capabilities |= CPU_CAPABILITY_SSSE3;
206 # elif defined (CAN_COMPILE_SSSE3)
207     if( i_ecx & 0x00000200 )
208         check_capability( "SSSE3", CPU_CAPABILITY_SSSE3,
209                           "pabsw %%xmm1, %%xmm0\n" );
210 # endif
211
212 # if defined (__SSE4_1__)
213     i_capabilities |= CPU_CAPABILITY_SSE4_1;
214 # elif defined (CAN_COMPILE_SSE4_1)
215     if( i_ecx & 0x00080000 )
216         check_capability( "SSE4.1", CPU_CAPABILITY_SSE4_1,
217                           "pmaxsb %%xmm1, %%xmm0\n" );
218 # endif
219
220 # if defined (__SSE4_2__)
221     i_capabilities |= CPU_CAPABILITY_SSE4_2;
222 # elif defined (CAN_COMPILE_SSE4_2)
223     if( i_ecx & 0x00100000 )
224         check_capability( "SSE4.2", CPU_CAPABILITY_SSE4_2,
225                           "pcmpgtq %%xmm1, %%xmm0\n" );
226 # endif
227
228     /* test for additional capabilities */
229     cpuid( 0x80000000 );
230
231     if( i_eax < 0x80000001 )
232         goto out;
233
234     /* list these additional capabilities */
235     cpuid( 0x80000001 );
236
237 # if defined (__3dNOW__)
238     i_capabilities |= CPU_CAPABILITY_3DNOW;
239 # elif defined (CAN_COMPILE_3DNOW)
240     if( i_edx & 0x80000000 )
241         check_capability( "3D Now!", CPU_CAPABILITY_3DNOW,
242                           "pfadd %%mm0,%%mm0\n" "femms\n" );
243 # endif
244
245     if( b_amd && ( i_edx & 0x00400000 ) )
246     {
247         i_capabilities |= CPU_CAPABILITY_MMXEXT;
248     }
249 out:
250
251 #elif defined( __arm__ )
252 #   if defined( __ARM_NEON__ )
253     i_capabilities |= CPU_CAPABILITY_NEON;
254 #   endif
255
256 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
257     || defined( __ppc64__ )
258
259 #   if defined(__APPLE__)
260     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
261     int i_has_altivec = 0;
262     size_t i_length = sizeof( i_has_altivec );
263     int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
264
265     if( i_error == 0 && i_has_altivec != 0 )
266         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
267
268 #   elif defined( CAN_COMPILE_ALTIVEC )
269     pid_t pid = fork();
270     if( pid == 0 )
271     {
272         asm volatile ("mtspr 256, %0\n\t"
273                       "vand %%v0, %%v0, %%v0"
274                       :
275                       : "r" (-1));
276         _exit(0);
277     }
278
279     if( check_OS_capability( "Altivec", pid ) )
280         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
281
282 #   endif
283
284 #endif
285     return i_capabilities;
286 }
287
288 uint32_t cpu_flags = 0;
289
290
291 /*****************************************************************************
292  * vlc_CPU: get pre-computed CPU capability flags
293  ****************************************************************************/
294 unsigned vlc_CPU (void)
295 {
296     return cpu_flags;
297 }
298
299 const struct
300 {
301     uint32_t value;
302     char name[12];
303 } cap_dirs[] = {
304 #if defined ( __i386__ ) || defined ( __x86_64__ )
305     { CPU_CAPABILITY_MMX,     "mmx" },
306     { CPU_CAPABILITY_MMXEXT,  "mmxext" },
307     { CPU_CAPABILITY_3DNOW,   "3dnow" },
308     { CPU_CAPABILITY_SSE,     "sse" },
309 #endif
310 #if defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
311     { CPU_CAPABILITY_ALTIVEC, "altivec" },
312 #endif
313 #if defined (__arm__)
314     { CPU_CAPABILITY_NEON,    "arm_neon" },
315 #endif
316 };
317
318 /**
319  * Check if a directory name contains usable plugins w.r.t. the hardware
320  * capabilities. Loading a plugin when the hardware has insufficient
321  * capabilities may lead to illegal instructions (SIGILL) and must be avoided.
322  *
323  * @param name the name of the directory (<b>not</b> the path)
324  *
325  * @return true if the hardware has sufficient capabilities or the directory
326  * does not require any special capability; false if the running hardware has
327  * insufficient capabilities.
328  */
329 bool vlc_CPU_CheckPluginDir (const char *name)
330 {
331     const unsigned flags = vlc_CPU ();
332     for (size_t i = 0; i < sizeof (cap_dirs) / sizeof (cap_dirs[0]); i++)
333     {
334         if (strcmp (name, cap_dirs[i].name))
335             continue;
336         return (flags & cap_dirs[i].value) != 0;
337     }
338     return true;
339 }
340
341 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
342 static vlc_memset_t pf_vlc_memset = memset;
343
344 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
345 {
346     if (cpy)
347         pf_vlc_memcpy = cpy;
348     if (set)
349         pf_vlc_memset = set;
350 }
351
352 /**
353  * vlc_memcpy: fast CPU-dependent memcpy
354  */
355 void *vlc_memcpy (void *tgt, const void *src, size_t n)
356 {
357     return pf_vlc_memcpy (tgt, src, n);
358 }
359
360 /**
361  * vlc_memset: fast CPU-dependent memset
362  */
363 void *vlc_memset (void *tgt, int c, size_t n)
364 {
365     return pf_vlc_memset (tgt, c, n);
366 }