PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/cachegrind/cg-amd64.c

https://github.com/eeight/tdheap
C | 343 lines | 203 code | 55 blank | 85 comment | 27 complexity | f47f168961dceda3f8c595ab9777c6bb MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0
  1. /*--------------------------------------------------------------------*/
  2. /*--- AMD64-specific definitions. cg-amd64.c ---*/
  3. /*--------------------------------------------------------------------*/
  4. /*
  5. This file is part of Cachegrind, a Valgrind tool for cache
  6. profiling programs.
  7. Copyright (C) 2002-2008 Nicholas Nethercote
  8. njn@valgrind.org
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation; either version 2 of the
  12. License, or (at your option) any later version.
  13. This program is distributed in the hope that it will be useful, but
  14. WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  20. 02111-1307, USA.
  21. The GNU General Public License is contained in the file COPYING.
  22. */
  23. #include "pub_tool_basics.h"
  24. #include "pub_tool_cpuid.h"
  25. #include "pub_tool_libcbase.h"
  26. #include "pub_tool_libcassert.h"
  27. #include "pub_tool_libcprint.h"
  28. #include "cg_arch.h"
  29. // All CPUID info taken from sandpile.org/a32/cpuid.htm */
  30. // Probably only works for Intel and AMD chips, and probably only for some of
  31. // them.
  32. static void micro_ops_warn(Int actual_size, Int used_size, Int line_size)
  33. {
  34. VG_(message)(Vg_DebugMsg,
  35. "warning: Pentium 4 with %d KB micro-op instruction trace cache",
  36. actual_size);
  37. VG_(message)(Vg_DebugMsg,
  38. " Simulating a %d KB I-cache with %d B lines",
  39. used_size, line_size);
  40. }
  41. /* Intel method is truly wretched. We have to do an insane indexing into an
  42. * array of pre-defined configurations for various parts of the memory
  43. * hierarchy.
  44. * According to Intel Processor Identification, App Note 485.
  45. */
  46. static
  47. Int Intel_cache_info(Int level, cache_t* I1c, cache_t* D1c, cache_t* L2c)
  48. {
  49. Int cpuid1_eax;
  50. Int cpuid1_ignore;
  51. Int family;
  52. Int model;
  53. UChar info[16];
  54. Int i, trials;
  55. Bool L2_found = False;
  56. if (level < 2) {
  57. VG_(message)(Vg_DebugMsg,
  58. "warning: CPUID level < 2 for Intel processor (%d)",
  59. level);
  60. return -1;
  61. }
  62. /* family/model needed to distinguish code reuse (currently 0x49) */
  63. VG_(cpuid)(1, &cpuid1_eax, &cpuid1_ignore,
  64. &cpuid1_ignore, &cpuid1_ignore);
  65. family = (((cpuid1_eax >> 20) & 0xff) << 4) + ((cpuid1_eax >> 8) & 0xf);
  66. model = (((cpuid1_eax >> 16) & 0xf) << 4) + ((cpuid1_eax >> 4) & 0xf);
  67. VG_(cpuid)(2, (Int*)&info[0], (Int*)&info[4],
  68. (Int*)&info[8], (Int*)&info[12]);
  69. trials = info[0] - 1; /* AL register - bits 0..7 of %eax */
  70. info[0] = 0x0; /* reset AL */
  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. for (i = 0; i < 16; i++) {
  78. switch (info[i]) {
  79. case 0x0: /* ignore zeros */
  80. break;
  81. /* TLB info, ignore */
  82. case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
  83. case 0x50: case 0x51: case 0x52: case 0x56: case 0x57:
  84. case 0x5b: case 0x5c: case 0x5d:
  85. case 0xb0: case 0xb1: case 0xb3: case 0xb4:
  86. break;
  87. case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break;
  88. case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break;
  89. case 0x30: *I1c = (cache_t) { 32, 8, 64 }; break;
  90. case 0x0a: *D1c = (cache_t) { 8, 2, 32 }; break;
  91. case 0x0c: *D1c = (cache_t) { 16, 4, 32 }; break;
  92. case 0x2c: *D1c = (cache_t) { 32, 8, 64 }; break;
  93. /* IA-64 info -- panic! */
  94. case 0x10: case 0x15: case 0x1a:
  95. case 0x88: case 0x89: case 0x8a: case 0x8d:
  96. case 0x90: case 0x96: case 0x9b:
  97. VG_(tool_panic)("IA-64 cache detected?!");
  98. case 0x22: case 0x23: case 0x25: case 0x29: case 0x46: case 0x47:
  99. VG_(message)(Vg_DebugMsg,
  100. "warning: L3 cache detected but ignored");
  101. break;
  102. /* These are sectored, whatever that means */
  103. case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break;
  104. case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break;
  105. /* If a P6 core, this means "no L2 cache".
  106. If a P4 core, this means "no L3 cache".
  107. We don't know what core it is, so don't issue a warning. To detect
  108. a missing L2 cache, we use 'L2_found'. */
  109. case 0x40:
  110. break;
  111. case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break;
  112. case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break;
  113. case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break;
  114. case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break;
  115. case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break;
  116. case 0x49:
  117. if ((family == 15) && (model == 6))
  118. /* On Xeon MP (family F, model 6), this is for L3 */
  119. VG_(message)(Vg_DebugMsg,
  120. "warning: L3 cache detected but ignored\n");
  121. else
  122. *L2c = (cache_t) { 4096, 16, 64 }; L2_found = True;
  123. break;
  124. /* These are sectored, whatever that means */
  125. case 0x60: *D1c = (cache_t) { 16, 8, 64 }; break; /* sectored */
  126. case 0x66: *D1c = (cache_t) { 8, 4, 64 }; break; /* sectored */
  127. case 0x67: *D1c = (cache_t) { 16, 4, 64 }; break; /* sectored */
  128. case 0x68: *D1c = (cache_t) { 32, 4, 64 }; break; /* sectored */
  129. /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based.
  130. * conversion to byte size is a total guess; treat the 12K and 16K
  131. * cases the same since the cache byte size must be a power of two for
  132. * everything to work!. Also guessing 32 bytes for the line size...
  133. */
  134. case 0x70: /* 12K micro-ops, 8-way */
  135. *I1c = (cache_t) { 16, 8, 32 };
  136. micro_ops_warn(12, 16, 32);
  137. break;
  138. case 0x71: /* 16K micro-ops, 8-way */
  139. *I1c = (cache_t) { 16, 8, 32 };
  140. micro_ops_warn(16, 16, 32);
  141. break;
  142. case 0x72: /* 32K micro-ops, 8-way */
  143. *I1c = (cache_t) { 32, 8, 32 };
  144. micro_ops_warn(32, 32, 32);
  145. break;
  146. /* These are sectored, whatever that means */
  147. case 0x79: *L2c = (cache_t) { 128, 8, 64 }; L2_found = True; break;
  148. case 0x7a: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break;
  149. case 0x7b: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
  150. case 0x7c: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
  151. case 0x7d: *L2c = (cache_t) { 2048, 8, 64 }; L2_found = True; break;
  152. case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break;
  153. case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break;
  154. case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break;
  155. case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break;
  156. case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break;
  157. case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break;
  158. case 0x86: *L2c = (cache_t) { 512, 4, 64 }; L2_found = True; break;
  159. case 0x87: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
  160. /* Ignore prefetch information */
  161. case 0xf0: case 0xf1:
  162. break;
  163. default:
  164. VG_(message)(Vg_DebugMsg,
  165. "warning: Unknown Intel cache config value "
  166. "(0x%x), ignoring", info[i]);
  167. break;
  168. }
  169. }
  170. if (!L2_found)
  171. VG_(message)(Vg_DebugMsg,
  172. "warning: L2 cache not installed, ignore L2 results.");
  173. return 0;
  174. }
  175. /* AMD method is straightforward, just extract appropriate bits from the
  176. * result registers.
  177. *
  178. * Bits, for D1 and I1:
  179. * 31..24 data L1 cache size in KBs
  180. * 23..16 data L1 cache associativity (FFh=full)
  181. * 15.. 8 data L1 cache lines per tag
  182. * 7.. 0 data L1 cache line size in bytes
  183. *
  184. * Bits, for L2:
  185. * 31..16 unified L2 cache size in KBs
  186. * 15..12 unified L2 cache associativity (0=off, FFh=full)
  187. * 11.. 8 unified L2 cache lines per tag
  188. * 7.. 0 unified L2 cache line size in bytes
  189. *
  190. * #3 The AMD K7 processor's L2 cache must be configured prior to relying
  191. * upon this information. (Whatever that means -- njn)
  192. *
  193. * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model
  194. * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB),
  195. * so we detect that.
  196. *
  197. * Returns 0 on success, non-zero on failure.
  198. */
  199. static
  200. Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c)
  201. {
  202. UInt ext_level;
  203. UInt dummy, model;
  204. UInt I1i, D1i, L2i;
  205. VG_(cpuid)(0x80000000, &ext_level, &dummy, &dummy, &dummy);
  206. if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) {
  207. VG_(message)(Vg_UserMsg,
  208. "warning: ext_level < 0x80000006 for AMD processor (0x%x)",
  209. ext_level);
  210. return -1;
  211. }
  212. VG_(cpuid)(0x80000005, &dummy, &dummy, &D1i, &I1i);
  213. VG_(cpuid)(0x80000006, &dummy, &dummy, &L2i, &dummy);
  214. VG_(cpuid)(0x1, &model, &dummy, &dummy, &dummy);
  215. /* Check for Duron bug */
  216. if (model == 0x630) {
  217. VG_(message)(Vg_UserMsg,
  218. "Buggy Duron stepping A0. Assuming L2 size=65536 bytes");
  219. L2i = (64 << 16) | (L2i & 0xffff);
  220. }
  221. D1c->size = (D1i >> 24) & 0xff;
  222. D1c->assoc = (D1i >> 16) & 0xff;
  223. D1c->line_size = (D1i >> 0) & 0xff;
  224. I1c->size = (I1i >> 24) & 0xff;
  225. I1c->assoc = (I1i >> 16) & 0xff;
  226. I1c->line_size = (I1i >> 0) & 0xff;
  227. L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */
  228. L2c->assoc = (L2i >> 12) & 0xf;
  229. L2c->line_size = (L2i >> 0) & 0xff;
  230. return 0;
  231. }
  232. static
  233. Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c)
  234. {
  235. Int level, ret;
  236. Char vendor_id[13];
  237. if (!VG_(has_cpuid)()) {
  238. VG_(message)(Vg_DebugMsg, "CPUID instruction not supported");
  239. return -1;
  240. }
  241. VG_(cpuid)(0, &level, (int*)&vendor_id[0],
  242. (int*)&vendor_id[8], (int*)&vendor_id[4]);
  243. vendor_id[12] = '\0';
  244. if (0 == level) {
  245. VG_(message)(Vg_DebugMsg, "CPUID level is 0, early Pentium?\n");
  246. return -1;
  247. }
  248. /* Only handling Intel and AMD chips... no Cyrix, Transmeta, etc */
  249. if (0 == VG_(strcmp)(vendor_id, "GenuineIntel")) {
  250. ret = Intel_cache_info(level, I1c, D1c, L2c);
  251. } else if (0 == VG_(strcmp)(vendor_id, "AuthenticAMD")) {
  252. ret = AMD_cache_info(I1c, D1c, L2c);
  253. } else {
  254. VG_(message)(Vg_DebugMsg, "CPU vendor ID not recognised (%s)",
  255. vendor_id);
  256. return -1;
  257. }
  258. /* Successful! Convert sizes from KB to bytes */
  259. I1c->size *= 1024;
  260. D1c->size *= 1024;
  261. L2c->size *= 1024;
  262. return ret;
  263. }
  264. void VG_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
  265. Bool all_caches_clo_defined)
  266. {
  267. Int res;
  268. // Set caches to default.
  269. *I1c = (cache_t) { 65536, 2, 64 };
  270. *D1c = (cache_t) { 65536, 2, 64 };
  271. *L2c = (cache_t) { 262144, 8, 64 };
  272. // Then replace with any info we can get from CPUID.
  273. res = get_caches_from_CPUID(I1c, D1c, L2c);
  274. // Warn if CPUID failed and config not completely specified from cmd line.
  275. if (res != 0 && !all_caches_clo_defined) {
  276. VG_(message)(Vg_DebugMsg,
  277. "Warning: Couldn't auto-detect cache config, using one "
  278. "or more defaults ");
  279. }
  280. }
  281. /*--------------------------------------------------------------------*/
  282. /*--- end ---*/
  283. /*--------------------------------------------------------------------*/