1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
5 #include "tests/tests.h"
6 #include "arch-tests.h"
8 #include "intel-pt-decoder/insn.h"
9 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
12 u8 data
[MAX_INSN_SIZE
];
15 const char *expected_op_str
;
16 const char *expected_branch_str
;
20 struct test_data test_data_32
[] = {
21 #include "insn-x86-dat-32.c"
22 {{0x0f, 0x01, 0xee}, 3, 0, NULL
, NULL
, "0f 01 ee \trdpkru"},
23 {{0x0f, 0x01, 0xef}, 3, 0, NULL
, NULL
, "0f 01 ef \twrpkru"},
24 {{0}, 0, 0, NULL
, NULL
, NULL
},
27 struct test_data test_data_64
[] = {
28 #include "insn-x86-dat-64.c"
29 {{0x0f, 0x01, 0xee}, 3, 0, NULL
, NULL
, "0f 01 ee \trdpkru"},
30 {{0x0f, 0x01, 0xef}, 3, 0, NULL
, NULL
, "0f 01 ef \twrpkru"},
31 {{0}, 0, 0, NULL
, NULL
, NULL
},
34 static int get_op(const char *op_str
)
40 {"other", INTEL_PT_OP_OTHER
},
41 {"call", INTEL_PT_OP_CALL
},
42 {"ret", INTEL_PT_OP_RET
},
43 {"jcc", INTEL_PT_OP_JCC
},
44 {"jmp", INTEL_PT_OP_JMP
},
45 {"loop", INTEL_PT_OP_LOOP
},
46 {"iret", INTEL_PT_OP_IRET
},
47 {"int", INTEL_PT_OP_INT
},
48 {"syscall", INTEL_PT_OP_SYSCALL
},
49 {"sysret", INTEL_PT_OP_SYSRET
},
54 if (!op_str
|| !strlen(op_str
))
57 for (val
= vals
; val
->name
; val
++) {
58 if (!strcmp(val
->name
, op_str
))
62 pr_debug("Failed to get op\n");
67 static int get_branch(const char *branch_str
)
73 {"no_branch", INTEL_PT_BR_NO_BRANCH
},
74 {"indirect", INTEL_PT_BR_INDIRECT
},
75 {"conditional", INTEL_PT_BR_CONDITIONAL
},
76 {"unconditional", INTEL_PT_BR_UNCONDITIONAL
},
81 if (!branch_str
|| !strlen(branch_str
))
84 for (val
= vals
; val
->name
; val
++) {
85 if (!strcmp(val
->name
, branch_str
))
89 pr_debug("Failed to get branch\n");
94 static int test_data_item(struct test_data
*dat
, int x86_64
)
96 struct intel_pt_insn intel_pt_insn
;
100 insn_init(&insn
, dat
->data
, MAX_INSN_SIZE
, x86_64
);
101 insn_get_length(&insn
);
103 if (!insn_complete(&insn
)) {
104 pr_debug("Failed to decode: %s\n", dat
->asm_rep
);
108 if (insn
.length
!= dat
->expected_length
) {
109 pr_debug("Failed to decode length (%d vs expected %d): %s\n",
110 insn
.length
, dat
->expected_length
, dat
->asm_rep
);
114 op
= get_op(dat
->expected_op_str
);
115 branch
= get_branch(dat
->expected_branch_str
);
117 if (intel_pt_get_insn(dat
->data
, MAX_INSN_SIZE
, x86_64
, &intel_pt_insn
)) {
118 pr_debug("Intel PT failed to decode: %s\n", dat
->asm_rep
);
122 if ((int)intel_pt_insn
.op
!= op
) {
123 pr_debug("Failed to decode 'op' value (%d vs expected %d): %s\n",
124 intel_pt_insn
.op
, op
, dat
->asm_rep
);
128 if ((int)intel_pt_insn
.branch
!= branch
) {
129 pr_debug("Failed to decode 'branch' value (%d vs expected %d): %s\n",
130 intel_pt_insn
.branch
, branch
, dat
->asm_rep
);
134 if (intel_pt_insn
.rel
!= dat
->expected_rel
) {
135 pr_debug("Failed to decode 'rel' value (%#x vs expected %#x): %s\n",
136 intel_pt_insn
.rel
, dat
->expected_rel
, dat
->asm_rep
);
140 pr_debug("Decoded ok: %s\n", dat
->asm_rep
);
145 static int test_data_set(struct test_data
*dat_set
, int x86_64
)
147 struct test_data
*dat
;
150 for (dat
= dat_set
; dat
->expected_length
; dat
++) {
151 if (test_data_item(dat
, x86_64
))
159 * test__insn_x86 - test x86 instruction decoder - new instructions.
161 * This function implements a test that decodes a selection of instructions and
162 * checks the results. The Intel PT function that further categorizes
163 * instructions (i.e. intel_pt_get_insn()) is also checked.
165 * The instructions are originally in insn-x86-dat-src.c which has been
166 * processed by scripts gen-insn-x86-dat.sh and gen-insn-x86-dat.awk to produce
167 * insn-x86-dat-32.c and insn-x86-dat-64.c which are included into this program.
168 * i.e. to add new instructions to the test, edit insn-x86-dat-src.c, run the
169 * gen-insn-x86-dat.sh script, make perf, and then run the test.
171 * If the test passes %0 is returned, otherwise %-1 is returned. Use the
172 * verbose (-v) option to see all the instructions and whether or not they
173 * decoded successfuly.
175 int test__insn_x86(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
179 if (test_data_set(test_data_32
, 0))
182 if (test_data_set(test_data_64
, 1))