Leaked source code of windows server 2003
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.

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