xref: /openbmc/linux/tools/perf/util/intel-pt-decoder/intel-pt-decoder.h (revision c840cbfeffcbf2a0fa3856e2ed5ea5c622b9e6b2)
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 
14f4aa0819SAdrian Hunter #include "intel-pt-insn-decoder.h"
15f4aa0819SAdrian Hunter 
16f4aa0819SAdrian Hunter #define INTEL_PT_IN_TX		(1 << 0)
17f4aa0819SAdrian Hunter #define INTEL_PT_ABORT_TX	(1 << 1)
18f4aa0819SAdrian Hunter #define INTEL_PT_ASYNC		(1 << 2)
19a472e65fSAdrian Hunter #define INTEL_PT_FUP_IP		(1 << 3)
2020aa3970SAdrian Hunter #define INTEL_PT_SAMPLE_IPC	(1 << 4)
21f4aa0819SAdrian Hunter 
22f4aa0819SAdrian Hunter enum intel_pt_sample_type {
23f4aa0819SAdrian Hunter 	INTEL_PT_BRANCH		= 1 << 0,
24f4aa0819SAdrian Hunter 	INTEL_PT_INSTRUCTION	= 1 << 1,
25f4aa0819SAdrian Hunter 	INTEL_PT_TRANSACTION	= 1 << 2,
26a472e65fSAdrian Hunter 	INTEL_PT_PTW		= 1 << 3,
27a472e65fSAdrian Hunter 	INTEL_PT_MWAIT_OP	= 1 << 4,
28a472e65fSAdrian Hunter 	INTEL_PT_PWR_ENTRY	= 1 << 5,
29a472e65fSAdrian Hunter 	INTEL_PT_EX_STOP	= 1 << 6,
30a472e65fSAdrian Hunter 	INTEL_PT_PWR_EXIT	= 1 << 7,
310a7c700dSAdrian Hunter 	INTEL_PT_CBR_CHG	= 1 << 8,
32c6b5da09SAdrian Hunter 	INTEL_PT_TRACE_BEGIN	= 1 << 9,
33c6b5da09SAdrian Hunter 	INTEL_PT_TRACE_END	= 1 << 10,
344c35595eSAdrian Hunter 	INTEL_PT_BLK_ITEMS	= 1 << 11,
35*c840cbfeSAdrian Hunter 	INTEL_PT_PSB_EVT	= 1 << 12,
36f4aa0819SAdrian Hunter };
37f4aa0819SAdrian Hunter 
38f4aa0819SAdrian Hunter enum intel_pt_period_type {
39f4aa0819SAdrian Hunter 	INTEL_PT_PERIOD_NONE,
40f4aa0819SAdrian Hunter 	INTEL_PT_PERIOD_INSTRUCTIONS,
41f4aa0819SAdrian Hunter 	INTEL_PT_PERIOD_TICKS,
4279b58424SAdrian Hunter 	INTEL_PT_PERIOD_MTC,
43f4aa0819SAdrian Hunter };
44f4aa0819SAdrian Hunter 
45f4aa0819SAdrian Hunter enum {
46f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NOMEM = 1,
47f4aa0819SAdrian Hunter 	INTEL_PT_ERR_INTERN,
48f4aa0819SAdrian Hunter 	INTEL_PT_ERR_BADPKT,
49f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NODATA,
50f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NOINSN,
51f4aa0819SAdrian Hunter 	INTEL_PT_ERR_MISMAT,
52f4aa0819SAdrian Hunter 	INTEL_PT_ERR_OVR,
53f4aa0819SAdrian Hunter 	INTEL_PT_ERR_LOST,
54f4aa0819SAdrian Hunter 	INTEL_PT_ERR_UNK,
55f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NELOOP,
56f4aa0819SAdrian Hunter 	INTEL_PT_ERR_MAX,
57f4aa0819SAdrian Hunter };
58f4aa0819SAdrian Hunter 
599fb52336SAdrian Hunter enum intel_pt_param_flags {
609fb52336SAdrian Hunter 	/*
619fb52336SAdrian Hunter 	 * FUP packet can contain next linear instruction pointer instead of
629fb52336SAdrian Hunter 	 * current linear instruction pointer.
639fb52336SAdrian Hunter 	 */
649fb52336SAdrian Hunter 	INTEL_PT_FUP_WITH_NLIP	= 1 << 0,
659fb52336SAdrian Hunter };
669fb52336SAdrian Hunter 
674c35595eSAdrian Hunter enum intel_pt_blk_type {
684c35595eSAdrian Hunter 	INTEL_PT_GP_REGS	= 1,
694c35595eSAdrian Hunter 	INTEL_PT_PEBS_BASIC	= 4,
704c35595eSAdrian Hunter 	INTEL_PT_PEBS_MEM	= 5,
714c35595eSAdrian Hunter 	INTEL_PT_LBR_0		= 8,
724c35595eSAdrian Hunter 	INTEL_PT_LBR_1		= 9,
734c35595eSAdrian Hunter 	INTEL_PT_LBR_2		= 10,
744c35595eSAdrian Hunter 	INTEL_PT_XMM		= 16,
754c35595eSAdrian Hunter 	INTEL_PT_BLK_TYPE_MAX
764c35595eSAdrian Hunter };
774c35595eSAdrian Hunter 
784c35595eSAdrian Hunter /*
794c35595eSAdrian Hunter  * The block type numbers are not sequential but here they are given sequential
804c35595eSAdrian Hunter  * positions to avoid wasting space for array placement.
814c35595eSAdrian Hunter  */
824c35595eSAdrian Hunter enum intel_pt_blk_type_pos {
834c35595eSAdrian Hunter 	INTEL_PT_GP_REGS_POS,
844c35595eSAdrian Hunter 	INTEL_PT_PEBS_BASIC_POS,
854c35595eSAdrian Hunter 	INTEL_PT_PEBS_MEM_POS,
864c35595eSAdrian Hunter 	INTEL_PT_LBR_0_POS,
874c35595eSAdrian Hunter 	INTEL_PT_LBR_1_POS,
884c35595eSAdrian Hunter 	INTEL_PT_LBR_2_POS,
894c35595eSAdrian Hunter 	INTEL_PT_XMM_POS,
904c35595eSAdrian Hunter 	INTEL_PT_BLK_TYPE_CNT
914c35595eSAdrian Hunter };
924c35595eSAdrian Hunter 
934c35595eSAdrian Hunter /* Get the array position for a block type */
944c35595eSAdrian Hunter static inline int intel_pt_blk_type_pos(enum intel_pt_blk_type blk_type)
954c35595eSAdrian Hunter {
964c35595eSAdrian Hunter #define BLK_TYPE(bt) [INTEL_PT_##bt] = INTEL_PT_##bt##_POS + 1
974c35595eSAdrian Hunter 	const int map[INTEL_PT_BLK_TYPE_MAX] = {
984c35595eSAdrian Hunter 		BLK_TYPE(GP_REGS),
994c35595eSAdrian Hunter 		BLK_TYPE(PEBS_BASIC),
1004c35595eSAdrian Hunter 		BLK_TYPE(PEBS_MEM),
1014c35595eSAdrian Hunter 		BLK_TYPE(LBR_0),
1024c35595eSAdrian Hunter 		BLK_TYPE(LBR_1),
1034c35595eSAdrian Hunter 		BLK_TYPE(LBR_2),
1044c35595eSAdrian Hunter 		BLK_TYPE(XMM),
1054c35595eSAdrian Hunter 	};
1064c35595eSAdrian Hunter #undef BLK_TYPE
1074c35595eSAdrian Hunter 
1084c35595eSAdrian Hunter 	return blk_type < INTEL_PT_BLK_TYPE_MAX ? map[blk_type] - 1 : -1;
1094c35595eSAdrian Hunter }
1104c35595eSAdrian Hunter 
1114c35595eSAdrian Hunter #define INTEL_PT_BLK_ITEM_ID_CNT	32
1124c35595eSAdrian Hunter 
1134c35595eSAdrian Hunter /*
1144c35595eSAdrian Hunter  * Use unions so that the block items can be accessed by name or by array index.
1154c35595eSAdrian Hunter  * There is an array of 32-bit masks for each block type, which indicate which
1164c35595eSAdrian Hunter  * values are present. Then arrays of 32 64-bit values for each block type.
1174c35595eSAdrian Hunter  */
1184c35595eSAdrian Hunter struct intel_pt_blk_items {
1194c35595eSAdrian Hunter 	union {
1204c35595eSAdrian Hunter 		uint32_t mask[INTEL_PT_BLK_TYPE_CNT];
1214c35595eSAdrian Hunter 		struct {
1224c35595eSAdrian Hunter 			uint32_t has_rflags:1;
1234c35595eSAdrian Hunter 			uint32_t has_rip:1;
1244c35595eSAdrian Hunter 			uint32_t has_rax:1;
1254c35595eSAdrian Hunter 			uint32_t has_rcx:1;
1264c35595eSAdrian Hunter 			uint32_t has_rdx:1;
1274c35595eSAdrian Hunter 			uint32_t has_rbx:1;
1284c35595eSAdrian Hunter 			uint32_t has_rsp:1;
1294c35595eSAdrian Hunter 			uint32_t has_rbp:1;
1304c35595eSAdrian Hunter 			uint32_t has_rsi:1;
1314c35595eSAdrian Hunter 			uint32_t has_rdi:1;
1324c35595eSAdrian Hunter 			uint32_t has_r8:1;
1334c35595eSAdrian Hunter 			uint32_t has_r9:1;
1344c35595eSAdrian Hunter 			uint32_t has_r10:1;
1354c35595eSAdrian Hunter 			uint32_t has_r11:1;
1364c35595eSAdrian Hunter 			uint32_t has_r12:1;
1374c35595eSAdrian Hunter 			uint32_t has_r13:1;
1384c35595eSAdrian Hunter 			uint32_t has_r14:1;
1394c35595eSAdrian Hunter 			uint32_t has_r15:1;
1404c35595eSAdrian Hunter 			uint32_t has_unused_0:14;
1414c35595eSAdrian Hunter 			uint32_t has_ip:1;
1424c35595eSAdrian Hunter 			uint32_t has_applicable_counters:1;
1434c35595eSAdrian Hunter 			uint32_t has_timestamp:1;
1444c35595eSAdrian Hunter 			uint32_t has_unused_1:29;
1454c35595eSAdrian Hunter 			uint32_t has_mem_access_address:1;
1464c35595eSAdrian Hunter 			uint32_t has_mem_aux_info:1;
1474c35595eSAdrian Hunter 			uint32_t has_mem_access_latency:1;
1484c35595eSAdrian Hunter 			uint32_t has_tsx_aux_info:1;
1494c35595eSAdrian Hunter 			uint32_t has_unused_2:28;
1504c35595eSAdrian Hunter 			uint32_t has_lbr_0;
1514c35595eSAdrian Hunter 			uint32_t has_lbr_1;
1524c35595eSAdrian Hunter 			uint32_t has_lbr_2;
1534c35595eSAdrian Hunter 			uint32_t has_xmm;
1544c35595eSAdrian Hunter 		};
1554c35595eSAdrian Hunter 	};
1564c35595eSAdrian Hunter 	union {
1574c35595eSAdrian Hunter 		uint64_t val[INTEL_PT_BLK_TYPE_CNT][INTEL_PT_BLK_ITEM_ID_CNT];
1584c35595eSAdrian Hunter 		struct {
1594c35595eSAdrian Hunter 			struct {
1604c35595eSAdrian Hunter 				uint64_t rflags;
1614c35595eSAdrian Hunter 				uint64_t rip;
1624c35595eSAdrian Hunter 				uint64_t rax;
1634c35595eSAdrian Hunter 				uint64_t rcx;
1644c35595eSAdrian Hunter 				uint64_t rdx;
1654c35595eSAdrian Hunter 				uint64_t rbx;
1664c35595eSAdrian Hunter 				uint64_t rsp;
1674c35595eSAdrian Hunter 				uint64_t rbp;
1684c35595eSAdrian Hunter 				uint64_t rsi;
1694c35595eSAdrian Hunter 				uint64_t rdi;
1704c35595eSAdrian Hunter 				uint64_t r8;
1714c35595eSAdrian Hunter 				uint64_t r9;
1724c35595eSAdrian Hunter 				uint64_t r10;
1734c35595eSAdrian Hunter 				uint64_t r11;
1744c35595eSAdrian Hunter 				uint64_t r12;
1754c35595eSAdrian Hunter 				uint64_t r13;
1764c35595eSAdrian Hunter 				uint64_t r14;
1774c35595eSAdrian Hunter 				uint64_t r15;
1784c35595eSAdrian Hunter 				uint64_t unused_0[INTEL_PT_BLK_ITEM_ID_CNT - 18];
1794c35595eSAdrian Hunter 			};
1804c35595eSAdrian Hunter 			struct {
1814c35595eSAdrian Hunter 				uint64_t ip;
1824c35595eSAdrian Hunter 				uint64_t applicable_counters;
1834c35595eSAdrian Hunter 				uint64_t timestamp;
1844c35595eSAdrian Hunter 				uint64_t unused_1[INTEL_PT_BLK_ITEM_ID_CNT - 3];
1854c35595eSAdrian Hunter 			};
1864c35595eSAdrian Hunter 			struct {
1874c35595eSAdrian Hunter 				uint64_t mem_access_address;
1884c35595eSAdrian Hunter 				uint64_t mem_aux_info;
1894c35595eSAdrian Hunter 				uint64_t mem_access_latency;
1904c35595eSAdrian Hunter 				uint64_t tsx_aux_info;
1914c35595eSAdrian Hunter 				uint64_t unused_2[INTEL_PT_BLK_ITEM_ID_CNT - 4];
1924c35595eSAdrian Hunter 			};
1934c35595eSAdrian Hunter 			uint64_t lbr_0[INTEL_PT_BLK_ITEM_ID_CNT];
1944c35595eSAdrian Hunter 			uint64_t lbr_1[INTEL_PT_BLK_ITEM_ID_CNT];
1954c35595eSAdrian Hunter 			uint64_t lbr_2[INTEL_PT_BLK_ITEM_ID_CNT];
1964c35595eSAdrian Hunter 			uint64_t xmm[INTEL_PT_BLK_ITEM_ID_CNT];
1974c35595eSAdrian Hunter 		};
1984c35595eSAdrian Hunter 	};
1994c35595eSAdrian Hunter 	bool is_32_bit;
2004c35595eSAdrian Hunter };
2014c35595eSAdrian Hunter 
202f4aa0819SAdrian Hunter struct intel_pt_state {
203f4aa0819SAdrian Hunter 	enum intel_pt_sample_type type;
204f4aa0819SAdrian Hunter 	int err;
205f4aa0819SAdrian Hunter 	uint64_t from_ip;
206f4aa0819SAdrian Hunter 	uint64_t to_ip;
207f4aa0819SAdrian Hunter 	uint64_t cr3;
2082a21d036SAdrian Hunter 	uint64_t tot_insn_cnt;
2097b4b4f83SAdrian Hunter 	uint64_t tot_cyc_cnt;
210f4aa0819SAdrian Hunter 	uint64_t timestamp;
211f4aa0819SAdrian Hunter 	uint64_t est_timestamp;
212f4aa0819SAdrian Hunter 	uint64_t trace_nr;
213a472e65fSAdrian Hunter 	uint64_t ptw_payload;
214a472e65fSAdrian Hunter 	uint64_t mwait_payload;
215a472e65fSAdrian Hunter 	uint64_t pwre_payload;
216a472e65fSAdrian Hunter 	uint64_t pwrx_payload;
2170a7c700dSAdrian Hunter 	uint64_t cbr_payload;
218*c840cbfeSAdrian Hunter 	uint64_t psb_offset;
21951b09186SAdrian Hunter 	uint32_t cbr;
220f4aa0819SAdrian Hunter 	uint32_t flags;
221f4aa0819SAdrian Hunter 	enum intel_pt_insn_op insn_op;
222f4aa0819SAdrian Hunter 	int insn_len;
223faaa8768SAndi Kleen 	char insn[INTEL_PT_INSN_BUF_SZ];
2244c35595eSAdrian Hunter 	struct intel_pt_blk_items items;
225f4aa0819SAdrian Hunter };
226f4aa0819SAdrian Hunter 
227f4aa0819SAdrian Hunter struct intel_pt_insn;
228f4aa0819SAdrian Hunter 
229f4aa0819SAdrian Hunter struct intel_pt_buffer {
230f4aa0819SAdrian Hunter 	const unsigned char *buf;
231f4aa0819SAdrian Hunter 	size_t len;
232f4aa0819SAdrian Hunter 	bool consecutive;
233f4aa0819SAdrian Hunter 	uint64_t ref_timestamp;
234f4aa0819SAdrian Hunter 	uint64_t trace_nr;
235f4aa0819SAdrian Hunter };
236f4aa0819SAdrian Hunter 
2374d678e90SAdrian Hunter typedef int (*intel_pt_lookahead_cb_t)(struct intel_pt_buffer *, void *);
2384d678e90SAdrian Hunter 
239f4aa0819SAdrian Hunter struct intel_pt_params {
240f4aa0819SAdrian Hunter 	int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
241f4aa0819SAdrian Hunter 	int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
242f4aa0819SAdrian Hunter 			 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
243f4aa0819SAdrian Hunter 			 uint64_t max_insn_cnt, void *data);
2449f1d122bSAdrian Hunter 	bool (*pgd_ip)(uint64_t ip, void *data);
2454d678e90SAdrian Hunter 	int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
246f4aa0819SAdrian Hunter 	void *data;
247f4aa0819SAdrian Hunter 	bool return_compression;
24883959817SAdrian Hunter 	bool branch_enable;
2496af4b600SAdrian Hunter 	uint64_t ctl;
250f4aa0819SAdrian Hunter 	uint64_t period;
251f4aa0819SAdrian Hunter 	enum intel_pt_period_type period_type;
252f4aa0819SAdrian Hunter 	unsigned max_non_turbo_ratio;
25311fa7cb8SAdrian Hunter 	unsigned int mtc_period;
25411fa7cb8SAdrian Hunter 	uint32_t tsc_ctc_ratio_n;
25511fa7cb8SAdrian Hunter 	uint32_t tsc_ctc_ratio_d;
2569fb52336SAdrian Hunter 	enum intel_pt_param_flags flags;
2577c1b16baSAdrian Hunter 	unsigned int quick;
258f4aa0819SAdrian Hunter };
259f4aa0819SAdrian Hunter 
260f4aa0819SAdrian Hunter struct intel_pt_decoder;
261f4aa0819SAdrian Hunter 
262f4aa0819SAdrian Hunter struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params);
263f4aa0819SAdrian Hunter void intel_pt_decoder_free(struct intel_pt_decoder *decoder);
264f4aa0819SAdrian Hunter 
265f4aa0819SAdrian Hunter const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder);
266f4aa0819SAdrian Hunter 
267a7fa19f5SAdrian Hunter int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp);
268a7fa19f5SAdrian Hunter 
269f4aa0819SAdrian Hunter unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
270f4aa0819SAdrian Hunter 				     unsigned char *buf_b, size_t len_b,
271117db4b2SAdrian Hunter 				     bool have_tsc, bool *consecutive);
272f4aa0819SAdrian Hunter 
273f4aa0819SAdrian Hunter int intel_pt__strerror(int code, char *buf, size_t buflen);
274f4aa0819SAdrian Hunter 
275f4aa0819SAdrian Hunter #endif
276