Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,26 @@
perf-util-y += intel-pt-pkt-decoder.o intel-pt-insn-decoder.o intel-pt-log.o intel-pt-decoder.o
inat_tables_script = $(srctree)/tools/arch/x86/tools/gen-insn-attr-x86.awk
inat_tables_maps = $(srctree)/tools/arch/x86/lib/x86-opcode-map.txt
$(OUTPUT)util/intel-pt-decoder/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
$(call rule_mkdir)
@$(call echo-cmd,gen)$(AWK) -f $(inat_tables_script) $(inat_tables_maps) > $@ || rm -f $@
perf-util-y += inat.o insn.o
$(OUTPUT)util/intel-pt-decoder/inat.o: $(srctree)/tools/arch/x86/lib/inat.c $(OUTPUT)util/intel-pt-decoder/inat-tables.c
$(call rule_mkdir)
$(call if_changed_dep,cc_o_c)
CFLAGS_inat.o += -I$(OUTPUT)util/intel-pt-decoder
$(OUTPUT)util/intel-pt-decoder/insn.o: $(srctree)/tools/arch/x86/lib/insn.c
$(call rule_mkdir)
$(call if_changed_dep,cc_o_c)
ifeq ($(CC_NO_CLANG), 1)
CFLAGS_insn.o += -Wno-override-init
endif
CFLAGS_insn.o += -Wno-packed
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,318 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* intel_pt_decoder.h: Intel Processor Trace support
* Copyright (c) 2013-2014, Intel Corporation.
*/
#ifndef INCLUDE__INTEL_PT_DECODER_H__
#define INCLUDE__INTEL_PT_DECODER_H__
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <linux/rbtree.h>
#include "intel-pt-insn-decoder.h"
#define INTEL_PT_IN_TX (1 << 0)
#define INTEL_PT_ABORT_TX (1 << 1)
#define INTEL_PT_IFLAG (1 << 2)
#define INTEL_PT_ASYNC (1 << 2)
#define INTEL_PT_FUP_IP (1 << 3)
#define INTEL_PT_SAMPLE_IPC (1 << 4)
enum intel_pt_sample_type {
INTEL_PT_BRANCH = 1 << 0,
INTEL_PT_INSTRUCTION = 1 << 1,
INTEL_PT_TRANSACTION = 1 << 2,
INTEL_PT_PTW = 1 << 3,
INTEL_PT_MWAIT_OP = 1 << 4,
INTEL_PT_PWR_ENTRY = 1 << 5,
INTEL_PT_EX_STOP = 1 << 6,
INTEL_PT_PWR_EXIT = 1 << 7,
INTEL_PT_CBR_CHG = 1 << 8,
INTEL_PT_TRACE_BEGIN = 1 << 9,
INTEL_PT_TRACE_END = 1 << 10,
INTEL_PT_BLK_ITEMS = 1 << 11,
INTEL_PT_PSB_EVT = 1 << 12,
INTEL_PT_EVT = 1 << 13,
INTEL_PT_IFLAG_CHG = 1 << 14,
};
enum intel_pt_period_type {
INTEL_PT_PERIOD_NONE,
INTEL_PT_PERIOD_INSTRUCTIONS,
INTEL_PT_PERIOD_TICKS,
INTEL_PT_PERIOD_MTC,
};
enum {
INTEL_PT_ERR_NOMEM = 1,
INTEL_PT_ERR_INTERN,
INTEL_PT_ERR_BADPKT,
INTEL_PT_ERR_NODATA,
INTEL_PT_ERR_NOINSN,
INTEL_PT_ERR_MISMAT,
INTEL_PT_ERR_OVR,
INTEL_PT_ERR_LOST,
INTEL_PT_ERR_UNK,
INTEL_PT_ERR_NELOOP,
INTEL_PT_ERR_EPTW,
INTEL_PT_ERR_MAX,
};
enum intel_pt_param_flags {
/*
* FUP packet can contain next linear instruction pointer instead of
* current linear instruction pointer.
*/
INTEL_PT_FUP_WITH_NLIP = 1 << 0,
};
enum intel_pt_blk_type {
INTEL_PT_GP_REGS = 1,
INTEL_PT_PEBS_BASIC = 4,
INTEL_PT_PEBS_MEM = 5,
INTEL_PT_LBR_0 = 8,
INTEL_PT_LBR_1 = 9,
INTEL_PT_LBR_2 = 10,
INTEL_PT_XMM = 16,
INTEL_PT_BLK_TYPE_MAX
};
/*
* The block type numbers are not sequential but here they are given sequential
* positions to avoid wasting space for array placement.
*/
enum intel_pt_blk_type_pos {
INTEL_PT_GP_REGS_POS,
INTEL_PT_PEBS_BASIC_POS,
INTEL_PT_PEBS_MEM_POS,
INTEL_PT_LBR_0_POS,
INTEL_PT_LBR_1_POS,
INTEL_PT_LBR_2_POS,
INTEL_PT_XMM_POS,
INTEL_PT_BLK_TYPE_CNT
};
/* Get the array position for a block type */
static inline int intel_pt_blk_type_pos(enum intel_pt_blk_type blk_type)
{
#define BLK_TYPE(bt) [INTEL_PT_##bt] = INTEL_PT_##bt##_POS + 1
const int map[INTEL_PT_BLK_TYPE_MAX] = {
BLK_TYPE(GP_REGS),
BLK_TYPE(PEBS_BASIC),
BLK_TYPE(PEBS_MEM),
BLK_TYPE(LBR_0),
BLK_TYPE(LBR_1),
BLK_TYPE(LBR_2),
BLK_TYPE(XMM),
};
#undef BLK_TYPE
return blk_type < INTEL_PT_BLK_TYPE_MAX ? map[blk_type] - 1 : -1;
}
#define INTEL_PT_BLK_ITEM_ID_CNT 32
/*
* Use unions so that the block items can be accessed by name or by array index.
* There is an array of 32-bit masks for each block type, which indicate which
* values are present. Then arrays of 32 64-bit values for each block type.
*/
struct intel_pt_blk_items {
union {
uint32_t mask[INTEL_PT_BLK_TYPE_CNT];
struct {
uint32_t has_rflags:1;
uint32_t has_rip:1;
uint32_t has_rax:1;
uint32_t has_rcx:1;
uint32_t has_rdx:1;
uint32_t has_rbx:1;
uint32_t has_rsp:1;
uint32_t has_rbp:1;
uint32_t has_rsi:1;
uint32_t has_rdi:1;
uint32_t has_r8:1;
uint32_t has_r9:1;
uint32_t has_r10:1;
uint32_t has_r11:1;
uint32_t has_r12:1;
uint32_t has_r13:1;
uint32_t has_r14:1;
uint32_t has_r15:1;
uint32_t has_unused_0:14;
uint32_t has_ip:1;
uint32_t has_applicable_counters:1;
uint32_t has_timestamp:1;
uint32_t has_unused_1:29;
uint32_t has_mem_access_address:1;
uint32_t has_mem_aux_info:1;
uint32_t has_mem_access_latency:1;
uint32_t has_tsx_aux_info:1;
uint32_t has_unused_2:28;
uint32_t has_lbr_0;
uint32_t has_lbr_1;
uint32_t has_lbr_2;
uint32_t has_xmm;
};
};
union {
uint64_t val[INTEL_PT_BLK_TYPE_CNT][INTEL_PT_BLK_ITEM_ID_CNT];
struct {
struct {
uint64_t rflags;
uint64_t rip;
uint64_t rax;
uint64_t rcx;
uint64_t rdx;
uint64_t rbx;
uint64_t rsp;
uint64_t rbp;
uint64_t rsi;
uint64_t rdi;
uint64_t r8;
uint64_t r9;
uint64_t r10;
uint64_t r11;
uint64_t r12;
uint64_t r13;
uint64_t r14;
uint64_t r15;
uint64_t unused_0[INTEL_PT_BLK_ITEM_ID_CNT - 18];
};
struct {
uint64_t ip;
uint64_t applicable_counters;
uint64_t timestamp;
uint64_t unused_1[INTEL_PT_BLK_ITEM_ID_CNT - 3];
};
struct {
uint64_t mem_access_address;
uint64_t mem_aux_info;
uint64_t mem_access_latency;
uint64_t tsx_aux_info;
uint64_t unused_2[INTEL_PT_BLK_ITEM_ID_CNT - 4];
};
uint64_t lbr_0[INTEL_PT_BLK_ITEM_ID_CNT];
uint64_t lbr_1[INTEL_PT_BLK_ITEM_ID_CNT];
uint64_t lbr_2[INTEL_PT_BLK_ITEM_ID_CNT];
uint64_t xmm[INTEL_PT_BLK_ITEM_ID_CNT];
};
};
bool is_32_bit;
};
struct intel_pt_vmcs_info {
struct rb_node rb_node;
uint64_t vmcs;
uint64_t tsc_offset;
bool reliable;
bool error_printed;
};
/*
* Maximum number of event trace data in one go, assuming at most 1 per type
* and 6-bits of type in the EVD packet.
*/
#define INTEL_PT_MAX_EVDS 64
/* Event trace data from EVD packet */
struct intel_pt_evd {
int type;
uint64_t payload;
};
struct intel_pt_state {
enum intel_pt_sample_type type;
bool from_nr;
bool to_nr;
bool from_iflag;
bool to_iflag;
int err;
uint64_t from_ip;
uint64_t to_ip;
uint64_t tot_insn_cnt;
uint64_t tot_cyc_cnt;
uint64_t cycles;
uint64_t timestamp;
uint64_t est_timestamp;
uint64_t trace_nr;
uint64_t ptw_payload;
uint64_t mwait_payload;
uint64_t pwre_payload;
uint64_t pwrx_payload;
uint64_t cbr_payload;
uint64_t psb_offset;
uint32_t cbr;
uint32_t flags;
enum intel_pt_insn_op insn_op;
int insn_len;
char insn[INTEL_PT_INSN_BUF_SZ];
struct intel_pt_blk_items items;
int cfe_type;
int cfe_vector;
int evd_cnt;
struct intel_pt_evd *evd;
};
struct intel_pt_insn;
struct intel_pt_buffer {
const unsigned char *buf;
size_t len;
bool consecutive;
uint64_t ref_timestamp;
uint64_t trace_nr;
};
typedef int (*intel_pt_lookahead_cb_t)(struct intel_pt_buffer *, void *);
struct intel_pt_params {
int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
uint64_t max_insn_cnt, void *data);
bool (*pgd_ip)(uint64_t ip, void *data);
int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
struct intel_pt_vmcs_info *(*findnew_vmcs_info)(void *data, uint64_t vmcs);
void *data;
bool return_compression;
bool branch_enable;
bool vm_time_correlation;
bool vm_tm_corr_dry_run;
uint64_t first_timestamp;
uint64_t ctl;
uint64_t period;
enum intel_pt_period_type period_type;
unsigned max_non_turbo_ratio;
unsigned int mtc_period;
uint32_t tsc_ctc_ratio_n;
uint32_t tsc_ctc_ratio_d;
enum intel_pt_param_flags flags;
unsigned int quick;
int max_loops;
};
struct intel_pt_decoder;
struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params);
void intel_pt_decoder_free(struct intel_pt_decoder *decoder);
const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder);
int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp);
unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
unsigned char *buf_b, size_t len_b,
bool have_tsc, bool *consecutive,
bool ooo_tsc);
int intel_pt__strerror(int code, char *buf, size_t buflen);
void intel_pt_set_first_timestamp(struct intel_pt_decoder *decoder,
uint64_t first_timestamp);
#endif
@@ -0,0 +1,313 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* intel_pt_insn_decoder.c: Intel Processor Trace support
* Copyright (c) 2013-2014, Intel Corporation.
*/
#include <linux/kernel.h>
#include <stdio.h>
#include <string.h>
#include <endian.h>
#include <byteswap.h>
#include "../../../arch/x86/include/asm/insn.h"
#include "event.h"
#include "intel-pt-insn-decoder.h"
#include "dump-insn.h"
#include "util/sample.h"
#if INTEL_PT_INSN_BUF_SZ < MAX_INSN_SIZE || INTEL_PT_INSN_BUF_SZ > MAX_INSN
#error Instruction buffer size too small
#endif
/* Based on branch_type() from arch/x86/events/intel/lbr.c */
static void intel_pt_insn_decoder(struct insn *insn,
struct intel_pt_insn *intel_pt_insn)
{
enum intel_pt_insn_op op = INTEL_PT_OP_OTHER;
enum intel_pt_insn_branch branch = INTEL_PT_BR_NO_BRANCH;
int ext;
intel_pt_insn->rel = 0;
intel_pt_insn->emulated_ptwrite = false;
if (insn_is_avx_or_xop(insn)) {
intel_pt_insn->op = INTEL_PT_OP_OTHER;
intel_pt_insn->branch = INTEL_PT_BR_NO_BRANCH;
intel_pt_insn->length = insn->length;
return;
}
switch (insn->opcode.bytes[0]) {
case 0xf:
switch (insn->opcode.bytes[1]) {
case 0x01:
switch (insn->modrm.bytes[0]) {
case 0xc2: /* vmlaunch */
case 0xc3: /* vmresume */
op = INTEL_PT_OP_VMENTRY;
branch = INTEL_PT_BR_INDIRECT;
break;
case 0xca:
switch (insn->prefixes.bytes[3]) {
case 0xf2: /* erets */
op = INTEL_PT_OP_ERETS;
branch = INTEL_PT_BR_INDIRECT;
break;
case 0xf3: /* eretu */
op = INTEL_PT_OP_ERETU;
branch = INTEL_PT_BR_INDIRECT;
break;
default:
break;
}
break;
default:
break;
}
break;
case 0x05: /* syscall */
case 0x34: /* sysenter */
op = INTEL_PT_OP_SYSCALL;
branch = INTEL_PT_BR_INDIRECT;
break;
case 0x07: /* sysret */
case 0x35: /* sysexit */
op = INTEL_PT_OP_SYSRET;
branch = INTEL_PT_BR_INDIRECT;
break;
case 0x80 ... 0x8f: /* jcc */
op = INTEL_PT_OP_JCC;
branch = INTEL_PT_BR_CONDITIONAL;
break;
default:
break;
}
break;
case 0x70 ... 0x7f: /* jcc */
op = INTEL_PT_OP_JCC;
branch = INTEL_PT_BR_CONDITIONAL;
break;
case 0xa1:
if (insn_is_rex2(insn)) { /* jmpabs */
intel_pt_insn->op = INTEL_PT_OP_JMP;
/* jmpabs causes a TIP packet like an indirect branch */
intel_pt_insn->branch = INTEL_PT_BR_INDIRECT;
intel_pt_insn->length = insn->length;
return;
}
break;
case 0xc2: /* near ret */
case 0xc3: /* near ret */
case 0xca: /* far ret */
case 0xcb: /* far ret */
op = INTEL_PT_OP_RET;
branch = INTEL_PT_BR_INDIRECT;
break;
case 0xcf: /* iret */
op = INTEL_PT_OP_IRET;
branch = INTEL_PT_BR_INDIRECT;
break;
case 0xcc ... 0xce: /* int */
op = INTEL_PT_OP_INT;
branch = INTEL_PT_BR_INDIRECT;
break;
case 0xe8: /* call near rel */
op = INTEL_PT_OP_CALL;
branch = INTEL_PT_BR_UNCONDITIONAL;
break;
case 0x9a: /* call far absolute */
op = INTEL_PT_OP_CALL;
branch = INTEL_PT_BR_INDIRECT;
break;
case 0xe0 ... 0xe2: /* loop */
op = INTEL_PT_OP_LOOP;
branch = INTEL_PT_BR_CONDITIONAL;
break;
case 0xe3: /* jcc */
op = INTEL_PT_OP_JCC;
branch = INTEL_PT_BR_CONDITIONAL;
break;
case 0xe9: /* jmp */
case 0xeb: /* jmp */
op = INTEL_PT_OP_JMP;
branch = INTEL_PT_BR_UNCONDITIONAL;
break;
case 0xea: /* far jmp */
op = INTEL_PT_OP_JMP;
branch = INTEL_PT_BR_INDIRECT;
break;
case 0xff: /* call near absolute, call far absolute ind */
ext = (insn->modrm.bytes[0] >> 3) & 0x7;
switch (ext) {
case 2: /* near ind call */
case 3: /* far ind call */
op = INTEL_PT_OP_CALL;
branch = INTEL_PT_BR_INDIRECT;
break;
case 4:
case 5:
op = INTEL_PT_OP_JMP;
branch = INTEL_PT_BR_INDIRECT;
break;
default:
break;
}
break;
default:
break;
}
intel_pt_insn->op = op;
intel_pt_insn->branch = branch;
intel_pt_insn->length = insn->length;
if (branch == INTEL_PT_BR_CONDITIONAL ||
branch == INTEL_PT_BR_UNCONDITIONAL) {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
switch (insn->immediate.nbytes) {
case 1:
intel_pt_insn->rel = insn->immediate.value;
break;
case 2:
intel_pt_insn->rel =
bswap_16((short)insn->immediate.value);
break;
case 4:
intel_pt_insn->rel = bswap_32(insn->immediate.value);
break;
default:
intel_pt_insn->rel = 0;
break;
}
#else
intel_pt_insn->rel = insn->immediate.value;
#endif
}
}
int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64,
struct intel_pt_insn *intel_pt_insn)
{
struct insn insn;
int ret;
ret = insn_decode(&insn, buf, len,
x86_64 ? INSN_MODE_64 : INSN_MODE_32);
if (ret < 0 || insn.length > len)
return -1;
intel_pt_insn_decoder(&insn, intel_pt_insn);
if (insn.length < INTEL_PT_INSN_BUF_SZ)
memcpy(intel_pt_insn->buf, buf, insn.length);
else
memcpy(intel_pt_insn->buf, buf, INTEL_PT_INSN_BUF_SZ);
return 0;
}
int arch_is_uncond_branch(const unsigned char *buf, size_t len, int x86_64)
{
struct intel_pt_insn in;
if (intel_pt_get_insn(buf, len, x86_64, &in) < 0)
return -1;
return in.branch == INTEL_PT_BR_UNCONDITIONAL ||
in.branch == INTEL_PT_BR_INDIRECT;
}
const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused,
u8 *inbuf, int inlen, int *lenp)
{
struct insn insn;
int n, i, ret;
ret = insn_decode(&insn, inbuf, inlen,
x->is64bit ? INSN_MODE_64 : INSN_MODE_32);
if (ret < 0 || insn.length > inlen)
return "<bad>";
if (lenp)
*lenp = insn.length;
n = scnprintf(x->out, sizeof(x->out), "insn: ");
for (i = 0; i < insn.length; i++)
n += scnprintf(x->out + n, sizeof(x->out) - n, "%02x ", inbuf[i]);
return x->out;
}
const char *branch_name[] = {
[INTEL_PT_OP_OTHER] = "Other",
[INTEL_PT_OP_CALL] = "Call",
[INTEL_PT_OP_RET] = "Ret",
[INTEL_PT_OP_JCC] = "Jcc",
[INTEL_PT_OP_JMP] = "Jmp",
[INTEL_PT_OP_LOOP] = "Loop",
[INTEL_PT_OP_IRET] = "IRet",
[INTEL_PT_OP_INT] = "Int",
[INTEL_PT_OP_SYSCALL] = "Syscall",
[INTEL_PT_OP_SYSRET] = "Sysret",
[INTEL_PT_OP_VMENTRY] = "VMentry",
[INTEL_PT_OP_ERETS] = "Erets",
[INTEL_PT_OP_ERETU] = "Eretu",
};
const char *intel_pt_insn_name(enum intel_pt_insn_op op)
{
return branch_name[op];
}
int intel_pt_insn_desc(const struct intel_pt_insn *intel_pt_insn, char *buf,
size_t buf_len)
{
switch (intel_pt_insn->branch) {
case INTEL_PT_BR_CONDITIONAL:
case INTEL_PT_BR_UNCONDITIONAL:
return snprintf(buf, buf_len, "%s %s%d",
intel_pt_insn_name(intel_pt_insn->op),
intel_pt_insn->rel > 0 ? "+" : "",
intel_pt_insn->rel);
case INTEL_PT_BR_NO_BRANCH:
case INTEL_PT_BR_INDIRECT:
return snprintf(buf, buf_len, "%s",
intel_pt_insn_name(intel_pt_insn->op));
default:
break;
}
return 0;
}
int intel_pt_insn_type(enum intel_pt_insn_op op)
{
switch (op) {
case INTEL_PT_OP_OTHER:
return 0;
case INTEL_PT_OP_CALL:
return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL;
case INTEL_PT_OP_RET:
return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN;
case INTEL_PT_OP_JCC:
return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
case INTEL_PT_OP_JMP:
return PERF_IP_FLAG_BRANCH;
case INTEL_PT_OP_LOOP:
return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
case INTEL_PT_OP_IRET:
case INTEL_PT_OP_ERETS:
case INTEL_PT_OP_ERETU:
return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
PERF_IP_FLAG_INTERRUPT;
case INTEL_PT_OP_INT:
return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
PERF_IP_FLAG_INTERRUPT;
case INTEL_PT_OP_SYSCALL:
return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
PERF_IP_FLAG_SYSCALLRET;
case INTEL_PT_OP_SYSRET:
return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
PERF_IP_FLAG_SYSCALLRET;
case INTEL_PT_OP_VMENTRY:
return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
PERF_IP_FLAG_VMENTRY;
default:
return 0;
}
}
@@ -0,0 +1,58 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* intel_pt_insn_decoder.h: Intel Processor Trace support
* Copyright (c) 2013-2014, Intel Corporation.
*/
#ifndef INCLUDE__INTEL_PT_INSN_DECODER_H__
#define INCLUDE__INTEL_PT_INSN_DECODER_H__
#include <stddef.h>
#include <stdint.h>
#define INTEL_PT_INSN_DESC_MAX 32
#define INTEL_PT_INSN_BUF_SZ 16
enum intel_pt_insn_op {
INTEL_PT_OP_OTHER,
INTEL_PT_OP_CALL,
INTEL_PT_OP_RET,
INTEL_PT_OP_JCC,
INTEL_PT_OP_JMP,
INTEL_PT_OP_LOOP,
INTEL_PT_OP_IRET,
INTEL_PT_OP_INT,
INTEL_PT_OP_SYSCALL,
INTEL_PT_OP_SYSRET,
INTEL_PT_OP_VMENTRY,
INTEL_PT_OP_ERETS,
INTEL_PT_OP_ERETU,
};
enum intel_pt_insn_branch {
INTEL_PT_BR_NO_BRANCH,
INTEL_PT_BR_INDIRECT,
INTEL_PT_BR_CONDITIONAL,
INTEL_PT_BR_UNCONDITIONAL,
};
struct intel_pt_insn {
enum intel_pt_insn_op op;
enum intel_pt_insn_branch branch;
bool emulated_ptwrite;
int length;
int32_t rel;
unsigned char buf[INTEL_PT_INSN_BUF_SZ];
};
int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64,
struct intel_pt_insn *intel_pt_insn);
const char *intel_pt_insn_name(enum intel_pt_insn_op op);
int intel_pt_insn_desc(const struct intel_pt_insn *intel_pt_insn, char *buf,
size_t buf_len);
int intel_pt_insn_type(enum intel_pt_insn_op op);
#endif
@@ -0,0 +1,267 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* intel_pt_log.c: Intel Processor Trace support
* Copyright (c) 2013-2014, Intel Corporation.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <linux/zalloc.h>
#include <linux/kernel.h>
#include "intel-pt-log.h"
#include "intel-pt-insn-decoder.h"
#include "intel-pt-pkt-decoder.h"
#define MAX_LOG_NAME 256
#define DFLT_BUF_SZ (16 * 1024)
struct log_buf {
char *buf;
size_t buf_sz;
size_t head;
bool wrapped;
FILE *backend;
};
static FILE *f;
static char log_name[MAX_LOG_NAME];
bool intel_pt_enable_logging;
static bool intel_pt_dump_log_on_error;
static unsigned int intel_pt_log_on_error_size;
static struct log_buf log_buf;
void *intel_pt_log_fp(void)
{
return f;
}
void intel_pt_log_enable(bool dump_log_on_error, unsigned int log_on_error_size)
{
intel_pt_enable_logging = true;
intel_pt_dump_log_on_error = dump_log_on_error;
intel_pt_log_on_error_size = log_on_error_size;
}
void intel_pt_log_disable(void)
{
if (f)
fflush(f);
intel_pt_enable_logging = false;
}
void intel_pt_log_set_name(const char *name)
{
strncpy(log_name, name, MAX_LOG_NAME - 5);
strcat(log_name, ".log");
}
static void intel_pt_print_data(const unsigned char *buf, int len, uint64_t pos,
int indent)
{
int i;
for (i = 0; i < indent; i++)
fprintf(f, " ");
fprintf(f, " %08" PRIx64 ": ", pos);
for (i = 0; i < len; i++)
fprintf(f, " %02x", buf[i]);
for (; i < 16; i++)
fprintf(f, " ");
fprintf(f, " ");
}
static void intel_pt_print_no_data(uint64_t pos, int indent)
{
int i;
for (i = 0; i < indent; i++)
fprintf(f, " ");
fprintf(f, " %08" PRIx64 ": ", pos);
for (i = 0; i < 16; i++)
fprintf(f, " ");
fprintf(f, " ");
}
static ssize_t log_buf__write(void *cookie, const char *buf, size_t size)
{
struct log_buf *b = cookie;
size_t sz = size;
if (!b->buf)
return size;
while (sz) {
size_t space = b->buf_sz - b->head;
size_t n = min(space, sz);
memcpy(b->buf + b->head, buf, n);
sz -= n;
buf += n;
b->head += n;
if (sz && b->head >= b->buf_sz) {
b->head = 0;
b->wrapped = true;
}
}
return size;
}
static int log_buf__close(void *cookie)
{
struct log_buf *b = cookie;
zfree(&b->buf);
return 0;
}
static FILE *log_buf__open(struct log_buf *b, FILE *backend, unsigned int sz)
{
cookie_io_functions_t fns = {
.write = log_buf__write,
.close = log_buf__close,
};
FILE *file;
memset(b, 0, sizeof(*b));
b->buf_sz = sz;
b->buf = malloc(b->buf_sz);
b->backend = backend;
file = fopencookie(b, "a", fns);
if (!file)
zfree(&b->buf);
return file;
}
static bool remove_first_line(const char **p, size_t *n)
{
for (; *n && **p != '\n'; ++*p, --*n)
;
if (*n) {
*p += 1;
*n -= 1;
return true;
}
return false;
}
static void write_lines(const char *p, size_t n, FILE *fp, bool *remove_first)
{
if (*remove_first)
*remove_first = !remove_first_line(&p, &n);
fwrite(p, n, 1, fp);
}
static void log_buf__dump(struct log_buf *b)
{
bool remove_first = false;
if (!b->buf)
return;
fflush(f); /* Could update b->head and b->wrapped */
fprintf(b->backend, "Dumping debug log buffer\n");
if (b->wrapped) {
remove_first = true;
write_lines(b->buf + b->head, b->buf_sz - b->head, b->backend, &remove_first);
}
write_lines(b->buf, b->head, b->backend, &remove_first);
fprintf(b->backend, "End of debug log buffer dump\n");
b->head = 0;
b->wrapped = false;
}
void intel_pt_log_dump_buf(void)
{
log_buf__dump(&log_buf);
}
static int intel_pt_log_open(void)
{
if (!intel_pt_enable_logging)
return -1;
if (f)
return 0;
if (log_name[0])
f = fopen(log_name, "w+");
else
f = stdout;
if (f && intel_pt_dump_log_on_error)
f = log_buf__open(&log_buf, f, intel_pt_log_on_error_size);
if (!f) {
intel_pt_enable_logging = false;
return -1;
}
return 0;
}
void __intel_pt_log_packet(const struct intel_pt_pkt *packet, int pkt_len,
uint64_t pos, const unsigned char *buf)
{
char desc[INTEL_PT_PKT_DESC_MAX];
if (intel_pt_log_open())
return;
intel_pt_print_data(buf, pkt_len, pos, 0);
intel_pt_pkt_desc(packet, desc, INTEL_PT_PKT_DESC_MAX);
fprintf(f, "%s\n", desc);
}
void __intel_pt_log_insn(struct intel_pt_insn *intel_pt_insn, uint64_t ip)
{
char desc[INTEL_PT_INSN_DESC_MAX];
size_t len = intel_pt_insn->length;
if (intel_pt_log_open())
return;
if (len > INTEL_PT_INSN_BUF_SZ)
len = INTEL_PT_INSN_BUF_SZ;
intel_pt_print_data(intel_pt_insn->buf, len, ip, 8);
if (intel_pt_insn_desc(intel_pt_insn, desc, INTEL_PT_INSN_DESC_MAX) > 0)
fprintf(f, "%s\n", desc);
else
fprintf(f, "Bad instruction!\n");
}
void __intel_pt_log_insn_no_data(struct intel_pt_insn *intel_pt_insn,
uint64_t ip)
{
char desc[INTEL_PT_INSN_DESC_MAX];
if (intel_pt_log_open())
return;
intel_pt_print_no_data(ip, 8);
if (intel_pt_insn_desc(intel_pt_insn, desc, INTEL_PT_INSN_DESC_MAX) > 0)
fprintf(f, "%s\n", desc);
else
fprintf(f, "Bad instruction!\n");
}
void __intel_pt_log(const char *fmt, ...)
{
va_list args;
if (intel_pt_log_open())
return;
va_start(args, fmt);
vfprintf(f, fmt, args);
va_end(args);
}
@@ -0,0 +1,76 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* intel_pt_log.h: Intel Processor Trace support
* Copyright (c) 2013-2014, Intel Corporation.
*/
#ifndef INCLUDE__INTEL_PT_LOG_H__
#define INCLUDE__INTEL_PT_LOG_H__
#include <linux/compiler.h>
#include <stdint.h>
#include <inttypes.h>
struct intel_pt_pkt;
void *intel_pt_log_fp(void);
void intel_pt_log_enable(bool dump_log_on_error, unsigned int log_on_error_size);
void intel_pt_log_disable(void);
void intel_pt_log_set_name(const char *name);
void intel_pt_log_dump_buf(void);
void __intel_pt_log_packet(const struct intel_pt_pkt *packet, int pkt_len,
uint64_t pos, const unsigned char *buf);
struct intel_pt_insn;
void __intel_pt_log_insn(struct intel_pt_insn *intel_pt_insn, uint64_t ip);
void __intel_pt_log_insn_no_data(struct intel_pt_insn *intel_pt_insn,
uint64_t ip);
void __intel_pt_log(const char *fmt, ...) __printf(1, 2);
#define intel_pt_log(fmt, ...) \
do { \
if (intel_pt_enable_logging) \
__intel_pt_log(fmt, ##__VA_ARGS__); \
} while (0)
#define intel_pt_log_packet(arg, ...) \
do { \
if (intel_pt_enable_logging) \
__intel_pt_log_packet(arg, ##__VA_ARGS__); \
} while (0)
#define intel_pt_log_insn(arg, ...) \
do { \
if (intel_pt_enable_logging) \
__intel_pt_log_insn(arg, ##__VA_ARGS__); \
} while (0)
#define intel_pt_log_insn_no_data(arg, ...) \
do { \
if (intel_pt_enable_logging) \
__intel_pt_log_insn_no_data(arg, ##__VA_ARGS__); \
} while (0)
#define x64_fmt "0x%" PRIx64
extern bool intel_pt_enable_logging;
static inline void intel_pt_log_at(const char *msg, uint64_t u)
{
intel_pt_log("%s at " x64_fmt "\n", msg, u);
}
static inline void intel_pt_log_to(const char *msg, uint64_t u)
{
intel_pt_log("%s to " x64_fmt "\n", msg, u);
}
#define intel_pt_log_var(var, fmt) intel_pt_log("%s: " #var " " fmt "\n", __func__, var)
#define intel_pt_log_x32(var) intel_pt_log_var(var, "%#x")
#define intel_pt_log_x64(var) intel_pt_log_var(var, "%#" PRIx64)
#endif
@@ -0,0 +1,791 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* intel_pt_pkt_decoder.c: Intel Processor Trace support
* Copyright (c) 2013-2014, Intel Corporation.
*/
#include <stdio.h>
#include <string.h>
#include <endian.h>
#include <byteswap.h>
#include <linux/kernel.h>
#include <linux/compiler.h>
#include <linux/unaligned.h>
#include "intel-pt-pkt-decoder.h"
#define BIT(n) (1 << (n))
#define BIT63 ((uint64_t)1 << 63)
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define memcpy_le64(d, s, n) do { \
memcpy((d), (s), (n)); \
*(d) = le64_to_cpu(*(d)); \
} while (0)
#else
#define memcpy_le64 memcpy
#endif
static const char * const packet_name[] = {
[INTEL_PT_BAD] = "Bad Packet!",
[INTEL_PT_PAD] = "PAD",
[INTEL_PT_TNT] = "TNT",
[INTEL_PT_TIP_PGD] = "TIP.PGD",
[INTEL_PT_TIP_PGE] = "TIP.PGE",
[INTEL_PT_TSC] = "TSC",
[INTEL_PT_TMA] = "TMA",
[INTEL_PT_MODE_EXEC] = "MODE.Exec",
[INTEL_PT_MODE_TSX] = "MODE.TSX",
[INTEL_PT_MTC] = "MTC",
[INTEL_PT_TIP] = "TIP",
[INTEL_PT_FUP] = "FUP",
[INTEL_PT_CYC] = "CYC",
[INTEL_PT_VMCS] = "VMCS",
[INTEL_PT_PSB] = "PSB",
[INTEL_PT_PSBEND] = "PSBEND",
[INTEL_PT_CBR] = "CBR",
[INTEL_PT_TRACESTOP] = "TraceSTOP",
[INTEL_PT_PIP] = "PIP",
[INTEL_PT_OVF] = "OVF",
[INTEL_PT_MNT] = "MNT",
[INTEL_PT_PTWRITE] = "PTWRITE",
[INTEL_PT_PTWRITE_IP] = "PTWRITE",
[INTEL_PT_EXSTOP] = "EXSTOP",
[INTEL_PT_EXSTOP_IP] = "EXSTOP",
[INTEL_PT_MWAIT] = "MWAIT",
[INTEL_PT_PWRE] = "PWRE",
[INTEL_PT_PWRX] = "PWRX",
[INTEL_PT_BBP] = "BBP",
[INTEL_PT_BIP] = "BIP",
[INTEL_PT_BEP] = "BEP",
[INTEL_PT_BEP_IP] = "BEP",
[INTEL_PT_CFE] = "CFE",
[INTEL_PT_CFE_IP] = "CFE",
[INTEL_PT_EVD] = "EVD",
};
const char *intel_pt_pkt_name(enum intel_pt_pkt_type type)
{
return packet_name[type];
}
static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
uint64_t payload;
int count;
if (len < 8)
return INTEL_PT_NEED_MORE_BYTES;
payload = get_unaligned_le64(buf);
for (count = 47; count; count--) {
if (payload & BIT63)
break;
payload <<= 1;
}
packet->type = INTEL_PT_TNT;
packet->count = count;
packet->payload = payload << 1;
return 8;
}
static int intel_pt_get_pip(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
uint64_t payload = 0;
if (len < 8)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_PIP;
memcpy_le64(&payload, buf + 2, 6);
packet->payload = payload;
return 8;
}
static int intel_pt_get_tracestop(struct intel_pt_pkt *packet)
{
packet->type = INTEL_PT_TRACESTOP;
return 2;
}
static int intel_pt_get_cbr(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 4)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_CBR;
packet->payload = get_unaligned_le16(buf + 2);
return 4;
}
static int intel_pt_get_vmcs(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 7)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_VMCS;
packet->count = 5;
memcpy_le64(&packet->payload, buf + 2, 5);
return 7;
}
static int intel_pt_get_ovf(struct intel_pt_pkt *packet)
{
packet->type = INTEL_PT_OVF;
return 2;
}
static int intel_pt_get_psb(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
int i;
if (len < 16)
return INTEL_PT_NEED_MORE_BYTES;
for (i = 2; i < 16; i += 2) {
if (buf[i] != 2 || buf[i + 1] != 0x82)
return INTEL_PT_BAD_PACKET;
}
packet->type = INTEL_PT_PSB;
return 16;
}
static int intel_pt_get_psbend(struct intel_pt_pkt *packet)
{
packet->type = INTEL_PT_PSBEND;
return 2;
}
static int intel_pt_get_tma(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 7)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_TMA;
packet->payload = buf[2] | (buf[3] << 8);
packet->count = buf[5] | ((buf[6] & BIT(0)) << 8);
return 7;
}
static int intel_pt_get_pad(struct intel_pt_pkt *packet)
{
packet->type = INTEL_PT_PAD;
return 1;
}
static int intel_pt_get_mnt(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 11)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_MNT;
packet->payload = get_unaligned_le64(buf + 3);
return 11;
}
static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 3)
return INTEL_PT_NEED_MORE_BYTES;
switch (buf[2]) {
case 0x88: /* MNT */
return intel_pt_get_mnt(buf, len, packet);
default:
return INTEL_PT_BAD_PACKET;
}
}
static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
packet->count = (buf[1] >> 5) & 0x3;
packet->type = buf[1] & BIT(7) ? INTEL_PT_PTWRITE_IP :
INTEL_PT_PTWRITE;
switch (packet->count) {
case 0:
if (len < 6)
return INTEL_PT_NEED_MORE_BYTES;
packet->payload = get_unaligned_le32(buf + 2);
return 6;
case 1:
if (len < 10)
return INTEL_PT_NEED_MORE_BYTES;
packet->payload = get_unaligned_le64(buf + 2);
return 10;
default:
return INTEL_PT_BAD_PACKET;
}
}
static int intel_pt_get_exstop(struct intel_pt_pkt *packet)
{
packet->type = INTEL_PT_EXSTOP;
return 2;
}
static int intel_pt_get_exstop_ip(struct intel_pt_pkt *packet)
{
packet->type = INTEL_PT_EXSTOP_IP;
return 2;
}
static int intel_pt_get_mwait(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 10)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_MWAIT;
packet->payload = get_unaligned_le64(buf + 2);
return 10;
}
static int intel_pt_get_pwre(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 4)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_PWRE;
memcpy_le64(&packet->payload, buf + 2, 2);
return 4;
}
static int intel_pt_get_pwrx(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 7)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_PWRX;
memcpy_le64(&packet->payload, buf + 2, 5);
return 7;
}
static int intel_pt_get_bbp(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 3)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_BBP;
packet->count = buf[2] >> 7;
packet->payload = buf[2] & 0x1f;
return 3;
}
static int intel_pt_get_bip_4(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 5)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_BIP;
packet->count = buf[0] >> 3;
memcpy_le64(&packet->payload, buf + 1, 4);
return 5;
}
static int intel_pt_get_bip_8(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 9)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_BIP;
packet->count = buf[0] >> 3;
packet->payload = get_unaligned_le64(buf + 1);
return 9;
}
static int intel_pt_get_bep(size_t len, struct intel_pt_pkt *packet)
{
if (len < 2)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_BEP;
return 2;
}
static int intel_pt_get_bep_ip(size_t len, struct intel_pt_pkt *packet)
{
if (len < 2)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_BEP_IP;
return 2;
}
static int intel_pt_get_cfe(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 4)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = buf[2] & 0x80 ? INTEL_PT_CFE_IP : INTEL_PT_CFE;
packet->count = buf[2] & 0x1f;
packet->payload = buf[3];
return 4;
}
static int intel_pt_get_evd(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 11)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_EVD;
packet->count = buf[2] & 0x3f;
packet->payload = buf[3];
packet->payload = get_unaligned_le64(buf + 3);
return 11;
}
static int intel_pt_get_ext(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 2)
return INTEL_PT_NEED_MORE_BYTES;
if ((buf[1] & 0x1f) == 0x12)
return intel_pt_get_ptwrite(buf, len, packet);
switch (buf[1]) {
case 0xa3: /* Long TNT */
return intel_pt_get_long_tnt(buf, len, packet);
case 0x43: /* PIP */
return intel_pt_get_pip(buf, len, packet);
case 0x83: /* TraceStop */
return intel_pt_get_tracestop(packet);
case 0x03: /* CBR */
return intel_pt_get_cbr(buf, len, packet);
case 0xc8: /* VMCS */
return intel_pt_get_vmcs(buf, len, packet);
case 0xf3: /* OVF */
return intel_pt_get_ovf(packet);
case 0x82: /* PSB */
return intel_pt_get_psb(buf, len, packet);
case 0x23: /* PSBEND */
return intel_pt_get_psbend(packet);
case 0x73: /* TMA */
return intel_pt_get_tma(buf, len, packet);
case 0xC3: /* 3-byte header */
return intel_pt_get_3byte(buf, len, packet);
case 0x62: /* EXSTOP no IP */
return intel_pt_get_exstop(packet);
case 0xE2: /* EXSTOP with IP */
return intel_pt_get_exstop_ip(packet);
case 0xC2: /* MWAIT */
return intel_pt_get_mwait(buf, len, packet);
case 0x22: /* PWRE */
return intel_pt_get_pwre(buf, len, packet);
case 0xA2: /* PWRX */
return intel_pt_get_pwrx(buf, len, packet);
case 0x63: /* BBP */
return intel_pt_get_bbp(buf, len, packet);
case 0x33: /* BEP no IP */
return intel_pt_get_bep(len, packet);
case 0xb3: /* BEP with IP */
return intel_pt_get_bep_ip(len, packet);
case 0x13: /* CFE */
return intel_pt_get_cfe(buf, len, packet);
case 0x53: /* EVD */
return intel_pt_get_evd(buf, len, packet);
default:
return INTEL_PT_BAD_PACKET;
}
}
static int intel_pt_get_short_tnt(unsigned int byte,
struct intel_pt_pkt *packet)
{
int count;
for (count = 6; count; count--) {
if (byte & BIT(7))
break;
byte <<= 1;
}
packet->type = INTEL_PT_TNT;
packet->count = count;
packet->payload = (uint64_t)byte << 57;
return 1;
}
static int intel_pt_get_cyc(unsigned int byte, const unsigned char *buf,
size_t len, struct intel_pt_pkt *packet)
{
unsigned int offs = 1, shift;
uint64_t payload = byte >> 3;
byte >>= 2;
len -= 1;
for (shift = 5; byte & 1; shift += 7) {
if (offs > 9)
return INTEL_PT_BAD_PACKET;
if (len < offs)
return INTEL_PT_NEED_MORE_BYTES;
byte = buf[offs++];
payload |= ((uint64_t)byte >> 1) << shift;
}
packet->type = INTEL_PT_CYC;
packet->payload = payload;
return offs;
}
static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
int ip_len;
packet->count = byte >> 5;
switch (packet->count) {
case 0:
ip_len = 0;
break;
case 1:
if (len < 3)
return INTEL_PT_NEED_MORE_BYTES;
ip_len = 2;
packet->payload = get_unaligned_le16(buf + 1);
break;
case 2:
if (len < 5)
return INTEL_PT_NEED_MORE_BYTES;
ip_len = 4;
packet->payload = get_unaligned_le32(buf + 1);
break;
case 3:
case 4:
if (len < 7)
return INTEL_PT_NEED_MORE_BYTES;
ip_len = 6;
memcpy_le64(&packet->payload, buf + 1, 6);
break;
case 6:
if (len < 9)
return INTEL_PT_NEED_MORE_BYTES;
ip_len = 8;
packet->payload = get_unaligned_le64(buf + 1);
break;
default:
return INTEL_PT_BAD_PACKET;
}
packet->type = type;
return ip_len + 1;
}
static int intel_pt_get_mode(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 2)
return INTEL_PT_NEED_MORE_BYTES;
switch (buf[1] >> 5) {
case 0:
packet->type = INTEL_PT_MODE_EXEC;
packet->count = buf[1];
switch (buf[1] & 3) {
case 0:
packet->payload = 16;
break;
case 1:
packet->payload = 64;
break;
case 2:
packet->payload = 32;
break;
default:
return INTEL_PT_BAD_PACKET;
}
break;
case 1:
packet->type = INTEL_PT_MODE_TSX;
if ((buf[1] & 3) == 3)
return INTEL_PT_BAD_PACKET;
packet->payload = buf[1] & 3;
break;
default:
return INTEL_PT_BAD_PACKET;
}
return 2;
}
static int intel_pt_get_tsc(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 8)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_TSC;
memcpy_le64(&packet->payload, buf + 1, 7);
return 8;
}
static int intel_pt_get_mtc(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet)
{
if (len < 2)
return INTEL_PT_NEED_MORE_BYTES;
packet->type = INTEL_PT_MTC;
packet->payload = buf[1];
return 2;
}
static int intel_pt_do_get_packet(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet,
enum intel_pt_pkt_ctx ctx)
{
unsigned int byte;
memset(packet, 0, sizeof(struct intel_pt_pkt));
if (!len)
return INTEL_PT_NEED_MORE_BYTES;
byte = buf[0];
switch (ctx) {
case INTEL_PT_NO_CTX:
break;
case INTEL_PT_BLK_4_CTX:
if ((byte & 0x7) == 4)
return intel_pt_get_bip_4(buf, len, packet);
break;
case INTEL_PT_BLK_8_CTX:
if ((byte & 0x7) == 4)
return intel_pt_get_bip_8(buf, len, packet);
break;
default:
break;
}
if (!(byte & BIT(0))) {
if (byte == 0)
return intel_pt_get_pad(packet);
if (byte == 2)
return intel_pt_get_ext(buf, len, packet);
return intel_pt_get_short_tnt(byte, packet);
}
if ((byte & 2))
return intel_pt_get_cyc(byte, buf, len, packet);
switch (byte & 0x1f) {
case 0x0D:
return intel_pt_get_ip(INTEL_PT_TIP, byte, buf, len, packet);
case 0x11:
return intel_pt_get_ip(INTEL_PT_TIP_PGE, byte, buf, len,
packet);
case 0x01:
return intel_pt_get_ip(INTEL_PT_TIP_PGD, byte, buf, len,
packet);
case 0x1D:
return intel_pt_get_ip(INTEL_PT_FUP, byte, buf, len, packet);
case 0x19:
switch (byte) {
case 0x99:
return intel_pt_get_mode(buf, len, packet);
case 0x19:
return intel_pt_get_tsc(buf, len, packet);
case 0x59:
return intel_pt_get_mtc(buf, len, packet);
default:
return INTEL_PT_BAD_PACKET;
}
default:
return INTEL_PT_BAD_PACKET;
}
}
void intel_pt_upd_pkt_ctx(const struct intel_pt_pkt *packet,
enum intel_pt_pkt_ctx *ctx)
{
switch (packet->type) {
case INTEL_PT_BAD:
case INTEL_PT_PAD:
case INTEL_PT_TSC:
case INTEL_PT_TMA:
case INTEL_PT_MTC:
case INTEL_PT_FUP:
case INTEL_PT_CYC:
case INTEL_PT_CBR:
case INTEL_PT_MNT:
case INTEL_PT_EXSTOP:
case INTEL_PT_EXSTOP_IP:
case INTEL_PT_PWRE:
case INTEL_PT_PWRX:
case INTEL_PT_BIP:
break;
case INTEL_PT_TNT:
case INTEL_PT_TIP:
case INTEL_PT_TIP_PGD:
case INTEL_PT_TIP_PGE:
case INTEL_PT_MODE_EXEC:
case INTEL_PT_MODE_TSX:
case INTEL_PT_PIP:
case INTEL_PT_OVF:
case INTEL_PT_VMCS:
case INTEL_PT_TRACESTOP:
case INTEL_PT_PSB:
case INTEL_PT_PSBEND:
case INTEL_PT_PTWRITE:
case INTEL_PT_PTWRITE_IP:
case INTEL_PT_MWAIT:
case INTEL_PT_BEP:
case INTEL_PT_BEP_IP:
case INTEL_PT_CFE:
case INTEL_PT_CFE_IP:
case INTEL_PT_EVD:
*ctx = INTEL_PT_NO_CTX;
break;
case INTEL_PT_BBP:
if (packet->count)
*ctx = INTEL_PT_BLK_4_CTX;
else
*ctx = INTEL_PT_BLK_8_CTX;
break;
default:
break;
}
}
int intel_pt_get_packet(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet, enum intel_pt_pkt_ctx *ctx)
{
int ret;
ret = intel_pt_do_get_packet(buf, len, packet, *ctx);
if (ret > 0) {
while (ret < 8 && len > (size_t)ret && !buf[ret])
ret += 1;
intel_pt_upd_pkt_ctx(packet, ctx);
}
return ret;
}
int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf,
size_t buf_len)
{
int ret, i, nr;
unsigned long long payload = packet->payload;
const char *name = intel_pt_pkt_name(packet->type);
switch (packet->type) {
case INTEL_PT_BAD:
case INTEL_PT_PAD:
case INTEL_PT_PSB:
case INTEL_PT_PSBEND:
case INTEL_PT_TRACESTOP:
case INTEL_PT_OVF:
return snprintf(buf, buf_len, "%s", name);
case INTEL_PT_TNT: {
size_t blen = buf_len;
ret = snprintf(buf, blen, "%s ", name);
if (ret < 0)
return ret;
buf += ret;
blen -= ret;
for (i = 0; i < packet->count; i++) {
if (payload & BIT63)
ret = snprintf(buf, blen, "T");
else
ret = snprintf(buf, blen, "N");
if (ret < 0)
return ret;
buf += ret;
blen -= ret;
payload <<= 1;
}
ret = snprintf(buf, blen, " (%d)", packet->count);
if (ret < 0)
return ret;
blen -= ret;
return buf_len - blen;
}
case INTEL_PT_TIP_PGD:
case INTEL_PT_TIP_PGE:
case INTEL_PT_TIP:
case INTEL_PT_FUP:
if (!(packet->count))
return snprintf(buf, buf_len, "%s no ip", name);
fallthrough;
case INTEL_PT_CYC:
case INTEL_PT_VMCS:
case INTEL_PT_MTC:
case INTEL_PT_MNT:
case INTEL_PT_CBR:
case INTEL_PT_TSC:
return snprintf(buf, buf_len, "%s 0x%llx", name, payload);
case INTEL_PT_TMA:
return snprintf(buf, buf_len, "%s CTC 0x%x FC 0x%x", name,
(unsigned)payload, packet->count);
case INTEL_PT_MODE_EXEC:
return snprintf(buf, buf_len, "%s IF:%d %lld",
name, !!(packet->count & 4), payload);
case INTEL_PT_MODE_TSX:
return snprintf(buf, buf_len, "%s TXAbort:%u InTX:%u",
name, (unsigned)(payload >> 1) & 1,
(unsigned)payload & 1);
case INTEL_PT_PIP:
nr = packet->payload & INTEL_PT_VMX_NR_FLAG ? 1 : 0;
payload &= ~INTEL_PT_VMX_NR_FLAG;
ret = snprintf(buf, buf_len, "%s 0x%llx (NR=%d)",
name, payload >> 1, nr);
return ret;
case INTEL_PT_PTWRITE:
return snprintf(buf, buf_len, "%s 0x%llx IP:0", name, payload);
case INTEL_PT_PTWRITE_IP:
return snprintf(buf, buf_len, "%s 0x%llx IP:1", name, payload);
case INTEL_PT_BEP:
case INTEL_PT_EXSTOP:
return snprintf(buf, buf_len, "%s IP:0", name);
case INTEL_PT_BEP_IP:
case INTEL_PT_EXSTOP_IP:
return snprintf(buf, buf_len, "%s IP:1", name);
case INTEL_PT_MWAIT:
return snprintf(buf, buf_len, "%s 0x%llx Hints 0x%x Extensions 0x%x",
name, payload, (unsigned int)(payload & 0xff),
(unsigned int)((payload >> 32) & 0x3));
case INTEL_PT_PWRE:
return snprintf(buf, buf_len, "%s 0x%llx HW:%u CState:%u Sub-CState:%u",
name, payload, !!(payload & 0x80),
(unsigned int)((payload >> 12) & 0xf),
(unsigned int)((payload >> 8) & 0xf));
case INTEL_PT_PWRX:
return snprintf(buf, buf_len, "%s 0x%llx Last CState:%u Deepest CState:%u Wake Reason 0x%x",
name, payload,
(unsigned int)((payload >> 4) & 0xf),
(unsigned int)(payload & 0xf),
(unsigned int)((payload >> 8) & 0xf));
case INTEL_PT_BBP:
return snprintf(buf, buf_len, "%s SZ %s-byte Type 0x%llx",
name, packet->count ? "4" : "8", payload);
case INTEL_PT_BIP:
return snprintf(buf, buf_len, "%s ID 0x%02x Value 0x%llx",
name, packet->count, payload);
case INTEL_PT_CFE:
case INTEL_PT_CFE_IP:
return snprintf(buf, buf_len, "%s IP:%d Type 0x%02x Vector 0x%llx",
name, packet->type == INTEL_PT_CFE_IP, packet->count, payload);
case INTEL_PT_EVD:
return snprintf(buf, buf_len, "%s Type 0x%02x Payload 0x%llx",
name, packet->count, payload);
default:
break;
}
return snprintf(buf, buf_len, "%s 0x%llx (%d)",
name, payload, packet->count);
}
@@ -0,0 +1,92 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* intel_pt_pkt_decoder.h: Intel Processor Trace support
* Copyright (c) 2013-2014, Intel Corporation.
*/
#ifndef INCLUDE__INTEL_PT_PKT_DECODER_H__
#define INCLUDE__INTEL_PT_PKT_DECODER_H__
#include <stddef.h>
#include <stdint.h>
#define INTEL_PT_PKT_DESC_MAX 256
#define INTEL_PT_NEED_MORE_BYTES -1
#define INTEL_PT_BAD_PACKET -2
#define INTEL_PT_PSB_STR "\002\202\002\202\002\202\002\202" \
"\002\202\002\202\002\202\002\202"
#define INTEL_PT_PSB_LEN 16
#define INTEL_PT_PKT_MAX_SZ 16
#define INTEL_PT_VMX_NR_FLAG 1
enum intel_pt_pkt_type {
INTEL_PT_BAD,
INTEL_PT_PAD,
INTEL_PT_TNT,
INTEL_PT_TIP_PGD,
INTEL_PT_TIP_PGE,
INTEL_PT_TSC,
INTEL_PT_TMA,
INTEL_PT_MODE_EXEC,
INTEL_PT_MODE_TSX,
INTEL_PT_MTC,
INTEL_PT_TIP,
INTEL_PT_FUP,
INTEL_PT_CYC,
INTEL_PT_VMCS,
INTEL_PT_PSB,
INTEL_PT_PSBEND,
INTEL_PT_CBR,
INTEL_PT_TRACESTOP,
INTEL_PT_PIP,
INTEL_PT_OVF,
INTEL_PT_MNT,
INTEL_PT_PTWRITE,
INTEL_PT_PTWRITE_IP,
INTEL_PT_EXSTOP,
INTEL_PT_EXSTOP_IP,
INTEL_PT_MWAIT,
INTEL_PT_PWRE,
INTEL_PT_PWRX,
INTEL_PT_BBP,
INTEL_PT_BIP,
INTEL_PT_BEP,
INTEL_PT_BEP_IP,
INTEL_PT_CFE,
INTEL_PT_CFE_IP,
INTEL_PT_EVD,
};
struct intel_pt_pkt {
enum intel_pt_pkt_type type;
int count;
uint64_t payload;
};
/*
* Decoding of BIP packets conflicts with single-byte TNT packets. Since BIP
* packets only occur in the context of a block (i.e. between BBP and BEP), that
* context must be recorded and passed to the packet decoder.
*/
enum intel_pt_pkt_ctx {
INTEL_PT_NO_CTX, /* BIP packets are invalid */
INTEL_PT_BLK_4_CTX, /* 4-byte BIP packets */
INTEL_PT_BLK_8_CTX, /* 8-byte BIP packets */
};
const char *intel_pt_pkt_name(enum intel_pt_pkt_type);
int intel_pt_get_packet(const unsigned char *buf, size_t len,
struct intel_pt_pkt *packet,
enum intel_pt_pkt_ctx *ctx);
void intel_pt_upd_pkt_ctx(const struct intel_pt_pkt *packet,
enum intel_pt_pkt_ctx *ctx);
int intel_pt_pkt_desc(const struct intel_pt_pkt *packet, char *buf, size_t len);
#endif