blob: a070c978cbac1a5e287b04c81af9038cf16f6d8d [file] [log] [blame]
nethercoteb35a8b92004-09-11 16:45:27 +00001
2/*--------------------------------------------------------------------*/
3/*--- Arch-specific definitions. x86/cg_arch.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Cachegrind, a Valgrind tool for cache
8 profiling programs.
9
10 Copyright (C) 2002-2004 Nicholas Nethercote
11 njn25@cam.ac.uk
12
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"
32#include "cg_arch.h"
33
34// All CPUID info taken from sandpile.org/a32/cpuid.htm */
35// Probably only works for Intel and AMD chips, and probably only for some of
36// them.
37
38static void micro_ops_warn(Int actual_size, Int used_size, Int line_size)
39{
40 VG_(message)(Vg_DebugMsg,
41 "warning: Pentium with %d K micro-op instruction trace cache",
42 actual_size);
43 VG_(message)(Vg_DebugMsg,
44 " Simulating a %d KB cache with %d B lines",
45 used_size, line_size);
46}
47
48/* Intel method is truly wretched. We have to do an insane indexing into an
49 * array of pre-defined configurations for various parts of the memory
50 * hierarchy.
51 */
52static
53Int Intel_cache_info(Int level, cache_t* I1c, cache_t* D1c, cache_t* L2c)
54{
55 UChar info[16];
56 Int i, trials;
57 Bool L2_found = False;
58
59 if (level < 2) {
60 VG_(message)(Vg_DebugMsg,
61 "warning: CPUID level < 2 for Intel processor (%d)",
62 level);
63 return -1;
64 }
65
66 VG_(cpuid)(2, (Int*)&info[0], (Int*)&info[4],
67 (Int*)&info[8], (Int*)&info[12]);
68 trials = info[0] - 1; /* AL register - bits 0..7 of %eax */
69 info[0] = 0x0; /* reset AL */
70
71 if (0 != trials) {
72 VG_(message)(Vg_DebugMsg,
73 "warning: non-zero CPUID trials for Intel processor (%d)",
74 trials);
75 return -1;
76 }
77
78 for (i = 0; i < 16; i++) {
79
80 switch (info[i]) {
81
82 case 0x0: /* ignore zeros */
83 break;
84
85 /* TLB info, ignore */
86 case 0x01: case 0x02: case 0x03: case 0x04:
87 case 0x50: case 0x51: case 0x52: case 0x5b: case 0x5c: case 0x5d:
88 case 0xb0: case 0xb3:
89 break;
90
91 case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break;
92 case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break;
93 case 0x30: *I1c = (cache_t) { 32, 8, 64 }; break;
94
95 case 0x0a: *D1c = (cache_t) { 8, 2, 32 }; break;
96 case 0x0c: *D1c = (cache_t) { 16, 4, 32 }; break;
97 case 0x2c: *D1c = (cache_t) { 32, 8, 64 }; break;
98
99 /* IA-64 info -- panic! */
100 case 0x10: case 0x15: case 0x1a:
101 case 0x88: case 0x89: case 0x8a: case 0x8d:
102 case 0x90: case 0x96: case 0x9b:
103 VG_(skin_panic)("IA-64 cache detected?!");
104
105 case 0x22: case 0x23: case 0x25: case 0x29:
106 VG_(message)(Vg_DebugMsg,
107 "warning: L3 cache detected but ignored\n");
108 break;
109
110 /* These are sectored, whatever that means */
111 case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break;
112 case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break;
113
114 /* If a P6 core, this means "no L2 cache".
115 If a P4 core, this means "no L3 cache".
116 We don't know what core it is, so don't issue a warning. To detect
117 a missing L2 cache, we use 'L2_found'. */
118 case 0x40:
119 break;
120
121 case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break;
122 case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break;
123 case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break;
124 case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break;
125 case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break;
126
127 /* These are sectored, whatever that means */
nethercoteac7ecd72004-10-13 11:30:14 +0000128 case 0x60: *D1c = (cache_t) { 16, 8, 64 }; break; /* sectored */
nethercoteb35a8b92004-09-11 16:45:27 +0000129 case 0x66: *D1c = (cache_t) { 8, 4, 64 }; break; /* sectored */
130 case 0x67: *D1c = (cache_t) { 16, 4, 64 }; break; /* sectored */
131 case 0x68: *D1c = (cache_t) { 32, 4, 64 }; break; /* sectored */
132
133 /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based.
134 * conversion to byte size is a total guess; treat the 12K and 16K
135 * cases the same since the cache byte size must be a power of two for
136 * everything to work!. Also guessing 32 bytes for the line size...
137 */
138 case 0x70: /* 12K micro-ops, 8-way */
139 *I1c = (cache_t) { 16, 8, 32 };
140 micro_ops_warn(12, 16, 32);
141 break;
142 case 0x71: /* 16K micro-ops, 8-way */
143 *I1c = (cache_t) { 16, 8, 32 };
144 micro_ops_warn(16, 16, 32);
145 break;
146 case 0x72: /* 32K micro-ops, 8-way */
147 *I1c = (cache_t) { 32, 8, 32 };
148 micro_ops_warn(32, 32, 32);
149 break;
150
151 /* These are sectored, whatever that means */
152 case 0x79: *L2c = (cache_t) { 128, 8, 64 }; L2_found = True; break;
153 case 0x7a: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break;
154 case 0x7b: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
155 case 0x7c: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
156 case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break;
157
158 case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break;
159 case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break;
160 case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break;
161 case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break;
162 case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break;
163 case 0x86: *L2c = (cache_t) { 512, 4, 64 }; L2_found = True; break;
164 case 0x87: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
165
166 default:
167 VG_(message)(Vg_DebugMsg,
168 "warning: Unknown Intel cache config value "
169 "(0x%x), ignoring", info[i]);
170 break;
171 }
172 }
173
174 if (!L2_found)
175 VG_(message)(Vg_DebugMsg,
176 "warning: L2 cache not installed, ignore L2 results.");
177
178 return 0;
179}
180
181/* AMD method is straightforward, just extract appropriate bits from the
182 * result registers.
183 *
184 * Bits, for D1 and I1:
185 * 31..24 data L1 cache size in KBs
186 * 23..16 data L1 cache associativity (FFh=full)
187 * 15.. 8 data L1 cache lines per tag
188 * 7.. 0 data L1 cache line size in bytes
189 *
190 * Bits, for L2:
191 * 31..16 unified L2 cache size in KBs
192 * 15..12 unified L2 cache associativity (0=off, FFh=full)
193 * 11.. 8 unified L2 cache lines per tag
194 * 7.. 0 unified L2 cache line size in bytes
195 *
196 * #3 The AMD K7 processor's L2 cache must be configured prior to relying
197 * upon this information. (Whatever that means -- njn)
198 *
199 * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model
200 * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB),
201 * so we detect that.
202 *
203 * Returns 0 on success, non-zero on failure.
204 */
205static
206Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c)
207{
208 UInt ext_level;
209 UInt dummy, model;
210 UInt I1i, D1i, L2i;
211
212 VG_(cpuid)(0x80000000, &ext_level, &dummy, &dummy, &dummy);
213
214 if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) {
215 VG_(message)(Vg_UserMsg,
216 "warning: ext_level < 0x80000006 for AMD processor (0x%x)",
217 ext_level);
218 return -1;
219 }
220
221 VG_(cpuid)(0x80000005, &dummy, &dummy, &D1i, &I1i);
222 VG_(cpuid)(0x80000006, &dummy, &dummy, &L2i, &dummy);
223
224 VG_(cpuid)(0x1, &model, &dummy, &dummy, &dummy);
225
226 /* Check for Duron bug */
227 if (model == 0x630) {
228 VG_(message)(Vg_UserMsg,
229 "Buggy Duron stepping A0. Assuming L2 size=65536 bytes");
230 L2i = (64 << 16) | (L2i & 0xffff);
231 }
232
233 D1c->size = (D1i >> 24) & 0xff;
234 D1c->assoc = (D1i >> 16) & 0xff;
235 D1c->line_size = (D1i >> 0) & 0xff;
236
237 I1c->size = (I1i >> 24) & 0xff;
238 I1c->assoc = (I1i >> 16) & 0xff;
239 I1c->line_size = (I1i >> 0) & 0xff;
240
241 L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */
242 L2c->assoc = (L2i >> 12) & 0xf;
243 L2c->line_size = (L2i >> 0) & 0xff;
244
245 return 0;
246}
247
248static jmp_buf cpuid_jmpbuf;
249
250static
251void cpuid_SIGILL_handler(int signum)
252{
253 __builtin_longjmp(cpuid_jmpbuf, 1);
254}
255
256static
257Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c)
258{
259 Int level, res, ret;
260 Char vendor_id[13];
nethercote73b526f2004-10-31 18:48:21 +0000261 struct vki_sigaction sigill_new, sigill_saved;
nethercoteb35a8b92004-09-11 16:45:27 +0000262
263 /* Install own SIGILL handler */
264 sigill_new.ksa_handler = cpuid_SIGILL_handler;
nethercote73b526f2004-10-31 18:48:21 +0000265 sigill_new.sa_flags = 0;
266 sigill_new.sa_restorer = NULL;
267 res = VG_(sigemptyset)( &sigill_new.sa_mask );
nethercoteb35a8b92004-09-11 16:45:27 +0000268 sk_assert(res == 0);
269
nethercote73b526f2004-10-31 18:48:21 +0000270 res = VG_(sigaction)( VKI_SIGILL, &sigill_new, &sigill_saved );
nethercoteb35a8b92004-09-11 16:45:27 +0000271 sk_assert(res == 0);
272
273 /* Trap for illegal instruction, in case it's a really old processor that
274 * doesn't support CPUID. */
275 if (__builtin_setjmp(cpuid_jmpbuf) == 0) {
276 VG_(cpuid)(0, &level, (int*)&vendor_id[0],
277 (int*)&vendor_id[8], (int*)&vendor_id[4]);
278 vendor_id[12] = '\0';
279
280 /* Restore old SIGILL handler */
nethercote73b526f2004-10-31 18:48:21 +0000281 res = VG_(sigaction)( VKI_SIGILL, &sigill_saved, NULL );
nethercoteb35a8b92004-09-11 16:45:27 +0000282 sk_assert(res == 0);
283
284 } else {
285 VG_(message)(Vg_DebugMsg, "CPUID instruction not supported");
286
287 /* Restore old SIGILL handler */
nethercote73b526f2004-10-31 18:48:21 +0000288 res = VG_(sigaction)( VKI_SIGILL, &sigill_saved, NULL );
nethercoteb35a8b92004-09-11 16:45:27 +0000289 sk_assert(res == 0);
290 return -1;
291 }
292
293 if (0 == level) {
294 VG_(message)(Vg_DebugMsg, "CPUID level is 0, early Pentium?\n");
295 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 {
319 VG_(message)(Vg_DebugMsg, "CPU vendor ID not recognised (%s)",
320 vendor_id);
321 return -1;
322 }
323
324 /* Successful! Convert sizes from KB to bytes */
325 I1c->size *= 1024;
326 D1c->size *= 1024;
327 L2c->size *= 1024;
328
329 return ret;
330}
331
332
333void VGA_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
334 cache_t* I1_dflt, cache_t* D1_dflt, cache_t* L2_dflt,
335 Bool all_caches_clo_defined)
336{
337 Int res;
338
339 // Set caches to default.
340 *I1_dflt = (cache_t) { 65536, 2, 64 };
341 *D1_dflt = (cache_t) { 65536, 2, 64 };
342 *L2_dflt = (cache_t) { 262144, 8, 64 };
343 *I1c = *I1_dflt;
344 *D1c = *D1_dflt;
345 *L2c = *L2_dflt;
346
347 // Then replace with any info we can get from CPUID.
348 res = get_caches_from_CPUID(I1c, D1c, L2c);
349
350 // Warn if CPUID failed and config not completely specified from cmd line.
351 if (res != 0 && !all_caches_clo_defined) {
352 VG_(message)(Vg_DebugMsg,
353 "Warning: Couldn't auto-detect cache config, using one "
354 "or more defaults ");
355 }
356}
357
358/*--------------------------------------------------------------------*/
359/*--- end ---*/
360/*--------------------------------------------------------------------*/