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 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(reg
);
51 #define AUTH 0x68747541 /* "Auth" */
52 #define ENTI 0x69746e65 /* "enti" */
53 #define CAMD 0x444d4163 /* "cAMD" */
55 /* Calls cpuid with an eax of 'ax' and returns the 16 bytes in *p
56 * We are compiled with -fPIC, so we can't clobber ebx.
58 static inline void do_cpuid(int ax
, int *p
)
61 __asm__("pushl %%ebx\n\t"
63 "movl %%ebx, %%esi\n\t"
65 : "=a" (p
[0]), "=S" (p
[1]), "=c" (p
[2]), "=d" (p
[3])
70 /* From xf86info havecpuid.c 1.11 */
71 static inline int have_cpuid(void)
85 : "=&r" (f1
), "=&r" (f2
)
87 return ((f1
^f2
) & 0x00200000) != 0;
93 static BYTE PF
[64] = {0,};
95 static void create_registry_keys( const SYSTEM_INFO
*info
)
97 static const WCHAR SystemW
[] = {'M','a','c','h','i','n','e','\\',
98 'H','a','r','d','w','a','r','e','\\',
99 'D','e','s','c','r','i','p','t','i','o','n','\\',
100 'S','y','s','t','e','m',0};
101 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};
102 static const WCHAR cpuW
[] = {'C','e','n','t','r','a','l','P','r','o','c','e','s','s','o','r',0};
103 static const WCHAR IdentifierW
[] = {'I','d','e','n','t','i','f','i','e','r',0};
104 static const WCHAR SysidW
[] = {'A','T',' ','c','o','m','p','a','t','i','b','l','e',0};
107 HKEY hkey
, system_key
, cpu_key
;
108 OBJECT_ATTRIBUTES attr
;
109 UNICODE_STRING nameW
, valueW
;
111 attr
.Length
= sizeof(attr
);
112 attr
.RootDirectory
= 0;
113 attr
.ObjectName
= &nameW
;
115 attr
.SecurityDescriptor
= NULL
;
116 attr
.SecurityQualityOfService
= NULL
;
118 RtlInitUnicodeString( &nameW
, SystemW
);
119 if (NtCreateKey( &system_key
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
)) return;
121 RtlInitUnicodeString( &valueW
, IdentifierW
);
122 NtSetValueKey( system_key
, &valueW
, 0, REG_SZ
, SysidW
, (strlenW(SysidW
)+1) * sizeof(WCHAR
) );
124 attr
.RootDirectory
= system_key
;
125 RtlInitUnicodeString( &nameW
, fpuW
);
126 if (!NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
)) NtClose( hkey
);
128 RtlInitUnicodeString( &nameW
, cpuW
);
129 if (!NtCreateKey( &cpu_key
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
))
131 for (i
= 0; i
< info
->dwNumberOfProcessors
; i
++)
133 char num
[10], id
[20];
135 attr
.RootDirectory
= cpu_key
;
136 sprintf( num
, "%d", i
);
137 RtlCreateUnicodeStringFromAsciiz( &nameW
, num
);
138 if (!NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
))
142 sprintf( id
, "CPU %ld", info
->dwProcessorType
);
143 RtlMultiByteToUnicodeN( idW
, sizeof(idW
), NULL
, id
, strlen(id
)+1 );
144 NtSetValueKey( hkey
, &valueW
, 0, REG_SZ
, idW
, (strlenW(idW
)+1)*sizeof(WCHAR
) );
147 RtlFreeUnicodeString( &nameW
);
151 NtClose( system_key
);
155 /***********************************************************************
156 * GetSystemInfo [KERNEL32.@]
158 * Gets the current system information.
160 * On the first call it creates cached values, so it doesn't have to determine
161 * them repeatedly. On Linux, the /proc/cpuinfo special file is used.
163 * It creates a registry subhierarchy, looking like:
164 * \HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\
165 * Identifier (CPU x86)
166 * Note that there is a hierarchy for every processor installed, so this
167 * supports multiprocessor systems. This is done like Win95 does it, I think.
169 * It also creates a cached flag array for IsProcessorFeaturePresent().
171 * No NULL ptr check for LPSYSTEM_INFO in Win9x.
176 VOID WINAPI
GetSystemInfo(
177 LPSYSTEM_INFO si
/* [out] system information */
179 static int cache
= 0;
180 static SYSTEM_INFO cachedsi
;
183 memcpy(si
,&cachedsi
,sizeof(*si
));
186 memset(PF
,0,sizeof(PF
));
188 /* choose sensible defaults ...
189 * FIXME: perhaps overrideable with precompiler flags?
191 cachedsi
.u
.s
.wProcessorArchitecture
= PROCESSOR_ARCHITECTURE_INTEL
;
192 cachedsi
.dwPageSize
= getpagesize();
194 /* FIXME: the two entries below should be computed somehow... */
195 cachedsi
.lpMinimumApplicationAddress
= (void *)0x00010000;
196 cachedsi
.lpMaximumApplicationAddress
= (void *)0x7FFFFFFF;
197 cachedsi
.dwActiveProcessorMask
= 1;
198 cachedsi
.dwNumberOfProcessors
= 1;
199 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
200 cachedsi
.dwAllocationGranularity
= 0x10000;
201 cachedsi
.wProcessorLevel
= 5; /* 586 */
202 cachedsi
.wProcessorRevision
= 0;
204 cache
= 1; /* even if there is no more info, we now have a cacheentry */
205 memcpy(si
,&cachedsi
,sizeof(*si
));
207 /* Hmm, reasonable processor feature defaults? */
212 FILE *f
= fopen ("/proc/cpuinfo", "r");
216 while (fgets(line
,200,f
)!=NULL
) {
219 /* NOTE: the ':' is the only character we can rely on */
220 if (!(value
= strchr(line
,':')))
222 /* terminate the valuename */
224 /* skip any leading spaces */
225 while (*value
==' ') value
++;
226 if ((s
=strchr(value
,'\n')))
230 if (!strncasecmp(line
, "cpu family",strlen("cpu family"))) {
231 if (isdigit (value
[0])) {
232 switch (value
[0] - '0') {
233 case 3: cachedsi
.dwProcessorType
= PROCESSOR_INTEL_386
;
234 cachedsi
.wProcessorLevel
= 3;
236 case 4: cachedsi
.dwProcessorType
= PROCESSOR_INTEL_486
;
237 cachedsi
.wProcessorLevel
= 4;
240 case 6: /* PPro/2/3 has same info as P1 */
241 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
242 cachedsi
.wProcessorLevel
= 5;
244 case 1: /* two-figure levels */
247 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
248 cachedsi
.wProcessorLevel
= 5;
253 FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value
);
260 if (!strncasecmp(line
, "cpu",strlen("cpu"))) {
261 if ( isdigit (value
[0]) && value
[1] == '8' &&
262 value
[2] == '6' && value
[3] == 0
264 switch (value
[0] - '0') {
265 case 3: cachedsi
.dwProcessorType
= PROCESSOR_INTEL_386
;
266 cachedsi
.wProcessorLevel
= 3;
268 case 4: cachedsi
.dwProcessorType
= PROCESSOR_INTEL_486
;
269 cachedsi
.wProcessorLevel
= 4;
272 case 6: /* PPro/2/3 has same info as P1 */
273 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
274 cachedsi
.wProcessorLevel
= 5;
277 FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value
);
283 if (!strncasecmp(line
,"fdiv_bug",strlen("fdiv_bug"))) {
284 if (!strncasecmp(value
,"yes",3))
285 PF
[PF_FLOATING_POINT_PRECISION_ERRATA
] = TRUE
;
289 if (!strncasecmp(line
,"fpu",strlen("fpu"))) {
290 if (!strncasecmp(value
,"no",2))
291 PF
[PF_FLOATING_POINT_EMULATED
] = TRUE
;
295 if (!strncasecmp(line
,"processor",strlen("processor"))) {
296 /* processor number counts up... */
299 if (sscanf(value
,"%d",&x
))
300 if (x
+1>cachedsi
.dwNumberOfProcessors
)
301 cachedsi
.dwNumberOfProcessors
=x
+1;
303 if (!strncasecmp(line
,"stepping",strlen("stepping"))) {
306 if (sscanf(value
,"%d",&x
))
307 cachedsi
.wProcessorRevision
= x
;
309 if ( !strncasecmp(line
,"flags",strlen("flags")) ||
310 !strncasecmp(line
,"features",strlen("features"))
312 if (strstr(value
,"cx8"))
313 PF
[PF_COMPARE_EXCHANGE_DOUBLE
] = TRUE
;
314 if (strstr(value
,"mmx"))
315 PF
[PF_MMX_INSTRUCTIONS_AVAILABLE
] = TRUE
;
316 if (strstr(value
,"tsc"))
317 PF
[PF_RDTSC_INSTRUCTION_AVAILABLE
] = TRUE
;
323 memcpy(si
,&cachedsi
,sizeof(*si
));
324 #elif defined (__NetBSD__)
330 FILE *f
= fopen ("/var/run/dmesg.boot", "r");
332 /* first deduce as much as possible from the sysctls */
333 mib
[0] = CTL_MACHDEP
;
334 #ifdef CPU_FPU_PRESENT
335 mib
[1] = CPU_FPU_PRESENT
;
336 value
[1] = sizeof(int);
337 if (sysctl(mib
, 2, value
, value
+1, NULL
, 0) >= 0)
338 if (value
) PF
[PF_FLOATING_POINT_EMULATED
] = FALSE
;
339 else PF
[PF_FLOATING_POINT_EMULATED
] = TRUE
;
342 mib
[1] = CPU_SSE
; /* this should imply MMX */
343 value
[1] = sizeof(int);
344 if (sysctl(mib
, 2, value
, value
+1, NULL
, 0) >= 0)
345 if (value
) PF
[PF_MMX_INSTRUCTIONS_AVAILABLE
] = TRUE
;
348 mib
[1] = CPU_SSE2
; /* this should imply MMX */
349 value
[1] = sizeof(int);
350 if (sysctl(mib
, 2, value
, value
+1, NULL
, 0) >= 0)
351 if (value
) PF
[PF_MMX_INSTRUCTIONS_AVAILABLE
] = TRUE
;
355 value
[1] = sizeof(int);
356 if (sysctl(mib
, 2, value
, value
+1, NULL
, 0) >= 0)
357 if (value
[0] > cachedsi
.dwNumberOfProcessors
)
358 cachedsi
.dwNumberOfProcessors
= value
[0];
361 if (sysctl(mib
, 2, model
, value
+1, NULL
, 0) >= 0) {
362 model
[value
[1]] = '\0'; /* just in case */
363 cpuclass
= strstr(model
, "-class");
364 if (cpuclass
!= NULL
) {
365 while(cpuclass
> model
&& cpuclass
[0] != '(') cpuclass
--;
366 if (!strncmp(cpuclass
+1, "386", 3)) {
367 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_386
;
368 cachedsi
.wProcessorLevel
= 3;
370 if (!strncmp(cpuclass
+1, "486", 3)) {
371 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_486
;
372 cachedsi
.wProcessorLevel
= 4;
374 if (!strncmp(cpuclass
+1, "586", 3)) {
375 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
376 cachedsi
.wProcessorLevel
= 5;
378 if (!strncmp(cpuclass
+1, "686", 3)) {
379 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
380 cachedsi
.wProcessorLevel
= 5;
381 /* this should imply MMX */
382 PF
[PF_MMX_INSTRUCTIONS_AVAILABLE
] = TRUE
;
387 /* it may be worth reading from /var/run/dmesg.boot for
388 additional information such as CX8, MMX and TSC
389 (however this information should be considered less
390 reliable than that from the sysctl calls) */
393 while (fgets(model
, 255, f
) != NULL
) {
394 if (sscanf(model
,"cpu%d: features %x<", value
, value
+1) == 2) {
395 /* we could scan the string but it is easier
396 to test the bits directly */
398 PF
[PF_FLOATING_POINT_EMULATED
] = TRUE
;
400 PF
[PF_RDTSC_INSTRUCTION_AVAILABLE
] = TRUE
;
401 if (value
[1] & 0x100)
402 PF
[PF_COMPARE_EXCHANGE_DOUBLE
] = TRUE
;
403 if (value
[1] & 0x800000)
404 PF
[PF_MMX_INSTRUCTIONS_AVAILABLE
] = TRUE
;
413 memcpy(si
,&cachedsi
,sizeof(*si
));
414 #elif defined(__FreeBSD__)
416 unsigned int regs
[4], regs2
[4];
419 regs
[0] = 0; /* No cpuid support -- skip the rest */
421 do_cpuid(0x00000000, regs
); /* get standard cpuid level and vendor name */
422 if (regs
[0]>=0x00000001) { /* Check for supported cpuid version */
423 do_cpuid(0x00000001, regs2
); /* get cpu features */
424 switch ((regs2
[0] >> 8)&0xf) { /* cpu family */
425 case 3: cachedsi
.dwProcessorType
= PROCESSOR_INTEL_386
;
426 cachedsi
.wProcessorLevel
= 3;
428 case 4: cachedsi
.dwProcessorType
= PROCESSOR_INTEL_486
;
429 cachedsi
.wProcessorLevel
= 4;
432 case 6: /* PPro/2/3 has same info as P1 */
433 cachedsi
.dwProcessorType
= PROCESSOR_INTEL_PENTIUM
;
434 cachedsi
.wProcessorLevel
= 5;
437 FIXME("unknown cpu family %d, please report! (-> setting to 386)\n", \
438 (regs2
[0] >> 8)&0xf);
441 PF
[PF_FLOATING_POINT_EMULATED
] = !(regs2
[3] & 1);
442 PF
[PF_RDTSC_INSTRUCTION_AVAILABLE
] = (regs2
[3] & (1 << 4 )) >> 4;
443 PF
[PF_COMPARE_EXCHANGE_DOUBLE
] = (regs2
[3] & (1 << 8 )) >> 8;
444 PF
[PF_MMX_INSTRUCTIONS_AVAILABLE
] = (regs2
[3] & (1 << 23)) >> 23;
445 /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
447 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
449 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
451 if (regs
[1] == AUTH
&&
454 do_cpuid(0x80000000, regs
); /* get vendor cpuid level */
455 if (regs
[0]>=0x80000001) {
456 do_cpuid(0x80000001, regs2
); /* get vendor features */
457 PF
[PF_AMD3D_INSTRUCTIONS_AVAILABLE
] =
458 (regs2
[3] & (1 << 31 )) >> 31;
463 ret
= sysctlbyname("hw.ncpu", &num
, &len
, NULL
, 0);
465 cachedsi
.dwNumberOfProcessors
= num
;
467 memcpy(si
,&cachedsi
,sizeof(*si
));
469 FIXME("not yet supported on this system\n");
471 TRACE("<- CPU arch %d, res'd %d, pagesize %ld, minappaddr %p, maxappaddr %p,"
472 " act.cpumask %08lx, numcpus %ld, CPU type %ld, allocgran. %ld, CPU level %d, CPU rev %d\n",
473 si
->u
.s
.wProcessorArchitecture
, si
->u
.s
.wReserved
, si
->dwPageSize
,
474 si
->lpMinimumApplicationAddress
, si
->lpMaximumApplicationAddress
,
475 si
->dwActiveProcessorMask
, si
->dwNumberOfProcessors
, si
->dwProcessorType
,
476 si
->dwAllocationGranularity
, si
->wProcessorLevel
, si
->wProcessorRevision
);
478 create_registry_keys( &cachedsi
);
482 /***********************************************************************
483 * IsProcessorFeaturePresent [KERNEL32.@]
485 * TRUE if processor feature present
488 BOOL WINAPI
IsProcessorFeaturePresent (
489 DWORD feature
/* [in] feature number, see PF_ defines */
492 GetSystemInfo (&si
); /* To ensure the information is loaded and cached */