blob: be5eb82ecd3ff6723580bc39ff5a54df5086ed41 [file] [log] [blame]
nethercoteb35a8b92004-09-11 16:45:27 +00001
2/*--------------------------------------------------------------------*/
njn4f6f54b2009-01-07 02:34:06 +00003/*--- x86-specific (and AMD64-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
njn9f207462009-03-10 22:02:09 +000010 Copyright (C) 2002-2009 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{
njn6f74a7e2009-03-12 00:06:45 +000045 VG_DMSG("warning: Pentium 4 with %d KB micro-op instruction trace cache",
46 actual_size);
47 VG_DMSG(" Simulating a %d KB I-cache with %d B lines",
48 used_size, line_size);
nethercoteb35a8b92004-09-11 16:45:27 +000049}
50
51/* Intel method is truly wretched. We have to do an insane indexing into an
52 * array of pre-defined configurations for various parts of the memory
weidendo1c3e3c52006-11-23 13:04:30 +000053 * hierarchy.
54 * According to Intel Processor Identification, App Note 485.
nethercoteb35a8b92004-09-11 16:45:27 +000055 */
56static
57Int Intel_cache_info(Int level, cache_t* I1c, cache_t* D1c, cache_t* L2c)
58{
weidendo1c3e3c52006-11-23 13:04:30 +000059 Int cpuid1_eax;
60 Int cpuid1_ignore;
61 Int family;
62 Int model;
nethercoteb35a8b92004-09-11 16:45:27 +000063 UChar info[16];
64 Int i, trials;
65 Bool L2_found = False;
66
67 if (level < 2) {
njn6f74a7e2009-03-12 00:06:45 +000068 VG_DMSG("warning: CPUID level < 2 for Intel processor (%d)", level);
nethercoteb35a8b92004-09-11 16:45:27 +000069 return -1;
70 }
71
weidendo1c3e3c52006-11-23 13:04:30 +000072 /* family/model needed to distinguish code reuse (currently 0x49) */
73 VG_(cpuid)(1, &cpuid1_eax, &cpuid1_ignore,
74 &cpuid1_ignore, &cpuid1_ignore);
75 family = (((cpuid1_eax >> 20) & 0xff) << 4) + ((cpuid1_eax >> 8) & 0xf);
76 model = (((cpuid1_eax >> 16) & 0xf) << 4) + ((cpuid1_eax >> 4) & 0xf);
77
nethercoteb35a8b92004-09-11 16:45:27 +000078 VG_(cpuid)(2, (Int*)&info[0], (Int*)&info[4],
79 (Int*)&info[8], (Int*)&info[12]);
80 trials = info[0] - 1; /* AL register - bits 0..7 of %eax */
81 info[0] = 0x0; /* reset AL */
82
83 if (0 != trials) {
njn6f74a7e2009-03-12 00:06:45 +000084 VG_DMSG("warning: non-zero CPUID trials for Intel processor (%d)",
85 trials);
nethercoteb35a8b92004-09-11 16:45:27 +000086 return -1;
87 }
88
89 for (i = 0; i < 16; i++) {
90
91 switch (info[i]) {
92
93 case 0x0: /* ignore zeros */
94 break;
95
96 /* TLB info, ignore */
weidendo966b5bd2006-10-12 14:23:38 +000097 case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
tom1e76ff52009-01-02 11:07:18 +000098 case 0x4f: case 0x50: case 0x51: case 0x52:
99 case 0x56: case 0x57: case 0x59:
weidendo966b5bd2006-10-12 14:23:38 +0000100 case 0x5b: case 0x5c: case 0x5d:
tom1e76ff52009-01-02 11:07:18 +0000101 case 0xb0: case 0xb1:
102 case 0xb3: case 0xb4: case 0xba: case 0xc0:
nethercoteb35a8b92004-09-11 16:45:27 +0000103 break;
104
105 case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break;
106 case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break;
107 case 0x30: *I1c = (cache_t) { 32, 8, 64 }; break;
108
109 case 0x0a: *D1c = (cache_t) { 8, 2, 32 }; break;
110 case 0x0c: *D1c = (cache_t) { 16, 4, 32 }; break;
weidendo144b76c2009-01-26 22:56:14 +0000111 case 0x0e: *D1c = (cache_t) { 24, 6, 64 }; break;
nethercoteb35a8b92004-09-11 16:45:27 +0000112 case 0x2c: *D1c = (cache_t) { 32, 8, 64 }; break;
113
114 /* IA-64 info -- panic! */
115 case 0x10: case 0x15: case 0x1a:
116 case 0x88: case 0x89: case 0x8a: case 0x8d:
117 case 0x90: case 0x96: case 0x9b:
njn67993252004-11-22 18:02:32 +0000118 VG_(tool_panic)("IA-64 cache detected?!");
nethercoteb35a8b92004-09-11 16:45:27 +0000119
tom70c5e5a2009-01-02 10:42:27 +0000120 case 0x22: case 0x23: case 0x25: case 0x29:
121 case 0x46: case 0x47: case 0x4a: case 0x4b: case 0x4c: case 0x4d:
njn6f74a7e2009-03-12 00:06:45 +0000122 VG_DMSG("warning: L3 cache detected but ignored");
nethercoteb35a8b92004-09-11 16:45:27 +0000123 break;
124
125 /* These are sectored, whatever that means */
126 case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break;
127 case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break;
128
129 /* If a P6 core, this means "no L2 cache".
130 If a P4 core, this means "no L3 cache".
131 We don't know what core it is, so don't issue a warning. To detect
132 a missing L2 cache, we use 'L2_found'. */
133 case 0x40:
134 break;
135
136 case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break;
137 case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break;
138 case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break;
139 case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break;
140 case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break;
weidendo144b76c2009-01-26 22:56:14 +0000141 case 0x48: *L2c = (cache_t) { 3072,12, 64 }; L2_found = True; break;
weidendo1c3e3c52006-11-23 13:04:30 +0000142 case 0x49:
143 if ((family == 15) && (model == 6))
144 /* On Xeon MP (family F, model 6), this is for L3 */
njn6f74a7e2009-03-12 00:06:45 +0000145 VG_DMSG("warning: L3 cache detected but ignored");
weidendo1c3e3c52006-11-23 13:04:30 +0000146 else
147 *L2c = (cache_t) { 4096, 16, 64 }; L2_found = True;
148 break;
weidendo144b76c2009-01-26 22:56:14 +0000149 case 0x4e: *L2c = (cache_t) { 6144, 24, 64 }; L2_found = True; break;
nethercoteb35a8b92004-09-11 16:45:27 +0000150
151 /* These are sectored, whatever that means */
nethercoteac7ecd72004-10-13 11:30:14 +0000152 case 0x60: *D1c = (cache_t) { 16, 8, 64 }; break; /* sectored */
nethercoteb35a8b92004-09-11 16:45:27 +0000153 case 0x66: *D1c = (cache_t) { 8, 4, 64 }; break; /* sectored */
154 case 0x67: *D1c = (cache_t) { 16, 4, 64 }; break; /* sectored */
155 case 0x68: *D1c = (cache_t) { 32, 4, 64 }; break; /* sectored */
156
157 /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based.
158 * conversion to byte size is a total guess; treat the 12K and 16K
159 * cases the same since the cache byte size must be a power of two for
160 * everything to work!. Also guessing 32 bytes for the line size...
161 */
162 case 0x70: /* 12K micro-ops, 8-way */
163 *I1c = (cache_t) { 16, 8, 32 };
164 micro_ops_warn(12, 16, 32);
165 break;
166 case 0x71: /* 16K micro-ops, 8-way */
167 *I1c = (cache_t) { 16, 8, 32 };
168 micro_ops_warn(16, 16, 32);
169 break;
170 case 0x72: /* 32K micro-ops, 8-way */
171 *I1c = (cache_t) { 32, 8, 32 };
172 micro_ops_warn(32, 32, 32);
173 break;
174
175 /* These are sectored, whatever that means */
176 case 0x79: *L2c = (cache_t) { 128, 8, 64 }; L2_found = True; break;
177 case 0x7a: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break;
178 case 0x7b: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
179 case 0x7c: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
njn8bc85822005-07-20 04:32:44 +0000180 case 0x7d: *L2c = (cache_t) { 2048, 8, 64 }; L2_found = True; break;
nethercoteb35a8b92004-09-11 16:45:27 +0000181 case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break;
182
tom1e76ff52009-01-02 11:07:18 +0000183 case 0x7f: *L2c = (cache_t) { 512, 2, 64 }; L2_found = True; break;
184 case 0x80: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
185
nethercoteb35a8b92004-09-11 16:45:27 +0000186 case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break;
187 case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break;
188 case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break;
189 case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break;
190 case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break;
191 case 0x86: *L2c = (cache_t) { 512, 4, 64 }; L2_found = True; break;
192 case 0x87: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
193
tom942d9ef2005-07-27 22:59:50 +0000194 /* Ignore prefetch information */
195 case 0xf0: case 0xf1:
njn6f74a7e2009-03-12 00:06:45 +0000196 break;
tom942d9ef2005-07-27 22:59:50 +0000197
nethercoteb35a8b92004-09-11 16:45:27 +0000198 default:
njn6f74a7e2009-03-12 00:06:45 +0000199 VG_DMSG("warning: Unknown Intel cache config value (0x%x), ignoring",
200 info[i]);
201 break;
nethercoteb35a8b92004-09-11 16:45:27 +0000202 }
203 }
204
205 if (!L2_found)
njn6f74a7e2009-03-12 00:06:45 +0000206 VG_DMSG("warning: L2 cache not installed, ignore L2 results.");
nethercoteb35a8b92004-09-11 16:45:27 +0000207
208 return 0;
209}
210
211/* AMD method is straightforward, just extract appropriate bits from the
212 * result registers.
213 *
214 * Bits, for D1 and I1:
215 * 31..24 data L1 cache size in KBs
216 * 23..16 data L1 cache associativity (FFh=full)
217 * 15.. 8 data L1 cache lines per tag
218 * 7.. 0 data L1 cache line size in bytes
219 *
220 * Bits, for L2:
221 * 31..16 unified L2 cache size in KBs
222 * 15..12 unified L2 cache associativity (0=off, FFh=full)
223 * 11.. 8 unified L2 cache lines per tag
224 * 7.. 0 unified L2 cache line size in bytes
225 *
226 * #3 The AMD K7 processor's L2 cache must be configured prior to relying
227 * upon this information. (Whatever that means -- njn)
228 *
229 * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model
230 * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB),
231 * so we detect that.
232 *
233 * Returns 0 on success, non-zero on failure.
234 */
235static
236Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c)
237{
238 UInt ext_level;
239 UInt dummy, model;
240 UInt I1i, D1i, L2i;
241
242 VG_(cpuid)(0x80000000, &ext_level, &dummy, &dummy, &dummy);
243
244 if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) {
njn6f74a7e2009-03-12 00:06:45 +0000245 VG_DMSG("warning: ext_level < 0x80000006 for AMD processor (0x%x)",
246 ext_level);
nethercoteb35a8b92004-09-11 16:45:27 +0000247 return -1;
248 }
249
250 VG_(cpuid)(0x80000005, &dummy, &dummy, &D1i, &I1i);
251 VG_(cpuid)(0x80000006, &dummy, &dummy, &L2i, &dummy);
252
253 VG_(cpuid)(0x1, &model, &dummy, &dummy, &dummy);
254
255 /* Check for Duron bug */
256 if (model == 0x630) {
njn6f74a7e2009-03-12 00:06:45 +0000257 VG_DMSG("warning: Buggy Duron stepping A0. Assuming L2 size=65536 bytes");
nethercoteb35a8b92004-09-11 16:45:27 +0000258 L2i = (64 << 16) | (L2i & 0xffff);
259 }
260
261 D1c->size = (D1i >> 24) & 0xff;
262 D1c->assoc = (D1i >> 16) & 0xff;
263 D1c->line_size = (D1i >> 0) & 0xff;
264
265 I1c->size = (I1i >> 24) & 0xff;
266 I1c->assoc = (I1i >> 16) & 0xff;
267 I1c->line_size = (I1i >> 0) & 0xff;
268
269 L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */
270 L2c->assoc = (L2i >> 12) & 0xf;
271 L2c->line_size = (L2i >> 0) & 0xff;
272
273 return 0;
274}
275
nethercoteb35a8b92004-09-11 16:45:27 +0000276static
277Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c)
278{
sewardjb5f6f512005-03-10 23:59:00 +0000279 Int level, ret;
nethercoteb35a8b92004-09-11 16:45:27 +0000280 Char vendor_id[13];
nethercoteb35a8b92004-09-11 16:45:27 +0000281
sewardjb5f6f512005-03-10 23:59:00 +0000282 if (!VG_(has_cpuid)()) {
njn6f74a7e2009-03-12 00:06:45 +0000283 VG_DMSG("CPUID instruction not supported");
nethercoteb35a8b92004-09-11 16:45:27 +0000284 return -1;
285 }
tomf4ed0592005-04-02 17:30:19 +0000286
sewardjb5f6f512005-03-10 23:59:00 +0000287 VG_(cpuid)(0, &level, (int*)&vendor_id[0],
288 (int*)&vendor_id[8], (int*)&vendor_id[4]);
289 vendor_id[12] = '\0';
nethercoteb35a8b92004-09-11 16:45:27 +0000290
291 if (0 == level) {
njn6f74a7e2009-03-12 00:06:45 +0000292 VG_DMSG("CPUID level is 0, early Pentium?");
nethercoteb35a8b92004-09-11 16:45:27 +0000293 return -1;
294 }
295
296 /* Only handling Intel and AMD chips... no Cyrix, Transmeta, etc */
297 if (0 == VG_(strcmp)(vendor_id, "GenuineIntel")) {
298 ret = Intel_cache_info(level, I1c, D1c, L2c);
299
300 } else if (0 == VG_(strcmp)(vendor_id, "AuthenticAMD")) {
301 ret = AMD_cache_info(I1c, D1c, L2c);
302
303 } else if (0 == VG_(strcmp)(vendor_id, "CentaurHauls")) {
304 /* Total kludge. Pretend to be a VIA Nehemiah. */
305 D1c->size = 64;
306 D1c->assoc = 16;
307 D1c->line_size = 16;
308 I1c->size = 64;
309 I1c->assoc = 4;
310 I1c->line_size = 16;
311 L2c->size = 64;
312 L2c->assoc = 16;
313 L2c->line_size = 16;
314 ret = 0;
315
316 } else {
njn6f74a7e2009-03-12 00:06:45 +0000317 VG_DMSG("CPU vendor ID not recognised (%s)", vendor_id);
nethercoteb35a8b92004-09-11 16:45:27 +0000318 return -1;
319 }
320
321 /* Successful! Convert sizes from KB to bytes */
322 I1c->size *= 1024;
323 D1c->size *= 1024;
324 L2c->size *= 1024;
325
326 return ret;
327}
328
329
njnaf839f52005-06-23 03:27:57 +0000330void VG_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
331 Bool all_caches_clo_defined)
nethercoteb35a8b92004-09-11 16:45:27 +0000332{
333 Int res;
334
335 // Set caches to default.
njna1d1a642004-11-26 18:36:02 +0000336 *I1c = (cache_t) { 65536, 2, 64 };
337 *D1c = (cache_t) { 65536, 2, 64 };
338 *L2c = (cache_t) { 262144, 8, 64 };
nethercoteb35a8b92004-09-11 16:45:27 +0000339
340 // Then replace with any info we can get from CPUID.
341 res = get_caches_from_CPUID(I1c, D1c, L2c);
342
343 // Warn if CPUID failed and config not completely specified from cmd line.
344 if (res != 0 && !all_caches_clo_defined) {
njn6f74a7e2009-03-12 00:06:45 +0000345 VG_DMSG("Warning: Couldn't auto-detect cache config, using one "
346 "or more defaults ");
nethercoteb35a8b92004-09-11 16:45:27 +0000347 }
348}
349
350/*--------------------------------------------------------------------*/
351/*--- end ---*/
352/*--------------------------------------------------------------------*/