ntdll: Return more correct information for SystemBasicInformation and GetSystemInfo.
[wine/multimedia.git] / dlls / kernel32 / cpu.c
blob6f500099059165cb9239851584abbdd5cdba4c18
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, (strlenW(SysidW)+1) * sizeof(WCHAR) );
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 cpuMHz = cpuHz / 1000000;
156 /*TODO: report 64bit processors properly*/
157 RtlInitUnicodeString( &valueW, IdentifierW );
158 sprintf( id, "x86 Family %d Model %d Stepping %d",
159 info->wProcessorLevel, HIBYTE(info->wProcessorRevision), LOBYTE(info->wProcessorRevision) );
161 RtlMultiByteToUnicodeN( idW, sizeof(idW), NULL, id, strlen(id)+1 );
162 NtSetValueKey( hkey, &valueW, 0, REG_SZ, idW, (strlenW(idW)+1)*sizeof(WCHAR) );
164 /*TODO; report amd's properly*/
165 RtlInitUnicodeString( &valueW, VendorIdentifierW );
166 NtSetValueKey( hkey, &valueW, 0, REG_SZ, VenidIntelW, (strlenW(VenidIntelW)+1) * sizeof(WCHAR) );
168 RtlInitUnicodeString( &valueW, mhzKeyW );
169 NtSetValueKey( hkey, &valueW, 0, REG_DWORD, &cpuMHz, sizeof(DWORD) );
170 NtClose( hkey );
172 RtlFreeUnicodeString( &nameW );
174 NtClose( cpu_key );
176 NtClose( system_key );
179 static void create_env_registry_keys( const SYSTEM_INFO *info )
181 static const WCHAR EnvironW[] = {'M','a','c','h','i','n','e','\\',
182 'S','y','s','t','e','m','\\',
183 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
184 'C','o','n','t','r','o','l','\\',
185 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
186 'E','n','v','i','r','o','n','m','e','n','t',0};
187 static const WCHAR NumProcW[] = {'N','U','M','B','E','R','_','O','F','_','P','R','O','C','E','S','S','O','R','S',0};
188 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};
189 static const WCHAR x86W[] = {'x','8','6',0};
190 static const WCHAR ProcIdW[] = {'P','R','O','C','E','S','S','O','R','_','I','D','E','N','T','I','F','I','E','R',0};
191 static const WCHAR ProcLvlW[] = {'P','R','O','C','E','S','S','O','R','_','L','E','V','E','L',0};
192 static const WCHAR ProcRevW[] = {'P','R','O','C','E','S','S','O','R','_','R','E','V','I','S','I','O','N',0};
194 HANDLE env_key;
195 OBJECT_ATTRIBUTES attr;
196 UNICODE_STRING nameW, valueW;
198 char nProc[10],id[60],procLevel[10],rev[10];
199 WCHAR nProcW[10],idW[60],procLevelW[10],revW[10];
201 /* Create some keys under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.
202 * All these environment variables are processor related and will be read during process initialization and hence
203 * show up in the environment of that process.
206 attr.Length = sizeof(attr);
207 attr.RootDirectory = 0;
208 attr.ObjectName = &nameW;
209 attr.Attributes = 0;
210 attr.SecurityDescriptor = NULL;
211 attr.SecurityQualityOfService = NULL;
213 RtlInitUnicodeString( &nameW, EnvironW );
214 if (NtCreateKey( &env_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
216 sprintf( nProc, "%d", info->dwNumberOfProcessors );
217 RtlMultiByteToUnicodeN( nProcW, sizeof(nProcW), NULL, nProc, strlen(nProc)+1 );
218 RtlInitUnicodeString( &valueW, NumProcW );
219 NtSetValueKey( env_key, &valueW, 0, REG_SZ, nProcW, (strlenW(nProcW)+1)*sizeof(WCHAR) );
221 /* TODO: currently hardcoded x86, add different processors */
222 RtlInitUnicodeString( &valueW, ProcArchW );
223 NtSetValueKey( env_key, &valueW, 0, REG_SZ, x86W, (strlenW(x86W)+1) * sizeof(WCHAR) );
225 /* TODO: currently hardcoded Intel, add different processors */
226 sprintf( id, "x86 Family %d Model %d Stepping %d, GenuineIntel",
227 info->wProcessorLevel, HIBYTE(info->wProcessorRevision), LOBYTE(info->wProcessorRevision) );
228 RtlMultiByteToUnicodeN( idW, sizeof(idW), NULL, id, strlen(id)+1 );
229 RtlInitUnicodeString( &valueW, ProcIdW );
230 NtSetValueKey( env_key, &valueW, 0, REG_SZ, idW, (strlenW(idW)+1)*sizeof(WCHAR) );
232 sprintf( procLevel, "%d", info->wProcessorLevel );
233 RtlMultiByteToUnicodeN( procLevelW, sizeof(procLevelW), NULL, procLevel, strlen(procLevel)+1 );
234 RtlInitUnicodeString( &valueW, ProcLvlW );
235 NtSetValueKey( env_key, &valueW, 0, REG_SZ, procLevelW, (strlenW(procLevelW)+1)*sizeof(WCHAR) );
237 /* Properly report model/stepping */
238 sprintf( rev, "%04x", info->wProcessorRevision);
239 RtlMultiByteToUnicodeN( revW, sizeof(revW), NULL, rev, strlen(rev)+1 );
240 RtlInitUnicodeString( &valueW, ProcRevW );
241 NtSetValueKey( env_key, &valueW, 0, REG_SZ, revW, (strlenW(revW)+1)*sizeof(WCHAR) );
243 NtClose( env_key );
246 static inline void get_cpuinfo( SYSTEM_INFO *info )
248 unsigned int regs[4], regs2[4];
250 if (!have_cpuid()) return;
252 do_cpuid(0x00000000, regs); /* get standard cpuid level and vendor name */
253 if (regs[0]>=0x00000001) /* Check for supported cpuid version */
255 do_cpuid(0x00000001, regs2); /* get cpu features */
256 switch ((regs2[0] >> 8) & 0xf) /* cpu family */
258 case 3:
259 info->dwProcessorType = PROCESSOR_INTEL_386;
260 info->wProcessorLevel = 3;
261 break;
262 case 4:
263 info->dwProcessorType = PROCESSOR_INTEL_486;
264 info->wProcessorLevel = 4;
265 break;
266 case 5:
267 info->dwProcessorType = PROCESSOR_INTEL_PENTIUM;
268 info->wProcessorLevel = 5;
269 break;
270 case 6:
271 case 15: /* PPro/2/3/4 has same info as P1 */
272 info->dwProcessorType = PROCESSOR_INTEL_PENTIUM;
273 info->wProcessorLevel = 6;
274 break;
275 default:
276 FIXME("unknown cpu family %d, please report! (-> setting to 386)\n",
277 (regs2[0] >> 8)&0xf);
278 break;
280 PF[PF_FLOATING_POINT_EMULATED] = !(regs2[3] & 1);
281 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] & (1 << 4 )) >> 4;
282 PF[PF_COMPARE_EXCHANGE_DOUBLE] = (regs2[3] & (1 << 8 )) >> 8;
283 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 23)) >> 23;
285 if (regs[1] == AUTH &&
286 regs[3] == ENTI &&
287 regs[2] == CAMD) {
288 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
289 if (regs[0]>=0x80000001) {
290 do_cpuid(0x80000001, regs2); /* get vendor features */
291 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 31 )) >> 31;
297 /****************************************************************************
298 * QueryPerformanceCounter (KERNEL32.@)
300 * Get the current value of the performance counter.
302 * PARAMS
303 * counter [O] Destination for the current counter reading
305 * RETURNS
306 * Success: TRUE. counter contains the current reading
307 * Failure: FALSE.
309 * SEE ALSO
310 * See QueryPerformanceFrequency.
312 BOOL WINAPI QueryPerformanceCounter(PLARGE_INTEGER counter)
314 NtQueryPerformanceCounter( counter, NULL );
315 return TRUE;
319 /****************************************************************************
320 * QueryPerformanceFrequency (KERNEL32.@)
322 * Get the resolution of the performance counter.
324 * PARAMS
325 * frequency [O] Destination for the counter resolution
327 * RETURNS
328 * Success. TRUE. Frequency contains the resolution of the counter.
329 * Failure: FALSE.
331 * SEE ALSO
332 * See QueryPerformanceCounter.
334 BOOL WINAPI QueryPerformanceFrequency(PLARGE_INTEGER frequency)
336 LARGE_INTEGER counter;
337 NtQueryPerformanceCounter( &counter, frequency );
338 return TRUE;
342 /***********************************************************************
343 * GetSystemInfo [KERNEL32.@]
345 * Get information about the system.
347 * RETURNS
348 * Nothing.
350 * NOTES
351 * On the first call it creates cached values, so it doesn't have to determine
352 * them repeatedly. On Linux, the "/proc/cpuinfo" special file is used.
354 * It creates a registry subhierarchy, looking like:
355 * "\HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\Identifier (CPU x86)".
356 * Note that there is a hierarchy for every processor installed, so this
357 * supports multiprocessor systems. This is done like Win95 does it, I think.
359 * It creates some registry entries in the environment part:
360 * "\HKLM\System\CurrentControlSet\Control\Session Manager\Environment". These are
361 * always present. When deleted, Windows will add them again.
363 * It also creates a cached flag array for IsProcessorFeaturePresent().
365 VOID WINAPI GetSystemInfo(
366 LPSYSTEM_INFO si /* [out] Destination for system information, may not be NULL */)
368 static int cache = 0;
369 static SYSTEM_INFO cachedsi;
370 SYSTEM_BASIC_INFORMATION sbi;
372 TRACE("si=0x%p\n", si);
373 if (cache) {
374 *si = cachedsi;
375 return;
377 memset(PF,0,sizeof(PF));
379 NtQuerySystemInformation( SystemBasicInformation, &sbi, sizeof(sbi), NULL );
380 cachedsi.dwPageSize = sbi.uPageSize;
381 cachedsi.lpMinimumApplicationAddress = sbi.pLowestUserAddress;
382 cachedsi.lpMaximumApplicationAddress = sbi.pMmHighestUserAddress;
383 cachedsi.dwNumberOfProcessors = sbi.uKeActiveProcessors;
384 cachedsi.dwAllocationGranularity = sbi.uAllocationGranularity;
386 /* choose sensible defaults ...
387 * FIXME: perhaps overridable with precompiler flags?
389 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
390 cachedsi.dwActiveProcessorMask = 0;
391 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
392 cachedsi.wProcessorLevel = 5; /* 586 */
393 cachedsi.wProcessorRevision = 0;
395 cache = 1; /* even if there is no more info, we now have a cache entry */
396 *si = cachedsi;
398 /* Hmm, reasonable processor feature defaults? */
400 #ifdef linux
402 char line[200];
403 FILE *f = fopen ("/proc/cpuinfo", "r");
405 if (!f)
406 return;
407 while (fgets(line,200,f)!=NULL) {
408 char *s,*value;
410 /* NOTE: the ':' is the only character we can rely on */
411 if (!(value = strchr(line,':')))
412 continue;
414 /* terminate the valuename */
415 s = value - 1;
416 while ((s >= line) && ((*s == ' ') || (*s == '\t'))) s--;
417 *(s + 1) = '\0';
419 /* and strip leading spaces from value */
420 value += 1;
421 while (*value==' ') value++;
422 if ((s=strchr(value,'\n')))
423 *s='\0';
425 if (!strcasecmp(line,"processor")) {
426 /* processor number counts up... */
427 unsigned int x;
429 if (sscanf(value,"%d",&x))
430 if (x+1>cachedsi.dwNumberOfProcessors)
431 cachedsi.dwNumberOfProcessors=x+1;
433 continue;
435 if (!strcasecmp(line,"model")) {
436 /* First part of wProcessorRevision */
437 int x;
439 if (sscanf(value,"%d",&x))
440 cachedsi.wProcessorRevision = cachedsi.wProcessorRevision | (x << 8);
442 continue;
445 /* 2.1 method */
446 if (!strcasecmp(line, "cpu family")) {
447 if (isdigit (value[0])) {
448 switch (value[0] - '0') {
449 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
450 cachedsi.wProcessorLevel= 3;
451 break;
452 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
453 cachedsi.wProcessorLevel= 4;
454 break;
455 case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
456 cachedsi.wProcessorLevel= 5;
457 break;
459 default:
460 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
461 cachedsi.wProcessorLevel = atoi(value);
462 break;
465 continue;
467 /* old 2.0 method */
468 if (!strcasecmp(line, "cpu")) {
469 if ( isdigit (value[0]) && value[1] == '8' &&
470 value[2] == '6' && value[3] == 0
472 switch (value[0] - '0') {
473 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
474 cachedsi.wProcessorLevel= 3;
475 break;
476 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
477 cachedsi.wProcessorLevel= 4;
478 break;
479 case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
480 cachedsi.wProcessorLevel= 5;
481 break;
482 case 6: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
483 cachedsi.wProcessorLevel= 6;
484 break;
485 default:
486 FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
487 break;
490 continue;
492 if (!strcasecmp(line,"stepping")) {
493 /* Second part of wProcessorRevision */
494 int x;
496 if (sscanf(value,"%d",&x))
497 cachedsi.wProcessorRevision = cachedsi.wProcessorRevision | x;
499 continue;
501 if (!strcasecmp(line, "cpu MHz")) {
502 double cmz;
503 if (sscanf( value, "%lf", &cmz ) == 1) {
504 /* SYSTEMINFO doesn't have a slot for cpu speed, so store in a global */
505 cpuHz = cmz * 1000 * 1000;
507 continue;
509 if (!strcasecmp(line,"fdiv_bug")) {
510 if (!strncasecmp(value,"yes",3))
511 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
513 continue;
515 if (!strcasecmp(line,"fpu")) {
516 if (!strncasecmp(value,"no",2))
517 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
519 continue;
521 if ( !strcasecmp(line,"flags") ||
522 !strcasecmp(line,"features")) {
523 if (strstr(value,"cx8"))
524 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
525 if (strstr(value,"mmx"))
526 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
527 if (strstr(value,"tsc"))
528 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
529 if (strstr(value,"3dnow"))
530 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
531 /* This will also catch sse2, but we have sse itself
532 * if we have sse2, so no problem */
533 if (strstr(value,"sse"))
534 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
535 if (strstr(value,"sse2"))
536 PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
537 if (strstr(value,"pae"))
538 PF[PF_PAE_ENABLED] = TRUE;
540 continue;
543 fclose (f);
545 #elif defined (__NetBSD__)
547 int mib[2];
548 int value[2];
549 char model[256];
550 char *cpuclass;
551 FILE *f = fopen ("/var/run/dmesg.boot", "r");
553 /* first deduce as much as possible from the sysctls */
554 mib[0] = CTL_MACHDEP;
555 #ifdef CPU_FPU_PRESENT
556 mib[1] = CPU_FPU_PRESENT;
557 value[1] = sizeof(int);
558 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
559 if (value) PF[PF_FLOATING_POINT_EMULATED] = FALSE;
560 else PF[PF_FLOATING_POINT_EMULATED] = TRUE;
561 #endif
562 #ifdef CPU_SSE
563 mib[1] = CPU_SSE; /* this should imply MMX */
564 value[1] = sizeof(int);
565 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
566 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
567 #endif
568 #ifdef CPU_SSE2
569 mib[1] = CPU_SSE2; /* this should imply MMX */
570 value[1] = sizeof(int);
571 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
572 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
573 #endif
574 mib[0] = CTL_HW;
575 mib[1] = HW_NCPU;
576 value[1] = sizeof(int);
577 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
578 if (value[0] > cachedsi.dwNumberOfProcessors)
579 cachedsi.dwNumberOfProcessors = value[0];
580 mib[1] = HW_MODEL;
581 value[1] = 255;
582 if (sysctl(mib, 2, model, value+1, NULL, 0) >= 0) {
583 model[value[1]] = '\0'; /* just in case */
584 cpuclass = strstr(model, "-class");
585 if (cpuclass != NULL) {
586 while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
587 if (!strncmp(cpuclass+1, "386", 3)) {
588 cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
589 cachedsi.wProcessorLevel= 3;
591 if (!strncmp(cpuclass+1, "486", 3)) {
592 cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
593 cachedsi.wProcessorLevel= 4;
595 if (!strncmp(cpuclass+1, "586", 3)) {
596 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
597 cachedsi.wProcessorLevel= 5;
599 if (!strncmp(cpuclass+1, "686", 3)) {
600 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
601 cachedsi.wProcessorLevel= 6;
602 /* this should imply MMX */
603 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
608 /* it may be worth reading from /var/run/dmesg.boot for
609 additional information such as CX8, MMX and TSC
610 (however this information should be considered less
611 reliable than that from the sysctl calls) */
612 if (f != NULL)
614 while (fgets(model, 255, f) != NULL) {
615 if (sscanf(model,"cpu%d: features %x<", value, value+1) == 2) {
616 /* we could scan the string but it is easier
617 to test the bits directly */
618 if (value[1] & 0x1)
619 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
620 if (value[1] & 0x10)
621 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
622 if (value[1] & 0x100)
623 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
624 if (value[1] & 0x800000)
625 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
627 break;
630 fclose(f);
634 #elif defined(__FreeBSD__)
636 int ret, num;
637 unsigned len;
639 get_cpuinfo( &cachedsi );
641 /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
642 /*len = sizeof(num);
643 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
644 if (!ret)
645 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
647 len = sizeof(num);
648 ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
649 if (!ret)
650 cachedsi.dwNumberOfProcessors = num;
652 #elif defined(__sun)
654 int num = sysconf( _SC_NPROCESSORS_ONLN );
656 if (num == -1) num = 1;
657 get_cpuinfo( &cachedsi );
658 cachedsi.dwNumberOfProcessors = num;
660 #elif defined (__APPLE__)
662 size_t valSize;
663 unsigned long long longVal;
664 int value;
665 int cputype;
666 char buffer[256];
668 valSize = sizeof(int);
669 if (sysctlbyname ("hw.optional.floatingpoint", &value, &valSize, NULL, 0) == 0)
671 if (value)
672 PF[PF_FLOATING_POINT_EMULATED] = FALSE;
673 else
674 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
676 valSize = sizeof(int);
677 if (sysctlbyname ("hw.ncpu", &value, &valSize, NULL, 0) == 0)
678 cachedsi.dwNumberOfProcessors = value;
680 valSize = sizeof(int);
681 if (sysctlbyname ("hw.activecpu", &value, &valSize, NULL, 0) == 0)
682 cachedsi.dwActiveProcessorMask = (1 << value) - 1;
684 valSize = sizeof(int);
685 if (sysctlbyname ("hw.cputype", &cputype, &valSize, NULL, 0) == 0)
687 switch (cputype)
689 case CPU_TYPE_POWERPC:
690 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_PPC;
691 valSize = sizeof(int);
692 if (sysctlbyname ("hw.cpusubtype", &value, &valSize, NULL, 0) == 0)
694 switch (value)
696 case CPU_SUBTYPE_POWERPC_601:
697 case CPU_SUBTYPE_POWERPC_602:
698 cachedsi.dwProcessorType = PROCESSOR_PPC_601;
699 cachedsi.wProcessorLevel = 1;
700 break;
701 case CPU_SUBTYPE_POWERPC_603:
702 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
703 cachedsi.wProcessorLevel = 3;
704 break;
705 case CPU_SUBTYPE_POWERPC_603e:
706 case CPU_SUBTYPE_POWERPC_603ev:
707 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
708 cachedsi.wProcessorLevel = 6;
709 break;
710 case CPU_SUBTYPE_POWERPC_604:
711 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
712 cachedsi.wProcessorLevel = 4;
713 break;
714 case CPU_SUBTYPE_POWERPC_604e:
715 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
716 cachedsi.wProcessorLevel = 9;
717 break;
718 case CPU_SUBTYPE_POWERPC_620:
719 cachedsi.dwProcessorType = PROCESSOR_PPC_620;
720 cachedsi.wProcessorLevel = 20;
721 break;
722 case CPU_SUBTYPE_POWERPC_750:
723 case CPU_SUBTYPE_POWERPC_7400:
724 case CPU_SUBTYPE_POWERPC_7450:
725 /* G3/G4 derive from 603 so ... */
726 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
727 cachedsi.wProcessorLevel = 6;
728 break;
729 case CPU_SUBTYPE_POWERPC_970:
730 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
731 cachedsi.wProcessorLevel = 9;
732 /* :o) PF[PF_ALTIVEC_INSTRUCTIONS_AVAILABLE] ;-) */
733 break;
734 default: break;
737 break; /* CPU_TYPE_POWERPC */
738 case CPU_TYPE_I386:
739 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
740 valSize = sizeof(int);
741 if (sysctlbyname ("machdep.cpu.family", &value, &valSize, NULL, 0) == 0)
743 cachedsi.wProcessorLevel = value;
744 switch (value)
746 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386; break;
747 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486; break;
748 default: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM; break;
751 valSize = sizeof(int);
752 if (sysctlbyname ("machdep.cpu.model", &value, &valSize, NULL, 0) == 0)
753 cachedsi.wProcessorRevision = (value << 8);
754 valSize = sizeof(int);
755 if (sysctlbyname ("machdep.cpu.stepping", &value, &valSize, NULL, 0) == 0)
756 cachedsi.wProcessorRevision |= value;
757 valSize = sizeof(buffer);
758 if (sysctlbyname ("machdep.cpu.features", buffer, &valSize, NULL, 0) == 0)
760 cachedsi.wProcessorRevision |= value;
761 if (strstr(buffer,"CX8")) PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
762 if (strstr(buffer,"MMX")) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
763 if (strstr(buffer,"TSC")) PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
764 if (strstr(buffer,"3DNOW")) PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
765 if (strstr(buffer,"SSE")) PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
766 if (strstr(buffer,"SSE2")) PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
767 if (strstr(buffer,"PAE")) PF[PF_PAE_ENABLED] = TRUE;
769 break; /* CPU_TYPE_I386 */
770 default: break;
771 } /* switch (cputype) */
773 valSize = sizeof(longVal);
774 if (!sysctlbyname("hw.cpufrequency", &longVal, &valSize, NULL, 0))
775 cpuHz = longVal;
777 #else
778 FIXME("not yet supported on this system\n");
779 #endif
780 if (!cachedsi.dwActiveProcessorMask)
781 cachedsi.dwActiveProcessorMask = (1 << cachedsi.dwNumberOfProcessors) - 1;
783 *si = cachedsi;
785 TRACE("<- CPU arch %d, res'd %d, pagesize %d, minappaddr %p, maxappaddr %p,"
786 " act.cpumask %08x, numcpus %d, CPU type %d, allocgran. %d, CPU level %d, CPU rev %d\n",
787 si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
788 si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
789 si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
790 si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
792 create_system_registry_keys( &cachedsi );
793 create_env_registry_keys( &cachedsi );
797 /***********************************************************************
798 * GetNativeSystemInfo [KERNEL32.@]
800 VOID WINAPI GetNativeSystemInfo(
801 LPSYSTEM_INFO si /* [out] Destination for system information, may not be NULL */)
803 static BOOL reported = FALSE;
804 if (!reported) {
805 FIXME("(%p) using GetSystemInfo()\n", si);
806 reported = TRUE;
807 } else
808 TRACE("(%p) using GetSystemInfo()\n", si);
809 GetSystemInfo(si);
812 /***********************************************************************
813 * IsProcessorFeaturePresent [KERNEL32.@]
815 * Determine if the cpu supports a given feature.
817 * RETURNS
818 * TRUE, If the processor supports feature,
819 * FALSE otherwise.
821 BOOL WINAPI IsProcessorFeaturePresent (
822 DWORD feature /* [in] Feature number, (PF_ constants from "winnt.h") */)
824 SYSTEM_INFO si;
825 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
827 if (feature < 64)
828 return PF[feature];
829 else
830 return FALSE;