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