x86emu.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* $NetBSD: x86emu.h,v 1.1 2007/12/01 20:14:10 joerg Exp $ */
  2. /* $OpenBSD: x86emu.h,v 1.3 2009/06/06 03:45:05 matthieu Exp $ */
  3. /****************************************************************************
  4. *
  5. * Realmode X86 Emulator Library
  6. *
  7. * Copyright (C) 1996-1999 SciTech Software, Inc.
  8. * Copyright (C) David Mosberger-Tang
  9. * Copyright (C) 1999 Egbert Eich
  10. * Copyright (C) 2007 Joerg Sonnenberger
  11. *
  12. * ========================================================================
  13. *
  14. * Permission to use, copy, modify, distribute, and sell this software and
  15. * its documentation for any purpose is hereby granted without fee,
  16. * provided that the above copyright notice appear in all copies and that
  17. * both that copyright notice and this permission notice appear in
  18. * supporting documentation, and that the name of the authors not be used
  19. * in advertising or publicity pertaining to distribution of the software
  20. * without specific, written prior permission. The authors makes no
  21. * representations about the suitability of this software for any purpose.
  22. * It is provided "as is" without express or implied warranty.
  23. *
  24. * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  25. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  26. * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  27. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  28. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  29. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  30. * PERFORMANCE OF THIS SOFTWARE.
  31. *
  32. ****************************************************************************/
  33. #ifndef __X86EMU_X86EMU_H
  34. #define __X86EMU_X86EMU_H
  35. #include <sys/types.h>
  36. #include <sys/endian.h>
  37. #ifdef _KERNEL
  38. #include <sys/systm.h>
  39. #else
  40. #include <setjmp.h>
  41. #endif
  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. struct x86emu_register32 {
  58. uint32_t e_reg;
  59. };
  60. struct x86emu_register16 {
  61. uint16_t filler0;
  62. uint16_t x_reg;
  63. };
  64. struct x86emu_register8 {
  65. uint8_t filler0, filler1;
  66. uint8_t h_reg, l_reg;
  67. };
  68. #else /* !__BIG_ENDIAN__ */
  69. struct x86emu_register32 {
  70. uint32_t e_reg;
  71. };
  72. struct x86emu_register16 {
  73. uint16_t x_reg;
  74. };
  75. struct x86emu_register8 {
  76. uint8_t l_reg, h_reg;
  77. };
  78. #endif /* BIG_ENDIAN */
  79. union x86emu_register {
  80. struct x86emu_register32 I32_reg;
  81. struct x86emu_register16 I16_reg;
  82. struct x86emu_register8 I8_reg;
  83. };
  84. struct x86emu_regs {
  85. uint16_t register_cs;
  86. uint16_t register_ds;
  87. uint16_t register_es;
  88. uint16_t register_fs;
  89. uint16_t register_gs;
  90. uint16_t register_ss;
  91. uint32_t register_flags;
  92. union x86emu_register register_a;
  93. union x86emu_register register_b;
  94. union x86emu_register register_c;
  95. union x86emu_register register_d;
  96. union x86emu_register register_sp;
  97. union x86emu_register register_bp;
  98. union x86emu_register register_si;
  99. union x86emu_register register_di;
  100. union x86emu_register register_ip;
  101. /*
  102. * MODE contains information on:
  103. * REPE prefix 2 bits repe,repne
  104. * SEGMENT overrides 5 bits normal,DS,SS,CS,ES
  105. * Delayed flag set 3 bits (zero, signed, parity)
  106. * reserved 6 bits
  107. * interrupt # 8 bits instruction raised interrupt
  108. * BIOS video segregs 4 bits
  109. * Interrupt Pending 1 bits
  110. * Extern interrupt 1 bits
  111. * Halted 1 bits
  112. */
  113. uint32_t mode;
  114. volatile int intr; /* mask of pending interrupts */
  115. uint8_t intno;
  116. uint8_t __pad[3];
  117. };
  118. struct x86emu {
  119. char *mem_base;
  120. size_t mem_size;
  121. void *sys_private;
  122. struct x86emu_regs x86;
  123. #ifdef _KERNEL
  124. label_t exec_state;
  125. #else
  126. jmp_buf exec_state;
  127. #endif
  128. uint64_t cur_cycles;
  129. unsigned int cur_mod:2;
  130. unsigned int cur_rl:3;
  131. unsigned int cur_rh:3;
  132. uint32_t cur_offset;
  133. uint8_t (*emu_rdb)(struct x86emu *, uint32_t addr);
  134. uint16_t (*emu_rdw)(struct x86emu *, uint32_t addr);
  135. uint32_t (*emu_rdl)(struct x86emu *, uint32_t addr);
  136. void (*emu_wrb)(struct x86emu *, uint32_t addr,uint8_t val);
  137. void (*emu_wrw)(struct x86emu *, uint32_t addr, uint16_t val);
  138. void (*emu_wrl)(struct x86emu *, uint32_t addr, uint32_t val);
  139. uint8_t (*emu_inb)(struct x86emu *, uint16_t addr);
  140. uint16_t (*emu_inw)(struct x86emu *, uint16_t addr);
  141. uint32_t (*emu_inl)(struct x86emu *, uint16_t addr);
  142. void (*emu_outb)(struct x86emu *, uint16_t addr, uint8_t val);
  143. void (*emu_outw)(struct x86emu *, uint16_t addr, uint16_t val);
  144. void (*emu_outl)(struct x86emu *, uint16_t addr, uint32_t val);
  145. void (*_x86emu_intrTab[256])(struct x86emu *, int);
  146. };
  147. __BEGIN_DECLS
  148. void x86emu_init_default(struct x86emu *);
  149. /* decode.c */
  150. void x86emu_exec(struct x86emu *);
  151. void x86emu_exec_call(struct x86emu *, uint16_t, uint16_t);
  152. void x86emu_exec_intr(struct x86emu *, uint8_t);
  153. void x86emu_halt_sys(struct x86emu *) __dead;
  154. __END_DECLS
  155. #endif /* __X86EMU_X86EMU_H */