xref: /freebsd/sys/contrib/x86emu/x86emu.h (revision 561524cfb0caa8b2674f41bd547d20723875d49c)
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 /*	$FreeBSD$	*/
4 
5 /****************************************************************************
6 *
7 *  Realmode X86 Emulator Library
8 *
9 *  Copyright (C) 1996-1999 SciTech Software, Inc.
10 *  Copyright (C) David Mosberger-Tang
11 *  Copyright (C) 1999 Egbert Eich
12 *  Copyright (C) 2007 Joerg Sonnenberger
13 *
14 *  ========================================================================
15 *
16 *  Permission to use, copy, modify, distribute, and sell this software and
17 *  its documentation for any purpose is hereby granted without fee,
18 *  provided that the above copyright notice appear in all copies and that
19 *  both that copyright notice and this permission notice appear in
20 *  supporting documentation, and that the name of the authors not be used
21 *  in advertising or publicity pertaining to distribution of the software
22 *  without specific, written prior permission.  The authors makes no
23 *  representations about the suitability of this software for any purpose.
24 *  It is provided "as is" without express or implied warranty.
25 *
26 *  THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
27 *  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
28 *  EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
29 *  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
30 *  USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
31 *  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
32 *  PERFORMANCE OF THIS SOFTWARE.
33 *
34 ****************************************************************************/
35 
36 #ifndef __X86EMU_X86EMU_H
37 #define __X86EMU_X86EMU_H
38 
39 #include <sys/types.h>
40 #include <sys/endian.h>
41 
42 #ifdef _KERNEL
43 #include <sys/systm.h>
44 #include <machine/setjmp.h>
45 #else
46 #include <setjmp.h>
47 #endif
48 
49 /*
50  * General EAX, EBX, ECX, EDX type registers.  Note that for
51  * portability, and speed, the issue of byte swapping is not addressed
52  * in the registers.  All registers are stored in the default format
53  * available on the host machine.  The only critical issue is that the
54  * registers should line up EXACTLY in the same manner as they do in
55  * the 386.  That is:
56  *
57  * EAX & 0xff  === AL
58  * EAX & 0xffff == AX
59  *
60  * etc.  The result is that alot of the calculations can then be
61  * done using the native instruction set fully.
62  */
63 
64 #ifdef	__BIG_ENDIAN__
65 
66 struct x86emu_register32 {
67 	uint32_t e_reg;
68 };
69 
70 struct x86emu_register16 {
71 	uint16_t filler0;
72 	uint16_t x_reg;
73 };
74 
75 struct x86emu_register8 {
76 	uint8_t filler0, filler1;
77 	uint8_t h_reg, l_reg;
78 };
79 
80 #else /* !__BIG_ENDIAN__ */
81 
82 struct x86emu_register32 {
83 	uint32_t e_reg;
84 };
85 
86 struct x86emu_register16 {
87 	uint16_t x_reg;
88 };
89 
90 struct x86emu_register8 {
91 	uint8_t l_reg, h_reg;
92 };
93 
94 #endif /* BIG_ENDIAN */
95 
96 union x86emu_register {
97 	struct x86emu_register32	I32_reg;
98 	struct x86emu_register16	I16_reg;
99 	struct x86emu_register8		I8_reg;
100 };
101 
102 struct x86emu_regs {
103 	uint16_t		register_cs;
104 	uint16_t		register_ds;
105 	uint16_t		register_es;
106 	uint16_t		register_fs;
107 	uint16_t		register_gs;
108 	uint16_t		register_ss;
109 	uint32_t		register_flags;
110 	union x86emu_register	register_a;
111 	union x86emu_register	register_b;
112 	union x86emu_register	register_c;
113 	union x86emu_register	register_d;
114 
115 	union x86emu_register	register_sp;
116 	union x86emu_register	register_bp;
117 	union x86emu_register	register_si;
118 	union x86emu_register	register_di;
119 	union x86emu_register	register_ip;
120 
121 	/*
122 	 * MODE contains information on:
123 	 *  REPE prefix             2 bits  repe,repne
124 	 *  SEGMENT overrides       5 bits  normal,DS,SS,CS,ES
125 	 *  Delayed flag set        3 bits  (zero, signed, parity)
126 	 *  reserved                6 bits
127 	 *  interrupt #             8 bits  instruction raised interrupt
128 	 *  BIOS video segregs      4 bits
129 	 *  Interrupt Pending       1 bits
130 	 *  Extern interrupt        1 bits
131 	 *  Halted                  1 bits
132 	 */
133 	uint32_t		mode;
134 	volatile int		intr;   /* mask of pending interrupts */
135 	uint8_t			intno;
136 	uint8_t			__pad[3];
137 };
138 
139 struct x86emu {
140 	char			*mem_base;
141 	size_t			mem_size;
142 	void        		*sys_private;
143 	struct x86emu_regs	x86;
144 
145 	jmp_buf		exec_state;
146 
147 	uint64_t	cur_cycles;
148 
149 	unsigned int	cur_mod:2;
150 	unsigned int	cur_rl:3;
151 	unsigned int	cur_rh:3;
152 	uint32_t	cur_offset;
153 
154 	uint8_t  	(*emu_rdb)(struct x86emu *, uint32_t addr);
155 	uint16_t 	(*emu_rdw)(struct x86emu *, uint32_t addr);
156 	uint32_t 	(*emu_rdl)(struct x86emu *, uint32_t addr);
157 	void		(*emu_wrb)(struct x86emu *, uint32_t addr,uint8_t val);
158 	void		(*emu_wrw)(struct x86emu *, uint32_t addr, uint16_t val);
159 	void		(*emu_wrl)(struct x86emu *, uint32_t addr, uint32_t val);
160 
161 	uint8_t  	(*emu_inb)(struct x86emu *, uint16_t addr);
162 	uint16_t 	(*emu_inw)(struct x86emu *, uint16_t addr);
163 	uint32_t 	(*emu_inl)(struct x86emu *, uint16_t addr);
164 	void		(*emu_outb)(struct x86emu *, uint16_t addr, uint8_t val);
165 	void		(*emu_outw)(struct x86emu *, uint16_t addr, uint16_t val);
166 	void		(*emu_outl)(struct x86emu *, uint16_t addr, uint32_t val);
167 
168 	void 		(*_x86emu_intrTab[256])(struct x86emu *, int);
169 };
170 
171 __BEGIN_DECLS
172 
173 /* decode.c */
174 
175 void 	x86emu_exec(struct x86emu *);
176 void	x86emu_exec_call(struct x86emu *, uint16_t, uint16_t);
177 void	x86emu_exec_intr(struct x86emu *, uint8_t);
178 void 	x86emu_halt_sys(struct x86emu *) __dead2;
179 
180 __END_DECLS
181 
182 #endif /* __X86EMU_X86EMU_H */
183