PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cachegrind/cg-x86-amd64.c

https://github.com/sos22/ppres
C | 365 lines | 216 code | 61 blank | 88 comment | 31 complexity | f6e75a3190ac497a7edec8ba6392e6fe MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0
  1. /*--------------------------------------------------------------------*/
  2. /*--- x86- and AMD64-specific definitions. cg-x86-amd64.c ---*/
  3. /*--------------------------------------------------------------------*/
  4. /*
  5. This file is part of Cachegrind, a Valgrind tool for cache
  6. profiling programs.
  7. Copyright (C) 2002-2010 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. #if defined(VGA_x86) || defined(VGA_amd64)
  24. #include "pub_tool_basics.h"
  25. #include "pub_tool_cpuid.h"
  26. #include "pub_tool_libcbase.h"
  27. #include "pub_tool_libcassert.h"
  28. #include "pub_tool_libcprint.h"
  29. #include "cg_arch.h"
  30. // All CPUID info taken from sandpile.org/ia32/cpuid.htm */
  31. // Probably only works for Intel and AMD chips, and probably only for some of
  32. // them.
  33. static void micro_ops_warn(Int actual_size, Int used_size, Int line_size)
  34. {
  35. VG_(dmsg)("warning: Pentium 4 with %d KB micro-op instruction trace cache\n",
  36. actual_size);
  37. VG_(dmsg)(" Simulating a %d KB I-cache with %d B lines\n",
  38. used_size, line_size);
  39. }
  40. /* Intel method is truly wretched. We have to do an insane indexing into an
  41. * array of pre-defined configurations for various parts of the memory
  42. * hierarchy.
  43. * According to Intel Processor Identification, App Note 485.
  44. */
  45. static
  46. Int Intel_cache_info(Int level, cache_t* I1c, cache_t* D1c, cache_t* L2c)
  47. {
  48. Int cpuid1_eax;
  49. Int cpuid1_ignore;
  50. Int family;
  51. Int model;
  52. UChar info[16];
  53. Int i, trials;
  54. Bool L2_found = False;
  55. if (level < 2) {
  56. VG_(dmsg)("warning: CPUID level < 2 for Intel processor (%d)\n", level);
  57. return -1;
  58. }
  59. /* family/model needed to distinguish code reuse (currently 0x49) */
  60. VG_(cpuid)(1, &cpuid1_eax, &cpuid1_ignore,
  61. &cpuid1_ignore, &cpuid1_ignore);
  62. family = (((cpuid1_eax >> 20) & 0xff) << 4) + ((cpuid1_eax >> 8) & 0xf);
  63. model = (((cpuid1_eax >> 16) & 0xf) << 4) + ((cpuid1_eax >> 4) & 0xf);
  64. VG_(cpuid)(2, (Int*)&info[0], (Int*)&info[4],
  65. (Int*)&info[8], (Int*)&info[12]);
  66. trials = info[0] - 1; /* AL register - bits 0..7 of %eax */
  67. info[0] = 0x0; /* reset AL */
  68. if (0 != trials) {
  69. VG_(dmsg)("warning: non-zero CPUID trials for Intel processor (%d)\n",
  70. trials);
  71. return -1;
  72. }
  73. for (i = 0; i < 16; i++) {
  74. switch (info[i]) {
  75. case 0x0: /* ignore zeros */
  76. break;
  77. /* TLB info, ignore */
  78. case 0x01: case 0x02: case 0x03: case 0x04: case 0x05:
  79. case 0x4f: case 0x50: case 0x51: case 0x52: case 0x55:
  80. case 0x56: case 0x57: case 0x59:
  81. case 0x5a: case 0x5b: case 0x5c: case 0x5d:
  82. case 0xb0: case 0xb1: case 0xb2:
  83. case 0xb3: case 0xb4: case 0xba: case 0xc0:
  84. case 0xca:
  85. break;
  86. case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break;
  87. case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break;
  88. case 0x09: *I1c = (cache_t) { 32, 4, 64 }; 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 0x0e: *D1c = (cache_t) { 24, 6, 64 }; break;
  93. case 0x2c: *D1c = (cache_t) { 32, 8, 64 }; break;
  94. /* IA-64 info -- panic! */
  95. case 0x10: case 0x15: case 0x1a:
  96. case 0x88: case 0x89: case 0x8a: case 0x8d:
  97. case 0x90: case 0x96: case 0x9b:
  98. VG_(tool_panic)("IA-64 cache detected?!");
  99. case 0x22: case 0x23: case 0x25: case 0x29:
  100. case 0x46: case 0x47: case 0x4a: case 0x4b: case 0x4c: case 0x4d:
  101. case 0xe2: case 0xe3: case 0xe4: case 0xea: case 0xeb: case 0xec:
  102. VG_(dmsg)("warning: L3 cache detected but ignored\n");
  103. break;
  104. /* Described as "MLC" in Intel documentation */
  105. case 0x21: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break;
  106. /* These are sectored, whatever that means */
  107. case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break;
  108. case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break;
  109. /* If a P6 core, this means "no L2 cache".
  110. If a P4 core, this means "no L3 cache".
  111. We don't know what core it is, so don't issue a warning. To detect
  112. a missing L2 cache, we use 'L2_found'. */
  113. case 0x40:
  114. break;
  115. case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break;
  116. case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break;
  117. case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break;
  118. case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break;
  119. case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break;
  120. case 0x48: *L2c = (cache_t) { 3072,12, 64 }; L2_found = True; break;
  121. case 0x49:
  122. if ((family == 15) && (model == 6))
  123. /* On Xeon MP (family F, model 6), this is for L3 */
  124. VG_(dmsg)("warning: L3 cache detected but ignored\n");
  125. else
  126. *L2c = (cache_t) { 4096, 16, 64 }; L2_found = True;
  127. break;
  128. case 0x4e: *L2c = (cache_t) { 6144, 24, 64 }; L2_found = True; break;
  129. /* These are sectored, whatever that means */
  130. case 0x60: *D1c = (cache_t) { 16, 8, 64 }; break; /* sectored */
  131. 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. /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based.
  135. * conversion to byte size is a total guess; treat the 12K and 16K
  136. * cases the same since the cache byte size must be a power of two for
  137. * everything to work!. Also guessing 32 bytes for the line size...
  138. */
  139. case 0x70: /* 12K micro-ops, 8-way */
  140. *I1c = (cache_t) { 16, 8, 32 };
  141. micro_ops_warn(12, 16, 32);
  142. break;
  143. case 0x71: /* 16K micro-ops, 8-way */
  144. *I1c = (cache_t) { 16, 8, 32 };
  145. micro_ops_warn(16, 16, 32);
  146. break;
  147. case 0x72: /* 32K micro-ops, 8-way */
  148. *I1c = (cache_t) { 32, 8, 32 };
  149. micro_ops_warn(32, 32, 32);
  150. break;
  151. /* not sectored, whatever that might mean */
  152. case 0x78: *L2c = (cache_t) { 1024, 4, 64 }; L2_found = True; break;
  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 0x7d: *L2c = (cache_t) { 2048, 8, 64 }; L2_found = True; break;
  159. case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break;
  160. case 0x7f: *L2c = (cache_t) { 512, 2, 64 }; L2_found = True; break;
  161. case 0x80: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
  162. case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break;
  163. case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break;
  164. case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break;
  165. case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break;
  166. case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break;
  167. case 0x86: *L2c = (cache_t) { 512, 4, 64 }; L2_found = True; break;
  168. case 0x87: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
  169. /* Ignore prefetch information */
  170. case 0xf0: case 0xf1:
  171. break;
  172. default:
  173. VG_(dmsg)("warning: Unknown Intel cache config value (0x%x), ignoring\n",
  174. info[i]);
  175. break;
  176. }
  177. }
  178. if (!L2_found)
  179. VG_(dmsg)("warning: L2 cache not installed, ignore L2 results.\n");
  180. return 0;
  181. }
  182. /* AMD method is straightforward, just extract appropriate bits from the
  183. * result registers.
  184. *
  185. * Bits, for D1 and I1:
  186. * 31..24 data L1 cache size in KBs
  187. * 23..16 data L1 cache associativity (FFh=full)
  188. * 15.. 8 data L1 cache lines per tag
  189. * 7.. 0 data L1 cache line size in bytes
  190. *
  191. * Bits, for L2:
  192. * 31..16 unified L2 cache size in KBs
  193. * 15..12 unified L2 cache associativity (0=off, FFh=full)
  194. * 11.. 8 unified L2 cache lines per tag
  195. * 7.. 0 unified L2 cache line size in bytes
  196. *
  197. * #3 The AMD K7 processor's L2 cache must be configured prior to relying
  198. * upon this information. (Whatever that means -- njn)
  199. *
  200. * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model
  201. * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB),
  202. * so we detect that.
  203. *
  204. * Returns 0 on success, non-zero on failure.
  205. */
  206. static
  207. Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c)
  208. {
  209. UInt ext_level;
  210. UInt dummy, model;
  211. UInt I1i, D1i, L2i;
  212. VG_(cpuid)(0x80000000, &ext_level, &dummy, &dummy, &dummy);
  213. if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) {
  214. VG_(dmsg)("warning: ext_level < 0x80000006 for AMD processor (0x%x)\n",
  215. ext_level);
  216. return -1;
  217. }
  218. VG_(cpuid)(0x80000005, &dummy, &dummy, &D1i, &I1i);
  219. VG_(cpuid)(0x80000006, &dummy, &dummy, &L2i, &dummy);
  220. VG_(cpuid)(0x1, &model, &dummy, &dummy, &dummy);
  221. /* Check for Duron bug */
  222. if (model == 0x630) {
  223. VG_(dmsg)("warning: Buggy Duron stepping A0. Assuming L2 size=65536 bytes\n");
  224. L2i = (64 << 16) | (L2i & 0xffff);
  225. }
  226. D1c->size = (D1i >> 24) & 0xff;
  227. D1c->assoc = (D1i >> 16) & 0xff;
  228. D1c->line_size = (D1i >> 0) & 0xff;
  229. I1c->size = (I1i >> 24) & 0xff;
  230. I1c->assoc = (I1i >> 16) & 0xff;
  231. I1c->line_size = (I1i >> 0) & 0xff;
  232. L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */
  233. L2c->assoc = (L2i >> 12) & 0xf;
  234. L2c->line_size = (L2i >> 0) & 0xff;
  235. return 0;
  236. }
  237. static
  238. Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c)
  239. {
  240. Int level, ret;
  241. Char vendor_id[13];
  242. if (!VG_(has_cpuid)()) {
  243. VG_(dmsg)("CPUID instruction not supported\n");
  244. return -1;
  245. }
  246. VG_(cpuid)(0, &level, (int*)&vendor_id[0],
  247. (int*)&vendor_id[8], (int*)&vendor_id[4]);
  248. vendor_id[12] = '\0';
  249. if (0 == level) {
  250. VG_(dmsg)("CPUID level is 0, early Pentium?\n");
  251. return -1;
  252. }
  253. /* Only handling Intel and AMD chips... no Cyrix, Transmeta, etc */
  254. if (0 == VG_(strcmp)(vendor_id, "GenuineIntel")) {
  255. ret = Intel_cache_info(level, I1c, D1c, L2c);
  256. } else if (0 == VG_(strcmp)(vendor_id, "AuthenticAMD")) {
  257. ret = AMD_cache_info(I1c, D1c, L2c);
  258. } else if (0 == VG_(strcmp)(vendor_id, "CentaurHauls")) {
  259. /* Total kludge. Pretend to be a VIA Nehemiah. */
  260. D1c->size = 64;
  261. D1c->assoc = 16;
  262. D1c->line_size = 16;
  263. I1c->size = 64;
  264. I1c->assoc = 4;
  265. I1c->line_size = 16;
  266. L2c->size = 64;
  267. L2c->assoc = 16;
  268. L2c->line_size = 16;
  269. ret = 0;
  270. } else {
  271. VG_(dmsg)("CPU vendor ID not recognised (%s)\n", vendor_id);
  272. return -1;
  273. }
  274. /* Successful! Convert sizes from KB to bytes */
  275. I1c->size *= 1024;
  276. D1c->size *= 1024;
  277. L2c->size *= 1024;
  278. return ret;
  279. }
  280. void VG_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
  281. Bool all_caches_clo_defined)
  282. {
  283. Int res;
  284. // Set caches to default.
  285. *I1c = (cache_t) { 65536, 2, 64 };
  286. *D1c = (cache_t) { 65536, 2, 64 };
  287. *L2c = (cache_t) { 262144, 8, 64 };
  288. // Then replace with any info we can get from CPUID.
  289. res = get_caches_from_CPUID(I1c, D1c, L2c);
  290. // Warn if CPUID failed and config not completely specified from cmd line.
  291. if (res != 0 && !all_caches_clo_defined) {
  292. VG_(dmsg)("Warning: Couldn't auto-detect cache config, using one "
  293. "or more defaults \n");
  294. }
  295. }
  296. #endif // defined(VGA_x86) || defined(VGA_amd64)
  297. /*--------------------------------------------------------------------*/
  298. /*--- end ---*/
  299. /*--------------------------------------------------------------------*/