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