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