source: trunk/src/kernel32/cpu.cpp@ 8463

Last change on this file since 8463 was 8463, checked in by sandervl, 23 years ago

logging updateshmstd.cpp

File size: 6.9 KB
Line 
1/* $Id: cpu.cpp,v 1.13 2002-05-22 12:57:16 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
27DEFAULT_DEBUG_CHANNEL(CPU)
28
29static BYTE PF[64] = {0,};
30static nrCPUs = 1;
31static SYSTEM_INFO cachedsi = {0};
32
33//******************************************************************************
34//******************************************************************************
35void 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 */
61VOID 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 if (cache) {
75 memcpy(si,&cachedsi,sizeof(*si));
76 return;
77 }
78 memset(PF,0,sizeof(PF));
79
80 /* choose sensible defaults ...
81 * FIXME: perhaps overrideable with precompiler flags?
82 */
83 cachedsi.u.x.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
84 cachedsi.dwPageSize = 4096;
85
86 /* FIXME: better values for the two entries below... */
87 cachedsi.lpMinimumApplicationAddress = (void *)0x00400000;
88 cachedsi.lpMaximumApplicationAddress = (void *)0x7FFFFFFF;
89 cachedsi.dwActiveProcessorMask = 1;
90 cachedsi.dwNumberOfProcessors = nrCPUs;
91 cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
92 cachedsi.dwAllocationGranularity = 0x10000;
93 cachedsi.wProcessorLevel = 3; /* 386 */
94 cachedsi.wProcessorRevision = 0;
95
96 cache = 1; /* even if there is no more info, we now have a cacheentry */
97 memcpy(si,&cachedsi,sizeof(*si));
98
99 /* Hmm, reasonable processor feature defaults? */
100
101 /* Create this registry key for all systems */
102 if (RegCreateKeyA(HKEY_LOCAL_MACHINE,"HARDWARE\\DESCRIPTION\\System\\CentralProcessor",&hkey)!=ERROR_SUCCESS) {
103 dprintf(("Unable to register CPU information\n"));
104 }
105
106 if(SupportsCPUID())
107 {
108 for(int i=0;i<cachedsi.dwNumberOfProcessors;i++)
109 {
110 // Create a new processor subkey
111 sprintf(buf,"%d",i);
112 if (xhkey)
113 RegCloseKey(xhkey);
114 RegCreateKeyA(hkey,buf,&xhkey);
115
116 signature = GetCPUSignature();
117
118 switch ((signature >> 8)&0xf) {
119 case 3: cachedsi.dwProcessorType = PROCESSOR_INTEL_386;
120 break;
121 case 4: cachedsi.dwProcessorType = PROCESSOR_INTEL_486;
122 break;
123 case 5: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
124 break;
125 case 6: cachedsi.dwProcessorType = PROCESSOR_INTEL_PENTIUM;
126 break;
127 default:
128 break;
129 }
130 cachedsi.wProcessorRevision = signature & 0xff;
131 cachedsi.wProcessorLevel = (signature >> 8)&0xf;
132
133 /* set the CPU type of the current processor */
134 sprintf(buf,"CPU %ld",cachedsi.dwProcessorType);
135 if (xhkey) {
136 RegSetValueExA(xhkey,"Identifier",0,REG_SZ,(LPBYTE)buf,strlen(buf));
137 memset(buf, 0, sizeof(buf));
138 GetCPUVendorString(buf);
139 RegSetValueExA(xhkey,"VendorIdentifier",0,REG_SZ,(LPBYTE)buf,strlen(buf));
140#ifdef __WIN32OS2__
141 if(i==0) {
142 DWORD mhz;
143 features = GetCPUFeatures();
144 if(features & CPUID_TIME_STAMP_COUNTER) {
145 LARGE_INTEGER tsc1, tsc2, freq, time1, time2;
146 double clockticks, millisec, frequency, tmp, tmp1, mhertz;
147
148 QueryPerformanceFrequency(&freq);
149
150 GetTSC((LONG *)&tsc1.LowPart, &tsc1.HighPart);
151 QueryPerformanceCounter(&time1);
152
153 Sleep(32); //sleep for about 32 ms
154
155 GetTSC((LONG *)&tsc2.LowPart, &tsc2.HighPart);
156 QueryPerformanceCounter(&time2);
157 tmp = (double)time2.LowPart + (double)time2.HighPart*4.0*1024.0*1024.0;
158 tmp1 = (double)time1.LowPart + (double)time1.HighPart*4.0*1024.0*1024.0;
159 millisec = tmp - tmp1;
160 frequency= (double)freq.LowPart;
161 frequency= frequency / 1000.0;
162 millisec = millisec / frequency;
163
164 tmp = (double)tsc2.LowPart + (double)tsc2.HighPart*4.0*1024.0*1024.0;
165 tmp1 = (double)tsc1.LowPart + (double)tsc1.HighPart*4.0*1024.0*1024.0;
166 clockticks = tmp - tmp1;
167
168 tmp = 1000 / millisec;
169 clockticks = clockticks * tmp; //ticks per second
170 mhertz = clockticks / 1000000.0;
171 mhz = (DWORD)mhertz;
172 }
173 else mhz = 100;
174 RegSetValueExA(xhkey,"~Mhz",0,REG_DWORD, (LPBYTE)&mhz, sizeof(DWORD));
175 }
176#endif
177 }
178//TODO: FPU fdiv bug
179#if 0
180 if (!lstrncmpiA(line,"fdiv_bug",strlen("fdiv_bug"))) {
181 if (!lstrncmpiA(value,"yes",3))
182 PF[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
183
184 continue;
185 }
186#endif
187 features = GetCPUFeatures();
188 if (features & CPUID_CMPXCHG8B_INSTRUCTION)
189 PF[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
190 if (features & CPUID_MMX)
191 PF[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
192
193 //Create FPU key if one is present
194 if (features & CPUID_FPU_PRESENT) {
195 if (i == 0) {
196 if(RegCreateKeyA(HKEY_LOCAL_MACHINE,"HARDWARE\\DESCRIPTION\\System\\FloatingPointProcessor",&fpukey)!=ERROR_SUCCESS)
197 dprintf(("Unable to register FPU information\n"));
198 }
199 // Create a new processor subkey
200 if(fpukey) {
201 sprintf(buf,"%d",i);
202 RegCreateKeyA(fpukey,buf,&xhfpukey);
203 }
204 }
205
206 } //for each cpu
207 }
208 memcpy(si,&cachedsi,sizeof(*si));
209
210 if (xhkey)
211 RegCloseKey(xhkey);
212 if (hkey)
213 RegCloseKey(hkey);
214 if (xhfpukey)
215 RegCloseKey(xhfpukey);
216 if (fpukey)
217 RegCloseKey(fpukey);
218
219}
220
221
222/***********************************************************************
223 * IsProcessorFeaturePresent [KERNELL32.880]
224 * RETURNS:
225 * TRUE if processorfeature present
226 * FALSE otherwise
227 */
228BOOL WINAPI IsProcessorFeaturePresent (DWORD feature) /* [in] feature number, see PF_ defines */
229{
230 SYSTEM_INFO si;
231 GetSystemInfo (&si); /* To ensure the information is loaded and cached */
232
233 if (feature < 64) {
234 dprintf(("IsProcessorFeaturePresent %x -> %x", feature, PF[feature]));
235 return PF[feature];
236 }
237 else
238 return FALSE;
239}
240//******************************************************************************
241//******************************************************************************
Note: See TracBrowser for help on using the repository browser.