Use exception handler for GlobalUnlock, GlobalFree.
[wine/wine64.git] / misc / cpu.c
blob5b6fa1ff35355796368cde10db061968043a7cf3
1 /*
2 * What processor?
4 * Copyright 1995,1997 Morten Welinder
5 * Copyright 1997-1998 Marcus Meissner
6 */
8 #include "config.h"
9 #include "wine/port.h"
11 #include <ctype.h>
12 #include <string.h>
13 #include <stdio.h>
15 #include "winbase.h"
16 #include "winreg.h"
17 #include "winnt.h"
18 #include "winerror.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(reg);
23 static BYTE PF[64] = {0,};
25 /***********************************************************************
26 * GetSystemInfo [KERNEL32.@]
28 * Gets the current system information.
30 * On the first call it creates cached values, so it doesn't have to determine
31 * them repeatedly. On Linux, the /proc/cpuinfo special file is used.
33 * It creates a registry subhierarchy, looking like:
34 * \HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\
35 * Identifier (CPU x86)
36 * Note that there is a hierarchy for every processor installed, so this
37 * supports multiprocessor systems. This is done like Win95 does it, I think.
39 * It also creates a cached flag array for IsProcessorFeaturePresent().
41 * No NULL ptr check for LPSYSTEM_INFO in Win9x.
43 * RETURNS
44 * nothing, really
46 VOID WINAPI GetSystemInfo(
47 LPSYSTEM_INFO si /* [out] system information */
48 ) {
49 static int cache = 0;
50 static SYSTEM_INFO cachedsi;
51 HKEY xhkey=0,hkey;
53 if (cache) {
54 memcpy(si,&cachedsi,sizeof(*si));
55 return;
57 memset(PF,0,sizeof(PF));
59 /* choose sensible defaults ...
60 * FIXME: perhaps overrideable with precompiler flags?
62 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
63 cachedsi.dwPageSize = getpagesize();
65 /* FIXME: the two entries below should be computed somehow... */
66 cachedsi.lpMinimumApplicationAddress = (void *)0x00010000;
67 cachedsi.lpMaximumApplicationAddress = (void *)0x7FFFFFFF;
68 cachedsi.dwActiveProcessorMask = 1;
69 cachedsi.dwNumberOfProcessors = 1;
70 cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
71 cachedsi.dwAllocationGranularity = 0x10000;
72 cachedsi.wProcessorLevel = 3; /* 386 */
73 cachedsi.wProcessorRevision = 0;
75 cache = 1; /* even if there is no more info, we now have a cacheentry */
76 memcpy(si,&cachedsi,sizeof(*si));
78 /* Hmm, reasonable processor feature defaults? */
80 /* Create these registry keys for all systems
81 * FPU entry is often empty on Windows, so we don't care either */
82 if ( (RegCreateKeyA(HKEY_LOCAL_MACHINE,"HARDWARE\\DESCRIPTION\\System\\FloatingPointProcessor",&hkey)!=ERROR_SUCCESS)
83 || (RegCreateKeyA(HKEY_LOCAL_MACHINE,"HARDWARE\\DESCRIPTION\\System\\CentralProcessor",&hkey)!=ERROR_SUCCESS) )
85 WARN("Unable to write FPU/CPU info to registry\n");
88 #ifdef linux
90 char buf[20];
91 char line[200];
92 FILE *f = fopen ("/proc/cpuinfo", "r");
94 if (!f)
95 return;
96 xhkey = 0;
97 while (fgets(line,200,f)!=NULL) {
98 char *s,*value;
100 /* NOTE: the ':' is the only character we can rely on */
101 if (!(value = strchr(line,':')))
102 continue;
103 /* terminate the valuename */
104 *value++ = '\0';
105 /* skip any leading spaces */
106 while (*value==' ') value++;
107 if ((s=strchr(value,'\n')))
108 *s='\0';
110 /* 2.1 method */
111 if (!strncasecmp(line, "cpu family",strlen("cpu family"))) {
112 if (isdigit (value[0])) {
113 switch (value[0] - '0') {
114 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
115 cachedsi.wProcessorLevel= 3;
116 break;
117 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
118 cachedsi.wProcessorLevel= 4;
119 break;
120 case 5:
121 case 6: /* PPro/2/3 has same info as P1 */
122 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
123 cachedsi.wProcessorLevel= 5;
124 break;
125 case 1: /* two-figure levels */
126 if (value[1] == '5')
128 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
129 cachedsi.wProcessorLevel= 5;
130 break;
132 /* fall through */
133 default:
134 FIXME("unknown cpu family '%s', please report ! (-> setting to 386)\n", value);
135 break;
138 /* set the CPU type of the current processor */
139 sprintf(buf,"CPU %ld",cachedsi.dwProcessorType);
140 if (xhkey)
141 RegSetValueExA(xhkey,"Identifier",0,REG_SZ,buf,strlen(buf));
142 continue;
144 /* old 2.0 method */
145 if (!strncasecmp(line, "cpu",strlen("cpu"))) {
146 if ( isdigit (value[0]) && value[1] == '8' &&
147 value[2] == '6' && value[3] == 0
149 switch (value[0] - '0') {
150 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
151 cachedsi.wProcessorLevel= 3;
152 break;
153 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
154 cachedsi.wProcessorLevel= 4;
155 break;
156 case 5:
157 case 6: /* PPro/2/3 has same info as P1 */
158 cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
159 cachedsi.wProcessorLevel= 5;
160 break;
161 default:
162 FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
163 break;
166 /* set the CPU type of the current processor
167 * FIXME: someone reported P4 as being set to
168 * " Intel(R) Pentium(R) 4 CPU 1500MHz"
169 * Do we need to do the same ?
170 * */
171 sprintf(buf,"CPU %ld",cachedsi.dwProcessorType);
172 if (xhkey)
173 RegSetValueExA(xhkey,"Identifier",0,REG_SZ,buf,strlen(buf));
174 continue;
176 if (!strncasecmp(line,"fdiv_bug",strlen("fdiv_bug"))) {
177 if (!strncasecmp(value,"yes",3))
178 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
180 continue;
182 if (!strncasecmp(line,"fpu",strlen("fpu"))) {
183 if (!strncasecmp(value,"no",2))
184 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
186 continue;
188 if (!strncasecmp(line,"processor",strlen("processor"))) {
189 /* processor number counts up... */
190 unsigned int x;
192 if (sscanf(value,"%d",&x))
193 if (x+1>cachedsi.dwNumberOfProcessors)
194 cachedsi.dwNumberOfProcessors=x+1;
196 /* Create a new processor subkey on a multiprocessor
197 * system
199 sprintf(buf,"%d",x);
200 if (xhkey)
201 RegCloseKey(xhkey);
202 RegCreateKeyA(hkey,buf,&xhkey);
204 if (!strncasecmp(line,"stepping",strlen("stepping"))) {
205 int x;
207 if (sscanf(value,"%d",&x))
208 cachedsi.wProcessorRevision = x;
210 if ( !strncasecmp(line,"flags",strlen("flags")) ||
211 !strncasecmp(line,"features",strlen("features"))
213 if (strstr(value,"cx8"))
214 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
215 if (strstr(value,"mmx"))
216 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
217 if (strstr(value,"tsc"))
218 PF[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
222 fclose (f);
224 memcpy(si,&cachedsi,sizeof(*si));
225 #else /* linux */
226 /* FIXME: how do we do this on other systems? */
228 RegCreateKeyA(hkey,"0",&xhkey);
229 RegSetValueExA(xhkey,"Identifier",0,REG_SZ,"CPU 386",strlen("CPU 386"));
230 #endif /* !linux */
231 if (xhkey)
232 RegCloseKey(xhkey);
233 if (hkey)
234 RegCloseKey(hkey);
238 /***********************************************************************
239 * IsProcessorFeaturePresent [KERNEL32.@]
240 * RETURNS:
241 * TRUE if processor feature present
242 * FALSE otherwise
244 BOOL WINAPI IsProcessorFeaturePresent (
245 DWORD feature /* [in] feature number, see PF_ defines */
247 SYSTEM_INFO si;
248 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
250 if (feature < 64)
251 return PF[feature];
252 else
253 return FALSE;