Masami Hiramatsu | eb13296 | 2009-08-13 16:34:13 -0400 | [diff] [blame] | 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 */ |
Masami Hiramatsu | e0e492e | 2009-10-27 16:42:27 -0400 | [diff] [blame] | 42 | struct insn_field vex_prefix; /* VEX prefix */ |
Masami Hiramatsu | eb13296 | 2009-08-13 16:34:13 -0400 | [diff] [blame] | 43 | struct insn_field opcode; /* |
| 44 | * opcode.bytes[0]: opcode1 |
| 45 | * opcode.bytes[1]: opcode2 |
| 46 | * opcode.bytes[2]: opcode3 |
| 47 | */ |
| 48 | struct insn_field modrm; |
| 49 | struct insn_field sib; |
| 50 | struct insn_field displacement; |
| 51 | union { |
| 52 | struct insn_field immediate; |
| 53 | struct insn_field moffset1; /* for 64bit MOV */ |
| 54 | struct insn_field immediate1; /* for 64bit imm or off16/32 */ |
| 55 | }; |
| 56 | union { |
| 57 | struct insn_field moffset2; /* for 64bit MOV */ |
| 58 | struct insn_field immediate2; /* for 64bit imm or seg16 */ |
| 59 | }; |
| 60 | |
| 61 | insn_attr_t attr; |
| 62 | unsigned char opnd_bytes; |
| 63 | unsigned char addr_bytes; |
| 64 | unsigned char length; |
| 65 | unsigned char x86_64; |
| 66 | |
| 67 | const insn_byte_t *kaddr; /* kernel address of insn to analyze */ |
| 68 | const insn_byte_t *next_byte; |
| 69 | }; |
| 70 | |
Peter Zijlstra | 30a813a | 2010-03-04 13:49:21 +0100 | [diff] [blame] | 71 | #define MAX_INSN_SIZE 16 |
| 72 | |
Masami Hiramatsu | eb13296 | 2009-08-13 16:34:13 -0400 | [diff] [blame] | 73 | #define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6) |
| 74 | #define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3) |
| 75 | #define X86_MODRM_RM(modrm) ((modrm) & 0x07) |
| 76 | |
| 77 | #define X86_SIB_SCALE(sib) (((sib) & 0xc0) >> 6) |
| 78 | #define X86_SIB_INDEX(sib) (((sib) & 0x38) >> 3) |
| 79 | #define X86_SIB_BASE(sib) ((sib) & 0x07) |
| 80 | |
| 81 | #define X86_REX_W(rex) ((rex) & 8) |
| 82 | #define X86_REX_R(rex) ((rex) & 4) |
| 83 | #define X86_REX_X(rex) ((rex) & 2) |
| 84 | #define X86_REX_B(rex) ((rex) & 1) |
| 85 | |
Masami Hiramatsu | e0e492e | 2009-10-27 16:42:27 -0400 | [diff] [blame] | 86 | /* VEX bit flags */ |
| 87 | #define X86_VEX_W(vex) ((vex) & 0x80) /* VEX3 Byte2 */ |
| 88 | #define X86_VEX_R(vex) ((vex) & 0x80) /* VEX2/3 Byte1 */ |
| 89 | #define X86_VEX_X(vex) ((vex) & 0x40) /* VEX3 Byte1 */ |
| 90 | #define X86_VEX_B(vex) ((vex) & 0x20) /* VEX3 Byte1 */ |
| 91 | #define X86_VEX_L(vex) ((vex) & 0x04) /* VEX3 Byte2, VEX2 Byte1 */ |
| 92 | /* VEX bit fields */ |
| 93 | #define X86_VEX3_M(vex) ((vex) & 0x1f) /* VEX3 Byte1 */ |
| 94 | #define X86_VEX2_M 1 /* VEX2.M always 1 */ |
| 95 | #define X86_VEX_V(vex) (((vex) & 0x78) >> 3) /* VEX3 Byte2, VEX2 Byte1 */ |
| 96 | #define X86_VEX_P(vex) ((vex) & 0x03) /* VEX3 Byte2, VEX2 Byte1 */ |
| 97 | #define X86_VEX_M_MAX 0x1f /* VEX3.M Maximum value */ |
| 98 | |
Masami Hiramatsu | eb13296 | 2009-08-13 16:34:13 -0400 | [diff] [blame] | 99 | /* The last prefix is needed for two-byte and three-byte opcodes */ |
| 100 | static inline insn_byte_t insn_last_prefix(struct insn *insn) |
| 101 | { |
| 102 | return insn->prefixes.bytes[3]; |
| 103 | } |
| 104 | |
| 105 | extern void insn_init(struct insn *insn, const void *kaddr, int x86_64); |
| 106 | extern void insn_get_prefixes(struct insn *insn); |
| 107 | extern void insn_get_opcode(struct insn *insn); |
| 108 | extern void insn_get_modrm(struct insn *insn); |
| 109 | extern void insn_get_sib(struct insn *insn); |
| 110 | extern void insn_get_displacement(struct insn *insn); |
| 111 | extern void insn_get_immediate(struct insn *insn); |
| 112 | extern void insn_get_length(struct insn *insn); |
| 113 | |
| 114 | /* Attribute will be determined after getting ModRM (for opcode groups) */ |
| 115 | static inline void insn_get_attribute(struct insn *insn) |
| 116 | { |
| 117 | insn_get_modrm(insn); |
| 118 | } |
| 119 | |
| 120 | /* Instruction uses RIP-relative addressing */ |
| 121 | extern int insn_rip_relative(struct insn *insn); |
| 122 | |
| 123 | /* Init insn for kernel text */ |
| 124 | static inline void kernel_insn_init(struct insn *insn, const void *kaddr) |
| 125 | { |
| 126 | #ifdef CONFIG_X86_64 |
| 127 | insn_init(insn, kaddr, 1); |
| 128 | #else /* CONFIG_X86_32 */ |
| 129 | insn_init(insn, kaddr, 0); |
| 130 | #endif |
| 131 | } |
| 132 | |
Masami Hiramatsu | e0e492e | 2009-10-27 16:42:27 -0400 | [diff] [blame] | 133 | static inline int insn_is_avx(struct insn *insn) |
| 134 | { |
| 135 | if (!insn->prefixes.got) |
| 136 | insn_get_prefixes(insn); |
| 137 | return (insn->vex_prefix.value != 0); |
| 138 | } |
| 139 | |
| 140 | static inline insn_byte_t insn_vex_m_bits(struct insn *insn) |
| 141 | { |
| 142 | if (insn->vex_prefix.nbytes == 2) /* 2 bytes VEX */ |
| 143 | return X86_VEX2_M; |
| 144 | else |
| 145 | return X86_VEX3_M(insn->vex_prefix.bytes[1]); |
| 146 | } |
| 147 | |
| 148 | static inline insn_byte_t insn_vex_p_bits(struct insn *insn) |
| 149 | { |
| 150 | if (insn->vex_prefix.nbytes == 2) /* 2 bytes VEX */ |
| 151 | return X86_VEX_P(insn->vex_prefix.bytes[1]); |
| 152 | else |
| 153 | return X86_VEX_P(insn->vex_prefix.bytes[2]); |
| 154 | } |
| 155 | |
Masami Hiramatsu | eb13296 | 2009-08-13 16:34:13 -0400 | [diff] [blame] | 156 | /* Offset of each field from kaddr */ |
| 157 | static inline int insn_offset_rex_prefix(struct insn *insn) |
| 158 | { |
| 159 | return insn->prefixes.nbytes; |
| 160 | } |
Masami Hiramatsu | e0e492e | 2009-10-27 16:42:27 -0400 | [diff] [blame] | 161 | static inline int insn_offset_vex_prefix(struct insn *insn) |
Masami Hiramatsu | eb13296 | 2009-08-13 16:34:13 -0400 | [diff] [blame] | 162 | { |
| 163 | return insn_offset_rex_prefix(insn) + insn->rex_prefix.nbytes; |
| 164 | } |
Masami Hiramatsu | e0e492e | 2009-10-27 16:42:27 -0400 | [diff] [blame] | 165 | static inline int insn_offset_opcode(struct insn *insn) |
| 166 | { |
| 167 | return insn_offset_vex_prefix(insn) + insn->vex_prefix.nbytes; |
| 168 | } |
Masami Hiramatsu | eb13296 | 2009-08-13 16:34:13 -0400 | [diff] [blame] | 169 | static inline int insn_offset_modrm(struct insn *insn) |
| 170 | { |
| 171 | return insn_offset_opcode(insn) + insn->opcode.nbytes; |
| 172 | } |
| 173 | static inline int insn_offset_sib(struct insn *insn) |
| 174 | { |
| 175 | return insn_offset_modrm(insn) + insn->modrm.nbytes; |
| 176 | } |
| 177 | static inline int insn_offset_displacement(struct insn *insn) |
| 178 | { |
| 179 | return insn_offset_sib(insn) + insn->sib.nbytes; |
| 180 | } |
| 181 | static inline int insn_offset_immediate(struct insn *insn) |
| 182 | { |
| 183 | return insn_offset_displacement(insn) + insn->displacement.nbytes; |
| 184 | } |
| 185 | |
| 186 | #endif /* _ASM_X86_INSN_H */ |