Some new Spanish translations and improvements to other existing
[wine/multimedia.git] / misc / cpu.c
blobea694a58828306c65e96d01afd1de4865b77dd71
1 /*
2 * What processor?
4 * Copyright 1995,1997 Morten Welinder
5 * Copyright 1997-1998 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #ifdef HAVE_SYS_PARAM_H
26 # include <sys/param.h>
27 #endif
28 #ifdef HAVE_SYS_SYSCTL_H
29 # include <sys/sysctl.h>
30 #endif
31 #ifdef HAVE_MACHINE_CPU_H
32 # include <machine/cpu.h>
33 #endif
35 #include <ctype.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
41 #endif
44 #define NONAMELESSUNION
45 #define NONAMELESSSTRUCT
46 #include "winbase.h"
47 #include "winnt.h"
48 #include "winternl.h"
49 #include "winerror.h"
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(reg);
55 #define AUTH 0x68747541 /* "Auth" */
56 #define ENTI 0x69746e65 /* "enti" */
57 #define CAMD 0x444d4163 /* "cAMD" */
59 /* Calls cpuid with an eax of 'ax' and returns the 16 bytes in *p
60 * We are compiled with -fPIC, so we can't clobber ebx.
62 static inline void do_cpuid(int ax, int *p)
64 #ifdef __i386__
65 __asm__("pushl %%ebx\n\t"
66 "cpuid\n\t"
67 "movl %%ebx, %%esi\n\t"
68 "popl %%ebx"
69 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
70 : "0" (ax));
71 #endif
74 /* From xf86info havecpuid.c 1.11 */
75 static inline int have_cpuid(void)
77 #ifdef __i386__
78 unsigned int f1, f2;
79 __asm__("pushfl\n\t"
80 "pushfl\n\t"
81 "popl %0\n\t"
82 "movl %0,%1\n\t"
83 "xorl %2,%0\n\t"
84 "pushl %0\n\t"
85 "popfl\n\t"
86 "pushfl\n\t"
87 "popl %0\n\t"
88 "popfl"
89 : "=&r" (f1), "=&r" (f2)
90 : "ir" (0x00200000));
91 return ((f1^f2) & 0x00200000) != 0;
92 #else
93 return 0;
94 #endif
97 static BYTE PF[64] = {0,};
98 static ULONGLONG cpuHz = 1000000000; /* default to a 1GHz */
100 static void create_registry_keys( const SYSTEM_INFO *info )
102 static const WCHAR SystemW[] = {'M','a','c','h','i','n','e','\\',
103 'H','a','r','d','w','a','r','e','\\',
104 'D','e','s','c','r','i','p','t','i','o','n','\\',
105 'S','y','s','t','e','m',0};
106 static const WCHAR fpuW[] = {'F','l','o','a','t','i','n','g','P','o','i','n','t','P','r','o','c','e','s','s','o','r',0};
107 static const WCHAR cpuW[] = {'C','e','n','t','r','a','l','P','r','o','c','e','s','s','o','r',0};
108 static const WCHAR IdentifierW[] = {'I','d','e','n','t','i','f','i','e','r',0};
109 static const WCHAR SysidW[] = {'A','T',' ','c','o','m','p','a','t','i','b','l','e',0};
110 static const WCHAR mhzKeyW[] = {'~','M','H','z',0};
112 int i;
113 HKEY hkey, system_key, cpu_key;
114 OBJECT_ATTRIBUTES attr;
115 UNICODE_STRING nameW, valueW;
117 attr.Length = sizeof(attr);
118 attr.RootDirectory = 0;
119 attr.ObjectName = &nameW;
120 attr.Attributes = 0;
121 attr.SecurityDescriptor = NULL;
122 attr.SecurityQualityOfService = NULL;
124 RtlInitUnicodeString( &nameW, SystemW );
125 if (NtCreateKey( &system_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
127 RtlInitUnicodeString( &valueW, IdentifierW );
128 NtSetValueKey( system_key, &valueW, 0, REG_SZ, SysidW, (strlenW(SysidW)+1) * sizeof(WCHAR) );
130 attr.RootDirectory = system_key;
131 RtlInitUnicodeString( &nameW, fpuW );
132 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
134 RtlInitUnicodeString( &nameW, cpuW );
135 if (!NtCreateKey( &cpu_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
137 for (i = 0; i < info->dwNumberOfProcessors; i++)
139 char num[10], id[20];
141 attr.RootDirectory = cpu_key;
142 sprintf( num, "%d", i );
143 RtlCreateUnicodeStringFromAsciiz( &nameW, num );
144 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
146 WCHAR idW[20];
147 DWORD cpuMHz = cpuHz / 1000000;
149 sprintf( id, "CPU %ld", info->dwProcessorType );
150 RtlMultiByteToUnicodeN( idW, sizeof(idW), NULL, id, strlen(id)+1 );
151 NtSetValueKey( hkey, &valueW, 0, REG_SZ, idW, (strlenW(idW)+1)*sizeof(WCHAR) );
153 RtlInitUnicodeString( &valueW, mhzKeyW );
154 NtSetValueKey( hkey, &valueW, 0, REG_DWORD, &cpuMHz, sizeof(DWORD) );
155 NtClose( hkey );
157 RtlFreeUnicodeString( &nameW );
159 NtClose( cpu_key );
161 NtClose( system_key );
164 /****************************************************************************
165 * QueryPerformanceCounter (KERNEL32.@)
167 BOOL WINAPI QueryPerformanceCounter(PLARGE_INTEGER counter)
169 struct timeval tv;
171 #if defined(__i386__) && defined(__GNUC__)
172 if (IsProcessorFeaturePresent( PF_RDTSC_INSTRUCTION_AVAILABLE )) {
173 /* i586 optimized version */
174 __asm__ __volatile__ ( "rdtsc"
175 : "=a" (counter->s.LowPart), "=d" (counter->s.HighPart) );
176 counter->QuadPart = counter->QuadPart / 1000; /* see below */
177 return TRUE;
179 #endif
181 /* fall back to generic routine (ie, for i386, i486) */
182 gettimeofday( &tv, NULL );
183 counter->QuadPart = (LONGLONG)tv.tv_usec + (LONGLONG)tv.tv_sec * 1000000;
184 return TRUE;
188 /****************************************************************************
189 * QueryPerformanceFrequency (KERNEL32.@)
191 BOOL WINAPI QueryPerformanceFrequency(PLARGE_INTEGER frequency)
193 #if defined(__i386__) && defined(__GNUC__)
194 if (IsProcessorFeaturePresent( PF_RDTSC_INSTRUCTION_AVAILABLE )) {
195 /* The way Windows calculates this value is unclear, however simply using the CPU frequency
196 gives a value out by approximately a thousand. That can cause some applications to crash,
197 so we divide here to make our number more similar to the one Windows gives */
198 frequency->QuadPart = cpuHz / 1000;
199 return TRUE;
201 #endif
202 frequency->s.LowPart = 1000000;
203 frequency->s.HighPart = 0;
204 return TRUE;
208 /***********************************************************************
209 * GetSystemInfo [KERNEL32.@]
211 * Gets the current system information.
213 * On the first call it creates cached values, so it doesn't have to determine
214 * them repeatedly. On Linux, the /proc/cpuinfo special file is used.
216 * It creates a registry subhierarchy, looking like:
217 * \HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\
218 * Identifier (CPU x86)
219 * Note that there is a hierarchy for every processor installed, so this
220 * supports multiprocessor systems. This is done like Win95 does it, I think.
222 * It also creates a cached flag array for IsProcessorFeaturePresent().
224 * No NULL ptr check for LPSYSTEM_INFO in Win9x.
226 * RETURNS
227 * nothing, really
229 VOID WINAPI GetSystemInfo(
230 LPSYSTEM_INFO si /* [out] system information */
232 static int cache = 0;
233 static SYSTEM_INFO cachedsi;
235 TRACE("si=0x%p\n", si);
236 if (cache) {
237 memcpy(si,&cachedsi,sizeof(*si));
238 return;
240 memset(PF,0,sizeof(PF));
242 /* choose sensible defaults ...
243 * FIXME: perhaps overrideable with precompiler flags?
245 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
246 cachedsi.dwPageSize = getpagesize();
248 /* FIXME: the two entries below should be computed somehow... */
249 cachedsi.lpMinimumApplicationAddress = (void *)0x00010000;
250 cachedsi.lpMaximumApplicationAddress = (void *)0x7FFFFFFF;
251 cachedsi.dwActiveProcessorMask = 1;
252 cachedsi.dwNumberOfProcessors = 1;
253 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
254 cachedsi.dwAllocationGranularity = 0x10000;
255 cachedsi.wProcessorLevel = 5; /* 586 */
256 cachedsi.wProcessorRevision = 0;
258 cache = 1; /* even if there is no more info, we now have a cacheentry */
259 memcpy(si,&cachedsi,sizeof(*si));
261 /* Hmm, reasonable processor feature defaults? */
263 #ifdef linux
265 char line[200];
266 FILE *f = fopen ("/proc/cpuinfo", "r");
268 if (!f)
269 return;
270 while (fgets(line,200,f)!=NULL) {
271 char *s,*value;
273 /* NOTE: the ':' is the only character we can rely on */
274 if (!(value = strchr(line,':')))
275 continue;
277 /* terminate the valuename */
278 s = value - 1;
279 while ((s >= line) && ((*s == ' ') || (*s == '\t'))) s--;
280 *(s + 1) = '\0';
282 /* and strip leading spaces from value */
283 value += 1;
284 while (*value==' ') value++;
285 if ((s=strchr(value,'\n')))
286 *s='\0';
288 /* 2.1 method */
289 if (!strcasecmp(line, "cpu family")) {
290 if (isdigit (value[0])) {
291 switch (value[0] - '0') {
292 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
293 cachedsi.wProcessorLevel= 3;
294 break;
295 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
296 cachedsi.wProcessorLevel= 4;
297 break;
298 case 5:
299 case 6: /* PPro/2/3 has same info as P1 */
300 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
301 cachedsi.wProcessorLevel= 5;
302 break;
303 case 1: /* two-figure levels */
304 if (value[1] == '5')
306 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
307 cachedsi.wProcessorLevel= 5;
308 break;
310 /* fall through */
311 default:
312 FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value);
313 break;
316 continue;
318 /* old 2.0 method */
319 if (!strcasecmp(line, "cpu")) {
320 if ( isdigit (value[0]) && value[1] == '8' &&
321 value[2] == '6' && value[3] == 0
323 switch (value[0] - '0') {
324 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
325 cachedsi.wProcessorLevel= 3;
326 break;
327 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
328 cachedsi.wProcessorLevel= 4;
329 break;
330 case 5:
331 case 6: /* PPro/2/3 has same info as P1 */
332 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
333 cachedsi.wProcessorLevel= 5;
334 break;
335 default:
336 FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
337 break;
340 continue;
342 if (!strcasecmp(line,"fdiv_bug")) {
343 if (!strncasecmp(value,"yes",3))
344 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
346 continue;
348 if (!strcasecmp(line,"fpu")) {
349 if (!strncasecmp(value,"no",2))
350 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
352 continue;
354 if (!strcasecmp(line,"processor")) {
355 /* processor number counts up... */
356 unsigned int x;
358 if (sscanf(value,"%d",&x))
359 if (x+1>cachedsi.dwNumberOfProcessors)
360 cachedsi.dwNumberOfProcessors=x+1;
362 continue;
364 if (!strcasecmp(line,"stepping")) {
365 int x;
367 if (sscanf(value,"%d",&x))
368 cachedsi.wProcessorRevision = x;
370 continue;
372 if (!strcasecmp(line, "cpu MHz")) {
373 double cmz;
374 if (sscanf( value, "%lf", &cmz ) == 1) {
375 /* SYSTEMINFO doesn't have a slot for cpu speed, so store in a global */
376 cpuHz = cmz * 1000 * 1000;
377 TRACE("CPU speed read as %lld\n", cpuHz);
379 continue;
381 if ( !strcasecmp(line,"flags") ||
382 !strcasecmp(line,"features")
384 if (strstr(value,"cx8"))
385 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
386 if (strstr(value,"mmx"))
387 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
388 if (strstr(value,"tsc"))
389 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
390 if (strstr(value,"3dnow"))
391 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
392 /* This will also catch sse2, but we have sse itself
393 * if we have sse2, so no problem */
394 if (strstr(value,"sse"))
395 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
396 if (strstr(value,"sse2"))
397 PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
398 if (strstr(value,"pae"))
399 PF[PF_PAE_ENABLED] = TRUE;
401 continue;
404 fclose (f);
406 memcpy(si,&cachedsi,sizeof(*si));
407 #elif defined (__NetBSD__)
409 int mib[2];
410 int value[2];
411 char model[256];
412 char *cpuclass;
413 FILE *f = fopen ("/var/run/dmesg.boot", "r");
415 /* first deduce as much as possible from the sysctls */
416 mib[0] = CTL_MACHDEP;
417 #ifdef CPU_FPU_PRESENT
418 mib[1] = CPU_FPU_PRESENT;
419 value[1] = sizeof(int);
420 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
421 if (value) PF[PF_FLOATING_POINT_EMULATED] = FALSE;
422 else PF[PF_FLOATING_POINT_EMULATED] = TRUE;
423 #endif
424 #ifdef CPU_SSE
425 mib[1] = CPU_SSE; /* this should imply MMX */
426 value[1] = sizeof(int);
427 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
428 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
429 #endif
430 #ifdef CPU_SSE2
431 mib[1] = CPU_SSE2; /* this should imply MMX */
432 value[1] = sizeof(int);
433 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
434 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
435 #endif
436 mib[0] = CTL_HW;
437 mib[1] = HW_NCPU;
438 value[1] = sizeof(int);
439 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
440 if (value[0] > cachedsi.dwNumberOfProcessors)
441 cachedsi.dwNumberOfProcessors = value[0];
442 mib[1] = HW_MODEL;
443 value[1] = 255;
444 if (sysctl(mib, 2, model, value+1, NULL, 0) >= 0) {
445 model[value[1]] = '\0'; /* just in case */
446 cpuclass = strstr(model, "-class");
447 if (cpuclass != NULL) {
448 while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
449 if (!strncmp(cpuclass+1, "386", 3)) {
450 cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
451 cachedsi.wProcessorLevel= 3;
453 if (!strncmp(cpuclass+1, "486", 3)) {
454 cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
455 cachedsi.wProcessorLevel= 4;
457 if (!strncmp(cpuclass+1, "586", 3)) {
458 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
459 cachedsi.wProcessorLevel= 5;
461 if (!strncmp(cpuclass+1, "686", 3)) {
462 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
463 cachedsi.wProcessorLevel= 5;
464 /* this should imply MMX */
465 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
470 /* it may be worth reading from /var/run/dmesg.boot for
471 additional information such as CX8, MMX and TSC
472 (however this information should be considered less
473 reliable than that from the sysctl calls) */
474 if (f != NULL)
476 while (fgets(model, 255, f) != NULL) {
477 if (sscanf(model,"cpu%d: features %x<", value, value+1) == 2) {
478 /* we could scan the string but it is easier
479 to test the bits directly */
480 if (value[1] & 0x1)
481 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
482 if (value[1] & 0x10)
483 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
484 if (value[1] & 0x100)
485 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
486 if (value[1] & 0x800000)
487 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
489 break;
492 fclose(f);
496 memcpy(si,&cachedsi,sizeof(*si));
497 #elif defined(__FreeBSD__)
499 unsigned int regs[4], regs2[4];
500 int ret, len, num;
501 if (!have_cpuid())
502 regs[0] = 0; /* No cpuid support -- skip the rest */
503 else
504 do_cpuid(0x00000000, regs); /* get standard cpuid level and vendor name */
505 if (regs[0]>=0x00000001) { /* Check for supported cpuid version */
506 do_cpuid(0x00000001, regs2); /* get cpu features */
507 switch ((regs2[0] >> 8)&0xf) { /* cpu family */
508 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
509 cachedsi.wProcessorLevel = 3;
510 break;
511 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
512 cachedsi.wProcessorLevel = 4;
513 break;
514 case 5:
515 case 6:
516 case 15: /* PPro/2/3/4 has same info as P1 */
517 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
518 cachedsi.wProcessorLevel = 5;
519 break;
520 default:
521 FIXME("unknown FreeBSD cpu family %d, please report! (-> setting to 386)\n", \
522 (regs2[0] >> 8)&0xf);
523 break;
525 PF[PF_FLOATING_POINT_EMULATED] = !(regs2[3] & 1);
526 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] & (1 << 4 )) >> 4;
527 PF[PF_COMPARE_EXCHANGE_DOUBLE] = (regs2[3] & (1 << 8 )) >> 8;
528 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 23)) >> 23;
529 /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
530 /*len = sizeof(num);
531 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
532 if (!ret)
533 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
535 if (regs[1] == AUTH &&
536 regs[3] == ENTI &&
537 regs[2] == CAMD) {
538 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
539 if (regs[0]>=0x80000001) {
540 do_cpuid(0x80000001, regs2); /* get vendor features */
541 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] =
542 (regs2[3] & (1 << 31 )) >> 31;
546 len = sizeof(num);
547 ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
548 if (!ret)
549 cachedsi.dwNumberOfProcessors = num;
551 memcpy(si,&cachedsi,sizeof(*si));
552 #else
553 FIXME("not yet supported on this system\n");
554 #endif
555 TRACE("<- CPU arch %d, res'd %d, pagesize %ld, minappaddr %p, maxappaddr %p,"
556 " act.cpumask %08lx, numcpus %ld, CPU type %ld, allocgran. %ld, CPU level %d, CPU rev %d\n",
557 si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
558 si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
559 si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
560 si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
562 create_registry_keys( &cachedsi );
566 /***********************************************************************
567 * IsProcessorFeaturePresent [KERNEL32.@]
568 * RETURNS:
569 * TRUE if processor feature present
570 * FALSE otherwise
572 BOOL WINAPI IsProcessorFeaturePresent (
573 DWORD feature /* [in] feature number, see PF_ defines */
575 SYSTEM_INFO si;
576 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
578 if (feature < 64)
579 return PF[feature];
580 else
581 return FALSE;