insn.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "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 *end_kaddr; /* kernel address of last insn in buffer */
  64. const insn_byte_t *next_byte;
  65. };
  66. #define MAX_INSN_SIZE 15
  67. #define X86_MODRM_MOD(modrm) (((modrm) & 0xc0) >> 6)
  68. #define X86_MODRM_REG(modrm) (((modrm) & 0x38) >> 3)
  69. #define X86_MODRM_RM(modrm) ((modrm) & 0x07)
  70. #define X86_SIB_SCALE(sib) (((sib) & 0xc0) >> 6)
  71. #define X86_SIB_INDEX(sib) (((sib) & 0x38) >> 3)
  72. #define X86_SIB_BASE(sib) ((sib) & 0x07)
  73. #define X86_REX_W(rex) ((rex) & 8)
  74. #define X86_REX_R(rex) ((rex) & 4)
  75. #define X86_REX_X(rex) ((rex) & 2)
  76. #define X86_REX_B(rex) ((rex) & 1)
  77. /* VEX bit flags */
  78. #define X86_VEX_W(vex) ((vex) & 0x80) /* VEX3 Byte2 */
  79. #define X86_VEX_R(vex) ((vex) & 0x80) /* VEX2/3 Byte1 */
  80. #define X86_VEX_X(vex) ((vex) & 0x40) /* VEX3 Byte1 */
  81. #define X86_VEX_B(vex) ((vex) & 0x20) /* VEX3 Byte1 */
  82. #define X86_VEX_L(vex) ((vex) & 0x04) /* VEX3 Byte2, VEX2 Byte1 */
  83. /* VEX bit fields */
  84. #define X86_VEX3_M(vex) ((vex) & 0x1f) /* VEX3 Byte1 */
  85. #define X86_VEX2_M 1 /* VEX2.M always 1 */
  86. #define X86_VEX_V(vex) (((vex) & 0x78) >> 3) /* VEX3 Byte2, VEX2 Byte1 */
  87. #define X86_VEX_P(vex) ((vex) & 0x03) /* VEX3 Byte2, VEX2 Byte1 */
  88. #define X86_VEX_M_MAX 0x1f /* VEX3.M Maximum value */
  89. extern void insn_init(struct insn *insn, const void *kaddr, int buf_len, 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. /* Attribute will be determined after getting ModRM (for opcode groups) */
  98. static inline void insn_get_attribute(struct insn *insn)
  99. {
  100. insn_get_modrm(insn);
  101. }
  102. /* Instruction uses RIP-relative addressing */
  103. extern int insn_rip_relative(struct insn *insn);
  104. /* Init insn for kernel text */
  105. static inline void kernel_insn_init(struct insn *insn,
  106. const void *kaddr, int buf_len)
  107. {
  108. #ifdef CONFIG_X86_64
  109. insn_init(insn, kaddr, buf_len, 1);
  110. #else /* CONFIG_X86_32 */
  111. insn_init(insn, kaddr, buf_len, 0);
  112. #endif
  113. }
  114. static inline int insn_is_avx(struct insn *insn)
  115. {
  116. if (!insn->prefixes.got)
  117. insn_get_prefixes(insn);
  118. return (insn->vex_prefix.value != 0);
  119. }
  120. /* Ensure this instruction is decoded completely */
  121. static inline int insn_complete(struct insn *insn)
  122. {
  123. return insn->opcode.got && insn->modrm.got && insn->sib.got &&
  124. insn->displacement.got && insn->immediate.got;
  125. }
  126. static inline insn_byte_t insn_vex_m_bits(struct insn *insn)
  127. {
  128. if (insn->vex_prefix.nbytes == 2) /* 2 bytes VEX */
  129. return X86_VEX2_M;
  130. else
  131. return X86_VEX3_M(insn->vex_prefix.bytes[1]);
  132. }
  133. static inline insn_byte_t insn_vex_p_bits(struct insn *insn)
  134. {
  135. if (insn->vex_prefix.nbytes == 2) /* 2 bytes VEX */
  136. return X86_VEX_P(insn->vex_prefix.bytes[1]);
  137. else
  138. return X86_VEX_P(insn->vex_prefix.bytes[2]);
  139. }
  140. /* Get the last prefix id from last prefix or VEX prefix */
  141. static inline int insn_last_prefix_id(struct insn *insn)
  142. {
  143. if (insn_is_avx(insn))
  144. return insn_vex_p_bits(insn); /* VEX_p is a SIMD prefix id */
  145. if (insn->prefixes.bytes[3])
  146. return inat_get_last_prefix_id(insn->prefixes.bytes[3]);
  147. return 0;
  148. }
  149. /* Offset of each field from kaddr */
  150. static inline int insn_offset_rex_prefix(struct insn *insn)
  151. {
  152. return insn->prefixes.nbytes;
  153. }
  154. static inline int insn_offset_vex_prefix(struct insn *insn)
  155. {
  156. return insn_offset_rex_prefix(insn) + insn->rex_prefix.nbytes;
  157. }
  158. static inline int insn_offset_opcode(struct insn *insn)
  159. {
  160. return insn_offset_vex_prefix(insn) + insn->vex_prefix.nbytes;
  161. }
  162. static inline int insn_offset_modrm(struct insn *insn)
  163. {
  164. return insn_offset_opcode(insn) + insn->opcode.nbytes;
  165. }
  166. static inline int insn_offset_sib(struct insn *insn)
  167. {
  168. return insn_offset_modrm(insn) + insn->modrm.nbytes;
  169. }
  170. static inline int insn_offset_displacement(struct insn *insn)
  171. {
  172. return insn_offset_sib(insn) + insn->sib.nbytes;
  173. }
  174. static inline int insn_offset_immediate(struct insn *insn)
  175. {
  176. return insn_offset_displacement(insn) + insn->displacement.nbytes;
  177. }
  178. #endif /* _ASM_X86_INSN_H */