PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cachegrind/cg-x86.c

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