1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * intel_pt_decoder.h: Intel Processor Trace support
4 * Copyright (c) 2013-2014, Intel Corporation.
7 #ifndef INCLUDE__INTEL_PT_DECODER_H__
8 #define INCLUDE__INTEL_PT_DECODER_H__
14 #include "intel-pt-insn-decoder.h"
16 #define INTEL_PT_IN_TX (1 << 0)
17 #define INTEL_PT_ABORT_TX (1 << 1)
18 #define INTEL_PT_ASYNC (1 << 2)
19 #define INTEL_PT_FUP_IP (1 << 3)
21 enum intel_pt_sample_type
{
22 INTEL_PT_BRANCH
= 1 << 0,
23 INTEL_PT_INSTRUCTION
= 1 << 1,
24 INTEL_PT_TRANSACTION
= 1 << 2,
25 INTEL_PT_PTW
= 1 << 3,
26 INTEL_PT_MWAIT_OP
= 1 << 4,
27 INTEL_PT_PWR_ENTRY
= 1 << 5,
28 INTEL_PT_EX_STOP
= 1 << 6,
29 INTEL_PT_PWR_EXIT
= 1 << 7,
30 INTEL_PT_CBR_CHG
= 1 << 8,
31 INTEL_PT_TRACE_BEGIN
= 1 << 9,
32 INTEL_PT_TRACE_END
= 1 << 10,
33 INTEL_PT_BLK_ITEMS
= 1 << 11,
36 enum intel_pt_period_type
{
38 INTEL_PT_PERIOD_INSTRUCTIONS
,
39 INTEL_PT_PERIOD_TICKS
,
44 INTEL_PT_ERR_NOMEM
= 1,
57 enum intel_pt_param_flags
{
59 * FUP packet can contain next linear instruction pointer instead of
60 * current linear instruction pointer.
62 INTEL_PT_FUP_WITH_NLIP
= 1 << 0,
65 enum intel_pt_blk_type
{
67 INTEL_PT_PEBS_BASIC
= 4,
68 INTEL_PT_PEBS_MEM
= 5,
77 * The block type numbers are not sequential but here they are given sequential
78 * positions to avoid wasting space for array placement.
80 enum intel_pt_blk_type_pos
{
82 INTEL_PT_PEBS_BASIC_POS
,
83 INTEL_PT_PEBS_MEM_POS
,
91 /* Get the array position for a block type */
92 static inline int intel_pt_blk_type_pos(enum intel_pt_blk_type blk_type
)
94 #define BLK_TYPE(bt) [INTEL_PT_##bt] = INTEL_PT_##bt##_POS + 1
95 const int map
[INTEL_PT_BLK_TYPE_MAX
] = {
106 return blk_type
< INTEL_PT_BLK_TYPE_MAX
? map
[blk_type
] - 1 : -1;
109 #define INTEL_PT_BLK_ITEM_ID_CNT 32
112 * Use unions so that the block items can be accessed by name or by array index.
113 * There is an array of 32-bit masks for each block type, which indicate which
114 * values are present. Then arrays of 32 64-bit values for each block type.
116 struct intel_pt_blk_items
{
118 uint32_t mask
[INTEL_PT_BLK_TYPE_CNT
];
120 uint32_t has_rflags
:1;
138 uint32_t has_unused_0
:14;
140 uint32_t has_applicable_counters
:1;
141 uint32_t has_timestamp
:1;
142 uint32_t has_unused_1
:29;
143 uint32_t has_mem_access_address
:1;
144 uint32_t has_mem_aux_info
:1;
145 uint32_t has_mem_access_latency
:1;
146 uint32_t has_tsx_aux_info
:1;
147 uint32_t has_unused_2
:28;
155 uint64_t val
[INTEL_PT_BLK_TYPE_CNT
][INTEL_PT_BLK_ITEM_ID_CNT
];
176 uint64_t unused_0
[INTEL_PT_BLK_ITEM_ID_CNT
- 18];
180 uint64_t applicable_counters
;
182 uint64_t unused_1
[INTEL_PT_BLK_ITEM_ID_CNT
- 3];
185 uint64_t mem_access_address
;
186 uint64_t mem_aux_info
;
187 uint64_t mem_access_latency
;
188 uint64_t tsx_aux_info
;
189 uint64_t unused_2
[INTEL_PT_BLK_ITEM_ID_CNT
- 4];
191 uint64_t lbr_0
[INTEL_PT_BLK_ITEM_ID_CNT
];
192 uint64_t lbr_1
[INTEL_PT_BLK_ITEM_ID_CNT
];
193 uint64_t lbr_2
[INTEL_PT_BLK_ITEM_ID_CNT
];
194 uint64_t xmm
[INTEL_PT_BLK_ITEM_ID_CNT
];
200 struct intel_pt_state
{
201 enum intel_pt_sample_type type
;
206 uint64_t tot_insn_cnt
;
207 uint64_t tot_cyc_cnt
;
209 uint64_t est_timestamp
;
211 uint64_t ptw_payload
;
212 uint64_t mwait_payload
;
213 uint64_t pwre_payload
;
214 uint64_t pwrx_payload
;
215 uint64_t cbr_payload
;
218 enum intel_pt_insn_op insn_op
;
220 char insn
[INTEL_PT_INSN_BUF_SZ
];
221 struct intel_pt_blk_items items
;
224 struct intel_pt_insn
;
226 struct intel_pt_buffer
{
227 const unsigned char *buf
;
230 uint64_t ref_timestamp
;
234 typedef int (*intel_pt_lookahead_cb_t
)(struct intel_pt_buffer
*, void *);
236 struct intel_pt_params
{
237 int (*get_trace
)(struct intel_pt_buffer
*buffer
, void *data
);
238 int (*walk_insn
)(struct intel_pt_insn
*intel_pt_insn
,
239 uint64_t *insn_cnt_ptr
, uint64_t *ip
, uint64_t to_ip
,
240 uint64_t max_insn_cnt
, void *data
);
241 bool (*pgd_ip
)(uint64_t ip
, void *data
);
242 int (*lookahead
)(void *data
, intel_pt_lookahead_cb_t cb
, void *cb_data
);
244 bool return_compression
;
247 enum intel_pt_period_type period_type
;
248 unsigned max_non_turbo_ratio
;
249 unsigned int mtc_period
;
250 uint32_t tsc_ctc_ratio_n
;
251 uint32_t tsc_ctc_ratio_d
;
252 enum intel_pt_param_flags flags
;
256 struct intel_pt_decoder
;
258 struct intel_pt_decoder
*intel_pt_decoder_new(struct intel_pt_params
*params
);
259 void intel_pt_decoder_free(struct intel_pt_decoder
*decoder
);
261 const struct intel_pt_state
*intel_pt_decode(struct intel_pt_decoder
*decoder
);
263 int intel_pt_fast_forward(struct intel_pt_decoder
*decoder
, uint64_t timestamp
);
265 unsigned char *intel_pt_find_overlap(unsigned char *buf_a
, size_t len_a
,
266 unsigned char *buf_b
, size_t len_b
,
267 bool have_tsc
, bool *consecutive
);
269 int intel_pt__strerror(int code
, char *buf
, size_t buflen
);