1#include <linux/types.h>
2
3#include "debug.h"
4#include "tests/tests.h"
5#include "arch-tests.h"
6
7#include "intel-pt-decoder/insn.h"
8#include "intel-pt-decoder/intel-pt-insn-decoder.h"
9
10struct 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
19struct 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},
24};
25
26struct 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},
31};
32
33static 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
66static 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
93static 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
144static 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174int test__insn_x86(int subtest __maybe_unused)
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}
186