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>
40 #ifdef HAVE_SYS_TIME_H
41 # include <sys/time.h>
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
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)
68 __asm__("pushl %%ebx\n\t"
70 "movl %%ebx, %%esi\n\t"
72 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
77 /* From xf86info havecpuid.c 1.11 */
78 static inline int have_cpuid(void)
92 : "=&r" (f1), "=&r" (f2)
94 return ((f1^f2) & 0x00200000) != 0;
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};
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;
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 ))
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) );
160 RtlFreeUnicodeString( &nameW );
164 NtClose( system_key );
167 /****************************************************************************
168 * QueryPerformanceCounter (KERNEL32.@)
170 * Get the current value of the performance counter.
173 * counter [O] Destination for the current counter reading
176 * Success: TRUE. counter contains the current reading
180 * See QueryPerformanceFrequency.
182 BOOL WINAPI QueryPerformanceCounter(PLARGE_INTEGER counter)
186 #if defined(__i386__) && defined(__GNUC__)
187 if (IsProcessorFeaturePresent( PF_RDTSC_INSTRUCTION_AVAILABLE )) {
188 /* i586 optimized version */
189 __asm__ __volatile__ ( "rdtsc"
190 : "=a" (counter->s.LowPart), "=d" (counter->s.HighPart) );
191 counter->QuadPart = counter->QuadPart / 1000; /* see below */
196 /* fall back to generic routine (ie, for i386, i486) */
197 gettimeofday( &tv, NULL );
198 counter->QuadPart = (LONGLONG)tv.tv_usec + (LONGLONG)tv.tv_sec * 1000000;
203 /****************************************************************************
204 * QueryPerformanceFrequency (KERNEL32.@)
206 * Get the resolution of the performace counter.
209 * frequency [O] Destination for the counter resolution
212 * Success. TRUE. Frequency contains the resolution of the counter.
216 * See QueryPerformanceCounter.
218 BOOL WINAPI QueryPerformanceFrequency(PLARGE_INTEGER frequency)
220 #if defined(__i386__) && defined(__GNUC__)
221 if (IsProcessorFeaturePresent( PF_RDTSC_INSTRUCTION_AVAILABLE )) {
222 /* The way Windows calculates this value is unclear, however simply using the CPU frequency
223 gives a value out by approximately a thousand. That can cause some applications to crash,
224 so we divide here to make our number more similar to the one Windows gives */
225 frequency->QuadPart = cpuHz / 1000;
229 frequency->s.LowPart = 1000000;
230 frequency->s.HighPart = 0;
235 /***********************************************************************
236 * GetSystemInfo [KERNEL32.@]
238 * Get information about the system.
244 * On the first call it creates cached values, so it doesn't have to determine
245 * them repeatedly. On Linux, the "/proc/cpuinfo" special file is used.
247 * It creates a registry subhierarchy, looking like:
248 * "\HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\Identifier (CPU x86)".
249 * Note that there is a hierarchy for every processor installed, so this
250 * supports multiprocessor systems. This is done like Win95 does it, I think.
252 * It also creates a cached flag array for IsProcessorFeaturePresent().
254 VOID WINAPI GetSystemInfo(
255 LPSYSTEM_INFO si /* [out] Destination for system information, may not be NULL */)
257 static int cache = 0;
258 static SYSTEM_INFO cachedsi;
260 TRACE("si=0x%p\n", si);
262 memcpy(si,&cachedsi,sizeof(*si));
265 memset(PF,0,sizeof(PF));
267 /* choose sensible defaults ...
268 * FIXME: perhaps overrideable with precompiler flags?
270 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
271 cachedsi.dwPageSize = getpagesize();
273 /* FIXME: the two entries below should be computed somehow... */
274 cachedsi.lpMinimumApplicationAddress = (void *)0x00010000;
275 cachedsi.lpMaximumApplicationAddress = (void *)0x7FFFFFFF;
276 cachedsi.dwActiveProcessorMask = 1;
277 cachedsi.dwNumberOfProcessors = 1;
278 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
279 cachedsi.dwAllocationGranularity = 0x10000;
280 cachedsi.wProcessorLevel = 5; /* 586 */
281 cachedsi.wProcessorRevision = 0;
283 cache = 1; /* even if there is no more info, we now have a cacheentry */
284 memcpy(si,&cachedsi,sizeof(*si));
286 /* Hmm, reasonable processor feature defaults? */
291 FILE *f = fopen ("/proc/cpuinfo", "r");
295 while (fgets(line,200,f)!=NULL) {
298 /* NOTE: the ':' is the only character we can rely on */
299 if (!(value = strchr(line,':')))
302 /* terminate the valuename */
304 while ((s >= line) && ((*s == ' ') || (*s == '\t'))) s--;
307 /* and strip leading spaces from value */
309 while (*value==' ') value++;
310 if ((s=strchr(value,'\n')))
314 if (!strcasecmp(line, "cpu family")) {
315 if (isdigit (value[0])) {
316 switch (value[0] - '0') {
317 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
318 cachedsi.wProcessorLevel= 3;
320 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
321 cachedsi.wProcessorLevel= 4;
324 case 6: /* PPro/2/3 has same info as P1 */
325 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
326 cachedsi.wProcessorLevel= 5;
328 case 1: /* two-figure levels */
331 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
332 cachedsi.wProcessorLevel= 5;
337 FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value);
344 if (!strcasecmp(line, "cpu")) {
345 if ( isdigit (value[0]) && value[1] == '8' &&
346 value[2] == '6' && value[3] == 0
348 switch (value[0] - '0') {
349 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
350 cachedsi.wProcessorLevel= 3;
352 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
353 cachedsi.wProcessorLevel= 4;
356 case 6: /* PPro/2/3 has same info as P1 */
357 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
358 cachedsi.wProcessorLevel= 5;
361 FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
367 if (!strcasecmp(line,"fdiv_bug")) {
368 if (!strncasecmp(value,"yes",3))
369 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
373 if (!strcasecmp(line,"fpu")) {
374 if (!strncasecmp(value,"no",2))
375 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
379 if (!strcasecmp(line,"processor")) {
380 /* processor number counts up... */
383 if (sscanf(value,"%d",&x))
384 if (x+1>cachedsi.dwNumberOfProcessors)
385 cachedsi.dwNumberOfProcessors=x+1;
389 if (!strcasecmp(line,"stepping")) {
392 if (sscanf(value,"%d",&x))
393 cachedsi.wProcessorRevision = x;
397 if (!strcasecmp(line, "cpu MHz")) {
399 if (sscanf( value, "%lf", &cmz ) == 1) {
400 /* SYSTEMINFO doesn't have a slot for cpu speed, so store in a global */
401 cpuHz = cmz * 1000 * 1000;
402 TRACE("CPU speed read as %lld\n", cpuHz);
406 if ( !strcasecmp(line,"flags") ||
407 !strcasecmp(line,"features")
409 if (strstr(value,"cx8"))
410 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
411 if (strstr(value,"mmx"))
412 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
413 if (strstr(value,"tsc"))
414 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
415 if (strstr(value,"3dnow"))
416 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
417 /* This will also catch sse2, but we have sse itself
418 * if we have sse2, so no problem */
419 if (strstr(value,"sse"))
420 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
421 if (strstr(value,"sse2"))
422 PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
423 if (strstr(value,"pae"))
424 PF[PF_PAE_ENABLED] = TRUE;
431 memcpy(si,&cachedsi,sizeof(*si));
432 #elif defined (__NetBSD__)
438 FILE *f = fopen ("/var/run/dmesg.boot", "r");
440 /* first deduce as much as possible from the sysctls */
441 mib[0] = CTL_MACHDEP;
442 #ifdef CPU_FPU_PRESENT
443 mib[1] = CPU_FPU_PRESENT;
444 value[1] = sizeof(int);
445 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
446 if (value) PF[PF_FLOATING_POINT_EMULATED] = FALSE;
447 else PF[PF_FLOATING_POINT_EMULATED] = TRUE;
450 mib[1] = CPU_SSE; /* this should imply MMX */
451 value[1] = sizeof(int);
452 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
453 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
456 mib[1] = CPU_SSE2; /* this should imply MMX */
457 value[1] = sizeof(int);
458 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
459 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
463 value[1] = sizeof(int);
464 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
465 if (value[0] > cachedsi.dwNumberOfProcessors)
466 cachedsi.dwNumberOfProcessors = value[0];
469 if (sysctl(mib, 2, model, value+1, NULL, 0) >= 0) {
470 model[value[1]] = '\0'; /* just in case */
471 cpuclass = strstr(model, "-class");
472 if (cpuclass != NULL) {
473 while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
474 if (!strncmp(cpuclass+1, "386", 3)) {
475 cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
476 cachedsi.wProcessorLevel= 3;
478 if (!strncmp(cpuclass+1, "486", 3)) {
479 cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
480 cachedsi.wProcessorLevel= 4;
482 if (!strncmp(cpuclass+1, "586", 3)) {
483 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
484 cachedsi.wProcessorLevel= 5;
486 if (!strncmp(cpuclass+1, "686", 3)) {
487 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
488 cachedsi.wProcessorLevel= 5;
489 /* this should imply MMX */
490 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
495 /* it may be worth reading from /var/run/dmesg.boot for
496 additional information such as CX8, MMX and TSC
497 (however this information should be considered less
498 reliable than that from the sysctl calls) */
501 while (fgets(model, 255, f) != NULL) {
502 if (sscanf(model,"cpu%d: features %x<", value, value+1) == 2) {
503 /* we could scan the string but it is easier
504 to test the bits directly */
506 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
508 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
509 if (value[1] & 0x100)
510 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
511 if (value[1] & 0x800000)
512 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
521 memcpy(si,&cachedsi,sizeof(*si));
522 #elif defined(__FreeBSD__)
524 unsigned int regs[4], regs2[4];
527 regs[0] = 0; /* No cpuid support -- skip the rest */
529 do_cpuid(0x00000000, regs); /* get standard cpuid level and vendor name */
530 if (regs[0]>=0x00000001) { /* Check for supported cpuid version */
531 do_cpuid(0x00000001, regs2); /* get cpu features */
532 switch ((regs2[0] >> 8)&0xf) { /* cpu family */
533 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
534 cachedsi.wProcessorLevel = 3;
536 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
537 cachedsi.wProcessorLevel = 4;
541 case 15: /* PPro/2/3/4 has same info as P1 */
542 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
543 cachedsi.wProcessorLevel = 5;
546 FIXME("unknown FreeBSD cpu family %d, please report! (-> setting to 386)\n", \
547 (regs2[0] >> 8)&0xf);
550 PF[PF_FLOATING_POINT_EMULATED] = !(regs2[3] & 1);
551 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] & (1 << 4 )) >> 4;
552 PF[PF_COMPARE_EXCHANGE_DOUBLE] = (regs2[3] & (1 << 8 )) >> 8;
553 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 23)) >> 23;
554 /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
556 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
558 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
560 if (regs[1] == AUTH &&
563 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
564 if (regs[0]>=0x80000001) {
565 do_cpuid(0x80000001, regs2); /* get vendor features */
566 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] =
567 (regs2[3] & (1 << 31 )) >> 31;
572 ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
574 cachedsi.dwNumberOfProcessors = num;
576 memcpy(si,&cachedsi,sizeof(*si));
578 FIXME("not yet supported on this system\n");
580 TRACE("<- CPU arch %d, res'd %d, pagesize %ld, minappaddr %p, maxappaddr %p,"
581 " act.cpumask %08lx, numcpus %ld, CPU type %ld, allocgran. %ld, CPU level %d, CPU rev %d\n",
582 si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
583 si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
584 si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
585 si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
587 create_registry_keys( &cachedsi );
591 /***********************************************************************
592 * IsProcessorFeaturePresent [KERNEL32.@]
594 * Determine if the cpu supports a given feature.
597 * TRUE, If the processor supports feature,
600 BOOL WINAPI IsProcessorFeaturePresent (
601 DWORD feature /* [in] Feature number, (PF_ constants from "winnt.h") */)
604 GetSystemInfo (&si); /* To ensure the information is loaded and cached */