If a .reg function is accessed through a symlink then write directly
[wine/wine64.git] / misc / cpu.c
blob578dd80d9cb8b3a389d2b48d643294d0bd6e31f6
1 /*
2 * What processor?
4 * Copyright 1995,1997 Morten Welinder
5 * Copyright 1997-1998 Marcus Meissner
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <ctype.h>
26 #include <string.h>
27 #include <stdio.h>
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"
36 WINE_DEFAULT_DEBUG_CHANNEL(reg);
38 static BYTE PF[64] = {0,};
40 static void create_registry_keys( const SYSTEM_INFO *info )
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};
51 int i;
52 HKEY hkey, system_key, cpu_key;
53 OBJECT_ATTRIBUTES attr;
54 UNICODE_STRING nameW, valueW;
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;
63 RtlInitUnicodeString( &nameW, SystemW );
64 if (NtCreateKey( &system_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
66 RtlInitUnicodeString( &valueW, IdentifierW );
67 NtSetValueKey( system_key, &valueW, 0, REG_SZ, SysidW, (strlenW(SysidW)+1) * sizeof(WCHAR) );
69 attr.RootDirectory = system_key;
70 RtlInitUnicodeString( &nameW, fpuW );
71 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
73 RtlInitUnicodeString( &nameW, cpuW );
74 if (!NtCreateKey( &cpu_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
76 for (i = 0; i < info->dwNumberOfProcessors; i++)
78 char num[10], id[20];
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 ))
85 WCHAR idW[20];
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 );
92 RtlFreeUnicodeString( &nameW );
94 NtClose( cpu_key );
96 NtClose( system_key );
100 /***********************************************************************
101 * GetSystemInfo [KERNEL32.@]
103 * Gets the current system information.
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.
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.
114 * It also creates a cached flag array for IsProcessorFeaturePresent().
116 * No NULL ptr check for LPSYSTEM_INFO in Win9x.
118 * RETURNS
119 * nothing, really
121 VOID WINAPI GetSystemInfo(
122 LPSYSTEM_INFO si /* [out] system information */
124 static int cache = 0;
125 static SYSTEM_INFO cachedsi;
127 if (cache) {
128 memcpy(si,&cachedsi,sizeof(*si));
129 return;
131 memset(PF,0,sizeof(PF));
133 /* choose sensible defaults ...
134 * FIXME: perhaps overrideable with precompiler flags?
136 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
137 cachedsi.dwPageSize = getpagesize();
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;
149 cache = 1; /* even if there is no more info, we now have a cacheentry */
150 memcpy(si,&cachedsi,sizeof(*si));
152 /* Hmm, reasonable processor feature defaults? */
154 #ifdef linux
156 char line[200];
157 FILE *f = fopen ("/proc/cpuinfo", "r");
159 if (!f)
160 return;
161 while (fgets(line,200,f)!=NULL) {
162 char *s,*value;
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';
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')
192 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
193 cachedsi.wProcessorLevel= 5;
194 break;
196 /* fall through */
197 default:
198 FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value);
199 break;
202 continue;
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
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;
226 continue;
228 if (!strncasecmp(line,"fdiv_bug",strlen("fdiv_bug"))) {
229 if (!strncasecmp(value,"yes",3))
230 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
232 continue;
234 if (!strncasecmp(line,"fpu",strlen("fpu"))) {
235 if (!strncasecmp(value,"no",2))
236 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
238 continue;
240 if (!strncasecmp(line,"processor",strlen("processor"))) {
241 /* processor number counts up... */
242 unsigned int x;
244 if (sscanf(value,"%d",&x))
245 if (x+1>cachedsi.dwNumberOfProcessors)
246 cachedsi.dwNumberOfProcessors=x+1;
248 if (!strncasecmp(line,"stepping",strlen("stepping"))) {
249 int x;
251 if (sscanf(value,"%d",&x))
252 cachedsi.wProcessorRevision = x;
254 if ( !strncasecmp(line,"flags",strlen("flags")) ||
255 !strncasecmp(line,"features",strlen("features"))
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;
266 fclose (f);
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);
279 create_registry_keys( &cachedsi );
283 /***********************************************************************
284 * IsProcessorFeaturePresent [KERNEL32.@]
285 * RETURNS:
286 * TRUE if processor feature present
287 * FALSE otherwise
289 BOOL WINAPI IsProcessorFeaturePresent (
290 DWORD feature /* [in] feature number, see PF_ defines */
292 SYSTEM_INFO si;
293 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
295 if (feature < 64)
296 return PF[feature];
297 else
298 return FALSE;