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. }
  206. return 0;
  207. }
  208. int ds_get_bts_size(void *ds)
  209. {
  210. int size_in_bytes;
  211. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  212. return -EOPNOTSUPP;
  213. if (!ds)
  214. return 0;
  215. size_in_bytes =
  216. get_bts_absolute_maximum(ds) -
  217. get_bts_buffer_base(ds);
  218. return size_in_bytes;
  219. }
  220. int ds_get_bts_end(void *ds)
  221. {
  222. int size_in_bytes = ds_get_bts_size(ds);
  223. if (size_in_bytes <= 0)
  224. return size_in_bytes;
  225. return size_in_bytes / ds_cfg.sizeof_bts;
  226. }
  227. int ds_get_bts_index(void *ds)
  228. {
  229. int index_offset_in_bytes;
  230. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  231. return -EOPNOTSUPP;
  232. index_offset_in_bytes =
  233. get_bts_index(ds) -
  234. get_bts_buffer_base(ds);
  235. return index_offset_in_bytes / ds_cfg.sizeof_bts;
  236. }
  237. int ds_set_overflow(void *ds, int method)
  238. {
  239. switch (method) {
  240. case DS_O_SIGNAL:
  241. return -EOPNOTSUPP;
  242. case DS_O_WRAP:
  243. return 0;
  244. default:
  245. return -EINVAL;
  246. }
  247. }
  248. int ds_get_overflow(void *ds)
  249. {
  250. return DS_O_WRAP;
  251. }
  252. int ds_clear(void *ds)
  253. {
  254. int bts_size = ds_get_bts_size(ds);
  255. unsigned long bts_base;
  256. if (bts_size <= 0)
  257. return bts_size;
  258. bts_base = get_bts_buffer_base(ds);
  259. memset((void *)bts_base, 0, bts_size);
  260. set_bts_index(ds, bts_base);
  261. return 0;
  262. }
  263. int ds_read_bts(void *ds, int index, struct bts_struct *out)
  264. {
  265. void *bts;
  266. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  267. return -EOPNOTSUPP;
  268. if (index < 0)
  269. return -EINVAL;
  270. if (index >= ds_get_bts_size(ds))
  271. return -EINVAL;
  272. bts = (void *)(get_bts_buffer_base(ds) + (index * ds_cfg.sizeof_bts));
  273. memset(out, 0, sizeof(*out));
  274. if (get_from_ip(bts) == BTS_ESCAPE_ADDRESS) {
  275. out->qualifier = get_info_type(bts);
  276. out->variant.jiffies = get_info_data(bts);
  277. } else {
  278. out->qualifier = BTS_BRANCH;
  279. out->variant.lbr.from_ip = get_from_ip(bts);
  280. out->variant.lbr.to_ip = get_to_ip(bts);
  281. }
  282. return sizeof(*out);;
  283. }
  284. int ds_write_bts(void *ds, const struct bts_struct *in)
  285. {
  286. unsigned long bts;
  287. if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
  288. return -EOPNOTSUPP;
  289. if (ds_get_bts_size(ds) <= 0)
  290. return -ENXIO;
  291. bts = get_bts_index(ds);
  292. memset((void *)bts, 0, ds_cfg.sizeof_bts);
  293. switch (in->qualifier) {
  294. case BTS_INVALID:
  295. break;
  296. case BTS_BRANCH:
  297. set_from_ip((void *)bts, in->variant.lbr.from_ip);
  298. set_to_ip((void *)bts, in->variant.lbr.to_ip);
  299. break;
  300. case BTS_TASK_ARRIVES:
  301. case BTS_TASK_DEPARTS:
  302. set_from_ip((void *)bts, BTS_ESCAPE_ADDRESS);
  303. set_info_type((void *)bts, in->qualifier);
  304. set_info_data((void *)bts, in->variant.jiffies);
  305. break;
  306. default:
  307. return -EINVAL;
  308. }
  309. bts = bts + ds_cfg.sizeof_bts;
  310. if (bts >= get_bts_absolute_maximum(ds))
  311. bts = get_bts_buffer_base(ds);
  312. set_bts_index(ds, bts);
  313. return ds_cfg.sizeof_bts;
  314. }
  315. unsigned long ds_debugctl_mask(void)
  316. {
  317. return ds_cfg.debugctl_mask;
  318. }
  319. #ifdef __i386__
  320. static const struct ds_configuration ds_cfg_netburst = {
  321. .sizeof_ds = 9 * 4,
  322. .bts_buffer_base = { 0, 4 },
  323. .bts_index = { 4, 4 },
  324. .bts_absolute_maximum = { 8, 4 },
  325. .bts_interrupt_threshold = { 12, 4 },
  326. .sizeof_bts = 3 * 4,
  327. .from_ip = { 0, 4 },
  328. .to_ip = { 4, 4 },
  329. .info_type = { 4, 1 },
  330. .info_data = { 8, 4 },
  331. .debugctl_mask = (1<<2)|(1<<3)
  332. };
  333. static const struct ds_configuration ds_cfg_pentium_m = {
  334. .sizeof_ds = 9 * 4,
  335. .bts_buffer_base = { 0, 4 },
  336. .bts_index = { 4, 4 },
  337. .bts_absolute_maximum = { 8, 4 },
  338. .bts_interrupt_threshold = { 12, 4 },
  339. .sizeof_bts = 3 * 4,
  340. .from_ip = { 0, 4 },
  341. .to_ip = { 4, 4 },
  342. .info_type = { 4, 1 },
  343. .info_data = { 8, 4 },
  344. .debugctl_mask = (1<<6)|(1<<7)
  345. };
  346. #endif /* _i386_ */
  347. static const struct ds_configuration ds_cfg_core2 = {
  348. .sizeof_ds = 9 * 8,
  349. .bts_buffer_base = { 0, 8 },
  350. .bts_index = { 8, 8 },
  351. .bts_absolute_maximum = { 16, 8 },
  352. .bts_interrupt_threshold = { 24, 8 },
  353. .sizeof_bts = 3 * 8,
  354. .from_ip = { 0, 8 },
  355. .to_ip = { 8, 8 },
  356. .info_type = { 8, 1 },
  357. .info_data = { 16, 8 },
  358. .debugctl_mask = (1<<6)|(1<<7)|(1<<9)
  359. };
  360. static inline void
  361. ds_configure(const struct ds_configuration *cfg)
  362. {
  363. ds_cfg = *cfg;
  364. }
  365. void __cpuinit ds_init_intel(struct cpuinfo_x86 *c)
  366. {
  367. switch (c->x86) {
  368. case 0x6:
  369. switch (c->x86_model) {
  370. #ifdef __i386__
  371. case 0xD:
  372. case 0xE: /* Pentium M */
  373. ds_configure(&ds_cfg_pentium_m);
  374. break;
  375. #endif /* _i386_ */
  376. case 0xF: /* Core2 */
  377. ds_configure(&ds_cfg_core2);
  378. break;
  379. default:
  380. /* sorry, don't know about them */
  381. break;
  382. }
  383. break;
  384. case 0xF:
  385. switch (c->x86_model) {
  386. #ifdef __i386__
  387. case 0x0:
  388. case 0x1:
  389. case 0x2: /* Netburst */
  390. ds_configure(&ds_cfg_netburst);
  391. break;
  392. #endif /* _i386_ */
  393. default:
  394. /* sorry, don't know about them */
  395. break;
  396. }
  397. break;
  398. default:
  399. /* sorry, don't know about them */
  400. break;
  401. }
  402. }