blob: 0fe048975b2e29fc8f11a8088a6d20e768636dbb [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:
103 case 0x50: case 0x51: case 0x52: case 0x56: case 0x57:
104 case 0x5b: case 0x5c: case 0x5d:
105 case 0xb0: case 0xb1: case 0xb3: case 0xb4:
nethercoteb35a8b92004-09-11 16:45:27 +0000106 break;
107
108 case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break;
109 case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break;
110 case 0x30: *I1c = (cache_t) { 32, 8, 64 }; break;
111
112 case 0x0a: *D1c = (cache_t) { 8, 2, 32 }; break;
113 case 0x0c: *D1c = (cache_t) { 16, 4, 32 }; break;
114 case 0x2c: *D1c = (cache_t) { 32, 8, 64 }; break;
115
116 /* IA-64 info -- panic! */
117 case 0x10: case 0x15: case 0x1a:
118 case 0x88: case 0x89: case 0x8a: case 0x8d:
119 case 0x90: case 0x96: case 0x9b:
njn67993252004-11-22 18:02:32 +0000120 VG_(tool_panic)("IA-64 cache detected?!");
nethercoteb35a8b92004-09-11 16:45:27 +0000121
tom70c5e5a2009-01-02 10:42:27 +0000122 case 0x22: case 0x23: case 0x25: case 0x29:
123 case 0x46: case 0x47: case 0x4a: case 0x4b: case 0x4c: case 0x4d:
weidendo1c3e3c52006-11-23 13:04:30 +0000124 VG_(message)(Vg_DebugMsg,
125 "warning: L3 cache detected but ignored");
nethercoteb35a8b92004-09-11 16:45:27 +0000126 break;
127
128 /* These are sectored, whatever that means */
129 case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break;
130 case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break;
131
132 /* If a P6 core, this means "no L2 cache".
133 If a P4 core, this means "no L3 cache".
134 We don't know what core it is, so don't issue a warning. To detect
135 a missing L2 cache, we use 'L2_found'. */
136 case 0x40:
137 break;
138
139 case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break;
140 case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break;
141 case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break;
142 case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break;
143 case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break;
tom70c5e5a2009-01-02 10:42:27 +0000144 case 0x48:
145 /* Real L2 cache configuration is:
146 *L2c = (cache_t) { 3072, 12, 64 }; L2_found = True; */
147 VG_(message)(Vg_DebugMsg, "warning: 3Mb L2 cache detected, treating as 2Mb\n");
148 *L2c = (cache_t) { 2048, 8, 64 }; L2_found = True;
149 break;
weidendo1c3e3c52006-11-23 13:04:30 +0000150 case 0x49:
151 if ((family == 15) && (model == 6))
152 /* On Xeon MP (family F, model 6), this is for L3 */
153 VG_(message)(Vg_DebugMsg,
154 "warning: L3 cache detected but ignored\n");
155 else
156 *L2c = (cache_t) { 4096, 16, 64 }; L2_found = True;
157 break;
tom70c5e5a2009-01-02 10:42:27 +0000158 case 0x4e:
159 /* Real L2 cache configuration is:
160 *L2c = (cache_t) { 6144, 24, 64 }; L2_found = True; */
161 VG_(message)(Vg_DebugMsg, "warning: 6Mb L2 cache detected, treating as 4Mb\n");
162 *L2c = (cache_t) { 4096, 16, 64 }; L2_found = True;
163 break;
nethercoteb35a8b92004-09-11 16:45:27 +0000164
165 /* These are sectored, whatever that means */
nethercoteac7ecd72004-10-13 11:30:14 +0000166 case 0x60: *D1c = (cache_t) { 16, 8, 64 }; break; /* sectored */
nethercoteb35a8b92004-09-11 16:45:27 +0000167 case 0x66: *D1c = (cache_t) { 8, 4, 64 }; break; /* sectored */
168 case 0x67: *D1c = (cache_t) { 16, 4, 64 }; break; /* sectored */
169 case 0x68: *D1c = (cache_t) { 32, 4, 64 }; break; /* sectored */
170
171 /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based.
172 * conversion to byte size is a total guess; treat the 12K and 16K
173 * cases the same since the cache byte size must be a power of two for
174 * everything to work!. Also guessing 32 bytes for the line size...
175 */
176 case 0x70: /* 12K micro-ops, 8-way */
177 *I1c = (cache_t) { 16, 8, 32 };
178 micro_ops_warn(12, 16, 32);
179 break;
180 case 0x71: /* 16K micro-ops, 8-way */
181 *I1c = (cache_t) { 16, 8, 32 };
182 micro_ops_warn(16, 16, 32);
183 break;
184 case 0x72: /* 32K micro-ops, 8-way */
185 *I1c = (cache_t) { 32, 8, 32 };
186 micro_ops_warn(32, 32, 32);
187 break;
188
189 /* These are sectored, whatever that means */
190 case 0x79: *L2c = (cache_t) { 128, 8, 64 }; L2_found = True; break;
191 case 0x7a: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break;
192 case 0x7b: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
193 case 0x7c: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
njn8bc85822005-07-20 04:32:44 +0000194 case 0x7d: *L2c = (cache_t) { 2048, 8, 64 }; L2_found = True; break;
nethercoteb35a8b92004-09-11 16:45:27 +0000195 case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break;
196
197 case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break;
198 case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break;
199 case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break;
200 case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break;
201 case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break;
202 case 0x86: *L2c = (cache_t) { 512, 4, 64 }; L2_found = True; break;
203 case 0x87: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
204
tom942d9ef2005-07-27 22:59:50 +0000205 /* Ignore prefetch information */
206 case 0xf0: case 0xf1:
207 break;
208
nethercoteb35a8b92004-09-11 16:45:27 +0000209 default:
210 VG_(message)(Vg_DebugMsg,
211 "warning: Unknown Intel cache config value "
212 "(0x%x), ignoring", info[i]);
213 break;
214 }
215 }
216
217 if (!L2_found)
218 VG_(message)(Vg_DebugMsg,
219 "warning: L2 cache not installed, ignore L2 results.");
220
221 return 0;
222}
223
224/* AMD method is straightforward, just extract appropriate bits from the
225 * result registers.
226 *
227 * Bits, for D1 and I1:
228 * 31..24 data L1 cache size in KBs
229 * 23..16 data L1 cache associativity (FFh=full)
230 * 15.. 8 data L1 cache lines per tag
231 * 7.. 0 data L1 cache line size in bytes
232 *
233 * Bits, for L2:
234 * 31..16 unified L2 cache size in KBs
235 * 15..12 unified L2 cache associativity (0=off, FFh=full)
236 * 11.. 8 unified L2 cache lines per tag
237 * 7.. 0 unified L2 cache line size in bytes
238 *
239 * #3 The AMD K7 processor's L2 cache must be configured prior to relying
240 * upon this information. (Whatever that means -- njn)
241 *
242 * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model
243 * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB),
244 * so we detect that.
245 *
246 * Returns 0 on success, non-zero on failure.
247 */
248static
249Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c)
250{
251 UInt ext_level;
252 UInt dummy, model;
253 UInt I1i, D1i, L2i;
254
255 VG_(cpuid)(0x80000000, &ext_level, &dummy, &dummy, &dummy);
256
257 if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) {
258 VG_(message)(Vg_UserMsg,
259 "warning: ext_level < 0x80000006 for AMD processor (0x%x)",
260 ext_level);
261 return -1;
262 }
263
264 VG_(cpuid)(0x80000005, &dummy, &dummy, &D1i, &I1i);
265 VG_(cpuid)(0x80000006, &dummy, &dummy, &L2i, &dummy);
266
267 VG_(cpuid)(0x1, &model, &dummy, &dummy, &dummy);
268
269 /* Check for Duron bug */
270 if (model == 0x630) {
271 VG_(message)(Vg_UserMsg,
272 "Buggy Duron stepping A0. Assuming L2 size=65536 bytes");
273 L2i = (64 << 16) | (L2i & 0xffff);
274 }
275
276 D1c->size = (D1i >> 24) & 0xff;
277 D1c->assoc = (D1i >> 16) & 0xff;
278 D1c->line_size = (D1i >> 0) & 0xff;
279
280 I1c->size = (I1i >> 24) & 0xff;
281 I1c->assoc = (I1i >> 16) & 0xff;
282 I1c->line_size = (I1i >> 0) & 0xff;
283
284 L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */
285 L2c->assoc = (L2i >> 12) & 0xf;
286 L2c->line_size = (L2i >> 0) & 0xff;
287
288 return 0;
289}
290
nethercoteb35a8b92004-09-11 16:45:27 +0000291static
292Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c)
293{
sewardjb5f6f512005-03-10 23:59:00 +0000294 Int level, ret;
nethercoteb35a8b92004-09-11 16:45:27 +0000295 Char vendor_id[13];
nethercoteb35a8b92004-09-11 16:45:27 +0000296
sewardjb5f6f512005-03-10 23:59:00 +0000297 if (!VG_(has_cpuid)()) {
nethercoteb35a8b92004-09-11 16:45:27 +0000298 VG_(message)(Vg_DebugMsg, "CPUID instruction not supported");
nethercoteb35a8b92004-09-11 16:45:27 +0000299 return -1;
300 }
tomf4ed0592005-04-02 17:30:19 +0000301
sewardjb5f6f512005-03-10 23:59:00 +0000302 VG_(cpuid)(0, &level, (int*)&vendor_id[0],
303 (int*)&vendor_id[8], (int*)&vendor_id[4]);
304 vendor_id[12] = '\0';
nethercoteb35a8b92004-09-11 16:45:27 +0000305
306 if (0 == level) {
307 VG_(message)(Vg_DebugMsg, "CPUID level is 0, early Pentium?\n");
308 return -1;
309 }
310
311 /* Only handling Intel and AMD chips... no Cyrix, Transmeta, etc */
312 if (0 == VG_(strcmp)(vendor_id, "GenuineIntel")) {
313 ret = Intel_cache_info(level, I1c, D1c, L2c);
314
315 } else if (0 == VG_(strcmp)(vendor_id, "AuthenticAMD")) {
316 ret = AMD_cache_info(I1c, D1c, L2c);
317
318 } else if (0 == VG_(strcmp)(vendor_id, "CentaurHauls")) {
319 /* Total kludge. Pretend to be a VIA Nehemiah. */
320 D1c->size = 64;
321 D1c->assoc = 16;
322 D1c->line_size = 16;
323 I1c->size = 64;
324 I1c->assoc = 4;
325 I1c->line_size = 16;
326 L2c->size = 64;
327 L2c->assoc = 16;
328 L2c->line_size = 16;
329 ret = 0;
330
331 } else {
332 VG_(message)(Vg_DebugMsg, "CPU vendor ID not recognised (%s)",
333 vendor_id);
334 return -1;
335 }
336
337 /* Successful! Convert sizes from KB to bytes */
338 I1c->size *= 1024;
339 D1c->size *= 1024;
340 L2c->size *= 1024;
341
342 return ret;
343}
344
345
njnaf839f52005-06-23 03:27:57 +0000346void VG_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
347 Bool all_caches_clo_defined)
nethercoteb35a8b92004-09-11 16:45:27 +0000348{
349 Int res;
350
351 // Set caches to default.
njna1d1a642004-11-26 18:36:02 +0000352 *I1c = (cache_t) { 65536, 2, 64 };
353 *D1c = (cache_t) { 65536, 2, 64 };
354 *L2c = (cache_t) { 262144, 8, 64 };
nethercoteb35a8b92004-09-11 16:45:27 +0000355
356 // Then replace with any info we can get from CPUID.
357 res = get_caches_from_CPUID(I1c, D1c, L2c);
358
359 // Warn if CPUID failed and config not completely specified from cmd line.
360 if (res != 0 && !all_caches_clo_defined) {
361 VG_(message)(Vg_DebugMsg,
362 "Warning: Couldn't auto-detect cache config, using one "
363 "or more defaults ");
364 }
365}
366
367/*--------------------------------------------------------------------*/
368/*--- end ---*/
369/*--------------------------------------------------------------------*/