blob: 6ce7b603049da13ffd7cf878532e86f50b19b110 [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
njn53612422005-03-12 16:22:54 +000010 Copyright (C) 2002-2005 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
31#include "tool.h"
njn97405b22005-06-02 03:39:33 +000032#include "pub_tool_libcbase.h"
njn36a20fa2005-06-03 03:08:39 +000033#include "pub_tool_libcprint.h"
nethercoteb35a8b92004-09-11 16:45:27 +000034#include "cg_arch.h"
35
36// All CPUID info taken from sandpile.org/a32/cpuid.htm */
37// Probably only works for Intel and AMD chips, and probably only for some of
38// them.
39
40static void micro_ops_warn(Int actual_size, Int used_size, Int line_size)
41{
42 VG_(message)(Vg_DebugMsg,
43 "warning: Pentium with %d K micro-op instruction trace cache",
44 actual_size);
45 VG_(message)(Vg_DebugMsg,
46 " Simulating a %d KB cache with %d B lines",
47 used_size, line_size);
48}
49
50/* Intel method is truly wretched. We have to do an insane indexing into an
51 * array of pre-defined configurations for various parts of the memory
52 * hierarchy.
53 */
54static
55Int Intel_cache_info(Int level, cache_t* I1c, cache_t* D1c, cache_t* L2c)
56{
57 UChar info[16];
58 Int i, trials;
59 Bool L2_found = False;
60
61 if (level < 2) {
62 VG_(message)(Vg_DebugMsg,
63 "warning: CPUID level < 2 for Intel processor (%d)",
64 level);
65 return -1;
66 }
67
68 VG_(cpuid)(2, (Int*)&info[0], (Int*)&info[4],
69 (Int*)&info[8], (Int*)&info[12]);
70 trials = info[0] - 1; /* AL register - bits 0..7 of %eax */
71 info[0] = 0x0; /* reset AL */
72
73 if (0 != trials) {
74 VG_(message)(Vg_DebugMsg,
75 "warning: non-zero CPUID trials for Intel processor (%d)",
76 trials);
77 return -1;
78 }
79
80 for (i = 0; i < 16; i++) {
81
82 switch (info[i]) {
83
84 case 0x0: /* ignore zeros */
85 break;
86
87 /* TLB info, ignore */
88 case 0x01: case 0x02: case 0x03: case 0x04:
89 case 0x50: case 0x51: case 0x52: case 0x5b: case 0x5c: case 0x5d:
90 case 0xb0: case 0xb3:
91 break;
92
93 case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break;
94 case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break;
95 case 0x30: *I1c = (cache_t) { 32, 8, 64 }; break;
96
97 case 0x0a: *D1c = (cache_t) { 8, 2, 32 }; break;
98 case 0x0c: *D1c = (cache_t) { 16, 4, 32 }; break;
99 case 0x2c: *D1c = (cache_t) { 32, 8, 64 }; break;
100
101 /* IA-64 info -- panic! */
102 case 0x10: case 0x15: case 0x1a:
103 case 0x88: case 0x89: case 0x8a: case 0x8d:
104 case 0x90: case 0x96: case 0x9b:
njn67993252004-11-22 18:02:32 +0000105 VG_(tool_panic)("IA-64 cache detected?!");
nethercoteb35a8b92004-09-11 16:45:27 +0000106
107 case 0x22: case 0x23: case 0x25: case 0x29:
108 VG_(message)(Vg_DebugMsg,
109 "warning: L3 cache detected but ignored\n");
110 break;
111
112 /* These are sectored, whatever that means */
113 case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break;
114 case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break;
115
116 /* If a P6 core, this means "no L2 cache".
117 If a P4 core, this means "no L3 cache".
118 We don't know what core it is, so don't issue a warning. To detect
119 a missing L2 cache, we use 'L2_found'. */
120 case 0x40:
121 break;
122
123 case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break;
124 case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break;
125 case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break;
126 case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break;
127 case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break;
128
129 /* These are sectored, whatever that means */
nethercoteac7ecd72004-10-13 11:30:14 +0000130 case 0x60: *D1c = (cache_t) { 16, 8, 64 }; break; /* sectored */
nethercoteb35a8b92004-09-11 16:45:27 +0000131 case 0x66: *D1c = (cache_t) { 8, 4, 64 }; break; /* sectored */
132 case 0x67: *D1c = (cache_t) { 16, 4, 64 }; break; /* sectored */
133 case 0x68: *D1c = (cache_t) { 32, 4, 64 }; break; /* sectored */
134
135 /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based.
136 * conversion to byte size is a total guess; treat the 12K and 16K
137 * cases the same since the cache byte size must be a power of two for
138 * everything to work!. Also guessing 32 bytes for the line size...
139 */
140 case 0x70: /* 12K micro-ops, 8-way */
141 *I1c = (cache_t) { 16, 8, 32 };
142 micro_ops_warn(12, 16, 32);
143 break;
144 case 0x71: /* 16K micro-ops, 8-way */
145 *I1c = (cache_t) { 16, 8, 32 };
146 micro_ops_warn(16, 16, 32);
147 break;
148 case 0x72: /* 32K micro-ops, 8-way */
149 *I1c = (cache_t) { 32, 8, 32 };
150 micro_ops_warn(32, 32, 32);
151 break;
152
153 /* These are sectored, whatever that means */
154 case 0x79: *L2c = (cache_t) { 128, 8, 64 }; L2_found = True; break;
155 case 0x7a: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break;
156 case 0x7b: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
157 case 0x7c: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
158 case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break;
159
160 case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break;
161 case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break;
162 case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break;
163 case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break;
164 case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break;
165 case 0x86: *L2c = (cache_t) { 512, 4, 64 }; L2_found = True; break;
166 case 0x87: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
167
168 default:
169 VG_(message)(Vg_DebugMsg,
170 "warning: Unknown Intel cache config value "
171 "(0x%x), ignoring", info[i]);
172 break;
173 }
174 }
175
176 if (!L2_found)
177 VG_(message)(Vg_DebugMsg,
178 "warning: L2 cache not installed, ignore L2 results.");
179
180 return 0;
181}
182
183/* AMD method is straightforward, just extract appropriate bits from the
184 * result registers.
185 *
186 * Bits, for D1 and I1:
187 * 31..24 data L1 cache size in KBs
188 * 23..16 data L1 cache associativity (FFh=full)
189 * 15.. 8 data L1 cache lines per tag
190 * 7.. 0 data L1 cache line size in bytes
191 *
192 * Bits, for L2:
193 * 31..16 unified L2 cache size in KBs
194 * 15..12 unified L2 cache associativity (0=off, FFh=full)
195 * 11.. 8 unified L2 cache lines per tag
196 * 7.. 0 unified L2 cache line size in bytes
197 *
198 * #3 The AMD K7 processor's L2 cache must be configured prior to relying
199 * upon this information. (Whatever that means -- njn)
200 *
201 * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model
202 * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB),
203 * so we detect that.
204 *
205 * Returns 0 on success, non-zero on failure.
206 */
207static
208Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c)
209{
210 UInt ext_level;
211 UInt dummy, model;
212 UInt I1i, D1i, L2i;
213
214 VG_(cpuid)(0x80000000, &ext_level, &dummy, &dummy, &dummy);
215
216 if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) {
217 VG_(message)(Vg_UserMsg,
218 "warning: ext_level < 0x80000006 for AMD processor (0x%x)",
219 ext_level);
220 return -1;
221 }
222
223 VG_(cpuid)(0x80000005, &dummy, &dummy, &D1i, &I1i);
224 VG_(cpuid)(0x80000006, &dummy, &dummy, &L2i, &dummy);
225
226 VG_(cpuid)(0x1, &model, &dummy, &dummy, &dummy);
227
228 /* Check for Duron bug */
229 if (model == 0x630) {
230 VG_(message)(Vg_UserMsg,
231 "Buggy Duron stepping A0. Assuming L2 size=65536 bytes");
232 L2i = (64 << 16) | (L2i & 0xffff);
233 }
234
235 D1c->size = (D1i >> 24) & 0xff;
236 D1c->assoc = (D1i >> 16) & 0xff;
237 D1c->line_size = (D1i >> 0) & 0xff;
238
239 I1c->size = (I1i >> 24) & 0xff;
240 I1c->assoc = (I1i >> 16) & 0xff;
241 I1c->line_size = (I1i >> 0) & 0xff;
242
243 L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */
244 L2c->assoc = (L2i >> 12) & 0xf;
245 L2c->line_size = (L2i >> 0) & 0xff;
246
247 return 0;
248}
249
nethercoteb35a8b92004-09-11 16:45:27 +0000250static
251Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c)
252{
sewardjb5f6f512005-03-10 23:59:00 +0000253 Int level, ret;
nethercoteb35a8b92004-09-11 16:45:27 +0000254 Char vendor_id[13];
nethercoteb35a8b92004-09-11 16:45:27 +0000255
sewardjb5f6f512005-03-10 23:59:00 +0000256 if (!VG_(has_cpuid)()) {
nethercoteb35a8b92004-09-11 16:45:27 +0000257 VG_(message)(Vg_DebugMsg, "CPUID instruction not supported");
nethercoteb35a8b92004-09-11 16:45:27 +0000258 return -1;
259 }
tomf4ed0592005-04-02 17:30:19 +0000260
sewardjb5f6f512005-03-10 23:59:00 +0000261 VG_(cpuid)(0, &level, (int*)&vendor_id[0],
262 (int*)&vendor_id[8], (int*)&vendor_id[4]);
263 vendor_id[12] = '\0';
nethercoteb35a8b92004-09-11 16:45:27 +0000264
265 if (0 == level) {
266 VG_(message)(Vg_DebugMsg, "CPUID level is 0, early Pentium?\n");
267 return -1;
268 }
269
270 /* Only handling Intel and AMD chips... no Cyrix, Transmeta, etc */
271 if (0 == VG_(strcmp)(vendor_id, "GenuineIntel")) {
272 ret = Intel_cache_info(level, I1c, D1c, L2c);
273
274 } else if (0 == VG_(strcmp)(vendor_id, "AuthenticAMD")) {
275 ret = AMD_cache_info(I1c, D1c, L2c);
276
277 } else if (0 == VG_(strcmp)(vendor_id, "CentaurHauls")) {
278 /* Total kludge. Pretend to be a VIA Nehemiah. */
279 D1c->size = 64;
280 D1c->assoc = 16;
281 D1c->line_size = 16;
282 I1c->size = 64;
283 I1c->assoc = 4;
284 I1c->line_size = 16;
285 L2c->size = 64;
286 L2c->assoc = 16;
287 L2c->line_size = 16;
288 ret = 0;
289
290 } else {
291 VG_(message)(Vg_DebugMsg, "CPU vendor ID not recognised (%s)",
292 vendor_id);
293 return -1;
294 }
295
296 /* Successful! Convert sizes from KB to bytes */
297 I1c->size *= 1024;
298 D1c->size *= 1024;
299 L2c->size *= 1024;
300
301 return ret;
302}
303
304
305void VGA_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
njna1d1a642004-11-26 18:36:02 +0000306 Bool all_caches_clo_defined)
nethercoteb35a8b92004-09-11 16:45:27 +0000307{
308 Int res;
309
310 // Set caches to default.
njna1d1a642004-11-26 18:36:02 +0000311 *I1c = (cache_t) { 65536, 2, 64 };
312 *D1c = (cache_t) { 65536, 2, 64 };
313 *L2c = (cache_t) { 262144, 8, 64 };
nethercoteb35a8b92004-09-11 16:45:27 +0000314
315 // Then replace with any info we can get from CPUID.
316 res = get_caches_from_CPUID(I1c, D1c, L2c);
317
318 // Warn if CPUID failed and config not completely specified from cmd line.
319 if (res != 0 && !all_caches_clo_defined) {
320 VG_(message)(Vg_DebugMsg,
321 "Warning: Couldn't auto-detect cache config, using one "
322 "or more defaults ");
323 }
324}
325
326/*--------------------------------------------------------------------*/
327/*--- end ---*/
328/*--------------------------------------------------------------------*/