1 | /* $Id: cpu.cpp,v 1.11 2000-10-03 17:28:29 sandervl Exp $ */
|
---|
2 | /*
|
---|
3 | * Odin win32 CPU apis
|
---|
4 | *
|
---|
5 | * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
6 | *
|
---|
7 | * Based on Wine Code (991031: misc\cpu.c)
|
---|
8 | *
|
---|
9 | * Copyright 1995,1997 Morten Welinder
|
---|
10 | * Copyright 1997-1998 Marcus Meissner
|
---|
11 | */
|
---|
12 |
|
---|
13 | #include <os2win.h>
|
---|
14 | #include <ctype.h>
|
---|
15 | #include <string.h>
|
---|
16 | #include "winreg.h"
|
---|
17 | #include "global.h"
|
---|
18 | #include "winnt.h"
|
---|
19 | #include "winerror.h"
|
---|
20 | #include "winreg.h"
|
---|
21 | #include "debugtools.h"
|
---|
22 | #include <cpuhlp.h>
|
---|
23 |
|
---|
24 | #define DBG_LOCALLOG DBG_cpu
|
---|
25 | #include "dbglocal.h"
|
---|
26 |
|
---|
27 | DEFAULT_DEBUG_CHANNEL(CPU)
|
---|
28 |
|
---|
29 | static BYTE PF[64] = {0,};
|
---|
30 | static nrCPUs = 1;
|
---|
31 | static SYSTEM_INFO cachedsi = {0};
|
---|
32 |
|
---|
33 | //******************************************************************************
|
---|
34 | //******************************************************************************
|
---|
35 | void InitSystemInfo(int nrcpus)
|
---|
36 | {
|
---|
37 | SYSTEM_INFO si;
|
---|
38 |
|
---|
39 | nrCPUs = nrcpus;
|
---|
40 | GetSystemInfo(&si);
|
---|
41 | }
|
---|
42 | /***********************************************************************
|
---|
43 | * GetSystemInfo [KERNELL32.404]
|
---|
44 | *
|
---|
45 | * Gets the current system information.
|
---|
46 | *
|
---|
47 | * On the first call it reads cached values, so it doesn't have to determine
|
---|
48 | * them repeatedly. On Linux, the /proc/cpuinfo special file is used.
|
---|
49 | *
|
---|
50 | * It creates a registry subhierarchy, looking like:
|
---|
51 | * \HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\
|
---|
52 | * Identifier (CPU x86)
|
---|
53 | * Note that there is a hierarchy for every processor installed, so this
|
---|
54 | * supports multiprocessor systems. This is done like Win95 does it, I think.
|
---|
55 | *
|
---|
56 | * It also creates a cached flag array for IsProcessorFeaturePresent().
|
---|
57 | *
|
---|
58 | * RETURNS
|
---|
59 | * nothing, really
|
---|
60 | */
|
---|
61 | VOID WINAPI GetSystemInfo(LPSYSTEM_INFO si) /* [out] system information */
|
---|
62 | {
|
---|
63 | HKEY xhkey=0,hkey;
|
---|
64 | HKEY fpukey=0, xhfpukey;
|
---|
65 | char buf[20];
|
---|
66 | DWORD features, signature;
|
---|
67 | static int cache = 0;
|
---|
68 |
|
---|
69 | if(!si) {
|
---|
70 | dprintf(("GetSystemInfo -> si == NULL!!"));
|
---|
71 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
72 | return;
|
---|
73 | }
|
---|
74 | dprintf(("GetSystemInfo"));
|
---|
75 | if (cache) {
|
---|
76 | memcpy(si,&cachedsi,sizeof(*si));
|
---|
77 | return;
|
---|
78 | }
|
---|
79 | memset(PF,0,sizeof(PF));
|
---|
80 |
|
---|
81 | /* choose sensible defaults ...
|
---|
82 | * FIXME: perhaps overrideable with precompiler flags?
|
---|
83 | */
|
---|
84 | cachedsi.u.x.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
|
---|
85 | cachedsi.dwPageSize = 4096;
|
---|
86 |
|
---|
87 | /* FIXME: better values for the two entries below... */
|
---|
88 | cachedsi.lpMinimumApplicationAddress = (void *)0x00400000;
|
---|
89 | cachedsi.lpMaximumApplicationAddress = (void *)0x7FFFFFFF;
|
---|
90 | cachedsi.dwActiveProcessorMask = 1;
|
---|
91 | cachedsi.dwNumberOfProcessors = nrCPUs;
|
---|
92 | cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
|
---|
93 | cachedsi.dwAllocationGranularity = 0x10000;
|
---|
94 | cachedsi.wProcessorLevel = 3; /* 386 */
|
---|
95 | cachedsi.wProcessorRevision = 0;
|
---|
96 |
|
---|
97 | cache = 1; /* even if there is no more info, we now have a cacheentry */
|
---|
98 | memcpy(si,&cachedsi,sizeof(*si));
|
---|
99 |
|
---|
100 | /* Hmm, reasonable processor feature defaults? */
|
---|
101 |
|
---|
102 | /* Create this registry key for all systems */
|
---|
103 | if (RegCreateKeyA(HKEY_LOCAL_MACHINE,"HARDWARE\\DESCRIPTION\\System\\CentralProcessor",&hkey)!=ERROR_SUCCESS) {
|
---|
104 | dprintf(("Unable to register CPU information\n"));
|
---|
105 | }
|
---|
106 |
|
---|
107 | if(SupportsCPUID())
|
---|
108 | {
|
---|
109 | for(int i=0;i<cachedsi.dwNumberOfProcessors;i++)
|
---|
110 | {
|
---|
111 | // Create a new processor subkey
|
---|
112 | sprintf(buf,"%d",i);
|
---|
113 | if (xhkey)
|
---|
114 | RegCloseKey(xhkey);
|
---|
115 | RegCreateKeyA(hkey,buf,&xhkey);
|
---|
116 |
|
---|
117 | signature = GetCPUSignature();
|
---|
118 |
|
---|
119 | switch ((signature >> 8)&0xf) {
|
---|
120 | case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
|
---|
121 | break;
|
---|
122 | case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
|
---|
123 | break;
|
---|
124 | case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
|
---|
125 | break;
|
---|
126 | case 6: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
|
---|
127 | break;
|
---|
128 | default:
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | cachedsi.wProcessorRevision = signature & 0xff;
|
---|
132 | cachedsi.wProcessorLevel = (signature >> 8)&0xf;
|
---|
133 |
|
---|
134 | /* set the CPU type of the current processor */
|
---|
135 | sprintf(buf,"CPU %ld",cachedsi.dwProcessorType);
|
---|
136 | if (xhkey) {
|
---|
137 | RegSetValueExA(xhkey,"Identifier",0,REG_SZ,(LPBYTE)buf,strlen(buf));
|
---|
138 | memset(buf, 0, sizeof(buf));
|
---|
139 | GetCPUVendorString(buf);
|
---|
140 | RegSetValueExA(xhkey,"VendorIdentifier",0,REG_SZ,(LPBYTE)buf,strlen(buf));
|
---|
141 | #ifdef __WIN32OS2__
|
---|
142 | if(i==0) {
|
---|
143 | DWORD mhz;
|
---|
144 | features = GetCPUFeatures();
|
---|
145 | if(features & CPUID_TIME_STAMP_COUNTER) {
|
---|
146 | LARGE_INTEGER tsc1, tsc2, freq, time1, time2;
|
---|
147 | double clockticks, millisec, frequency, tmp, tmp1, mhertz;
|
---|
148 |
|
---|
149 | QueryPerformanceFrequency(&freq);
|
---|
150 |
|
---|
151 | GetTSC((LONG *)&tsc1.LowPart, &tsc1.HighPart);
|
---|
152 | QueryPerformanceCounter(&time1);
|
---|
153 |
|
---|
154 | Sleep(32); //sleep for about 32 ms
|
---|
155 |
|
---|
156 | GetTSC((LONG *)&tsc2.LowPart, &tsc2.HighPart);
|
---|
157 | QueryPerformanceCounter(&time2);
|
---|
158 | tmp = (double)time2.LowPart + (double)time2.HighPart*4.0*1024.0*1024.0;
|
---|
159 | tmp1 = (double)time1.LowPart + (double)time1.HighPart*4.0*1024.0*1024.0;
|
---|
160 | millisec = tmp - tmp1;
|
---|
161 | frequency= (double)freq.LowPart;
|
---|
162 | frequency= frequency / 1000.0;
|
---|
163 | millisec = millisec / frequency;
|
---|
164 |
|
---|
165 | tmp = (double)tsc2.LowPart + (double)tsc2.HighPart*4.0*1024.0*1024.0;
|
---|
166 | tmp1 = (double)tsc1.LowPart + (double)tsc1.HighPart*4.0*1024.0*1024.0;
|
---|
167 | clockticks = tmp - tmp1;
|
---|
168 |
|
---|
169 | tmp = 1000 / millisec;
|
---|
170 | clockticks = clockticks * tmp; //ticks per second
|
---|
171 | mhertz = clockticks / 1000000.0;
|
---|
172 | mhz = (DWORD)mhertz;
|
---|
173 | }
|
---|
174 | else mhz = 100;
|
---|
175 | RegSetValueExA(xhkey,"~Mhz",0,REG_DWORD, (LPBYTE)&mhz, sizeof(DWORD));
|
---|
176 | }
|
---|
177 | #endif
|
---|
178 | }
|
---|
179 | //TODO: FPU fdiv bug
|
---|
180 | #if 0
|
---|
181 | if (!lstrncmpiA(line,"fdiv_bug",strlen("fdiv_bug"))) {
|
---|
182 | if (!lstrncmpiA(value,"yes",3))
|
---|
183 | PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
|
---|
184 |
|
---|
185 | continue;
|
---|
186 | }
|
---|
187 | #endif
|
---|
188 | features = GetCPUFeatures();
|
---|
189 | if (features & CPUID_CMPXCHG8B_INSTRUCTION)
|
---|
190 | PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
|
---|
191 | if (features & CPUID_MMX)
|
---|
192 | PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
|
---|
193 |
|
---|
194 | //Create FPU key if one is present
|
---|
195 | if (features & CPUID_FPU_PRESENT) {
|
---|
196 | if (i == 0) {
|
---|
197 | if(RegCreateKeyA(HKEY_LOCAL_MACHINE,"HARDWARE\\DESCRIPTION\\System\\FloatingPointProcessor",&fpukey)!=ERROR_SUCCESS)
|
---|
198 | dprintf(("Unable to register FPU information\n"));
|
---|
199 | }
|
---|
200 | // Create a new processor subkey
|
---|
201 | if(fpukey) {
|
---|
202 | sprintf(buf,"%d",i);
|
---|
203 | RegCreateKeyA(fpukey,buf,&xhfpukey);
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | } //for each cpu
|
---|
208 | }
|
---|
209 | memcpy(si,&cachedsi,sizeof(*si));
|
---|
210 |
|
---|
211 | if (xhkey)
|
---|
212 | RegCloseKey(xhkey);
|
---|
213 | if (hkey)
|
---|
214 | RegCloseKey(hkey);
|
---|
215 | if (xhfpukey)
|
---|
216 | RegCloseKey(xhfpukey);
|
---|
217 | if (fpukey)
|
---|
218 | RegCloseKey(fpukey);
|
---|
219 |
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | /***********************************************************************
|
---|
224 | * IsProcessorFeaturePresent [KERNELL32.880]
|
---|
225 | * RETURNS:
|
---|
226 | * TRUE if processorfeature present
|
---|
227 | * FALSE otherwise
|
---|
228 | */
|
---|
229 | BOOL WINAPI IsProcessorFeaturePresent (DWORD feature) /* [in] feature number, see PF_ defines */
|
---|
230 | {
|
---|
231 | SYSTEM_INFO si;
|
---|
232 | GetSystemInfo (&si); /* To ensure the information is loaded and cached */
|
---|
233 |
|
---|
234 | if (feature < 64)
|
---|
235 | return PF[feature];
|
---|
236 | else
|
---|
237 | return FALSE;
|
---|
238 | }
|
---|
239 | //******************************************************************************
|
---|
240 | //******************************************************************************
|
---|