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 const char *dump_insn(struct perf_insn
*x
, uint64_t ip __maybe_unused
,
184 u8
*inbuf
, int inlen
, int *lenp
)
190 insn_init(&insn
, inbuf
, inlen
, x
->is64bit
);
191 insn_get_length(&insn
);
192 if (!insn_complete(&insn
) || insn
.length
> inlen
)
196 left
= sizeof(x
->out
);
197 n
= snprintf(x
->out
, left
, "insn: ");
199 for (i
= 0; i
< insn
.length
; i
++) {
200 n
+= snprintf(x
->out
+ n
, left
, "%02x ", inbuf
[i
]);
206 const char *branch_name
[] = {
207 [INTEL_PT_OP_OTHER
] = "Other",
208 [INTEL_PT_OP_CALL
] = "Call",
209 [INTEL_PT_OP_RET
] = "Ret",
210 [INTEL_PT_OP_JCC
] = "Jcc",
211 [INTEL_PT_OP_JMP
] = "Jmp",
212 [INTEL_PT_OP_LOOP
] = "Loop",
213 [INTEL_PT_OP_IRET
] = "IRet",
214 [INTEL_PT_OP_INT
] = "Int",
215 [INTEL_PT_OP_SYSCALL
] = "Syscall",
216 [INTEL_PT_OP_SYSRET
] = "Sysret",
219 const char *intel_pt_insn_name(enum intel_pt_insn_op op
)
221 return branch_name
[op
];
224 int intel_pt_insn_desc(const struct intel_pt_insn
*intel_pt_insn
, char *buf
,
227 switch (intel_pt_insn
->branch
) {
228 case INTEL_PT_BR_CONDITIONAL
:
229 case INTEL_PT_BR_UNCONDITIONAL
:
230 return snprintf(buf
, buf_len
, "%s %s%d",
231 intel_pt_insn_name(intel_pt_insn
->op
),
232 intel_pt_insn
->rel
> 0 ? "+" : "",
234 case INTEL_PT_BR_NO_BRANCH
:
235 case INTEL_PT_BR_INDIRECT
:
236 return snprintf(buf
, buf_len
, "%s",
237 intel_pt_insn_name(intel_pt_insn
->op
));
244 int intel_pt_insn_type(enum intel_pt_insn_op op
)
247 case INTEL_PT_OP_OTHER
:
249 case INTEL_PT_OP_CALL
:
250 return PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
;
251 case INTEL_PT_OP_RET
:
252 return PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
;
253 case INTEL_PT_OP_JCC
:
254 return PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CONDITIONAL
;
255 case INTEL_PT_OP_JMP
:
256 return PERF_IP_FLAG_BRANCH
;
257 case INTEL_PT_OP_LOOP
:
258 return PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CONDITIONAL
;
259 case INTEL_PT_OP_IRET
:
260 return PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
|
261 PERF_IP_FLAG_INTERRUPT
;
262 case INTEL_PT_OP_INT
:
263 return PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
|
264 PERF_IP_FLAG_INTERRUPT
;
265 case INTEL_PT_OP_SYSCALL
:
266 return PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
|
267 PERF_IP_FLAG_SYSCALLRET
;
268 case INTEL_PT_OP_SYSRET
:
269 return PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
|
270 PERF_IP_FLAG_SYSCALLRET
;