push 0661da64275a0e67e1a41492b8d7716e364e773c
[wine/hacks.git] / dlls / kernel32 / cpu.c
blob63ca2774e968be96b1e488c479ab85cf790388de
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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
34 #ifdef HAVE_MACH_MACHINE_H
35 # include <mach/machine.h>
36 #endif
38 #include <ctype.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
45 #endif
48 #define NONAMELESSUNION
49 #define NONAMELESSSTRUCT
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winnt.h"
53 #include "winternl.h"
54 #include "wine/unicode.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(reg);
59 #define AUTH 0x68747541 /* "Auth" */
60 #define ENTI 0x69746e65 /* "enti" */
61 #define CAMD 0x444d4163 /* "cAMD" */
63 /* Calls cpuid with an eax of 'ax' and returns the 16 bytes in *p
64 * We are compiled with -fPIC, so we can't clobber ebx.
66 static inline void do_cpuid(unsigned int ax, unsigned int *p)
68 #ifdef __i386__
69 __asm__("pushl %%ebx\n\t"
70 "cpuid\n\t"
71 "movl %%ebx, %%esi\n\t"
72 "popl %%ebx"
73 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
74 : "0" (ax));
75 #endif
78 /* From xf86info havecpuid.c 1.11 */
79 static inline int have_cpuid(void)
81 #ifdef __i386__
82 unsigned int f1, f2;
83 __asm__("pushfl\n\t"
84 "pushfl\n\t"
85 "popl %0\n\t"
86 "movl %0,%1\n\t"
87 "xorl %2,%0\n\t"
88 "pushl %0\n\t"
89 "popfl\n\t"
90 "pushfl\n\t"
91 "popl %0\n\t"
92 "popfl"
93 : "=&r" (f1), "=&r" (f2)
94 : "ir" (0x00200000));
95 return ((f1^f2) & 0x00200000) != 0;
96 #else
97 return 0;
98 #endif
101 static BYTE PF[64] = {0,};
102 static ULONGLONG cpuHz = 1000000000; /* default to a 1GHz */
104 static void create_system_registry_keys( const SYSTEM_INFO *info )
106 static const WCHAR SystemW[] = {'M','a','c','h','i','n','e','\\',
107 'H','a','r','d','w','a','r','e','\\',
108 'D','e','s','c','r','i','p','t','i','o','n','\\',
109 'S','y','s','t','e','m',0};
110 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};
111 static const WCHAR cpuW[] = {'C','e','n','t','r','a','l','P','r','o','c','e','s','s','o','r',0};
112 static const WCHAR IdentifierW[] = {'I','d','e','n','t','i','f','i','e','r',0};
113 static const WCHAR SysidW[] = {'A','T',' ','c','o','m','p','a','t','i','b','l','e',0};
114 static const WCHAR mhzKeyW[] = {'~','M','H','z',0};
115 static const WCHAR VendorIdentifierW[] = {'V','e','n','d','o','r','I','d','e','n','t','i','f','i','e','r',0};
116 static const WCHAR VenidIntelW[] = {'G','e','n','u','i','n','e','I','n','t','e','l',0};
117 /* static const WCHAR VenidAMDW[] = {'A','u','t','h','e','n','t','i','c','A','M','D',0}; */
119 unsigned int i;
120 HANDLE hkey, system_key, cpu_key;
121 OBJECT_ATTRIBUTES attr;
122 UNICODE_STRING nameW, valueW;
124 attr.Length = sizeof(attr);
125 attr.RootDirectory = 0;
126 attr.ObjectName = &nameW;
127 attr.Attributes = 0;
128 attr.SecurityDescriptor = NULL;
129 attr.SecurityQualityOfService = NULL;
131 RtlInitUnicodeString( &nameW, SystemW );
132 if (NtCreateKey( &system_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
134 RtlInitUnicodeString( &valueW, IdentifierW );
135 NtSetValueKey( system_key, &valueW, 0, REG_SZ, SysidW, sizeof(SysidW) );
137 attr.RootDirectory = system_key;
138 RtlInitUnicodeString( &nameW, fpuW );
139 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
141 RtlInitUnicodeString( &nameW, cpuW );
142 if (!NtCreateKey( &cpu_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
144 for (i = 0; i < info->dwNumberOfProcessors; i++)
146 char num[10], id[60];
148 attr.RootDirectory = cpu_key;
149 sprintf( num, "%d", i );
150 RtlCreateUnicodeStringFromAsciiz( &nameW, num );
151 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
153 WCHAR idW[60];
154 DWORD sizeW;
155 DWORD cpuMHz = cpuHz / 1000000;
157 /*TODO: report 64bit processors properly*/
158 RtlInitUnicodeString( &valueW, IdentifierW );
159 sprintf( id, "x86 Family %d Model %d Stepping %d",
160 info->wProcessorLevel, HIBYTE(info->wProcessorRevision), LOBYTE(info->wProcessorRevision) );
162 RtlMultiByteToUnicodeN( idW, sizeof(idW), &sizeW, id, strlen(id)+1 );
163 NtSetValueKey( hkey, &valueW, 0, REG_SZ, idW, sizeW );
165 /*TODO; report amd's properly*/
166 RtlInitUnicodeString( &valueW, VendorIdentifierW );
167 NtSetValueKey( hkey, &valueW, 0, REG_SZ, VenidIntelW, sizeof(VenidIntelW) );
169 RtlInitUnicodeString( &valueW, mhzKeyW );
170 NtSetValueKey( hkey, &valueW, 0, REG_DWORD, &cpuMHz, sizeof(DWORD) );
171 NtClose( hkey );
173 RtlFreeUnicodeString( &nameW );
175 NtClose( cpu_key );
177 NtClose( system_key );
180 static void create_env_registry_keys( const SYSTEM_INFO *info )
182 static const WCHAR EnvironW[] = {'M','a','c','h','i','n','e','\\',
183 'S','y','s','t','e','m','\\',
184 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
185 'C','o','n','t','r','o','l','\\',
186 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
187 'E','n','v','i','r','o','n','m','e','n','t',0};
188 static const WCHAR NumProcW[] = {'N','U','M','B','E','R','_','O','F','_','P','R','O','C','E','S','S','O','R','S',0};
189 static const WCHAR ProcArchW[] = {'P','R','O','C','E','S','S','O','R','_','A','R','C','H','I','T','E','C','T','U','R','E',0};
190 static const WCHAR x86W[] = {'x','8','6',0};
191 static const WCHAR ProcIdW[] = {'P','R','O','C','E','S','S','O','R','_','I','D','E','N','T','I','F','I','E','R',0};
192 static const WCHAR ProcLvlW[] = {'P','R','O','C','E','S','S','O','R','_','L','E','V','E','L',0};
193 static const WCHAR ProcRevW[] = {'P','R','O','C','E','S','S','O','R','_','R','E','V','I','S','I','O','N',0};
195 HANDLE env_key;
196 OBJECT_ATTRIBUTES attr;
197 UNICODE_STRING nameW, valueW;
198 DWORD sizeW;
200 char nProc[10],id[60],procLevel[10],rev[10];
201 WCHAR nProcW[10],idW[60],procLevelW[10],revW[10];
203 /* Create some keys under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.
204 * All these environment variables are processor related and will be read during process initialization and hence
205 * show up in the environment of that process.
208 attr.Length = sizeof(attr);
209 attr.RootDirectory = 0;
210 attr.ObjectName = &nameW;
211 attr.Attributes = 0;
212 attr.SecurityDescriptor = NULL;
213 attr.SecurityQualityOfService = NULL;
215 RtlInitUnicodeString( &nameW, EnvironW );
216 if (NtCreateKey( &env_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
218 sprintf( nProc, "%d", info->dwNumberOfProcessors );
219 RtlMultiByteToUnicodeN( nProcW, sizeof(nProcW), &sizeW, nProc, strlen(nProc)+1 );
220 RtlInitUnicodeString( &valueW, NumProcW );
221 NtSetValueKey( env_key, &valueW, 0, REG_SZ, nProcW, sizeW );
223 /* TODO: currently hardcoded x86, add different processors */
224 RtlInitUnicodeString( &valueW, ProcArchW );
225 NtSetValueKey( env_key, &valueW, 0, REG_SZ, x86W, sizeof(x86W) );
227 /* TODO: currently hardcoded Intel, add different processors */
228 sprintf( id, "x86 Family %d Model %d Stepping %d, GenuineIntel",
229 info->wProcessorLevel, HIBYTE(info->wProcessorRevision), LOBYTE(info->wProcessorRevision) );
230 RtlMultiByteToUnicodeN( idW, sizeof(idW), &sizeW, id, strlen(id)+1 );
231 RtlInitUnicodeString( &valueW, ProcIdW );
232 NtSetValueKey( env_key, &valueW, 0, REG_SZ, idW, sizeW );
234 sprintf( procLevel, "%d", info->wProcessorLevel );
235 RtlMultiByteToUnicodeN( procLevelW, sizeof(procLevelW), &sizeW, procLevel, strlen(procLevel)+1 );
236 RtlInitUnicodeString( &valueW, ProcLvlW );
237 NtSetValueKey( env_key, &valueW, 0, REG_SZ, procLevelW, sizeW );
239 /* Properly report model/stepping */
240 sprintf( rev, "%04x", info->wProcessorRevision);
241 RtlMultiByteToUnicodeN( revW, sizeof(revW), &sizeW, rev, strlen(rev)+1 );
242 RtlInitUnicodeString( &valueW, ProcRevW );
243 NtSetValueKey( env_key, &valueW, 0, REG_SZ, revW, sizeW );
245 NtClose( env_key );
248 static inline void get_cpuinfo( SYSTEM_INFO *info )
250 unsigned int regs[4], regs2[4];
252 if (!have_cpuid()) return;
254 do_cpuid(0x00000000, regs); /* get standard cpuid level and vendor name */
255 if (regs[0]>=0x00000001) /* Check for supported cpuid version */
257 do_cpuid(0x00000001, regs2); /* get cpu features */
258 switch ((regs2[0] >> 8) & 0xf) /* cpu family */
260 case 3:
261 info->dwProcessorType = PROCESSOR_INTEL_386;
262 info->wProcessorLevel = 3;
263 break;
264 case 4:
265 info->dwProcessorType = PROCESSOR_INTEL_486;
266 info->wProcessorLevel = 4;
267 break;
268 case 5:
269 info->dwProcessorType = PROCESSOR_INTEL_PENTIUM;
270 info->wProcessorLevel = 5;
271 break;
272 case 6:
273 case 15: /* PPro/2/3/4 has same info as P1 */
274 info->dwProcessorType = PROCESSOR_INTEL_PENTIUM;
275 info->wProcessorLevel = 6;
276 break;
277 default:
278 FIXME("unknown cpu family %d, please report! (-> setting to 386)\n",
279 (regs2[0] >> 8)&0xf);
280 break;
282 PF[PF_FLOATING_POINT_EMULATED] = !(regs2[3] & 1);
283 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] & (1 << 4 )) >> 4;
284 PF[PF_COMPARE_EXCHANGE_DOUBLE] = (regs2[3] & (1 << 8 )) >> 8;
285 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 23)) >> 23;
287 if (regs[1] == AUTH &&
288 regs[3] == ENTI &&
289 regs[2] == CAMD) {
290 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
291 if (regs[0]>=0x80000001) {
292 do_cpuid(0x80000001, regs2); /* get vendor features */
293 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 31 )) >> 31;
299 /****************************************************************************
300 * QueryPerformanceCounter (KERNEL32.@)
302 * Get the current value of the performance counter.
304 * PARAMS
305 * counter [O] Destination for the current counter reading
307 * RETURNS
308 * Success: TRUE. counter contains the current reading
309 * Failure: FALSE.
311 * SEE ALSO
312 * See QueryPerformanceFrequency.
314 BOOL WINAPI QueryPerformanceCounter(PLARGE_INTEGER counter)
316 NtQueryPerformanceCounter( counter, NULL );
317 return TRUE;
321 /****************************************************************************
322 * QueryPerformanceFrequency (KERNEL32.@)
324 * Get the resolution of the performance counter.
326 * PARAMS
327 * frequency [O] Destination for the counter resolution
329 * RETURNS
330 * Success. TRUE. Frequency contains the resolution of the counter.
331 * Failure: FALSE.
333 * SEE ALSO
334 * See QueryPerformanceCounter.
336 BOOL WINAPI QueryPerformanceFrequency(PLARGE_INTEGER frequency)
338 LARGE_INTEGER counter;
339 NtQueryPerformanceCounter( &counter, frequency );
340 return TRUE;
344 /***********************************************************************
345 * GetSystemInfo [KERNEL32.@]
347 * Get information about the system.
349 * RETURNS
350 * Nothing.
352 * NOTES
353 * On the first call it creates cached values, so it doesn't have to determine
354 * them repeatedly. On Linux, the "/proc/cpuinfo" special file is used.
356 * It creates a registry subhierarchy, looking like:
357 * "\HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\Identifier (CPU x86)".
358 * Note that there is a hierarchy for every processor installed, so this
359 * supports multiprocessor systems. This is done like Win95 does it, I think.
361 * It creates some registry entries in the environment part:
362 * "\HKLM\System\CurrentControlSet\Control\Session Manager\Environment". These are
363 * always present. When deleted, Windows will add them again.
365 * It also creates a cached flag array for IsProcessorFeaturePresent().
367 VOID WINAPI GetSystemInfo(
368 LPSYSTEM_INFO si /* [out] Destination for system information, may not be NULL */)
370 static int cache = 0;
371 static SYSTEM_INFO cachedsi;
372 SYSTEM_BASIC_INFORMATION sbi;
374 TRACE("si=0x%p\n", si);
375 if (cache) {
376 *si = cachedsi;
377 return;
379 memset(PF,0,sizeof(PF));
381 NtQuerySystemInformation( SystemBasicInformation, &sbi, sizeof(sbi), NULL );
382 cachedsi.dwPageSize = sbi.uPageSize;
383 cachedsi.lpMinimumApplicationAddress = sbi.pLowestUserAddress;
384 cachedsi.lpMaximumApplicationAddress = sbi.pMmHighestUserAddress;
385 cachedsi.dwNumberOfProcessors = sbi.uKeActiveProcessors;
386 cachedsi.dwAllocationGranularity = sbi.uAllocationGranularity;
388 /* choose sensible defaults ...
389 * FIXME: perhaps overridable with precompiler flags?
391 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
392 cachedsi.dwActiveProcessorMask = 0;
393 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
394 cachedsi.wProcessorLevel = 5; /* 586 */
395 cachedsi.wProcessorRevision = 0;
397 cache = 1; /* even if there is no more info, we now have a cache entry */
398 *si = cachedsi;
400 /* Hmm, reasonable processor feature defaults? */
402 #ifdef linux
404 char line[200];
405 FILE *f = fopen ("/proc/cpuinfo", "r");
407 if (!f)
408 return;
409 while (fgets(line,200,f)!=NULL) {
410 char *s,*value;
412 /* NOTE: the ':' is the only character we can rely on */
413 if (!(value = strchr(line,':')))
414 continue;
416 /* terminate the valuename */
417 s = value - 1;
418 while ((s >= line) && ((*s == ' ') || (*s == '\t'))) s--;
419 *(s + 1) = '\0';
421 /* and strip leading spaces from value */
422 value += 1;
423 while (*value==' ') value++;
424 if ((s=strchr(value,'\n')))
425 *s='\0';
427 if (!strcasecmp(line,"processor")) {
428 /* processor number counts up... */
429 unsigned int x;
431 if (sscanf(value,"%d",&x))
432 if (x+1>cachedsi.dwNumberOfProcessors)
433 cachedsi.dwNumberOfProcessors=x+1;
435 continue;
437 if (!strcasecmp(line,"model")) {
438 /* First part of wProcessorRevision */
439 int x;
441 if (sscanf(value,"%d",&x))
442 cachedsi.wProcessorRevision = cachedsi.wProcessorRevision | (x << 8);
444 continue;
447 /* 2.1 method */
448 if (!strcasecmp(line, "cpu family")) {
449 if (isdigit (value[0])) {
450 switch (value[0] - '0') {
451 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
452 cachedsi.wProcessorLevel= 3;
453 break;
454 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
455 cachedsi.wProcessorLevel= 4;
456 break;
457 case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
458 cachedsi.wProcessorLevel= 5;
459 break;
461 default:
462 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
463 cachedsi.wProcessorLevel = atoi(value);
464 break;
467 continue;
469 /* old 2.0 method */
470 if (!strcasecmp(line, "cpu")) {
471 if ( isdigit (value[0]) && value[1] == '8' &&
472 value[2] == '6' && value[3] == 0
474 switch (value[0] - '0') {
475 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
476 cachedsi.wProcessorLevel= 3;
477 break;
478 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
479 cachedsi.wProcessorLevel= 4;
480 break;
481 case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
482 cachedsi.wProcessorLevel= 5;
483 break;
484 case 6: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
485 cachedsi.wProcessorLevel= 6;
486 break;
487 default:
488 FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
489 break;
492 continue;
494 if (!strcasecmp(line,"stepping")) {
495 /* Second part of wProcessorRevision */
496 int x;
498 if (sscanf(value,"%d",&x))
499 cachedsi.wProcessorRevision = cachedsi.wProcessorRevision | x;
501 continue;
503 if (!strcasecmp(line, "cpu MHz")) {
504 double cmz;
505 if (sscanf( value, "%lf", &cmz ) == 1) {
506 /* SYSTEMINFO doesn't have a slot for cpu speed, so store in a global */
507 cpuHz = cmz * 1000 * 1000;
509 continue;
511 if (!strcasecmp(line,"fdiv_bug")) {
512 if (!strncasecmp(value,"yes",3))
513 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
515 continue;
517 if (!strcasecmp(line,"fpu")) {
518 if (!strncasecmp(value,"no",2))
519 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
521 continue;
523 if ( !strcasecmp(line,"flags") ||
524 !strcasecmp(line,"features")) {
525 if (strstr(value,"cx8"))
526 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
527 if (strstr(value,"mmx"))
528 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
529 if (strstr(value,"tsc"))
530 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
531 if (strstr(value,"3dnow"))
532 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
533 /* This will also catch sse2, but we have sse itself
534 * if we have sse2, so no problem */
535 if (strstr(value,"sse"))
536 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
537 if (strstr(value,"sse2"))
538 PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
539 if (strstr(value,"pae"))
540 PF[PF_PAE_ENABLED] = TRUE;
542 continue;
545 fclose (f);
547 #elif defined (__NetBSD__)
549 int mib[2];
550 int value[2];
551 char model[256];
552 char *cpuclass;
553 FILE *f = fopen ("/var/run/dmesg.boot", "r");
555 /* first deduce as much as possible from the sysctls */
556 mib[0] = CTL_MACHDEP;
557 #ifdef CPU_FPU_PRESENT
558 mib[1] = CPU_FPU_PRESENT;
559 value[1] = sizeof(int);
560 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
561 if (value) PF[PF_FLOATING_POINT_EMULATED] = FALSE;
562 else PF[PF_FLOATING_POINT_EMULATED] = TRUE;
563 #endif
564 #ifdef CPU_SSE
565 mib[1] = CPU_SSE; /* this should imply MMX */
566 value[1] = sizeof(int);
567 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
568 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
569 #endif
570 #ifdef CPU_SSE2
571 mib[1] = CPU_SSE2; /* this should imply MMX */
572 value[1] = sizeof(int);
573 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
574 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
575 #endif
576 mib[0] = CTL_HW;
577 mib[1] = HW_NCPU;
578 value[1] = sizeof(int);
579 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
580 if (value[0] > cachedsi.dwNumberOfProcessors)
581 cachedsi.dwNumberOfProcessors = value[0];
582 mib[1] = HW_MODEL;
583 value[1] = 255;
584 if (sysctl(mib, 2, model, value+1, NULL, 0) >= 0) {
585 model[value[1]] = '\0'; /* just in case */
586 cpuclass = strstr(model, "-class");
587 if (cpuclass != NULL) {
588 while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
589 if (!strncmp(cpuclass+1, "386", 3)) {
590 cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
591 cachedsi.wProcessorLevel= 3;
593 if (!strncmp(cpuclass+1, "486", 3)) {
594 cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
595 cachedsi.wProcessorLevel= 4;
597 if (!strncmp(cpuclass+1, "586", 3)) {
598 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
599 cachedsi.wProcessorLevel= 5;
601 if (!strncmp(cpuclass+1, "686", 3)) {
602 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
603 cachedsi.wProcessorLevel= 6;
604 /* this should imply MMX */
605 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
610 /* it may be worth reading from /var/run/dmesg.boot for
611 additional information such as CX8, MMX and TSC
612 (however this information should be considered less
613 reliable than that from the sysctl calls) */
614 if (f != NULL)
616 while (fgets(model, 255, f) != NULL) {
617 if (sscanf(model,"cpu%d: features %x<", value, value+1) == 2) {
618 /* we could scan the string but it is easier
619 to test the bits directly */
620 if (value[1] & 0x1)
621 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
622 if (value[1] & 0x10)
623 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
624 if (value[1] & 0x100)
625 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
626 if (value[1] & 0x800000)
627 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
629 break;
632 fclose(f);
636 #elif defined(__FreeBSD__)
638 int ret, num;
639 unsigned len;
641 get_cpuinfo( &cachedsi );
643 /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
644 /*len = sizeof(num);
645 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
646 if (!ret)
647 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
649 len = sizeof(num);
650 ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
651 if (!ret)
652 cachedsi.dwNumberOfProcessors = num;
654 #elif defined(__sun)
656 int num = sysconf( _SC_NPROCESSORS_ONLN );
658 if (num == -1) num = 1;
659 get_cpuinfo( &cachedsi );
660 cachedsi.dwNumberOfProcessors = num;
662 #elif defined (__APPLE__)
664 size_t valSize;
665 unsigned long long longVal;
666 int value;
667 int cputype;
668 char buffer[256];
670 valSize = sizeof(int);
671 if (sysctlbyname ("hw.optional.floatingpoint", &value, &valSize, NULL, 0) == 0)
673 if (value)
674 PF[PF_FLOATING_POINT_EMULATED] = FALSE;
675 else
676 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
678 valSize = sizeof(int);
679 if (sysctlbyname ("hw.ncpu", &value, &valSize, NULL, 0) == 0)
680 cachedsi.dwNumberOfProcessors = value;
682 valSize = sizeof(int);
683 if (sysctlbyname ("hw.activecpu", &value, &valSize, NULL, 0) == 0)
684 cachedsi.dwActiveProcessorMask = (1 << value) - 1;
686 valSize = sizeof(int);
687 if (sysctlbyname ("hw.cputype", &cputype, &valSize, NULL, 0) == 0)
689 switch (cputype)
691 case CPU_TYPE_POWERPC:
692 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_PPC;
693 valSize = sizeof(int);
694 if (sysctlbyname ("hw.cpusubtype", &value, &valSize, NULL, 0) == 0)
696 switch (value)
698 case CPU_SUBTYPE_POWERPC_601:
699 case CPU_SUBTYPE_POWERPC_602:
700 cachedsi.dwProcessorType = PROCESSOR_PPC_601;
701 cachedsi.wProcessorLevel = 1;
702 break;
703 case CPU_SUBTYPE_POWERPC_603:
704 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
705 cachedsi.wProcessorLevel = 3;
706 break;
707 case CPU_SUBTYPE_POWERPC_603e:
708 case CPU_SUBTYPE_POWERPC_603ev:
709 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
710 cachedsi.wProcessorLevel = 6;
711 break;
712 case CPU_SUBTYPE_POWERPC_604:
713 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
714 cachedsi.wProcessorLevel = 4;
715 break;
716 case CPU_SUBTYPE_POWERPC_604e:
717 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
718 cachedsi.wProcessorLevel = 9;
719 break;
720 case CPU_SUBTYPE_POWERPC_620:
721 cachedsi.dwProcessorType = PROCESSOR_PPC_620;
722 cachedsi.wProcessorLevel = 20;
723 break;
724 case CPU_SUBTYPE_POWERPC_750:
725 case CPU_SUBTYPE_POWERPC_7400:
726 case CPU_SUBTYPE_POWERPC_7450:
727 /* G3/G4 derive from 603 so ... */
728 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
729 cachedsi.wProcessorLevel = 6;
730 break;
731 case CPU_SUBTYPE_POWERPC_970:
732 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
733 cachedsi.wProcessorLevel = 9;
734 /* :o) PF[PF_ALTIVEC_INSTRUCTIONS_AVAILABLE] ;-) */
735 break;
736 default: break;
739 break; /* CPU_TYPE_POWERPC */
740 case CPU_TYPE_I386:
741 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
742 valSize = sizeof(int);
743 if (sysctlbyname ("machdep.cpu.family", &value, &valSize, NULL, 0) == 0)
745 cachedsi.wProcessorLevel = value;
746 switch (value)
748 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386; break;
749 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486; break;
750 default: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM; break;
753 valSize = sizeof(int);
754 if (sysctlbyname ("machdep.cpu.model", &value, &valSize, NULL, 0) == 0)
755 cachedsi.wProcessorRevision = (value << 8);
756 valSize = sizeof(int);
757 if (sysctlbyname ("machdep.cpu.stepping", &value, &valSize, NULL, 0) == 0)
758 cachedsi.wProcessorRevision |= value;
759 valSize = sizeof(buffer);
760 if (sysctlbyname ("machdep.cpu.features", buffer, &valSize, NULL, 0) == 0)
762 cachedsi.wProcessorRevision |= value;
763 if (strstr(buffer,"CX8")) PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
764 if (strstr(buffer,"MMX")) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
765 if (strstr(buffer,"TSC")) PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
766 if (strstr(buffer,"3DNOW")) PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
767 if (strstr(buffer,"SSE")) PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
768 if (strstr(buffer,"SSE2")) PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
769 if (strstr(buffer,"PAE")) PF[PF_PAE_ENABLED] = TRUE;
771 break; /* CPU_TYPE_I386 */
772 default: break;
773 } /* switch (cputype) */
775 valSize = sizeof(longVal);
776 if (!sysctlbyname("hw.cpufrequency", &longVal, &valSize, NULL, 0))
777 cpuHz = longVal;
779 #else
780 FIXME("not yet supported on this system\n");
781 #endif
782 if (!cachedsi.dwActiveProcessorMask)
783 cachedsi.dwActiveProcessorMask = (1 << cachedsi.dwNumberOfProcessors) - 1;
785 *si = cachedsi;
787 TRACE("<- CPU arch %d, res'd %d, pagesize %d, minappaddr %p, maxappaddr %p,"
788 " act.cpumask %08x, numcpus %d, CPU type %d, allocgran. %d, CPU level %d, CPU rev %d\n",
789 si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
790 si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
791 si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
792 si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
794 create_system_registry_keys( &cachedsi );
795 create_env_registry_keys( &cachedsi );
799 /***********************************************************************
800 * GetNativeSystemInfo [KERNEL32.@]
802 VOID WINAPI GetNativeSystemInfo(
803 LPSYSTEM_INFO si /* [out] Destination for system information, may not be NULL */)
805 static BOOL reported = FALSE;
806 if (!reported) {
807 FIXME("(%p) using GetSystemInfo()\n", si);
808 reported = TRUE;
809 } else
810 TRACE("(%p) using GetSystemInfo()\n", si);
811 GetSystemInfo(si);
814 /***********************************************************************
815 * IsProcessorFeaturePresent [KERNEL32.@]
817 * Determine if the cpu supports a given feature.
819 * RETURNS
820 * TRUE, If the processor supports feature,
821 * FALSE otherwise.
823 BOOL WINAPI IsProcessorFeaturePresent (
824 DWORD feature /* [in] Feature number, (PF_ constants from "winnt.h") */)
826 SYSTEM_INFO si;
827 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
829 if (feature < 64)
830 return PF[feature];
831 else
832 return FALSE;