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