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