1 #ifndef _ASM_X86_INSN_H
2 #define _ASM_X86_INSN_H
4 * x86 instruction analysis
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.
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.
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.
20 * Copyright (C) IBM Corporation, 2009
23 /* insn_attr_t is defined in inat.h */
31 /* !0 if we've run insn_get_xxx() for this field */
37 struct insn_field prefixes; /*
39 * prefixes.bytes[3]: last prefix
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
47 struct insn_field modrm;
48 struct insn_field sib;
49 struct insn_field displacement;
51 struct insn_field immediate;
52 struct insn_field moffset1; /* for 64bit MOV */
53 struct insn_field immediate1; /* for 64bit imm or off16/32 */
56 struct insn_field moffset2; /* for 64bit MOV */
57 struct insn_field immediate2; /* for 64bit imm or seg16 */
61 unsigned char opnd_bytes;
62 unsigned char addr_bytes;
66 const insn_byte_t *kaddr; /* kernel address of insn to analyze */
67 const insn_byte_t *next_byte;
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)
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)
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)
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)
86 return insn->prefixes.bytes[3];
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);
98 /* Attribute will be determined after getting ModRM (for opcode groups) */
99 static inline void insn_get_attribute(struct insn *insn)
101 insn_get_modrm(insn);
104 /* Instruction uses RIP-relative addressing */
105 extern int insn_rip_relative(struct insn *insn);
107 /* Init insn for kernel text */
108 static inline void kernel_insn_init(struct insn *insn, const void *kaddr)
111 insn_init(insn, kaddr, 1);
112 #else /* CONFIG_X86_32 */
113 insn_init(insn, kaddr, 0);
117 /* Offset of each field from kaddr */
118 static inline int insn_offset_rex_prefix(struct insn *insn)
120 return insn->prefixes.nbytes;
122 static inline int insn_offset_opcode(struct insn *insn)
124 return insn_offset_rex_prefix(insn) + insn->rex_prefix.nbytes;
126 static inline int insn_offset_modrm(struct insn *insn)
128 return insn_offset_opcode(insn) + insn->opcode.nbytes;
130 static inline int insn_offset_sib(struct insn *insn)
132 return insn_offset_modrm(insn) + insn->modrm.nbytes;
134 static inline int insn_offset_displacement(struct insn *insn)
136 return insn_offset_sib(insn) + insn->sib.nbytes;
138 static inline int insn_offset_immediate(struct insn *insn)
140 return insn_offset_displacement(insn) + insn->displacement.nbytes;
143 #endif /* _ASM_X86_INSN_H */