ds.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Debug Store support
  3. *
  4. * This provides a low-level interface to the hardware's Debug Store
  5. * feature that is used for last branch recording (LBR) and
  6. * precise-event based sampling (PEBS).
  7. *
  8. * Different architectures use a different DS layout/pointer size.
  9. * The below functions therefore work on a void*.
  10. *
  11. *
  12. * Since there is no user for PEBS, yet, only LBR (or branch
  13. * trace store, BTS) is supported.
  14. *
  15. *
  16. * Copyright (C) 2007 Intel Corporation.
  17. * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
  18. */
  19. #include <asm/ds.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/slab.h>
  23. /*
  24. * Debug Store (DS) save area configuration (see Intel64 and IA32
  25. * Architectures Software Developer's Manual, section 18.5)
  26. *
  27. * The DS configuration consists of the following fields; different
  28. * architetures vary in the size of those fields.
  29. * - double-word aligned base linear address of the BTS buffer
  30. * - write pointer into the BTS buffer
  31. * - end linear address of the BTS buffer (one byte beyond the end of
  32. * the buffer)
  33. * - interrupt pointer into BTS buffer
  34. * (interrupt occurs when write pointer passes interrupt pointer)
  35. * - double-word aligned base linear address of the PEBS buffer
  36. * - write pointer into the PEBS buffer
  37. * - end linear address of the PEBS buffer (one byte beyond the end of
  38. * the buffer)
  39. * - interrupt pointer into PEBS buffer
  40. * (interrupt occurs when write pointer passes interrupt pointer)
  41. * - value to which counter is reset following counter overflow
  42. *
  43. * On later architectures, the last branch recording hardware uses
  44. * 64bit pointers even in 32bit mode.
  45. *
  46. *
  47. * Branch Trace Store (BTS) records store information about control
  48. * flow changes. They at least provide the following information:
  49. * - source linear address
  50. * - destination linear address
  51. *
  52. * Netburst supported a predicated bit that had been dropped in later
  53. * architectures. We do not suppor it.
  54. *
  55. *
  56. * In order to abstract from the actual DS and BTS layout, we describe
  57. * the access to the relevant fields.
  58. * Thanks to Andi Kleen for proposing this design.
  59. *
  60. * The implementation, however, is not as general as it might seem. In
  61. * order to stay somewhat simple and efficient, we assume an
  62. * underlying unsigned type (mostly a pointer type) and we expect the
  63. * field to be at least as big as that type.
  64. */
  65. /*
  66. * A special from_ip address to indicate that the BTS record is an
  67. * info record that needs to be interpreted or skipped.
  68. */
  69. #define BTS_ESCAPE_ADDRESS (-1)
  70. /*
  71. * A field access descriptor
  72. */
  73. struct access_desc {
  74. unsigned char offset;
  75. unsigned char size;
  76. };
  77. /*
  78. * The configuration for a particular DS/BTS hardware implementation.
  79. */
  80. struct ds_configuration {
  81. /* the DS configuration */
  82. unsigned char sizeof_ds;
  83. struct access_desc bts_buffer_base;
  84. struct access_desc bts_index;
  85. struct access_desc bts_absolute_maximum;
  86. struct access_desc bts_interrupt_threshold;
  87. /* the BTS configuration */
  88. unsigned char sizeof_bts;
  89. struct access_desc from_ip;
  90. struct access_desc to_ip;
  91. /* BTS variants used to store additional information like
  92. timestamps */
  93. struct access_desc info_type;
  94. struct access_desc info_data;
  95. unsigned long debugctl_mask;
  96. };
  97. /*
  98. * The global configuration used by the below accessor functions
  99. */
  100. static struct ds_configuration ds_cfg;
  101. /*
  102. * Accessor functions for some DS and BTS fields using the above
  103. * global ptrace_bts_cfg.
  104. */
  105. static inline void *get_bts_buffer_base(char *base)
  106. {
  107. return *(void **)(base + ds_cfg.bts_buffer_base.offset);
  108. }
  109. static inline void set_bts_buffer_base(char *base, void *value)
  110. {
  111. (*(void **)(base + ds_cfg.bts_buffer_base.offset)) = value;
  112. }
  113. static inline void *get_bts_index(char *base)
  114. {
  115. return *(void **)(base + ds_cfg.bts_index.offset);
  116. }
  117. static inline void set_bts_index(char *base, void *value)
  118. {
  119. (*(void **)(base + ds_cfg.bts_index.offset)) = value;
  120. }
  121. static inline void *get_bts_absolute_maximum(char *base)
  122. {
  123. return *(void **)(base + ds_cfg.bts_absolute_maximum.offset);
  124. }
  125. static inline void set_bts_absolute_maximum(char *base, void *value)
  126. {
  127. (*(void **)(base + ds_cfg.bts_absolute_maximum.offset)) = value;
  128. }
  129. static inline void *get_bts_interrupt_threshold(char *base)
  130. {
  131. return *(void **)(base + ds_cfg.bts_interrupt_threshold.offset);
  132. }
  133. static inline void set_bts_interrupt_threshold(char *base, void *value)
  134. {
  135. (*(void **)(base + ds_cfg.bts_interrupt_threshold.offset)) = value;
  136. }
  137. static inline long get_from_ip(char *base)
  138. {
  139. return *(long *)(base + ds_cfg.from_ip.offset);
  140. }
  141. static inline void set_from_ip(char *base, long value)
  142. {
  143. (*(long *)(base + ds_cfg.from_ip.offset)) = value;
  144. }
  145. static inline long get_to_ip(char *base)
  146. {
  147. return *(long *)(base + ds_cfg.to_ip.offset);
  148. }
  149. static inline void set_to_ip(char *base, long value)
  150. {
  151. (*(long *)(base + ds_cfg.to_ip.offset)) = value;
  152. }
  153. static inline unsigned char get_info_type(char *base)
  154. {
  155. return *(unsigned char *)(base + ds_cfg.info_type.offset);
  156. }
  157. static inline void set_info_type(char *base, unsigned char value)
  158. {
  159. (*(unsigned char *)(base + ds_cfg.info_type.offset)) = value;
  160. }
  161. /*
  162. * The info data might overlap with the info type on some architectures.
  163. * We therefore read and write the exact number of bytes.
  164. */
  165. static inline unsigned long long get_info_data(char *base)
  166. {
  167. unsigned long long value = 0;
  168. memcpy(&value,
  169. base + ds_cfg.info_data.offset,
  170. ds_cfg.info_data.size);
  171. return value;
  172. }
  173. static inline void set_info_data(char *base, unsigned long long value)
  174. {
  175. memcpy(base + ds_cfg.info_data.offset,
  176. &value,
  177. ds_cfg.info_data.size);
  178. }
  179. int ds_allocate(void **dsp, size_t bts_size_in_records)
  180. {
  181. size_t bts_size_in_bytes = 0;
  182. void *bts = 0;
  183. void *ds = 0;
  184. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  185. return -EOPNOTSUPP;
  186. if (bts_size_in_records < 0)
  187. return -EINVAL;
  188. bts_size_in_bytes =
  189. bts_size_in_records * ds_cfg.sizeof_bts;
  190. if (bts_size_in_bytes <= 0)
  191. return -EINVAL;
  192. bts = kzalloc(bts_size_in_bytes, GFP_KERNEL);
  193. if (!bts)
  194. return -ENOMEM;
  195. ds = kzalloc(ds_cfg.sizeof_ds, GFP_KERNEL);
  196. if (!ds) {
  197. kfree(bts);
  198. return -ENOMEM;
  199. }
  200. set_bts_buffer_base(ds, bts);
  201. set_bts_index(ds, bts);
  202. set_bts_absolute_maximum(ds, bts + bts_size_in_bytes);
  203. set_bts_interrupt_threshold(ds, bts + bts_size_in_bytes + 1);
  204. *dsp = ds;
  205. return 0;
  206. }
  207. int ds_free(void **dsp)
  208. {
  209. if (*dsp)
  210. kfree(get_bts_buffer_base(*dsp));
  211. kfree(*dsp);
  212. *dsp = 0;
  213. return 0;
  214. }
  215. int ds_get_bts_size(void *ds)
  216. {
  217. size_t size_in_bytes;
  218. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  219. return -EOPNOTSUPP;
  220. size_in_bytes =
  221. get_bts_absolute_maximum(ds) -
  222. get_bts_buffer_base(ds);
  223. return size_in_bytes / ds_cfg.sizeof_bts;
  224. }
  225. int ds_get_bts_index(void *ds)
  226. {
  227. size_t index_offset_in_bytes;
  228. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  229. return -EOPNOTSUPP;
  230. index_offset_in_bytes =
  231. get_bts_index(ds) -
  232. get_bts_buffer_base(ds);
  233. return index_offset_in_bytes / ds_cfg.sizeof_bts;
  234. }
  235. int ds_read_bts(void *ds, size_t index, struct bts_struct *out)
  236. {
  237. void *bts;
  238. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  239. return -EOPNOTSUPP;
  240. if (index < 0)
  241. return -EINVAL;
  242. if (index >= ds_get_bts_size(ds))
  243. return -EINVAL;
  244. bts = get_bts_buffer_base(ds);
  245. bts = (char *)bts + (index * ds_cfg.sizeof_bts);
  246. memset(out, 0, sizeof(*out));
  247. if (get_from_ip(bts) == BTS_ESCAPE_ADDRESS) {
  248. out->qualifier = get_info_type(bts);
  249. out->variant.timestamp = get_info_data(bts);
  250. } else {
  251. out->qualifier = BTS_BRANCH;
  252. out->variant.lbr.from_ip = get_from_ip(bts);
  253. out->variant.lbr.to_ip = get_to_ip(bts);
  254. }
  255. return 0;
  256. }
  257. int ds_write_bts(void *ds, const struct bts_struct *in)
  258. {
  259. void *bts;
  260. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  261. return -EOPNOTSUPP;
  262. if (ds_get_bts_size(ds) <= 0)
  263. return -ENXIO;
  264. bts = get_bts_index(ds);
  265. memset(bts, 0, ds_cfg.sizeof_bts);
  266. switch (in->qualifier) {
  267. case BTS_INVALID:
  268. break;
  269. case BTS_BRANCH:
  270. set_from_ip(bts, in->variant.lbr.from_ip);
  271. set_to_ip(bts, in->variant.lbr.to_ip);
  272. break;
  273. case BTS_TASK_ARRIVES:
  274. case BTS_TASK_DEPARTS:
  275. set_from_ip(bts, BTS_ESCAPE_ADDRESS);
  276. set_info_type(bts, in->qualifier);
  277. set_info_data(bts, in->variant.timestamp);
  278. break;
  279. default:
  280. return -EINVAL;
  281. }
  282. bts = (char *)bts + ds_cfg.sizeof_bts;
  283. if (bts >= get_bts_absolute_maximum(ds))
  284. bts = get_bts_buffer_base(ds);
  285. set_bts_index(ds, bts);
  286. return 0;
  287. }
  288. unsigned long ds_debugctl_mask(void)
  289. {
  290. return ds_cfg.debugctl_mask;
  291. }
  292. #ifdef __i386__
  293. static const struct ds_configuration ds_cfg_netburst = {
  294. .sizeof_ds = 9 * 4,
  295. .bts_buffer_base = { 0, 4 },
  296. .bts_index = { 4, 4 },
  297. .bts_absolute_maximum = { 8, 4 },
  298. .bts_interrupt_threshold = { 12, 4 },
  299. .sizeof_bts = 3 * 4,
  300. .from_ip = { 0, 4 },
  301. .to_ip = { 4, 4 },
  302. .info_type = { 4, 1 },
  303. .info_data = { 5, 7 },
  304. .debugctl_mask = (1<<2)|(1<<3)
  305. };
  306. static const struct ds_configuration ds_cfg_pentium_m = {
  307. .sizeof_ds = 9 * 4,
  308. .bts_buffer_base = { 0, 4 },
  309. .bts_index = { 4, 4 },
  310. .bts_absolute_maximum = { 8, 4 },
  311. .bts_interrupt_threshold = { 12, 4 },
  312. .sizeof_bts = 3 * 4,
  313. .from_ip = { 0, 4 },
  314. .to_ip = { 4, 4 },
  315. .info_type = { 4, 1 },
  316. .info_data = { 5, 7 },
  317. .debugctl_mask = (1<<6)|(1<<7)
  318. };
  319. #endif /* _i386_ */
  320. static const struct ds_configuration ds_cfg_core2 = {
  321. .sizeof_ds = 9 * 8,
  322. .bts_buffer_base = { 0, 8 },
  323. .bts_index = { 8, 8 },
  324. .bts_absolute_maximum = { 16, 8 },
  325. .bts_interrupt_threshold = { 24, 8 },
  326. .sizeof_bts = 3 * 8,
  327. .from_ip = { 0, 8 },
  328. .to_ip = { 8, 8 },
  329. .info_type = { 8, 1 },
  330. .info_data = { 9, 7 },
  331. .debugctl_mask = (1<<6)|(1<<7)|(1<<9)
  332. };
  333. static inline void
  334. ds_configure(const struct ds_configuration *cfg)
  335. {
  336. ds_cfg = *cfg;
  337. }
  338. void __cpuinit ds_init_intel(struct cpuinfo_x86 *c)
  339. {
  340. switch (c->x86) {
  341. case 0x6:
  342. switch (c->x86_model) {
  343. #ifdef __i386__
  344. case 0xD:
  345. case 0xE: /* Pentium M */
  346. ds_configure(&ds_cfg_pentium_m);
  347. break;
  348. #endif /* _i386_ */
  349. case 0xF: /* Core2 */
  350. ds_configure(&ds_cfg_core2);
  351. break;
  352. default:
  353. /* sorry, don't know about them */
  354. break;
  355. }
  356. break;
  357. case 0xF:
  358. switch (c->x86_model) {
  359. #ifdef __i386__
  360. case 0x0:
  361. case 0x1:
  362. case 0x2: /* Netburst */
  363. ds_configure(&ds_cfg_netburst);
  364. break;
  365. #endif /* _i386_ */
  366. default:
  367. /* sorry, don't know about them */
  368. break;
  369. }
  370. break;
  371. default:
  372. /* sorry, don't know about them */
  373. break;
  374. }
  375. }