ds.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Debug Store (DS) support
  3. *
  4. * This provides a low-level interface to the hardware's Debug Store
  5. * feature that is used for branch trace store (BTS) and
  6. * precise-event based sampling (PEBS).
  7. *
  8. * It manages:
  9. * - DS and BTS hardware configuration
  10. * - buffer overflow handling (to be done)
  11. * - buffer access
  12. *
  13. * It does not do:
  14. * - security checking (is the caller allowed to trace the task)
  15. * - buffer allocation (memory accounting)
  16. *
  17. *
  18. * Copyright (C) 2007-2009 Intel Corporation.
  19. * Markus Metzger <markus.t.metzger@intel.com>, 2007-2009
  20. */
  21. #ifndef _ASM_X86_DS_H
  22. #define _ASM_X86_DS_H
  23. #include <linux/types.h>
  24. #include <linux/init.h>
  25. #include <linux/err.h>
  26. #ifdef CONFIG_X86_DS
  27. struct task_struct;
  28. struct ds_context;
  29. struct ds_tracer;
  30. struct bts_tracer;
  31. struct pebs_tracer;
  32. typedef void (*bts_ovfl_callback_t)(struct bts_tracer *);
  33. typedef void (*pebs_ovfl_callback_t)(struct pebs_tracer *);
  34. /*
  35. * A list of features plus corresponding macros to talk about them in
  36. * the ds_request function's flags parameter.
  37. *
  38. * We use the enum to index an array of corresponding control bits;
  39. * we use the macro to index a flags bit-vector.
  40. */
  41. enum ds_feature {
  42. dsf_bts = 0,
  43. dsf_bts_kernel,
  44. #define BTS_KERNEL (1 << dsf_bts_kernel)
  45. /* trace kernel-mode branches */
  46. dsf_bts_user,
  47. #define BTS_USER (1 << dsf_bts_user)
  48. /* trace user-mode branches */
  49. dsf_bts_overflow,
  50. dsf_bts_max,
  51. dsf_pebs = dsf_bts_max,
  52. dsf_pebs_max,
  53. dsf_ctl_max = dsf_pebs_max,
  54. dsf_bts_timestamps = dsf_ctl_max,
  55. #define BTS_TIMESTAMPS (1 << dsf_bts_timestamps)
  56. /* add timestamps into BTS trace */
  57. #define BTS_USER_FLAGS (BTS_KERNEL | BTS_USER | BTS_TIMESTAMPS)
  58. };
  59. /*
  60. * Request BTS or PEBS
  61. *
  62. * Due to alignement constraints, the actual buffer may be slightly
  63. * smaller than the requested or provided buffer.
  64. *
  65. * Returns a pointer to a tracer structure on success, or
  66. * ERR_PTR(errcode) on failure.
  67. *
  68. * The interrupt threshold is independent from the overflow callback
  69. * to allow users to use their own overflow interrupt handling mechanism.
  70. *
  71. * The function might sleep.
  72. *
  73. * task: the task to request recording for
  74. * cpu: the cpu to request recording for
  75. * base: the base pointer for the (non-pageable) buffer;
  76. * size: the size of the provided buffer in bytes
  77. * ovfl: pointer to a function to be called on buffer overflow;
  78. * NULL if cyclic buffer requested
  79. * th: the interrupt threshold in records from the end of the buffer;
  80. * -1 if no interrupt threshold is requested.
  81. * flags: a bit-mask of the above flags
  82. */
  83. extern struct bts_tracer *ds_request_bts_task(struct task_struct *task,
  84. void *base, size_t size,
  85. bts_ovfl_callback_t ovfl,
  86. size_t th, unsigned int flags);
  87. extern struct bts_tracer *ds_request_bts_cpu(int cpu, void *base, size_t size,
  88. bts_ovfl_callback_t ovfl,
  89. size_t th, unsigned int flags);
  90. extern struct pebs_tracer *ds_request_pebs_task(struct task_struct *task,
  91. void *base, size_t size,
  92. pebs_ovfl_callback_t ovfl,
  93. size_t th, unsigned int flags);
  94. extern struct pebs_tracer *ds_request_pebs_cpu(int cpu,
  95. void *base, size_t size,
  96. pebs_ovfl_callback_t ovfl,
  97. size_t th, unsigned int flags);
  98. /*
  99. * Release BTS or PEBS resources
  100. * Suspend and resume BTS or PEBS tracing
  101. *
  102. * Must be called with irq's enabled.
  103. *
  104. * tracer: the tracer handle returned from ds_request_~()
  105. */
  106. extern void ds_release_bts(struct bts_tracer *tracer);
  107. extern void ds_suspend_bts(struct bts_tracer *tracer);
  108. extern void ds_resume_bts(struct bts_tracer *tracer);
  109. extern void ds_release_pebs(struct pebs_tracer *tracer);
  110. extern void ds_suspend_pebs(struct pebs_tracer *tracer);
  111. extern void ds_resume_pebs(struct pebs_tracer *tracer);
  112. /*
  113. * Release BTS or PEBS resources
  114. * Suspend and resume BTS or PEBS tracing
  115. *
  116. * Cpu tracers must call this on the traced cpu.
  117. * Task tracers must call ds_release_~_noirq() for themselves.
  118. *
  119. * May be called with irq's disabled.
  120. *
  121. * Returns 0 if successful;
  122. * -EPERM if the cpu tracer does not trace the current cpu.
  123. * -EPERM if the task tracer does not trace itself.
  124. *
  125. * tracer: the tracer handle returned from ds_request_~()
  126. */
  127. extern int ds_release_bts_noirq(struct bts_tracer *tracer);
  128. extern int ds_suspend_bts_noirq(struct bts_tracer *tracer);
  129. extern int ds_resume_bts_noirq(struct bts_tracer *tracer);
  130. extern int ds_release_pebs_noirq(struct pebs_tracer *tracer);
  131. extern int ds_suspend_pebs_noirq(struct pebs_tracer *tracer);
  132. extern int ds_resume_pebs_noirq(struct pebs_tracer *tracer);
  133. /*
  134. * The raw DS buffer state as it is used for BTS and PEBS recording.
  135. *
  136. * This is the low-level, arch-dependent interface for working
  137. * directly on the raw trace data.
  138. */
  139. struct ds_trace {
  140. /* the number of bts/pebs records */
  141. size_t n;
  142. /* the size of a bts/pebs record in bytes */
  143. size_t size;
  144. /* pointers into the raw buffer:
  145. - to the first entry */
  146. void *begin;
  147. /* - one beyond the last entry */
  148. void *end;
  149. /* - one beyond the newest entry */
  150. void *top;
  151. /* - the interrupt threshold */
  152. void *ith;
  153. /* flags given on ds_request() */
  154. unsigned int flags;
  155. };
  156. /*
  157. * An arch-independent view on branch trace data.
  158. */
  159. enum bts_qualifier {
  160. bts_invalid,
  161. #define BTS_INVALID bts_invalid
  162. bts_branch,
  163. #define BTS_BRANCH bts_branch
  164. bts_task_arrives,
  165. #define BTS_TASK_ARRIVES bts_task_arrives
  166. bts_task_departs,
  167. #define BTS_TASK_DEPARTS bts_task_departs
  168. bts_qual_bit_size = 4,
  169. bts_qual_max = (1 << bts_qual_bit_size),
  170. };
  171. struct bts_struct {
  172. __u64 qualifier;
  173. union {
  174. /* BTS_BRANCH */
  175. struct {
  176. __u64 from;
  177. __u64 to;
  178. } lbr;
  179. /* BTS_TASK_ARRIVES or BTS_TASK_DEPARTS */
  180. struct {
  181. __u64 clock;
  182. pid_t pid;
  183. } event;
  184. } variant;
  185. };
  186. /*
  187. * The BTS state.
  188. *
  189. * This gives access to the raw DS state and adds functions to provide
  190. * an arch-independent view of the BTS data.
  191. */
  192. struct bts_trace {
  193. struct ds_trace ds;
  194. int (*read)(struct bts_tracer *tracer, const void *at,
  195. struct bts_struct *out);
  196. int (*write)(struct bts_tracer *tracer, const struct bts_struct *in);
  197. };
  198. /*
  199. * The PEBS state.
  200. *
  201. * This gives access to the raw DS state and the PEBS-specific counter
  202. * reset value.
  203. */
  204. struct pebs_trace {
  205. struct ds_trace ds;
  206. /* the number of valid counters in the below array */
  207. unsigned int counters;
  208. #define MAX_PEBS_COUNTERS 4
  209. /* the counter reset value */
  210. unsigned long long counter_reset[MAX_PEBS_COUNTERS];
  211. };
  212. /*
  213. * Read the BTS or PEBS trace.
  214. *
  215. * Returns a view on the trace collected for the parameter tracer.
  216. *
  217. * The view remains valid as long as the traced task is not running or
  218. * the tracer is suspended.
  219. * Writes into the trace buffer are not reflected.
  220. *
  221. * tracer: the tracer handle returned from ds_request_~()
  222. */
  223. extern const struct bts_trace *ds_read_bts(struct bts_tracer *tracer);
  224. extern const struct pebs_trace *ds_read_pebs(struct pebs_tracer *tracer);
  225. /*
  226. * Reset the write pointer of the BTS/PEBS buffer.
  227. *
  228. * Returns 0 on success; -Eerrno on error
  229. *
  230. * tracer: the tracer handle returned from ds_request_~()
  231. */
  232. extern int ds_reset_bts(struct bts_tracer *tracer);
  233. extern int ds_reset_pebs(struct pebs_tracer *tracer);
  234. /*
  235. * Set the PEBS counter reset value.
  236. *
  237. * Returns 0 on success; -Eerrno on error
  238. *
  239. * tracer: the tracer handle returned from ds_request_pebs()
  240. * counter: the index of the counter
  241. * value: the new counter reset value
  242. */
  243. extern int ds_set_pebs_reset(struct pebs_tracer *tracer,
  244. unsigned int counter, u64 value);
  245. /*
  246. * Initialization
  247. */
  248. struct cpuinfo_x86;
  249. extern void __cpuinit ds_init_intel(struct cpuinfo_x86 *);
  250. /*
  251. * Context switch work
  252. */
  253. extern void ds_switch_to(struct task_struct *prev, struct task_struct *next);
  254. #else /* CONFIG_X86_DS */
  255. struct cpuinfo_x86;
  256. static inline void __cpuinit ds_init_intel(struct cpuinfo_x86 *ignored) {}
  257. static inline void ds_switch_to(struct task_struct *prev,
  258. struct task_struct *next) {}
  259. #endif /* CONFIG_X86_DS */
  260. #endif /* _ASM_X86_DS_H */