blob: 164f944e61aaca23823368598a771b82cd5808df [file] [log] [blame]
nethercoteb35a8b92004-09-11 16:45:27 +00001
2/*--------------------------------------------------------------------*/
njn528b07e2005-06-10 04:46:19 +00003/*--- x86-specific definitions. cg-x86.c ---*/
nethercoteb35a8b92004-09-11 16:45:27 +00004/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Cachegrind, a Valgrind tool for cache
8 profiling programs.
9
sewardj4d474d02008-02-11 11:34:59 +000010 Copyright (C) 2002-2008 Nicholas Nethercote
njn2bc10122005-05-08 02:10:27 +000011 njn@valgrind.org
nethercoteb35a8b92004-09-11 16:45:27 +000012
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
njnc7561b92005-06-19 01:24:32 +000031#include "pub_tool_basics.h"
njn68980862005-06-18 18:31:26 +000032#include "pub_tool_cpuid.h"
njn97405b22005-06-02 03:39:33 +000033#include "pub_tool_libcbase.h"
njnf39e9a32005-06-12 02:43:17 +000034#include "pub_tool_libcassert.h"
njn36a20fa2005-06-03 03:08:39 +000035#include "pub_tool_libcprint.h"
njnc7561b92005-06-19 01:24:32 +000036
nethercoteb35a8b92004-09-11 16:45:27 +000037#include "cg_arch.h"
38
39// All CPUID info taken from sandpile.org/a32/cpuid.htm */
40// Probably only works for Intel and AMD chips, and probably only for some of
41// them.
42
43static void micro_ops_warn(Int actual_size, Int used_size, Int line_size)
44{
45 VG_(message)(Vg_DebugMsg,
sewardjc32ba462005-10-19 23:49:45 +000046 "warning: Pentium 4 with %d KB micro-op instruction trace cache",
nethercoteb35a8b92004-09-11 16:45:27 +000047 actual_size);
48 VG_(message)(Vg_DebugMsg,
sewardjc32ba462005-10-19 23:49:45 +000049 " Simulating a %d KB I-cache with %d B lines",
nethercoteb35a8b92004-09-11 16:45:27 +000050 used_size, line_size);
51}
52
53/* Intel method is truly wretched. We have to do an insane indexing into an
54 * array of pre-defined configurations for various parts of the memory
weidendo1c3e3c52006-11-23 13:04:30 +000055 * hierarchy.
56 * According to Intel Processor Identification, App Note 485.
nethercoteb35a8b92004-09-11 16:45:27 +000057 */
58static
59Int Intel_cache_info(Int level, cache_t* I1c, cache_t* D1c, cache_t* L2c)
60{
weidendo1c3e3c52006-11-23 13:04:30 +000061 Int cpuid1_eax;
62 Int cpuid1_ignore;
63 Int family;
64 Int model;
nethercoteb35a8b92004-09-11 16:45:27 +000065 UChar info[16];
66 Int i, trials;
67 Bool L2_found = False;
68
69 if (level < 2) {
70 VG_(message)(Vg_DebugMsg,
71 "warning: CPUID level < 2 for Intel processor (%d)",
72 level);
73 return -1;
74 }
75
weidendo1c3e3c52006-11-23 13:04:30 +000076 /* family/model needed to distinguish code reuse (currently 0x49) */
77 VG_(cpuid)(1, &cpuid1_eax, &cpuid1_ignore,
78 &cpuid1_ignore, &cpuid1_ignore);
79 family = (((cpuid1_eax >> 20) & 0xff) << 4) + ((cpuid1_eax >> 8) & 0xf);
80 model = (((cpuid1_eax >> 16) & 0xf) << 4) + ((cpuid1_eax >> 4) & 0xf);
81
nethercoteb35a8b92004-09-11 16:45:27 +000082 VG_(cpuid)(2, (Int*)&info[0], (Int*)&info[4],
83 (Int*)&info[8], (Int*)&info[12]);
84 trials = info[0] - 1; /* AL register - bits 0..7 of %eax */
85 info[0] = 0x0; /* reset AL */
86
87 if (0 != trials) {
88 VG_(message)(Vg_DebugMsg,
89 "warning: non-zero CPUID trials for Intel processor (%d)",
90 trials);
91 return -1;
92 }
93
94 for (i = 0; i < 16; i++) {
95
96 switch (info[i]) {
97
98 case 0x0: /* ignore zeros */
99 break;
100
101 /* TLB info, ignore */
weidendo966b5bd2006-10-12 14:23:38 +0000102 case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
tom1e76ff52009-01-02 11:07:18 +0000103 case 0x4f: case 0x50: case 0x51: case 0x52:
104 case 0x56: case 0x57: case 0x59:
weidendo966b5bd2006-10-12 14:23:38 +0000105 case 0x5b: case 0x5c: case 0x5d:
tom1e76ff52009-01-02 11:07:18 +0000106 case 0xb0: case 0xb1:
107 case 0xb3: case 0xb4: case 0xba: case 0xc0:
nethercoteb35a8b92004-09-11 16:45:27 +0000108 break;
109
110 case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break;
111 case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break;
112 case 0x30: *I1c = (cache_t) { 32, 8, 64 }; break;
113
114 case 0x0a: *D1c = (cache_t) { 8, 2, 32 }; break;
115 case 0x0c: *D1c = (cache_t) { 16, 4, 32 }; break;
tom1e76ff52009-01-02 11:07:18 +0000116 case 0x0e:
117 /* Real D1 cache configuration is:
118 D1c = (cache_t) { 24, 6, 64 }; */
119 VG_(message)(Vg_DebugMsg, "warning: 24Kb D1 cache detected, treating as 16Kb");
120 *D1c = (cache_t) { 16, 4, 64 };
121 break;
nethercoteb35a8b92004-09-11 16:45:27 +0000122 case 0x2c: *D1c = (cache_t) { 32, 8, 64 }; break;
123
124 /* IA-64 info -- panic! */
125 case 0x10: case 0x15: case 0x1a:
126 case 0x88: case 0x89: case 0x8a: case 0x8d:
127 case 0x90: case 0x96: case 0x9b:
njn67993252004-11-22 18:02:32 +0000128 VG_(tool_panic)("IA-64 cache detected?!");
nethercoteb35a8b92004-09-11 16:45:27 +0000129
tom70c5e5a2009-01-02 10:42:27 +0000130 case 0x22: case 0x23: case 0x25: case 0x29:
131 case 0x46: case 0x47: case 0x4a: case 0x4b: case 0x4c: case 0x4d:
weidendo1c3e3c52006-11-23 13:04:30 +0000132 VG_(message)(Vg_DebugMsg,
133 "warning: L3 cache detected but ignored");
nethercoteb35a8b92004-09-11 16:45:27 +0000134 break;
135
136 /* These are sectored, whatever that means */
137 case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break;
138 case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break;
139
140 /* If a P6 core, this means "no L2 cache".
141 If a P4 core, this means "no L3 cache".
142 We don't know what core it is, so don't issue a warning. To detect
143 a missing L2 cache, we use 'L2_found'. */
144 case 0x40:
145 break;
146
147 case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break;
148 case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break;
149 case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break;
150 case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break;
151 case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break;
tom70c5e5a2009-01-02 10:42:27 +0000152 case 0x48:
153 /* Real L2 cache configuration is:
154 *L2c = (cache_t) { 3072, 12, 64 }; L2_found = True; */
tom415a7b12009-01-02 11:03:55 +0000155 VG_(message)(Vg_DebugMsg, "warning: 3Mb L2 cache detected, treating as 2Mb");
tom70c5e5a2009-01-02 10:42:27 +0000156 *L2c = (cache_t) { 2048, 8, 64 }; L2_found = True;
157 break;
weidendo1c3e3c52006-11-23 13:04:30 +0000158 case 0x49:
159 if ((family == 15) && (model == 6))
160 /* On Xeon MP (family F, model 6), this is for L3 */
161 VG_(message)(Vg_DebugMsg,
tom415a7b12009-01-02 11:03:55 +0000162 "warning: L3 cache detected but ignored");
weidendo1c3e3c52006-11-23 13:04:30 +0000163 else
164 *L2c = (cache_t) { 4096, 16, 64 }; L2_found = True;
165 break;
tom70c5e5a2009-01-02 10:42:27 +0000166 case 0x4e:
167 /* Real L2 cache configuration is:
168 *L2c = (cache_t) { 6144, 24, 64 }; L2_found = True; */
tom415a7b12009-01-02 11:03:55 +0000169 VG_(message)(Vg_DebugMsg, "warning: 6Mb L2 cache detected, treating as 4Mb");
tom70c5e5a2009-01-02 10:42:27 +0000170 *L2c = (cache_t) { 4096, 16, 64 }; L2_found = True;
171 break;
nethercoteb35a8b92004-09-11 16:45:27 +0000172
173 /* These are sectored, whatever that means */
nethercoteac7ecd72004-10-13 11:30:14 +0000174 case 0x60: *D1c = (cache_t) { 16, 8, 64 }; break; /* sectored */
nethercoteb35a8b92004-09-11 16:45:27 +0000175 case 0x66: *D1c = (cache_t) { 8, 4, 64 }; break; /* sectored */
176 case 0x67: *D1c = (cache_t) { 16, 4, 64 }; break; /* sectored */
177 case 0x68: *D1c = (cache_t) { 32, 4, 64 }; break; /* sectored */
178
179 /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based.
180 * conversion to byte size is a total guess; treat the 12K and 16K
181 * cases the same since the cache byte size must be a power of two for
182 * everything to work!. Also guessing 32 bytes for the line size...
183 */
184 case 0x70: /* 12K micro-ops, 8-way */
185 *I1c = (cache_t) { 16, 8, 32 };
186 micro_ops_warn(12, 16, 32);
187 break;
188 case 0x71: /* 16K micro-ops, 8-way */
189 *I1c = (cache_t) { 16, 8, 32 };
190 micro_ops_warn(16, 16, 32);
191 break;
192 case 0x72: /* 32K micro-ops, 8-way */
193 *I1c = (cache_t) { 32, 8, 32 };
194 micro_ops_warn(32, 32, 32);
195 break;
196
197 /* These are sectored, whatever that means */
198 case 0x79: *L2c = (cache_t) { 128, 8, 64 }; L2_found = True; break;
199 case 0x7a: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break;
200 case 0x7b: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
201 case 0x7c: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
njn8bc85822005-07-20 04:32:44 +0000202 case 0x7d: *L2c = (cache_t) { 2048, 8, 64 }; L2_found = True; break;
nethercoteb35a8b92004-09-11 16:45:27 +0000203 case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break;
204
tom1e76ff52009-01-02 11:07:18 +0000205 case 0x7f: *L2c = (cache_t) { 512, 2, 64 }; L2_found = True; break;
206 case 0x80: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
207
nethercoteb35a8b92004-09-11 16:45:27 +0000208 case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break;
209 case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break;
210 case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break;
211 case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break;
212 case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break;
213 case 0x86: *L2c = (cache_t) { 512, 4, 64 }; L2_found = True; break;
214 case 0x87: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
215
tom942d9ef2005-07-27 22:59:50 +0000216 /* Ignore prefetch information */
217 case 0xf0: case 0xf1:
218 break;
219
nethercoteb35a8b92004-09-11 16:45:27 +0000220 default:
221 VG_(message)(Vg_DebugMsg,
222 "warning: Unknown Intel cache config value "
223 "(0x%x), ignoring", info[i]);
224 break;
225 }
226 }
227
228 if (!L2_found)
229 VG_(message)(Vg_DebugMsg,
230 "warning: L2 cache not installed, ignore L2 results.");
231
232 return 0;
233}
234
235/* AMD method is straightforward, just extract appropriate bits from the
236 * result registers.
237 *
238 * Bits, for D1 and I1:
239 * 31..24 data L1 cache size in KBs
240 * 23..16 data L1 cache associativity (FFh=full)
241 * 15.. 8 data L1 cache lines per tag
242 * 7.. 0 data L1 cache line size in bytes
243 *
244 * Bits, for L2:
245 * 31..16 unified L2 cache size in KBs
246 * 15..12 unified L2 cache associativity (0=off, FFh=full)
247 * 11.. 8 unified L2 cache lines per tag
248 * 7.. 0 unified L2 cache line size in bytes
249 *
250 * #3 The AMD K7 processor's L2 cache must be configured prior to relying
251 * upon this information. (Whatever that means -- njn)
252 *
253 * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model
254 * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB),
255 * so we detect that.
256 *
257 * Returns 0 on success, non-zero on failure.
258 */
259static
260Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c)
261{
262 UInt ext_level;
263 UInt dummy, model;
264 UInt I1i, D1i, L2i;
265
266 VG_(cpuid)(0x80000000, &ext_level, &dummy, &dummy, &dummy);
267
268 if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) {
269 VG_(message)(Vg_UserMsg,
270 "warning: ext_level < 0x80000006 for AMD processor (0x%x)",
271 ext_level);
272 return -1;
273 }
274
275 VG_(cpuid)(0x80000005, &dummy, &dummy, &D1i, &I1i);
276 VG_(cpuid)(0x80000006, &dummy, &dummy, &L2i, &dummy);
277
278 VG_(cpuid)(0x1, &model, &dummy, &dummy, &dummy);
279
280 /* Check for Duron bug */
281 if (model == 0x630) {
282 VG_(message)(Vg_UserMsg,
283 "Buggy Duron stepping A0. Assuming L2 size=65536 bytes");
284 L2i = (64 << 16) | (L2i & 0xffff);
285 }
286
287 D1c->size = (D1i >> 24) & 0xff;
288 D1c->assoc = (D1i >> 16) & 0xff;
289 D1c->line_size = (D1i >> 0) & 0xff;
290
291 I1c->size = (I1i >> 24) & 0xff;
292 I1c->assoc = (I1i >> 16) & 0xff;
293 I1c->line_size = (I1i >> 0) & 0xff;
294
295 L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */
296 L2c->assoc = (L2i >> 12) & 0xf;
297 L2c->line_size = (L2i >> 0) & 0xff;
298
299 return 0;
300}
301
nethercoteb35a8b92004-09-11 16:45:27 +0000302static
303Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c)
304{
sewardjb5f6f512005-03-10 23:59:00 +0000305 Int level, ret;
nethercoteb35a8b92004-09-11 16:45:27 +0000306 Char vendor_id[13];
nethercoteb35a8b92004-09-11 16:45:27 +0000307
sewardjb5f6f512005-03-10 23:59:00 +0000308 if (!VG_(has_cpuid)()) {
nethercoteb35a8b92004-09-11 16:45:27 +0000309 VG_(message)(Vg_DebugMsg, "CPUID instruction not supported");
nethercoteb35a8b92004-09-11 16:45:27 +0000310 return -1;
311 }
tomf4ed0592005-04-02 17:30:19 +0000312
sewardjb5f6f512005-03-10 23:59:00 +0000313 VG_(cpuid)(0, &level, (int*)&vendor_id[0],
314 (int*)&vendor_id[8], (int*)&vendor_id[4]);
315 vendor_id[12] = '\0';
nethercoteb35a8b92004-09-11 16:45:27 +0000316
317 if (0 == level) {
tom415a7b12009-01-02 11:03:55 +0000318 VG_(message)(Vg_DebugMsg, "CPUID level is 0, early Pentium?");
nethercoteb35a8b92004-09-11 16:45:27 +0000319 return -1;
320 }
321
322 /* Only handling Intel and AMD chips... no Cyrix, Transmeta, etc */
323 if (0 == VG_(strcmp)(vendor_id, "GenuineIntel")) {
324 ret = Intel_cache_info(level, I1c, D1c, L2c);
325
326 } else if (0 == VG_(strcmp)(vendor_id, "AuthenticAMD")) {
327 ret = AMD_cache_info(I1c, D1c, L2c);
328
329 } else if (0 == VG_(strcmp)(vendor_id, "CentaurHauls")) {
330 /* Total kludge. Pretend to be a VIA Nehemiah. */
331 D1c->size = 64;
332 D1c->assoc = 16;
333 D1c->line_size = 16;
334 I1c->size = 64;
335 I1c->assoc = 4;
336 I1c->line_size = 16;
337 L2c->size = 64;
338 L2c->assoc = 16;
339 L2c->line_size = 16;
340 ret = 0;
341
342 } else {
343 VG_(message)(Vg_DebugMsg, "CPU vendor ID not recognised (%s)",
344 vendor_id);
345 return -1;
346 }
347
348 /* Successful! Convert sizes from KB to bytes */
349 I1c->size *= 1024;
350 D1c->size *= 1024;
351 L2c->size *= 1024;
352
353 return ret;
354}
355
356
njnaf839f52005-06-23 03:27:57 +0000357void VG_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
358 Bool all_caches_clo_defined)
nethercoteb35a8b92004-09-11 16:45:27 +0000359{
360 Int res;
361
362 // Set caches to default.
njna1d1a642004-11-26 18:36:02 +0000363 *I1c = (cache_t) { 65536, 2, 64 };
364 *D1c = (cache_t) { 65536, 2, 64 };
365 *L2c = (cache_t) { 262144, 8, 64 };
nethercoteb35a8b92004-09-11 16:45:27 +0000366
367 // Then replace with any info we can get from CPUID.
368 res = get_caches_from_CPUID(I1c, D1c, L2c);
369
370 // Warn if CPUID failed and config not completely specified from cmd line.
371 if (res != 0 && !all_caches_clo_defined) {
372 VG_(message)(Vg_DebugMsg,
373 "Warning: Couldn't auto-detect cache config, using one "
374 "or more defaults ");
375 }
376}
377
378/*--------------------------------------------------------------------*/
379/*--- end ---*/
380/*--------------------------------------------------------------------*/