1 #include <linux/types.h>
4 #include "tests/tests.h"
5 #include "arch-tests.h"
7 #include "intel-pt-decoder/insn.h"
8 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
11 u8 data
[MAX_INSN_SIZE
];
14 const char *expected_op_str
;
15 const char *expected_branch_str
;
19 struct test_data test_data_32
[] = {
20 #include "insn-x86-dat-32.c"
21 {{0x0f, 0x01, 0xee}, 3, 0, NULL
, NULL
, "0f 01 ee \trdpkru"},
22 {{0x0f, 0x01, 0xef}, 3, 0, NULL
, NULL
, "0f 01 ef \twrpkru"},
23 {{0}, 0, 0, NULL
, NULL
, NULL
},
26 struct test_data test_data_64
[] = {
27 #include "insn-x86-dat-64.c"
28 {{0x0f, 0x01, 0xee}, 3, 0, NULL
, NULL
, "0f 01 ee \trdpkru"},
29 {{0x0f, 0x01, 0xef}, 3, 0, NULL
, NULL
, "0f 01 ef \twrpkru"},
30 {{0}, 0, 0, NULL
, NULL
, NULL
},
33 static int get_op(const char *op_str
)
39 {"other", INTEL_PT_OP_OTHER
},
40 {"call", INTEL_PT_OP_CALL
},
41 {"ret", INTEL_PT_OP_RET
},
42 {"jcc", INTEL_PT_OP_JCC
},
43 {"jmp", INTEL_PT_OP_JMP
},
44 {"loop", INTEL_PT_OP_LOOP
},
45 {"iret", INTEL_PT_OP_IRET
},
46 {"int", INTEL_PT_OP_INT
},
47 {"syscall", INTEL_PT_OP_SYSCALL
},
48 {"sysret", INTEL_PT_OP_SYSRET
},
53 if (!op_str
|| !strlen(op_str
))
56 for (val
= vals
; val
->name
; val
++) {
57 if (!strcmp(val
->name
, op_str
))
61 pr_debug("Failed to get op\n");
66 static int get_branch(const char *branch_str
)
72 {"no_branch", INTEL_PT_BR_NO_BRANCH
},
73 {"indirect", INTEL_PT_BR_INDIRECT
},
74 {"conditional", INTEL_PT_BR_CONDITIONAL
},
75 {"unconditional", INTEL_PT_BR_UNCONDITIONAL
},
80 if (!branch_str
|| !strlen(branch_str
))
83 for (val
= vals
; val
->name
; val
++) {
84 if (!strcmp(val
->name
, branch_str
))
88 pr_debug("Failed to get branch\n");
93 static int test_data_item(struct test_data
*dat
, int x86_64
)
95 struct intel_pt_insn intel_pt_insn
;
99 insn_init(&insn
, dat
->data
, MAX_INSN_SIZE
, x86_64
);
100 insn_get_length(&insn
);
102 if (!insn_complete(&insn
)) {
103 pr_debug("Failed to decode: %s\n", dat
->asm_rep
);
107 if (insn
.length
!= dat
->expected_length
) {
108 pr_debug("Failed to decode length (%d vs expected %d): %s\n",
109 insn
.length
, dat
->expected_length
, dat
->asm_rep
);
113 op
= get_op(dat
->expected_op_str
);
114 branch
= get_branch(dat
->expected_branch_str
);
116 if (intel_pt_get_insn(dat
->data
, MAX_INSN_SIZE
, x86_64
, &intel_pt_insn
)) {
117 pr_debug("Intel PT failed to decode: %s\n", dat
->asm_rep
);
121 if ((int)intel_pt_insn
.op
!= op
) {
122 pr_debug("Failed to decode 'op' value (%d vs expected %d): %s\n",
123 intel_pt_insn
.op
, op
, dat
->asm_rep
);
127 if ((int)intel_pt_insn
.branch
!= branch
) {
128 pr_debug("Failed to decode 'branch' value (%d vs expected %d): %s\n",
129 intel_pt_insn
.branch
, branch
, dat
->asm_rep
);
133 if (intel_pt_insn
.rel
!= dat
->expected_rel
) {
134 pr_debug("Failed to decode 'rel' value (%#x vs expected %#x): %s\n",
135 intel_pt_insn
.rel
, dat
->expected_rel
, dat
->asm_rep
);
139 pr_debug("Decoded ok: %s\n", dat
->asm_rep
);
144 static int test_data_set(struct test_data
*dat_set
, int x86_64
)
146 struct test_data
*dat
;
149 for (dat
= dat_set
; dat
->expected_length
; dat
++) {
150 if (test_data_item(dat
, x86_64
))
158 * test__insn_x86 - test x86 instruction decoder - new instructions.
160 * This function implements a test that decodes a selection of instructions and
161 * checks the results. The Intel PT function that further categorizes
162 * instructions (i.e. intel_pt_get_insn()) is also checked.
164 * The instructions are originally in insn-x86-dat-src.c which has been
165 * processed by scripts gen-insn-x86-dat.sh and gen-insn-x86-dat.awk to produce
166 * insn-x86-dat-32.c and insn-x86-dat-64.c which are included into this program.
167 * i.e. to add new instructions to the test, edit insn-x86-dat-src.c, run the
168 * gen-insn-x86-dat.sh script, make perf, and then run the test.
170 * If the test passes %0 is returned, otherwise %-1 is returned. Use the
171 * verbose (-v) option to see all the instructions and whether or not they
172 * decoded successfuly.
174 int test__insn_x86(int subtest __maybe_unused
)
178 if (test_data_set(test_data_32
, 0))
181 if (test_data_set(test_data_64
, 1))