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