1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_pt_insn_decoder.c: Intel Processor Trace support
4 * Copyright (c) 2013-2014, Intel Corporation.
7 #include <linux/kernel.h>
12 #include "../../../arch/x86/include/asm/insn.h"
14 #include "../../../arch/x86/lib/inat.c"
15 #include "../../../arch/x86/lib/insn.c"
19 #include "intel-pt-insn-decoder.h"
20 #include "dump-insn.h"
22 #if INTEL_PT_INSN_BUF_SZ < MAX_INSN_SIZE || INTEL_PT_INSN_BUF_SZ > MAX_INSN
23 #error Instruction buffer size too small
26 /* Based on branch_type() from arch/x86/events/intel/lbr.c */
27 static void intel_pt_insn_decoder(struct insn *insn,
28 struct intel_pt_insn *intel_pt_insn)
30 enum intel_pt_insn_op op = INTEL_PT_OP_OTHER;
31 enum intel_pt_insn_branch branch = INTEL_PT_BR_NO_BRANCH;
34 intel_pt_insn->rel = 0;
36 if (insn_is_avx(insn)) {
37 intel_pt_insn->op = INTEL_PT_OP_OTHER;
38 intel_pt_insn->branch = INTEL_PT_BR_NO_BRANCH;
39 intel_pt_insn->length = insn->length;
43 switch (insn->opcode.bytes[0]) {
45 switch (insn->opcode.bytes[1]) {
47 switch (insn->modrm.bytes[0]) {
48 case 0xc2: /* vmlaunch */
49 case 0xc3: /* vmresume */
50 op = INTEL_PT_OP_VMENTRY;
51 branch = INTEL_PT_BR_INDIRECT;
57 case 0x05: /* syscall */
58 case 0x34: /* sysenter */
59 op = INTEL_PT_OP_SYSCALL;
60 branch = INTEL_PT_BR_INDIRECT;
62 case 0x07: /* sysret */
63 case 0x35: /* sysexit */
64 op = INTEL_PT_OP_SYSRET;
65 branch = INTEL_PT_BR_INDIRECT;
67 case 0x80 ... 0x8f: /* jcc */
69 branch = INTEL_PT_BR_CONDITIONAL;
75 case 0x70 ... 0x7f: /* jcc */
77 branch = INTEL_PT_BR_CONDITIONAL;
79 case 0xc2: /* near ret */
80 case 0xc3: /* near ret */
81 case 0xca: /* far ret */
82 case 0xcb: /* far ret */
84 branch = INTEL_PT_BR_INDIRECT;
87 op = INTEL_PT_OP_IRET;
88 branch = INTEL_PT_BR_INDIRECT;
90 case 0xcc ... 0xce: /* int */
92 branch = INTEL_PT_BR_INDIRECT;
94 case 0xe8: /* call near rel */
95 op = INTEL_PT_OP_CALL;
96 branch = INTEL_PT_BR_UNCONDITIONAL;
98 case 0x9a: /* call far absolute */
99 op = INTEL_PT_OP_CALL;
100 branch = INTEL_PT_BR_INDIRECT;
102 case 0xe0 ... 0xe2: /* loop */
103 op = INTEL_PT_OP_LOOP;
104 branch = INTEL_PT_BR_CONDITIONAL;
107 op = INTEL_PT_OP_JCC;
108 branch = INTEL_PT_BR_CONDITIONAL;
112 op = INTEL_PT_OP_JMP;
113 branch = INTEL_PT_BR_UNCONDITIONAL;
115 case 0xea: /* far jmp */
116 op = INTEL_PT_OP_JMP;
117 branch = INTEL_PT_BR_INDIRECT;
119 case 0xff: /* call near absolute, call far absolute ind */
120 ext = (insn->modrm.bytes[0] >> 3) & 0x7;
122 case 2: /* near ind call */
123 case 3: /* far ind call */
124 op = INTEL_PT_OP_CALL;
125 branch = INTEL_PT_BR_INDIRECT;
129 op = INTEL_PT_OP_JMP;
130 branch = INTEL_PT_BR_INDIRECT;
140 intel_pt_insn->op = op;
141 intel_pt_insn->branch = branch;
142 intel_pt_insn->length = insn->length;
144 if (branch == INTEL_PT_BR_CONDITIONAL ||
145 branch == INTEL_PT_BR_UNCONDITIONAL) {
146 #if __BYTE_ORDER == __BIG_ENDIAN
147 switch (insn->immediate.nbytes) {
149 intel_pt_insn->rel = insn->immediate.value;
153 bswap_16((short)insn->immediate.value);
156 intel_pt_insn->rel = bswap_32(insn->immediate.value);
159 intel_pt_insn->rel = 0;
163 intel_pt_insn->rel = insn->immediate.value;
168 int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64,
169 struct intel_pt_insn *intel_pt_insn)
173 insn_init(&insn, buf, len, x86_64);
174 insn_get_length(&insn);
175 if (!insn_complete(&insn) || insn.length > len)
177 intel_pt_insn_decoder(&insn, intel_pt_insn);
178 if (insn.length < INTEL_PT_INSN_BUF_SZ)
179 memcpy(intel_pt_insn->buf, buf, insn.length);
181 memcpy(intel_pt_insn->buf, buf, INTEL_PT_INSN_BUF_SZ);
185 int arch_is_branch(const unsigned char *buf, size_t len, int x86_64)
187 struct intel_pt_insn in;
188 if (intel_pt_get_insn(buf, len, x86_64, &in) < 0)
190 return in.branch != INTEL_PT_BR_NO_BRANCH;
193 const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused,
194 u8 *inbuf, int inlen, int *lenp)
200 insn_init(&insn, inbuf, inlen, x->is64bit);
201 insn_get_length(&insn);
202 if (!insn_complete(&insn) || insn.length > inlen)
206 left = sizeof(x->out);
207 n = snprintf(x->out, left, "insn: ");
209 for (i = 0; i < insn.length; i++) {
210 n += snprintf(x->out + n, left, "%02x ", inbuf[i]);
216 const char *branch_name[] = {
217 [INTEL_PT_OP_OTHER] = "Other",
218 [INTEL_PT_OP_CALL] = "Call",
219 [INTEL_PT_OP_RET] = "Ret",
220 [INTEL_PT_OP_JCC] = "Jcc",
221 [INTEL_PT_OP_JMP] = "Jmp",
222 [INTEL_PT_OP_LOOP] = "Loop",
223 [INTEL_PT_OP_IRET] = "IRet",
224 [INTEL_PT_OP_INT] = "Int",
225 [INTEL_PT_OP_SYSCALL] = "Syscall",
226 [INTEL_PT_OP_SYSRET] = "Sysret",
227 [INTEL_PT_OP_VMENTRY] = "VMentry",
230 const char *intel_pt_insn_name(enum intel_pt_insn_op op)
232 return branch_name[op];
235 int intel_pt_insn_desc(const struct intel_pt_insn *intel_pt_insn, char *buf,
238 switch (intel_pt_insn->branch) {
239 case INTEL_PT_BR_CONDITIONAL:
240 case INTEL_PT_BR_UNCONDITIONAL:
241 return snprintf(buf, buf_len, "%s %s%d",
242 intel_pt_insn_name(intel_pt_insn->op),
243 intel_pt_insn->rel > 0 ? "+" : "",
245 case INTEL_PT_BR_NO_BRANCH:
246 case INTEL_PT_BR_INDIRECT:
247 return snprintf(buf, buf_len, "%s",
248 intel_pt_insn_name(intel_pt_insn->op));
255 int intel_pt_insn_type(enum intel_pt_insn_op op)
258 case INTEL_PT_OP_OTHER:
260 case INTEL_PT_OP_CALL:
261 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL;
262 case INTEL_PT_OP_RET:
263 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN;
264 case INTEL_PT_OP_JCC:
265 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
266 case INTEL_PT_OP_JMP:
267 return PERF_IP_FLAG_BRANCH;
268 case INTEL_PT_OP_LOOP:
269 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
270 case INTEL_PT_OP_IRET:
271 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
272 PERF_IP_FLAG_INTERRUPT;
273 case INTEL_PT_OP_INT:
274 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
275 PERF_IP_FLAG_INTERRUPT;
276 case INTEL_PT_OP_SYSCALL:
277 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
278 PERF_IP_FLAG_SYSCALLRET;
279 case INTEL_PT_OP_SYSRET:
280 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
281 PERF_IP_FLAG_SYSCALLRET;
282 case INTEL_PT_OP_VMENTRY:
283 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
284 PERF_IP_FLAG_VMENTRY;