Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

525 lines
14 KiB

  1. /*++
  2. Module Name:
  3. iA32DEF.H
  4. Abstract:
  5. This file defines iA32 macros for iA32Trap.c and Opcode Emulation use
  6. Author:
  7. Environment:
  8. Kernel mode only.
  9. Revision History:
  10. --*/
  11. #define KERNELONLY 1
  12. // #include ks386.inc
  13. // #include callconv.inc // calling convention macros
  14. // #include i386\kimacro.inc
  15. // #include mac386.inc
  16. // #include i386\mi.inc
  17. //
  18. // Equates for exceptions which cause system fatal error
  19. //
  20. #define EXCEPTION_DIVIDED_BY_ZERO 0
  21. #define EXCEPTION_DEBUG 1
  22. #define EXCEPTION_NMI 2
  23. #define EXCEPTION_INT3 3
  24. #define EXCEPTION_BOUND_CHECK 5
  25. #define EXCEPTION_INVALID_OPCODE 6
  26. #define EXCEPTION_NPX_NOT_AVAILABLE 7
  27. #define EXCEPTION_DOUBLE_FAULT 8
  28. #define EXCEPTION_NPX_OVERRUN 9
  29. #define EXCEPTION_INVALID_TSS 0x0A
  30. #define EXCEPTION_SEGMENT_NOT_PRESENT 0x0B
  31. #define EXCEPTION_STACK_FAULT 0x0C
  32. #define EXCEPTION_GP_FAULT 0x0D
  33. #define EXCEPTION_RESERVED_TRAP 0x0F
  34. #define EXCEPTION_NPX_ERROR 0x010
  35. #define EXCEPTION_ALIGNMENT_CHECK 0x011
  36. #define BREAKPOINT_BREAK 0x00
  37. //
  38. // Exception flags
  39. //
  40. #define EXCEPT_UNKNOWN_ACCESS 0
  41. #define EXCEPT_LIMIT_ACCESS 0x10
  42. //
  43. // page fault read/write mask
  44. //
  45. #define ERR_0E_STORE 2
  46. //
  47. // Debug register 6 (dr6) BS (single step) bit mask
  48. //
  49. #define DR6_BS_MASK 0x4000
  50. //
  51. // EFLAGS single step bit
  52. //
  53. #define EFLAGS_TF_BIT 0x100
  54. #define EFLAGS_OF_BIT 0x4000
  55. //
  56. // The mask of selecot's table indicator (ldt or gdt)
  57. //
  58. #define TABLE_INDICATOR_MASK 4
  59. //
  60. // Opcode for Pop SegReg and iret instructions
  61. //
  62. #define POP_DS 0x01F
  63. #define POP_ES 0x07
  64. #define POP_FS 0x0A10F
  65. #define POP_GS 0x0A90F
  66. #define IRET_OP 0x0CF
  67. #define CLI_OP 0x0FA
  68. #define STI_OP 0x0FB
  69. #define PUSHF_OP 0x09C
  70. #define POPF_OP 0x09D
  71. #define INTNN_OP 0x00CD
  72. #define FRSTOR_ECX 0x0021DD9B
  73. #define FWAIT_OP 0x009b
  74. #define GATE_TYPE_386INT 0x0E00
  75. #define GATE_TYPE_386TRAP 0x0F00
  76. #define GATE_TYPE_TASK 0x0500
  77. #define D_GATE 0
  78. #define D_PRESENT 0x08000
  79. #define D_DPL_3 0x06000
  80. #define D_DPL_0 0
  81. //
  82. // Definitions for present 386 trap and interrupt gate attributes
  83. //
  84. #define D_TRAP032 D_PRESENT+D_DPL_0+D_GATE+GATE_TYPE_386TRAP
  85. #define D_TRAP332 D_PRESENT+D_DPL_3+D_GATE+GATE_TYPE_386TRAP
  86. #define D_INT032 D_PRESENT+D_DPL_0+D_GATE+GATE_TYPE_386INT
  87. #define D_INT332 D_PRESENT+D_DPL_3+D_GATE+GATE_TYPE_386INT
  88. #define D_TASK D_PRESENT+D_DPL_0+D_GATE+GATE_TYPE_TASK
  89. //
  90. // Bit patterns for Intercept_Code or Trap_Code,
  91. // patterns used in IIM on IA32 trap
  92. //
  93. #define TRAPCODE_TB 0x0004 // taken branch trap
  94. #define TRAPCODE_SS 0x0008 // single step trap
  95. #define TRAPCODE_B0 0x0010 // Data breakpoint trap
  96. #define TRAPCODE_B1 0x0020
  97. #define TRAPCODE_B2 0x0040
  98. #define TRAPCODE_B3 0x0080
  99. #define INTERCEPT_OS 0x0002 // Operand size
  100. #define INTERCEPT_AS 0x0004 // Address size
  101. #define INTERCEPT_LP 0x0008 // Lock Prefix
  102. #define INTERCEPT_RP 0x0010 // REP prefix
  103. #define INTERCEPT_NP 0x0020 // REPNE prefix
  104. #define INTERCEPT_SP 0x0040 // Segment prefix
  105. #define INTERCEPT_SEG 0x0380 // Segment valuse
  106. #define INTERCEPT_0F 0x0400 // 0F opcode series
  107. #define HARDWARE_VM 0x0800 // VM86 mode
  108. #define HARDWARE_RM 0x1000 // Real Mode
  109. #define HARDWARE_PM 0x2000 // Protect Mode
  110. #define HARDWARE_SS 0x4000 // Stack size, 32 or 16 bits
  111. #define HARDWARE_UR 0x8000 // User or privileged mode
  112. #define MI_SMSW 0x01
  113. #define MI_SMSW_REGOP 0x20
  114. //
  115. // Following MI_*** definitions are created from MI386.INC
  116. //
  117. #define MAX_INSTRUCTION_LENGTH 15
  118. #define MAX_INSTRUCTION_PREFIX_LENGTH 4
  119. #define MI_LOCK_PREFIX 0x0F0
  120. #define MI_REPNE_PREFIX 0x0F2
  121. #define MI_REP_PREFIX 0x0F3
  122. #define MI_SEGCS_PREFIX 0x02E
  123. #define MI_SEGSS_PREFIX 0x036
  124. #define MI_SEGDS_PREFIX 0x03E
  125. #define MI_SEGES_PREFIX 0x026
  126. #define MI_SEGFS_PREFIX 0x064
  127. #define MI_SEGGS_PREFIX 0x065
  128. #define MI_OPERANDSIZE_PREFIX 0x066
  129. #define MI_ADDRESSOVERRIDE_PREFIX 0x067
  130. #define MI_TWO_BYTE 0x0F
  131. #define MI_HLT 0x0F4
  132. #define MI_LTR_LLDT 0
  133. #define MI_LGDT_LIDT_LMSW 0x01
  134. #define MI_MODRM_MASK 0x38
  135. #define MI_LLDT_MASK 0x10
  136. #define MI_LTR_MASK 0x18
  137. #define MI_LGDT_MASK 0x10
  138. #define MI_LIDT_MASK 0x18
  139. #define MI_LMSW_MASK 0x30
  140. #define MI_SPECIAL_MOV_MASK 0x20
  141. #define MI_REP_INS_OUTS 0x0F3
  142. #define MI_MIN_INS_OUTS 0x06C
  143. #define MI_MAX_INS_OUTS 0x06F
  144. #define MI_LMSW_OPCODE 0x001 // second byte of lmsw
  145. #define MI_CLTS_OPCODE 0x006 // second byte of clts
  146. #define MI_GET_CRx_OPCODE 0x020 // mov r32,CRx
  147. #define MI_SET_CRx_OPCODE 0x022 // mov CRx,r32
  148. #define MI_GET_TRx_OPCODE 0x024 // mov r32,TRx
  149. #define MI_SET_TRx_OPCODE 0x026 // mov TRx,r32
  150. #define MI_REGMASK 0x038 // REG field mask
  151. #define MI_REGSHIFT 0x3 // REG field shift
  152. #define MI_REGLMSW 0x030 // REG field for lmsw
  153. #define MI_MODMASK 0x0C0 // MOD field mask
  154. #define MI_MODSHIFT 0x6 // MOD field shift
  155. #define MI_MODMOVSPEC 0x0C0 // MOD field for mov to/from special
  156. #define MI_MODNONE 0
  157. #define MI_RMMASK 0x007 // RM field mask
  158. #define MI_RMBP 0x006 // RM value for bp reg
  159. #define MI_RMSIB 0x004 // RM value for sib
  160. #define MI_SIB_BASEMASK 0x007 // SIB BASE field mask
  161. #define MI_SIB_BASENONE 0x005
  162. #define MI_SIB_BASESHIFT 0
  163. #define MI_SIB_INDEXMASK 0x038
  164. #define MI_SIB_INDEXSHIFT 3
  165. #define MI_SIB_INDEXNONE 0x020
  166. #define MI_SIB_SSMASK 0x0c0
  167. #define MI_SIB_SSSHIFT 0x6
  168. //
  169. // definition for floating status word error mask
  170. //
  171. #define FSW_INVALID_OPERATION 0x0001
  172. #define FSW_DENORMAL 0x0002
  173. #define FSW_ZERO_DIVIDE 0x0004
  174. #define FSW_OVERFLOW 0x0008
  175. #define FSW_UNDERFLOW 0x0010
  176. #define FSW_PRECISION 0x0020
  177. #define FSW_STACK_FAULT 0x0040
  178. #define FSW_ERROR_SUMMARY 0x0080
  179. #define FSW_CONDITION_CODE_0 0x0100
  180. #define FSW_CONDITION_CODE_1 0x0200
  181. #define FSW_CONDITION_CODE_2 0x0400
  182. #define FSW_CONDITION_CODE_3 0x4000
  183. #define FSW_ERR_MASK (FSW_INVALID_OPERATION | FSW_DENORMAL | FSW_ZERO_DIVIDE | FSW_OVERFLOW | FSW_UNDERFLOW | FSW_PRECISION | FSW_STACK_FAULT)
  184. //
  185. // Definitions of the shifts to get to the katmai status and control
  186. // Once the bits are shifted, they are in the same place as the
  187. // 387 status and control, so the masks above work as well
  188. // See the IA64 Application Architecture (Vol 1) for where the
  189. // bit shift values come from
  190. //
  191. #define KATMAI_SHIFT_CONTROL 39
  192. #define KATMAI_SHIFT_STATUS 32
  193. #define CPL_STATE(SegCs) (SegCs & RPL_MASK)
  194. // Use the IIPA since that points to the start of the ia32 instruction
  195. #define EIP(frame) ((ULONG) (frame)->StIIPA & 0xffffffff)
  196. #define ESP(frame) ((ULONG) (frame)->IntSp & 0xffffffff)
  197. #define ECX(frame) ((ULONG) (frame)->IntT2 & 0xffffffff)
  198. #define EDX(frame) ((ULONG) (frame)->IntT3 & 0xffffffff)
  199. #define ISRCode(frame) ((USHORT) ((frame)->StISR) & 0xffff)
  200. #define ISRVector(frame) ((UCHAR) ((frame)->StISR >> 16) & 0xff)
  201. //
  202. // Helpers for instruction decoding
  203. //
  204. BOOLEAN
  205. KiIa32Compute32BitEffectiveAddress (
  206. IN PKTRAP_FRAME Frame,
  207. IN OUT PUCHAR *InstAddr,
  208. OUT PUINT_PTR Addr,
  209. OUT PBOOLEAN RegisterMode
  210. );
  211. NTSTATUS
  212. KiIa32InterceptUnalignedLock (
  213. IN PKTRAP_FRAME TrapFrame
  214. );
  215. NTSTATUS
  216. KiIa32ValidateInstruction (
  217. IN PKTRAP_FRAME TrapFrame
  218. );
  219. //
  220. // The following register indices are valid only if called through
  221. // GetX86Reg (...)
  222. //
  223. #define IA32_REG_EAX 0
  224. #define IA32_REG_ECX 1
  225. #define IA32_REG_EDX 2
  226. #define IA32_REG_EBX 3
  227. #define IA32_REG_ESP 4
  228. #define IA32_REG_EBP 5
  229. #define IA32_REG_ESI 6
  230. #define IA32_REG_EDI 7
  231. #define IA32_DISP_NONE 0x00
  232. #define IA32_DISP8 0x01
  233. #define IA32_DISP16 0x02
  234. //
  235. // x86 Eflags register layout
  236. //
  237. typedef union _IA32_EFLAGS
  238. {
  239. ULONGLONG Value;
  240. struct
  241. {
  242. ULONGLONG cf : 1;
  243. ULONGLONG v1 : 1;
  244. ULONGLONG pf : 1;
  245. ULONGLONG v2 : 1;
  246. ULONGLONG af : 1;
  247. ULONGLONG v3 : 1;
  248. ULONGLONG zf : 1;
  249. ULONGLONG sf : 1;
  250. ULONGLONG tf : 1;
  251. ULONGLONG ifl : 1;
  252. ULONGLONG df : 1;
  253. ULONGLONG of : 1;
  254. ULONGLONG iopl : 2;
  255. ULONGLONG nt : 1;
  256. ULONGLONG v4 : 1;
  257. ULONGLONG rf : 1;
  258. ULONGLONG vm : 1;
  259. ULONGLONG ac : 1;
  260. ULONGLONG vif : 1;
  261. ULONGLONG vip : 1;
  262. ULONGLONG id : 1;
  263. } u;
  264. } IA32_EFLAGS, *PIA32_EFLAGS;
  265. //
  266. // Eflags bits to update
  267. //
  268. #define IA32_EFLAGS_CF 0x0001
  269. #define IA32_EFLAGS_SF 0x0002
  270. #define IA32_EFLAGS_OF 0x0004
  271. #define IA32_EFLAGS_PF 0x0008
  272. #define IA32_EFLAGS_ZF 0x0010
  273. #define IA32_EFLAGS_AF 0x0020
  274. //
  275. // Operand size
  276. //
  277. typedef enum _IA32_OPERAND_SIZE
  278. {
  279. OPERANDSIZE_NONE,
  280. OPERANDSIZE_ONEBYTE,
  281. OPERANDSIZE_TWOBYTES,
  282. OPERANDSIZE_FOURBYTES
  283. } IA32_OPERAND_SIZE;
  284. typedef enum _IA32_OPCODE_PARAMETERS
  285. {
  286. IA32_PARAM_RM8_IMM8,
  287. IA32_PARAM_RM_IMM,
  288. IA32_PARAM_RM_IMM8SIGN,
  289. IA32_PARAM_RM8_R,
  290. IA32_PARAM_RM_R,
  291. IA32_PARAM_R_RM8,
  292. IA32_PARAM_R_RM,
  293. IA32_PARAM_RM8,
  294. IA32_PARAM_RM,
  295. IA32_PARAM_SEGREG_RM8,
  296. IA32_PARAM_SEGREG_RM
  297. } IA32_OPCODE_PARAMETERS;
  298. //
  299. // Opcode decription
  300. //
  301. typedef struct _IA32_OPCODE_DESCRIPTION
  302. {
  303. //
  304. // 1st, 2nd and 3rd byte. The 3rd byte is actually the /Reg bits
  305. //
  306. UCHAR Byte1;
  307. UCHAR Byte2;
  308. UCHAR Byte3;
  309. union
  310. {
  311. UCHAR Value;
  312. struct
  313. {
  314. UCHAR Bytes : 4;
  315. UCHAR RegOpcode : 4;
  316. } m;
  317. } Count;
  318. //
  319. // Parameter of this opcode
  320. //
  321. UCHAR Type;
  322. //
  323. // Opcode
  324. //
  325. UCHAR Opcode;
  326. } IA32_OPCODE_DESCRIPTION, *PIA32_OPCODE_DESCRIPTION;
  327. //
  328. // Specific data structure to represent the lock-prefixed instruction
  329. // operands and immediates.
  330. //
  331. typedef struct _IA32_OPERAND
  332. {
  333. ULONG_PTR v;
  334. BOOLEAN RegisterMode;
  335. } IA32_OPERAND, *PIA32_OPERAND;
  336. typedef union _IA32_PREFIX
  337. {
  338. ULONG Value;
  339. struct _IA32_PREFIX_BITS
  340. {
  341. ULONG Lock : 1;
  342. ULONG RepNe : 1;
  343. ULONG Rep : 1;
  344. ULONG CsOverride : 1;
  345. ULONG SsOverride : 1;
  346. ULONG DsOverride : 1;
  347. ULONG EsOverride : 1;
  348. ULONG FsOverride : 1;
  349. ULONG GsOverride : 1;
  350. ULONG SizeOverride : 1;
  351. ULONG AddressOverride : 1;
  352. } b;
  353. } IA32_PREFIX, *PIA32_PREFIX;
  354. typedef struct _IA32_INSTRUCTION
  355. {
  356. //
  357. // Instruction EIP
  358. //
  359. PCHAR Eip;
  360. //
  361. // Instruction description
  362. //
  363. PIA32_OPCODE_DESCRIPTION Description;
  364. //
  365. // Eflags
  366. //
  367. IA32_EFLAGS Eflags;
  368. //
  369. // Instruction opcode
  370. //
  371. UCHAR Opcode;
  372. //
  373. // Operands size and mask
  374. //
  375. UCHAR OperandSize;
  376. ULONG OperandMask;
  377. //
  378. // Instruction operands
  379. //
  380. IA32_OPERAND Operand1;
  381. IA32_OPERAND Operand2;
  382. //
  383. // Instruction prefixes
  384. //
  385. IA32_PREFIX Prefix;
  386. } IA32_INSTRUCTION, *PIA32_INSTRUCTION;
  387. #if defined(IADBG)
  388. ULONG IA32Debug = 0x000fffff;
  389. #define IA32_DEBUG_INTERCEPTION 0x00000001
  390. #define IA32_DEBUG_EXCEPTION 0x00000002
  391. #define IA32_DEBUG_INTERRUPT 0x00000004
  392. #define IA32_DEBUG_DIVIDE 0x00000010
  393. #define IA32_DEBUG_DEBUG 0x00000020
  394. #define IA32_DEBUG_OVERFLOW 0x00000040
  395. #define IA32_DEBUG_BOUND 0x00000080
  396. #define IA32_DEBUG_INSTRUCTION 0x00000100
  397. #define IA32_DEBUG_NODEVICE 0x00000200
  398. #define IA32_DEBUG_NOTPRESENT 0x00000400
  399. #define IA32_DEBUG_STACK 0x00000800
  400. #define IA32_DEBUG_GPFAULT 0x00001000
  401. #define IA32_DEBUG_FPFAULT 0x00002000
  402. #define IA32_DEBUG_ALIGNMENT 0x00004000
  403. #define IA32_DEBUG_GATE 0x00008000
  404. #define IA32_DEBUG_BREAK 0x00010000
  405. #define IA32_DEBUG_INTNN 0x00020000
  406. #define IA32_DEBUG_FLAG 0x00040000
  407. #define IA32_DEBUG_LOCK 0x00080000
  408. //
  409. // define debug macro
  410. //
  411. #define IF_IA32TRAP_DEBUG( ComponentFlag ) \
  412. if (IA32Debug & (IA32_DEBUG_ ## ComponentFlag))
  413. #else // IADBG
  414. #define IF_IA32TRAP_DEBUG( ComponentFlag ) if (FALSE)
  415. #endif // IADBG