blob: 821a8c7746cc02d7a3db9d26e9147cca9db44b2d [file] [log] [blame]
nethercoteb35a8b92004-09-11 16:45:27 +00001
2/*--------------------------------------------------------------------*/
njn8b68b642009-06-24 00:37:09 +00003/*--- x86- and AMD64-specific definitions. cg-x86-amd64.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
njn8b68b642009-06-24 00:37:09 +000031#if defined(VGA_x86) || defined(VGA_amd64)
32
njnc7561b92005-06-19 01:24:32 +000033#include "pub_tool_basics.h"
njn68980862005-06-18 18:31:26 +000034#include "pub_tool_cpuid.h"
njn97405b22005-06-02 03:39:33 +000035#include "pub_tool_libcbase.h"
njnf39e9a32005-06-12 02:43:17 +000036#include "pub_tool_libcassert.h"
njn36a20fa2005-06-03 03:08:39 +000037#include "pub_tool_libcprint.h"
njnc7561b92005-06-19 01:24:32 +000038
nethercoteb35a8b92004-09-11 16:45:27 +000039#include "cg_arch.h"
40
41// All CPUID info taken from sandpile.org/a32/cpuid.htm */
42// Probably only works for Intel and AMD chips, and probably only for some of
43// them.
44
45static void micro_ops_warn(Int actual_size, Int used_size, Int line_size)
46{
njn6f74a7e2009-03-12 00:06:45 +000047 VG_DMSG("warning: Pentium 4 with %d KB micro-op instruction trace cache",
48 actual_size);
49 VG_DMSG(" Simulating a %d KB I-cache with %d B lines",
50 used_size, line_size);
nethercoteb35a8b92004-09-11 16:45:27 +000051}
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) {
njn6f74a7e2009-03-12 00:06:45 +000070 VG_DMSG("warning: CPUID level < 2 for Intel processor (%d)", level);
nethercoteb35a8b92004-09-11 16:45:27 +000071 return -1;
72 }
73
weidendo1c3e3c52006-11-23 13:04:30 +000074 /* family/model needed to distinguish code reuse (currently 0x49) */
75 VG_(cpuid)(1, &cpuid1_eax, &cpuid1_ignore,
76 &cpuid1_ignore, &cpuid1_ignore);
77 family = (((cpuid1_eax >> 20) & 0xff) << 4) + ((cpuid1_eax >> 8) & 0xf);
78 model = (((cpuid1_eax >> 16) & 0xf) << 4) + ((cpuid1_eax >> 4) & 0xf);
79
nethercoteb35a8b92004-09-11 16:45:27 +000080 VG_(cpuid)(2, (Int*)&info[0], (Int*)&info[4],
81 (Int*)&info[8], (Int*)&info[12]);
82 trials = info[0] - 1; /* AL register - bits 0..7 of %eax */
83 info[0] = 0x0; /* reset AL */
84
85 if (0 != trials) {
njn6f74a7e2009-03-12 00:06:45 +000086 VG_DMSG("warning: non-zero CPUID trials for Intel processor (%d)",
87 trials);
nethercoteb35a8b92004-09-11 16:45:27 +000088 return -1;
89 }
90
91 for (i = 0; i < 16; i++) {
92
93 switch (info[i]) {
94
95 case 0x0: /* ignore zeros */
96 break;
97
98 /* TLB info, ignore */
weidendo966b5bd2006-10-12 14:23:38 +000099 case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
tom1e76ff52009-01-02 11:07:18 +0000100 case 0x4f: case 0x50: case 0x51: case 0x52:
101 case 0x56: case 0x57: case 0x59:
weidendo966b5bd2006-10-12 14:23:38 +0000102 case 0x5b: case 0x5c: case 0x5d:
tom1e76ff52009-01-02 11:07:18 +0000103 case 0xb0: case 0xb1:
104 case 0xb3: case 0xb4: case 0xba: case 0xc0:
nethercoteb35a8b92004-09-11 16:45:27 +0000105 break;
106
107 case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break;
108 case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break;
109 case 0x30: *I1c = (cache_t) { 32, 8, 64 }; break;
110
111 case 0x0a: *D1c = (cache_t) { 8, 2, 32 }; break;
112 case 0x0c: *D1c = (cache_t) { 16, 4, 32 }; break;
weidendo144b76c2009-01-26 22:56:14 +0000113 case 0x0e: *D1c = (cache_t) { 24, 6, 64 }; break;
nethercoteb35a8b92004-09-11 16:45:27 +0000114 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:
njn6f74a7e2009-03-12 00:06:45 +0000124 VG_DMSG("warning: L3 cache detected but ignored");
nethercoteb35a8b92004-09-11 16:45:27 +0000125 break;
126
127 /* These are sectored, whatever that means */
128 case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break;
129 case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break;
130
131 /* If a P6 core, this means "no L2 cache".
132 If a P4 core, this means "no L3 cache".
133 We don't know what core it is, so don't issue a warning. To detect
134 a missing L2 cache, we use 'L2_found'. */
135 case 0x40:
136 break;
137
138 case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break;
139 case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break;
140 case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break;
141 case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break;
142 case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break;
weidendo144b76c2009-01-26 22:56:14 +0000143 case 0x48: *L2c = (cache_t) { 3072,12, 64 }; L2_found = True; break;
weidendo1c3e3c52006-11-23 13:04:30 +0000144 case 0x49:
145 if ((family == 15) && (model == 6))
146 /* On Xeon MP (family F, model 6), this is for L3 */
njn6f74a7e2009-03-12 00:06:45 +0000147 VG_DMSG("warning: L3 cache detected but ignored");
weidendo1c3e3c52006-11-23 13:04:30 +0000148 else
149 *L2c = (cache_t) { 4096, 16, 64 }; L2_found = True;
150 break;
weidendo144b76c2009-01-26 22:56:14 +0000151 case 0x4e: *L2c = (cache_t) { 6144, 24, 64 }; L2_found = True; break;
nethercoteb35a8b92004-09-11 16:45:27 +0000152
153 /* These are sectored, whatever that means */
nethercoteac7ecd72004-10-13 11:30:14 +0000154 case 0x60: *D1c = (cache_t) { 16, 8, 64 }; break; /* sectored */
nethercoteb35a8b92004-09-11 16:45:27 +0000155 case 0x66: *D1c = (cache_t) { 8, 4, 64 }; break; /* sectored */
156 case 0x67: *D1c = (cache_t) { 16, 4, 64 }; break; /* sectored */
157 case 0x68: *D1c = (cache_t) { 32, 4, 64 }; break; /* sectored */
158
159 /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based.
160 * conversion to byte size is a total guess; treat the 12K and 16K
161 * cases the same since the cache byte size must be a power of two for
162 * everything to work!. Also guessing 32 bytes for the line size...
163 */
164 case 0x70: /* 12K micro-ops, 8-way */
165 *I1c = (cache_t) { 16, 8, 32 };
166 micro_ops_warn(12, 16, 32);
167 break;
168 case 0x71: /* 16K micro-ops, 8-way */
169 *I1c = (cache_t) { 16, 8, 32 };
170 micro_ops_warn(16, 16, 32);
171 break;
172 case 0x72: /* 32K micro-ops, 8-way */
173 *I1c = (cache_t) { 32, 8, 32 };
174 micro_ops_warn(32, 32, 32);
175 break;
176
177 /* These are sectored, whatever that means */
178 case 0x79: *L2c = (cache_t) { 128, 8, 64 }; L2_found = True; break;
179 case 0x7a: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break;
180 case 0x7b: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
181 case 0x7c: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
njn8bc85822005-07-20 04:32:44 +0000182 case 0x7d: *L2c = (cache_t) { 2048, 8, 64 }; L2_found = True; break;
nethercoteb35a8b92004-09-11 16:45:27 +0000183 case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break;
184
tom1e76ff52009-01-02 11:07:18 +0000185 case 0x7f: *L2c = (cache_t) { 512, 2, 64 }; L2_found = True; break;
186 case 0x80: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
187
nethercoteb35a8b92004-09-11 16:45:27 +0000188 case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break;
189 case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break;
190 case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break;
191 case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break;
192 case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break;
193 case 0x86: *L2c = (cache_t) { 512, 4, 64 }; L2_found = True; break;
194 case 0x87: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
195
tom942d9ef2005-07-27 22:59:50 +0000196 /* Ignore prefetch information */
197 case 0xf0: case 0xf1:
njn6f74a7e2009-03-12 00:06:45 +0000198 break;
tom942d9ef2005-07-27 22:59:50 +0000199
nethercoteb35a8b92004-09-11 16:45:27 +0000200 default:
njn6f74a7e2009-03-12 00:06:45 +0000201 VG_DMSG("warning: Unknown Intel cache config value (0x%x), ignoring",
202 info[i]);
203 break;
nethercoteb35a8b92004-09-11 16:45:27 +0000204 }
205 }
206
207 if (!L2_found)
njn6f74a7e2009-03-12 00:06:45 +0000208 VG_DMSG("warning: L2 cache not installed, ignore L2 results.");
nethercoteb35a8b92004-09-11 16:45:27 +0000209
210 return 0;
211}
212
213/* AMD method is straightforward, just extract appropriate bits from the
214 * result registers.
215 *
216 * Bits, for D1 and I1:
217 * 31..24 data L1 cache size in KBs
218 * 23..16 data L1 cache associativity (FFh=full)
219 * 15.. 8 data L1 cache lines per tag
220 * 7.. 0 data L1 cache line size in bytes
221 *
222 * Bits, for L2:
223 * 31..16 unified L2 cache size in KBs
224 * 15..12 unified L2 cache associativity (0=off, FFh=full)
225 * 11.. 8 unified L2 cache lines per tag
226 * 7.. 0 unified L2 cache line size in bytes
227 *
228 * #3 The AMD K7 processor's L2 cache must be configured prior to relying
229 * upon this information. (Whatever that means -- njn)
230 *
231 * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model
232 * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB),
233 * so we detect that.
234 *
235 * Returns 0 on success, non-zero on failure.
236 */
237static
238Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c)
239{
240 UInt ext_level;
241 UInt dummy, model;
242 UInt I1i, D1i, L2i;
243
244 VG_(cpuid)(0x80000000, &ext_level, &dummy, &dummy, &dummy);
245
246 if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) {
njn6f74a7e2009-03-12 00:06:45 +0000247 VG_DMSG("warning: ext_level < 0x80000006 for AMD processor (0x%x)",
248 ext_level);
nethercoteb35a8b92004-09-11 16:45:27 +0000249 return -1;
250 }
251
252 VG_(cpuid)(0x80000005, &dummy, &dummy, &D1i, &I1i);
253 VG_(cpuid)(0x80000006, &dummy, &dummy, &L2i, &dummy);
254
255 VG_(cpuid)(0x1, &model, &dummy, &dummy, &dummy);
256
257 /* Check for Duron bug */
258 if (model == 0x630) {
njn6f74a7e2009-03-12 00:06:45 +0000259 VG_DMSG("warning: Buggy Duron stepping A0. Assuming L2 size=65536 bytes");
nethercoteb35a8b92004-09-11 16:45:27 +0000260 L2i = (64 << 16) | (L2i & 0xffff);
261 }
262
263 D1c->size = (D1i >> 24) & 0xff;
264 D1c->assoc = (D1i >> 16) & 0xff;
265 D1c->line_size = (D1i >> 0) & 0xff;
266
267 I1c->size = (I1i >> 24) & 0xff;
268 I1c->assoc = (I1i >> 16) & 0xff;
269 I1c->line_size = (I1i >> 0) & 0xff;
270
271 L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */
272 L2c->assoc = (L2i >> 12) & 0xf;
273 L2c->line_size = (L2i >> 0) & 0xff;
274
275 return 0;
276}
277
nethercoteb35a8b92004-09-11 16:45:27 +0000278static
279Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c)
280{
sewardjb5f6f512005-03-10 23:59:00 +0000281 Int level, ret;
nethercoteb35a8b92004-09-11 16:45:27 +0000282 Char vendor_id[13];
nethercoteb35a8b92004-09-11 16:45:27 +0000283
sewardjb5f6f512005-03-10 23:59:00 +0000284 if (!VG_(has_cpuid)()) {
njn6f74a7e2009-03-12 00:06:45 +0000285 VG_DMSG("CPUID instruction not supported");
nethercoteb35a8b92004-09-11 16:45:27 +0000286 return -1;
287 }
tomf4ed0592005-04-02 17:30:19 +0000288
sewardjb5f6f512005-03-10 23:59:00 +0000289 VG_(cpuid)(0, &level, (int*)&vendor_id[0],
290 (int*)&vendor_id[8], (int*)&vendor_id[4]);
291 vendor_id[12] = '\0';
nethercoteb35a8b92004-09-11 16:45:27 +0000292
293 if (0 == level) {
njn6f74a7e2009-03-12 00:06:45 +0000294 VG_DMSG("CPUID level is 0, early Pentium?");
nethercoteb35a8b92004-09-11 16:45:27 +0000295 return -1;
296 }
297
298 /* Only handling Intel and AMD chips... no Cyrix, Transmeta, etc */
299 if (0 == VG_(strcmp)(vendor_id, "GenuineIntel")) {
300 ret = Intel_cache_info(level, I1c, D1c, L2c);
301
302 } else if (0 == VG_(strcmp)(vendor_id, "AuthenticAMD")) {
303 ret = AMD_cache_info(I1c, D1c, L2c);
304
305 } else if (0 == VG_(strcmp)(vendor_id, "CentaurHauls")) {
306 /* Total kludge. Pretend to be a VIA Nehemiah. */
307 D1c->size = 64;
308 D1c->assoc = 16;
309 D1c->line_size = 16;
310 I1c->size = 64;
311 I1c->assoc = 4;
312 I1c->line_size = 16;
313 L2c->size = 64;
314 L2c->assoc = 16;
315 L2c->line_size = 16;
316 ret = 0;
317
318 } else {
njn6f74a7e2009-03-12 00:06:45 +0000319 VG_DMSG("CPU vendor ID not recognised (%s)", vendor_id);
nethercoteb35a8b92004-09-11 16:45:27 +0000320 return -1;
321 }
322
323 /* Successful! Convert sizes from KB to bytes */
324 I1c->size *= 1024;
325 D1c->size *= 1024;
326 L2c->size *= 1024;
327
328 return ret;
329}
330
331
njnaf839f52005-06-23 03:27:57 +0000332void VG_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
333 Bool all_caches_clo_defined)
nethercoteb35a8b92004-09-11 16:45:27 +0000334{
335 Int res;
336
337 // Set caches to default.
njna1d1a642004-11-26 18:36:02 +0000338 *I1c = (cache_t) { 65536, 2, 64 };
339 *D1c = (cache_t) { 65536, 2, 64 };
340 *L2c = (cache_t) { 262144, 8, 64 };
nethercoteb35a8b92004-09-11 16:45:27 +0000341
342 // Then replace with any info we can get from CPUID.
343 res = get_caches_from_CPUID(I1c, D1c, L2c);
344
345 // Warn if CPUID failed and config not completely specified from cmd line.
346 if (res != 0 && !all_caches_clo_defined) {
njn6f74a7e2009-03-12 00:06:45 +0000347 VG_DMSG("Warning: Couldn't auto-detect cache config, using one "
348 "or more defaults ");
nethercoteb35a8b92004-09-11 16:45:27 +0000349 }
350}
351
njn8b68b642009-06-24 00:37:09 +0000352#endif // defined(VGA_x86) || defined(VGA_amd64)
353
nethercoteb35a8b92004-09-11 16:45:27 +0000354/*--------------------------------------------------------------------*/
355/*--- end ---*/
356/*--------------------------------------------------------------------*/