ds.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 unsigned long get_bts_buffer_base(char *base)
  106. {
  107. return *(unsigned long *)(base + ds_cfg.bts_buffer_base.offset);
  108. }
  109. static inline void set_bts_buffer_base(char *base, unsigned long value)
  110. {
  111. (*(unsigned long *)(base + ds_cfg.bts_buffer_base.offset)) = value;
  112. }
  113. static inline unsigned long get_bts_index(char *base)
  114. {
  115. return *(unsigned long *)(base + ds_cfg.bts_index.offset);
  116. }
  117. static inline void set_bts_index(char *base, unsigned long value)
  118. {
  119. (*(unsigned long *)(base + ds_cfg.bts_index.offset)) = value;
  120. }
  121. static inline unsigned long get_bts_absolute_maximum(char *base)
  122. {
  123. return *(unsigned long *)(base + ds_cfg.bts_absolute_maximum.offset);
  124. }
  125. static inline void set_bts_absolute_maximum(char *base, unsigned long value)
  126. {
  127. (*(unsigned long *)(base + ds_cfg.bts_absolute_maximum.offset)) = value;
  128. }
  129. static inline unsigned long get_bts_interrupt_threshold(char *base)
  130. {
  131. return *(unsigned long *)(base + ds_cfg.bts_interrupt_threshold.offset);
  132. }
  133. static inline void set_bts_interrupt_threshold(char *base, unsigned long value)
  134. {
  135. (*(unsigned long *)(base + ds_cfg.bts_interrupt_threshold.offset)) = value;
  136. }
  137. static inline unsigned long get_from_ip(char *base)
  138. {
  139. return *(unsigned long *)(base + ds_cfg.from_ip.offset);
  140. }
  141. static inline void set_from_ip(char *base, unsigned long value)
  142. {
  143. (*(unsigned long *)(base + ds_cfg.from_ip.offset)) = value;
  144. }
  145. static inline unsigned long get_to_ip(char *base)
  146. {
  147. return *(unsigned long *)(base + ds_cfg.to_ip.offset);
  148. }
  149. static inline void set_to_ip(char *base, unsigned long value)
  150. {
  151. (*(unsigned 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. static inline unsigned long get_info_data(char *base)
  162. {
  163. return *(unsigned long *)(base + ds_cfg.info_data.offset);
  164. }
  165. static inline void set_info_data(char *base, unsigned long value)
  166. {
  167. (*(unsigned long *)(base + ds_cfg.info_data.offset)) = value;
  168. }
  169. int ds_allocate(void **dsp, size_t bts_size_in_bytes)
  170. {
  171. size_t bts_size_in_records;
  172. unsigned long bts;
  173. void *ds;
  174. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  175. return -EOPNOTSUPP;
  176. if (bts_size_in_bytes < 0)
  177. return -EINVAL;
  178. bts_size_in_records =
  179. bts_size_in_bytes / ds_cfg.sizeof_bts;
  180. bts_size_in_bytes =
  181. bts_size_in_records * ds_cfg.sizeof_bts;
  182. if (bts_size_in_bytes <= 0)
  183. return -EINVAL;
  184. bts = (unsigned long)kzalloc(bts_size_in_bytes, GFP_KERNEL);
  185. if (!bts)
  186. return -ENOMEM;
  187. ds = kzalloc(ds_cfg.sizeof_ds, GFP_KERNEL);
  188. if (!ds) {
  189. kfree((void *)bts);
  190. return -ENOMEM;
  191. }
  192. set_bts_buffer_base(ds, bts);
  193. set_bts_index(ds, bts);
  194. set_bts_absolute_maximum(ds, bts + bts_size_in_bytes);
  195. set_bts_interrupt_threshold(ds, bts + bts_size_in_bytes + 1);
  196. *dsp = ds;
  197. return 0;
  198. }
  199. int ds_free(void **dsp)
  200. {
  201. if (*dsp)
  202. kfree((void *)get_bts_buffer_base(*dsp));
  203. kfree(*dsp);
  204. *dsp = NULL;
  205. return 0;
  206. }
  207. int ds_get_bts_size(void *ds)
  208. {
  209. int size_in_bytes;
  210. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  211. return -EOPNOTSUPP;
  212. if (!ds)
  213. return 0;
  214. size_in_bytes =
  215. get_bts_absolute_maximum(ds) -
  216. get_bts_buffer_base(ds);
  217. return size_in_bytes;
  218. }
  219. int ds_get_bts_end(void *ds)
  220. {
  221. int size_in_bytes = ds_get_bts_size(ds);
  222. if (size_in_bytes <= 0)
  223. return size_in_bytes;
  224. return size_in_bytes / ds_cfg.sizeof_bts;
  225. }
  226. int ds_get_bts_index(void *ds)
  227. {
  228. int index_offset_in_bytes;
  229. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  230. return -EOPNOTSUPP;
  231. index_offset_in_bytes =
  232. get_bts_index(ds) -
  233. get_bts_buffer_base(ds);
  234. return index_offset_in_bytes / ds_cfg.sizeof_bts;
  235. }
  236. int ds_set_overflow(void *ds, int method)
  237. {
  238. switch (method) {
  239. case DS_O_SIGNAL:
  240. return -EOPNOTSUPP;
  241. case DS_O_WRAP:
  242. return 0;
  243. default:
  244. return -EINVAL;
  245. }
  246. }
  247. int ds_get_overflow(void *ds)
  248. {
  249. return DS_O_WRAP;
  250. }
  251. int ds_clear(void *ds)
  252. {
  253. int bts_size = ds_get_bts_size(ds);
  254. unsigned long bts_base;
  255. if (bts_size <= 0)
  256. return bts_size;
  257. bts_base = get_bts_buffer_base(ds);
  258. memset((void *)bts_base, 0, bts_size);
  259. set_bts_index(ds, bts_base);
  260. return 0;
  261. }
  262. int ds_read_bts(void *ds, int index, struct bts_struct *out)
  263. {
  264. void *bts;
  265. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  266. return -EOPNOTSUPP;
  267. if (index < 0)
  268. return -EINVAL;
  269. if (index >= ds_get_bts_size(ds))
  270. return -EINVAL;
  271. bts = (void *)(get_bts_buffer_base(ds) + (index * ds_cfg.sizeof_bts));
  272. memset(out, 0, sizeof(*out));
  273. if (get_from_ip(bts) == BTS_ESCAPE_ADDRESS) {
  274. out->qualifier = get_info_type(bts);
  275. out->variant.jiffies = get_info_data(bts);
  276. } else {
  277. out->qualifier = BTS_BRANCH;
  278. out->variant.lbr.from_ip = get_from_ip(bts);
  279. out->variant.lbr.to_ip = get_to_ip(bts);
  280. }
  281. return sizeof(*out);;
  282. }
  283. int ds_write_bts(void *ds, const struct bts_struct *in)
  284. {
  285. unsigned long bts;
  286. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  287. return -EOPNOTSUPP;
  288. if (ds_get_bts_size(ds) <= 0)
  289. return -ENXIO;
  290. bts = get_bts_index(ds);
  291. memset((void *)bts, 0, ds_cfg.sizeof_bts);
  292. switch (in->qualifier) {
  293. case BTS_INVALID:
  294. break;
  295. case BTS_BRANCH:
  296. set_from_ip((void *)bts, in->variant.lbr.from_ip);
  297. set_to_ip((void *)bts, in->variant.lbr.to_ip);
  298. break;
  299. case BTS_TASK_ARRIVES:
  300. case BTS_TASK_DEPARTS:
  301. set_from_ip((void *)bts, BTS_ESCAPE_ADDRESS);
  302. set_info_type((void *)bts, in->qualifier);
  303. set_info_data((void *)bts, in->variant.jiffies);
  304. break;
  305. default:
  306. return -EINVAL;
  307. }
  308. bts = bts + ds_cfg.sizeof_bts;
  309. if (bts >= get_bts_absolute_maximum(ds))
  310. bts = get_bts_buffer_base(ds);
  311. set_bts_index(ds, bts);
  312. return ds_cfg.sizeof_bts;
  313. }
  314. unsigned long ds_debugctl_mask(void)
  315. {
  316. return ds_cfg.debugctl_mask;
  317. }
  318. #ifdef __i386__
  319. static const struct ds_configuration ds_cfg_netburst = {
  320. .sizeof_ds = 9 * 4,
  321. .bts_buffer_base = { 0, 4 },
  322. .bts_index = { 4, 4 },
  323. .bts_absolute_maximum = { 8, 4 },
  324. .bts_interrupt_threshold = { 12, 4 },
  325. .sizeof_bts = 3 * 4,
  326. .from_ip = { 0, 4 },
  327. .to_ip = { 4, 4 },
  328. .info_type = { 4, 1 },
  329. .info_data = { 8, 4 },
  330. .debugctl_mask = (1<<2)|(1<<3)
  331. };
  332. static const struct ds_configuration ds_cfg_pentium_m = {
  333. .sizeof_ds = 9 * 4,
  334. .bts_buffer_base = { 0, 4 },
  335. .bts_index = { 4, 4 },
  336. .bts_absolute_maximum = { 8, 4 },
  337. .bts_interrupt_threshold = { 12, 4 },
  338. .sizeof_bts = 3 * 4,
  339. .from_ip = { 0, 4 },
  340. .to_ip = { 4, 4 },
  341. .info_type = { 4, 1 },
  342. .info_data = { 8, 4 },
  343. .debugctl_mask = (1<<6)|(1<<7)
  344. };
  345. #endif /* _i386_ */
  346. static const struct ds_configuration ds_cfg_core2 = {
  347. .sizeof_ds = 9 * 8,
  348. .bts_buffer_base = { 0, 8 },
  349. .bts_index = { 8, 8 },
  350. .bts_absolute_maximum = { 16, 8 },
  351. .bts_interrupt_threshold = { 24, 8 },
  352. .sizeof_bts = 3 * 8,
  353. .from_ip = { 0, 8 },
  354. .to_ip = { 8, 8 },
  355. .info_type = { 8, 1 },
  356. .info_data = { 16, 8 },
  357. .debugctl_mask = (1<<6)|(1<<7)|(1<<9)
  358. };
  359. static inline void
  360. ds_configure(const struct ds_configuration *cfg)
  361. {
  362. ds_cfg = *cfg;
  363. }
  364. void __cpuinit ds_init_intel(struct cpuinfo_x86 *c)
  365. {
  366. switch (c->x86) {
  367. case 0x6:
  368. switch (c->x86_model) {
  369. #ifdef __i386__
  370. case 0xD:
  371. case 0xE: /* Pentium M */
  372. ds_configure(&ds_cfg_pentium_m);
  373. break;
  374. #endif /* _i386_ */
  375. case 0xF: /* Core2 */
  376. ds_configure(&ds_cfg_core2);
  377. break;
  378. default:
  379. /* sorry, don't know about them */
  380. break;
  381. }
  382. break;
  383. case 0xF:
  384. switch (c->x86_model) {
  385. #ifdef __i386__
  386. case 0x0:
  387. case 0x1:
  388. case 0x2: /* Netburst */
  389. ds_configure(&ds_cfg_netburst);
  390. break;
  391. #endif /* _i386_ */
  392. default:
  393. /* sorry, don't know about them */
  394. break;
  395. }
  396. break;
  397. default:
  398. /* sorry, don't know about them */
  399. break;
  400. }
  401. }