xref: /openbmc/linux/tools/perf/util/intel-pt-decoder/intel-pt-decoder.h (revision f2b91386ffe66dba0860c03c9dec1c6b45c2daba)
12025cf9eSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2f4aa0819SAdrian Hunter /*
3f4aa0819SAdrian Hunter  * intel_pt_decoder.h: Intel Processor Trace support
4f4aa0819SAdrian Hunter  * Copyright (c) 2013-2014, Intel Corporation.
5f4aa0819SAdrian Hunter  */
6f4aa0819SAdrian Hunter 
7f4aa0819SAdrian Hunter #ifndef INCLUDE__INTEL_PT_DECODER_H__
8f4aa0819SAdrian Hunter #define INCLUDE__INTEL_PT_DECODER_H__
9f4aa0819SAdrian Hunter 
10f4aa0819SAdrian Hunter #include <stdint.h>
11f4aa0819SAdrian Hunter #include <stddef.h>
12f4aa0819SAdrian Hunter #include <stdbool.h>
13f4aa0819SAdrian Hunter 
140fc9d338SAdrian Hunter #include <linux/rbtree.h>
150fc9d338SAdrian Hunter 
16f4aa0819SAdrian Hunter #include "intel-pt-insn-decoder.h"
17f4aa0819SAdrian Hunter 
18f4aa0819SAdrian Hunter #define INTEL_PT_IN_TX		(1 << 0)
19f4aa0819SAdrian Hunter #define INTEL_PT_ABORT_TX	(1 << 1)
20f4aa0819SAdrian Hunter #define INTEL_PT_ASYNC		(1 << 2)
21a472e65fSAdrian Hunter #define INTEL_PT_FUP_IP		(1 << 3)
2220aa3970SAdrian Hunter #define INTEL_PT_SAMPLE_IPC	(1 << 4)
23f4aa0819SAdrian Hunter 
24f4aa0819SAdrian Hunter enum intel_pt_sample_type {
25f4aa0819SAdrian Hunter 	INTEL_PT_BRANCH		= 1 << 0,
26f4aa0819SAdrian Hunter 	INTEL_PT_INSTRUCTION	= 1 << 1,
27f4aa0819SAdrian Hunter 	INTEL_PT_TRANSACTION	= 1 << 2,
28a472e65fSAdrian Hunter 	INTEL_PT_PTW		= 1 << 3,
29a472e65fSAdrian Hunter 	INTEL_PT_MWAIT_OP	= 1 << 4,
30a472e65fSAdrian Hunter 	INTEL_PT_PWR_ENTRY	= 1 << 5,
31a472e65fSAdrian Hunter 	INTEL_PT_EX_STOP	= 1 << 6,
32a472e65fSAdrian Hunter 	INTEL_PT_PWR_EXIT	= 1 << 7,
330a7c700dSAdrian Hunter 	INTEL_PT_CBR_CHG	= 1 << 8,
34c6b5da09SAdrian Hunter 	INTEL_PT_TRACE_BEGIN	= 1 << 9,
35c6b5da09SAdrian Hunter 	INTEL_PT_TRACE_END	= 1 << 10,
364c35595eSAdrian Hunter 	INTEL_PT_BLK_ITEMS	= 1 << 11,
37c840cbfeSAdrian Hunter 	INTEL_PT_PSB_EVT	= 1 << 12,
38f4aa0819SAdrian Hunter };
39f4aa0819SAdrian Hunter 
40f4aa0819SAdrian Hunter enum intel_pt_period_type {
41f4aa0819SAdrian Hunter 	INTEL_PT_PERIOD_NONE,
42f4aa0819SAdrian Hunter 	INTEL_PT_PERIOD_INSTRUCTIONS,
43f4aa0819SAdrian Hunter 	INTEL_PT_PERIOD_TICKS,
4479b58424SAdrian Hunter 	INTEL_PT_PERIOD_MTC,
45f4aa0819SAdrian Hunter };
46f4aa0819SAdrian Hunter 
47f4aa0819SAdrian Hunter enum {
48f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NOMEM = 1,
49f4aa0819SAdrian Hunter 	INTEL_PT_ERR_INTERN,
50f4aa0819SAdrian Hunter 	INTEL_PT_ERR_BADPKT,
51f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NODATA,
52f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NOINSN,
53f4aa0819SAdrian Hunter 	INTEL_PT_ERR_MISMAT,
54f4aa0819SAdrian Hunter 	INTEL_PT_ERR_OVR,
55f4aa0819SAdrian Hunter 	INTEL_PT_ERR_LOST,
56f4aa0819SAdrian Hunter 	INTEL_PT_ERR_UNK,
57f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NELOOP,
58f4aa0819SAdrian Hunter 	INTEL_PT_ERR_MAX,
59f4aa0819SAdrian Hunter };
60f4aa0819SAdrian Hunter 
619fb52336SAdrian Hunter enum intel_pt_param_flags {
629fb52336SAdrian Hunter 	/*
639fb52336SAdrian Hunter 	 * FUP packet can contain next linear instruction pointer instead of
649fb52336SAdrian Hunter 	 * current linear instruction pointer.
659fb52336SAdrian Hunter 	 */
669fb52336SAdrian Hunter 	INTEL_PT_FUP_WITH_NLIP	= 1 << 0,
679fb52336SAdrian Hunter };
689fb52336SAdrian Hunter 
694c35595eSAdrian Hunter enum intel_pt_blk_type {
704c35595eSAdrian Hunter 	INTEL_PT_GP_REGS	= 1,
714c35595eSAdrian Hunter 	INTEL_PT_PEBS_BASIC	= 4,
724c35595eSAdrian Hunter 	INTEL_PT_PEBS_MEM	= 5,
734c35595eSAdrian Hunter 	INTEL_PT_LBR_0		= 8,
744c35595eSAdrian Hunter 	INTEL_PT_LBR_1		= 9,
754c35595eSAdrian Hunter 	INTEL_PT_LBR_2		= 10,
764c35595eSAdrian Hunter 	INTEL_PT_XMM		= 16,
774c35595eSAdrian Hunter 	INTEL_PT_BLK_TYPE_MAX
784c35595eSAdrian Hunter };
794c35595eSAdrian Hunter 
804c35595eSAdrian Hunter /*
814c35595eSAdrian Hunter  * The block type numbers are not sequential but here they are given sequential
824c35595eSAdrian Hunter  * positions to avoid wasting space for array placement.
834c35595eSAdrian Hunter  */
844c35595eSAdrian Hunter enum intel_pt_blk_type_pos {
854c35595eSAdrian Hunter 	INTEL_PT_GP_REGS_POS,
864c35595eSAdrian Hunter 	INTEL_PT_PEBS_BASIC_POS,
874c35595eSAdrian Hunter 	INTEL_PT_PEBS_MEM_POS,
884c35595eSAdrian Hunter 	INTEL_PT_LBR_0_POS,
894c35595eSAdrian Hunter 	INTEL_PT_LBR_1_POS,
904c35595eSAdrian Hunter 	INTEL_PT_LBR_2_POS,
914c35595eSAdrian Hunter 	INTEL_PT_XMM_POS,
924c35595eSAdrian Hunter 	INTEL_PT_BLK_TYPE_CNT
934c35595eSAdrian Hunter };
944c35595eSAdrian Hunter 
954c35595eSAdrian Hunter /* Get the array position for a block type */
964c35595eSAdrian Hunter static inline int intel_pt_blk_type_pos(enum intel_pt_blk_type blk_type)
974c35595eSAdrian Hunter {
984c35595eSAdrian Hunter #define BLK_TYPE(bt) [INTEL_PT_##bt] = INTEL_PT_##bt##_POS + 1
994c35595eSAdrian Hunter 	const int map[INTEL_PT_BLK_TYPE_MAX] = {
1004c35595eSAdrian Hunter 		BLK_TYPE(GP_REGS),
1014c35595eSAdrian Hunter 		BLK_TYPE(PEBS_BASIC),
1024c35595eSAdrian Hunter 		BLK_TYPE(PEBS_MEM),
1034c35595eSAdrian Hunter 		BLK_TYPE(LBR_0),
1044c35595eSAdrian Hunter 		BLK_TYPE(LBR_1),
1054c35595eSAdrian Hunter 		BLK_TYPE(LBR_2),
1064c35595eSAdrian Hunter 		BLK_TYPE(XMM),
1074c35595eSAdrian Hunter 	};
1084c35595eSAdrian Hunter #undef BLK_TYPE
1094c35595eSAdrian Hunter 
1104c35595eSAdrian Hunter 	return blk_type < INTEL_PT_BLK_TYPE_MAX ? map[blk_type] - 1 : -1;
1114c35595eSAdrian Hunter }
1124c35595eSAdrian Hunter 
1134c35595eSAdrian Hunter #define INTEL_PT_BLK_ITEM_ID_CNT	32
1144c35595eSAdrian Hunter 
1154c35595eSAdrian Hunter /*
1164c35595eSAdrian Hunter  * Use unions so that the block items can be accessed by name or by array index.
1174c35595eSAdrian Hunter  * There is an array of 32-bit masks for each block type, which indicate which
1184c35595eSAdrian Hunter  * values are present. Then arrays of 32 64-bit values for each block type.
1194c35595eSAdrian Hunter  */
1204c35595eSAdrian Hunter struct intel_pt_blk_items {
1214c35595eSAdrian Hunter 	union {
1224c35595eSAdrian Hunter 		uint32_t mask[INTEL_PT_BLK_TYPE_CNT];
1234c35595eSAdrian Hunter 		struct {
1244c35595eSAdrian Hunter 			uint32_t has_rflags:1;
1254c35595eSAdrian Hunter 			uint32_t has_rip:1;
1264c35595eSAdrian Hunter 			uint32_t has_rax:1;
1274c35595eSAdrian Hunter 			uint32_t has_rcx:1;
1284c35595eSAdrian Hunter 			uint32_t has_rdx:1;
1294c35595eSAdrian Hunter 			uint32_t has_rbx:1;
1304c35595eSAdrian Hunter 			uint32_t has_rsp:1;
1314c35595eSAdrian Hunter 			uint32_t has_rbp:1;
1324c35595eSAdrian Hunter 			uint32_t has_rsi:1;
1334c35595eSAdrian Hunter 			uint32_t has_rdi:1;
1344c35595eSAdrian Hunter 			uint32_t has_r8:1;
1354c35595eSAdrian Hunter 			uint32_t has_r9:1;
1364c35595eSAdrian Hunter 			uint32_t has_r10:1;
1374c35595eSAdrian Hunter 			uint32_t has_r11:1;
1384c35595eSAdrian Hunter 			uint32_t has_r12:1;
1394c35595eSAdrian Hunter 			uint32_t has_r13:1;
1404c35595eSAdrian Hunter 			uint32_t has_r14:1;
1414c35595eSAdrian Hunter 			uint32_t has_r15:1;
1424c35595eSAdrian Hunter 			uint32_t has_unused_0:14;
1434c35595eSAdrian Hunter 			uint32_t has_ip:1;
1444c35595eSAdrian Hunter 			uint32_t has_applicable_counters:1;
1454c35595eSAdrian Hunter 			uint32_t has_timestamp:1;
1464c35595eSAdrian Hunter 			uint32_t has_unused_1:29;
1474c35595eSAdrian Hunter 			uint32_t has_mem_access_address:1;
1484c35595eSAdrian Hunter 			uint32_t has_mem_aux_info:1;
1494c35595eSAdrian Hunter 			uint32_t has_mem_access_latency:1;
1504c35595eSAdrian Hunter 			uint32_t has_tsx_aux_info:1;
1514c35595eSAdrian Hunter 			uint32_t has_unused_2:28;
1524c35595eSAdrian Hunter 			uint32_t has_lbr_0;
1534c35595eSAdrian Hunter 			uint32_t has_lbr_1;
1544c35595eSAdrian Hunter 			uint32_t has_lbr_2;
1554c35595eSAdrian Hunter 			uint32_t has_xmm;
1564c35595eSAdrian Hunter 		};
1574c35595eSAdrian Hunter 	};
1584c35595eSAdrian Hunter 	union {
1594c35595eSAdrian Hunter 		uint64_t val[INTEL_PT_BLK_TYPE_CNT][INTEL_PT_BLK_ITEM_ID_CNT];
1604c35595eSAdrian Hunter 		struct {
1614c35595eSAdrian Hunter 			struct {
1624c35595eSAdrian Hunter 				uint64_t rflags;
1634c35595eSAdrian Hunter 				uint64_t rip;
1644c35595eSAdrian Hunter 				uint64_t rax;
1654c35595eSAdrian Hunter 				uint64_t rcx;
1664c35595eSAdrian Hunter 				uint64_t rdx;
1674c35595eSAdrian Hunter 				uint64_t rbx;
1684c35595eSAdrian Hunter 				uint64_t rsp;
1694c35595eSAdrian Hunter 				uint64_t rbp;
1704c35595eSAdrian Hunter 				uint64_t rsi;
1714c35595eSAdrian Hunter 				uint64_t rdi;
1724c35595eSAdrian Hunter 				uint64_t r8;
1734c35595eSAdrian Hunter 				uint64_t r9;
1744c35595eSAdrian Hunter 				uint64_t r10;
1754c35595eSAdrian Hunter 				uint64_t r11;
1764c35595eSAdrian Hunter 				uint64_t r12;
1774c35595eSAdrian Hunter 				uint64_t r13;
1784c35595eSAdrian Hunter 				uint64_t r14;
1794c35595eSAdrian Hunter 				uint64_t r15;
1804c35595eSAdrian Hunter 				uint64_t unused_0[INTEL_PT_BLK_ITEM_ID_CNT - 18];
1814c35595eSAdrian Hunter 			};
1824c35595eSAdrian Hunter 			struct {
1834c35595eSAdrian Hunter 				uint64_t ip;
1844c35595eSAdrian Hunter 				uint64_t applicable_counters;
1854c35595eSAdrian Hunter 				uint64_t timestamp;
1864c35595eSAdrian Hunter 				uint64_t unused_1[INTEL_PT_BLK_ITEM_ID_CNT - 3];
1874c35595eSAdrian Hunter 			};
1884c35595eSAdrian Hunter 			struct {
1894c35595eSAdrian Hunter 				uint64_t mem_access_address;
1904c35595eSAdrian Hunter 				uint64_t mem_aux_info;
1914c35595eSAdrian Hunter 				uint64_t mem_access_latency;
1924c35595eSAdrian Hunter 				uint64_t tsx_aux_info;
1934c35595eSAdrian Hunter 				uint64_t unused_2[INTEL_PT_BLK_ITEM_ID_CNT - 4];
1944c35595eSAdrian Hunter 			};
1954c35595eSAdrian Hunter 			uint64_t lbr_0[INTEL_PT_BLK_ITEM_ID_CNT];
1964c35595eSAdrian Hunter 			uint64_t lbr_1[INTEL_PT_BLK_ITEM_ID_CNT];
1974c35595eSAdrian Hunter 			uint64_t lbr_2[INTEL_PT_BLK_ITEM_ID_CNT];
1984c35595eSAdrian Hunter 			uint64_t xmm[INTEL_PT_BLK_ITEM_ID_CNT];
1994c35595eSAdrian Hunter 		};
2004c35595eSAdrian Hunter 	};
2014c35595eSAdrian Hunter 	bool is_32_bit;
2024c35595eSAdrian Hunter };
2034c35595eSAdrian Hunter 
2040fc9d338SAdrian Hunter struct intel_pt_vmcs_info {
2050fc9d338SAdrian Hunter 	struct rb_node rb_node;
2060fc9d338SAdrian Hunter 	uint64_t vmcs;
2070fc9d338SAdrian Hunter 	uint64_t tsc_offset;
2080fc9d338SAdrian Hunter 	bool reliable;
2090fc9d338SAdrian Hunter 	bool error_printed;
2100fc9d338SAdrian Hunter };
2110fc9d338SAdrian Hunter 
212f4aa0819SAdrian Hunter struct intel_pt_state {
213f4aa0819SAdrian Hunter 	enum intel_pt_sample_type type;
21480a03886SAdrian Hunter 	bool from_nr;
21580a03886SAdrian Hunter 	bool to_nr;
216f4aa0819SAdrian Hunter 	int err;
217f4aa0819SAdrian Hunter 	uint64_t from_ip;
218f4aa0819SAdrian Hunter 	uint64_t to_ip;
2192a21d036SAdrian Hunter 	uint64_t tot_insn_cnt;
2207b4b4f83SAdrian Hunter 	uint64_t tot_cyc_cnt;
221*f2b91386SAdrian Hunter 	uint64_t cycles;
222f4aa0819SAdrian Hunter 	uint64_t timestamp;
223f4aa0819SAdrian Hunter 	uint64_t est_timestamp;
224f4aa0819SAdrian Hunter 	uint64_t trace_nr;
225a472e65fSAdrian Hunter 	uint64_t ptw_payload;
226a472e65fSAdrian Hunter 	uint64_t mwait_payload;
227a472e65fSAdrian Hunter 	uint64_t pwre_payload;
228a472e65fSAdrian Hunter 	uint64_t pwrx_payload;
2290a7c700dSAdrian Hunter 	uint64_t cbr_payload;
230c840cbfeSAdrian Hunter 	uint64_t psb_offset;
23151b09186SAdrian Hunter 	uint32_t cbr;
232f4aa0819SAdrian Hunter 	uint32_t flags;
233f4aa0819SAdrian Hunter 	enum intel_pt_insn_op insn_op;
234f4aa0819SAdrian Hunter 	int insn_len;
235faaa8768SAndi Kleen 	char insn[INTEL_PT_INSN_BUF_SZ];
2364c35595eSAdrian Hunter 	struct intel_pt_blk_items items;
237f4aa0819SAdrian Hunter };
238f4aa0819SAdrian Hunter 
239f4aa0819SAdrian Hunter struct intel_pt_insn;
240f4aa0819SAdrian Hunter 
241f4aa0819SAdrian Hunter struct intel_pt_buffer {
242f4aa0819SAdrian Hunter 	const unsigned char *buf;
243f4aa0819SAdrian Hunter 	size_t len;
244f4aa0819SAdrian Hunter 	bool consecutive;
245f4aa0819SAdrian Hunter 	uint64_t ref_timestamp;
246f4aa0819SAdrian Hunter 	uint64_t trace_nr;
247f4aa0819SAdrian Hunter };
248f4aa0819SAdrian Hunter 
2494d678e90SAdrian Hunter typedef int (*intel_pt_lookahead_cb_t)(struct intel_pt_buffer *, void *);
2504d678e90SAdrian Hunter 
251f4aa0819SAdrian Hunter struct intel_pt_params {
252f4aa0819SAdrian Hunter 	int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
253f4aa0819SAdrian Hunter 	int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
254f4aa0819SAdrian Hunter 			 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
255f4aa0819SAdrian Hunter 			 uint64_t max_insn_cnt, void *data);
2569f1d122bSAdrian Hunter 	bool (*pgd_ip)(uint64_t ip, void *data);
2574d678e90SAdrian Hunter 	int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
2580fc9d338SAdrian Hunter 	struct intel_pt_vmcs_info *(*findnew_vmcs_info)(void *data, uint64_t vmcs);
259f4aa0819SAdrian Hunter 	void *data;
260f4aa0819SAdrian Hunter 	bool return_compression;
26183959817SAdrian Hunter 	bool branch_enable;
262fa8f949dSAdrian Hunter 	bool vm_time_correlation;
263fa8f949dSAdrian Hunter 	bool vm_tm_corr_dry_run;
2645ac35d77SAdrian Hunter 	uint64_t first_timestamp;
2656af4b600SAdrian Hunter 	uint64_t ctl;
266f4aa0819SAdrian Hunter 	uint64_t period;
267f4aa0819SAdrian Hunter 	enum intel_pt_period_type period_type;
268f4aa0819SAdrian Hunter 	unsigned max_non_turbo_ratio;
26911fa7cb8SAdrian Hunter 	unsigned int mtc_period;
27011fa7cb8SAdrian Hunter 	uint32_t tsc_ctc_ratio_n;
27111fa7cb8SAdrian Hunter 	uint32_t tsc_ctc_ratio_d;
2729fb52336SAdrian Hunter 	enum intel_pt_param_flags flags;
2737c1b16baSAdrian Hunter 	unsigned int quick;
274b4b046ffSAdrian Hunter 	int max_loops;
275f4aa0819SAdrian Hunter };
276f4aa0819SAdrian Hunter 
277f4aa0819SAdrian Hunter struct intel_pt_decoder;
278f4aa0819SAdrian Hunter 
279f4aa0819SAdrian Hunter struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params);
280f4aa0819SAdrian Hunter void intel_pt_decoder_free(struct intel_pt_decoder *decoder);
281f4aa0819SAdrian Hunter 
282f4aa0819SAdrian Hunter const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder);
283f4aa0819SAdrian Hunter 
284a7fa19f5SAdrian Hunter int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp);
285a7fa19f5SAdrian Hunter 
286f4aa0819SAdrian Hunter unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
287f4aa0819SAdrian Hunter 				     unsigned char *buf_b, size_t len_b,
288335358ccSAdrian Hunter 				     bool have_tsc, bool *consecutive,
289335358ccSAdrian Hunter 				     bool ooo_tsc);
290f4aa0819SAdrian Hunter 
291f4aa0819SAdrian Hunter int intel_pt__strerror(int code, char *buf, size_t buflen);
292f4aa0819SAdrian Hunter 
2935ac35d77SAdrian Hunter void intel_pt_set_first_timestamp(struct intel_pt_decoder *decoder,
2945ac35d77SAdrian Hunter 				  uint64_t first_timestamp);
2955ac35d77SAdrian Hunter 
296f4aa0819SAdrian Hunter #endif
297