Minor update.
[wine/multimedia.git] / misc / cpu.c
blobeb27bfac46db8eb39a474108057620783251b3ce
1 /*
2 * What processor?
4 * Copyright 1995,1997 Morten Welinder
5 * Copyright 1997-1998 Marcus Meissner
6 */
8 #include <ctype.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include "winbase.h"
12 #include "wine/winbase16.h"
13 #include "wine/winestring.h"
14 #include "winreg.h"
15 #include "global.h"
16 #include "winnt.h"
17 #include "winerror.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(reg);
22 static BYTE PF[64] = {0,};
24 /***********************************************************************
25 * GetSystemInfo [KERNELL32.404]
27 * Gets the current system information.
29 * On the first call it reads cached values, so it doesn't have to determine
30 * them repeatedly. On Linux, the /proc/cpuinfo special file is used.
32 * It creates a registry subhierarchy, looking like:
33 * \HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\
34 * Identifier (CPU x86)
35 * Note that there is a hierarchy for every processor installed, so this
36 * supports multiprocessor systems. This is done like Win95 does it, I think.
38 * It also creates a cached flag array for IsProcessorFeaturePresent().
40 * RETURNS
41 * nothing, really
43 VOID WINAPI GetSystemInfo(
44 LPSYSTEM_INFO si /* [out] system information */
45 ) {
46 static int cache = 0;
47 static SYSTEM_INFO cachedsi;
48 HKEY xhkey=0,hkey;
50 if (cache) {
51 memcpy(si,&cachedsi,sizeof(*si));
52 return;
54 memset(PF,0,sizeof(PF));
56 /* choose sensible defaults ...
57 * FIXME: perhaps overrideable with precompiler flags?
59 cachedsi.u.s.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
60 cachedsi.dwPageSize = VIRTUAL_GetPageSize();
62 /* FIXME: better values for the two entries below... */
63 cachedsi.lpMinimumApplicationAddress = (void *)0x40000000;
64 cachedsi.lpMaximumApplicationAddress = (void *)0x7FFFFFFF;
65 cachedsi.dwActiveProcessorMask = 1;
66 cachedsi.dwNumberOfProcessors = 1;
67 cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
68 cachedsi.dwAllocationGranularity = 0x10000;
69 cachedsi.wProcessorLevel = 3; /* 386 */
70 cachedsi.wProcessorRevision = 0;
72 cache = 1; /* even if there is no more info, we now have a cacheentry */
73 memcpy(si,&cachedsi,sizeof(*si));
75 /* Hmm, reasonable processor feature defaults? */
77 /* Create this registry key for all systems */
78 if (RegCreateKey16(HKEY_LOCAL_MACHINE,"HARDWARE\\DESCRIPTION\\System\\CentralProcessor",&hkey)!=ERROR_SUCCESS) {
79 WARN("Unable to register CPU information\n");
82 #ifdef linux
84 char buf[20];
85 char line[200];
86 FILE *f = fopen ("/proc/cpuinfo", "r");
88 if (!f)
89 return;
90 xhkey = 0;
91 while (fgets(line,200,f)!=NULL) {
92 char *s,*value;
94 /* NOTE: the ':' is the only character we can rely on */
95 if (!(value = strchr(line,':')))
96 continue;
97 /* terminate the valuename */
98 *value++ = '\0';
99 /* skip any leading spaces */
100 while (*value==' ') value++;
101 if ((s=strchr(value,'\n')))
102 *s='\0';
104 /* 2.1 method */
105 if (!lstrncmpiA(line, "cpu family",strlen("cpu family"))) {
106 if (isdigit (value[0])) {
107 switch (value[0] - '0') {
108 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
109 cachedsi.wProcessorLevel= 3;
110 break;
111 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
112 cachedsi.wProcessorLevel= 4;
113 break;
114 case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
115 cachedsi.wProcessorLevel= 5;
116 break;
117 case 6: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
118 cachedsi.wProcessorLevel= 5;
119 break;
120 default:
121 break;
124 /* set the CPU type of the current processor */
125 sprintf(buf,"CPU %ld",cachedsi.dwProcessorType);
126 if (xhkey)
127 RegSetValueExA(xhkey,"Identifier",0,REG_SZ,buf,strlen(buf));
128 continue;
130 /* old 2.0 method */
131 if (!lstrncmpiA(line, "cpu",strlen("cpu"))) {
132 if ( isdigit (value[0]) && value[1] == '8' &&
133 value[2] == '6' && value[3] == 0
135 switch (value[0] - '0') {
136 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
137 cachedsi.wProcessorLevel= 3;
138 break;
139 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
140 cachedsi.wProcessorLevel= 4;
141 break;
142 case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
143 cachedsi.wProcessorLevel= 5;
144 break;
145 case 6: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
146 cachedsi.wProcessorLevel= 5;
147 break;
148 default:
149 break;
152 /* set the CPU type of the current processor */
153 sprintf(buf,"CPU %ld",cachedsi.dwProcessorType);
154 if (xhkey)
155 RegSetValueExA(xhkey,"Identifier",0,REG_SZ,buf,strlen(buf));
156 continue;
158 if (!lstrncmpiA(line,"fdiv_bug",strlen("fdiv_bug"))) {
159 if (!lstrncmpiA(value,"yes",3))
160 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
162 continue;
164 if (!lstrncmpiA(line,"fpu",strlen("fpu"))) {
165 if (!lstrncmpiA(value,"no",2))
166 PF[PF_FLOATING_POINT_EMULATED] = TRUE;
168 continue;
170 if (!lstrncmpiA(line,"processor",strlen("processor"))) {
171 /* processor number counts up...*/
172 int x;
174 if (sscanf(value,"%d",&x))
175 if (x+1>cachedsi.dwNumberOfProcessors)
176 cachedsi.dwNumberOfProcessors=x+1;
178 /* Create a new processor subkey on a multiprocessor
179 * system
181 sprintf(buf,"%d",x);
182 if (xhkey)
183 RegCloseKey(xhkey);
184 RegCreateKey16(hkey,buf,&xhkey);
186 if (!lstrncmpiA(line,"stepping",strlen("stepping"))) {
187 int x;
189 if (sscanf(value,"%d",&x))
190 cachedsi.wProcessorRevision = x;
192 if (!lstrncmpiA(line,"flags",strlen("flags"))) {
193 if (strstr(value,"cx8"))
194 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
195 if (strstr(value,"mmx"))
196 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
200 fclose (f);
202 memcpy(si,&cachedsi,sizeof(*si));
203 #else /* linux */
204 /* FIXME: how do we do this on other systems? */
206 RegCreateKey16(hkey,"0",&xhkey);
207 RegSetValueExA(xhkey,"Identifier",0,REG_SZ,"CPU 386",strlen("CPU 386"));
208 #endif /* !linux */
209 if (xhkey)
210 RegCloseKey(xhkey);
211 if (hkey)
212 RegCloseKey(hkey);
216 /***********************************************************************
217 * IsProcessorFeaturePresent [KERNELL32.880]
218 * RETURNS:
219 * TRUE if processorfeature present
220 * FALSE otherwise
222 BOOL WINAPI IsProcessorFeaturePresent (
223 DWORD feature /* [in] feature number, see PF_ defines */
225 SYSTEM_INFO si;
226 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
228 if (feature < 64)
229 return PF[feature];
230 else
231 return FALSE;