Merge CPU detection code into misc/cpu.c.
[wine/multimedia.git] / misc / cpu.c
blob6413a532545ede45188c0abd256609ebb5887740
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;
276 /* terminate the valuename */
277 *value++ = '\0';
278 /* skip any leading spaces */
279 while (*value==' ') value++;
280 if ((s=strchr(value,'\n')))
281 *s='\0';
283 /* 2.1 method */
284 if (!strncasecmp(line, "cpu family",strlen("cpu family"))) {
285 if (isdigit (value[0])) {
286 switch (value[0] - '0') {
287 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
288 cachedsi.wProcessorLevel= 3;
289 break;
290 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
291 cachedsi.wProcessorLevel= 4;
292 break;
293 case 5:
294 case 6: /* PPro/2/3 has same info as P1 */
295 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
296 cachedsi.wProcessorLevel= 5;
297 break;
298 case 1: /* two-figure levels */
299 if (value[1] == '5')
301 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
302 cachedsi.wProcessorLevel= 5;
303 break;
305 /* fall through */
306 default:
307 FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value);
308 break;
311 continue;
313 /* old 2.0 method */
314 if (!strncasecmp(line, "cpu",strlen("cpu"))) {
315 if ( isdigit (value[0]) && value[1] == '8' &&
316 value[2] == '6' && value[3] == 0
318 switch (value[0] - '0') {
319 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
320 cachedsi.wProcessorLevel= 3;
321 break;
322 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
323 cachedsi.wProcessorLevel= 4;
324 break;
325 case 5:
326 case 6: /* PPro/2/3 has same info as P1 */
327 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
328 cachedsi.wProcessorLevel= 5;
329 break;
330 default:
331 FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
332 break;
335 continue;
337 if (!strncasecmp(line,"fdiv_bug",strlen("fdiv_bug"))) {
338 if (!strncasecmp(value,"yes",3))
339 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
341 continue;
343 if (!strncasecmp(line,"fpu",strlen("fpu"))) {
344 if (!strncasecmp(value,"no",2))
345 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
347 continue;
349 if (!strncasecmp(line,"processor",strlen("processor"))) {
350 /* processor number counts up... */
351 unsigned int x;
353 if (sscanf(value,"%d",&x))
354 if (x+1>cachedsi.dwNumberOfProcessors)
355 cachedsi.dwNumberOfProcessors=x+1;
357 if (!strncasecmp(line,"stepping",strlen("stepping"))) {
358 int x;
360 if (sscanf(value,"%d",&x))
361 cachedsi.wProcessorRevision = x;
363 if (!strncasecmp(line, "cpu MHz",strlen("cpu MHz"))) {
364 double cmz;
365 if (sscanf( value, "%lf", &cmz ) == 1) {
366 /* SYSTEMINFO doesn't have a slot for cpu speed, so store in a global */
367 cpuHz = cmz * 1000 * 1000;
368 TRACE("CPU speed read as %lld\n", cpuHz);
371 if ( !strncasecmp(line,"flags",strlen("flags")) ||
372 !strncasecmp(line,"features",strlen("features"))
374 if (strstr(value,"cx8"))
375 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
376 if (strstr(value,"mmx"))
377 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
378 if (strstr(value,"tsc"))
379 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
380 if (strstr(value,"3dnow"))
381 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
382 /* This will also catch sse2, but we have sse itself
383 * if we have sse2, so no problem */
384 if (strstr(value,"sse"))
385 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
386 if (strstr(value,"sse2"))
387 PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
388 if (strstr(value,"pae"))
389 PF[PF_PAE_ENABLED] = TRUE;
393 fclose (f);
395 memcpy(si,&cachedsi,sizeof(*si));
396 #elif defined (__NetBSD__)
398 int mib[2];
399 int value[2];
400 char model[256];
401 char *cpuclass;
402 FILE *f = fopen ("/var/run/dmesg.boot", "r");
404 /* first deduce as much as possible from the sysctls */
405 mib[0] = CTL_MACHDEP;
406 #ifdef CPU_FPU_PRESENT
407 mib[1] = CPU_FPU_PRESENT;
408 value[1] = sizeof(int);
409 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
410 if (value) PF[PF_FLOATING_POINT_EMULATED] = FALSE;
411 else PF[PF_FLOATING_POINT_EMULATED] = TRUE;
412 #endif
413 #ifdef CPU_SSE
414 mib[1] = CPU_SSE; /* this should imply MMX */
415 value[1] = sizeof(int);
416 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
417 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
418 #endif
419 #ifdef CPU_SSE2
420 mib[1] = CPU_SSE2; /* this should imply MMX */
421 value[1] = sizeof(int);
422 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
423 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
424 #endif
425 mib[0] = CTL_HW;
426 mib[1] = HW_NCPU;
427 value[1] = sizeof(int);
428 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
429 if (value[0] > cachedsi.dwNumberOfProcessors)
430 cachedsi.dwNumberOfProcessors = value[0];
431 mib[1] = HW_MODEL;
432 value[1] = 255;
433 if (sysctl(mib, 2, model, value+1, NULL, 0) >= 0) {
434 model[value[1]] = '\0'; /* just in case */
435 cpuclass = strstr(model, "-class");
436 if (cpuclass != NULL) {
437 while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
438 if (!strncmp(cpuclass+1, "386", 3)) {
439 cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
440 cachedsi.wProcessorLevel= 3;
442 if (!strncmp(cpuclass+1, "486", 3)) {
443 cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
444 cachedsi.wProcessorLevel= 4;
446 if (!strncmp(cpuclass+1, "586", 3)) {
447 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
448 cachedsi.wProcessorLevel= 5;
450 if (!strncmp(cpuclass+1, "686", 3)) {
451 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
452 cachedsi.wProcessorLevel= 5;
453 /* this should imply MMX */
454 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
459 /* it may be worth reading from /var/run/dmesg.boot for
460 additional information such as CX8, MMX and TSC
461 (however this information should be considered less
462 reliable than that from the sysctl calls) */
463 if (f != NULL)
465 while (fgets(model, 255, f) != NULL) {
466 if (sscanf(model,"cpu%d: features %x<", value, value+1) == 2) {
467 /* we could scan the string but it is easier
468 to test the bits directly */
469 if (value[1] & 0x1)
470 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
471 if (value[1] & 0x10)
472 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
473 if (value[1] & 0x100)
474 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
475 if (value[1] & 0x800000)
476 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
478 break;
481 fclose(f);
485 memcpy(si,&cachedsi,sizeof(*si));
486 #elif defined(__FreeBSD__)
488 unsigned int regs[4], regs2[4];
489 int ret, len, num;
490 if (!have_cpuid())
491 regs[0] = 0; /* No cpuid support -- skip the rest */
492 else
493 do_cpuid(0x00000000, regs); /* get standard cpuid level and vendor name */
494 if (regs[0]>=0x00000001) { /* Check for supported cpuid version */
495 do_cpuid(0x00000001, regs2); /* get cpu features */
496 switch ((regs2[0] >> 8)&0xf) { /* cpu family */
497 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
498 cachedsi.wProcessorLevel = 3;
499 break;
500 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
501 cachedsi.wProcessorLevel = 4;
502 break;
503 case 5:
504 case 6:
505 case 15: /* PPro/2/3/4 has same info as P1 */
506 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
507 cachedsi.wProcessorLevel = 5;
508 break;
509 default:
510 FIXME("unknown FreeBSD cpu family %d, please report! (-> setting to 386)\n", \
511 (regs2[0] >> 8)&0xf);
512 break;
514 PF[PF_FLOATING_POINT_EMULATED] = !(regs2[3] & 1);
515 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] & (1 << 4 )) >> 4;
516 PF[PF_COMPARE_EXCHANGE_DOUBLE] = (regs2[3] & (1 << 8 )) >> 8;
517 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 23)) >> 23;
518 /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
519 /*len = sizeof(num);
520 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
521 if (!ret)
522 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
524 if (regs[1] == AUTH &&
525 regs[3] == ENTI &&
526 regs[2] == CAMD) {
527 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
528 if (regs[0]>=0x80000001) {
529 do_cpuid(0x80000001, regs2); /* get vendor features */
530 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] =
531 (regs2[3] & (1 << 31 )) >> 31;
535 len = sizeof(num);
536 ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
537 if (!ret)
538 cachedsi.dwNumberOfProcessors = num;
540 memcpy(si,&cachedsi,sizeof(*si));
541 #else
542 FIXME("not yet supported on this system\n");
543 #endif
544 TRACE("<- CPU arch %d, res'd %d, pagesize %ld, minappaddr %p, maxappaddr %p,"
545 " act.cpumask %08lx, numcpus %ld, CPU type %ld, allocgran. %ld, CPU level %d, CPU rev %d\n",
546 si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
547 si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
548 si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
549 si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
551 create_registry_keys( &cachedsi );
555 /***********************************************************************
556 * IsProcessorFeaturePresent [KERNEL32.@]
557 * RETURNS:
558 * TRUE if processor feature present
559 * FALSE otherwise
561 BOOL WINAPI IsProcessorFeaturePresent (
562 DWORD feature /* [in] feature number, see PF_ defines */
564 SYSTEM_INFO si;
565 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
567 if (feature < 64)
568 return PF[feature];
569 else
570 return FALSE;