ds.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 memory allocation (optional)
  11. * - buffer overflow handling
  12. * - buffer access
  13. *
  14. * It assumes:
  15. * - get_task_struct on all parameter tasks
  16. * - current is allowed to trace parameter tasks
  17. *
  18. *
  19. * Copyright (C) 2007-2008 Intel Corporation.
  20. * Markus Metzger <markus.t.metzger@intel.com>, 2007-2008
  21. */
  22. #ifndef ASM_X86__DS_H
  23. #define ASM_X86__DS_H
  24. #ifdef CONFIG_X86_DS
  25. #include <linux/types.h>
  26. #include <linux/init.h>
  27. struct task_struct;
  28. /*
  29. * Request BTS or PEBS
  30. *
  31. * Due to alignement constraints, the actual buffer may be slightly
  32. * smaller than the requested or provided buffer.
  33. *
  34. * Returns 0 on success; -Eerrno otherwise
  35. *
  36. * task: the task to request recording for;
  37. * NULL for per-cpu recording on the current cpu
  38. * base: the base pointer for the (non-pageable) buffer;
  39. * NULL if buffer allocation requested
  40. * size: the size of the requested or provided buffer
  41. * ovfl: pointer to a function to be called on buffer overflow;
  42. * NULL if cyclic buffer requested
  43. */
  44. typedef void (*ds_ovfl_callback_t)(struct task_struct *);
  45. extern int ds_request_bts(struct task_struct *task, void *base, size_t size,
  46. ds_ovfl_callback_t ovfl);
  47. extern int ds_request_pebs(struct task_struct *task, void *base, size_t size,
  48. ds_ovfl_callback_t ovfl);
  49. /*
  50. * Release BTS or PEBS resources
  51. *
  52. * Frees buffers allocated on ds_request.
  53. *
  54. * Returns 0 on success; -Eerrno otherwise
  55. *
  56. * task: the task to release resources for;
  57. * NULL to release resources for the current cpu
  58. */
  59. extern int ds_release_bts(struct task_struct *task);
  60. extern int ds_release_pebs(struct task_struct *task);
  61. /*
  62. * Return the (array) index of the write pointer.
  63. * (assuming an array of BTS/PEBS records)
  64. *
  65. * Returns -Eerrno on error
  66. *
  67. * task: the task to access;
  68. * NULL to access the current cpu
  69. * pos (out): if not NULL, will hold the result
  70. */
  71. extern int ds_get_bts_index(struct task_struct *task, size_t *pos);
  72. extern int ds_get_pebs_index(struct task_struct *task, size_t *pos);
  73. /*
  74. * Return the (array) index one record beyond the end of the array.
  75. * (assuming an array of BTS/PEBS records)
  76. *
  77. * Returns -Eerrno on error
  78. *
  79. * task: the task to access;
  80. * NULL to access the current cpu
  81. * pos (out): if not NULL, will hold the result
  82. */
  83. extern int ds_get_bts_end(struct task_struct *task, size_t *pos);
  84. extern int ds_get_pebs_end(struct task_struct *task, size_t *pos);
  85. /*
  86. * Provide a pointer to the BTS/PEBS record at parameter index.
  87. * (assuming an array of BTS/PEBS records)
  88. *
  89. * The pointer points directly into the buffer. The user is
  90. * responsible for copying the record.
  91. *
  92. * Returns the size of a single record on success; -Eerrno on error
  93. *
  94. * task: the task to access;
  95. * NULL to access the current cpu
  96. * index: the index of the requested record
  97. * record (out): pointer to the requested record
  98. */
  99. extern int ds_access_bts(struct task_struct *task,
  100. size_t index, const void **record);
  101. extern int ds_access_pebs(struct task_struct *task,
  102. size_t index, const void **record);
  103. /*
  104. * Write one or more BTS/PEBS records at the write pointer index and
  105. * advance the write pointer.
  106. *
  107. * If size is not a multiple of the record size, trailing bytes are
  108. * zeroed out.
  109. *
  110. * May result in one or more overflow notifications.
  111. *
  112. * If called during overflow handling, that is, with index >=
  113. * interrupt threshold, the write will wrap around.
  114. *
  115. * An overflow notification is given if and when the interrupt
  116. * threshold is reached during or after the write.
  117. *
  118. * Returns the number of bytes written or -Eerrno.
  119. *
  120. * task: the task to access;
  121. * NULL to access the current cpu
  122. * buffer: the buffer to write
  123. * size: the size of the buffer
  124. */
  125. extern int ds_write_bts(struct task_struct *task,
  126. const void *buffer, size_t size);
  127. extern int ds_write_pebs(struct task_struct *task,
  128. const void *buffer, size_t size);
  129. /*
  130. * Same as ds_write_bts/pebs, but omit ownership checks.
  131. *
  132. * This is needed to have some other task than the owner of the
  133. * BTS/PEBS buffer or the parameter task itself write into the
  134. * respective buffer.
  135. */
  136. extern int ds_unchecked_write_bts(struct task_struct *task,
  137. const void *buffer, size_t size);
  138. extern int ds_unchecked_write_pebs(struct task_struct *task,
  139. const void *buffer, size_t size);
  140. /*
  141. * Reset the write pointer of the BTS/PEBS buffer.
  142. *
  143. * Returns 0 on success; -Eerrno on error
  144. *
  145. * task: the task to access;
  146. * NULL to access the current cpu
  147. */
  148. extern int ds_reset_bts(struct task_struct *task);
  149. extern int ds_reset_pebs(struct task_struct *task);
  150. /*
  151. * Clear the BTS/PEBS buffer and reset the write pointer.
  152. * The entire buffer will be zeroed out.
  153. *
  154. * Returns 0 on success; -Eerrno on error
  155. *
  156. * task: the task to access;
  157. * NULL to access the current cpu
  158. */
  159. extern int ds_clear_bts(struct task_struct *task);
  160. extern int ds_clear_pebs(struct task_struct *task);
  161. /*
  162. * Provide the PEBS counter reset value.
  163. *
  164. * Returns 0 on success; -Eerrno on error
  165. *
  166. * task: the task to access;
  167. * NULL to access the current cpu
  168. * value (out): the counter reset value
  169. */
  170. extern int ds_get_pebs_reset(struct task_struct *task, u64 *value);
  171. /*
  172. * Set the PEBS counter reset value.
  173. *
  174. * Returns 0 on success; -Eerrno on error
  175. *
  176. * task: the task to access;
  177. * NULL to access the current cpu
  178. * value: the new counter reset value
  179. */
  180. extern int ds_set_pebs_reset(struct task_struct *task, u64 value);
  181. /*
  182. * Initialization
  183. */
  184. struct cpuinfo_x86;
  185. extern void __cpuinit ds_init_intel(struct cpuinfo_x86 *);
  186. /*
  187. * The DS context - part of struct thread_struct.
  188. */
  189. struct ds_context {
  190. /* pointer to the DS configuration; goes into MSR_IA32_DS_AREA */
  191. unsigned char *ds;
  192. /* the owner of the BTS and PEBS configuration, respectively */
  193. struct task_struct *owner[2];
  194. /* buffer overflow notification function for BTS and PEBS */
  195. ds_ovfl_callback_t callback[2];
  196. /* the original buffer address */
  197. void *buffer[2];
  198. /* the number of allocated pages for on-request allocated buffers */
  199. unsigned int pages[2];
  200. /* use count */
  201. unsigned long count;
  202. /* a pointer to the context location inside the thread_struct
  203. * or the per_cpu context array */
  204. struct ds_context **this;
  205. /* a pointer to the task owning this context, or NULL, if the
  206. * context is owned by a cpu */
  207. struct task_struct *task;
  208. };
  209. /* called by exit_thread() to free leftover contexts */
  210. extern void ds_free(struct ds_context *context);
  211. #else /* CONFIG_X86_DS */
  212. #define ds_init_intel(config) do {} while (0)
  213. #endif /* CONFIG_X86_DS */
  214. #endif /* ASM_X86__DS_H */