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;
35 intel_pt_insn->emulated_ptwrite = false;
37 if (insn_is_avx(insn)) {
38 intel_pt_insn->op = INTEL_PT_OP_OTHER;
39 intel_pt_insn->branch = INTEL_PT_BR_NO_BRANCH;
40 intel_pt_insn->length = insn->length;
44 switch (insn->opcode.bytes[0]) {
46 switch (insn->opcode.bytes[1]) {
48 switch (insn->modrm.bytes[0]) {
49 case 0xc2: /* vmlaunch */
50 case 0xc3: /* vmresume */
51 op = INTEL_PT_OP_VMENTRY;
52 branch = INTEL_PT_BR_INDIRECT;
58 case 0x05: /* syscall */
59 case 0x34: /* sysenter */
60 op = INTEL_PT_OP_SYSCALL;
61 branch = INTEL_PT_BR_INDIRECT;
63 case 0x07: /* sysret */
64 case 0x35: /* sysexit */
65 op = INTEL_PT_OP_SYSRET;
66 branch = INTEL_PT_BR_INDIRECT;
68 case 0x80 ... 0x8f: /* jcc */
70 branch = INTEL_PT_BR_CONDITIONAL;
76 case 0x70 ... 0x7f: /* jcc */
78 branch = INTEL_PT_BR_CONDITIONAL;
80 case 0xc2: /* near ret */
81 case 0xc3: /* near ret */
82 case 0xca: /* far ret */
83 case 0xcb: /* far ret */
85 branch = INTEL_PT_BR_INDIRECT;
88 op = INTEL_PT_OP_IRET;
89 branch = INTEL_PT_BR_INDIRECT;
91 case 0xcc ... 0xce: /* int */
93 branch = INTEL_PT_BR_INDIRECT;
95 case 0xe8: /* call near rel */
96 op = INTEL_PT_OP_CALL;
97 branch = INTEL_PT_BR_UNCONDITIONAL;
99 case 0x9a: /* call far absolute */
100 op = INTEL_PT_OP_CALL;
101 branch = INTEL_PT_BR_INDIRECT;
103 case 0xe0 ... 0xe2: /* loop */
104 op = INTEL_PT_OP_LOOP;
105 branch = INTEL_PT_BR_CONDITIONAL;
108 op = INTEL_PT_OP_JCC;
109 branch = INTEL_PT_BR_CONDITIONAL;
113 op = INTEL_PT_OP_JMP;
114 branch = INTEL_PT_BR_UNCONDITIONAL;
116 case 0xea: /* far jmp */
117 op = INTEL_PT_OP_JMP;
118 branch = INTEL_PT_BR_INDIRECT;
120 case 0xff: /* call near absolute, call far absolute ind */
121 ext = (insn->modrm.bytes[0] >> 3) & 0x7;
123 case 2: /* near ind call */
124 case 3: /* far ind call */
125 op = INTEL_PT_OP_CALL;
126 branch = INTEL_PT_BR_INDIRECT;
130 op = INTEL_PT_OP_JMP;
131 branch = INTEL_PT_BR_INDIRECT;
141 intel_pt_insn->op = op;
142 intel_pt_insn->branch = branch;
143 intel_pt_insn->length = insn->length;
145 if (branch == INTEL_PT_BR_CONDITIONAL ||
146 branch == INTEL_PT_BR_UNCONDITIONAL) {
147 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
148 switch (insn->immediate.nbytes) {
150 intel_pt_insn->rel = insn->immediate.value;
154 bswap_16((short)insn->immediate.value);
157 intel_pt_insn->rel = bswap_32(insn->immediate.value);
160 intel_pt_insn->rel = 0;
164 intel_pt_insn->rel = insn->immediate.value;
169 int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64,
170 struct intel_pt_insn *intel_pt_insn)
175 ret = insn_decode(&insn, buf, len,
176 x86_64 ? INSN_MODE_64 : INSN_MODE_32);
177 if (ret < 0 || insn.length > len)
180 intel_pt_insn_decoder(&insn, intel_pt_insn);
181 if (insn.length < INTEL_PT_INSN_BUF_SZ)
182 memcpy(intel_pt_insn->buf, buf, insn.length);
184 memcpy(intel_pt_insn->buf, buf, INTEL_PT_INSN_BUF_SZ);
188 int arch_is_branch(const unsigned char *buf, size_t len, int x86_64)
190 struct intel_pt_insn in;
191 if (intel_pt_get_insn(buf, len, x86_64, &in) < 0)
193 return in.branch != INTEL_PT_BR_NO_BRANCH;
196 const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused,
197 u8 *inbuf, int inlen, int *lenp)
203 ret = insn_decode(&insn, inbuf, inlen,
204 x->is64bit ? INSN_MODE_64 : INSN_MODE_32);
206 if (ret < 0 || insn.length > inlen)
210 left = sizeof(x->out);
211 n = snprintf(x->out, left, "insn: ");
213 for (i = 0; i < insn.length; i++) {
214 n += snprintf(x->out + n, left, "%02x ", inbuf[i]);
220 const char *branch_name[] = {
221 [INTEL_PT_OP_OTHER] = "Other",
222 [INTEL_PT_OP_CALL] = "Call",
223 [INTEL_PT_OP_RET] = "Ret",
224 [INTEL_PT_OP_JCC] = "Jcc",
225 [INTEL_PT_OP_JMP] = "Jmp",
226 [INTEL_PT_OP_LOOP] = "Loop",
227 [INTEL_PT_OP_IRET] = "IRet",
228 [INTEL_PT_OP_INT] = "Int",
229 [INTEL_PT_OP_SYSCALL] = "Syscall",
230 [INTEL_PT_OP_SYSRET] = "Sysret",
231 [INTEL_PT_OP_VMENTRY] = "VMentry",
234 const char *intel_pt_insn_name(enum intel_pt_insn_op op)
236 return branch_name[op];
239 int intel_pt_insn_desc(const struct intel_pt_insn *intel_pt_insn, char *buf,
242 switch (intel_pt_insn->branch) {
243 case INTEL_PT_BR_CONDITIONAL:
244 case INTEL_PT_BR_UNCONDITIONAL:
245 return snprintf(buf, buf_len, "%s %s%d",
246 intel_pt_insn_name(intel_pt_insn->op),
247 intel_pt_insn->rel > 0 ? "+" : "",
249 case INTEL_PT_BR_NO_BRANCH:
250 case INTEL_PT_BR_INDIRECT:
251 return snprintf(buf, buf_len, "%s",
252 intel_pt_insn_name(intel_pt_insn->op));
259 int intel_pt_insn_type(enum intel_pt_insn_op op)
262 case INTEL_PT_OP_OTHER:
264 case INTEL_PT_OP_CALL:
265 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL;
266 case INTEL_PT_OP_RET:
267 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN;
268 case INTEL_PT_OP_JCC:
269 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
270 case INTEL_PT_OP_JMP:
271 return PERF_IP_FLAG_BRANCH;
272 case INTEL_PT_OP_LOOP:
273 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL;
274 case INTEL_PT_OP_IRET:
275 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
276 PERF_IP_FLAG_INTERRUPT;
277 case INTEL_PT_OP_INT:
278 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
279 PERF_IP_FLAG_INTERRUPT;
280 case INTEL_PT_OP_SYSCALL:
281 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
282 PERF_IP_FLAG_SYSCALLRET;
283 case INTEL_PT_OP_SYSRET:
284 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN |
285 PERF_IP_FLAG_SYSCALLRET;
286 case INTEL_PT_OP_VMENTRY:
287 return PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
288 PERF_IP_FLAG_VMENTRY;