Commit | Line | Data |
---|---|---|
98e4619f AH |
1 | #include <linux/types.h> |
2 | ||
3 | #include "debug.h" | |
d8b167f9 MF |
4 | #include "tests/tests.h" |
5 | #include "arch-tests.h" | |
98e4619f AH |
6 | |
7 | #include "intel-pt-decoder/insn.h" | |
8 | #include "intel-pt-decoder/intel-pt-insn-decoder.h" | |
9 | ||
10 | struct test_data { | |
11 | u8 data[MAX_INSN_SIZE]; | |
12 | int expected_length; | |
13 | int expected_rel; | |
14 | const char *expected_op_str; | |
15 | const char *expected_branch_str; | |
16 | const char *asm_rep; | |
17 | }; | |
18 | ||
19 | struct test_data test_data_32[] = { | |
20 | #include "insn-x86-dat-32.c" | |
978260cd AH |
21 | {{0x0f, 0x01, 0xee}, 3, 0, NULL, NULL, "0f 01 ee \trdpkru"}, |
22 | {{0x0f, 0x01, 0xef}, 3, 0, NULL, NULL, "0f 01 ef \twrpkru"}, | |
98e4619f AH |
23 | {{0}, 0, 0, NULL, NULL, NULL}, |
24 | }; | |
25 | ||
26 | struct test_data test_data_64[] = { | |
27 | #include "insn-x86-dat-64.c" | |
978260cd AH |
28 | {{0x0f, 0x01, 0xee}, 3, 0, NULL, NULL, "0f 01 ee \trdpkru"}, |
29 | {{0x0f, 0x01, 0xef}, 3, 0, NULL, NULL, "0f 01 ef \twrpkru"}, | |
98e4619f AH |
30 | {{0}, 0, 0, NULL, NULL, NULL}, |
31 | }; | |
32 | ||
33 | static int get_op(const char *op_str) | |
34 | { | |
35 | struct val_data { | |
36 | const char *name; | |
37 | int val; | |
38 | } vals[] = { | |
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}, | |
49 | {NULL, 0}, | |
50 | }; | |
51 | struct val_data *val; | |
52 | ||
53 | if (!op_str || !strlen(op_str)) | |
54 | return 0; | |
55 | ||
56 | for (val = vals; val->name; val++) { | |
57 | if (!strcmp(val->name, op_str)) | |
58 | return val->val; | |
59 | } | |
60 | ||
61 | pr_debug("Failed to get op\n"); | |
62 | ||
63 | return -1; | |
64 | } | |
65 | ||
66 | static int get_branch(const char *branch_str) | |
67 | { | |
68 | struct val_data { | |
69 | const char *name; | |
70 | int val; | |
71 | } vals[] = { | |
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}, | |
76 | {NULL, 0}, | |
77 | }; | |
78 | struct val_data *val; | |
79 | ||
80 | if (!branch_str || !strlen(branch_str)) | |
81 | return 0; | |
82 | ||
83 | for (val = vals; val->name; val++) { | |
84 | if (!strcmp(val->name, branch_str)) | |
85 | return val->val; | |
86 | } | |
87 | ||
88 | pr_debug("Failed to get branch\n"); | |
89 | ||
90 | return -1; | |
91 | } | |
92 | ||
93 | static int test_data_item(struct test_data *dat, int x86_64) | |
94 | { | |
95 | struct intel_pt_insn intel_pt_insn; | |
96 | struct insn insn; | |
97 | int op, branch; | |
98 | ||
99 | insn_init(&insn, dat->data, MAX_INSN_SIZE, x86_64); | |
100 | insn_get_length(&insn); | |
101 | ||
102 | if (!insn_complete(&insn)) { | |
103 | pr_debug("Failed to decode: %s\n", dat->asm_rep); | |
104 | return -1; | |
105 | } | |
106 | ||
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); | |
110 | return -1; | |
111 | } | |
112 | ||
113 | op = get_op(dat->expected_op_str); | |
114 | branch = get_branch(dat->expected_branch_str); | |
115 | ||
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); | |
118 | return -1; | |
119 | } | |
120 | ||
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); | |
124 | return -1; | |
125 | } | |
126 | ||
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); | |
130 | return -1; | |
131 | } | |
132 | ||
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); | |
136 | return -1; | |
137 | } | |
138 | ||
139 | pr_debug("Decoded ok: %s\n", dat->asm_rep); | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
144 | static int test_data_set(struct test_data *dat_set, int x86_64) | |
145 | { | |
146 | struct test_data *dat; | |
147 | int ret = 0; | |
148 | ||
149 | for (dat = dat_set; dat->expected_length; dat++) { | |
150 | if (test_data_item(dat, x86_64)) | |
151 | ret = -1; | |
152 | } | |
153 | ||
154 | return ret; | |
155 | } | |
156 | ||
157 | /** | |
158 | * test__insn_x86 - test x86 instruction decoder - new instructions. | |
159 | * | |
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. | |
163 | * | |
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. | |
169 | * | |
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. | |
173 | */ | |
81f17c90 | 174 | int test__insn_x86(struct test *test __maybe_unused, int subtest __maybe_unused) |
98e4619f AH |
175 | { |
176 | int ret = 0; | |
177 | ||
178 | if (test_data_set(test_data_32, 0)) | |
179 | ret = -1; | |
180 | ||
181 | if (test_data_set(test_data_64, 1)) | |
182 | ret = -1; | |
183 | ||
184 | return ret; | |
185 | } |