xref: /freebsd/sys/contrib/x86emu/x86emu.h (revision efba048eb589d0403e45570940e4dfa1afa9dfac)
1884e6d60SXin LI /*	$NetBSD: x86emu.h,v 1.1 2007/12/01 20:14:10 joerg Exp $	*/
2884e6d60SXin LI /*	$OpenBSD: x86emu.h,v 1.3 2009/06/06 03:45:05 matthieu Exp $ */
3efba048eSXin LI /*	$FreeBSD$	*/
4884e6d60SXin LI 
5884e6d60SXin LI /****************************************************************************
6884e6d60SXin LI *
7884e6d60SXin LI *  Realmode X86 Emulator Library
8884e6d60SXin LI *
9884e6d60SXin LI *  Copyright (C) 1996-1999 SciTech Software, Inc.
10884e6d60SXin LI *  Copyright (C) David Mosberger-Tang
11884e6d60SXin LI *  Copyright (C) 1999 Egbert Eich
12884e6d60SXin LI *  Copyright (C) 2007 Joerg Sonnenberger
13884e6d60SXin LI *
14884e6d60SXin LI *  ========================================================================
15884e6d60SXin LI *
16884e6d60SXin LI *  Permission to use, copy, modify, distribute, and sell this software and
17884e6d60SXin LI *  its documentation for any purpose is hereby granted without fee,
18884e6d60SXin LI *  provided that the above copyright notice appear in all copies and that
19884e6d60SXin LI *  both that copyright notice and this permission notice appear in
20884e6d60SXin LI *  supporting documentation, and that the name of the authors not be used
21884e6d60SXin LI *  in advertising or publicity pertaining to distribution of the software
22884e6d60SXin LI *  without specific, written prior permission.  The authors makes no
23884e6d60SXin LI *  representations about the suitability of this software for any purpose.
24884e6d60SXin LI *  It is provided "as is" without express or implied warranty.
25884e6d60SXin LI *
26884e6d60SXin LI *  THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
27884e6d60SXin LI *  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
28884e6d60SXin LI *  EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
29884e6d60SXin LI *  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
30884e6d60SXin LI *  USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
31884e6d60SXin LI *  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
32884e6d60SXin LI *  PERFORMANCE OF THIS SOFTWARE.
33884e6d60SXin LI *
34884e6d60SXin LI ****************************************************************************/
35884e6d60SXin LI 
36884e6d60SXin LI #ifndef __X86EMU_X86EMU_H
37884e6d60SXin LI #define __X86EMU_X86EMU_H
38884e6d60SXin LI 
39884e6d60SXin LI #include <sys/types.h>
40884e6d60SXin LI #include <sys/endian.h>
41884e6d60SXin LI 
42884e6d60SXin LI #ifdef _KERNEL
43884e6d60SXin LI #include <sys/systm.h>
44efba048eSXin LI #include <machine/setjmp.h>
45884e6d60SXin LI #else
46884e6d60SXin LI #include <setjmp.h>
47884e6d60SXin LI #endif
48884e6d60SXin LI 
49884e6d60SXin LI /*
50884e6d60SXin LI  * General EAX, EBX, ECX, EDX type registers.  Note that for
51884e6d60SXin LI  * portability, and speed, the issue of byte swapping is not addressed
52884e6d60SXin LI  * in the registers.  All registers are stored in the default format
53884e6d60SXin LI  * available on the host machine.  The only critical issue is that the
54884e6d60SXin LI  * registers should line up EXACTLY in the same manner as they do in
55884e6d60SXin LI  * the 386.  That is:
56884e6d60SXin LI  *
57884e6d60SXin LI  * EAX & 0xff  === AL
58884e6d60SXin LI  * EAX & 0xffff == AX
59884e6d60SXin LI  *
60884e6d60SXin LI  * etc.  The result is that alot of the calculations can then be
61884e6d60SXin LI  * done using the native instruction set fully.
62884e6d60SXin LI  */
63884e6d60SXin LI 
64884e6d60SXin LI #ifdef	__BIG_ENDIAN__
65884e6d60SXin LI 
66884e6d60SXin LI struct x86emu_register32 {
67884e6d60SXin LI 	uint32_t e_reg;
68884e6d60SXin LI };
69884e6d60SXin LI 
70884e6d60SXin LI struct x86emu_register16 {
71884e6d60SXin LI 	uint16_t filler0;
72884e6d60SXin LI 	uint16_t x_reg;
73884e6d60SXin LI };
74884e6d60SXin LI 
75884e6d60SXin LI struct x86emu_register8 {
76884e6d60SXin LI 	uint8_t filler0, filler1;
77884e6d60SXin LI 	uint8_t h_reg, l_reg;
78884e6d60SXin LI };
79884e6d60SXin LI 
80884e6d60SXin LI #else /* !__BIG_ENDIAN__ */
81884e6d60SXin LI 
82884e6d60SXin LI struct x86emu_register32 {
83884e6d60SXin LI 	uint32_t e_reg;
84884e6d60SXin LI };
85884e6d60SXin LI 
86884e6d60SXin LI struct x86emu_register16 {
87884e6d60SXin LI 	uint16_t x_reg;
88884e6d60SXin LI };
89884e6d60SXin LI 
90884e6d60SXin LI struct x86emu_register8 {
91884e6d60SXin LI 	uint8_t l_reg, h_reg;
92884e6d60SXin LI };
93884e6d60SXin LI 
94884e6d60SXin LI #endif /* BIG_ENDIAN */
95884e6d60SXin LI 
96884e6d60SXin LI union x86emu_register {
97884e6d60SXin LI 	struct x86emu_register32	I32_reg;
98884e6d60SXin LI 	struct x86emu_register16	I16_reg;
99884e6d60SXin LI 	struct x86emu_register8		I8_reg;
100884e6d60SXin LI };
101884e6d60SXin LI 
102884e6d60SXin LI struct x86emu_regs {
103884e6d60SXin LI 	uint16_t		register_cs;
104884e6d60SXin LI 	uint16_t		register_ds;
105884e6d60SXin LI 	uint16_t		register_es;
106884e6d60SXin LI 	uint16_t		register_fs;
107884e6d60SXin LI 	uint16_t		register_gs;
108884e6d60SXin LI 	uint16_t		register_ss;
109884e6d60SXin LI 	uint32_t		register_flags;
110884e6d60SXin LI 	union x86emu_register	register_a;
111884e6d60SXin LI 	union x86emu_register	register_b;
112884e6d60SXin LI 	union x86emu_register	register_c;
113884e6d60SXin LI 	union x86emu_register	register_d;
114884e6d60SXin LI 
115884e6d60SXin LI 	union x86emu_register	register_sp;
116884e6d60SXin LI 	union x86emu_register	register_bp;
117884e6d60SXin LI 	union x86emu_register	register_si;
118884e6d60SXin LI 	union x86emu_register	register_di;
119884e6d60SXin LI 	union x86emu_register	register_ip;
120884e6d60SXin LI 
121884e6d60SXin LI 	/*
122884e6d60SXin LI 	 * MODE contains information on:
123884e6d60SXin LI 	 *  REPE prefix             2 bits  repe,repne
124884e6d60SXin LI 	 *  SEGMENT overrides       5 bits  normal,DS,SS,CS,ES
125884e6d60SXin LI 	 *  Delayed flag set        3 bits  (zero, signed, parity)
126884e6d60SXin LI 	 *  reserved                6 bits
127884e6d60SXin LI 	 *  interrupt #             8 bits  instruction raised interrupt
128884e6d60SXin LI 	 *  BIOS video segregs      4 bits
129884e6d60SXin LI 	 *  Interrupt Pending       1 bits
130884e6d60SXin LI 	 *  Extern interrupt        1 bits
131884e6d60SXin LI 	 *  Halted                  1 bits
132884e6d60SXin LI 	 */
133884e6d60SXin LI 	uint32_t		mode;
134884e6d60SXin LI 	volatile int		intr;   /* mask of pending interrupts */
135884e6d60SXin LI 	uint8_t			intno;
136884e6d60SXin LI 	uint8_t			__pad[3];
137884e6d60SXin LI };
138884e6d60SXin LI 
139884e6d60SXin LI struct x86emu {
140884e6d60SXin LI 	char			*mem_base;
141884e6d60SXin LI 	size_t			mem_size;
142884e6d60SXin LI 	void        		*sys_private;
143884e6d60SXin LI 	struct x86emu_regs	x86;
144884e6d60SXin LI 
145884e6d60SXin LI 	jmp_buf		exec_state;
146884e6d60SXin LI 
147884e6d60SXin LI 	uint64_t	cur_cycles;
148884e6d60SXin LI 
149884e6d60SXin LI 	unsigned int	cur_mod:2;
150884e6d60SXin LI 	unsigned int	cur_rl:3;
151884e6d60SXin LI 	unsigned int	cur_rh:3;
152884e6d60SXin LI 	uint32_t	cur_offset;
153884e6d60SXin LI 
154884e6d60SXin LI 	uint8_t  	(*emu_rdb)(struct x86emu *, uint32_t addr);
155884e6d60SXin LI 	uint16_t 	(*emu_rdw)(struct x86emu *, uint32_t addr);
156884e6d60SXin LI 	uint32_t 	(*emu_rdl)(struct x86emu *, uint32_t addr);
157884e6d60SXin LI 	void		(*emu_wrb)(struct x86emu *, uint32_t addr,uint8_t val);
158884e6d60SXin LI 	void		(*emu_wrw)(struct x86emu *, uint32_t addr, uint16_t val);
159884e6d60SXin LI 	void		(*emu_wrl)(struct x86emu *, uint32_t addr, uint32_t val);
160884e6d60SXin LI 
161884e6d60SXin LI 	uint8_t  	(*emu_inb)(struct x86emu *, uint16_t addr);
162884e6d60SXin LI 	uint16_t 	(*emu_inw)(struct x86emu *, uint16_t addr);
163884e6d60SXin LI 	uint32_t 	(*emu_inl)(struct x86emu *, uint16_t addr);
164884e6d60SXin LI 	void		(*emu_outb)(struct x86emu *, uint16_t addr, uint8_t val);
165884e6d60SXin LI 	void		(*emu_outw)(struct x86emu *, uint16_t addr, uint16_t val);
166884e6d60SXin LI 	void		(*emu_outl)(struct x86emu *, uint16_t addr, uint32_t val);
167884e6d60SXin LI 
168884e6d60SXin LI 	void 		(*_x86emu_intrTab[256])(struct x86emu *, int);
169884e6d60SXin LI };
170884e6d60SXin LI 
171884e6d60SXin LI __BEGIN_DECLS
172884e6d60SXin LI 
173884e6d60SXin LI void	x86emu_init_default(struct x86emu *);
174884e6d60SXin LI 
175884e6d60SXin LI /* decode.c */
176884e6d60SXin LI 
177884e6d60SXin LI void 	x86emu_exec(struct x86emu *);
178884e6d60SXin LI void	x86emu_exec_call(struct x86emu *, uint16_t, uint16_t);
179884e6d60SXin LI void	x86emu_exec_intr(struct x86emu *, uint8_t);
180efba048eSXin LI void 	x86emu_halt_sys(struct x86emu *) __dead2;
181884e6d60SXin LI 
182884e6d60SXin LI __END_DECLS
183884e6d60SXin LI 
184884e6d60SXin LI #endif /* __X86EMU_X86EMU_H */
185