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)
20cf0c98e2SAdrian Hunter #define INTEL_PT_IFLAG (1 << 2)
21f4aa0819SAdrian Hunter #define INTEL_PT_ASYNC (1 << 2)
22a472e65fSAdrian Hunter #define INTEL_PT_FUP_IP (1 << 3)
2320aa3970SAdrian Hunter #define INTEL_PT_SAMPLE_IPC (1 << 4)
24f4aa0819SAdrian Hunter
25f4aa0819SAdrian Hunter enum intel_pt_sample_type {
26f4aa0819SAdrian Hunter INTEL_PT_BRANCH = 1 << 0,
27f4aa0819SAdrian Hunter INTEL_PT_INSTRUCTION = 1 << 1,
28f4aa0819SAdrian Hunter INTEL_PT_TRANSACTION = 1 << 2,
29a472e65fSAdrian Hunter INTEL_PT_PTW = 1 << 3,
30a472e65fSAdrian Hunter INTEL_PT_MWAIT_OP = 1 << 4,
31a472e65fSAdrian Hunter INTEL_PT_PWR_ENTRY = 1 << 5,
32a472e65fSAdrian Hunter INTEL_PT_EX_STOP = 1 << 6,
33a472e65fSAdrian Hunter INTEL_PT_PWR_EXIT = 1 << 7,
340a7c700dSAdrian Hunter INTEL_PT_CBR_CHG = 1 << 8,
35c6b5da09SAdrian Hunter INTEL_PT_TRACE_BEGIN = 1 << 9,
36c6b5da09SAdrian Hunter INTEL_PT_TRACE_END = 1 << 10,
374c35595eSAdrian Hunter INTEL_PT_BLK_ITEMS = 1 << 11,
38c840cbfeSAdrian Hunter INTEL_PT_PSB_EVT = 1 << 12,
393733a98bSAdrian Hunter INTEL_PT_EVT = 1 << 13,
40cf0c98e2SAdrian Hunter INTEL_PT_IFLAG_CHG = 1 << 14,
41f4aa0819SAdrian Hunter };
42f4aa0819SAdrian Hunter
43f4aa0819SAdrian Hunter enum intel_pt_period_type {
44f4aa0819SAdrian Hunter INTEL_PT_PERIOD_NONE,
45f4aa0819SAdrian Hunter INTEL_PT_PERIOD_INSTRUCTIONS,
46f4aa0819SAdrian Hunter INTEL_PT_PERIOD_TICKS,
4779b58424SAdrian Hunter INTEL_PT_PERIOD_MTC,
48f4aa0819SAdrian Hunter };
49f4aa0819SAdrian Hunter
50f4aa0819SAdrian Hunter enum {
51f4aa0819SAdrian Hunter INTEL_PT_ERR_NOMEM = 1,
52f4aa0819SAdrian Hunter INTEL_PT_ERR_INTERN,
53f4aa0819SAdrian Hunter INTEL_PT_ERR_BADPKT,
54f4aa0819SAdrian Hunter INTEL_PT_ERR_NODATA,
55f4aa0819SAdrian Hunter INTEL_PT_ERR_NOINSN,
56f4aa0819SAdrian Hunter INTEL_PT_ERR_MISMAT,
57f4aa0819SAdrian Hunter INTEL_PT_ERR_OVR,
58f4aa0819SAdrian Hunter INTEL_PT_ERR_LOST,
59f4aa0819SAdrian Hunter INTEL_PT_ERR_UNK,
60f4aa0819SAdrian Hunter INTEL_PT_ERR_NELOOP,
61*d7015e50SAdrian Hunter INTEL_PT_ERR_EPTW,
62f4aa0819SAdrian Hunter INTEL_PT_ERR_MAX,
63f4aa0819SAdrian Hunter };
64f4aa0819SAdrian Hunter
659fb52336SAdrian Hunter enum intel_pt_param_flags {
669fb52336SAdrian Hunter /*
679fb52336SAdrian Hunter * FUP packet can contain next linear instruction pointer instead of
689fb52336SAdrian Hunter * current linear instruction pointer.
699fb52336SAdrian Hunter */
709fb52336SAdrian Hunter INTEL_PT_FUP_WITH_NLIP = 1 << 0,
719fb52336SAdrian Hunter };
729fb52336SAdrian Hunter
734c35595eSAdrian Hunter enum intel_pt_blk_type {
744c35595eSAdrian Hunter INTEL_PT_GP_REGS = 1,
754c35595eSAdrian Hunter INTEL_PT_PEBS_BASIC = 4,
764c35595eSAdrian Hunter INTEL_PT_PEBS_MEM = 5,
774c35595eSAdrian Hunter INTEL_PT_LBR_0 = 8,
784c35595eSAdrian Hunter INTEL_PT_LBR_1 = 9,
794c35595eSAdrian Hunter INTEL_PT_LBR_2 = 10,
804c35595eSAdrian Hunter INTEL_PT_XMM = 16,
814c35595eSAdrian Hunter INTEL_PT_BLK_TYPE_MAX
824c35595eSAdrian Hunter };
834c35595eSAdrian Hunter
844c35595eSAdrian Hunter /*
854c35595eSAdrian Hunter * The block type numbers are not sequential but here they are given sequential
864c35595eSAdrian Hunter * positions to avoid wasting space for array placement.
874c35595eSAdrian Hunter */
884c35595eSAdrian Hunter enum intel_pt_blk_type_pos {
894c35595eSAdrian Hunter INTEL_PT_GP_REGS_POS,
904c35595eSAdrian Hunter INTEL_PT_PEBS_BASIC_POS,
914c35595eSAdrian Hunter INTEL_PT_PEBS_MEM_POS,
924c35595eSAdrian Hunter INTEL_PT_LBR_0_POS,
934c35595eSAdrian Hunter INTEL_PT_LBR_1_POS,
944c35595eSAdrian Hunter INTEL_PT_LBR_2_POS,
954c35595eSAdrian Hunter INTEL_PT_XMM_POS,
964c35595eSAdrian Hunter INTEL_PT_BLK_TYPE_CNT
974c35595eSAdrian Hunter };
984c35595eSAdrian Hunter
994c35595eSAdrian Hunter /* Get the array position for a block type */
intel_pt_blk_type_pos(enum intel_pt_blk_type blk_type)1004c35595eSAdrian Hunter static inline int intel_pt_blk_type_pos(enum intel_pt_blk_type blk_type)
1014c35595eSAdrian Hunter {
1024c35595eSAdrian Hunter #define BLK_TYPE(bt) [INTEL_PT_##bt] = INTEL_PT_##bt##_POS + 1
1034c35595eSAdrian Hunter const int map[INTEL_PT_BLK_TYPE_MAX] = {
1044c35595eSAdrian Hunter BLK_TYPE(GP_REGS),
1054c35595eSAdrian Hunter BLK_TYPE(PEBS_BASIC),
1064c35595eSAdrian Hunter BLK_TYPE(PEBS_MEM),
1074c35595eSAdrian Hunter BLK_TYPE(LBR_0),
1084c35595eSAdrian Hunter BLK_TYPE(LBR_1),
1094c35595eSAdrian Hunter BLK_TYPE(LBR_2),
1104c35595eSAdrian Hunter BLK_TYPE(XMM),
1114c35595eSAdrian Hunter };
1124c35595eSAdrian Hunter #undef BLK_TYPE
1134c35595eSAdrian Hunter
1144c35595eSAdrian Hunter return blk_type < INTEL_PT_BLK_TYPE_MAX ? map[blk_type] - 1 : -1;
1154c35595eSAdrian Hunter }
1164c35595eSAdrian Hunter
1174c35595eSAdrian Hunter #define INTEL_PT_BLK_ITEM_ID_CNT 32
1184c35595eSAdrian Hunter
1194c35595eSAdrian Hunter /*
1204c35595eSAdrian Hunter * Use unions so that the block items can be accessed by name or by array index.
1214c35595eSAdrian Hunter * There is an array of 32-bit masks for each block type, which indicate which
1224c35595eSAdrian Hunter * values are present. Then arrays of 32 64-bit values for each block type.
1234c35595eSAdrian Hunter */
1244c35595eSAdrian Hunter struct intel_pt_blk_items {
1254c35595eSAdrian Hunter union {
1264c35595eSAdrian Hunter uint32_t mask[INTEL_PT_BLK_TYPE_CNT];
1274c35595eSAdrian Hunter struct {
1284c35595eSAdrian Hunter uint32_t has_rflags:1;
1294c35595eSAdrian Hunter uint32_t has_rip:1;
1304c35595eSAdrian Hunter uint32_t has_rax:1;
1314c35595eSAdrian Hunter uint32_t has_rcx:1;
1324c35595eSAdrian Hunter uint32_t has_rdx:1;
1334c35595eSAdrian Hunter uint32_t has_rbx:1;
1344c35595eSAdrian Hunter uint32_t has_rsp:1;
1354c35595eSAdrian Hunter uint32_t has_rbp:1;
1364c35595eSAdrian Hunter uint32_t has_rsi:1;
1374c35595eSAdrian Hunter uint32_t has_rdi:1;
1384c35595eSAdrian Hunter uint32_t has_r8:1;
1394c35595eSAdrian Hunter uint32_t has_r9:1;
1404c35595eSAdrian Hunter uint32_t has_r10:1;
1414c35595eSAdrian Hunter uint32_t has_r11:1;
1424c35595eSAdrian Hunter uint32_t has_r12:1;
1434c35595eSAdrian Hunter uint32_t has_r13:1;
1444c35595eSAdrian Hunter uint32_t has_r14:1;
1454c35595eSAdrian Hunter uint32_t has_r15:1;
1464c35595eSAdrian Hunter uint32_t has_unused_0:14;
1474c35595eSAdrian Hunter uint32_t has_ip:1;
1484c35595eSAdrian Hunter uint32_t has_applicable_counters:1;
1494c35595eSAdrian Hunter uint32_t has_timestamp:1;
1504c35595eSAdrian Hunter uint32_t has_unused_1:29;
1514c35595eSAdrian Hunter uint32_t has_mem_access_address:1;
1524c35595eSAdrian Hunter uint32_t has_mem_aux_info:1;
1534c35595eSAdrian Hunter uint32_t has_mem_access_latency:1;
1544c35595eSAdrian Hunter uint32_t has_tsx_aux_info:1;
1554c35595eSAdrian Hunter uint32_t has_unused_2:28;
1564c35595eSAdrian Hunter uint32_t has_lbr_0;
1574c35595eSAdrian Hunter uint32_t has_lbr_1;
1584c35595eSAdrian Hunter uint32_t has_lbr_2;
1594c35595eSAdrian Hunter uint32_t has_xmm;
1604c35595eSAdrian Hunter };
1614c35595eSAdrian Hunter };
1624c35595eSAdrian Hunter union {
1634c35595eSAdrian Hunter uint64_t val[INTEL_PT_BLK_TYPE_CNT][INTEL_PT_BLK_ITEM_ID_CNT];
1644c35595eSAdrian Hunter struct {
1654c35595eSAdrian Hunter struct {
1664c35595eSAdrian Hunter uint64_t rflags;
1674c35595eSAdrian Hunter uint64_t rip;
1684c35595eSAdrian Hunter uint64_t rax;
1694c35595eSAdrian Hunter uint64_t rcx;
1704c35595eSAdrian Hunter uint64_t rdx;
1714c35595eSAdrian Hunter uint64_t rbx;
1724c35595eSAdrian Hunter uint64_t rsp;
1734c35595eSAdrian Hunter uint64_t rbp;
1744c35595eSAdrian Hunter uint64_t rsi;
1754c35595eSAdrian Hunter uint64_t rdi;
1764c35595eSAdrian Hunter uint64_t r8;
1774c35595eSAdrian Hunter uint64_t r9;
1784c35595eSAdrian Hunter uint64_t r10;
1794c35595eSAdrian Hunter uint64_t r11;
1804c35595eSAdrian Hunter uint64_t r12;
1814c35595eSAdrian Hunter uint64_t r13;
1824c35595eSAdrian Hunter uint64_t r14;
1834c35595eSAdrian Hunter uint64_t r15;
1844c35595eSAdrian Hunter uint64_t unused_0[INTEL_PT_BLK_ITEM_ID_CNT - 18];
1854c35595eSAdrian Hunter };
1864c35595eSAdrian Hunter struct {
1874c35595eSAdrian Hunter uint64_t ip;
1884c35595eSAdrian Hunter uint64_t applicable_counters;
1894c35595eSAdrian Hunter uint64_t timestamp;
1904c35595eSAdrian Hunter uint64_t unused_1[INTEL_PT_BLK_ITEM_ID_CNT - 3];
1914c35595eSAdrian Hunter };
1924c35595eSAdrian Hunter struct {
1934c35595eSAdrian Hunter uint64_t mem_access_address;
1944c35595eSAdrian Hunter uint64_t mem_aux_info;
1954c35595eSAdrian Hunter uint64_t mem_access_latency;
1964c35595eSAdrian Hunter uint64_t tsx_aux_info;
1974c35595eSAdrian Hunter uint64_t unused_2[INTEL_PT_BLK_ITEM_ID_CNT - 4];
1984c35595eSAdrian Hunter };
1994c35595eSAdrian Hunter uint64_t lbr_0[INTEL_PT_BLK_ITEM_ID_CNT];
2004c35595eSAdrian Hunter uint64_t lbr_1[INTEL_PT_BLK_ITEM_ID_CNT];
2014c35595eSAdrian Hunter uint64_t lbr_2[INTEL_PT_BLK_ITEM_ID_CNT];
2024c35595eSAdrian Hunter uint64_t xmm[INTEL_PT_BLK_ITEM_ID_CNT];
2034c35595eSAdrian Hunter };
2044c35595eSAdrian Hunter };
2054c35595eSAdrian Hunter bool is_32_bit;
2064c35595eSAdrian Hunter };
2074c35595eSAdrian Hunter
2080fc9d338SAdrian Hunter struct intel_pt_vmcs_info {
2090fc9d338SAdrian Hunter struct rb_node rb_node;
2100fc9d338SAdrian Hunter uint64_t vmcs;
2110fc9d338SAdrian Hunter uint64_t tsc_offset;
2120fc9d338SAdrian Hunter bool reliable;
2130fc9d338SAdrian Hunter bool error_printed;
2140fc9d338SAdrian Hunter };
2150fc9d338SAdrian Hunter
2163733a98bSAdrian Hunter /*
2173733a98bSAdrian Hunter * Maximum number of event trace data in one go, assuming at most 1 per type
2183733a98bSAdrian Hunter * and 6-bits of type in the EVD packet.
2193733a98bSAdrian Hunter */
2203733a98bSAdrian Hunter #define INTEL_PT_MAX_EVDS 64
2213733a98bSAdrian Hunter
2223733a98bSAdrian Hunter /* Event trace data from EVD packet */
2233733a98bSAdrian Hunter struct intel_pt_evd {
2243733a98bSAdrian Hunter int type;
2253733a98bSAdrian Hunter uint64_t payload;
2263733a98bSAdrian Hunter };
2273733a98bSAdrian Hunter
228f4aa0819SAdrian Hunter struct intel_pt_state {
229f4aa0819SAdrian Hunter enum intel_pt_sample_type type;
23080a03886SAdrian Hunter bool from_nr;
23180a03886SAdrian Hunter bool to_nr;
232cf0c98e2SAdrian Hunter bool from_iflag;
233cf0c98e2SAdrian Hunter bool to_iflag;
234f4aa0819SAdrian Hunter int err;
235f4aa0819SAdrian Hunter uint64_t from_ip;
236f4aa0819SAdrian Hunter uint64_t to_ip;
2372a21d036SAdrian Hunter uint64_t tot_insn_cnt;
2387b4b4f83SAdrian Hunter uint64_t tot_cyc_cnt;
239f2b91386SAdrian Hunter uint64_t cycles;
240f4aa0819SAdrian Hunter uint64_t timestamp;
241f4aa0819SAdrian Hunter uint64_t est_timestamp;
242f4aa0819SAdrian Hunter uint64_t trace_nr;
243a472e65fSAdrian Hunter uint64_t ptw_payload;
244a472e65fSAdrian Hunter uint64_t mwait_payload;
245a472e65fSAdrian Hunter uint64_t pwre_payload;
246a472e65fSAdrian Hunter uint64_t pwrx_payload;
2470a7c700dSAdrian Hunter uint64_t cbr_payload;
248c840cbfeSAdrian Hunter uint64_t psb_offset;
24951b09186SAdrian Hunter uint32_t cbr;
250f4aa0819SAdrian Hunter uint32_t flags;
251f4aa0819SAdrian Hunter enum intel_pt_insn_op insn_op;
252f4aa0819SAdrian Hunter int insn_len;
253faaa8768SAndi Kleen char insn[INTEL_PT_INSN_BUF_SZ];
2544c35595eSAdrian Hunter struct intel_pt_blk_items items;
2553733a98bSAdrian Hunter int cfe_type;
2563733a98bSAdrian Hunter int cfe_vector;
2573733a98bSAdrian Hunter int evd_cnt;
2583733a98bSAdrian Hunter struct intel_pt_evd *evd;
259f4aa0819SAdrian Hunter };
260f4aa0819SAdrian Hunter
261f4aa0819SAdrian Hunter struct intel_pt_insn;
262f4aa0819SAdrian Hunter
263f4aa0819SAdrian Hunter struct intel_pt_buffer {
264f4aa0819SAdrian Hunter const unsigned char *buf;
265f4aa0819SAdrian Hunter size_t len;
266f4aa0819SAdrian Hunter bool consecutive;
267f4aa0819SAdrian Hunter uint64_t ref_timestamp;
268f4aa0819SAdrian Hunter uint64_t trace_nr;
269f4aa0819SAdrian Hunter };
270f4aa0819SAdrian Hunter
2714d678e90SAdrian Hunter typedef int (*intel_pt_lookahead_cb_t)(struct intel_pt_buffer *, void *);
2724d678e90SAdrian Hunter
273f4aa0819SAdrian Hunter struct intel_pt_params {
274f4aa0819SAdrian Hunter int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
275f4aa0819SAdrian Hunter int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
276f4aa0819SAdrian Hunter uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
277f4aa0819SAdrian Hunter uint64_t max_insn_cnt, void *data);
2789f1d122bSAdrian Hunter bool (*pgd_ip)(uint64_t ip, void *data);
2794d678e90SAdrian Hunter int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
2800fc9d338SAdrian Hunter struct intel_pt_vmcs_info *(*findnew_vmcs_info)(void *data, uint64_t vmcs);
281f4aa0819SAdrian Hunter void *data;
282f4aa0819SAdrian Hunter bool return_compression;
28383959817SAdrian Hunter bool branch_enable;
284fa8f949dSAdrian Hunter bool vm_time_correlation;
285fa8f949dSAdrian Hunter bool vm_tm_corr_dry_run;
2865ac35d77SAdrian Hunter uint64_t first_timestamp;
2876af4b600SAdrian Hunter uint64_t ctl;
288f4aa0819SAdrian Hunter uint64_t period;
289f4aa0819SAdrian Hunter enum intel_pt_period_type period_type;
290f4aa0819SAdrian Hunter unsigned max_non_turbo_ratio;
29111fa7cb8SAdrian Hunter unsigned int mtc_period;
29211fa7cb8SAdrian Hunter uint32_t tsc_ctc_ratio_n;
29311fa7cb8SAdrian Hunter uint32_t tsc_ctc_ratio_d;
2949fb52336SAdrian Hunter enum intel_pt_param_flags flags;
2957c1b16baSAdrian Hunter unsigned int quick;
296b4b046ffSAdrian Hunter int max_loops;
297f4aa0819SAdrian Hunter };
298f4aa0819SAdrian Hunter
299f4aa0819SAdrian Hunter struct intel_pt_decoder;
300f4aa0819SAdrian Hunter
301f4aa0819SAdrian Hunter struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params);
302f4aa0819SAdrian Hunter void intel_pt_decoder_free(struct intel_pt_decoder *decoder);
303f4aa0819SAdrian Hunter
304f4aa0819SAdrian Hunter const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder);
305f4aa0819SAdrian Hunter
306a7fa19f5SAdrian Hunter int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp);
307a7fa19f5SAdrian Hunter
308f4aa0819SAdrian Hunter unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
309f4aa0819SAdrian Hunter unsigned char *buf_b, size_t len_b,
310335358ccSAdrian Hunter bool have_tsc, bool *consecutive,
311335358ccSAdrian Hunter bool ooo_tsc);
312f4aa0819SAdrian Hunter
313f4aa0819SAdrian Hunter int intel_pt__strerror(int code, char *buf, size_t buflen);
314f4aa0819SAdrian Hunter
3155ac35d77SAdrian Hunter void intel_pt_set_first_timestamp(struct intel_pt_decoder *decoder,
3165ac35d77SAdrian Hunter uint64_t first_timestamp);
3175ac35d77SAdrian Hunter
318f4aa0819SAdrian Hunter #endif
319