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)
174 ret = insn_decode(&insn, buf, len,
175 x86_64 ? INSN_MODE_64 : INSN_MODE_32);
176 if (ret < 0 || insn.length > len)
179 intel_pt_insn_decoder(&insn, intel_pt_insn);
180 if (insn.length < INTEL_PT_INSN_BUF_SZ)
181 memcpy(intel_pt_insn->buf, buf, insn.length);
183 memcpy(intel_pt_insn->buf, buf, INTEL_PT_INSN_BUF_SZ);
187 int arch_is_branch(const unsigned char *buf, size_t len, int x86_64)
189 struct intel_pt_insn in;
190 if (intel_pt_get_insn(buf, len, x86_64, &in) < 0)
192 return in.branch != INTEL_PT_BR_NO_BRANCH;
195 const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused,
196 u8 *inbuf, int inlen, int *lenp)
202 ret = insn_decode(&insn, inbuf, inlen,
203 x->is64bit ? INSN_MODE_64 : INSN_MODE_32);
205 if (ret < 0 || insn.length > inlen)
209 left = sizeof(x->out);
210 n = snprintf(x->out, left, "insn: ");
212 for (i = 0; i < insn.length; i++) {
213 n += snprintf(x->out + n, left, "%02x ", inbuf[i]);
219 const char *branch_name[] = {
220 [INTEL_PT_OP_OTHER] = "Other",
221 [INTEL_PT_OP_CALL] = "Call",
222 [INTEL_PT_OP_RET] = "Ret",
223 [INTEL_PT_OP_JCC] = "Jcc",
224 [INTEL_PT_OP_JMP] = "Jmp",
225 [INTEL_PT_OP_LOOP] = "Loop",
226 [INTEL_PT_OP_IRET] = "IRet",
227 [INTEL_PT_OP_INT] = "Int",
228 [INTEL_PT_OP_SYSCALL] = "Syscall",
229 [INTEL_PT_OP_SYSRET] = "Sysret",
230 [INTEL_PT_OP_VMENTRY] = "VMentry",
233 const char *intel_pt_insn_name(enum intel_pt_insn_op op)
235 return branch_name[op];
238 int intel_pt_insn_desc(const struct intel_pt_insn *intel_pt_insn, char *buf,
241 switch (intel_pt_insn->branch) {
242 case INTEL_PT_BR_CONDITIONAL:
243 case INTEL_PT_BR_UNCONDITIONAL:
244 return snprintf(buf, buf_len, "%s %s%d",
245 intel_pt_insn_name(intel_pt_insn->op),
246 intel_pt_insn->rel > 0 ? "+" : "",
248 case INTEL_PT_BR_NO_BRANCH:
249 case INTEL_PT_BR_INDIRECT:
250 return snprintf(buf, buf_len, "%s",
251 intel_pt_insn_name(intel_pt_insn->op));
258 int intel_pt_insn_type(enum intel_pt_insn_op op)
261 case INTEL_PT_OP_OTHER:
263 case INTEL_PT_OP_CALL:
264 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL;
265 case INTEL_PT_OP_RET:
266 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN;
267 case INTEL_PT_OP_JCC:
268 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
269 case INTEL_PT_OP_JMP:
270 return PERF_IP_FLAG_BRANCH;
271 case INTEL_PT_OP_LOOP:
272 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
273 case INTEL_PT_OP_IRET:
274 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
275 PERF_IP_FLAG_INTERRUPT;
276 case INTEL_PT_OP_INT:
277 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
278 PERF_IP_FLAG_INTERRUPT;
279 case INTEL_PT_OP_SYSCALL:
280 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
281 PERF_IP_FLAG_SYSCALLRET;
282 case INTEL_PT_OP_SYSRET:
283 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
284 PERF_IP_FLAG_SYSCALLRET;
285 case INTEL_PT_OP_VMENTRY:
286 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
287 PERF_IP_FLAG_VMENTRY;