msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / kernel32 / cpu.c
blobbc890c60397980a27b5ba3a73e879abe614e59bb
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;
551 size_t val_len;
552 char model[256];
553 char *cpuclass;
554 FILE *f = fopen ("/var/run/dmesg.boot", "r");
556 /* first deduce as much as possible from the sysctls */
557 mib[0] = CTL_MACHDEP;
558 #ifdef CPU_FPU_PRESENT
559 mib[1] = CPU_FPU_PRESENT;
560 val_len = sizeof(value);
561 if (sysctl(mib, 2, &value, &val_len, NULL, 0) >= 0)
562 PF[PF_FLOATING_POINT_EMULATED] = !value;
563 #endif
564 #ifdef CPU_SSE
565 mib[1] = CPU_SSE; /* this should imply MMX */
566 val_len = sizeof(value);
567 if (sysctl(mib, 2, &value, &val_len, 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 val_len = sizeof(value);
573 if (sysctl(mib, 2, &value, &val_len, 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 val_len = sizeof(value);
579 if (sysctl(mib, 2, &value, &val_len, NULL, 0) >= 0)
580 if (value > cachedsi.dwNumberOfProcessors)
581 cachedsi.dwNumberOfProcessors = value;
582 mib[1] = HW_MODEL;
583 val_len = sizeof(model)-1;
584 if (sysctl(mib, 2, model, &val_len, NULL, 0) >= 0) {
585 model[val_len] = '\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 int cpu, features;
618 if (sscanf(model,"cpu%d: features %x<", &cpu, &features) == 2) {
619 /* we could scan the string but it is easier
620 to test the bits directly */
621 if (features & 0x1)
622 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
623 if (features & 0x10)
624 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
625 if (features & 0x100)
626 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
627 if (features & 0x800000)
628 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
630 break;
633 fclose(f);
637 #elif defined(__FreeBSD__)
639 int ret, num;
640 size_t len;
642 get_cpuinfo( &cachedsi );
644 /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
645 /*len = sizeof(num);
646 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
647 if (!ret)
648 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
650 len = sizeof(num);
651 ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
652 if (!ret)
653 cachedsi.dwNumberOfProcessors = num;
655 #elif defined(__sun)
657 int num = sysconf( _SC_NPROCESSORS_ONLN );
659 if (num == -1) num = 1;
660 get_cpuinfo( &cachedsi );
661 cachedsi.dwNumberOfProcessors = num;
663 #elif defined (__APPLE__)
665 size_t valSize;
666 unsigned long long longVal;
667 int value;
668 int cputype;
669 char buffer[256];
671 valSize = sizeof(int);
672 if (sysctlbyname ("hw.optional.floatingpoint", &value, &valSize, NULL, 0) == 0)
674 if (value)
675 PF[PF_FLOATING_POINT_EMULATED] = FALSE;
676 else
677 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
679 valSize = sizeof(int);
680 if (sysctlbyname ("hw.ncpu", &value, &valSize, NULL, 0) == 0)
681 cachedsi.dwNumberOfProcessors = value;
683 valSize = sizeof(int);
684 if (sysctlbyname ("hw.activecpu", &value, &valSize, NULL, 0) == 0)
685 cachedsi.dwActiveProcessorMask = ((ULONG_PTR)1 << value) - 1;
687 valSize = sizeof(int);
688 if (sysctlbyname ("hw.cputype", &cputype, &valSize, NULL, 0) == 0)
690 switch (cputype)
692 case CPU_TYPE_POWERPC:
693 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_PPC;
694 valSize = sizeof(int);
695 if (sysctlbyname ("hw.cpusubtype", &value, &valSize, NULL, 0) == 0)
697 switch (value)
699 case CPU_SUBTYPE_POWERPC_601:
700 case CPU_SUBTYPE_POWERPC_602:
701 cachedsi.dwProcessorType = PROCESSOR_PPC_601;
702 cachedsi.wProcessorLevel = 1;
703 break;
704 case CPU_SUBTYPE_POWERPC_603:
705 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
706 cachedsi.wProcessorLevel = 3;
707 break;
708 case CPU_SUBTYPE_POWERPC_603e:
709 case CPU_SUBTYPE_POWERPC_603ev:
710 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
711 cachedsi.wProcessorLevel = 6;
712 break;
713 case CPU_SUBTYPE_POWERPC_604:
714 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
715 cachedsi.wProcessorLevel = 4;
716 break;
717 case CPU_SUBTYPE_POWERPC_604e:
718 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
719 cachedsi.wProcessorLevel = 9;
720 break;
721 case CPU_SUBTYPE_POWERPC_620:
722 cachedsi.dwProcessorType = PROCESSOR_PPC_620;
723 cachedsi.wProcessorLevel = 20;
724 break;
725 case CPU_SUBTYPE_POWERPC_750:
726 case CPU_SUBTYPE_POWERPC_7400:
727 case CPU_SUBTYPE_POWERPC_7450:
728 /* G3/G4 derive from 603 so ... */
729 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
730 cachedsi.wProcessorLevel = 6;
731 break;
732 case CPU_SUBTYPE_POWERPC_970:
733 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
734 cachedsi.wProcessorLevel = 9;
735 /* :o) PF[PF_ALTIVEC_INSTRUCTIONS_AVAILABLE] ;-) */
736 break;
737 default: break;
740 break; /* CPU_TYPE_POWERPC */
741 case CPU_TYPE_I386:
742 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
743 valSize = sizeof(int);
744 if (sysctlbyname ("machdep.cpu.family", &value, &valSize, NULL, 0) == 0)
746 cachedsi.wProcessorLevel = value;
747 switch (value)
749 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386; break;
750 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486; break;
751 default: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM; break;
754 valSize = sizeof(int);
755 if (sysctlbyname ("machdep.cpu.model", &value, &valSize, NULL, 0) == 0)
756 cachedsi.wProcessorRevision = (value << 8);
757 valSize = sizeof(int);
758 if (sysctlbyname ("machdep.cpu.stepping", &value, &valSize, NULL, 0) == 0)
759 cachedsi.wProcessorRevision |= value;
760 valSize = sizeof(buffer);
761 if (sysctlbyname ("machdep.cpu.features", buffer, &valSize, NULL, 0) == 0)
763 cachedsi.wProcessorRevision |= value;
764 if (strstr(buffer,"CX8")) PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
765 if (strstr(buffer,"MMX")) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
766 if (strstr(buffer,"TSC")) PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
767 if (strstr(buffer,"3DNOW")) PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
768 if (strstr(buffer,"SSE")) PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
769 if (strstr(buffer,"SSE2")) PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
770 if (strstr(buffer,"PAE")) PF[PF_PAE_ENABLED] = TRUE;
772 break; /* CPU_TYPE_I386 */
773 default: break;
774 } /* switch (cputype) */
776 valSize = sizeof(longVal);
777 if (!sysctlbyname("hw.cpufrequency", &longVal, &valSize, NULL, 0))
778 cpuHz = longVal;
780 #else
781 FIXME("not yet supported on this system\n");
782 #endif
783 if (!cachedsi.dwActiveProcessorMask)
784 cachedsi.dwActiveProcessorMask = ((ULONG_PTR)1 << cachedsi.dwNumberOfProcessors) - 1;
786 *si = cachedsi;
788 TRACE("<- CPU arch %d, res'd %d, pagesize %d, minappaddr %p, maxappaddr %p,"
789 " act.cpumask %lx, numcpus %d, CPU type %d, allocgran. %d, CPU level %d, CPU rev %d\n",
790 si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
791 si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
792 si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
793 si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
795 create_system_registry_keys( &cachedsi );
796 create_env_registry_keys( &cachedsi );
800 /***********************************************************************
801 * GetNativeSystemInfo [KERNEL32.@]
803 VOID WINAPI GetNativeSystemInfo(
804 LPSYSTEM_INFO si /* [out] Destination for system information, may not be NULL */)
806 static BOOL reported = FALSE;
807 if (!reported) {
808 FIXME("(%p) using GetSystemInfo()\n", si);
809 reported = TRUE;
810 } else
811 TRACE("(%p) using GetSystemInfo()\n", si);
812 GetSystemInfo(si);
815 /***********************************************************************
816 * IsProcessorFeaturePresent [KERNEL32.@]
818 * Determine if the cpu supports a given feature.
820 * RETURNS
821 * TRUE, If the processor supports feature,
822 * FALSE otherwise.
824 BOOL WINAPI IsProcessorFeaturePresent (
825 DWORD feature /* [in] Feature number, (PF_ constants from "winnt.h") */)
827 SYSTEM_INFO si;
828 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
830 if (feature < 64)
831 return PF[feature];
832 else
833 return FALSE;