1 /*
2  * intel_pt_decoder.h: Intel Processor Trace support
3  * Copyright (c) 2013-2014, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  */
15 
16 #ifndef INCLUDE__INTEL_PT_DECODER_H__
17 #define INCLUDE__INTEL_PT_DECODER_H__
18 
19 #include <stdint.h>
20 #include <stddef.h>
21 #include <stdbool.h>
22 
23 #include "intel-pt-insn-decoder.h"
24 
25 #define INTEL_PT_IN_TX		(1 << 0)
26 #define INTEL_PT_ABORT_TX	(1 << 1)
27 #define INTEL_PT_ASYNC		(1 << 2)
28 #define INTEL_PT_FUP_IP		(1 << 3)
29 
30 enum intel_pt_sample_type {
31 	INTEL_PT_BRANCH		= 1 << 0,
32 	INTEL_PT_INSTRUCTION	= 1 << 1,
33 	INTEL_PT_TRANSACTION	= 1 << 2,
34 	INTEL_PT_PTW		= 1 << 3,
35 	INTEL_PT_MWAIT_OP	= 1 << 4,
36 	INTEL_PT_PWR_ENTRY	= 1 << 5,
37 	INTEL_PT_EX_STOP	= 1 << 6,
38 	INTEL_PT_PWR_EXIT	= 1 << 7,
39 	INTEL_PT_CBR_CHG	= 1 << 8,
40 	INTEL_PT_TRACE_BEGIN	= 1 << 9,
41 	INTEL_PT_TRACE_END	= 1 << 10,
42 };
43 
44 enum intel_pt_period_type {
45 	INTEL_PT_PERIOD_NONE,
46 	INTEL_PT_PERIOD_INSTRUCTIONS,
47 	INTEL_PT_PERIOD_TICKS,
48 	INTEL_PT_PERIOD_MTC,
49 };
50 
51 enum {
52 	INTEL_PT_ERR_NOMEM = 1,
53 	INTEL_PT_ERR_INTERN,
54 	INTEL_PT_ERR_BADPKT,
55 	INTEL_PT_ERR_NODATA,
56 	INTEL_PT_ERR_NOINSN,
57 	INTEL_PT_ERR_MISMAT,
58 	INTEL_PT_ERR_OVR,
59 	INTEL_PT_ERR_LOST,
60 	INTEL_PT_ERR_UNK,
61 	INTEL_PT_ERR_NELOOP,
62 	INTEL_PT_ERR_MAX,
63 };
64 
65 enum intel_pt_param_flags {
66 	/*
67 	 * FUP packet can contain next linear instruction pointer instead of
68 	 * current linear instruction pointer.
69 	 */
70 	INTEL_PT_FUP_WITH_NLIP	= 1 << 0,
71 };
72 
73 struct intel_pt_state {
74 	enum intel_pt_sample_type type;
75 	int err;
76 	uint64_t from_ip;
77 	uint64_t to_ip;
78 	uint64_t cr3;
79 	uint64_t tot_insn_cnt;
80 	uint64_t timestamp;
81 	uint64_t est_timestamp;
82 	uint64_t trace_nr;
83 	uint64_t ptw_payload;
84 	uint64_t mwait_payload;
85 	uint64_t pwre_payload;
86 	uint64_t pwrx_payload;
87 	uint64_t cbr_payload;
88 	uint32_t flags;
89 	enum intel_pt_insn_op insn_op;
90 	int insn_len;
91 	char insn[INTEL_PT_INSN_BUF_SZ];
92 };
93 
94 struct intel_pt_insn;
95 
96 struct intel_pt_buffer {
97 	const unsigned char *buf;
98 	size_t len;
99 	bool consecutive;
100 	uint64_t ref_timestamp;
101 	uint64_t trace_nr;
102 };
103 
104 struct intel_pt_params {
105 	int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
106 	int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
107 			 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
108 			 uint64_t max_insn_cnt, void *data);
109 	bool (*pgd_ip)(uint64_t ip, void *data);
110 	void *data;
111 	bool return_compression;
112 	bool branch_enable;
113 	uint64_t period;
114 	enum intel_pt_period_type period_type;
115 	unsigned max_non_turbo_ratio;
116 	unsigned int mtc_period;
117 	uint32_t tsc_ctc_ratio_n;
118 	uint32_t tsc_ctc_ratio_d;
119 	enum intel_pt_param_flags flags;
120 };
121 
122 struct intel_pt_decoder;
123 
124 struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params);
125 void intel_pt_decoder_free(struct intel_pt_decoder *decoder);
126 
127 const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder);
128 
129 unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
130 				     unsigned char *buf_b, size_t len_b,
131 				     bool have_tsc, bool *consecutive);
132 
133 int intel_pt__strerror(int code, char *buf, size_t buflen);
134 
135 #endif
136