advpack: sign-compare fix.
[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 "winerror.h"
55 #include "wine/unicode.h"
56 #include "wine/debug.h"
57
58 WINE_DEFAULT_DEBUG_CHANNEL(reg);
59
60 #define AUTH    0x68747541      /* "Auth" */
61 #define ENTI    0x69746e65      /* "enti" */
62 #define CAMD    0x444d4163      /* "cAMD" */
63
64 /* Calls cpuid with an eax of 'ax' and returns the 16 bytes in *p
65  * We are compiled with -fPIC, so we can't clobber ebx.
66  */
67 static inline void do_cpuid(unsigned int ax, unsigned int *p)
68 {
69 #ifdef __i386__
70         __asm__("pushl %%ebx\n\t"
71                 "cpuid\n\t"
72                 "movl %%ebx, %%esi\n\t"
73                 "popl %%ebx"
74                 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
75                 :  "0" (ax));
76 #endif
77 }
78
79 /* From xf86info havecpuid.c 1.11 */
80 static inline int have_cpuid(void)
81 {
82 #ifdef __i386__
83         unsigned int f1, f2;
84         __asm__("pushfl\n\t"
85                 "pushfl\n\t"
86                 "popl %0\n\t"
87                 "movl %0,%1\n\t"
88                 "xorl %2,%0\n\t"
89                 "pushl %0\n\t"
90                 "popfl\n\t"
91                 "pushfl\n\t"
92                 "popl %0\n\t"
93                 "popfl"
94                 : "=&r" (f1), "=&r" (f2)
95                 : "ir" (0x00200000));
96         return ((f1^f2) & 0x00200000) != 0;
97 #else
98         return 0;
99 #endif
100 }
101
102 static BYTE PF[64] = {0,};
103 static ULONGLONG cpuHz = 1000000000; /* default to a 1GHz */
104
105 static void create_system_registry_keys( const SYSTEM_INFO *info )
106 {
107     static const WCHAR SystemW[] = {'M','a','c','h','i','n','e','\\',
108                                     'H','a','r','d','w','a','r','e','\\',
109                                     'D','e','s','c','r','i','p','t','i','o','n','\\',
110                                     'S','y','s','t','e','m',0};
111     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};
112     static const WCHAR cpuW[] = {'C','e','n','t','r','a','l','P','r','o','c','e','s','s','o','r',0};
113     static const WCHAR IdentifierW[] = {'I','d','e','n','t','i','f','i','e','r',0};
114     static const WCHAR SysidW[] = {'A','T',' ','c','o','m','p','a','t','i','b','l','e',0};
115     static const WCHAR mhzKeyW[] = {'~','M','H','z',0};
116     static const WCHAR VendorIdentifierW[] = {'V','e','n','d','o','r','I','d','e','n','t','i','f','i','e','r',0};
117     static const WCHAR VenidIntelW[] = {'G','e','n','u','i','n','e','I','n','t','e','l',0};
118     /* static const WCHAR VenidAMDW[] = {'A','u','t','h','e','n','t','i','c','A','M','D',0}; */
119
120     unsigned int i;
121     HANDLE hkey, system_key, cpu_key;
122     OBJECT_ATTRIBUTES attr;
123     UNICODE_STRING nameW, valueW;
124
125     attr.Length = sizeof(attr);
126     attr.RootDirectory = 0;
127     attr.ObjectName = &nameW;
128     attr.Attributes = 0;
129     attr.SecurityDescriptor = NULL;
130     attr.SecurityQualityOfService = NULL;
131
132     RtlInitUnicodeString( &nameW, SystemW );
133     if (NtCreateKey( &system_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
134
135     RtlInitUnicodeString( &valueW, IdentifierW );
136     NtSetValueKey( system_key, &valueW, 0, REG_SZ, SysidW, (strlenW(SysidW)+1) * sizeof(WCHAR) );
137
138     attr.RootDirectory = system_key;
139     RtlInitUnicodeString( &nameW, fpuW );
140     if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
141
142     RtlInitUnicodeString( &nameW, cpuW );
143     if (!NtCreateKey( &cpu_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
144     {
145         for (i = 0; i < info->dwNumberOfProcessors; i++)
146         {
147             char num[10], id[60];
148
149             attr.RootDirectory = cpu_key;
150             sprintf( num, "%d", i );
151             RtlCreateUnicodeStringFromAsciiz( &nameW, num );
152             if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
153             {
154                 WCHAR idW[60];
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), NULL, id, strlen(id)+1 );
163                 NtSetValueKey( hkey, &valueW, 0, REG_SZ, idW, (strlenW(idW)+1)*sizeof(WCHAR) );
164
165                 /*TODO; report amd's properly*/
166                 RtlInitUnicodeString( &valueW, VendorIdentifierW );
167                 NtSetValueKey( hkey, &valueW, 0, REG_SZ, VenidIntelW, (strlenW(VenidIntelW)+1) * sizeof(WCHAR) );
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
199     char nProc[10],id[60],procLevel[10],rev[10];
200     WCHAR nProcW[10],idW[60],procLevelW[10],revW[10];
201
202     /* Create some keys under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.
203      * All these environment variables are processor related and will be read during process initialization and hence
204      * show up in the environment of that process.
205      */
206
207     attr.Length = sizeof(attr);
208     attr.RootDirectory = 0;
209     attr.ObjectName = &nameW;
210     attr.Attributes = 0;
211     attr.SecurityDescriptor = NULL;
212     attr.SecurityQualityOfService = NULL;
213
214     RtlInitUnicodeString( &nameW, EnvironW );
215     if (NtCreateKey( &env_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
216
217     sprintf( nProc, "%d", info->dwNumberOfProcessors );
218     RtlMultiByteToUnicodeN( nProcW, sizeof(nProcW), NULL, nProc, strlen(nProc)+1 );
219     RtlInitUnicodeString( &valueW, NumProcW );
220     NtSetValueKey( env_key, &valueW, 0, REG_SZ, nProcW, (strlenW(nProcW)+1)*sizeof(WCHAR) );
221
222     /* TODO: currently hardcoded x86, add different processors */
223     RtlInitUnicodeString( &valueW, ProcArchW );
224     NtSetValueKey( env_key, &valueW, 0, REG_SZ, x86W, (strlenW(x86W)+1) * sizeof(WCHAR) );
225
226     /* TODO: currently hardcoded Intel, add different processors */
227     sprintf( id, "x86 Family %d Model %d Stepping %d, GenuineIntel",
228         info->wProcessorLevel, HIBYTE(info->wProcessorRevision), LOBYTE(info->wProcessorRevision) );
229     RtlMultiByteToUnicodeN( idW, sizeof(idW), NULL, id, strlen(id)+1 );
230     RtlInitUnicodeString( &valueW, ProcIdW );
231     NtSetValueKey( env_key, &valueW, 0, REG_SZ, idW, (strlenW(idW)+1)*sizeof(WCHAR) );
232
233     sprintf( procLevel, "%d", info->wProcessorLevel );
234     RtlMultiByteToUnicodeN( procLevelW, sizeof(procLevelW), NULL, procLevel, strlen(procLevel)+1 );
235     RtlInitUnicodeString( &valueW, ProcLvlW );
236     NtSetValueKey( env_key, &valueW, 0, REG_SZ, procLevelW, (strlenW(procLevelW)+1)*sizeof(WCHAR) );
237
238     /* Properly report model/stepping */
239     sprintf( rev, "%04x", info->wProcessorRevision);
240     RtlMultiByteToUnicodeN( revW, sizeof(revW), NULL, rev, strlen(rev)+1 );
241     RtlInitUnicodeString( &valueW, ProcRevW );
242     NtSetValueKey( env_key, &valueW, 0, REG_SZ, revW, (strlenW(revW)+1)*sizeof(WCHAR) );
243
244     NtClose( env_key );
245 }
246
247 static inline void get_cpuinfo( SYSTEM_INFO *info )
248 {
249     unsigned int regs[4], regs2[4];
250
251     if (!have_cpuid()) return;
252
253     do_cpuid(0x00000000, regs);  /* get standard cpuid level and vendor name */
254     if (regs[0]>=0x00000001)   /* Check for supported cpuid version */
255     {
256         do_cpuid(0x00000001, regs2); /* get cpu features */
257         switch ((regs2[0] >> 8) & 0xf)  /* cpu family */
258         {
259         case 3:
260             info->dwProcessorType = PROCESSOR_INTEL_386;
261             info->wProcessorLevel = 3;
262             break;
263         case 4:
264             info->dwProcessorType = PROCESSOR_INTEL_486;
265             info->wProcessorLevel = 4;
266             break;
267         case 5:
268             info->dwProcessorType = PROCESSOR_INTEL_PENTIUM;
269             info->wProcessorLevel = 5;
270             break;
271         case 6:
272         case 15: /* PPro/2/3/4 has same info as P1 */
273             info->dwProcessorType = PROCESSOR_INTEL_PENTIUM;
274             info->wProcessorLevel = 6;
275             break;
276         default:
277             FIXME("unknown cpu family %d, please report! (-> setting to 386)\n",
278                   (regs2[0] >> 8)&0xf);
279             break;
280         }
281         PF[PF_FLOATING_POINT_EMULATED]     = !(regs2[3] & 1);
282         PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] & (1 << 4 )) >> 4;
283         PF[PF_COMPARE_EXCHANGE_DOUBLE]     = (regs2[3] & (1 << 8 )) >> 8;
284         PF[PF_MMX_INSTRUCTIONS_AVAILABLE]  = (regs2[3] & (1 << 23)) >> 23;
285
286         if (regs[1] == AUTH &&
287             regs[3] == ENTI &&
288             regs[2] == CAMD) {
289             do_cpuid(0x80000000, regs);  /* get vendor cpuid level */
290             if (regs[0]>=0x80000001) {
291                 do_cpuid(0x80000001, regs2);  /* get vendor features */
292                 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 31 )) >> 31;
293             }
294         }
295     }
296 }
297
298 /****************************************************************************
299  *              QueryPerformanceCounter (KERNEL32.@)
300  *
301  * Get the current value of the performance counter.
302  * 
303  * PARAMS
304  *  counter [O] Destination for the current counter reading
305  *
306  * RETURNS
307  *  Success: TRUE. counter contains the current reading
308  *  Failure: FALSE.
309  *
310  * SEE ALSO
311  *  See QueryPerformanceFrequency.
312  */
313 BOOL WINAPI QueryPerformanceCounter(PLARGE_INTEGER counter)
314 {
315     NtQueryPerformanceCounter( counter, NULL );
316     return TRUE;
317 }
318
319
320 /****************************************************************************
321  *              QueryPerformanceFrequency (KERNEL32.@)
322  *
323  * Get the resolution of the performace counter.
324  *
325  * PARAMS
326  *  frequency [O] Destination for the counter resolution
327  *
328  * RETURNS
329  *  Success. TRUE. Frequency contains the resolution of the counter.
330  *  Failure: FALSE.
331  *
332  * SEE ALSO
333  *  See QueryPerformanceCounter.
334  */
335 BOOL WINAPI QueryPerformanceFrequency(PLARGE_INTEGER frequency)
336 {
337     LARGE_INTEGER counter;
338     NtQueryPerformanceCounter( &counter, frequency );
339     return TRUE;
340 }
341
342
343 /***********************************************************************
344  *                      GetSystemInfo                   [KERNEL32.@]
345  *
346  * Get information about the system.
347  *
348  * RETURNS
349  *  Nothing.
350  *
351  * NOTES
352  * On the first call it creates cached values, so it doesn't have to determine
353  * them repeatedly. On Linux, the "/proc/cpuinfo" special file is used.
354  *
355  * It creates a registry subhierarchy, looking like:
356  * "\HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\Identifier (CPU x86)".
357  * Note that there is a hierarchy for every processor installed, so this
358  * supports multiprocessor systems. This is done like Win95 does it, I think.
359  *
360  * It creates some registry entries in the environment part:
361  * "\HKLM\System\CurrentControlSet\Control\Session Manager\Environment". These are
362  * always present. When deleted, Windows will add them again.
363  *
364  * It also creates a cached flag array for IsProcessorFeaturePresent().
365  */
366 VOID WINAPI GetSystemInfo(
367         LPSYSTEM_INFO si        /* [out] Destination for system information, may not be NULL */)
368 {
369         static int cache = 0;
370         static SYSTEM_INFO cachedsi;
371
372         TRACE("si=0x%p\n", si);
373         if (cache) {
374                 memcpy(si,&cachedsi,sizeof(*si));
375                 return;
376         }
377         memset(PF,0,sizeof(PF));
378
379         /* choose sensible defaults ...
380          * FIXME: perhaps overridable with precompiler flags?
381          */
382         cachedsi.u.s.wProcessorArchitecture     = PROCESSOR_ARCHITECTURE_INTEL;
383         cachedsi.dwPageSize                     = getpagesize();
384
385         /* FIXME: the two entries below should be computed somehow... */
386         cachedsi.lpMinimumApplicationAddress    = (void *)0x00010000;
387         cachedsi.lpMaximumApplicationAddress    = (void *)0x7FFEFFFF;
388         cachedsi.dwActiveProcessorMask          = 1;
389         cachedsi.dwNumberOfProcessors           = 1;
390         cachedsi.dwProcessorType                = PROCESSOR_INTEL_PENTIUM;
391         cachedsi.dwAllocationGranularity        = 0x10000;
392         cachedsi.wProcessorLevel                = 5; /* 586 */
393         cachedsi.wProcessorRevision             = 0;
394
395         cache = 1; /* even if there is no more info, we now have a cache entry */
396         memcpy(si,&cachedsi,sizeof(*si));
397
398         /* Hmm, reasonable processor feature defaults? */
399
400 #ifdef linux
401         {
402         char line[200];
403         FILE *f = fopen ("/proc/cpuinfo", "r");
404
405         if (!f)
406                 return;
407         while (fgets(line,200,f)!=NULL) {
408                 char    *s,*value;
409
410                 /* NOTE: the ':' is the only character we can rely on */
411                 if (!(value = strchr(line,':')))
412                         continue;
413                 
414                 /* terminate the valuename */
415                 s = value - 1;
416                 while ((s >= line) && ((*s == ' ') || (*s == '\t'))) s--;
417                 *(s + 1) = '\0';
418
419                 /* and strip leading spaces from value */
420                 value += 1;
421                 while (*value==' ') value++;
422                 if ((s=strchr(value,'\n')))
423                         *s='\0';
424
425                 if (!strcasecmp(line,"processor")) {
426                         /* processor number counts up... */
427                         unsigned int x;
428
429                         if (sscanf(value,"%d",&x))
430                                 if (x+1>cachedsi.dwNumberOfProcessors)
431                                         cachedsi.dwNumberOfProcessors=x+1;
432                         
433                         continue;
434                 }
435                 if (!strcasecmp(line,"model")) {
436                         /* First part of wProcessorRevision */
437                         int     x;
438
439                         if (sscanf(value,"%d",&x))
440                                 cachedsi.wProcessorRevision = cachedsi.wProcessorRevision | (x << 8);
441
442                         continue;
443                 }
444
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;
458                                 case 6: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
459                                         cachedsi.wProcessorLevel= 6;
460                                         break;
461                                 case 1: /* two-figure levels */
462                                     if (value[1] == '5')
463                                     {
464                                         cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
465                                         cachedsi.wProcessorLevel= 6;
466                                         break;
467                                     }
468                                     /* fall through */
469                                 default:
470                                         FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value);
471                                         break;
472                                 }
473                         }
474                         continue;
475                 }
476                 /* old 2.0 method */
477                 if (!strcasecmp(line, "cpu")) {
478                         if (    isdigit (value[0]) && value[1] == '8' &&
479                                 value[2] == '6' && value[3] == 0
480                         ) {
481                                 switch (value[0] - '0') {
482                                 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
483                                         cachedsi.wProcessorLevel= 3;
484                                         break;
485                                 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
486                                         cachedsi.wProcessorLevel= 4;
487                                         break;
488                                 case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
489                                         cachedsi.wProcessorLevel= 5;
490                                         break;
491                                 case 6: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
492                                         cachedsi.wProcessorLevel= 6;
493                                         break;
494                                 default:
495                                         FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
496                                         break;
497                                 }
498                         }
499                         continue;
500                 }
501                 if (!strcasecmp(line,"stepping")) {
502                         /* Second part of wProcessorRevision */
503                         int     x;
504
505                         if (sscanf(value,"%d",&x))
506                                 cachedsi.wProcessorRevision = cachedsi.wProcessorRevision | x;
507
508                         continue;
509                 }
510                 if (!strcasecmp(line, "cpu MHz")) {
511                         double cmz;
512                         if (sscanf( value, "%lf", &cmz ) == 1) {
513                                 /* SYSTEMINFO doesn't have a slot for cpu speed, so store in a global */
514                                 cpuHz = cmz * 1000 * 1000;
515                         }
516                         continue;
517                 }
518                 if (!strcasecmp(line,"fdiv_bug")) {
519                         if (!strncasecmp(value,"yes",3))
520                                 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
521
522                         continue;
523                 }
524                 if (!strcasecmp(line,"fpu")) {
525                         if (!strncasecmp(value,"no",2))
526                                 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
527
528                         continue;
529                 }
530                 if (    !strcasecmp(line,"flags")       ||
531                         !strcasecmp(line,"features")) {
532                         if (strstr(value,"cx8"))
533                                 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
534                         if (strstr(value,"mmx"))
535                                 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
536                         if (strstr(value,"tsc"))
537                                 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
538                         if (strstr(value,"3dnow"))
539                                 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
540                         /* This will also catch sse2, but we have sse itself
541                          * if we have sse2, so no problem */
542                         if (strstr(value,"sse"))
543                                 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
544                         if (strstr(value,"sse2"))
545                                 PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
546                         if (strstr(value,"pae"))
547                                 PF[PF_PAE_ENABLED] = TRUE;
548                         
549                         continue;
550                 }
551         }
552         fclose (f);
553         }
554 #elif defined (__NetBSD__)
555         {
556              int mib[2];
557              int value[2];
558              char model[256];
559              char *cpuclass;
560              FILE *f = fopen ("/var/run/dmesg.boot", "r");
561
562              /* first deduce as much as possible from the sysctls */
563              mib[0] = CTL_MACHDEP;
564 #ifdef CPU_FPU_PRESENT
565              mib[1] = CPU_FPU_PRESENT;
566              value[1] = sizeof(int);
567              if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
568                  if (value) PF[PF_FLOATING_POINT_EMULATED] = FALSE;
569                  else       PF[PF_FLOATING_POINT_EMULATED] = TRUE;
570 #endif
571 #ifdef CPU_SSE
572              mib[1] = CPU_SSE;   /* this should imply MMX */
573              value[1] = sizeof(int);
574              if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
575                  if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
576 #endif
577 #ifdef CPU_SSE2
578              mib[1] = CPU_SSE2;  /* this should imply MMX */
579              value[1] = sizeof(int);
580              if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
581                  if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
582 #endif
583              mib[0] = CTL_HW;
584              mib[1] = HW_NCPU;
585              value[1] = sizeof(int);
586              if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
587                  if (value[0] > cachedsi.dwNumberOfProcessors)
588                     cachedsi.dwNumberOfProcessors = value[0];
589              mib[1] = HW_MODEL;
590              value[1] = 255;
591              if (sysctl(mib, 2, model, value+1, NULL, 0) >= 0) {
592                   model[value[1]] = '\0'; /* just in case */
593                   cpuclass = strstr(model, "-class");
594                   if (cpuclass != NULL) {
595                        while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
596                        if (!strncmp(cpuclass+1, "386", 3)) {
597                             cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
598                             cachedsi.wProcessorLevel= 3;
599                        }
600                        if (!strncmp(cpuclass+1, "486", 3)) {
601                             cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
602                             cachedsi.wProcessorLevel= 4;
603                        }
604                        if (!strncmp(cpuclass+1, "586", 3)) {
605                             cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
606                             cachedsi.wProcessorLevel= 5;
607                        }
608                        if (!strncmp(cpuclass+1, "686", 3)) {
609                             cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
610                             cachedsi.wProcessorLevel= 6;
611                             /* this should imply MMX */
612                             PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
613                        }
614                   }
615              }
616
617              /* it may be worth reading from /var/run/dmesg.boot for
618                 additional information such as CX8, MMX and TSC
619                 (however this information should be considered less
620                  reliable than that from the sysctl calls) */
621              if (f != NULL)
622              {
623                  while (fgets(model, 255, f) != NULL) {
624                         if (sscanf(model,"cpu%d: features %x<", value, value+1) == 2) {
625                             /* we could scan the string but it is easier
626                                to test the bits directly */
627                             if (value[1] & 0x1)
628                                 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
629                             if (value[1] & 0x10)
630                                 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
631                             if (value[1] & 0x100)
632                                 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
633                             if (value[1] & 0x800000)
634                                 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
635
636                             break;
637                         }
638                  }
639                  fclose(f);
640              }
641
642         }
643 #elif defined(__FreeBSD__)
644         {
645         int ret, len, num;
646
647         get_cpuinfo( &cachedsi );
648
649         /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
650         /*len = sizeof(num);
651           ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
652           if (!ret)
653           PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
654
655         len = sizeof(num);
656         ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
657         if (!ret)
658                 cachedsi.dwNumberOfProcessors = num;
659         }
660 #elif defined(__sun)
661         {
662             int num = sysconf( _SC_NPROCESSORS_ONLN );
663
664             if (num == -1) num = 1;
665             get_cpuinfo( &cachedsi );
666             cachedsi.dwNumberOfProcessors = num;
667         }
668 #elif defined (__APPLE__)
669         {
670         size_t valSize;
671         unsigned long long longVal;
672         int value;
673         int cputype;
674         char buffer[256];
675
676         valSize = sizeof(int);
677         if (sysctlbyname ("hw.optional.floatingpoint", &value, &valSize, NULL, 0) == 0)
678         {
679             if (value)
680                 PF[PF_FLOATING_POINT_EMULATED] = FALSE;
681             else
682                 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
683         }
684         valSize = sizeof(int);
685         if (sysctlbyname ("hw.ncpu", &value, &valSize, NULL, 0) == 0)
686         cachedsi.dwNumberOfProcessors = value;
687  
688         valSize = sizeof(int);
689         if (sysctlbyname ("hw.activecpu", &value, &valSize, NULL, 0) == 0)
690             cachedsi.dwActiveProcessorMask = value;
691             
692         valSize = sizeof(int);
693         if (sysctlbyname ("hw.cputype", &cputype, &valSize, NULL, 0) == 0)
694         {
695             switch (cputype)
696             {
697             case CPU_TYPE_POWERPC:
698                 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_PPC;
699                 valSize = sizeof(int);
700                 if (sysctlbyname ("hw.cpusubtype", &value, &valSize, NULL, 0) == 0)
701                 {
702                         switch (value)
703                         {
704                             case CPU_SUBTYPE_POWERPC_601:
705                             case CPU_SUBTYPE_POWERPC_602:
706                                 cachedsi.dwProcessorType = PROCESSOR_PPC_601;
707                                 cachedsi.wProcessorLevel = 1;
708                                 break;
709                             case CPU_SUBTYPE_POWERPC_603:
710                                 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
711                                 cachedsi.wProcessorLevel = 3;
712                                 break;
713                             case CPU_SUBTYPE_POWERPC_603e:
714                             case CPU_SUBTYPE_POWERPC_603ev:
715                                 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
716                                 cachedsi.wProcessorLevel = 6;
717                                 break;
718                             case CPU_SUBTYPE_POWERPC_604:
719                                 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
720                                 cachedsi.wProcessorLevel = 4;
721                                 break;
722                             case CPU_SUBTYPE_POWERPC_604e:
723                                 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
724                                 cachedsi.wProcessorLevel = 9;
725                                 break;
726                             case CPU_SUBTYPE_POWERPC_620:
727                                 cachedsi.dwProcessorType = PROCESSOR_PPC_620;
728                                 cachedsi.wProcessorLevel = 20;
729                                 break;
730                             case CPU_SUBTYPE_POWERPC_750:
731                             case CPU_SUBTYPE_POWERPC_7400:
732                             case CPU_SUBTYPE_POWERPC_7450:
733                                 /* G3/G4 derivate from 603 so ... */
734                                 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
735                                 cachedsi.wProcessorLevel = 6;
736                                 break;
737                             case CPU_SUBTYPE_POWERPC_970:
738                                 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
739                                 cachedsi.wProcessorLevel = 9;
740                                 /* :o) PF[PF_ALTIVEC_INSTRUCTIONS_AVAILABLE] ;-) */
741                                 break;
742                             default: break;
743                         }
744                 }
745                 break; /* CPU_TYPE_POWERPC */
746             case CPU_TYPE_I386:
747                 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
748                 valSize = sizeof(int);
749                 if (sysctlbyname ("machdep.cpu.family", &value, &valSize, NULL, 0) == 0)
750                 {
751                     cachedsi.wProcessorLevel = value;
752                     switch (value)
753                     {
754                     case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386; break;
755                     case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486; break;
756                     default: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM; break;
757                     }
758                 }
759                 valSize = sizeof(int);
760                 if (sysctlbyname ("machdep.cpu.model", &value, &valSize, NULL, 0) == 0)
761                     cachedsi.wProcessorRevision = (value << 8);
762                 valSize = sizeof(int);
763                 if (sysctlbyname ("machdep.cpu.stepping", &value, &valSize, NULL, 0) == 0)
764                     cachedsi.wProcessorRevision |= value;
765                 valSize = sizeof(buffer);
766                 if (sysctlbyname ("machdep.cpu.features", buffer, &valSize, NULL, 0) == 0)
767                 {
768                     cachedsi.wProcessorRevision |= value;
769                     if (strstr(buffer,"CX8"))  PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
770                     if (strstr(buffer,"MMX"))   PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
771                     if (strstr(buffer,"TSC"))   PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
772                     if (strstr(buffer,"3DNOW")) PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
773                     if (strstr(buffer,"SSE"))   PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
774                     if (strstr(buffer,"SSE2"))  PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
775                     if (strstr(buffer,"PAE"))   PF[PF_PAE_ENABLED] = TRUE;
776                 }
777                 break; /* CPU_TYPE_I386 */
778             default: break;
779             } /* switch (cputype) */
780         }
781         valSize = sizeof(longVal);
782         if (!sysctlbyname("hw.cpufrequency", &longVal, &valSize, NULL, 0))
783             cpuHz = longVal;
784         }
785 #else
786         FIXME("not yet supported on this system\n");
787 #endif
788         memcpy(si,&cachedsi,sizeof(*si));
789
790         TRACE("<- CPU arch %d, res'd %d, pagesize %d, minappaddr %p, maxappaddr %p,"
791               " act.cpumask %08x, numcpus %d, CPU type %d, allocgran. %d, CPU level %d, CPU rev %d\n",
792               si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
793               si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
794               si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
795               si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
796
797         create_system_registry_keys( &cachedsi );
798         create_env_registry_keys( &cachedsi );
799 }
800
801
802 /***********************************************************************
803  *                      GetNativeSystemInfo             [KERNEL32.@]
804  */
805 VOID WINAPI GetNativeSystemInfo(
806     LPSYSTEM_INFO si    /* [out] Destination for system information, may not be NULL */)
807 {
808     FIXME("(%p) using GetSystemInfo()\n", si);
809     GetSystemInfo(si); 
810 }
811
812 /***********************************************************************
813  *                      IsProcessorFeaturePresent       [KERNEL32.@]
814  *
815  * Determine if the cpu supports a given feature.
816  * 
817  * RETURNS
818  *  TRUE, If the processor supports feature,
819  *  FALSE otherwise.
820  */
821 BOOL WINAPI IsProcessorFeaturePresent (
822         DWORD feature   /* [in] Feature number, (PF_ constants from "winnt.h") */) 
823 {
824   SYSTEM_INFO si;
825   GetSystemInfo (&si); /* To ensure the information is loaded and cached */
826
827   if (feature < 64)
828     return PF[feature];
829   else
830     return FALSE;
831 }