diff options
Diffstat (limited to 'arch/x86/include')
-rw-r--r-- | arch/x86/include/asm/inat.h | 188 | ||||
-rw-r--r-- | arch/x86/include/asm/inat_types.h | 29 | ||||
-rw-r--r-- | arch/x86/include/asm/insn.h | 143 |
3 files changed, 360 insertions, 0 deletions
diff --git a/arch/x86/include/asm/inat.h b/arch/x86/include/asm/inat.h new file mode 100644 index 000000000000..2866fddd1848 --- /dev/null +++ b/arch/x86/include/asm/inat.h | |||
@@ -0,0 +1,188 @@ | |||
1 | #ifndef _ASM_X86_INAT_H | ||
2 | #define _ASM_X86_INAT_H | ||
3 | /* | ||
4 | * x86 instruction attributes | ||
5 | * | ||
6 | * Written by Masami Hiramatsu <mhiramat@redhat.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
21 | * | ||
22 | */ | ||
23 | #include <asm/inat_types.h> | ||
24 | |||
25 | /* | ||
26 | * Internal bits. Don't use bitmasks directly, because these bits are | ||
27 | * unstable. You should use checking functions. | ||
28 | */ | ||
29 | |||
30 | #define INAT_OPCODE_TABLE_SIZE 256 | ||
31 | #define INAT_GROUP_TABLE_SIZE 8 | ||
32 | |||
33 | /* Legacy instruction prefixes */ | ||
34 | #define INAT_PFX_OPNDSZ 1 /* 0x66 */ /* LPFX1 */ | ||
35 | #define INAT_PFX_REPNE 2 /* 0xF2 */ /* LPFX2 */ | ||
36 | #define INAT_PFX_REPE 3 /* 0xF3 */ /* LPFX3 */ | ||
37 | #define INAT_PFX_LOCK 4 /* 0xF0 */ | ||
38 | #define INAT_PFX_CS 5 /* 0x2E */ | ||
39 | #define INAT_PFX_DS 6 /* 0x3E */ | ||
40 | #define INAT_PFX_ES 7 /* 0x26 */ | ||
41 | #define INAT_PFX_FS 8 /* 0x64 */ | ||
42 | #define INAT_PFX_GS 9 /* 0x65 */ | ||
43 | #define INAT_PFX_SS 10 /* 0x36 */ | ||
44 | #define INAT_PFX_ADDRSZ 11 /* 0x67 */ | ||
45 | |||
46 | #define INAT_LPREFIX_MAX 3 | ||
47 | |||
48 | /* Immediate size */ | ||
49 | #define INAT_IMM_BYTE 1 | ||
50 | #define INAT_IMM_WORD 2 | ||
51 | #define INAT_IMM_DWORD 3 | ||
52 | #define INAT_IMM_QWORD 4 | ||
53 | #define INAT_IMM_PTR 5 | ||
54 | #define INAT_IMM_VWORD32 6 | ||
55 | #define INAT_IMM_VWORD 7 | ||
56 | |||
57 | /* Legacy prefix */ | ||
58 | #define INAT_PFX_OFFS 0 | ||
59 | #define INAT_PFX_BITS 4 | ||
60 | #define INAT_PFX_MAX ((1 << INAT_PFX_BITS) - 1) | ||
61 | #define INAT_PFX_MASK (INAT_PFX_MAX << INAT_PFX_OFFS) | ||
62 | /* Escape opcodes */ | ||
63 | #define INAT_ESC_OFFS (INAT_PFX_OFFS + INAT_PFX_BITS) | ||
64 | #define INAT_ESC_BITS 2 | ||
65 | #define INAT_ESC_MAX ((1 << INAT_ESC_BITS) - 1) | ||
66 | #define INAT_ESC_MASK (INAT_ESC_MAX << INAT_ESC_OFFS) | ||
67 | /* Group opcodes (1-16) */ | ||
68 | #define INAT_GRP_OFFS (INAT_ESC_OFFS + INAT_ESC_BITS) | ||
69 | #define INAT_GRP_BITS 5 | ||
70 | #define INAT_GRP_MAX ((1 << INAT_GRP_BITS) - 1) | ||
71 | #define INAT_GRP_MASK (INAT_GRP_MAX << INAT_GRP_OFFS) | ||
72 | /* Immediates */ | ||
73 | #define INAT_IMM_OFFS (INAT_GRP_OFFS + INAT_GRP_BITS) | ||
74 | #define INAT_IMM_BITS 3 | ||
75 | #define INAT_IMM_MASK (((1 << INAT_IMM_BITS) - 1) << INAT_IMM_OFFS) | ||
76 | /* Flags */ | ||
77 | #define INAT_FLAG_OFFS (INAT_IMM_OFFS + INAT_IMM_BITS) | ||
78 | #define INAT_REXPFX (1 << INAT_FLAG_OFFS) | ||
79 | #define INAT_MODRM (1 << (INAT_FLAG_OFFS + 1)) | ||
80 | #define INAT_FORCE64 (1 << (INAT_FLAG_OFFS + 2)) | ||
81 | #define INAT_SCNDIMM (1 << (INAT_FLAG_OFFS + 3)) | ||
82 | #define INAT_MOFFSET (1 << (INAT_FLAG_OFFS + 4)) | ||
83 | #define INAT_VARIANT (1 << (INAT_FLAG_OFFS + 5)) | ||
84 | /* Attribute making macros for attribute tables */ | ||
85 | #define INAT_MAKE_PREFIX(pfx) (pfx << INAT_PFX_OFFS) | ||
86 | #define INAT_MAKE_ESCAPE(esc) (esc << INAT_ESC_OFFS) | ||
87 | #define INAT_MAKE_GROUP(grp) ((grp << INAT_GRP_OFFS) | INAT_MODRM) | ||
88 | #define INAT_MAKE_IMM(imm) (imm << INAT_IMM_OFFS) | ||
89 | |||
90 | /* Attribute search APIs */ | ||
91 | extern insn_attr_t inat_get_opcode_attribute(insn_byte_t opcode); | ||
92 | extern insn_attr_t inat_get_escape_attribute(insn_byte_t opcode, | ||
93 | insn_byte_t last_pfx, | ||
94 | insn_attr_t esc_attr); | ||
95 | extern insn_attr_t inat_get_group_attribute(insn_byte_t modrm, | ||
96 | insn_byte_t last_pfx, | ||
97 | insn_attr_t esc_attr); | ||
98 | |||
99 | /* Attribute checking functions */ | ||
100 | static inline int inat_is_prefix(insn_attr_t attr) | ||
101 | { | ||
102 | return attr & INAT_PFX_MASK; | ||
103 | } | ||
104 | |||
105 | static inline int inat_is_address_size_prefix(insn_attr_t attr) | ||
106 | { | ||
107 | return (attr & INAT_PFX_MASK) == INAT_PFX_ADDRSZ; | ||
108 | } | ||
109 | |||
110 | static inline int inat_is_operand_size_prefix(insn_attr_t attr) | ||
111 | { | ||
112 | return (attr & INAT_PFX_MASK) == INAT_PFX_OPNDSZ; | ||
113 | } | ||
114 | |||
115 | static inline int inat_last_prefix_id(insn_attr_t attr) | ||
116 | { | ||
117 | if ((attr & INAT_PFX_MASK) > INAT_LPREFIX_MAX) | ||
118 | return 0; | ||
119 | else | ||
120 | return attr & INAT_PFX_MASK; | ||
121 | } | ||
122 | |||
123 | static inline int inat_is_escape(insn_attr_t attr) | ||
124 | { | ||
125 | return attr & INAT_ESC_MASK; | ||
126 | } | ||
127 | |||
128 | static inline int inat_escape_id(insn_attr_t attr) | ||
129 | { | ||
130 | return (attr & INAT_ESC_MASK) >> INAT_ESC_OFFS; | ||
131 | } | ||
132 | |||
133 | static inline int inat_is_group(insn_attr_t attr) | ||
134 | { | ||
135 | return attr & INAT_GRP_MASK; | ||
136 | } | ||
137 | |||
138 | static inline int inat_group_id(insn_attr_t attr) | ||
139 | { | ||
140 | return (attr & INAT_GRP_MASK) >> INAT_GRP_OFFS; | ||
141 | } | ||
142 | |||
143 | static inline int inat_group_common_attribute(insn_attr_t attr) | ||
144 | { | ||
145 | return attr & ~INAT_GRP_MASK; | ||
146 | } | ||
147 | |||
148 | static inline int inat_has_immediate(insn_attr_t attr) | ||
149 | { | ||
150 | return attr & INAT_IMM_MASK; | ||
151 | } | ||
152 | |||
153 | static inline int inat_immediate_size(insn_attr_t attr) | ||
154 | { | ||
155 | return (attr & INAT_IMM_MASK) >> INAT_IMM_OFFS; | ||
156 | } | ||
157 | |||
158 | static inline int inat_is_rex_prefix(insn_attr_t attr) | ||
159 | { | ||
160 | return attr & INAT_REXPFX; | ||
161 | } | ||
162 | |||
163 | static inline int inat_has_modrm(insn_attr_t attr) | ||
164 | { | ||
165 | return attr & INAT_MODRM; | ||
166 | } | ||
167 | |||
168 | static inline int inat_is_force64(insn_attr_t attr) | ||
169 | { | ||
170 | return attr & INAT_FORCE64; | ||
171 | } | ||
172 | |||
173 | static inline int inat_has_second_immediate(insn_attr_t attr) | ||
174 | { | ||
175 | return attr & INAT_SCNDIMM; | ||
176 | } | ||
177 | |||
178 | static inline int inat_has_moffset(insn_attr_t attr) | ||
179 | { | ||
180 | return attr & INAT_MOFFSET; | ||
181 | } | ||
182 | |||
183 | static inline int inat_has_variant(insn_attr_t attr) | ||
184 | { | ||
185 | return attr & INAT_VARIANT; | ||
186 | } | ||
187 | |||
188 | #endif | ||
diff --git a/arch/x86/include/asm/inat_types.h b/arch/x86/include/asm/inat_types.h new file mode 100644 index 000000000000..cb3c20ce39cf --- /dev/null +++ b/arch/x86/include/asm/inat_types.h | |||
@@ -0,0 +1,29 @@ | |||
1 | #ifndef _ASM_X86_INAT_TYPES_H | ||
2 | #define _ASM_X86_INAT_TYPES_H | ||
3 | /* | ||
4 | * x86 instruction attributes | ||
5 | * | ||
6 | * Written by Masami Hiramatsu <mhiramat@redhat.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | * GNU General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | /* Instruction attributes */ | ||
25 | typedef unsigned int insn_attr_t; | ||
26 | typedef unsigned char insn_byte_t; | ||
27 | typedef signed int insn_value_t; | ||
28 | |||
29 | #endif | ||
diff --git a/arch/x86/include/asm/insn.h b/arch/x86/include/asm/insn.h new file mode 100644 index 000000000000..12b4e3751d3f --- /dev/null +++ b/arch/x86/include/asm/insn.h | |||
@@ -0,0 +1,143 @@ | |||
1 | #ifndef _ASM_X86_INSN_H | ||
2 | #define _ASM_X86_INSN_H | ||
3 | /* | ||
4 | * x86 instruction analysis | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
19 | * | ||
20 | * Copyright (C) IBM Corporation, 2009 | ||
21 | */ | ||
22 | |||
23 | /* insn_attr_t is defined in inat.h */ | ||
24 | #include <asm/inat.h> | ||
25 | |||
26 | struct insn_field { | ||
27 | union { | ||
28 | insn_value_t value; | ||
29 | insn_byte_t bytes[4]; | ||
30 | }; | ||
31 | /* !0 if we've run insn_get_xxx() for this field */ | ||
32 | unsigned char got; | ||
33 | unsigned char nbytes; | ||
34 | }; | ||
35 | |||
36 | struct insn { | ||
37 | struct insn_field prefixes; /* | ||
38 | * Prefixes | ||
39 | * prefixes.bytes[3]: last prefix | ||
40 | */ | ||
41 | struct insn_field rex_prefix; /* REX prefix */ | ||
42 | struct insn_field opcode; /* | ||
43 | * opcode.bytes[0]: opcode1 | ||
44 | * opcode.bytes[1]: opcode2 | ||
45 | * opcode.bytes[2]: opcode3 | ||
46 | */ | ||
47 | struct insn_field modrm; | ||
48 | struct insn_field sib; | ||
49 | struct insn_field displacement; | ||
50 | union { | ||
51 | struct insn_field immediate; | ||
52 | struct insn_field moffset1; /* for 64bit MOV */ | ||
53 | struct insn_field immediate1; /* for 64bit imm or off16/32 */ | ||
54 | }; | ||
55 | union { | ||
56 | struct insn_field moffset2; /* for 64bit MOV */ | ||
57 | struct insn_field immediate2; /* for 64bit imm or seg16 */ | ||
58 | }; | ||
59 | |||
60 | insn_attr_t attr; | ||
61 | unsigned char opnd_bytes; | ||
62 | unsigned char addr_bytes; | ||
63 | unsigned char length; | ||
64 | unsigned char x86_64; | ||
65 | |||
66 | const insn_byte_t *kaddr; /* kernel address of insn to analyze */ | ||
67 | const insn_byte_t *next_byte; | ||
68 | }; | ||
69 | |||
70 | #define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6) | ||
71 | #define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3) | ||
72 | #define X86_MODRM_RM(modrm) ((modrm) & 0x07) | ||
73 | |||
74 | #define X86_SIB_SCALE(sib) (((sib) & 0xc0) >> 6) | ||
75 | #define X86_SIB_INDEX(sib) (((sib) & 0x38) >> 3) | ||
76 | #define X86_SIB_BASE(sib) ((sib) & 0x07) | ||
77 | |||
78 | #define X86_REX_W(rex) ((rex) & 8) | ||
79 | #define X86_REX_R(rex) ((rex) & 4) | ||
80 | #define X86_REX_X(rex) ((rex) & 2) | ||
81 | #define X86_REX_B(rex) ((rex) & 1) | ||
82 | |||
83 | /* The last prefix is needed for two-byte and three-byte opcodes */ | ||
84 | static inline insn_byte_t insn_last_prefix(struct insn *insn) | ||
85 | { | ||
86 | return insn->prefixes.bytes[3]; | ||
87 | } | ||
88 | |||
89 | extern void insn_init(struct insn *insn, const void *kaddr, int x86_64); | ||
90 | extern void insn_get_prefixes(struct insn *insn); | ||
91 | extern void insn_get_opcode(struct insn *insn); | ||
92 | extern void insn_get_modrm(struct insn *insn); | ||
93 | extern void insn_get_sib(struct insn *insn); | ||
94 | extern void insn_get_displacement(struct insn *insn); | ||
95 | extern void insn_get_immediate(struct insn *insn); | ||
96 | extern void insn_get_length(struct insn *insn); | ||
97 | |||
98 | /* Attribute will be determined after getting ModRM (for opcode groups) */ | ||
99 | static inline void insn_get_attribute(struct insn *insn) | ||
100 | { | ||
101 | insn_get_modrm(insn); | ||
102 | } | ||
103 | |||
104 | /* Instruction uses RIP-relative addressing */ | ||
105 | extern int insn_rip_relative(struct insn *insn); | ||
106 | |||
107 | /* Init insn for kernel text */ | ||
108 | static inline void kernel_insn_init(struct insn *insn, const void *kaddr) | ||
109 | { | ||
110 | #ifdef CONFIG_X86_64 | ||
111 | insn_init(insn, kaddr, 1); | ||
112 | #else /* CONFIG_X86_32 */ | ||
113 | insn_init(insn, kaddr, 0); | ||
114 | #endif | ||
115 | } | ||
116 | |||
117 | /* Offset of each field from kaddr */ | ||
118 | static inline int insn_offset_rex_prefix(struct insn *insn) | ||
119 | { | ||
120 | return insn->prefixes.nbytes; | ||
121 | } | ||
122 | static inline int insn_offset_opcode(struct insn *insn) | ||
123 | { | ||
124 | return insn_offset_rex_prefix(insn) + insn->rex_prefix.nbytes; | ||
125 | } | ||
126 | static inline int insn_offset_modrm(struct insn *insn) | ||
127 | { | ||
128 | return insn_offset_opcode(insn) + insn->opcode.nbytes; | ||
129 | } | ||
130 | static inline int insn_offset_sib(struct insn *insn) | ||
131 | { | ||
132 | return insn_offset_modrm(insn) + insn->modrm.nbytes; | ||
133 | } | ||
134 | static inline int insn_offset_displacement(struct insn *insn) | ||
135 | { | ||
136 | return insn_offset_sib(insn) + insn->sib.nbytes; | ||
137 | } | ||
138 | static inline int insn_offset_immediate(struct insn *insn) | ||
139 | { | ||
140 | return insn_offset_displacement(insn) + insn->displacement.nbytes; | ||
141 | } | ||
142 | |||
143 | #endif /* _ASM_X86_INSN_H */ | ||