kernel32: Remove the file parameter check again in LoadLibraryExW since some broken...
[wine] / dlls / kernel32 / cpu.c
1 /*
2  * What processor?
3  *
4  * Copyright 1995,1997 Morten Welinder
5  * Copyright 1997-1998 Marcus Meissner
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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
37
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
46
47
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"
56
57 WINE_DEFAULT_DEBUG_CHANNEL(reg);
58
59 #define AUTH    0x68747541      /* "Auth" */
60 #define ENTI    0x69746e65      /* "enti" */
61 #define CAMD    0x444d4163      /* "cAMD" */
62
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.
65  */
66 static inline void do_cpuid(unsigned int ax, unsigned int *p)
67 {
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
76 }
77
78 /* From xf86info havecpuid.c 1.11 */
79 static inline int have_cpuid(void)
80 {
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
99 }
100
101 static BYTE PF[64] = {0,};
102 static ULONGLONG cpuHz = 1000000000; /* default to a 1GHz */
103
104 static void create_system_registry_keys( const SYSTEM_INFO *info )
105 {
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}; */
118
119     unsigned int i;
120     HANDLE hkey, system_key, cpu_key;
121     OBJECT_ATTRIBUTES attr;
122     UNICODE_STRING nameW, valueW;
123
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;
130
131     RtlInitUnicodeString( &nameW, SystemW );
132     if (NtCreateKey( &system_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
133
134     RtlInitUnicodeString( &valueW, IdentifierW );
135     NtSetValueKey( system_key, &valueW, 0, REG_SZ, SysidW, sizeof(SysidW) );
136
137     attr.RootDirectory = system_key;
138     RtlInitUnicodeString( &nameW, fpuW );
139     if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
140
141     RtlInitUnicodeString( &nameW, cpuW );
142     if (!NtCreateKey( &cpu_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
143     {
144         for (i = 0; i < info->dwNumberOfProcessors; i++)
145         {
146             char num[10], id[60];
147
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 ))
152             {
153                 WCHAR idW[60];
154                 DWORD sizeW;
155                 DWORD cpuMHz = cpuHz / 1000000;
156
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) );
161
162                 RtlMultiByteToUnicodeN( idW, sizeof(idW), &sizeW, id, strlen(id)+1 );
163                 NtSetValueKey( hkey, &valueW, 0, REG_SZ, idW, sizeW );
164
165                 /*TODO; report amd's properly*/
166                 RtlInitUnicodeString( &valueW, VendorIdentifierW );
167                 NtSetValueKey( hkey, &valueW, 0, REG_SZ, VenidIntelW, sizeof(VenidIntelW) );
168
169                 RtlInitUnicodeString( &valueW, mhzKeyW );
170                 NtSetValueKey( hkey, &valueW, 0, REG_DWORD, &cpuMHz, sizeof(DWORD) );
171                 NtClose( hkey );
172             }
173             RtlFreeUnicodeString( &nameW );
174         }
175         NtClose( cpu_key );
176     }
177     NtClose( system_key );
178 }
179
180 static void create_env_registry_keys( const SYSTEM_INFO *info )
181 {
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};
194
195     HANDLE env_key;
196     OBJECT_ATTRIBUTES attr;
197     UNICODE_STRING nameW, valueW;
198     DWORD sizeW;
199
200     char nProc[10],id[60],procLevel[10],rev[10];
201     WCHAR nProcW[10],idW[60],procLevelW[10],revW[10];
202
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.
206      */
207
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;
214
215     RtlInitUnicodeString( &nameW, EnvironW );
216     if (NtCreateKey( &env_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
217
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 );
222
223     /* TODO: currently hardcoded x86, add different processors */
224     RtlInitUnicodeString( &valueW, ProcArchW );
225     NtSetValueKey( env_key, &valueW, 0, REG_SZ, x86W, sizeof(x86W) );
226
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 );
233
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 );
238
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 );
244
245     NtClose( env_key );
246 }
247
248 static inline void get_cpuinfo( SYSTEM_INFO *info )
249 {
250     unsigned int regs[4], regs2[4];
251
252     if (!have_cpuid()) return;
253
254     do_cpuid(0x00000000, regs);  /* get standard cpuid level and vendor name */
255     if (regs[0]>=0x00000001)   /* Check for supported cpuid version */
256     {
257         do_cpuid(0x00000001, regs2); /* get cpu features */
258         switch ((regs2[0] >> 8) & 0xf)  /* cpu family */
259         {
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;
281         }
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;
286
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;
294             }
295         }
296     }
297 }
298
299 /****************************************************************************
300  *              QueryPerformanceCounter (KERNEL32.@)
301  *
302  * Get the current value of the performance counter.
303  * 
304  * PARAMS
305  *  counter [O] Destination for the current counter reading
306  *
307  * RETURNS
308  *  Success: TRUE. counter contains the current reading
309  *  Failure: FALSE.
310  *
311  * SEE ALSO
312  *  See QueryPerformanceFrequency.
313  */
314 BOOL WINAPI QueryPerformanceCounter(PLARGE_INTEGER counter)
315 {
316     NtQueryPerformanceCounter( counter, NULL );
317     return TRUE;
318 }
319
320
321 /****************************************************************************
322  *              QueryPerformanceFrequency (KERNEL32.@)
323  *
324  * Get the resolution of the performance counter.
325  *
326  * PARAMS
327  *  frequency [O] Destination for the counter resolution
328  *
329  * RETURNS
330  *  Success. TRUE. Frequency contains the resolution of the counter.
331  *  Failure: FALSE.
332  *
333  * SEE ALSO
334  *  See QueryPerformanceCounter.
335  */
336 BOOL WINAPI QueryPerformanceFrequency(PLARGE_INTEGER frequency)
337 {
338     LARGE_INTEGER counter;
339     NtQueryPerformanceCounter( &counter, frequency );
340     return TRUE;
341 }
342
343
344 /***********************************************************************
345  *                      GetSystemInfo                   [KERNEL32.@]
346  *
347  * Get information about the system.
348  *
349  * RETURNS
350  *  Nothing.
351  *
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.
355  *
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.
360  *
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.
364  *
365  * It also creates a cached flag array for IsProcessorFeaturePresent().
366  */
367 VOID WINAPI GetSystemInfo(
368         LPSYSTEM_INFO si        /* [out] Destination for system information, may not be NULL */)
369 {
370         static int cache = 0;
371         static SYSTEM_INFO cachedsi;
372         SYSTEM_BASIC_INFORMATION sbi;
373
374         TRACE("si=0x%p\n", si);
375         if (cache) {
376                 *si = cachedsi;
377                 return;
378         }
379         memset(PF,0,sizeof(PF));
380
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;
387
388         /* choose sensible defaults ...
389          * FIXME: perhaps overridable with precompiler flags?
390          */
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;
396
397         cache = 1; /* even if there is no more info, we now have a cache entry */
398         *si = cachedsi;
399
400         /* Hmm, reasonable processor feature defaults? */
401
402 #ifdef linux
403         {
404         char line[200];
405         FILE *f = fopen ("/proc/cpuinfo", "r");
406
407         if (!f)
408                 return;
409         while (fgets(line,200,f)!=NULL) {
410                 char    *s,*value;
411
412                 /* NOTE: the ':' is the only character we can rely on */
413                 if (!(value = strchr(line,':')))
414                         continue;
415                 
416                 /* terminate the valuename */
417                 s = value - 1;
418                 while ((s >= line) && ((*s == ' ') || (*s == '\t'))) s--;
419                 *(s + 1) = '\0';
420
421                 /* and strip leading spaces from value */
422                 value += 1;
423                 while (*value==' ') value++;
424                 if ((s=strchr(value,'\n')))
425                         *s='\0';
426
427                 if (!strcasecmp(line,"processor")) {
428                         /* processor number counts up... */
429                         unsigned int x;
430
431                         if (sscanf(value,"%d",&x))
432                                 if (x+1>cachedsi.dwNumberOfProcessors)
433                                         cachedsi.dwNumberOfProcessors=x+1;
434                         
435                         continue;
436                 }
437                 if (!strcasecmp(line,"model")) {
438                         /* First part of wProcessorRevision */
439                         int     x;
440
441                         if (sscanf(value,"%d",&x))
442                                 cachedsi.wProcessorRevision = cachedsi.wProcessorRevision | (x << 8);
443
444                         continue;
445                 }
446
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;
460
461                                 default:
462                                         cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
463                                         cachedsi.wProcessorLevel = atoi(value);
464                                         break;
465                                 }
466                         }
467                         continue;
468                 }
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
473                         ) {
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;
490                                 }
491                         }
492                         continue;
493                 }
494                 if (!strcasecmp(line,"stepping")) {
495                         /* Second part of wProcessorRevision */
496                         int     x;
497
498                         if (sscanf(value,"%d",&x))
499                                 cachedsi.wProcessorRevision = cachedsi.wProcessorRevision | x;
500
501                         continue;
502                 }
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;
508                         }
509                         continue;
510                 }
511                 if (!strcasecmp(line,"fdiv_bug")) {
512                         if (!strncasecmp(value,"yes",3))
513                                 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
514
515                         continue;
516                 }
517                 if (!strcasecmp(line,"fpu")) {
518                         if (!strncasecmp(value,"no",2))
519                                 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
520
521                         continue;
522                 }
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;
541                         
542                         continue;
543                 }
544         }
545         fclose (f);
546         }
547 #elif defined (__NetBSD__)
548         {
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");
555
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;
592                        }
593                        if (!strncmp(cpuclass+1, "486", 3)) {
594                             cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
595                             cachedsi.wProcessorLevel= 4;
596                        }
597                        if (!strncmp(cpuclass+1, "586", 3)) {
598                             cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
599                             cachedsi.wProcessorLevel= 5;
600                        }
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;
606                        }
607                   }
608              }
609
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)
615              {
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;
629
630                             break;
631                         }
632                  }
633                  fclose(f);
634              }
635
636         }
637 #elif defined(__FreeBSD__)
638         {
639         int ret, num;
640         size_t len;
641
642         get_cpuinfo( &cachedsi );
643
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;*/
649
650         len = sizeof(num);
651         ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
652         if (!ret)
653                 cachedsi.dwNumberOfProcessors = num;
654         }
655 #elif defined(__sun)
656         {
657             int num = sysconf( _SC_NPROCESSORS_ONLN );
658
659             if (num == -1) num = 1;
660             get_cpuinfo( &cachedsi );
661             cachedsi.dwNumberOfProcessors = num;
662         }
663 #elif defined (__APPLE__)
664         {
665         size_t valSize;
666         unsigned long long longVal;
667         int value;
668         int cputype;
669         char buffer[256];
670
671         valSize = sizeof(int);
672         if (sysctlbyname ("hw.optional.floatingpoint", &value, &valSize, NULL, 0) == 0)
673         {
674             if (value)
675                 PF[PF_FLOATING_POINT_EMULATED] = FALSE;
676             else
677                 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
678         }
679         valSize = sizeof(int);
680         if (sysctlbyname ("hw.ncpu", &value, &valSize, NULL, 0) == 0)
681             cachedsi.dwNumberOfProcessors = value;
682
683         valSize = sizeof(int);
684         if (sysctlbyname ("hw.activecpu", &value, &valSize, NULL, 0) == 0)
685             cachedsi.dwActiveProcessorMask = ((ULONG_PTR)1 << value) - 1;
686
687         valSize = sizeof(int);
688         if (sysctlbyname ("hw.cputype", &cputype, &valSize, NULL, 0) == 0)
689         {
690             switch (cputype)
691             {
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)
696                 {
697                         switch (value)
698                         {
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;
738                         }
739                 }
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)
745                 {
746                     cachedsi.wProcessorLevel = value;
747                     switch (value)
748                     {
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;
752                     }
753                 }
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)
762                 {
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;
771                 }
772                 break; /* CPU_TYPE_I386 */
773             default: break;
774             } /* switch (cputype) */
775         }
776         valSize = sizeof(longVal);
777         if (!sysctlbyname("hw.cpufrequency", &longVal, &valSize, NULL, 0))
778             cpuHz = longVal;
779         }
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;
785
786         *si = cachedsi;
787
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);
794
795         create_system_registry_keys( &cachedsi );
796         create_env_registry_keys( &cachedsi );
797 }
798
799
800 /***********************************************************************
801  *                      GetNativeSystemInfo             [KERNEL32.@]
802  */
803 VOID WINAPI GetNativeSystemInfo(
804     LPSYSTEM_INFO si    /* [out] Destination for system information, may not be NULL */)
805 {
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); 
813 }
814
815 /***********************************************************************
816  *                      IsProcessorFeaturePresent       [KERNEL32.@]
817  *
818  * Determine if the cpu supports a given feature.
819  * 
820  * RETURNS
821  *  TRUE, If the processor supports feature,
822  *  FALSE otherwise.
823  */
824 BOOL WINAPI IsProcessorFeaturePresent (
825         DWORD feature   /* [in] Feature number, (PF_ constants from "winnt.h") */) 
826 {
827   SYSTEM_INFO si;
828   GetSystemInfo (&si); /* To ensure the information is loaded and cached */
829
830   if (feature < 64)
831     return PF[feature];
832   else
833     return FALSE;
834 }