blob: f714bbafc4890b77560763274b55496259355726 [file] [log] [blame]
njnc6168192004-11-29 13:54:10 +00001
2/*--------------------------------------------------------------------*/
njn528b07e2005-06-10 04:46:19 +00003/*--- AMD64-specific definitions. cg-amd64.c ---*/
njnc6168192004-11-29 13:54:10 +00004/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Cachegrind, a Valgrind tool for cache
8 profiling programs.
9
sewardje4b0bf02006-06-05 23:21:15 +000010 Copyright (C) 2002-2006 Nicholas Nethercote
njn2bc10122005-05-08 02:10:27 +000011 njn@valgrind.org
njnc6168192004-11-29 13:54:10 +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"
sewardjc93cf062005-06-30 23:20:30 +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"
njnc6168192004-11-29 13:54:10 +000036
njnc7561b92005-06-19 01:24:32 +000037#include "cg_arch.h"
38
njnc6168192004-11-29 13:54:10 +000039// 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
njnc6168192004-11-29 13:54:10 +000043static void micro_ops_warn(Int actual_size, Int used_size, Int line_size)
44{
45 VG_(message)(Vg_DebugMsg,
sewardjfcf126f2006-08-01 17:26:38 +000046 "warning: Pentium 4 with %d KB micro-op instruction trace cache",
njnc6168192004-11-29 13:54:10 +000047 actual_size);
48 VG_(message)(Vg_DebugMsg,
sewardjfcf126f2006-08-01 17:26:38 +000049 " Simulating a %d KB I-cache with %d B lines",
njnc6168192004-11-29 13:54:10 +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
55 * hierarchy.
56 */
57static
58Int Intel_cache_info(Int level, cache_t* I1c, cache_t* D1c, cache_t* L2c)
59{
60 UChar info[16];
61 Int i, trials;
62 Bool L2_found = False;
63
64 if (level < 2) {
65 VG_(message)(Vg_DebugMsg,
66 "warning: CPUID level < 2 for Intel processor (%d)",
67 level);
68 return -1;
69 }
70
71 VG_(cpuid)(2, (Int*)&info[0], (Int*)&info[4],
72 (Int*)&info[8], (Int*)&info[12]);
73 trials = info[0] - 1; /* AL register - bits 0..7 of %eax */
74 info[0] = 0x0; /* reset AL */
75
76 if (0 != trials) {
77 VG_(message)(Vg_DebugMsg,
78 "warning: non-zero CPUID trials for Intel processor (%d)",
79 trials);
80 return -1;
81 }
82
83 for (i = 0; i < 16; i++) {
84
85 switch (info[i]) {
86
87 case 0x0: /* ignore zeros */
88 break;
89
90 /* TLB info, ignore */
weidendo966b5bd2006-10-12 14:23:38 +000091 case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
92 case 0x50: case 0x51: case 0x52: case 0x56: case 0x57:
93 case 0x5b: case 0x5c: case 0x5d:
94 case 0xb0: case 0xb1: case 0xb3: case 0xb4:
njnc6168192004-11-29 13:54:10 +000095 break;
96
97 case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break;
98 case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break;
99 case 0x30: *I1c = (cache_t) { 32, 8, 64 }; break;
100
101 case 0x0a: *D1c = (cache_t) { 8, 2, 32 }; break;
102 case 0x0c: *D1c = (cache_t) { 16, 4, 32 }; break;
103 case 0x2c: *D1c = (cache_t) { 32, 8, 64 }; break;
104
105 /* IA-64 info -- panic! */
106 case 0x10: case 0x15: case 0x1a:
107 case 0x88: case 0x89: case 0x8a: case 0x8d:
108 case 0x90: case 0x96: case 0x9b:
109 VG_(tool_panic)("IA-64 cache detected?!");
110
weidendo966b5bd2006-10-12 14:23:38 +0000111 case 0x22: case 0x23: case 0x25: case 0x29: case 0x46: case 0x47:
njnc6168192004-11-29 13:54:10 +0000112 VG_(message)(Vg_DebugMsg,
113 "warning: L3 cache detected but ignored\n");
114 break;
115
116 /* These are sectored, whatever that means */
117 case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break;
118 case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break;
119
120 /* If a P6 core, this means "no L2 cache".
121 If a P4 core, this means "no L3 cache".
122 We don't know what core it is, so don't issue a warning. To detect
123 a missing L2 cache, we use 'L2_found'. */
124 case 0x40:
125 break;
126
127 case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break;
128 case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break;
129 case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break;
130 case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break;
131 case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break;
weidendo966b5bd2006-10-12 14:23:38 +0000132 case 0x49: *L2c = (cache_t) { 4096,16, 64 }; L2_found = True; break;
njnc6168192004-11-29 13:54:10 +0000133
134 /* These are sectored, whatever that means */
135 case 0x60: *D1c = (cache_t) { 16, 8, 64 }; break; /* sectored */
136 case 0x66: *D1c = (cache_t) { 8, 4, 64 }; break; /* sectored */
137 case 0x67: *D1c = (cache_t) { 16, 4, 64 }; break; /* sectored */
138 case 0x68: *D1c = (cache_t) { 32, 4, 64 }; break; /* sectored */
139
140 /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based.
141 * conversion to byte size is a total guess; treat the 12K and 16K
142 * cases the same since the cache byte size must be a power of two for
143 * everything to work!. Also guessing 32 bytes for the line size...
144 */
145 case 0x70: /* 12K micro-ops, 8-way */
146 *I1c = (cache_t) { 16, 8, 32 };
147 micro_ops_warn(12, 16, 32);
148 break;
149 case 0x71: /* 16K micro-ops, 8-way */
150 *I1c = (cache_t) { 16, 8, 32 };
151 micro_ops_warn(16, 16, 32);
152 break;
153 case 0x72: /* 32K micro-ops, 8-way */
154 *I1c = (cache_t) { 32, 8, 32 };
155 micro_ops_warn(32, 32, 32);
156 break;
157
158 /* These are sectored, whatever that means */
159 case 0x79: *L2c = (cache_t) { 128, 8, 64 }; L2_found = True; break;
160 case 0x7a: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break;
161 case 0x7b: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
162 case 0x7c: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
njn8bc85822005-07-20 04:32:44 +0000163 case 0x7d: *L2c = (cache_t) { 2048, 8, 64 }; L2_found = True; break;
njnc6168192004-11-29 13:54:10 +0000164 case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break;
165
166 case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break;
167 case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break;
168 case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break;
169 case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break;
170 case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break;
171 case 0x86: *L2c = (cache_t) { 512, 4, 64 }; L2_found = True; break;
172 case 0x87: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
173
weidendo966b5bd2006-10-12 14:23:38 +0000174 /* Ignore prefetch information */
175 case 0xf0: case 0xf1:
176 break;
177
njnc6168192004-11-29 13:54:10 +0000178 default:
179 VG_(message)(Vg_DebugMsg,
180 "warning: Unknown Intel cache config value "
181 "(0x%x), ignoring", info[i]);
182 break;
183 }
184 }
185
186 if (!L2_found)
187 VG_(message)(Vg_DebugMsg,
188 "warning: L2 cache not installed, ignore L2 results.");
189
190 return 0;
191}
192
193/* AMD method is straightforward, just extract appropriate bits from the
194 * result registers.
195 *
196 * Bits, for D1 and I1:
197 * 31..24 data L1 cache size in KBs
198 * 23..16 data L1 cache associativity (FFh=full)
199 * 15.. 8 data L1 cache lines per tag
200 * 7.. 0 data L1 cache line size in bytes
201 *
202 * Bits, for L2:
203 * 31..16 unified L2 cache size in KBs
204 * 15..12 unified L2 cache associativity (0=off, FFh=full)
205 * 11.. 8 unified L2 cache lines per tag
206 * 7.. 0 unified L2 cache line size in bytes
207 *
208 * #3 The AMD K7 processor's L2 cache must be configured prior to relying
209 * upon this information. (Whatever that means -- njn)
210 *
211 * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model
212 * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB),
213 * so we detect that.
214 *
215 * Returns 0 on success, non-zero on failure.
216 */
217static
218Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c)
219{
220 UInt ext_level;
221 UInt dummy, model;
222 UInt I1i, D1i, L2i;
223
224 VG_(cpuid)(0x80000000, &ext_level, &dummy, &dummy, &dummy);
225
226 if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) {
227 VG_(message)(Vg_UserMsg,
228 "warning: ext_level < 0x80000006 for AMD processor (0x%x)",
229 ext_level);
230 return -1;
231 }
232
233 VG_(cpuid)(0x80000005, &dummy, &dummy, &D1i, &I1i);
234 VG_(cpuid)(0x80000006, &dummy, &dummy, &L2i, &dummy);
235
236 VG_(cpuid)(0x1, &model, &dummy, &dummy, &dummy);
237
238 /* Check for Duron bug */
239 if (model == 0x630) {
240 VG_(message)(Vg_UserMsg,
241 "Buggy Duron stepping A0. Assuming L2 size=65536 bytes");
242 L2i = (64 << 16) | (L2i & 0xffff);
243 }
244
245 D1c->size = (D1i >> 24) & 0xff;
246 D1c->assoc = (D1i >> 16) & 0xff;
247 D1c->line_size = (D1i >> 0) & 0xff;
248
249 I1c->size = (I1i >> 24) & 0xff;
250 I1c->assoc = (I1i >> 16) & 0xff;
251 I1c->line_size = (I1i >> 0) & 0xff;
252
253 L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */
254 L2c->assoc = (L2i >> 12) & 0xf;
255 L2c->line_size = (L2i >> 0) & 0xff;
256
257 return 0;
258}
259
njnc6168192004-11-29 13:54:10 +0000260static
261Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c)
262{
tom094031a2005-04-02 17:26:07 +0000263 Int level, ret;
njnc6168192004-11-29 13:54:10 +0000264 Char vendor_id[13];
tomf4ed0592005-04-02 17:30:19 +0000265
266 if (!VG_(has_cpuid)()) {
njnc6168192004-11-29 13:54:10 +0000267 VG_(message)(Vg_DebugMsg, "CPUID instruction not supported");
njnc6168192004-11-29 13:54:10 +0000268 return -1;
269 }
270
tomf4ed0592005-04-02 17:30:19 +0000271 VG_(cpuid)(0, &level, (int*)&vendor_id[0],
272 (int*)&vendor_id[8], (int*)&vendor_id[4]);
273 vendor_id[12] = '\0';
274
njnc6168192004-11-29 13:54:10 +0000275 if (0 == level) {
276 VG_(message)(Vg_DebugMsg, "CPUID level is 0, early Pentium?\n");
277 return -1;
278 }
279
280 /* Only handling Intel and AMD chips... no Cyrix, Transmeta, etc */
281 if (0 == VG_(strcmp)(vendor_id, "GenuineIntel")) {
282 ret = Intel_cache_info(level, I1c, D1c, L2c);
283
284 } else if (0 == VG_(strcmp)(vendor_id, "AuthenticAMD")) {
285 ret = AMD_cache_info(I1c, D1c, L2c);
286
njnc6168192004-11-29 13:54:10 +0000287 } else {
288 VG_(message)(Vg_DebugMsg, "CPU vendor ID not recognised (%s)",
289 vendor_id);
290 return -1;
291 }
292
293 /* Successful! Convert sizes from KB to bytes */
294 I1c->size *= 1024;
295 D1c->size *= 1024;
296 L2c->size *= 1024;
297
298 return ret;
299}
njnc6168192004-11-29 13:54:10 +0000300
301
njnaf839f52005-06-23 03:27:57 +0000302void VG_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
303 Bool all_caches_clo_defined)
njnc6168192004-11-29 13:54:10 +0000304{
njnc6168192004-11-29 13:54:10 +0000305 Int res;
306
307 // Set caches to default.
308 *I1c = (cache_t) { 65536, 2, 64 };
309 *D1c = (cache_t) { 65536, 2, 64 };
310 *L2c = (cache_t) { 262144, 8, 64 };
311
312 // Then replace with any info we can get from CPUID.
313 res = get_caches_from_CPUID(I1c, D1c, L2c);
314
315 // Warn if CPUID failed and config not completely specified from cmd line.
316 if (res != 0 && !all_caches_clo_defined) {
317 VG_(message)(Vg_DebugMsg,
318 "Warning: Couldn't auto-detect cache config, using one "
319 "or more defaults ");
320 }
njnc6168192004-11-29 13:54:10 +0000321}
322
323/*--------------------------------------------------------------------*/
324/*--- end ---*/
325/*--------------------------------------------------------------------*/