ds.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * - per-thread and per-cpu allocation of BTS and PEBS
  10. * - buffer overflow handling (to be done)
  11. * - buffer access
  12. *
  13. * It assumes:
  14. * - get_task_struct on all traced tasks
  15. * - current is allowed to trace tasks
  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_tracer;
  29. struct bts_tracer;
  30. struct pebs_tracer;
  31. typedef void (*bts_ovfl_callback_t)(struct bts_tracer *);
  32. typedef void (*pebs_ovfl_callback_t)(struct pebs_tracer *);
  33. /*
  34. * Request BTS or PEBS
  35. *
  36. * Due to alignement constraints, the actual buffer may be slightly
  37. * smaller than the requested or provided buffer.
  38. *
  39. * Returns a pointer to a tracer structure on success, or
  40. * ERR_PTR(errcode) on failure.
  41. *
  42. * The interrupt threshold is independent from the overflow callback
  43. * to allow users to use their own overflow interrupt handling mechanism.
  44. *
  45. * task: the task to request recording for;
  46. * NULL for per-cpu recording on the current cpu
  47. * base: the base pointer for the (non-pageable) buffer;
  48. * size: the size of the provided buffer in bytes
  49. * ovfl: pointer to a function to be called on buffer overflow;
  50. * NULL if cyclic buffer requested
  51. * th: the interrupt threshold in records from the end of the buffer;
  52. * -1 if no interrupt threshold is requested.
  53. */
  54. extern struct bts_tracer *ds_request_bts(struct task_struct *task,
  55. void *base, size_t size,
  56. bts_ovfl_callback_t ovfl, size_t th);
  57. extern struct pebs_tracer *ds_request_pebs(struct task_struct *task,
  58. void *base, size_t size,
  59. pebs_ovfl_callback_t ovfl,
  60. size_t th);
  61. /*
  62. * Release BTS or PEBS resources
  63. *
  64. * Returns 0 on success; -Eerrno otherwise
  65. *
  66. * tracer: the tracer handle returned from ds_request_~()
  67. */
  68. extern int ds_release_bts(struct bts_tracer *tracer);
  69. extern int ds_release_pebs(struct pebs_tracer *tracer);
  70. /*
  71. * Get the (array) index of the write pointer.
  72. * (assuming an array of BTS/PEBS records)
  73. *
  74. * Returns 0 on success; -Eerrno on error
  75. *
  76. * tracer: the tracer handle returned from ds_request_~()
  77. * pos (out): will hold the result
  78. */
  79. extern int ds_get_bts_index(struct bts_tracer *tracer, size_t *pos);
  80. extern int ds_get_pebs_index(struct pebs_tracer *tracer, size_t *pos);
  81. /*
  82. * Get the (array) index one record beyond the end of the array.
  83. * (assuming an array of BTS/PEBS records)
  84. *
  85. * Returns 0 on success; -Eerrno on error
  86. *
  87. * tracer: the tracer handle returned from ds_request_~()
  88. * pos (out): will hold the result
  89. */
  90. extern int ds_get_bts_end(struct bts_tracer *tracer, size_t *pos);
  91. extern int ds_get_pebs_end(struct pebs_tracer *tracer, size_t *pos);
  92. /*
  93. * Provide a pointer to the BTS/PEBS record at parameter index.
  94. * (assuming an array of BTS/PEBS records)
  95. *
  96. * The pointer points directly into the buffer. The user is
  97. * responsible for copying the record.
  98. *
  99. * Returns the size of a single record on success; -Eerrno on error
  100. *
  101. * tracer: the tracer handle returned from ds_request_~()
  102. * index: the index of the requested record
  103. * record (out): pointer to the requested record
  104. */
  105. extern int ds_access_bts(struct bts_tracer *tracer,
  106. size_t index, const void **record);
  107. extern int ds_access_pebs(struct pebs_tracer *tracer,
  108. size_t index, const void **record);
  109. /*
  110. * Write one or more BTS/PEBS records at the write pointer index and
  111. * advance the write pointer.
  112. *
  113. * If size is not a multiple of the record size, trailing bytes are
  114. * zeroed out.
  115. *
  116. * May result in one or more overflow notifications.
  117. *
  118. * If called during overflow handling, that is, with index >=
  119. * interrupt threshold, the write will wrap around.
  120. *
  121. * An overflow notification is given if and when the interrupt
  122. * threshold is reached during or after the write.
  123. *
  124. * Returns the number of bytes written or -Eerrno.
  125. *
  126. * tracer: the tracer handle returned from ds_request_~()
  127. * buffer: the buffer to write
  128. * size: the size of the buffer
  129. */
  130. extern int ds_write_bts(struct bts_tracer *tracer,
  131. const void *buffer, size_t size);
  132. extern int ds_write_pebs(struct pebs_tracer *tracer,
  133. const void *buffer, size_t size);
  134. /*
  135. * Reset the write pointer of the BTS/PEBS buffer.
  136. *
  137. * Returns 0 on success; -Eerrno on error
  138. *
  139. * tracer: the tracer handle returned from ds_request_~()
  140. */
  141. extern int ds_reset_bts(struct bts_tracer *tracer);
  142. extern int ds_reset_pebs(struct pebs_tracer *tracer);
  143. /*
  144. * Clear the BTS/PEBS buffer and reset the write pointer.
  145. * The entire buffer will be zeroed out.
  146. *
  147. * Returns 0 on success; -Eerrno on error
  148. *
  149. * tracer: the tracer handle returned from ds_request_~()
  150. */
  151. extern int ds_clear_bts(struct bts_tracer *tracer);
  152. extern int ds_clear_pebs(struct pebs_tracer *tracer);
  153. /*
  154. * Provide the PEBS counter reset value.
  155. *
  156. * Returns 0 on success; -Eerrno on error
  157. *
  158. * tracer: the tracer handle returned from ds_request_pebs()
  159. * value (out): the counter reset value
  160. */
  161. extern int ds_get_pebs_reset(struct pebs_tracer *tracer, u64 *value);
  162. /*
  163. * Set the PEBS counter reset value.
  164. *
  165. * Returns 0 on success; -Eerrno on error
  166. *
  167. * tracer: the tracer handle returned from ds_request_pebs()
  168. * value: the new counter reset value
  169. */
  170. extern int ds_set_pebs_reset(struct pebs_tracer *tracer, u64 value);
  171. /*
  172. * Initialization
  173. */
  174. struct cpuinfo_x86;
  175. extern void __cpuinit ds_init_intel(struct cpuinfo_x86 *);
  176. /*
  177. * The DS context - part of struct thread_struct.
  178. */
  179. #define MAX_SIZEOF_DS (12 * 8)
  180. struct ds_context {
  181. /* pointer to the DS configuration; goes into MSR_IA32_DS_AREA */
  182. unsigned char ds[MAX_SIZEOF_DS];
  183. /* the owner of the BTS and PEBS configuration, respectively */
  184. struct ds_tracer *owner[2];
  185. /* use count */
  186. unsigned long count;
  187. /* a pointer to the context location inside the thread_struct
  188. * or the per_cpu context array */
  189. struct ds_context **this;
  190. /* a pointer to the task owning this context, or NULL, if the
  191. * context is owned by a cpu */
  192. struct task_struct *task;
  193. };
  194. /* called by exit_thread() to free leftover contexts */
  195. extern void ds_free(struct ds_context *context);
  196. #else /* CONFIG_X86_DS */
  197. struct cpuinfo_x86;
  198. static inline void __cpuinit ds_init_intel(struct cpuinfo_x86 *ignored) {}
  199. #endif /* CONFIG_X86_DS */
  200. #endif /* _ASM_X86_DS_H */