perf tests: Add arch tests
[cascardo/linux.git] / tools / perf / tests / insn-x86.c
1 #include <linux/types.h>
2
3 #include "debug.h"
4 #include "tests.h"
5
6 #include "intel-pt-decoder/insn.h"
7 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
8
9 struct test_data {
10         u8 data[MAX_INSN_SIZE];
11         int expected_length;
12         int expected_rel;
13         const char *expected_op_str;
14         const char *expected_branch_str;
15         const char *asm_rep;
16 };
17
18 struct test_data test_data_32[] = {
19 #include "insn-x86-dat-32.c"
20         {{0x0f, 0x01, 0xee}, 3, 0, NULL, NULL, "0f 01 ee             \trdpkru"},
21         {{0x0f, 0x01, 0xef}, 3, 0, NULL, NULL, "0f 01 ef             \twrpkru"},
22         {{0}, 0, 0, NULL, NULL, NULL},
23 };
24
25 struct test_data test_data_64[] = {
26 #include "insn-x86-dat-64.c"
27         {{0x0f, 0x01, 0xee}, 3, 0, NULL, NULL, "0f 01 ee             \trdpkru"},
28         {{0x0f, 0x01, 0xef}, 3, 0, NULL, NULL, "0f 01 ef             \twrpkru"},
29         {{0}, 0, 0, NULL, NULL, NULL},
30 };
31
32 static int get_op(const char *op_str)
33 {
34         struct val_data {
35                 const char *name;
36                 int val;
37         } vals[] = {
38                 {"other",   INTEL_PT_OP_OTHER},
39                 {"call",    INTEL_PT_OP_CALL},
40                 {"ret",     INTEL_PT_OP_RET},
41                 {"jcc",     INTEL_PT_OP_JCC},
42                 {"jmp",     INTEL_PT_OP_JMP},
43                 {"loop",    INTEL_PT_OP_LOOP},
44                 {"iret",    INTEL_PT_OP_IRET},
45                 {"int",     INTEL_PT_OP_INT},
46                 {"syscall", INTEL_PT_OP_SYSCALL},
47                 {"sysret",  INTEL_PT_OP_SYSRET},
48                 {NULL, 0},
49         };
50         struct val_data *val;
51
52         if (!op_str || !strlen(op_str))
53                 return 0;
54
55         for (val = vals; val->name; val++) {
56                 if (!strcmp(val->name, op_str))
57                         return val->val;
58         }
59
60         pr_debug("Failed to get op\n");
61
62         return -1;
63 }
64
65 static int get_branch(const char *branch_str)
66 {
67         struct val_data {
68                 const char *name;
69                 int val;
70         } vals[] = {
71                 {"no_branch",     INTEL_PT_BR_NO_BRANCH},
72                 {"indirect",      INTEL_PT_BR_INDIRECT},
73                 {"conditional",   INTEL_PT_BR_CONDITIONAL},
74                 {"unconditional", INTEL_PT_BR_UNCONDITIONAL},
75                 {NULL, 0},
76         };
77         struct val_data *val;
78
79         if (!branch_str || !strlen(branch_str))
80                 return 0;
81
82         for (val = vals; val->name; val++) {
83                 if (!strcmp(val->name, branch_str))
84                         return val->val;
85         }
86
87         pr_debug("Failed to get branch\n");
88
89         return -1;
90 }
91
92 static int test_data_item(struct test_data *dat, int x86_64)
93 {
94         struct intel_pt_insn intel_pt_insn;
95         struct insn insn;
96         int op, branch;
97
98         insn_init(&insn, dat->data, MAX_INSN_SIZE, x86_64);
99         insn_get_length(&insn);
100
101         if (!insn_complete(&insn)) {
102                 pr_debug("Failed to decode: %s\n", dat->asm_rep);
103                 return -1;
104         }
105
106         if (insn.length != dat->expected_length) {
107                 pr_debug("Failed to decode length (%d vs expected %d): %s\n",
108                          insn.length, dat->expected_length, dat->asm_rep);
109                 return -1;
110         }
111
112         op = get_op(dat->expected_op_str);
113         branch = get_branch(dat->expected_branch_str);
114
115         if (intel_pt_get_insn(dat->data, MAX_INSN_SIZE, x86_64, &intel_pt_insn)) {
116                 pr_debug("Intel PT failed to decode: %s\n", dat->asm_rep);
117                 return -1;
118         }
119
120         if ((int)intel_pt_insn.op != op) {
121                 pr_debug("Failed to decode 'op' value (%d vs expected %d): %s\n",
122                          intel_pt_insn.op, op, dat->asm_rep);
123                 return -1;
124         }
125
126         if ((int)intel_pt_insn.branch != branch) {
127                 pr_debug("Failed to decode 'branch' value (%d vs expected %d): %s\n",
128                          intel_pt_insn.branch, branch, dat->asm_rep);
129                 return -1;
130         }
131
132         if (intel_pt_insn.rel != dat->expected_rel) {
133                 pr_debug("Failed to decode 'rel' value (%#x vs expected %#x): %s\n",
134                          intel_pt_insn.rel, dat->expected_rel, dat->asm_rep);
135                 return -1;
136         }
137
138         pr_debug("Decoded ok: %s\n", dat->asm_rep);
139
140         return 0;
141 }
142
143 static int test_data_set(struct test_data *dat_set, int x86_64)
144 {
145         struct test_data *dat;
146         int ret = 0;
147
148         for (dat = dat_set; dat->expected_length; dat++) {
149                 if (test_data_item(dat, x86_64))
150                         ret = -1;
151         }
152
153         return ret;
154 }
155
156 /**
157  * test__insn_x86 - test x86 instruction decoder - new instructions.
158  *
159  * This function implements a test that decodes a selection of instructions and
160  * checks the results.  The Intel PT function that further categorizes
161  * instructions (i.e. intel_pt_get_insn()) is also checked.
162  *
163  * The instructions are originally in insn-x86-dat-src.c which has been
164  * processed by scripts gen-insn-x86-dat.sh and gen-insn-x86-dat.awk to produce
165  * insn-x86-dat-32.c and insn-x86-dat-64.c which are included into this program.
166  * i.e. to add new instructions to the test, edit insn-x86-dat-src.c, run the
167  * gen-insn-x86-dat.sh script, make perf, and then run the test.
168  *
169  * If the test passes %0 is returned, otherwise %-1 is returned.  Use the
170  * verbose (-v) option to see all the instructions and whether or not they
171  * decoded successfuly.
172  */
173 int test__insn_x86(void)
174 {
175         int ret = 0;
176
177         if (test_data_set(test_data_32, 0))
178                 ret = -1;
179
180         if (test_data_set(test_data_64, 1))
181                 ret = -1;
182
183         return ret;
184 }