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 "intel-pt-insn-decoder.h"
15 
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)
20 
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 };
34 
35 enum intel_pt_period_type {
36 	INTEL_PT_PERIOD_NONE,
37 	INTEL_PT_PERIOD_INSTRUCTIONS,
38 	INTEL_PT_PERIOD_TICKS,
39 	INTEL_PT_PERIOD_MTC,
40 };
41 
42 enum {
43 	INTEL_PT_ERR_NOMEM = 1,
44 	INTEL_PT_ERR_INTERN,
45 	INTEL_PT_ERR_BADPKT,
46 	INTEL_PT_ERR_NODATA,
47 	INTEL_PT_ERR_NOINSN,
48 	INTEL_PT_ERR_MISMAT,
49 	INTEL_PT_ERR_OVR,
50 	INTEL_PT_ERR_LOST,
51 	INTEL_PT_ERR_UNK,
52 	INTEL_PT_ERR_NELOOP,
53 	INTEL_PT_ERR_MAX,
54 };
55 
56 enum intel_pt_param_flags {
57 	/*
58 	 * FUP packet can contain next linear instruction pointer instead of
59 	 * current linear instruction pointer.
60 	 */
61 	INTEL_PT_FUP_WITH_NLIP	= 1 << 0,
62 };
63 
64 struct intel_pt_state {
65 	enum intel_pt_sample_type type;
66 	int err;
67 	uint64_t from_ip;
68 	uint64_t to_ip;
69 	uint64_t cr3;
70 	uint64_t tot_insn_cnt;
71 	uint64_t tot_cyc_cnt;
72 	uint64_t timestamp;
73 	uint64_t est_timestamp;
74 	uint64_t trace_nr;
75 	uint64_t ptw_payload;
76 	uint64_t mwait_payload;
77 	uint64_t pwre_payload;
78 	uint64_t pwrx_payload;
79 	uint64_t cbr_payload;
80 	uint32_t flags;
81 	enum intel_pt_insn_op insn_op;
82 	int insn_len;
83 	char insn[INTEL_PT_INSN_BUF_SZ];
84 };
85 
86 struct intel_pt_insn;
87 
88 struct intel_pt_buffer {
89 	const unsigned char *buf;
90 	size_t len;
91 	bool consecutive;
92 	uint64_t ref_timestamp;
93 	uint64_t trace_nr;
94 };
95 
96 typedef int (*intel_pt_lookahead_cb_t)(struct intel_pt_buffer *, void *);
97 
98 struct intel_pt_params {
99 	int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
100 	int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
101 			 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
102 			 uint64_t max_insn_cnt, void *data);
103 	bool (*pgd_ip)(uint64_t ip, void *data);
104 	int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
105 	void *data;
106 	bool return_compression;
107 	bool branch_enable;
108 	uint64_t period;
109 	enum intel_pt_period_type period_type;
110 	unsigned max_non_turbo_ratio;
111 	unsigned int mtc_period;
112 	uint32_t tsc_ctc_ratio_n;
113 	uint32_t tsc_ctc_ratio_d;
114 	enum intel_pt_param_flags flags;
115 };
116 
117 struct intel_pt_decoder;
118 
119 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params);
120 void intel_pt_decoder_free(struct intel_pt_decoder *decoder);
121 
122 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder);
123 
124 int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp);
125 
126 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
127 				     unsigned char *buf_b, size_t len_b,
128 				     bool have_tsc, bool *consecutive);
129 
130 int intel_pt__strerror(int code, char *buf, size_t buflen);
131 
132 #endif
133