Fixed some issues found by winapi_check.
[wine] / misc / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <ctype.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include "winbase.h"
30 #include "winnt.h"
31 #include "winternl.h"
32 #include "winerror.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(reg);
37
38 static BYTE PF[64] = {0,};
39
40 static void create_registry_keys( const SYSTEM_INFO *info )
41 {
42     static const WCHAR SystemW[] = {'M','a','c','h','i','n','e','\\',
43                                     'H','a','r','d','w','a','r','e','\\',
44                                     'D','e','s','c','r','i','p','t','i','o','n','\\',
45                                     'S','y','s','t','e','m',0};
46     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};
47     static const WCHAR cpuW[] = {'C','e','n','t','r','a','l','P','r','o','c','e','s','s','o','r',0};
48     static const WCHAR IdentifierW[] = {'I','d','e','n','t','i','f','i','e','r',0};
49     static const WCHAR SysidW[] = {'A','T',' ','c','o','m','p','a','t','i','b','l','e',0};
50
51     int i;
52     HKEY hkey, system_key, cpu_key;
53     OBJECT_ATTRIBUTES attr;
54     UNICODE_STRING nameW, valueW;
55
56     attr.Length = sizeof(attr);
57     attr.RootDirectory = 0;
58     attr.ObjectName = &nameW;
59     attr.Attributes = 0;
60     attr.SecurityDescriptor = NULL;
61     attr.SecurityQualityOfService = NULL;
62
63     RtlInitUnicodeString( &nameW, SystemW );
64     if (NtCreateKey( &system_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
65
66     RtlInitUnicodeString( &valueW, IdentifierW );
67     NtSetValueKey( system_key, &valueW, 0, REG_SZ, SysidW, (strlenW(SysidW)+1) * sizeof(WCHAR) );
68
69     attr.RootDirectory = system_key;
70     RtlInitUnicodeString( &nameW, fpuW );
71     if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
72
73     RtlInitUnicodeString( &nameW, cpuW );
74     if (!NtCreateKey( &cpu_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
75     {
76         for (i = 0; i < info->dwNumberOfProcessors; i++)
77         {
78             char num[10], id[20];
79
80             attr.RootDirectory = cpu_key;
81             sprintf( num, "%d", i );
82             RtlCreateUnicodeStringFromAsciiz( &nameW, num );
83             if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
84             {
85                 WCHAR idW[20];
86
87                 sprintf( id, "CPU %ld", info->dwProcessorType );
88                 RtlMultiByteToUnicodeN( idW, sizeof(idW), NULL, id, strlen(id)+1 );
89                 NtSetValueKey( hkey, &valueW, 0, REG_SZ, idW, (strlenW(idW)+1)*sizeof(WCHAR) );
90                 NtClose( hkey );
91             }
92             RtlFreeUnicodeString( &nameW );
93         }
94         NtClose( cpu_key );
95     }
96     NtClose( system_key );
97 }
98
99
100 /***********************************************************************
101  *                      GetSystemInfo                   [KERNEL32.@]
102  *
103  * Gets the current system information.
104  *
105  * On the first call it creates cached values, so it doesn't have to determine
106  * them repeatedly. On Linux, the /proc/cpuinfo special file is used.
107  *
108  * It creates a registry subhierarchy, looking like:
109  * \HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\
110  *                                                      Identifier (CPU x86)
111  * Note that there is a hierarchy for every processor installed, so this
112  * supports multiprocessor systems. This is done like Win95 does it, I think.
113  *
114  * It also creates a cached flag array for IsProcessorFeaturePresent().
115  *
116  * No NULL ptr check for LPSYSTEM_INFO in Win9x.
117  *
118  * RETURNS
119  *      nothing, really
120  */
121 VOID WINAPI GetSystemInfo(
122         LPSYSTEM_INFO si        /* [out] system information */
123 ) {
124         static int cache = 0;
125         static SYSTEM_INFO cachedsi;
126
127         if (cache) {
128                 memcpy(si,&cachedsi,sizeof(*si));
129                 return;
130         }
131         memset(PF,0,sizeof(PF));
132
133         /* choose sensible defaults ...
134          * FIXME: perhaps overrideable with precompiler flags?
135          */
136         cachedsi.u.s.wProcessorArchitecture     = PROCESSOR_ARCHITECTURE_INTEL;
137         cachedsi.dwPageSize                     = getpagesize();
138
139         /* FIXME: the two entries below should be computed somehow... */
140         cachedsi.lpMinimumApplicationAddress    = (void *)0x00010000;
141         cachedsi.lpMaximumApplicationAddress    = (void *)0x7FFFFFFF;
142         cachedsi.dwActiveProcessorMask          = 1;
143         cachedsi.dwNumberOfProcessors           = 1;
144         cachedsi.dwProcessorType                = PROCESSOR_INTEL_PENTIUM;
145         cachedsi.dwAllocationGranularity        = 0x10000;
146         cachedsi.wProcessorLevel                = 5; /* 586 */
147         cachedsi.wProcessorRevision             = 0;
148
149         cache = 1; /* even if there is no more info, we now have a cacheentry */
150         memcpy(si,&cachedsi,sizeof(*si));
151
152         /* Hmm, reasonable processor feature defaults? */
153
154 #ifdef linux
155         {
156         char line[200];
157         FILE *f = fopen ("/proc/cpuinfo", "r");
158
159         if (!f)
160                 return;
161         while (fgets(line,200,f)!=NULL) {
162                 char    *s,*value;
163
164                 /* NOTE: the ':' is the only character we can rely on */
165                 if (!(value = strchr(line,':')))
166                         continue;
167                 /* terminate the valuename */
168                 *value++ = '\0';
169                 /* skip any leading spaces */
170                 while (*value==' ') value++;
171                 if ((s=strchr(value,'\n')))
172                         *s='\0';
173
174                 /* 2.1 method */
175                 if (!strncasecmp(line, "cpu family",strlen("cpu family"))) {
176                         if (isdigit (value[0])) {
177                                 switch (value[0] - '0') {
178                                 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
179                                         cachedsi.wProcessorLevel= 3;
180                                         break;
181                                 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
182                                         cachedsi.wProcessorLevel= 4;
183                                         break;
184                                 case 5:
185                                 case 6: /* PPro/2/3 has same info as P1 */
186                                         cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
187                                         cachedsi.wProcessorLevel= 5;
188                                         break;
189                                 case 1: /* two-figure levels */
190                                     if (value[1] == '5')
191                                     {
192                                         cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
193                                         cachedsi.wProcessorLevel= 5;
194                                         break;
195                                     }
196                                     /* fall through */
197                                 default:
198                                         FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value);
199                                         break;
200                                 }
201                         }
202                         continue;
203                 }
204                 /* old 2.0 method */
205                 if (!strncasecmp(line, "cpu",strlen("cpu"))) {
206                         if (    isdigit (value[0]) && value[1] == '8' &&
207                                 value[2] == '6' && value[3] == 0
208                         ) {
209                                 switch (value[0] - '0') {
210                                 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
211                                         cachedsi.wProcessorLevel= 3;
212                                         break;
213                                 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
214                                         cachedsi.wProcessorLevel= 4;
215                                         break;
216                                 case 5:
217                                 case 6: /* PPro/2/3 has same info as P1 */
218                                         cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
219                                         cachedsi.wProcessorLevel= 5;
220                                         break;
221                                 default:
222                                         FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
223                                         break;
224                                 }
225                         }
226                         continue;
227                 }
228                 if (!strncasecmp(line,"fdiv_bug",strlen("fdiv_bug"))) {
229                         if (!strncasecmp(value,"yes",3))
230                                 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
231
232                         continue;
233                 }
234                 if (!strncasecmp(line,"fpu",strlen("fpu"))) {
235                         if (!strncasecmp(value,"no",2))
236                                 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
237
238                         continue;
239                 }
240                 if (!strncasecmp(line,"processor",strlen("processor"))) {
241                         /* processor number counts up... */
242                         unsigned int x;
243
244                         if (sscanf(value,"%d",&x))
245                                 if (x+1>cachedsi.dwNumberOfProcessors)
246                                         cachedsi.dwNumberOfProcessors=x+1;
247                 }
248                 if (!strncasecmp(line,"stepping",strlen("stepping"))) {
249                         int     x;
250
251                         if (sscanf(value,"%d",&x))
252                                 cachedsi.wProcessorRevision = x;
253                 }
254                 if (    !strncasecmp(line,"flags",strlen("flags"))      ||
255                         !strncasecmp(line,"features",strlen("features"))
256                 ) {
257                         if (strstr(value,"cx8"))
258                                 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
259                         if (strstr(value,"mmx"))
260                                 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
261                         if (strstr(value,"tsc"))
262                                 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
263
264                 }
265         }
266         fclose (f);
267         }
268         memcpy(si,&cachedsi,sizeof(*si));
269 #else  /* linux */
270         FIXME("not yet supported on this system\n");
271 #endif  /* !linux */
272         TRACE("<- CPU arch %d, res'd %d, pagesize %ld, minappaddr %p, maxappaddr %p,"
273               " act.cpumask %08lx, numcpus %ld, CPU type %ld, allocgran. %ld, CPU level %d, CPU rev %d\n",
274               si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
275               si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
276               si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
277               si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
278
279         create_registry_keys( &cachedsi );
280 }
281
282
283 /***********************************************************************
284  *                      IsProcessorFeaturePresent       [KERNEL32.@]
285  * RETURNS:
286  *      TRUE if processor feature present
287  *      FALSE otherwise
288  */
289 BOOL WINAPI IsProcessorFeaturePresent (
290         DWORD feature   /* [in] feature number, see PF_ defines */
291 ) {
292   SYSTEM_INFO si;
293   GetSystemInfo (&si); /* To ensure the information is loaded and cached */
294
295   if (feature < 64)
296     return PF[feature];
297   else
298     return FALSE;
299 }