2 * intel_pt_insn_decoder.c: Intel Processor Trace support
3 * Copyright (c) 2013-2014, Intel Corporation.
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.
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
28 #include "intel-pt-insn-decoder.h"
29 #include "dump-insn.h"
31 #if INTEL_PT_INSN_BUF_SZ < MAX_INSN_SIZE || INTEL_PT_INSN_BUF_SZ > MAX_INSN
32 #error Instruction buffer size too small
35 /* Based on branch_type() from arch/x86/events/intel/lbr.c */
36 static void intel_pt_insn_decoder(struct insn *insn,
37 struct intel_pt_insn *intel_pt_insn)
39 enum intel_pt_insn_op op = INTEL_PT_OP_OTHER;
40 enum intel_pt_insn_branch branch = INTEL_PT_BR_NO_BRANCH;
43 intel_pt_insn->rel = 0;
45 if (insn_is_avx(insn)) {
46 intel_pt_insn->op = INTEL_PT_OP_OTHER;
47 intel_pt_insn->branch = INTEL_PT_BR_NO_BRANCH;
48 intel_pt_insn->length = insn->length;
52 switch (insn->opcode.bytes[0]) {
54 switch (insn->opcode.bytes[1]) {
55 case 0x05: /* syscall */
56 case 0x34: /* sysenter */
57 op = INTEL_PT_OP_SYSCALL;
58 branch = INTEL_PT_BR_INDIRECT;
60 case 0x07: /* sysret */
61 case 0x35: /* sysexit */
62 op = INTEL_PT_OP_SYSRET;
63 branch = INTEL_PT_BR_INDIRECT;
65 case 0x80 ... 0x8f: /* jcc */
67 branch = INTEL_PT_BR_CONDITIONAL;
73 case 0x70 ... 0x7f: /* jcc */
75 branch = INTEL_PT_BR_CONDITIONAL;
77 case 0xc2: /* near ret */
78 case 0xc3: /* near ret */
79 case 0xca: /* far ret */
80 case 0xcb: /* far ret */
82 branch = INTEL_PT_BR_INDIRECT;
85 op = INTEL_PT_OP_IRET;
86 branch = INTEL_PT_BR_INDIRECT;
88 case 0xcc ... 0xce: /* int */
90 branch = INTEL_PT_BR_INDIRECT;
92 case 0xe8: /* call near rel */
93 op = INTEL_PT_OP_CALL;
94 branch = INTEL_PT_BR_UNCONDITIONAL;
96 case 0x9a: /* call far absolute */
97 op = INTEL_PT_OP_CALL;
98 branch = INTEL_PT_BR_INDIRECT;
100 case 0xe0 ... 0xe2: /* loop */
101 op = INTEL_PT_OP_LOOP;
102 branch = INTEL_PT_BR_CONDITIONAL;
105 op = INTEL_PT_OP_JCC;
106 branch = INTEL_PT_BR_CONDITIONAL;
110 op = INTEL_PT_OP_JMP;
111 branch = INTEL_PT_BR_UNCONDITIONAL;
113 case 0xea: /* far jmp */
114 op = INTEL_PT_OP_JMP;
115 branch = INTEL_PT_BR_INDIRECT;
117 case 0xff: /* call near absolute, call far absolute ind */
118 ext = (insn->modrm.bytes[0] >> 3) & 0x7;
120 case 2: /* near ind call */
121 case 3: /* far ind call */
122 op = INTEL_PT_OP_CALL;
123 branch = INTEL_PT_BR_INDIRECT;
127 op = INTEL_PT_OP_JMP;
128 branch = INTEL_PT_BR_INDIRECT;
138 intel_pt_insn->op = op;
139 intel_pt_insn->branch = branch;
140 intel_pt_insn->length = insn->length;
142 if (branch == INTEL_PT_BR_CONDITIONAL ||
143 branch == INTEL_PT_BR_UNCONDITIONAL) {
144 #if __BYTE_ORDER == __BIG_ENDIAN
145 switch (insn->immediate.nbytes) {
147 intel_pt_insn->rel = insn->immediate.value;
151 bswap_16((short)insn->immediate.value);
154 intel_pt_insn->rel = bswap_32(insn->immediate.value);
157 intel_pt_insn->rel = 0;
161 intel_pt_insn->rel = insn->immediate.value;
166 int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64,
167 struct intel_pt_insn *intel_pt_insn)
171 insn_init(&insn, buf, len, x86_64);
172 insn_get_length(&insn);
173 if (!insn_complete(&insn) || insn.length > len)
175 intel_pt_insn_decoder(&insn, intel_pt_insn);
176 if (insn.length < INTEL_PT_INSN_BUF_SZ)
177 memcpy(intel_pt_insn->buf, buf, insn.length);
179 memcpy(intel_pt_insn->buf, buf, INTEL_PT_INSN_BUF_SZ);
183 int arch_is_branch(const unsigned char *buf, size_t len, int x86_64)
185 struct intel_pt_insn in;
186 if (intel_pt_get_insn(buf, len, x86_64, &in) < 0)
188 return in.branch != INTEL_PT_BR_NO_BRANCH;
191 const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused,
192 u8 *inbuf, int inlen, int *lenp)
198 insn_init(&insn, inbuf, inlen, x->is64bit);
199 insn_get_length(&insn);
200 if (!insn_complete(&insn) || insn.length > inlen)
204 left = sizeof(x->out);
205 n = snprintf(x->out, left, "insn: ");
207 for (i = 0; i < insn.length; i++) {
208 n += snprintf(x->out + n, left, "%02x ", inbuf[i]);
214 const char *branch_name[] = {
215 [INTEL_PT_OP_OTHER] = "Other",
216 [INTEL_PT_OP_CALL] = "Call",
217 [INTEL_PT_OP_RET] = "Ret",
218 [INTEL_PT_OP_JCC] = "Jcc",
219 [INTEL_PT_OP_JMP] = "Jmp",
220 [INTEL_PT_OP_LOOP] = "Loop",
221 [INTEL_PT_OP_IRET] = "IRet",
222 [INTEL_PT_OP_INT] = "Int",
223 [INTEL_PT_OP_SYSCALL] = "Syscall",
224 [INTEL_PT_OP_SYSRET] = "Sysret",
227 const char *intel_pt_insn_name(enum intel_pt_insn_op op)
229 return branch_name[op];
232 int intel_pt_insn_desc(const struct intel_pt_insn *intel_pt_insn, char *buf,
235 switch (intel_pt_insn->branch) {
236 case INTEL_PT_BR_CONDITIONAL:
237 case INTEL_PT_BR_UNCONDITIONAL:
238 return snprintf(buf, buf_len, "%s %s%d",
239 intel_pt_insn_name(intel_pt_insn->op),
240 intel_pt_insn->rel > 0 ? "+" : "",
242 case INTEL_PT_BR_NO_BRANCH:
243 case INTEL_PT_BR_INDIRECT:
244 return snprintf(buf, buf_len, "%s",
245 intel_pt_insn_name(intel_pt_insn->op));
252 int intel_pt_insn_type(enum intel_pt_insn_op op)
255 case INTEL_PT_OP_OTHER:
257 case INTEL_PT_OP_CALL:
258 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL;
259 case INTEL_PT_OP_RET:
260 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN;
261 case INTEL_PT_OP_JCC:
262 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
263 case INTEL_PT_OP_JMP:
264 return PERF_IP_FLAG_BRANCH;
265 case INTEL_PT_OP_LOOP:
266 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
267 case INTEL_PT_OP_IRET:
268 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
269 PERF_IP_FLAG_INTERRUPT;
270 case INTEL_PT_OP_INT:
271 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
272 PERF_IP_FLAG_INTERRUPT;
273 case INTEL_PT_OP_SYSCALL:
274 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
275 PERF_IP_FLAG_SYSCALLRET;
276 case INTEL_PT_OP_SYSRET:
277 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
278 PERF_IP_FLAG_SYSCALLRET;