intel-pt-decoder.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * intel_pt_decoder.h: Intel Processor Trace support
  4. * Copyright (c) 2013-2014, Intel Corporation.
  5. */
  6. #ifndef INCLUDE__INTEL_PT_DECODER_H__
  7. #define INCLUDE__INTEL_PT_DECODER_H__
  8. #include <stdint.h>
  9. #include <stddef.h>
  10. #include <stdbool.h>
  11. #include <linux/rbtree.h>
  12. #include "intel-pt-insn-decoder.h"
  13. #define INTEL_PT_IN_TX (1 << 0)
  14. #define INTEL_PT_ABORT_TX (1 << 1)
  15. #define INTEL_PT_IFLAG (1 << 2)
  16. #define INTEL_PT_ASYNC (1 << 2)
  17. #define INTEL_PT_FUP_IP (1 << 3)
  18. #define INTEL_PT_SAMPLE_IPC (1 << 4)
  19. enum intel_pt_sample_type {
  20. INTEL_PT_BRANCH = 1 << 0,
  21. INTEL_PT_INSTRUCTION = 1 << 1,
  22. INTEL_PT_TRANSACTION = 1 << 2,
  23. INTEL_PT_PTW = 1 << 3,
  24. INTEL_PT_MWAIT_OP = 1 << 4,
  25. INTEL_PT_PWR_ENTRY = 1 << 5,
  26. INTEL_PT_EX_STOP = 1 << 6,
  27. INTEL_PT_PWR_EXIT = 1 << 7,
  28. INTEL_PT_CBR_CHG = 1 << 8,
  29. INTEL_PT_TRACE_BEGIN = 1 << 9,
  30. INTEL_PT_TRACE_END = 1 << 10,
  31. INTEL_PT_BLK_ITEMS = 1 << 11,
  32. INTEL_PT_PSB_EVT = 1 << 12,
  33. INTEL_PT_EVT = 1 << 13,
  34. INTEL_PT_IFLAG_CHG = 1 << 14,
  35. };
  36. enum intel_pt_period_type {
  37. INTEL_PT_PERIOD_NONE,
  38. INTEL_PT_PERIOD_INSTRUCTIONS,
  39. INTEL_PT_PERIOD_TICKS,
  40. INTEL_PT_PERIOD_MTC,
  41. };
  42. enum {
  43. INTEL_PT_ERR_NOMEM = 1,
  44. INTEL_PT_ERR_INTERN,
  45. INTEL_PT_ERR_BADPKT,
  46. INTEL_PT_ERR_NODATA,
  47. INTEL_PT_ERR_NOINSN,
  48. INTEL_PT_ERR_MISMAT,
  49. INTEL_PT_ERR_OVR,
  50. INTEL_PT_ERR_LOST,
  51. INTEL_PT_ERR_UNK,
  52. INTEL_PT_ERR_NELOOP,
  53. INTEL_PT_ERR_EPTW,
  54. INTEL_PT_ERR_MAX,
  55. };
  56. enum intel_pt_param_flags {
  57. /*
  58. * FUP packet can contain next linear instruction pointer instead of
  59. * current linear instruction pointer.
  60. */
  61. INTEL_PT_FUP_WITH_NLIP = 1 << 0,
  62. };
  63. enum intel_pt_blk_type {
  64. INTEL_PT_GP_REGS = 1,
  65. INTEL_PT_PEBS_BASIC = 4,
  66. INTEL_PT_PEBS_MEM = 5,
  67. INTEL_PT_LBR_0 = 8,
  68. INTEL_PT_LBR_1 = 9,
  69. INTEL_PT_LBR_2 = 10,
  70. INTEL_PT_XMM = 16,
  71. INTEL_PT_BLK_TYPE_MAX
  72. };
  73. /*
  74. * The block type numbers are not sequential but here they are given sequential
  75. * positions to avoid wasting space for array placement.
  76. */
  77. enum intel_pt_blk_type_pos {
  78. INTEL_PT_GP_REGS_POS,
  79. INTEL_PT_PEBS_BASIC_POS,
  80. INTEL_PT_PEBS_MEM_POS,
  81. INTEL_PT_LBR_0_POS,
  82. INTEL_PT_LBR_1_POS,
  83. INTEL_PT_LBR_2_POS,
  84. INTEL_PT_XMM_POS,
  85. INTEL_PT_BLK_TYPE_CNT
  86. };
  87. /* Get the array position for a block type */
  88. static inline int intel_pt_blk_type_pos(enum intel_pt_blk_type blk_type)
  89. {
  90. #define BLK_TYPE(bt) [INTEL_PT_##bt] = INTEL_PT_##bt##_POS + 1
  91. const int map[INTEL_PT_BLK_TYPE_MAX] = {
  92. BLK_TYPE(GP_REGS),
  93. BLK_TYPE(PEBS_BASIC),
  94. BLK_TYPE(PEBS_MEM),
  95. BLK_TYPE(LBR_0),
  96. BLK_TYPE(LBR_1),
  97. BLK_TYPE(LBR_2),
  98. BLK_TYPE(XMM),
  99. };
  100. #undef BLK_TYPE
  101. return blk_type < INTEL_PT_BLK_TYPE_MAX ? map[blk_type] - 1 : -1;
  102. }
  103. #define INTEL_PT_BLK_ITEM_ID_CNT 32
  104. /*
  105. * Use unions so that the block items can be accessed by name or by array index.
  106. * There is an array of 32-bit masks for each block type, which indicate which
  107. * values are present. Then arrays of 32 64-bit values for each block type.
  108. */
  109. struct intel_pt_blk_items {
  110. union {
  111. uint32_t mask[INTEL_PT_BLK_TYPE_CNT];
  112. struct {
  113. uint32_t has_rflags:1;
  114. uint32_t has_rip:1;
  115. uint32_t has_rax:1;
  116. uint32_t has_rcx:1;
  117. uint32_t has_rdx:1;
  118. uint32_t has_rbx:1;
  119. uint32_t has_rsp:1;
  120. uint32_t has_rbp:1;
  121. uint32_t has_rsi:1;
  122. uint32_t has_rdi:1;
  123. uint32_t has_r8:1;
  124. uint32_t has_r9:1;
  125. uint32_t has_r10:1;
  126. uint32_t has_r11:1;
  127. uint32_t has_r12:1;
  128. uint32_t has_r13:1;
  129. uint32_t has_r14:1;
  130. uint32_t has_r15:1;
  131. uint32_t has_unused_0:14;
  132. uint32_t has_ip:1;
  133. uint32_t has_applicable_counters:1;
  134. uint32_t has_timestamp:1;
  135. uint32_t has_unused_1:29;
  136. uint32_t has_mem_access_address:1;
  137. uint32_t has_mem_aux_info:1;
  138. uint32_t has_mem_access_latency:1;
  139. uint32_t has_tsx_aux_info:1;
  140. uint32_t has_unused_2:28;
  141. uint32_t has_lbr_0;
  142. uint32_t has_lbr_1;
  143. uint32_t has_lbr_2;
  144. uint32_t has_xmm;
  145. };
  146. };
  147. union {
  148. uint64_t val[INTEL_PT_BLK_TYPE_CNT][INTEL_PT_BLK_ITEM_ID_CNT];
  149. struct {
  150. struct {
  151. uint64_t rflags;
  152. uint64_t rip;
  153. uint64_t rax;
  154. uint64_t rcx;
  155. uint64_t rdx;
  156. uint64_t rbx;
  157. uint64_t rsp;
  158. uint64_t rbp;
  159. uint64_t rsi;
  160. uint64_t rdi;
  161. uint64_t r8;
  162. uint64_t r9;
  163. uint64_t r10;
  164. uint64_t r11;
  165. uint64_t r12;
  166. uint64_t r13;
  167. uint64_t r14;
  168. uint64_t r15;
  169. uint64_t unused_0[INTEL_PT_BLK_ITEM_ID_CNT - 18];
  170. };
  171. struct {
  172. uint64_t ip;
  173. uint64_t applicable_counters;
  174. uint64_t timestamp;
  175. uint64_t unused_1[INTEL_PT_BLK_ITEM_ID_CNT - 3];
  176. };
  177. struct {
  178. uint64_t mem_access_address;
  179. uint64_t mem_aux_info;
  180. uint64_t mem_access_latency;
  181. uint64_t tsx_aux_info;
  182. uint64_t unused_2[INTEL_PT_BLK_ITEM_ID_CNT - 4];
  183. };
  184. uint64_t lbr_0[INTEL_PT_BLK_ITEM_ID_CNT];
  185. uint64_t lbr_1[INTEL_PT_BLK_ITEM_ID_CNT];
  186. uint64_t lbr_2[INTEL_PT_BLK_ITEM_ID_CNT];
  187. uint64_t xmm[INTEL_PT_BLK_ITEM_ID_CNT];
  188. };
  189. };
  190. bool is_32_bit;
  191. };
  192. struct intel_pt_vmcs_info {
  193. struct rb_node rb_node;
  194. uint64_t vmcs;
  195. uint64_t tsc_offset;
  196. bool reliable;
  197. bool error_printed;
  198. };
  199. /*
  200. * Maximum number of event trace data in one go, assuming at most 1 per type
  201. * and 6-bits of type in the EVD packet.
  202. */
  203. #define INTEL_PT_MAX_EVDS 64
  204. /* Event trace data from EVD packet */
  205. struct intel_pt_evd {
  206. int type;
  207. uint64_t payload;
  208. };
  209. struct intel_pt_state {
  210. enum intel_pt_sample_type type;
  211. bool from_nr;
  212. bool to_nr;
  213. bool from_iflag;
  214. bool to_iflag;
  215. int err;
  216. uint64_t from_ip;
  217. uint64_t to_ip;
  218. uint64_t tot_insn_cnt;
  219. uint64_t tot_cyc_cnt;
  220. uint64_t cycles;
  221. uint64_t timestamp;
  222. uint64_t est_timestamp;
  223. uint64_t trace_nr;
  224. uint64_t ptw_payload;
  225. uint64_t mwait_payload;
  226. uint64_t pwre_payload;
  227. uint64_t pwrx_payload;
  228. uint64_t cbr_payload;
  229. uint64_t psb_offset;
  230. uint32_t cbr;
  231. uint32_t flags;
  232. enum intel_pt_insn_op insn_op;
  233. int insn_len;
  234. char insn[INTEL_PT_INSN_BUF_SZ];
  235. struct intel_pt_blk_items items;
  236. int cfe_type;
  237. int cfe_vector;
  238. int evd_cnt;
  239. struct intel_pt_evd *evd;
  240. };
  241. struct intel_pt_insn;
  242. struct intel_pt_buffer {
  243. const unsigned char *buf;
  244. size_t len;
  245. bool consecutive;
  246. uint64_t ref_timestamp;
  247. uint64_t trace_nr;
  248. };
  249. typedef int (*intel_pt_lookahead_cb_t)(struct intel_pt_buffer *, void *);
  250. struct intel_pt_params {
  251. int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
  252. int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
  253. uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
  254. uint64_t max_insn_cnt, void *data);
  255. bool (*pgd_ip)(uint64_t ip, void *data);
  256. int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
  257. struct intel_pt_vmcs_info *(*findnew_vmcs_info)(void *data, uint64_t vmcs);
  258. void *data;
  259. bool return_compression;
  260. bool branch_enable;
  261. bool vm_time_correlation;
  262. bool vm_tm_corr_dry_run;
  263. uint64_t first_timestamp;
  264. uint64_t ctl;
  265. uint64_t period;
  266. enum intel_pt_period_type period_type;
  267. unsigned max_non_turbo_ratio;
  268. unsigned int mtc_period;
  269. uint32_t tsc_ctc_ratio_n;
  270. uint32_t tsc_ctc_ratio_d;
  271. enum intel_pt_param_flags flags;
  272. unsigned int quick;
  273. int max_loops;
  274. };
  275. struct intel_pt_decoder;
  276. struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params);
  277. void intel_pt_decoder_free(struct intel_pt_decoder *decoder);
  278. const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder);
  279. int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp);
  280. unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
  281. unsigned char *buf_b, size_t len_b,
  282. bool have_tsc, bool *consecutive,
  283. bool ooo_tsc);
  284. int intel_pt__strerror(int code, char *buf, size_t buflen);
  285. void intel_pt_set_first_timestamp(struct intel_pt_decoder *decoder,
  286. uint64_t first_timestamp);
  287. #endif