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