ds.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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-2008 Intel Corporation.
  19. * Markus Metzger <markus.t.metzger@intel.com>, 2007-2008
  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. * task: the task to request recording for;
  72. * NULL for per-cpu recording on the current cpu
  73. * base: the base pointer for the (non-pageable) buffer;
  74. * size: the size of the provided buffer in bytes
  75. * ovfl: pointer to a function to be called on buffer overflow;
  76. * NULL if cyclic buffer requested
  77. * th: the interrupt threshold in records from the end of the buffer;
  78. * -1 if no interrupt threshold is requested.
  79. * flags: a bit-mask of the above flags
  80. */
  81. extern struct bts_tracer *ds_request_bts(struct task_struct *task,
  82. void *base, size_t size,
  83. bts_ovfl_callback_t ovfl,
  84. size_t th, unsigned int flags);
  85. extern struct pebs_tracer *ds_request_pebs(struct task_struct *task,
  86. void *base, size_t size,
  87. pebs_ovfl_callback_t ovfl,
  88. size_t th, unsigned int flags);
  89. /*
  90. * Release BTS or PEBS resources
  91. * Suspend and resume BTS or PEBS tracing
  92. *
  93. * tracer: the tracer handle returned from ds_request_~()
  94. */
  95. extern void ds_release_bts(struct bts_tracer *tracer);
  96. extern void ds_suspend_bts(struct bts_tracer *tracer);
  97. extern void ds_resume_bts(struct bts_tracer *tracer);
  98. extern void ds_release_pebs(struct pebs_tracer *tracer);
  99. extern void ds_suspend_pebs(struct pebs_tracer *tracer);
  100. extern void ds_resume_pebs(struct pebs_tracer *tracer);
  101. /*
  102. * The raw DS buffer state as it is used for BTS and PEBS recording.
  103. *
  104. * This is the low-level, arch-dependent interface for working
  105. * directly on the raw trace data.
  106. */
  107. struct ds_trace {
  108. /* the number of bts/pebs records */
  109. size_t n;
  110. /* the size of a bts/pebs record in bytes */
  111. size_t size;
  112. /* pointers into the raw buffer:
  113. - to the first entry */
  114. void *begin;
  115. /* - one beyond the last entry */
  116. void *end;
  117. /* - one beyond the newest entry */
  118. void *top;
  119. /* - the interrupt threshold */
  120. void *ith;
  121. /* flags given on ds_request() */
  122. unsigned int flags;
  123. };
  124. /*
  125. * An arch-independent view on branch trace data.
  126. */
  127. enum bts_qualifier {
  128. bts_invalid,
  129. #define BTS_INVALID bts_invalid
  130. bts_branch,
  131. #define BTS_BRANCH bts_branch
  132. bts_task_arrives,
  133. #define BTS_TASK_ARRIVES bts_task_arrives
  134. bts_task_departs,
  135. #define BTS_TASK_DEPARTS bts_task_departs
  136. bts_qual_bit_size = 4,
  137. bts_qual_max = (1 << bts_qual_bit_size),
  138. };
  139. struct bts_struct {
  140. __u64 qualifier;
  141. union {
  142. /* BTS_BRANCH */
  143. struct {
  144. __u64 from;
  145. __u64 to;
  146. } lbr;
  147. /* BTS_TASK_ARRIVES or BTS_TASK_DEPARTS */
  148. struct {
  149. __u64 jiffies;
  150. pid_t pid;
  151. } timestamp;
  152. } variant;
  153. };
  154. /*
  155. * The BTS state.
  156. *
  157. * This gives access to the raw DS state and adds functions to provide
  158. * an arch-independent view of the BTS data.
  159. */
  160. struct bts_trace {
  161. struct ds_trace ds;
  162. int (*read)(struct bts_tracer *tracer, const void *at,
  163. struct bts_struct *out);
  164. int (*write)(struct bts_tracer *tracer, const struct bts_struct *in);
  165. };
  166. /*
  167. * The PEBS state.
  168. *
  169. * This gives access to the raw DS state and the PEBS-specific counter
  170. * reset value.
  171. */
  172. struct pebs_trace {
  173. struct ds_trace ds;
  174. /* the PEBS reset value */
  175. unsigned long long reset_value;
  176. };
  177. /*
  178. * Read the BTS or PEBS trace.
  179. *
  180. * Returns a view on the trace collected for the parameter tracer.
  181. *
  182. * The view remains valid as long as the traced task is not running or
  183. * the tracer is suspended.
  184. * Writes into the trace buffer are not reflected.
  185. *
  186. * tracer: the tracer handle returned from ds_request_~()
  187. */
  188. extern const struct bts_trace *ds_read_bts(struct bts_tracer *tracer);
  189. extern const struct pebs_trace *ds_read_pebs(struct pebs_tracer *tracer);
  190. /*
  191. * Reset the write pointer of the BTS/PEBS buffer.
  192. *
  193. * Returns 0 on success; -Eerrno on error
  194. *
  195. * tracer: the tracer handle returned from ds_request_~()
  196. */
  197. extern int ds_reset_bts(struct bts_tracer *tracer);
  198. extern int ds_reset_pebs(struct pebs_tracer *tracer);
  199. /*
  200. * Set the PEBS counter reset value.
  201. *
  202. * Returns 0 on success; -Eerrno on error
  203. *
  204. * tracer: the tracer handle returned from ds_request_pebs()
  205. * value: the new counter reset value
  206. */
  207. extern int ds_set_pebs_reset(struct pebs_tracer *tracer, u64 value);
  208. /*
  209. * Initialization
  210. */
  211. struct cpuinfo_x86;
  212. extern void __cpuinit ds_init_intel(struct cpuinfo_x86 *);
  213. /*
  214. * Context switch work
  215. */
  216. extern void ds_switch_to(struct task_struct *prev, struct task_struct *next);
  217. /*
  218. * Task clone/init and cleanup work
  219. */
  220. extern void ds_copy_thread(struct task_struct *tsk, struct task_struct *father);
  221. extern void ds_exit_thread(struct task_struct *tsk);
  222. #else /* CONFIG_X86_DS */
  223. struct cpuinfo_x86;
  224. static inline void __cpuinit ds_init_intel(struct cpuinfo_x86 *ignored) {}
  225. static inline void ds_switch_to(struct task_struct *prev,
  226. struct task_struct *next) {}
  227. static inline void ds_copy_thread(struct task_struct *tsk,
  228. struct task_struct *father) {}
  229. static inline void ds_exit_thread(struct task_struct *tsk) {}
  230. #endif /* CONFIG_X86_DS */
  231. #endif /* _ASM_X86_DS_H */