- implement FB => Texture blits
[wine/wine64.git] / misc / cpu.c
blob81e89cffee29e8f291a68ecbc3a3b27d4ceca243
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 #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
35 #include <ctype.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdlib.h>
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
42 #include "winbase.h"
43 #include "winnt.h"
44 #include "winternl.h"
45 #include "winerror.h"
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(reg);
51 #define AUTH 0x68747541 /* "Auth" */
52 #define ENTI 0x69746e65 /* "enti" */
53 #define CAMD 0x444d4163 /* "cAMD" */
55 /* Calls cpuid with an eax of 'ax' and returns the 16 bytes in *p
56 * We are compiled with -fPIC, so we can't clobber ebx.
58 static inline void do_cpuid(int ax, int *p)
60 #ifdef __i386__
61 __asm__("pushl %%ebx\n\t"
62 "cpuid\n\t"
63 "movl %%ebx, %%esi\n\t"
64 "popl %%ebx"
65 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
66 : "0" (ax));
67 #endif
70 /* From xf86info havecpuid.c 1.11 */
71 static inline int have_cpuid(void)
73 #ifdef __i386__
74 unsigned int f1, f2;
75 __asm__("pushfl\n\t"
76 "pushfl\n\t"
77 "popl %0\n\t"
78 "movl %0,%1\n\t"
79 "xorl %2,%0\n\t"
80 "pushl %0\n\t"
81 "popfl\n\t"
82 "pushfl\n\t"
83 "popl %0\n\t"
84 "popfl"
85 : "=&r" (f1), "=&r" (f2)
86 : "ir" (0x00200000));
87 return ((f1^f2) & 0x00200000) != 0;
88 #else
89 return 0;
90 #endif
93 static BYTE PF[64] = {0,};
95 static void create_registry_keys( const SYSTEM_INFO *info )
97 static const WCHAR SystemW[] = {'M','a','c','h','i','n','e','\\',
98 'H','a','r','d','w','a','r','e','\\',
99 'D','e','s','c','r','i','p','t','i','o','n','\\',
100 'S','y','s','t','e','m',0};
101 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};
102 static const WCHAR cpuW[] = {'C','e','n','t','r','a','l','P','r','o','c','e','s','s','o','r',0};
103 static const WCHAR IdentifierW[] = {'I','d','e','n','t','i','f','i','e','r',0};
104 static const WCHAR SysidW[] = {'A','T',' ','c','o','m','p','a','t','i','b','l','e',0};
106 int i;
107 HKEY hkey, system_key, cpu_key;
108 OBJECT_ATTRIBUTES attr;
109 UNICODE_STRING nameW, valueW;
111 attr.Length = sizeof(attr);
112 attr.RootDirectory = 0;
113 attr.ObjectName = &nameW;
114 attr.Attributes = 0;
115 attr.SecurityDescriptor = NULL;
116 attr.SecurityQualityOfService = NULL;
118 RtlInitUnicodeString( &nameW, SystemW );
119 if (NtCreateKey( &system_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) return;
121 RtlInitUnicodeString( &valueW, IdentifierW );
122 NtSetValueKey( system_key, &valueW, 0, REG_SZ, SysidW, (strlenW(SysidW)+1) * sizeof(WCHAR) );
124 attr.RootDirectory = system_key;
125 RtlInitUnicodeString( &nameW, fpuW );
126 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
128 RtlInitUnicodeString( &nameW, cpuW );
129 if (!NtCreateKey( &cpu_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
131 for (i = 0; i < info->dwNumberOfProcessors; i++)
133 char num[10], id[20];
135 attr.RootDirectory = cpu_key;
136 sprintf( num, "%d", i );
137 RtlCreateUnicodeStringFromAsciiz( &nameW, num );
138 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
140 WCHAR idW[20];
142 sprintf( id, "CPU %ld", info->dwProcessorType );
143 RtlMultiByteToUnicodeN( idW, sizeof(idW), NULL, id, strlen(id)+1 );
144 NtSetValueKey( hkey, &valueW, 0, REG_SZ, idW, (strlenW(idW)+1)*sizeof(WCHAR) );
145 NtClose( hkey );
147 RtlFreeUnicodeString( &nameW );
149 NtClose( cpu_key );
151 NtClose( system_key );
155 /***********************************************************************
156 * GetSystemInfo [KERNEL32.@]
158 * Gets the current system information.
160 * On the first call it creates cached values, so it doesn't have to determine
161 * them repeatedly. On Linux, the /proc/cpuinfo special file is used.
163 * It creates a registry subhierarchy, looking like:
164 * \HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\
165 * Identifier (CPU x86)
166 * Note that there is a hierarchy for every processor installed, so this
167 * supports multiprocessor systems. This is done like Win95 does it, I think.
169 * It also creates a cached flag array for IsProcessorFeaturePresent().
171 * No NULL ptr check for LPSYSTEM_INFO in Win9x.
173 * RETURNS
174 * nothing, really
176 VOID WINAPI GetSystemInfo(
177 LPSYSTEM_INFO si /* [out] system information */
179 static int cache = 0;
180 static SYSTEM_INFO cachedsi;
182 if (cache) {
183 memcpy(si,&cachedsi,sizeof(*si));
184 return;
186 memset(PF,0,sizeof(PF));
188 /* choose sensible defaults ...
189 * FIXME: perhaps overrideable with precompiler flags?
191 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
192 cachedsi.dwPageSize = getpagesize();
194 /* FIXME: the two entries below should be computed somehow... */
195 cachedsi.lpMinimumApplicationAddress = (void *)0x00010000;
196 cachedsi.lpMaximumApplicationAddress = (void *)0x7FFFFFFF;
197 cachedsi.dwActiveProcessorMask = 1;
198 cachedsi.dwNumberOfProcessors = 1;
199 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
200 cachedsi.dwAllocationGranularity = 0x10000;
201 cachedsi.wProcessorLevel = 5; /* 586 */
202 cachedsi.wProcessorRevision = 0;
204 cache = 1; /* even if there is no more info, we now have a cacheentry */
205 memcpy(si,&cachedsi,sizeof(*si));
207 /* Hmm, reasonable processor feature defaults? */
209 #ifdef linux
211 char line[200];
212 FILE *f = fopen ("/proc/cpuinfo", "r");
214 if (!f)
215 return;
216 while (fgets(line,200,f)!=NULL) {
217 char *s,*value;
219 /* NOTE: the ':' is the only character we can rely on */
220 if (!(value = strchr(line,':')))
221 continue;
222 /* terminate the valuename */
223 *value++ = '\0';
224 /* skip any leading spaces */
225 while (*value==' ') value++;
226 if ((s=strchr(value,'\n')))
227 *s='\0';
229 /* 2.1 method */
230 if (!strncasecmp(line, "cpu family",strlen("cpu family"))) {
231 if (isdigit (value[0])) {
232 switch (value[0] - '0') {
233 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
234 cachedsi.wProcessorLevel= 3;
235 break;
236 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
237 cachedsi.wProcessorLevel= 4;
238 break;
239 case 5:
240 case 6: /* PPro/2/3 has same info as P1 */
241 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
242 cachedsi.wProcessorLevel= 5;
243 break;
244 case 1: /* two-figure levels */
245 if (value[1] == '5')
247 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
248 cachedsi.wProcessorLevel= 5;
249 break;
251 /* fall through */
252 default:
253 FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value);
254 break;
257 continue;
259 /* old 2.0 method */
260 if (!strncasecmp(line, "cpu",strlen("cpu"))) {
261 if ( isdigit (value[0]) && value[1] == '8' &&
262 value[2] == '6' && value[3] == 0
264 switch (value[0] - '0') {
265 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
266 cachedsi.wProcessorLevel= 3;
267 break;
268 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
269 cachedsi.wProcessorLevel= 4;
270 break;
271 case 5:
272 case 6: /* PPro/2/3 has same info as P1 */
273 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
274 cachedsi.wProcessorLevel= 5;
275 break;
276 default:
277 FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
278 break;
281 continue;
283 if (!strncasecmp(line,"fdiv_bug",strlen("fdiv_bug"))) {
284 if (!strncasecmp(value,"yes",3))
285 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
287 continue;
289 if (!strncasecmp(line,"fpu",strlen("fpu"))) {
290 if (!strncasecmp(value,"no",2))
291 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
293 continue;
295 if (!strncasecmp(line,"processor",strlen("processor"))) {
296 /* processor number counts up... */
297 unsigned int x;
299 if (sscanf(value,"%d",&x))
300 if (x+1>cachedsi.dwNumberOfProcessors)
301 cachedsi.dwNumberOfProcessors=x+1;
303 if (!strncasecmp(line,"stepping",strlen("stepping"))) {
304 int x;
306 if (sscanf(value,"%d",&x))
307 cachedsi.wProcessorRevision = x;
309 if ( !strncasecmp(line,"flags",strlen("flags")) ||
310 !strncasecmp(line,"features",strlen("features"))
312 if (strstr(value,"cx8"))
313 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
314 if (strstr(value,"mmx"))
315 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
316 if (strstr(value,"tsc"))
317 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
321 fclose (f);
323 memcpy(si,&cachedsi,sizeof(*si));
324 #elif defined (__NetBSD__)
326 int mib[2];
327 int value[2];
328 char model[256];
329 char *cpuclass;
330 FILE *f = fopen ("/var/run/dmesg.boot", "r");
332 /* first deduce as much as possible from the sysctls */
333 mib[0] = CTL_MACHDEP;
334 #ifdef CPU_FPU_PRESENT
335 mib[1] = CPU_FPU_PRESENT;
336 value[1] = sizeof(int);
337 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
338 if (value) PF[PF_FLOATING_POINT_EMULATED] = FALSE;
339 else PF[PF_FLOATING_POINT_EMULATED] = TRUE;
340 #endif
341 #ifdef CPU_SSE
342 mib[1] = CPU_SSE; /* this should imply MMX */
343 value[1] = sizeof(int);
344 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
345 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
346 #endif
347 #ifdef CPU_SSE2
348 mib[1] = CPU_SSE2; /* this should imply MMX */
349 value[1] = sizeof(int);
350 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
351 if (value) PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
352 #endif
353 mib[0] = CTL_HW;
354 mib[1] = HW_NCPU;
355 value[1] = sizeof(int);
356 if (sysctl(mib, 2, value, value+1, NULL, 0) >= 0)
357 if (value[0] > cachedsi.dwNumberOfProcessors)
358 cachedsi.dwNumberOfProcessors = value[0];
359 mib[1] = HW_MODEL;
360 value[1] = 255;
361 if (sysctl(mib, 2, model, value+1, NULL, 0) >= 0) {
362 model[value[1]] = '\0'; /* just in case */
363 cpuclass = strstr(model, "-class");
364 if (cpuclass != NULL) {
365 while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
366 if (!strncmp(cpuclass+1, "386", 3)) {
367 cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
368 cachedsi.wProcessorLevel= 3;
370 if (!strncmp(cpuclass+1, "486", 3)) {
371 cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
372 cachedsi.wProcessorLevel= 4;
374 if (!strncmp(cpuclass+1, "586", 3)) {
375 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
376 cachedsi.wProcessorLevel= 5;
378 if (!strncmp(cpuclass+1, "686", 3)) {
379 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
380 cachedsi.wProcessorLevel= 5;
381 /* this should imply MMX */
382 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
387 /* it may be worth reading from /var/run/dmesg.boot for
388 additional information such as CX8, MMX and TSC
389 (however this information should be considered less
390 reliable than that from the sysctl calls) */
391 if (f != NULL)
393 while (fgets(model, 255, f) != NULL) {
394 if (sscanf(model,"cpu%d: features %x<", value, value+1) == 2) {
395 /* we could scan the string but it is easier
396 to test the bits directly */
397 if (value[1] & 0x1)
398 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
399 if (value[1] & 0x10)
400 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
401 if (value[1] & 0x100)
402 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
403 if (value[1] & 0x800000)
404 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
406 break;
409 fclose(f);
413 memcpy(si,&cachedsi,sizeof(*si));
414 #elif defined(__FreeBSD__)
416 unsigned int regs[4], regs2[4];
417 int ret, len, num;
418 if (!have_cpuid())
419 regs[0] = 0; /* No cpuid support -- skip the rest */
420 else
421 do_cpuid(0x00000000, regs); /* get standard cpuid level and vendor name */
422 if (regs[0]>=0x00000001) { /* Check for supported cpuid version */
423 do_cpuid(0x00000001, regs2); /* get cpu features */
424 switch ((regs2[0] >> 8)&0xf) { /* cpu family */
425 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
426 cachedsi.wProcessorLevel = 3;
427 break;
428 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
429 cachedsi.wProcessorLevel = 4;
430 break;
431 case 5:
432 case 6: /* PPro/2/3 has same info as P1 */
433 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
434 cachedsi.wProcessorLevel = 5;
435 break;
436 default:
437 FIXME("unknown cpu family %d, please report! (-> setting to 386)\n", \
438 (regs2[0] >> 8)&0xf);
439 break;
441 PF[PF_FLOATING_POINT_EMULATED] = !(regs2[3] & 1);
442 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] & (1 << 4 )) >> 4;
443 PF[PF_COMPARE_EXCHANGE_DOUBLE] = (regs2[3] & (1 << 8 )) >> 8;
444 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 23)) >> 23;
445 /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
446 /*len = sizeof(num);
447 ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
448 if (!ret)
449 PF[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
451 if (regs[1] == AUTH &&
452 regs[3] == ENTI &&
453 regs[2] == CAMD) {
454 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
455 if (regs[0]>=0x80000001) {
456 do_cpuid(0x80000001, regs2); /* get vendor features */
457 PF[PF_AMD3D_INSTRUCTIONS_AVAILABLE] =
458 (regs2[3] & (1 << 31 )) >> 31;
462 len = sizeof(num);
463 ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
464 if (!ret)
465 cachedsi.dwNumberOfProcessors = num;
467 memcpy(si,&cachedsi,sizeof(*si));
468 #else
469 FIXME("not yet supported on this system\n");
470 #endif
471 TRACE("<- CPU arch %d, res'd %d, pagesize %ld, minappaddr %p, maxappaddr %p,"
472 " act.cpumask %08lx, numcpus %ld, CPU type %ld, allocgran. %ld, CPU level %d, CPU rev %d\n",
473 si->u.s.wProcessorArchitecture, si->u.s.wReserved, si->dwPageSize,
474 si->lpMinimumApplicationAddress, si->lpMaximumApplicationAddress,
475 si->dwActiveProcessorMask, si->dwNumberOfProcessors, si->dwProcessorType,
476 si->dwAllocationGranularity, si->wProcessorLevel, si->wProcessorRevision);
478 create_registry_keys( &cachedsi );
482 /***********************************************************************
483 * IsProcessorFeaturePresent [KERNEL32.@]
484 * RETURNS:
485 * TRUE if processor feature present
486 * FALSE otherwise
488 BOOL WINAPI IsProcessorFeaturePresent (
489 DWORD feature /* [in] feature number, see PF_ defines */
491 SYSTEM_INFO si;
492 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
494 if (feature < 64)
495 return PF[feature];
496 else
497 return FALSE;