kernel32: Renamed the kernel directory to kernel32.
[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(int ax, 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, "%ld", 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 /****************************************************************************
248  *              QueryPerformanceCounter (KERNEL32.@)
249  *
250  * Get the current value of the performance counter.
251  * 
252  * PARAMS
253  *  counter [O] Destination for the current counter reading
254  *
255  * RETURNS
256  *  Success: TRUE. counter contains the current reading
257  *  Failure: FALSE.
258  *
259  * SEE ALSO
260  *  See QueryPerformanceFrequency.
261  */
262 BOOL WINAPI QueryPerformanceCounter(PLARGE_INTEGER counter)
263 {
264     NtQueryPerformanceCounter( counter, NULL );
265     return TRUE;
266 }
267
268
269 /****************************************************************************
270  *              QueryPerformanceFrequency (KERNEL32.@)
271  *
272  * Get the resolution of the performace counter.
273  *
274  * PARAMS
275  *  frequency [O] Destination for the counter resolution
276  *
277  * RETURNS
278  *  Success. TRUE. Frequency contains the resolution of the counter.
279  *  Failure: FALSE.
280  *
281  * SEE ALSO
282  *  See QueryPerformanceCounter.
283  */
284 BOOL WINAPI QueryPerformanceFrequency(PLARGE_INTEGER frequency)
285 {
286     LARGE_INTEGER counter;
287     NtQueryPerformanceCounter( &counter, frequency );
288     return TRUE;
289 }
290
291
292 /***********************************************************************
293  *                      GetSystemInfo                   [KERNEL32.@]
294  *
295  * Get information about the system.
296  *
297  * RETURNS
298  *  Nothing.
299  *
300  * NOTES
301  * On the first call it creates cached values, so it doesn't have to determine
302  * them repeatedly. On Linux, the "/proc/cpuinfo" special file is used.
303  *
304  * It creates a registry subhierarchy, looking like:
305  * "\HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\Identifier (CPU x86)".
306  * Note that there is a hierarchy for every processor installed, so this
307  * supports multiprocessor systems. This is done like Win95 does it, I think.
308  *
309  * It creates some registry entries in the environment part:
310  * "\HKLM\System\CurrentControlSet\Control\Session Manager\Environment". These are
311  * always present. When deleted, Windows will add them again.
312  *
313  * It also creates a cached flag array for IsProcessorFeaturePresent().
314  */
315 VOID WINAPI GetSystemInfo(
316         LPSYSTEM_INFO si        /* [out] Destination for system information, may not be NULL */)
317 {
318         static int cache = 0;
319         static SYSTEM_INFO cachedsi;
320
321         TRACE("si=0x%p\n", si);
322         if (cache) {
323                 memcpy(si,&cachedsi,sizeof(*si));
324                 return;
325         }
326         memset(PF,0,sizeof(PF));
327
328         /* choose sensible defaults ...
329          * FIXME: perhaps overrideable with precompiler flags?
330          */
331         cachedsi.u.s.wProcessorArchitecture     = PROCESSOR_ARCHITECTURE_INTEL;
332         cachedsi.dwPageSize                     = getpagesize();
333
334         /* FIXME: the two entries below should be computed somehow... */
335         cachedsi.lpMinimumApplicationAddress    = (void *)0x00010000;
336         cachedsi.lpMaximumApplicationAddress    = (void *)0x7FFEFFFF;
337         cachedsi.dwActiveProcessorMask          = 1;
338         cachedsi.dwNumberOfProcessors           = 1;
339         cachedsi.dwProcessorType                = PROCESSOR_INTEL_PENTIUM;
340         cachedsi.dwAllocationGranularity        = 0x10000;
341         cachedsi.wProcessorLevel                = 5; /* 586 */
342         cachedsi.wProcessorRevision             = 0;
343
344         cache = 1; /* even if there is no more info, we now have a cacheentry */
345         memcpy(si,&cachedsi,sizeof(*si));
346
347         /* Hmm, reasonable processor feature defaults? */
348
349 #ifdef linux
350         {
351         char line[200];
352         FILE *f = fopen ("/proc/cpuinfo", "r");
353
354         if (!f)
355                 return;
356         while (fgets(line,200,f)!=NULL) {
357                 char    *s,*value;
358
359                 /* NOTE: the ':' is the only character we can rely on */
360                 if (!(value = strchr(line,':')))
361                         continue;
362                 
363                 /* terminate the valuename */
364                 s = value - 1;
365                 while ((s >= line) && ((*s == ' ') || (*s == '\t'))) s--;
366                 *(s + 1) = '\0';
367
368                 /* and strip leading spaces from value */
369                 value += 1;
370                 while (*value==' ') value++;
371                 if ((s=strchr(value,'\n')))
372                         *s='\0';
373
374                 if (!strcasecmp(line,"processor")) {
375                         /* processor number counts up... */
376                         unsigned int x;
377
378                         if (sscanf(value,"%d",&x))
379                                 if (x+1>cachedsi.dwNumberOfProcessors)
380                                         cachedsi.dwNumberOfProcessors=x+1;
381                         
382                         continue;
383                 }
384                 if (!strcasecmp(line,"model")) {
385                         /* First part of wProcessorRevision */
386                         int     x;
387
388                         if (sscanf(value,"%d",&x))
389                                 cachedsi.wProcessorRevision = cachedsi.wProcessorRevision | (x << 8);
390
391                         continue;
392                 }
393
394                 /* 2.1 method */
395                 if (!strcasecmp(line, "cpu family")) {
396                         if (isdigit (value[0])) {
397                                 switch (value[0] - '0') {
398                                 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
399                                         cachedsi.wProcessorLevel= 3;
400                                         break;
401                                 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
402                                         cachedsi.wProcessorLevel= 4;
403                                         break;
404                                 case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
405                                         cachedsi.wProcessorLevel= 5;
406                                         break;
407                                 case 6: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
408                                         cachedsi.wProcessorLevel= 6;
409                                         break;
410                                 case 1: /* two-figure levels */
411                                     if (value[1] == '5')
412                                     {
413                                         cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
414                                         cachedsi.wProcessorLevel= 6;
415                                         break;
416                                     }
417                                     /* fall through */
418                                 default:
419                                         FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value);
420                                         break;
421                                 }
422                         }
423                         continue;
424                 }
425                 /* old 2.0 method */
426                 if (!strcasecmp(line, "cpu")) {
427                         if (    isdigit (value[0]) && value[1] == '8' &&
428                                 value[2] == '6' && value[3] == 0
429                         ) {
430                                 switch (value[0] - '0') {
431                                 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
432                                         cachedsi.wProcessorLevel= 3;
433                                         break;
434                                 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
435                                         cachedsi.wProcessorLevel= 4;
436                                         break;
437                                 case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
438                                         cachedsi.wProcessorLevel= 5;
439                                         break;
440                                 case 6: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
441                                         cachedsi.wProcessorLevel= 6;
442                                         break;
443                                 default:
444                                         FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
445                                         break;
446                                 }
447                         }
448                         continue;
449                 }
450                 if (!strcasecmp(line,"stepping")) {
451                         /* Second part of wProcessorRevision */
452                         int     x;
453
454                         if (sscanf(value,"%d",&x))
455                                 cachedsi.wProcessorRevision = cachedsi.wProcessorRevision | x;
456
457                         continue;
458                 }
459                 if (!strcasecmp(line, "cpu MHz")) {
460                         double cmz;
461                         if (sscanf( value, "%lf", &cmz ) == 1) {
462                                 /* SYSTEMINFO doesn't have a slot for cpu speed, so store in a global */
463                                 cpuHz = cmz * 1000 * 1000;
464                         }
465                         continue;
466                 }
467                 if (!strcasecmp(line,"fdiv_bug")) {
468                         if (!strncasecmp(value,"yes",3))
469                                 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
470
471                         continue;
472                 }
473                 if (!strcasecmp(line,"fpu")) {
474                         if (!strncasecmp(value,"no",2))
475                                 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
476
477                         continue;
478                 }
479                 if (    !strcasecmp(line,"flags")       ||
480                         !strcasecmp(line,"features")) {
481                         if (strstr(value,"cx8"))
482                                 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
483                         if (strstr(value,"mmx"))
484                                 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
485                         if (strstr(value,"tsc"))
486                                 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
487                         if (strstr(value,"3dnow"))
488                                 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
489                         /* This will also catch sse2, but we have sse itself
490                          * if we have sse2, so no problem */
491                         if (strstr(value,"sse"))
492                                 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
493                         if (strstr(value,"sse2"))
494                                 PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
495                         if (strstr(value,"pae"))
496                                 PF[PF_PAE_ENABLED] = TRUE;
497                         
498                         continue;
499                 }
500         }
501         fclose (f);
502         }
503         memcpy(si,&cachedsi,sizeof(*si));
504 #elif defined (__NetBSD__)
505         {
506              int mib[2];
507              int value[2];
508              char model[256];
509              char *cpuclass;
510              FILE *f = fopen ("/var/run/dmesg.boot", "r");
511
512              /* first deduce as much as possible from the sysctls */
513              mib[0] = CTL_MACHDEP;
514 #ifdef CPU_FPU_PRESENT
515              mib[1] = CPU_FPU_PRESENT;
516              value[1] = sizeof(int);
517              if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
518                  if (value) PF[PF_FLOATING_POINT_EMULATED] = FALSE;
519                  else       PF[PF_FLOATING_POINT_EMULATED] = TRUE;
520 #endif
521 #ifdef CPU_SSE
522              mib[1] = CPU_SSE;   /* this should imply MMX */
523              value[1] = sizeof(int);
524              if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
525                  if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
526 #endif
527 #ifdef CPU_SSE2
528              mib[1] = CPU_SSE2;  /* this should imply MMX */
529              value[1] = sizeof(int);
530              if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
531                  if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
532 #endif
533              mib[0] = CTL_HW;
534              mib[1] = HW_NCPU;
535              value[1] = sizeof(int);
536              if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
537                  if (value[0] > cachedsi.dwNumberOfProcessors)
538                     cachedsi.dwNumberOfProcessors = value[0];
539              mib[1] = HW_MODEL;
540              value[1] = 255;
541              if (sysctl(mib, 2, model, value+1, NULL, 0) >= 0) {
542                   model[value[1]] = '\0'; /* just in case */
543                   cpuclass = strstr(model, "-class");
544                   if (cpuclass != NULL) {
545                        while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
546                        if (!strncmp(cpuclass+1, "386", 3)) {
547                             cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
548                             cachedsi.wProcessorLevel= 3;
549                        }
550                        if (!strncmp(cpuclass+1, "486", 3)) {
551                             cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
552                             cachedsi.wProcessorLevel= 4;
553                        }
554                        if (!strncmp(cpuclass+1, "586", 3)) {
555                             cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
556                             cachedsi.wProcessorLevel= 5;
557                        }
558                        if (!strncmp(cpuclass+1, "686", 3)) {
559                             cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
560                             cachedsi.wProcessorLevel= 6;
561                             /* this should imply MMX */
562                             PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
563                        }
564                   }
565              }
566
567              /* it may be worth reading from /var/run/dmesg.boot for
568                 additional information such as CX8, MMX and TSC
569                 (however this information should be considered less
570                  reliable than that from the sysctl calls) */
571              if (f != NULL)
572              {
573                  while (fgets(model, 255, f) != NULL) {
574                         if (sscanf(model,"cpu%d: features %x<", value, value+1) == 2) {
575                             /* we could scan the string but it is easier
576                                to test the bits directly */
577                             if (value[1] & 0x1)
578                                 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
579                             if (value[1] & 0x10)
580                                 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
581                             if (value[1] & 0x100)
582                                 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
583                             if (value[1] & 0x800000)
584                                 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
585
586                             break;
587                         }
588                  }
589                  fclose(f);
590              }
591
592         }
593         memcpy(si,&cachedsi,sizeof(*si));
594 #elif defined(__FreeBSD__)
595         {
596         unsigned int regs[4], regs2[4];
597         int ret, len, num;
598         if (!have_cpuid())
599                 regs[0] = 0;                    /* No cpuid support -- skip the rest */
600         else
601                 do_cpuid(0x00000000, regs);     /* get standard cpuid level and vendor name */
602         if (regs[0]>=0x00000001) {              /* Check for supported cpuid version */
603                 do_cpuid(0x00000001, regs2);    /* get cpu features */
604                 switch ((regs2[0] >> 8)&0xf) {  /* cpu family */
605                 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
606                         cachedsi.wProcessorLevel = 3;
607                         break;
608                 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
609                         cachedsi.wProcessorLevel = 4;
610                         break;
611                 case 5:
612                         cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
613                         cachedsi.wProcessorLevel = 5;
614                         break;
615                 case 6:
616                 case 15: /* PPro/2/3/4 has same info as P1 */
617                         cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
618                         cachedsi.wProcessorLevel = 6;
619                         break;
620                 default:
621                         FIXME("unknown FreeBSD cpu family %d, please report! (-> setting to 386)\n", \
622                                 (regs2[0] >> 8)&0xf);
623                         break;
624                 }
625                 PF[PF_FLOATING_POINT_EMULATED]     = !(regs2[3] & 1);
626                 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] & (1 << 4 )) >> 4;
627                 PF[PF_COMPARE_EXCHANGE_DOUBLE]     = (regs2[3] & (1 << 8 )) >> 8;
628                 PF[PF_MMX_INSTRUCTIONS_AVAILABLE]  = (regs2[3] & (1 << 23)) >> 23;
629                 /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
630                 /*len = sizeof(num);
631                 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
632                 if (!ret)
633                         PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
634                 
635                 if (regs[1] == AUTH &&
636                     regs[3] == ENTI &&
637                     regs[2] == CAMD) {
638                         do_cpuid(0x80000000, regs);             /* get vendor cpuid level */
639                         if (regs[0]>=0x80000001) {
640                                 do_cpuid(0x80000001, regs2);    /* get vendor features */
641                                 PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = 
642                                     (regs2[3] & (1 << 31 )) >> 31;
643                         }
644                 }
645         }
646         len = sizeof(num);
647         ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
648         if (!ret)
649                 cachedsi.dwNumberOfProcessors = num;
650         }
651         memcpy(si,&cachedsi,sizeof(*si));
652 #elif defined (__APPLE__)
653         {
654         size_t valSize;
655         unsigned long long longVal;
656         int value;
657         int cputype;
658         char buffer[256];
659
660         valSize = sizeof(int);
661         if (sysctlbyname ("hw.optional.floatingpoint", &value, &valSize, NULL, 0) == 0)
662         {
663             if (value)
664                 PF[PF_FLOATING_POINT_EMULATED] = FALSE;
665             else
666                 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
667         }
668         valSize = sizeof(int);
669         if (sysctlbyname ("hw.ncpu", &value, &valSize, NULL, 0) == 0)
670         cachedsi.dwNumberOfProcessors = value;
671  
672         valSize = sizeof(int);
673         if (sysctlbyname ("hw.activecpu", &value, &valSize, NULL, 0) == 0)
674             cachedsi.dwActiveProcessorMask = value;
675             
676         valSize = sizeof(int);
677         if (sysctlbyname ("hw.cputype", &cputype, &valSize, NULL, 0) == 0)
678         {
679             switch (cputype)
680             {
681             case CPU_TYPE_POWERPC:
682                 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_PPC;
683                 valSize = sizeof(int);
684                 if (sysctlbyname ("hw.cpusubtype", &value, &valSize, NULL, 0) == 0)
685                 {
686                         switch (value)
687                         {
688                             case CPU_SUBTYPE_POWERPC_601:
689                             case CPU_SUBTYPE_POWERPC_602:
690                                 cachedsi.dwProcessorType = PROCESSOR_PPC_601;
691                                 cachedsi.wProcessorLevel = 1;
692                                 break;
693                             case CPU_SUBTYPE_POWERPC_603:
694                                 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
695                                 cachedsi.wProcessorLevel = 3;
696                                 break;
697                             case CPU_SUBTYPE_POWERPC_603e:
698                             case CPU_SUBTYPE_POWERPC_603ev:
699                                 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
700                                 cachedsi.wProcessorLevel = 6;
701                                 break;
702                             case CPU_SUBTYPE_POWERPC_604:
703                                 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
704                                 cachedsi.wProcessorLevel = 4;
705                                 break;
706                             case CPU_SUBTYPE_POWERPC_604e:
707                                 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
708                                 cachedsi.wProcessorLevel = 9;
709                                 break;
710                             case CPU_SUBTYPE_POWERPC_620:
711                                 cachedsi.dwProcessorType = PROCESSOR_PPC_620;
712                                 cachedsi.wProcessorLevel = 20;
713                                 break;
714                             case CPU_SUBTYPE_POWERPC_750:
715                             case CPU_SUBTYPE_POWERPC_7400:
716                             case CPU_SUBTYPE_POWERPC_7450:
717                                 /* G3/G4 derivate from 603 so ... */
718                                 cachedsi.dwProcessorType = PROCESSOR_PPC_603;
719                                 cachedsi.wProcessorLevel = 6;
720                                 break;
721                             case CPU_SUBTYPE_POWERPC_970:
722                                 cachedsi.dwProcessorType = PROCESSOR_PPC_604;
723                                 cachedsi.wProcessorLevel = 9;
724                                 /* :o) PF[PF_ALTIVEC_INSTRUCTIONS_AVAILABLE] ;-) */
725                                 break;
726                             default: break;
727                         }
728                 }
729                 break; /* CPU_TYPE_POWERPC */
730             case CPU_TYPE_I386:
731                 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
732                 valSize = sizeof(int);
733                 if (sysctlbyname ("machdep.cpu.family", &value, &valSize, NULL, 0) == 0)
734                 {
735                     cachedsi.wProcessorLevel = value;
736                     switch (value)
737                     {
738                     case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386; break;
739                     case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486; break;
740                     default: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM; break;
741                     }
742                 }
743                 valSize = sizeof(int);
744                 if (sysctlbyname ("machdep.cpu.model", &value, &valSize, NULL, 0) == 0)
745                     cachedsi.wProcessorRevision = (value << 8);
746                 valSize = sizeof(int);
747                 if (sysctlbyname ("machdep.cpu.stepping", &value, &valSize, NULL, 0) == 0)
748                     cachedsi.wProcessorRevision |= value;
749                 valSize = sizeof(buffer);
750                 if (sysctlbyname ("machdep.cpu.features", buffer, &valSize, NULL, 0) == 0)
751                 {
752                     cachedsi.wProcessorRevision |= value;
753                     if (strstr(buffer,"CX8"))  PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
754                     if (strstr(buffer,"MMX"))   PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
755                     if (strstr(buffer,"TSC"))   PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
756                     if (strstr(buffer,"3DNOW")) PF[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
757                     if (strstr(buffer,"SSE"))   PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
758                     if (strstr(buffer,"SSE2"))  PF[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
759                     if (strstr(buffer,"PAE"))   PF[PF_PAE_ENABLED] = TRUE;
760                 }
761                 break; /* CPU_TYPE_I386 */
762             default: break;
763             } /* switch (cputype) */
764         }
765         valSize = sizeof(longVal);
766         if (!sysctlbyname("hw.cpufrequency", &longVal, &valSize, NULL, 0))
767             cpuHz = longVal;
768         }
769         memcpy(si,&cachedsi,sizeof(*si));
770 #else
771         FIXME("not yet supported on this system\n");
772 #endif
773         TRACE("<- CPU arch %d, res'd %d, pagesize %ld, minappaddr %p, maxappaddr %p,"
774               " act.cpumask %08lx, numcpus %ld, CPU type %ld, allocgran. %ld, CPU level %d, CPU rev %d\n",
775               si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
776               si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
777               si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
778               si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
779
780         create_system_registry_keys( &cachedsi );
781         create_env_registry_keys( &cachedsi );
782 }
783
784
785 /***********************************************************************
786  *                      GetNativeSystemInfo             [KERNEL32.@]
787  */
788 VOID WINAPI GetNativeSystemInfo(
789     LPSYSTEM_INFO si    /* [out] Destination for system information, may not be NULL */)
790 {
791     FIXME("(%p) using GetSystemInfo()\n", si);
792     GetSystemInfo(si); 
793 }
794
795 /***********************************************************************
796  *                      IsProcessorFeaturePresent       [KERNEL32.@]
797  *
798  * Determine if the cpu supports a given feature.
799  * 
800  * RETURNS
801  *  TRUE, If the processor supports feature,
802  *  FALSE otherwise.
803  */
804 BOOL WINAPI IsProcessorFeaturePresent (
805         DWORD feature   /* [in] Feature number, (PF_ constants from "winnt.h") */) 
806 {
807   SYSTEM_INFO si;
808   GetSystemInfo (&si); /* To ensure the information is loaded and cached */
809
810   if (feature < 64)
811     return PF[feature];
812   else
813     return FALSE;
814 }