regs.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /****************************************************************************
  2. *
  3. * Realmode X86 Emulator Library
  4. *
  5. * Copyright (C) 1991-2004 SciTech Software, Inc.
  6. * Copyright (C) David Mosberger-Tang
  7. * Copyright (C) 1999 Egbert Eich
  8. *
  9. * ========================================================================
  10. *
  11. * Permission to use, copy, modify, distribute, and sell this software and
  12. * its documentation for any purpose is hereby granted without fee,
  13. * provided that the above copyright notice appear in all copies and that
  14. * both that copyright notice and this permission notice appear in
  15. * supporting documentation, and that the name of the authors not be used
  16. * in advertising or publicity pertaining to distribution of the software
  17. * without specific, written prior permission. The authors makes no
  18. * representations about the suitability of this software for any purpose.
  19. * It is provided "as is" without express or implied warranty.
  20. *
  21. * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  22. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  23. * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  24. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  25. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  26. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  27. * PERFORMANCE OF THIS SOFTWARE.
  28. *
  29. * ========================================================================
  30. *
  31. * Language: ANSI C
  32. * Environment: Any
  33. * Developer: Kendall Bennett
  34. *
  35. * Description: Header file for x86 register definitions.
  36. *
  37. ****************************************************************************/
  38. #ifndef __X86EMU_REGS_H
  39. #define __X86EMU_REGS_H
  40. /*---------------------- Macros and type definitions ----------------------*/
  41. #pragma pack(1)
  42. /*
  43. * General EAX, EBX, ECX, EDX type registers. Note that for
  44. * portability, and speed, the issue of byte swapping is not addressed
  45. * in the registers. All registers are stored in the default format
  46. * available on the host machine. The only critical issue is that the
  47. * registers should line up EXACTLY in the same manner as they do in
  48. * the 386. That is:
  49. *
  50. * EAX & 0xff === AL
  51. * EAX & 0xffff == AX
  52. *
  53. * etc. The result is that alot of the calculations can then be
  54. * done using the native instruction set fully.
  55. */
  56. #ifdef __BIG_ENDIAN__
  57. typedef struct {
  58. u32 e_reg;
  59. } I32_reg_t;
  60. typedef struct {
  61. u16 filler0, x_reg;
  62. } I16_reg_t;
  63. typedef struct {
  64. u8 filler0, filler1, h_reg, l_reg;
  65. } I8_reg_t;
  66. #else /* !__BIG_ENDIAN__ */
  67. typedef struct {
  68. u32 e_reg;
  69. } I32_reg_t;
  70. typedef struct {
  71. u16 x_reg;
  72. } I16_reg_t;
  73. typedef struct {
  74. u8 l_reg, h_reg;
  75. } I8_reg_t;
  76. #endif /* BIG_ENDIAN */
  77. typedef union {
  78. I32_reg_t I32_reg;
  79. I16_reg_t I16_reg;
  80. I8_reg_t I8_reg;
  81. } i386_general_register;
  82. struct i386_general_regs {
  83. i386_general_register A, B, C, D;
  84. };
  85. typedef struct i386_general_regs Gen_reg_t;
  86. struct i386_special_regs {
  87. i386_general_register SP, BP, SI, DI, IP;
  88. u32 FLAGS;
  89. };
  90. /*
  91. * Segment registers here represent the 16 bit quantities
  92. * CS, DS, ES, SS.
  93. */
  94. #undef CS
  95. #undef DS
  96. #undef SS
  97. #undef ES
  98. #undef FS
  99. #undef GS
  100. struct i386_segment_regs {
  101. u16 CS, DS, SS, ES, FS, GS;
  102. };
  103. /* 8 bit registers */
  104. #define R_AH gen.A.I8_reg.h_reg
  105. #define R_AL gen.A.I8_reg.l_reg
  106. #define R_BH gen.B.I8_reg.h_reg
  107. #define R_BL gen.B.I8_reg.l_reg
  108. #define R_CH gen.C.I8_reg.h_reg
  109. #define R_CL gen.C.I8_reg.l_reg
  110. #define R_DH gen.D.I8_reg.h_reg
  111. #define R_DL gen.D.I8_reg.l_reg
  112. /* 16 bit registers */
  113. #define R_AX gen.A.I16_reg.x_reg
  114. #define R_BX gen.B.I16_reg.x_reg
  115. #define R_CX gen.C.I16_reg.x_reg
  116. #define R_DX gen.D.I16_reg.x_reg
  117. /* 32 bit extended registers */
  118. #define R_EAX gen.A.I32_reg.e_reg
  119. #define R_EBX gen.B.I32_reg.e_reg
  120. #define R_ECX gen.C.I32_reg.e_reg
  121. #define R_EDX gen.D.I32_reg.e_reg
  122. /* special registers */
  123. #define R_SP spc.SP.I16_reg.x_reg
  124. #define R_BP spc.BP.I16_reg.x_reg
  125. #define R_SI spc.SI.I16_reg.x_reg
  126. #define R_DI spc.DI.I16_reg.x_reg
  127. #define R_IP spc.IP.I16_reg.x_reg
  128. #define R_FLG spc.FLAGS
  129. /* special registers */
  130. #define R_SP spc.SP.I16_reg.x_reg
  131. #define R_BP spc.BP.I16_reg.x_reg
  132. #define R_SI spc.SI.I16_reg.x_reg
  133. #define R_DI spc.DI.I16_reg.x_reg
  134. #define R_IP spc.IP.I16_reg.x_reg
  135. #define R_FLG spc.FLAGS
  136. /* special registers */
  137. #define R_ESP spc.SP.I32_reg.e_reg
  138. #define R_EBP spc.BP.I32_reg.e_reg
  139. #define R_ESI spc.SI.I32_reg.e_reg
  140. #define R_EDI spc.DI.I32_reg.e_reg
  141. #define R_EIP spc.IP.I32_reg.e_reg
  142. #define R_EFLG spc.FLAGS
  143. /* segment registers */
  144. #define R_CS seg.CS
  145. #define R_DS seg.DS
  146. #define R_SS seg.SS
  147. #define R_ES seg.ES
  148. #define R_FS seg.FS
  149. #define R_GS seg.GS
  150. /* flag conditions */
  151. #define FB_CF 0x0001 /* CARRY flag */
  152. #define FB_PF 0x0004 /* PARITY flag */
  153. #define FB_AF 0x0010 /* AUX flag */
  154. #define FB_ZF 0x0040 /* ZERO flag */
  155. #define FB_SF 0x0080 /* SIGN flag */
  156. #define FB_TF 0x0100 /* TRAP flag */
  157. #define FB_IF 0x0200 /* INTERRUPT ENABLE flag */
  158. #define FB_DF 0x0400 /* DIR flag */
  159. #define FB_OF 0x0800 /* OVERFLOW flag */
  160. /* 80286 and above always have bit#1 set */
  161. #define F_ALWAYS_ON (0x0002) /* flag bits always on */
  162. /*
  163. * Define a mask for only those flag bits we will ever pass back
  164. * (via PUSHF)
  165. */
  166. #define F_MSK (FB_CF|FB_PF|FB_AF|FB_ZF|FB_SF|FB_TF|FB_IF|FB_DF|FB_OF)
  167. /* following bits masked in to a 16bit quantity */
  168. #define F_CF 0x0001 /* CARRY flag */
  169. #define F_PF 0x0004 /* PARITY flag */
  170. #define F_AF 0x0010 /* AUX flag */
  171. #define F_ZF 0x0040 /* ZERO flag */
  172. #define F_SF 0x0080 /* SIGN flag */
  173. #define F_TF 0x0100 /* TRAP flag */
  174. #define F_IF 0x0200 /* INTERRUPT ENABLE flag */
  175. #define F_DF 0x0400 /* DIR flag */
  176. #define F_OF 0x0800 /* OVERFLOW flag */
  177. #define TOGGLE_FLAG(flag) (M.x86.R_FLG ^= (flag))
  178. #define SET_FLAG(flag) (M.x86.R_FLG |= (flag))
  179. #define CLEAR_FLAG(flag) (M.x86.R_FLG &= ~(flag))
  180. #define ACCESS_FLAG(flag) (M.x86.R_FLG & (flag))
  181. #define CLEARALL_FLAG(m) (M.x86.R_FLG = 0)
  182. #define CONDITIONAL_SET_FLAG(COND,FLAG) \
  183. if (COND) SET_FLAG(FLAG); else CLEAR_FLAG(FLAG)
  184. #define F_PF_CALC 0x010000 /* PARITY flag has been calced */
  185. #define F_ZF_CALC 0x020000 /* ZERO flag has been calced */
  186. #define F_SF_CALC 0x040000 /* SIGN flag has been calced */
  187. #define F_ALL_CALC 0xff0000 /* All have been calced */
  188. /*
  189. * Emulator machine state.
  190. * Segment usage control.
  191. */
  192. #define SYSMODE_SEG_DS_SS 0x00000001
  193. #define SYSMODE_SEGOVR_CS 0x00000002
  194. #define SYSMODE_SEGOVR_DS 0x00000004
  195. #define SYSMODE_SEGOVR_ES 0x00000008
  196. #define SYSMODE_SEGOVR_FS 0x00000010
  197. #define SYSMODE_SEGOVR_GS 0x00000020
  198. #define SYSMODE_SEGOVR_SS 0x00000040
  199. #define SYSMODE_PREFIX_REPE 0x00000080
  200. #define SYSMODE_PREFIX_REPNE 0x00000100
  201. #define SYSMODE_PREFIX_DATA 0x00000200
  202. #define SYSMODE_PREFIX_ADDR 0x00000400
  203. #define SYSMODE_INTR_PENDING 0x10000000
  204. #define SYSMODE_EXTRN_INTR 0x20000000
  205. #define SYSMODE_HALTED 0x40000000
  206. #define SYSMODE_SEGMASK (SYSMODE_SEG_DS_SS | \
  207. SYSMODE_SEGOVR_CS | \
  208. SYSMODE_SEGOVR_DS | \
  209. SYSMODE_SEGOVR_ES | \
  210. SYSMODE_SEGOVR_FS | \
  211. SYSMODE_SEGOVR_GS | \
  212. SYSMODE_SEGOVR_SS)
  213. #define SYSMODE_CLRMASK (SYSMODE_SEG_DS_SS | \
  214. SYSMODE_SEGOVR_CS | \
  215. SYSMODE_SEGOVR_DS | \
  216. SYSMODE_SEGOVR_ES | \
  217. SYSMODE_SEGOVR_FS | \
  218. SYSMODE_SEGOVR_GS | \
  219. SYSMODE_SEGOVR_SS | \
  220. SYSMODE_PREFIX_DATA | \
  221. SYSMODE_PREFIX_ADDR)
  222. #define INTR_SYNCH 0x1
  223. #define INTR_ASYNCH 0x2
  224. #define INTR_HALTED 0x4
  225. typedef struct {
  226. struct i386_general_regs gen;
  227. struct i386_special_regs spc;
  228. struct i386_segment_regs seg;
  229. /*
  230. * MODE contains information on:
  231. * REPE prefix 2 bits repe,repne
  232. * SEGMENT overrides 5 bits normal,DS,SS,CS,ES
  233. * Delayed flag set 3 bits (zero, signed, parity)
  234. * reserved 6 bits
  235. * interrupt # 8 bits instruction raised interrupt
  236. * BIOS video segregs 4 bits
  237. * Interrupt Pending 1 bits
  238. * Extern interrupt 1 bits
  239. * Halted 1 bits
  240. */
  241. long mode;
  242. u8 intno;
  243. volatile int intr; /* mask of pending interrupts */
  244. int debug;
  245. #ifdef DEBUG
  246. int check;
  247. u16 saved_ip;
  248. u16 saved_cs;
  249. int enc_pos;
  250. int enc_str_pos;
  251. char decode_buf[32]; /* encoded byte stream */
  252. char decoded_buf[256]; /* disassembled strings */
  253. #endif
  254. } X86EMU_regs;
  255. /****************************************************************************
  256. REMARKS:
  257. Structure maintaining the emulator machine state.
  258. MEMBERS:
  259. x86 - X86 registers
  260. mem_base - Base real mode memory for the emulator
  261. mem_size - Size of the real mode memory block for the emulator
  262. ****************************************************************************/
  263. #undef x86
  264. typedef struct {
  265. X86EMU_regs x86;
  266. u8 *mem_base;
  267. u32 mem_size;
  268. void *private;
  269. } X86EMU_sysEnv;
  270. #pragma pack()
  271. /*----------------------------- Global Variables --------------------------*/
  272. #ifdef __cplusplus
  273. extern "C" { /* Use "C" linkage when in C++ mode */
  274. #endif
  275. /* Global emulator machine state.
  276. *
  277. * We keep it global to avoid pointer dereferences in the code for speed.
  278. */
  279. extern X86EMU_sysEnv _X86EMU_env;
  280. #define M _X86EMU_env
  281. /*-------------------------- Function Prototypes --------------------------*/
  282. /* Function to log information at runtime */
  283. #ifndef __KERNEL__
  284. void printk(const char *fmt, ...);
  285. #endif
  286. #ifdef __cplusplus
  287. } /* End of "C" linkage for C++ */
  288. #endif
  289. #endif /* __X86EMU_REGS_H */