xref: /openbmc/linux/tools/perf/util/intel-pt-decoder/intel-pt-decoder.h (revision 2025cf9e193de05b0654570dd639acb49ebd3adf)
1*2025cf9eSThomas 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)
20f4aa0819SAdrian Hunter 
21f4aa0819SAdrian Hunter enum intel_pt_sample_type {
22f4aa0819SAdrian Hunter 	INTEL_PT_BRANCH		= 1 << 0,
23f4aa0819SAdrian Hunter 	INTEL_PT_INSTRUCTION	= 1 << 1,
24f4aa0819SAdrian Hunter 	INTEL_PT_TRANSACTION	= 1 << 2,
25a472e65fSAdrian Hunter 	INTEL_PT_PTW		= 1 << 3,
26a472e65fSAdrian Hunter 	INTEL_PT_MWAIT_OP	= 1 << 4,
27a472e65fSAdrian Hunter 	INTEL_PT_PWR_ENTRY	= 1 << 5,
28a472e65fSAdrian Hunter 	INTEL_PT_EX_STOP	= 1 << 6,
29a472e65fSAdrian Hunter 	INTEL_PT_PWR_EXIT	= 1 << 7,
300a7c700dSAdrian Hunter 	INTEL_PT_CBR_CHG	= 1 << 8,
31c6b5da09SAdrian Hunter 	INTEL_PT_TRACE_BEGIN	= 1 << 9,
32c6b5da09SAdrian Hunter 	INTEL_PT_TRACE_END	= 1 << 10,
33f4aa0819SAdrian Hunter };
34f4aa0819SAdrian Hunter 
35f4aa0819SAdrian Hunter enum intel_pt_period_type {
36f4aa0819SAdrian Hunter 	INTEL_PT_PERIOD_NONE,
37f4aa0819SAdrian Hunter 	INTEL_PT_PERIOD_INSTRUCTIONS,
38f4aa0819SAdrian Hunter 	INTEL_PT_PERIOD_TICKS,
3979b58424SAdrian Hunter 	INTEL_PT_PERIOD_MTC,
40f4aa0819SAdrian Hunter };
41f4aa0819SAdrian Hunter 
42f4aa0819SAdrian Hunter enum {
43f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NOMEM = 1,
44f4aa0819SAdrian Hunter 	INTEL_PT_ERR_INTERN,
45f4aa0819SAdrian Hunter 	INTEL_PT_ERR_BADPKT,
46f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NODATA,
47f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NOINSN,
48f4aa0819SAdrian Hunter 	INTEL_PT_ERR_MISMAT,
49f4aa0819SAdrian Hunter 	INTEL_PT_ERR_OVR,
50f4aa0819SAdrian Hunter 	INTEL_PT_ERR_LOST,
51f4aa0819SAdrian Hunter 	INTEL_PT_ERR_UNK,
52f4aa0819SAdrian Hunter 	INTEL_PT_ERR_NELOOP,
53f4aa0819SAdrian Hunter 	INTEL_PT_ERR_MAX,
54f4aa0819SAdrian Hunter };
55f4aa0819SAdrian Hunter 
569fb52336SAdrian Hunter enum intel_pt_param_flags {
579fb52336SAdrian Hunter 	/*
589fb52336SAdrian Hunter 	 * FUP packet can contain next linear instruction pointer instead of
599fb52336SAdrian Hunter 	 * current linear instruction pointer.
609fb52336SAdrian Hunter 	 */
619fb52336SAdrian Hunter 	INTEL_PT_FUP_WITH_NLIP	= 1 << 0,
629fb52336SAdrian Hunter };
639fb52336SAdrian Hunter 
64f4aa0819SAdrian Hunter struct intel_pt_state {
65f4aa0819SAdrian Hunter 	enum intel_pt_sample_type type;
66f4aa0819SAdrian Hunter 	int err;
67f4aa0819SAdrian Hunter 	uint64_t from_ip;
68f4aa0819SAdrian Hunter 	uint64_t to_ip;
69f4aa0819SAdrian Hunter 	uint64_t cr3;
702a21d036SAdrian Hunter 	uint64_t tot_insn_cnt;
71f4aa0819SAdrian Hunter 	uint64_t timestamp;
72f4aa0819SAdrian Hunter 	uint64_t est_timestamp;
73f4aa0819SAdrian Hunter 	uint64_t trace_nr;
74a472e65fSAdrian Hunter 	uint64_t ptw_payload;
75a472e65fSAdrian Hunter 	uint64_t mwait_payload;
76a472e65fSAdrian Hunter 	uint64_t pwre_payload;
77a472e65fSAdrian Hunter 	uint64_t pwrx_payload;
780a7c700dSAdrian Hunter 	uint64_t cbr_payload;
79f4aa0819SAdrian Hunter 	uint32_t flags;
80f4aa0819SAdrian Hunter 	enum intel_pt_insn_op insn_op;
81f4aa0819SAdrian Hunter 	int insn_len;
82faaa8768SAndi Kleen 	char insn[INTEL_PT_INSN_BUF_SZ];
83f4aa0819SAdrian Hunter };
84f4aa0819SAdrian Hunter 
85f4aa0819SAdrian Hunter struct intel_pt_insn;
86f4aa0819SAdrian Hunter 
87f4aa0819SAdrian Hunter struct intel_pt_buffer {
88f4aa0819SAdrian Hunter 	const unsigned char *buf;
89f4aa0819SAdrian Hunter 	size_t len;
90f4aa0819SAdrian Hunter 	bool consecutive;
91f4aa0819SAdrian Hunter 	uint64_t ref_timestamp;
92f4aa0819SAdrian Hunter 	uint64_t trace_nr;
93f4aa0819SAdrian Hunter };
94f4aa0819SAdrian Hunter 
95f4aa0819SAdrian Hunter struct intel_pt_params {
96f4aa0819SAdrian Hunter 	int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
97f4aa0819SAdrian Hunter 	int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
98f4aa0819SAdrian Hunter 			 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
99f4aa0819SAdrian Hunter 			 uint64_t max_insn_cnt, void *data);
1009f1d122bSAdrian Hunter 	bool (*pgd_ip)(uint64_t ip, void *data);
101f4aa0819SAdrian Hunter 	void *data;
102f4aa0819SAdrian Hunter 	bool return_compression;
10383959817SAdrian Hunter 	bool branch_enable;
104f4aa0819SAdrian Hunter 	uint64_t period;
105f4aa0819SAdrian Hunter 	enum intel_pt_period_type period_type;
106f4aa0819SAdrian Hunter 	unsigned max_non_turbo_ratio;
10711fa7cb8SAdrian Hunter 	unsigned int mtc_period;
10811fa7cb8SAdrian Hunter 	uint32_t tsc_ctc_ratio_n;
10911fa7cb8SAdrian Hunter 	uint32_t tsc_ctc_ratio_d;
1109fb52336SAdrian Hunter 	enum intel_pt_param_flags flags;
111f4aa0819SAdrian Hunter };
112f4aa0819SAdrian Hunter 
113f4aa0819SAdrian Hunter struct intel_pt_decoder;
114f4aa0819SAdrian Hunter 
115f4aa0819SAdrian Hunter struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params);
116f4aa0819SAdrian Hunter void intel_pt_decoder_free(struct intel_pt_decoder *decoder);
117f4aa0819SAdrian Hunter 
118f4aa0819SAdrian Hunter const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder);
119f4aa0819SAdrian Hunter 
120f4aa0819SAdrian Hunter unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
121f4aa0819SAdrian Hunter 				     unsigned char *buf_b, size_t len_b,
122117db4b2SAdrian Hunter 				     bool have_tsc, bool *consecutive);
123f4aa0819SAdrian Hunter 
124f4aa0819SAdrian Hunter int intel_pt__strerror(int code, char *buf, size_t buflen);
125f4aa0819SAdrian Hunter 
126f4aa0819SAdrian Hunter #endif
127