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
23 #include "wine/port.h"
25 #ifdef HAVE_SYS_PARAM_H
26 # include <sys/param.h>
28 #ifdef HAVE_SYS_SYSCTL_H
29 # include <sys/sysctl.h>
31 #ifdef HAVE_MACHINE_CPU_H
32 # include <machine/cpu.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
44 #define NONAMELESSUNION
45 #define NONAMELESSSTRUCT
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
)
65 __asm__("pushl %%ebx\n\t"
67 "movl %%ebx, %%esi\n\t"
69 : "=a" (p
[0]), "=S" (p
[1]), "=c" (p
[2]), "=d" (p
[3])
74 /* From xf86info havecpuid.c 1.11 */
75 static inline int have_cpuid(void)
89 : "=&r" (f1
), "=&r" (f2
)
91 return ((f1
^f2
) & 0x00200000) != 0;
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};
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
;
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
))
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
) );
157 RtlFreeUnicodeString( &nameW
);
161 NtClose( system_key
);
164 /****************************************************************************
165 * QueryPerformanceCounter (KERNEL32.@)
167 BOOL WINAPI
QueryPerformanceCounter(PLARGE_INTEGER counter
)
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 */
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;
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;
202 frequency
->s
.LowPart
= 1000000;
203 frequency
->s
.HighPart
= 0;
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.
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
);
237 memcpy(si
,&cachedsi
,sizeof(*si
));
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? */
266 FILE *f
= fopen ("/proc/cpuinfo", "r");
270 while (fgets(line
,200,f
)!=NULL
) {
273 /* NOTE: the ':' is the only character we can rely on */
274 if (!(value
= strchr(line
,':')))
277 /* terminate the valuename */
279 while ((s
>= line
) && ((*s
== ' ') || (*s
== '\t'))) s
--;
282 /* and strip leading spaces from value */
284 while (*value
==' ') value
++;
285 if ((s
=strchr(value
,'\n')))
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;
295 case 4: cachedsi
.dwProcessorType
= PROCESSOR_INTEL_486
;
296 cachedsi
.wProcessorLevel
= 4;
299 case 6: /* PPro/2/3 has same info as P1 */
300 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
301 cachedsi
.wProcessorLevel
= 5;
303 case 1: /* two-figure levels */
306 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
307 cachedsi
.wProcessorLevel
= 5;
312 FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value
);
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;
327 case 4: cachedsi
.dwProcessorType
= PROCESSOR_INTEL_486
;
328 cachedsi
.wProcessorLevel
= 4;
331 case 6: /* PPro/2/3 has same info as P1 */
332 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
333 cachedsi
.wProcessorLevel
= 5;
336 FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value
);
342 if (!strcasecmp(line
,"fdiv_bug")) {
343 if (!strncasecmp(value
,"yes",3))
344 PF
[PF_FLOATING_POINT_PRECISION_ERRATA
] = TRUE
;
348 if (!strcasecmp(line
,"fpu")) {
349 if (!strncasecmp(value
,"no",2))
350 PF
[PF_FLOATING_POINT_EMULATED
] = TRUE
;
354 if (!strcasecmp(line
,"processor")) {
355 /* processor number counts up... */
358 if (sscanf(value
,"%d",&x
))
359 if (x
+1>cachedsi
.dwNumberOfProcessors
)
360 cachedsi
.dwNumberOfProcessors
=x
+1;
364 if (!strcasecmp(line
,"stepping")) {
367 if (sscanf(value
,"%d",&x
))
368 cachedsi
.wProcessorRevision
= x
;
372 if (!strcasecmp(line
, "cpu MHz")) {
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
);
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
;
406 memcpy(si
,&cachedsi
,sizeof(*si
));
407 #elif defined (__NetBSD__)
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
;
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
;
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
;
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];
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) */
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 */
481 PF
[PF_FLOATING_POINT_EMULATED
] = TRUE
;
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
;
496 memcpy(si
,&cachedsi
,sizeof(*si
));
497 #elif defined(__FreeBSD__)
499 unsigned int regs
[4], regs2
[4];
502 regs
[0] = 0; /* No cpuid support -- skip the rest */
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;
511 case 4: cachedsi
.dwProcessorType
= PROCESSOR_INTEL_486
;
512 cachedsi
.wProcessorLevel
= 4;
516 case 15: /* PPro/2/3/4 has same info as P1 */
517 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
518 cachedsi
.wProcessorLevel
= 5;
521 FIXME("unknown FreeBSD cpu family %d, please report! (-> setting to 386)\n", \
522 (regs2
[0] >> 8)&0xf);
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? */
531 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
533 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
535 if (regs
[1] == AUTH
&&
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;
547 ret
= sysctlbyname("hw.ncpu", &num
, &len
, NULL
, 0);
549 cachedsi
.dwNumberOfProcessors
= num
;
551 memcpy(si
,&cachedsi
,sizeof(*si
));
553 FIXME("not yet supported on this system\n");
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.@]
569 * TRUE if processor feature present
572 BOOL WINAPI
IsProcessorFeaturePresent (
573 DWORD feature
/* [in] feature number, see PF_ defines */
576 GetSystemInfo (&si
); /* To ensure the information is loaded and cached */