#ifndef _ASM_X86_INSN_H #define _ASM_X86_INSN_H /* * x86 instruction analysis * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Copyright (C) IBM Corporation, 2009 */ #ifdef __KERNEL__ #include /* insn_attr_t is defined in inat.h */ #include #else #include "insn_x86_user.h" #include "inat.h" #endif struct insn_field { union { s32 value; u8 bytes[4]; }; bool got; /* true if we've run insn_get_xxx() for this field */ u8 nbytes; }; struct insn { struct insn_field prefixes; /* 4 prefixes */ struct insn_field rex_prefix; /* REX prefix */ struct insn_field opcode; /* * opcode.bytes[0]: opcode1 * opcode.bytes[1]: opcode2 * opcode.bytes[2]: opcode3 */ struct insn_field modrm; struct insn_field sib; struct insn_field displacement; union { struct insn_field immediate; struct insn_field moffset1; /* for 64bit MOV */ struct insn_field immediate1; /* for 64bit imm or off16/32 */ }; union { struct insn_field moffset2; /* for 64bit MOV */ struct insn_field immediate2; /* for 64bit imm or seg16 */ }; insn_attr_t attr; u8 opnd_bytes; u8 addr_bytes; u8 length; bool x86_64; const u8 *kaddr; /* kernel address of insn (copy) to analyze */ const u8 *next_byte; }; #define OPCODE1(insn) ((insn)->opcode.bytes[0]) #define OPCODE2(insn) ((insn)->opcode.bytes[1]) #define OPCODE3(insn) ((insn)->opcode.bytes[2]) #define MODRM_MOD(insn) (((insn)->modrm.value & 0xc0) >> 6) #define MODRM_REG(insn) (((insn)->modrm.value & 0x38) >> 3) #define MODRM_RM(insn) ((insn)->modrm.value & 0x07) #define SIB_SCALE(insn) (((insn)->sib.value & 0xc0) >> 6) #define SIB_INDEX(insn) (((insn)->sib.value & 0x38) >> 3) #define SIB_BASE(insn) ((insn)->sib.value & 0x07) #define REX_W(insn) ((insn)->rex_prefix.value & 8) #define REX_R(insn) ((insn)->rex_prefix.value & 4) #define REX_X(insn) ((insn)->rex_prefix.value & 2) #define REX_B(insn) ((insn)->rex_prefix.value & 1) #define MOFFSET64(insn) (((u64)((insn)->moffset2.value) << 32) | \ (u32)((insn)->moffset1.value)) #define IMMEDIATE64(insn) (((u64)((insn)->immediate2.value) << 32) | \ (u32)((insn)->immediate1.value)) extern void insn_init(struct insn *insn, const u8 *kaddr, bool x86_64); extern void insn_get_prefixes(struct insn *insn); extern void insn_get_opcode(struct insn *insn); extern void insn_get_modrm(struct insn *insn); extern void insn_get_sib(struct insn *insn); extern void insn_get_displacement(struct insn *insn); extern void insn_get_immediate(struct insn *insn); extern void insn_get_length(struct insn *insn); /* Attribute will be determined after getting ModRM (for opcode groups) */ static inline void insn_get_attr(struct insn *insn) { insn_get_modrm(insn); } /* The last prefix is needed for two-byte and three-byte opcodes */ static inline u8 insn_last_prefix(struct insn *insn) { if (insn->prefixes.nbytes == 0) return 0; return (insn)->prefixes.bytes[(insn)->prefixes.nbytes - 1]; } /* Instruction uses RIP-relative addressing */ extern bool insn_rip_relative(struct insn *insn); #ifdef CONFIG_X86_64 /* Init insn for kernel text */ #define insn_init_kernel(insn, kaddr) insn_init(insn, kaddr, 1) #else /* CONFIG_X86_32 */ #define insn_init_kernel(insn, kaddr) insn_init(insn, kaddr, 0) #endif static inline bool insn_field_exists(const struct insn_field *field) { return (field->nbytes > 0); } #endif /* _ASM_X86_INSN_H */