insn.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /* insn_attr_t is defined in inat.h */
  23. #include <asm/inat.h>
  24. struct insn_field {
  25. union {
  26. insn_value_t value;
  27. insn_byte_t bytes[4];
  28. };
  29. /* !0 if we've run insn_get_xxx() for this field */
  30. unsigned char got;
  31. unsigned char nbytes;
  32. };
  33. struct insn {
  34. struct insn_field prefixes; /*
  35. * Prefixes
  36. * prefixes.bytes[3]: last prefix
  37. */
  38. struct insn_field rex_prefix; /* REX prefix */
  39. struct insn_field vex_prefix; /* VEX prefix */
  40. struct insn_field opcode; /*
  41. * opcode.bytes[0]: opcode1
  42. * opcode.bytes[1]: opcode2
  43. * opcode.bytes[2]: opcode3
  44. */
  45. struct insn_field modrm;
  46. struct insn_field sib;
  47. struct insn_field displacement;
  48. union {
  49. struct insn_field immediate;
  50. struct insn_field moffset1; /* for 64bit MOV */
  51. struct insn_field immediate1; /* for 64bit imm or off16/32 */
  52. };
  53. union {
  54. struct insn_field moffset2; /* for 64bit MOV */
  55. struct insn_field immediate2; /* for 64bit imm or seg16 */
  56. };
  57. insn_attr_t attr;
  58. unsigned char opnd_bytes;
  59. unsigned char addr_bytes;
  60. unsigned char length;
  61. unsigned char x86_64;
  62. const insn_byte_t *kaddr; /* kernel address of insn to analyze */
  63. const insn_byte_t *next_byte;
  64. };
  65. #define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6)
  66. #define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3)
  67. #define X86_MODRM_RM(modrm) ((modrm) & 0x07)
  68. #define X86_SIB_SCALE(sib) (((sib) & 0xc0) >> 6)
  69. #define X86_SIB_INDEX(sib) (((sib) & 0x38) >> 3)
  70. #define X86_SIB_BASE(sib) ((sib) & 0x07)
  71. #define X86_REX_W(rex) ((rex) & 8)
  72. #define X86_REX_R(rex) ((rex) & 4)
  73. #define X86_REX_X(rex) ((rex) & 2)
  74. #define X86_REX_B(rex) ((rex) & 1)
  75. /* VEX bit flags */
  76. #define X86_VEX_W(vex) ((vex) & 0x80) /* VEX3 Byte2 */
  77. #define X86_VEX_R(vex) ((vex) & 0x80) /* VEX2/3 Byte1 */
  78. #define X86_VEX_X(vex) ((vex) & 0x40) /* VEX3 Byte1 */
  79. #define X86_VEX_B(vex) ((vex) & 0x20) /* VEX3 Byte1 */
  80. #define X86_VEX_L(vex) ((vex) & 0x04) /* VEX3 Byte2, VEX2 Byte1 */
  81. /* VEX bit fields */
  82. #define X86_VEX3_M(vex) ((vex) & 0x1f) /* VEX3 Byte1 */
  83. #define X86_VEX2_M 1 /* VEX2.M always 1 */
  84. #define X86_VEX_V(vex) (((vex) & 0x78) >> 3) /* VEX3 Byte2, VEX2 Byte1 */
  85. #define X86_VEX_P(vex) ((vex) & 0x03) /* VEX3 Byte2, VEX2 Byte1 */
  86. #define X86_VEX_M_MAX 0x1f /* VEX3.M Maximum value */
  87. /* The last prefix is needed for two-byte and three-byte opcodes */
  88. static inline insn_byte_t insn_last_prefix(struct insn *insn)
  89. {
  90. return insn->prefixes.bytes[3];
  91. }
  92. extern void insn_init(struct insn *insn, const void *kaddr, int x86_64);
  93. extern void insn_get_prefixes(struct insn *insn);
  94. extern void insn_get_opcode(struct insn *insn);
  95. extern void insn_get_modrm(struct insn *insn);
  96. extern void insn_get_sib(struct insn *insn);
  97. extern void insn_get_displacement(struct insn *insn);
  98. extern void insn_get_immediate(struct insn *insn);
  99. extern void insn_get_length(struct insn *insn);
  100. /* Attribute will be determined after getting ModRM (for opcode groups) */
  101. static inline void insn_get_attribute(struct insn *insn)
  102. {
  103. insn_get_modrm(insn);
  104. }
  105. /* Instruction uses RIP-relative addressing */
  106. 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)
  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. static inline int insn_is_avx(struct insn *insn)
  117. {
  118. if (!insn->prefixes.got)
  119. insn_get_prefixes(insn);
  120. return (insn->vex_prefix.value != 0);
  121. }
  122. static inline insn_byte_t insn_vex_m_bits(struct insn *insn)
  123. {
  124. if (insn->vex_prefix.nbytes == 2) /* 2 bytes VEX */
  125. return X86_VEX2_M;
  126. else
  127. return X86_VEX3_M(insn->vex_prefix.bytes[1]);
  128. }
  129. static inline insn_byte_t insn_vex_p_bits(struct insn *insn)
  130. {
  131. if (insn->vex_prefix.nbytes == 2) /* 2 bytes VEX */
  132. return X86_VEX_P(insn->vex_prefix.bytes[1]);
  133. else
  134. return X86_VEX_P(insn->vex_prefix.bytes[2]);
  135. }
  136. /* Offset of each field from kaddr */
  137. static inline int insn_offset_rex_prefix(struct insn *insn)
  138. {
  139. return insn->prefixes.nbytes;
  140. }
  141. static inline int insn_offset_vex_prefix(struct insn *insn)
  142. {
  143. return insn_offset_rex_prefix(insn) + insn->rex_prefix.nbytes;
  144. }
  145. static inline int insn_offset_opcode(struct insn *insn)
  146. {
  147. return insn_offset_vex_prefix(insn) + insn->vex_prefix.nbytes;
  148. }
  149. static inline int insn_offset_modrm(struct insn *insn)
  150. {
  151. return insn_offset_opcode(insn) + insn->opcode.nbytes;
  152. }
  153. static inline int insn_offset_sib(struct insn *insn)
  154. {
  155. return insn_offset_modrm(insn) + insn->modrm.nbytes;
  156. }
  157. static inline int insn_offset_displacement(struct insn *insn)
  158. {
  159. return insn_offset_sib(insn) + insn->sib.nbytes;
  160. }
  161. static inline int insn_offset_immediate(struct insn *insn)
  162. {
  163. return insn_offset_displacement(insn) + insn->displacement.nbytes;
  164. }
  165. #endif /* _ASM_X86_INSN_H */