Remove unused variable debug_channels.
[wine/wine-gecko.git] / misc / cpu.c
blob12410b19e62e5f510d8c1f3644080d42e9dfb390
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 <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #ifdef HAVE_SYS_TIME_H
41 # include <sys/time.h>
42 #endif
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winnt.h"
50 #include "winreg.h"
51 #include "winternl.h"
52 #include "winerror.h"
53 #include "wine/unicode.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(reg);
58 #define AUTH 0x68747541 /* "Auth" */
59 #define ENTI 0x69746e65 /* "enti" */
60 #define CAMD 0x444d4163 /* "cAMD" */
62 /* Calls cpuid with an eax of 'ax' and returns the 16 bytes in *p
63 * We are compiled with -fPIC, so we can't clobber ebx.
65 static inline void do_cpuid(int ax, int *p)
67 #ifdef __i386__
68 __asm__("pushl %%ebx\n\t"
69 "cpuid\n\t"
70 "movl %%ebx, %%esi\n\t"
71 "popl %%ebx"
72 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
73 : "0" (ax));
74 #endif
77 /* From xf86info havecpuid.c 1.11 */
78 static inline int have_cpuid(void)
80 #ifdef __i386__
81 unsigned int f1, f2;
82 __asm__("pushfl\n\t"
83 "pushfl\n\t"
84 "popl %0\n\t"
85 "movl %0,%1\n\t"
86 "xorl %2,%0\n\t"
87 "pushl %0\n\t"
88 "popfl\n\t"
89 "pushfl\n\t"
90 "popl %0\n\t"
91 "popfl"
92 : "=&r" (f1), "=&r" (f2)
93 : "ir" (0x00200000));
94 return ((f1^f2) & 0x00200000) != 0;
95 #else
96 return 0;
97 #endif
100 static BYTE PF[64] = {0,};
101 static ULONGLONG cpuHz = 1000000000; /* default to a 1GHz */
103 static void create_registry_keys( const SYSTEM_INFO *info )
105 static const WCHAR SystemW[] = {'M','a','c','h','i','n','e','\\',
106 'H','a','r','d','w','a','r','e','\\',
107 'D','e','s','c','r','i','p','t','i','o','n','\\',
108 'S','y','s','t','e','m',0};
109 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};
110 static const WCHAR cpuW[] = {'C','e','n','t','r','a','l','P','r','o','c','e','s','s','o','r',0};
111 static const WCHAR IdentifierW[] = {'I','d','e','n','t','i','f','i','e','r',0};
112 static const WCHAR SysidW[] = {'A','T',' ','c','o','m','p','a','t','i','b','l','e',0};
113 static const WCHAR mhzKeyW[] = {'~','M','H','z',0};
115 int i;
116 HKEY hkey, system_key, cpu_key;
117 OBJECT_ATTRIBUTES attr;
118 UNICODE_STRING nameW, valueW;
120 attr.Length = sizeof(attr);
121 attr.RootDirectory = 0;
122 attr.ObjectName = &nameW;
123 attr.Attributes = 0;
124 attr.SecurityDescriptor = NULL;
125 attr.SecurityQualityOfService = NULL;
127 RtlInitUnicodeString( &nameW, SystemW );
128 if (NtCreateKey( &system_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
130 RtlInitUnicodeString( &valueW, IdentifierW );
131 NtSetValueKey( system_key, &valueW, 0, REG_SZ, SysidW, (strlenW(SysidW)+1) * sizeof(WCHAR) );
133 attr.RootDirectory = system_key;
134 RtlInitUnicodeString( &nameW, fpuW );
135 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
137 RtlInitUnicodeString( &nameW, cpuW );
138 if (!NtCreateKey( &cpu_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
140 for (i = 0; i < info->dwNumberOfProcessors; i++)
142 char num[10], id[20];
144 attr.RootDirectory = cpu_key;
145 sprintf( num, "%d", i );
146 RtlCreateUnicodeStringFromAsciiz( &nameW, num );
147 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
149 WCHAR idW[20];
150 DWORD cpuMHz = cpuHz / 1000000;
152 sprintf( id, "CPU %ld", info->dwProcessorType );
153 RtlMultiByteToUnicodeN( idW, sizeof(idW), NULL, id, strlen(id)+1 );
154 NtSetValueKey( hkey, &valueW, 0, REG_SZ, idW, (strlenW(idW)+1)*sizeof(WCHAR) );
156 RtlInitUnicodeString( &valueW, mhzKeyW );
157 NtSetValueKey( hkey, &valueW, 0, REG_DWORD, &cpuMHz, sizeof(DWORD) );
158 NtClose( hkey );
160 RtlFreeUnicodeString( &nameW );
162 NtClose( cpu_key );
164 NtClose( system_key );
167 /****************************************************************************
168 * QueryPerformanceCounter (KERNEL32.@)
170 BOOL WINAPI QueryPerformanceCounter(PLARGE_INTEGER counter)
172 struct timeval tv;
174 #if defined(__i386__) && defined(__GNUC__)
175 if (IsProcessorFeaturePresent( PF_RDTSC_INSTRUCTION_AVAILABLE )) {
176 /* i586 optimized version */
177 __asm__ __volatile__ ( "rdtsc"
178 : "=a" (counter->s.LowPart), "=d" (counter->s.HighPart) );
179 counter->QuadPart = counter->QuadPart / 1000; /* see below */
180 return TRUE;
182 #endif
184 /* fall back to generic routine (ie, for i386, i486) */
185 gettimeofday( &tv, NULL );
186 counter->QuadPart = (LONGLONG)tv.tv_usec + (LONGLONG)tv.tv_sec * 1000000;
187 return TRUE;
191 /****************************************************************************
192 * QueryPerformanceFrequency (KERNEL32.@)
194 BOOL WINAPI QueryPerformanceFrequency(PLARGE_INTEGER frequency)
196 #if defined(__i386__) && defined(__GNUC__)
197 if (IsProcessorFeaturePresent( PF_RDTSC_INSTRUCTION_AVAILABLE )) {
198 /* The way Windows calculates this value is unclear, however simply using the CPU frequency
199 gives a value out by approximately a thousand. That can cause some applications to crash,
200 so we divide here to make our number more similar to the one Windows gives */
201 frequency->QuadPart = cpuHz / 1000;
202 return TRUE;
204 #endif
205 frequency->s.LowPart = 1000000;
206 frequency->s.HighPart = 0;
207 return TRUE;
211 /***********************************************************************
212 * GetSystemInfo [KERNEL32.@]
214 * Gets the current system information.
216 * On the first call it creates cached values, so it doesn't have to determine
217 * them repeatedly. On Linux, the /proc/cpuinfo special file is used.
219 * It creates a registry subhierarchy, looking like:
220 * \HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\
221 * Identifier (CPU x86)
222 * Note that there is a hierarchy for every processor installed, so this
223 * supports multiprocessor systems. This is done like Win95 does it, I think.
225 * It also creates a cached flag array for IsProcessorFeaturePresent().
227 * No NULL ptr check for LPSYSTEM_INFO in Win9x.
229 * RETURNS
230 * nothing, really
232 VOID WINAPI GetSystemInfo(
233 LPSYSTEM_INFO si /* [out] system information */
235 static int cache = 0;
236 static SYSTEM_INFO cachedsi;
238 TRACE("si=0x%p\n", si);
239 if (cache) {
240 memcpy(si,&cachedsi,sizeof(*si));
241 return;
243 memset(PF,0,sizeof(PF));
245 /* choose sensible defaults ...
246 * FIXME: perhaps overrideable with precompiler flags?
248 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
249 cachedsi.dwPageSize = getpagesize();
251 /* FIXME: the two entries below should be computed somehow... */
252 cachedsi.lpMinimumApplicationAddress = (void *)0x00010000;
253 cachedsi.lpMaximumApplicationAddress = (void *)0x7FFFFFFF;
254 cachedsi.dwActiveProcessorMask = 1;
255 cachedsi.dwNumberOfProcessors = 1;
256 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
257 cachedsi.dwAllocationGranularity = 0x10000;
258 cachedsi.wProcessorLevel = 5; /* 586 */
259 cachedsi.wProcessorRevision = 0;
261 cache = 1; /* even if there is no more info, we now have a cacheentry */
262 memcpy(si,&cachedsi,sizeof(*si));
264 /* Hmm, reasonable processor feature defaults? */
266 #ifdef linux
268 char line[200];
269 FILE *f = fopen ("/proc/cpuinfo", "r");
271 if (!f)
272 return;
273 while (fgets(line,200,f)!=NULL) {
274 char *s,*value;
276 /* NOTE: the ':' is the only character we can rely on */
277 if (!(value = strchr(line,':')))
278 continue;
280 /* terminate the valuename */
281 s = value - 1;
282 while ((s >= line) && ((*s == ' ') || (*s == '\t'))) s--;
283 *(s + 1) = '\0';
285 /* and strip leading spaces from value */
286 value += 1;
287 while (*value==' ') value++;
288 if ((s=strchr(value,'\n')))
289 *s='\0';
291 /* 2.1 method */
292 if (!strcasecmp(line, "cpu family")) {
293 if (isdigit (value[0])) {
294 switch (value[0] - '0') {
295 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
296 cachedsi.wProcessorLevel= 3;
297 break;
298 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
299 cachedsi.wProcessorLevel= 4;
300 break;
301 case 5:
302 case 6: /* PPro/2/3 has same info as P1 */
303 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
304 cachedsi.wProcessorLevel= 5;
305 break;
306 case 1: /* two-figure levels */
307 if (value[1] == '5')
309 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
310 cachedsi.wProcessorLevel= 5;
311 break;
313 /* fall through */
314 default:
315 FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value);
316 break;
319 continue;
321 /* old 2.0 method */
322 if (!strcasecmp(line, "cpu")) {
323 if ( isdigit (value[0]) && value[1] == '8' &&
324 value[2] == '6' && value[3] == 0
326 switch (value[0] - '0') {
327 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
328 cachedsi.wProcessorLevel= 3;
329 break;
330 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
331 cachedsi.wProcessorLevel= 4;
332 break;
333 case 5:
334 case 6: /* PPro/2/3 has same info as P1 */
335 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
336 cachedsi.wProcessorLevel= 5;
337 break;
338 default:
339 FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
340 break;
343 continue;
345 if (!strcasecmp(line,"fdiv_bug")) {
346 if (!strncasecmp(value,"yes",3))
347 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
349 continue;
351 if (!strcasecmp(line,"fpu")) {
352 if (!strncasecmp(value,"no",2))
353 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
355 continue;
357 if (!strcasecmp(line,"processor")) {
358 /* processor number counts up... */
359 unsigned int x;
361 if (sscanf(value,"%d",&x))
362 if (x+1>cachedsi.dwNumberOfProcessors)
363 cachedsi.dwNumberOfProcessors=x+1;
365 continue;
367 if (!strcasecmp(line,"stepping")) {
368 int x;
370 if (sscanf(value,"%d",&x))
371 cachedsi.wProcessorRevision = x;
373 continue;
375 if (!strcasecmp(line, "cpu MHz")) {
376 double cmz;
377 if (sscanf( value, "%lf", &cmz ) == 1) {
378 /* SYSTEMINFO doesn't have a slot for cpu speed, so store in a global */
379 cpuHz = cmz * 1000 * 1000;
380 TRACE("CPU speed read as %lld\n", cpuHz);
382 continue;
384 if ( !strcasecmp(line,"flags") ||
385 !strcasecmp(line,"features")
387 if (strstr(value,"cx8"))
388 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
389 if (strstr(value,"mmx"))
390 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
391 if (strstr(value,"tsc"))
392 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
393 if (strstr(value,"3dnow"))
394 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
395 /* This will also catch sse2, but we have sse itself
396 * if we have sse2, so no problem */
397 if (strstr(value,"sse"))
398 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
399 if (strstr(value,"sse2"))
400 PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
401 if (strstr(value,"pae"))
402 PF[PF_PAE_ENABLED] = TRUE;
404 continue;
407 fclose (f);
409 memcpy(si,&cachedsi,sizeof(*si));
410 #elif defined (__NetBSD__)
412 int mib[2];
413 int value[2];
414 char model[256];
415 char *cpuclass;
416 FILE *f = fopen ("/var/run/dmesg.boot", "r");
418 /* first deduce as much as possible from the sysctls */
419 mib[0] = CTL_MACHDEP;
420 #ifdef CPU_FPU_PRESENT
421 mib[1] = CPU_FPU_PRESENT;
422 value[1] = sizeof(int);
423 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
424 if (value) PF[PF_FLOATING_POINT_EMULATED] = FALSE;
425 else PF[PF_FLOATING_POINT_EMULATED] = TRUE;
426 #endif
427 #ifdef CPU_SSE
428 mib[1] = CPU_SSE; /* this should imply MMX */
429 value[1] = sizeof(int);
430 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
431 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
432 #endif
433 #ifdef CPU_SSE2
434 mib[1] = CPU_SSE2; /* this should imply MMX */
435 value[1] = sizeof(int);
436 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
437 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
438 #endif
439 mib[0] = CTL_HW;
440 mib[1] = HW_NCPU;
441 value[1] = sizeof(int);
442 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
443 if (value[0] > cachedsi.dwNumberOfProcessors)
444 cachedsi.dwNumberOfProcessors = value[0];
445 mib[1] = HW_MODEL;
446 value[1] = 255;
447 if (sysctl(mib, 2, model, value+1, NULL, 0) >= 0) {
448 model[value[1]] = '\0'; /* just in case */
449 cpuclass = strstr(model, "-class");
450 if (cpuclass != NULL) {
451 while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
452 if (!strncmp(cpuclass+1, "386", 3)) {
453 cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
454 cachedsi.wProcessorLevel= 3;
456 if (!strncmp(cpuclass+1, "486", 3)) {
457 cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
458 cachedsi.wProcessorLevel= 4;
460 if (!strncmp(cpuclass+1, "586", 3)) {
461 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
462 cachedsi.wProcessorLevel= 5;
464 if (!strncmp(cpuclass+1, "686", 3)) {
465 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
466 cachedsi.wProcessorLevel= 5;
467 /* this should imply MMX */
468 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
473 /* it may be worth reading from /var/run/dmesg.boot for
474 additional information such as CX8, MMX and TSC
475 (however this information should be considered less
476 reliable than that from the sysctl calls) */
477 if (f != NULL)
479 while (fgets(model, 255, f) != NULL) {
480 if (sscanf(model,"cpu%d: features %x<", value, value+1) == 2) {
481 /* we could scan the string but it is easier
482 to test the bits directly */
483 if (value[1] & 0x1)
484 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
485 if (value[1] & 0x10)
486 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
487 if (value[1] & 0x100)
488 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
489 if (value[1] & 0x800000)
490 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
492 break;
495 fclose(f);
499 memcpy(si,&cachedsi,sizeof(*si));
500 #elif defined(__FreeBSD__)
502 unsigned int regs[4], regs2[4];
503 int ret, len, num;
504 if (!have_cpuid())
505 regs[0] = 0; /* No cpuid support -- skip the rest */
506 else
507 do_cpuid(0x00000000, regs); /* get standard cpuid level and vendor name */
508 if (regs[0]>=0x00000001) { /* Check for supported cpuid version */
509 do_cpuid(0x00000001, regs2); /* get cpu features */
510 switch ((regs2[0] >> 8)&0xf) { /* cpu family */
511 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
512 cachedsi.wProcessorLevel = 3;
513 break;
514 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
515 cachedsi.wProcessorLevel = 4;
516 break;
517 case 5:
518 case 6:
519 case 15: /* PPro/2/3/4 has same info as P1 */
520 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
521 cachedsi.wProcessorLevel = 5;
522 break;
523 default:
524 FIXME("unknown FreeBSD cpu family %d, please report! (-> setting to 386)\n", \
525 (regs2[0] >> 8)&0xf);
526 break;
528 PF[PF_FLOATING_POINT_EMULATED] = !(regs2[3] & 1);
529 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] & (1 << 4 )) >> 4;
530 PF[PF_COMPARE_EXCHANGE_DOUBLE] = (regs2[3] & (1 << 8 )) >> 8;
531 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 23)) >> 23;
532 /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
533 /*len = sizeof(num);
534 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
535 if (!ret)
536 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
538 if (regs[1] == AUTH &&
539 regs[3] == ENTI &&
540 regs[2] == CAMD) {
541 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
542 if (regs[0]>=0x80000001) {
543 do_cpuid(0x80000001, regs2); /* get vendor features */
544 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] =
545 (regs2[3] & (1 << 31 )) >> 31;
549 len = sizeof(num);
550 ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
551 if (!ret)
552 cachedsi.dwNumberOfProcessors = num;
554 memcpy(si,&cachedsi,sizeof(*si));
555 #else
556 FIXME("not yet supported on this system\n");
557 #endif
558 TRACE("<- CPU arch %d, res'd %d, pagesize %ld, minappaddr %p, maxappaddr %p,"
559 " act.cpumask %08lx, numcpus %ld, CPU type %ld, allocgran. %ld, CPU level %d, CPU rev %d\n",
560 si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
561 si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
562 si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
563 si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
565 create_registry_keys( &cachedsi );
569 /***********************************************************************
570 * IsProcessorFeaturePresent [KERNEL32.@]
571 * RETURNS:
572 * TRUE if processor feature present
573 * FALSE otherwise
575 BOOL WINAPI IsProcessorFeaturePresent (
576 DWORD feature /* [in] feature number, see PF_ defines */
578 SYSTEM_INFO si;
579 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
581 if (feature < 64)
582 return PF[feature];
583 else
584 return FALSE;