forked from wbhart/mpir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpuid.c
276 lines (254 loc) · 8.37 KB
/
cpuid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
Free Software Foundation, Inc.
Copyright 2008 William Hart.
Copyright 2009,2010,2011 Jason Moxham
Copyright 2010 Gonzalo Tornaria
This file is part of the MPIR Library.
The MPIR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
The MPIR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the MPIR Library; see the file COPYING.LIB. If not, write
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#define WANT_FAKE_BUILD_CPU 0
#define WANT_FAKE_FAT_CPU 0
#define FAKE_BUILD_CPU_VENDOR "MPIRSNOTFAKE"
#define FAKE_BUILD_CPU_FAMILY 0
#define FAKE_BUILD_CPU_EXTFAMILY 0
#define FAKE_BUILD_CPU_MODEL 0
#define FAKE_BUILD_CPU_EXTMODEL 0
#define FAKE_FAT_CPU_VENDOR "MPIRSNOTFAKE"
#define FAKE_FAT_CPU_FAMILY 0
#define FAKE_FAT_CPU_EXTFAMILY 0
#define FAKE_FAT_CPU_MODEL 0
#define FAKE_FAT_CPU_EXTMODEL 0
#if INFAT
#define WANT_FAKE_CPU WANT_FAKE_FAT_CPU
#define FAKE_CPU_VENDOR FAKE_FAT_CPU_VENDOR
#define FAKE_CPU_FAMILY FAKE_FAT_CPU_FAMILY
#define FAKE_CPU_EXTFAMILY FAKE_FAT_CPU_EXTFAMILY
#define FAKE_CPU_MODEL FAKE_FAT_CPU_MODEL
#define FAKE_CPU_EXTMODEL FAKE_FAT_CPU_EXTMODEL
#endif
#if CONFIG_GUESS
#define WANT_FAKE_CPU WANT_FAKE_BUILD_CPU
#define FAKE_CPU_VENDOR FAKE_BUILD_CPU_VENDOR
#define FAKE_CPU_FAMILY FAKE_BUILD_CPU_FAMILY
#define FAKE_CPU_EXTFAMILY FAKE_BUILD_CPU_EXTFAMILY
#define FAKE_CPU_MODEL FAKE_BUILD_CPU_MODEL
#define FAKE_CPU_EXTMODEL FAKE_BUILD_CPU_EXTMODEL
#endif
#if WANT_FAKE_CPU
long fake_cpuid(char *p,unsigned int level)
{unsigned int eax,feat801=0,feat2=0,family,extfamily,model,extmodel;
char *vendor;
// can set feat801=1 for netburstlahf
// can set feat2=256 for prescott
vendor=FAKE_CPU_VENDOR;
family=FAKE_CPU_FAMILY;
extfamily=FAKE_CPU_EXTFAMILY;
model=FAKE_CPU_MODEL;
extmodel=FAKE_CPU_EXTMODEL;
memset(p,0,12);
if(level==0){strncpy(p,vendor,12);return 1;}
if(level==1){eax=0+(model<<4)+(family<<8)+(0<<12)+(extmodel<<16)+(extfamily<<20);memcpy(p,&feat2,4);return eax;}
if(level==0x80000000){return 1;}
if(level==0x80000001){memcpy(p+8,&feat801,4);return 0;}
return 0;}
#endif
#if WANT_FAKE_CPU
#define __gmpn_cpuid fake_cpuid
#else
#if CONFIG_GUESS
#define __gmpn_cpuid(_x,_y) cpuid(_x,_y,1,0,0)
#endif
#endif
#define FEAT_HAS_AVX 0x10000000
#if CONFIG_GUESS
// use's the stringinzing directive #x ie #x expands to "x"
#define CPUIS(x) modelstr=#x
char* __gmpn_cpu(int *vector){
#endif
#if INFAT
#define CPUIS(x) do{TRACE(printf(" "#x"\n"));CPUSETUP_##x;}while(0)
char* __gmpn_cpu(struct cpuvec_t *vector){
struct cpuvec_t decided_cpuvec;
#endif
char vendor_string[13];
char features[12];
long fms;
int family, model, stepping;
char *modelstr=0;
#if INFAT
memset (&decided_cpuvec, '\0', sizeof (decided_cpuvec));
CPUVEC_SETUP_fat;
#if FAT32
CPUVEC_SETUP_x86;
#endif
#if FAT64
CPUVEC_SETUP_x86_64;
#endif
#endif
__gmpn_cpuid (vendor_string, 0);
vendor_string[12] = 0;
fms = __gmpn_cpuid (features, 1);
family = ((fms >> 8) & 15) + ((fms >> 20) & 0xff);
model = ((fms >> 4) & 15) + ((fms >> 12) & 0xf0);
stepping = fms & 15;
#if CONFIG_GUESS_64BIT
modelstr = "x86_64";
#else
modelstr = "i486";// shouldn't we make this x86??
#endif
if (strcmp (vendor_string, "GenuineIntel") == 0)
{
switch (family)
{
#if CONFIG_GUESS_32BIT || FAT32
case 5:
if (model <= 2) CPUIS(pentium);
if (model >= 4) CPUIS(pentiummmx);
break;
#endif
case 6:
#if CONFIG_GUESS_32BIT || FAT32
if (model == 1) { CPUIS(pentiumpro);break;}
if (model <= 6) { CPUIS(pentium2);break;}
if (model <= 13){ CPUIS(pentium3);break;}
if (model == 14){ CPUIS(core);break;}
if (model == 16){ CPUIS(core);break;}
#endif
if (model == 15){ CPUIS(core2);break;}
if (model == 17){ CPUIS(penryn);break;}
if (model == 22){ CPUIS(core2);break;}
if (model == 23){ CPUIS(penryn);break;}
if (model == 25){ CPUIS(westmere);break;}
if (model == 26){ CPUIS(nehalem);break;}
if (model == 28){ CPUIS(atom);break;}// 45nm
if (model == 29){ CPUIS(penryn);break;}
if (model == 30){ CPUIS(nehalem);break;}
if (model == 31){ CPUIS(nehalem);break;}
if (model == 37){ CPUIS(westmere);break;}
if (model == 38){ CPUIS(atom);break;}// atom z670 tunnel creek
if (model == 39){ CPUIS(atom);break;}// Intel Atom Z2460 (Medfield platform, Penwell SoC, Saltwell core)
if (model == 42){
int feat = ((int *)features)[2];
if (feat & FEAT_HAS_AVX) { CPUIS(sandybridge);break;}
else { CPUIS(westmere);break;} /* Really a crippled sandybridge with no avx */
}
if (model == 43){ CPUIS(sandybridge);break;}
if (model == 44){ CPUIS(westmere);break;}
if (model == 45){ CPUIS(sandybridge);break;}
if (model == 46){ CPUIS(nehalem);break;}
if (model == 47){ CPUIS(westmere);break;}
if (model == 54){ CPUIS(atom);break;}//DualCore Intel Atom D2700, 2133 MHz (16 x 133) (Cedarview, Saltwell core) 32nm
if (model == 55){ CPUIS(atom);break;}
if (model == 58){ CPUIS(ivybridge);break;}
if (model == 60){
int feat = ((int *)features)[2];
if (feat & FEAT_HAS_AVX) { CPUIS(haswellavx);break; } /* Core i Haswell */
else { CPUIS(haswell);break; } /* Celeron/Pentium Haswell without AVX */
}
if (model == 61){ CPUIS(broadwell);break;}
if (model == 62){ CPUIS(ivybridge);break;}
if (model == 63){ CPUIS(haswellavx);break;}
if (model == 69){ CPUIS(haswellavx);break;}
if (model == 70){ CPUIS(haswellavx);break;}
if (model == 71){ CPUIS(broadwell);break;}
if (model == 78){ CPUIS(skylakeavx);break;}
if (model == 79){ CPUIS(broadwell);break;}
if (model == 94){
int feat = ((int *)features)[2];
if (feat & FEAT_HAS_AVX) { CPUIS(skylakeavx);break; } /* Core i Skylake */
else { CPUIS(skylake);break; } /* Celeron/Pentium Skylake without AVX2 */
}
break;
case 15:
#if CONFIG_GUESS_64BIT || FAT64
__gmpn_cpuid(features,0x80000001);
if ( features[8]&1 ){ CPUIS(netburstlahf);break;}
CPUIS(netburst);break;
#endif
#if CONFIG_GUESS_32BIT || FAT32
if (model <= 6) { CPUIS(pentium4);break;}
int feat = ((int *)features)[2];
if (feat & 1) { CPUIS(prescott);break;}
#endif
break;
}
}
else if (strcmp (vendor_string, "AuthenticAMD") == 0)
{
switch (family)
{
#if CONFIG_GUESS_32BIT || FAT32
case 5:
if (model <= 3) { CPUIS(k5);break;}
if (model <= 7) { CPUIS(k6);break;}
if (model <= 8) { CPUIS(k62);break;}
if (model <= 9) { CPUIS(k63);break;}
break;
case 6:
CPUIS(k7);
break;
#endif
case 15:
CPUIS(k8);
break;
case 16:
if (model == 2) { CPUIS(k10);break; }
if (model == 4) { CPUIS(k102);break; }
if (model == 5) { CPUIS(k102);break; }
if (model == 6) { CPUIS(k102);break; }
if (model == 8) { CPUIS(k102);break; }
if (model == 9) { CPUIS(k102);break; }
if (model == 10) { CPUIS(k102);break; }
break;
case 17:
CPUIS(k8);// low power k8
break;
case 18:
CPUIS(k103);// like k102 but with hardware divider, this is lano
break;
case 20:
CPUIS(bobcat);// fusion of bobcat and GPU
break;
case 21:
if (model == 1) { CPUIS(bulldozer); break; }
if (model == 2) { CPUIS(piledriver); break; }
if (model == 3) { CPUIS(piledriver); break; }
if (model == 16) { CPUIS(piledriver); break; }
if (model == 18) { CPUIS(piledriver); break; }
if (model == 19) { CPUIS(piledriver); break; }
break;
/*
case 22:
CPUIS(jaguar); ?????
break;
*/
}
}
else if (strcmp (vendor_string, "CentaurHauls") == 0)
{
switch (family)
{
case 6:
if (model == 15){CPUIS(nano);break;}
#if CONFIG_GUESS_32BIT || FAT32
if (model < 9) { CPUIS(viac3);break;}
CPUIS(viac32);break;
#endif
}
}
#if INFAT
*vector=decided_cpuvec;
#endif
return modelstr;}