aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/tools/test_get_len.c
diff options
context:
space:
mode:
authorMasami Hiramatsu <mhiramat@redhat.com>2009-08-13 16:34:21 -0400
committerFrederic Weisbecker <fweisbec@gmail.com>2009-08-26 18:35:56 -0400
commitca0e9badd1a39fecdd235f4bf1481b9da756e27b (patch)
treee7d7f9a398bf50cb1bd327b38a29d756b54b7b95 /arch/x86/tools/test_get_len.c
parenteb13296cfaf6c699566473669a96a38a90562384 (diff)
x86: X86 instruction decoder build-time selftest
Add a user-space selftest of x86 instruction decoder at kernel build time. When CONFIG_X86_DECODER_SELFTEST=y, Kbuild builds a test harness of x86 instruction decoder and performs it after building vmlinux. The test compares the results of objdump and x86 instruction decoder code and check there are no differences. Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com> Signed-off-by: Jim Keniston <jkenisto@us.ibm.com> Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com> Cc: Avi Kivity <avi@redhat.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: Frank Ch. Eigler <fche@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Jason Baron <jbaron@redhat.com> Cc: K.Prasad <prasad@linux.vnet.ibm.com> Cc: Lai Jiangshan <laijs@cn.fujitsu.com> Cc: Li Zefan <lizf@cn.fujitsu.com> Cc: Przemysław Pawełczyk <przemyslaw@pawelczyk.it> Cc: Roland McGrath <roland@redhat.com> Cc: Sam Ravnborg <sam@ravnborg.org> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Tom Zanussi <tzanussi@gmail.com> Cc: Vegard Nossum <vegard.nossum@gmail.com> LKML-Reference: <20090813203421.31965.29006.stgit@localhost.localdomain> Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Diffstat (limited to 'arch/x86/tools/test_get_len.c')
-rw-r--r--arch/x86/tools/test_get_len.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/arch/x86/tools/test_get_len.c b/arch/x86/tools/test_get_len.c
new file mode 100644
index 000000000000..1e81adb2d8a9
--- /dev/null
+++ b/arch/x86/tools/test_get_len.c
@@ -0,0 +1,113 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) IBM Corporation, 2009
17 */
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <assert.h>
23
24#ifdef __x86_64__
25#define CONFIG_X86_64
26#else
27#define CONFIG_X86_32
28#endif
29#define unlikely(cond) (cond)
30
31#include <asm/insn.h>
32#include <inat.c>
33#include <insn.c>
34
35/*
36 * Test of instruction analysis in general and insn_get_length() in
37 * particular. See if insn_get_length() and the disassembler agree
38 * on the length of each instruction in an elf disassembly.
39 *
40 * Usage: objdump -d a.out | awk -f distill.awk | ./test_get_len
41 */
42
43const char *prog;
44
45static void usage(void)
46{
47 fprintf(stderr, "Usage: objdump -d a.out | awk -f distill.awk |"
48 " ./test_get_len\n");
49 exit(1);
50}
51
52static void malformed_line(const char *line, int line_nr)
53{
54 fprintf(stderr, "%s: malformed line %d:\n%s", prog, line_nr, line);
55 exit(3);
56}
57
58#define BUFSIZE 256
59
60int main(int argc, char **argv)
61{
62 char line[BUFSIZE];
63 unsigned char insn_buf[16];
64 struct insn insn;
65 int insns = 0;
66
67 prog = argv[0];
68 if (argc > 1)
69 usage();
70
71 while (fgets(line, BUFSIZE, stdin)) {
72 char copy[BUFSIZE], *s, *tab1, *tab2;
73 int nb = 0;
74 unsigned int b;
75
76 insns++;
77 memset(insn_buf, 0, 16);
78 strcpy(copy, line);
79 tab1 = strchr(copy, '\t');
80 if (!tab1)
81 malformed_line(line, insns);
82 s = tab1 + 1;
83 s += strspn(s, " ");
84 tab2 = strchr(s, '\t');
85 if (!tab2)
86 malformed_line(line, insns);
87 *tab2 = '\0'; /* Characters beyond tab2 aren't examined */
88 while (s < tab2) {
89 if (sscanf(s, "%x", &b) == 1) {
90 insn_buf[nb++] = (unsigned char) b;
91 s += 3;
92 } else
93 break;
94 }
95 /* Decode an instruction */
96#ifdef __x86_64__
97 insn_init(&insn, insn_buf, 1);
98#else
99 insn_init(&insn, insn_buf, 0);
100#endif
101 insn_get_length(&insn);
102 if (insn.length != nb) {
103 fprintf(stderr, "Error: %s", line);
104 fprintf(stderr, "Error: objdump says %d bytes, but "
105 "insn_get_length() says %d (attr:%x)\n", nb,
106 insn.length, insn.attr);
107 exit(2);
108 }
109 }
110 fprintf(stderr, "Succeed: decoded and checked %d instructions\n",
111 insns);
112 return 0;
113}