1197007Sdelphij/*	$NetBSD: x86emu.h,v 1.1 2007/12/01 20:14:10 joerg Exp $	*/
2197007Sdelphij/*	$OpenBSD: x86emu.h,v 1.3 2009/06/06 03:45:05 matthieu Exp $ */
3197019Sdelphij/*	$FreeBSD$	*/
4197007Sdelphij
5197007Sdelphij/****************************************************************************
6197007Sdelphij*
7197007Sdelphij*  Realmode X86 Emulator Library
8197007Sdelphij*
9197007Sdelphij*  Copyright (C) 1996-1999 SciTech Software, Inc.
10197007Sdelphij*  Copyright (C) David Mosberger-Tang
11197007Sdelphij*  Copyright (C) 1999 Egbert Eich
12197007Sdelphij*  Copyright (C) 2007 Joerg Sonnenberger
13197007Sdelphij*
14197007Sdelphij*  ========================================================================
15197007Sdelphij*
16197007Sdelphij*  Permission to use, copy, modify, distribute, and sell this software and
17197007Sdelphij*  its documentation for any purpose is hereby granted without fee,
18197007Sdelphij*  provided that the above copyright notice appear in all copies and that
19197007Sdelphij*  both that copyright notice and this permission notice appear in
20197007Sdelphij*  supporting documentation, and that the name of the authors not be used
21197007Sdelphij*  in advertising or publicity pertaining to distribution of the software
22197007Sdelphij*  without specific, written prior permission.  The authors makes no
23197007Sdelphij*  representations about the suitability of this software for any purpose.
24197007Sdelphij*  It is provided "as is" without express or implied warranty.
25197007Sdelphij*
26197007Sdelphij*  THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
27197007Sdelphij*  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
28197007Sdelphij*  EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
29197007Sdelphij*  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
30197007Sdelphij*  USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
31197007Sdelphij*  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
32197007Sdelphij*  PERFORMANCE OF THIS SOFTWARE.
33197007Sdelphij*
34197007Sdelphij****************************************************************************/
35197007Sdelphij
36197007Sdelphij#ifndef __X86EMU_X86EMU_H
37197007Sdelphij#define __X86EMU_X86EMU_H
38197007Sdelphij
39197007Sdelphij#include <sys/types.h>
40197007Sdelphij#include <sys/endian.h>
41197007Sdelphij
42197007Sdelphij#ifdef _KERNEL
43197007Sdelphij#include <sys/systm.h>
44197019Sdelphij#include <machine/setjmp.h>
45197007Sdelphij#else
46197007Sdelphij#include <setjmp.h>
47197007Sdelphij#endif
48197007Sdelphij
49197007Sdelphij/*
50197007Sdelphij * General EAX, EBX, ECX, EDX type registers.  Note that for
51197007Sdelphij * portability, and speed, the issue of byte swapping is not addressed
52197007Sdelphij * in the registers.  All registers are stored in the default format
53197007Sdelphij * available on the host machine.  The only critical issue is that the
54197007Sdelphij * registers should line up EXACTLY in the same manner as they do in
55197007Sdelphij * the 386.  That is:
56197007Sdelphij *
57197007Sdelphij * EAX & 0xff  === AL
58197007Sdelphij * EAX & 0xffff == AX
59197007Sdelphij *
60197007Sdelphij * etc.  The result is that alot of the calculations can then be
61197007Sdelphij * done using the native instruction set fully.
62197007Sdelphij */
63197007Sdelphij
64197007Sdelphij#ifdef	__BIG_ENDIAN__
65197007Sdelphij
66197007Sdelphijstruct x86emu_register32 {
67197007Sdelphij	uint32_t e_reg;
68197007Sdelphij};
69197007Sdelphij
70197007Sdelphijstruct x86emu_register16 {
71197007Sdelphij	uint16_t filler0;
72197007Sdelphij	uint16_t x_reg;
73197007Sdelphij};
74197007Sdelphij
75197007Sdelphijstruct x86emu_register8 {
76197007Sdelphij	uint8_t filler0, filler1;
77197007Sdelphij	uint8_t h_reg, l_reg;
78197007Sdelphij};
79197007Sdelphij
80197007Sdelphij#else /* !__BIG_ENDIAN__ */
81197007Sdelphij
82197007Sdelphijstruct x86emu_register32 {
83197007Sdelphij	uint32_t e_reg;
84197007Sdelphij};
85197007Sdelphij
86197007Sdelphijstruct x86emu_register16 {
87197007Sdelphij	uint16_t x_reg;
88197007Sdelphij};
89197007Sdelphij
90197007Sdelphijstruct x86emu_register8 {
91197007Sdelphij	uint8_t l_reg, h_reg;
92197007Sdelphij};
93197007Sdelphij
94197007Sdelphij#endif /* BIG_ENDIAN */
95197007Sdelphij
96197007Sdelphijunion x86emu_register {
97197007Sdelphij	struct x86emu_register32	I32_reg;
98197007Sdelphij	struct x86emu_register16	I16_reg;
99197007Sdelphij	struct x86emu_register8		I8_reg;
100197007Sdelphij};
101197007Sdelphij
102197007Sdelphijstruct x86emu_regs {
103197007Sdelphij	uint16_t		register_cs;
104197007Sdelphij	uint16_t		register_ds;
105197007Sdelphij	uint16_t		register_es;
106197007Sdelphij	uint16_t		register_fs;
107197007Sdelphij	uint16_t		register_gs;
108197007Sdelphij	uint16_t		register_ss;
109197007Sdelphij	uint32_t		register_flags;
110197007Sdelphij	union x86emu_register	register_a;
111197007Sdelphij	union x86emu_register	register_b;
112197007Sdelphij	union x86emu_register	register_c;
113197007Sdelphij	union x86emu_register	register_d;
114197007Sdelphij
115197007Sdelphij	union x86emu_register	register_sp;
116197007Sdelphij	union x86emu_register	register_bp;
117197007Sdelphij	union x86emu_register	register_si;
118197007Sdelphij	union x86emu_register	register_di;
119197007Sdelphij	union x86emu_register	register_ip;
120197007Sdelphij
121197007Sdelphij	/*
122197007Sdelphij	 * MODE contains information on:
123197007Sdelphij	 *  REPE prefix             2 bits  repe,repne
124197007Sdelphij	 *  SEGMENT overrides       5 bits  normal,DS,SS,CS,ES
125197007Sdelphij	 *  Delayed flag set        3 bits  (zero, signed, parity)
126197007Sdelphij	 *  reserved                6 bits
127197007Sdelphij	 *  interrupt #             8 bits  instruction raised interrupt
128197007Sdelphij	 *  BIOS video segregs      4 bits
129197007Sdelphij	 *  Interrupt Pending       1 bits
130197007Sdelphij	 *  Extern interrupt        1 bits
131197007Sdelphij	 *  Halted                  1 bits
132197007Sdelphij	 */
133197007Sdelphij	uint32_t		mode;
134197007Sdelphij	volatile int		intr;   /* mask of pending interrupts */
135197007Sdelphij	uint8_t			intno;
136197007Sdelphij	uint8_t			__pad[3];
137197007Sdelphij};
138197007Sdelphij
139197007Sdelphijstruct x86emu {
140197007Sdelphij	char			*mem_base;
141197007Sdelphij	size_t			mem_size;
142197007Sdelphij	void        		*sys_private;
143197007Sdelphij	struct x86emu_regs	x86;
144197007Sdelphij
145197007Sdelphij	jmp_buf		exec_state;
146197007Sdelphij
147197007Sdelphij	uint64_t	cur_cycles;
148197007Sdelphij
149197007Sdelphij	unsigned int	cur_mod:2;
150197007Sdelphij	unsigned int	cur_rl:3;
151197007Sdelphij	unsigned int	cur_rh:3;
152197007Sdelphij	uint32_t	cur_offset;
153197007Sdelphij
154197007Sdelphij	uint8_t  	(*emu_rdb)(struct x86emu *, uint32_t addr);
155197007Sdelphij	uint16_t 	(*emu_rdw)(struct x86emu *, uint32_t addr);
156197007Sdelphij	uint32_t 	(*emu_rdl)(struct x86emu *, uint32_t addr);
157197007Sdelphij	void		(*emu_wrb)(struct x86emu *, uint32_t addr,uint8_t val);
158197007Sdelphij	void		(*emu_wrw)(struct x86emu *, uint32_t addr, uint16_t val);
159197007Sdelphij	void		(*emu_wrl)(struct x86emu *, uint32_t addr, uint32_t val);
160197007Sdelphij
161197007Sdelphij	uint8_t  	(*emu_inb)(struct x86emu *, uint16_t addr);
162197007Sdelphij	uint16_t 	(*emu_inw)(struct x86emu *, uint16_t addr);
163197007Sdelphij	uint32_t 	(*emu_inl)(struct x86emu *, uint16_t addr);
164197007Sdelphij	void		(*emu_outb)(struct x86emu *, uint16_t addr, uint8_t val);
165197007Sdelphij	void		(*emu_outw)(struct x86emu *, uint16_t addr, uint16_t val);
166197007Sdelphij	void		(*emu_outl)(struct x86emu *, uint16_t addr, uint32_t val);
167197007Sdelphij
168197007Sdelphij	void 		(*_x86emu_intrTab[256])(struct x86emu *, int);
169197007Sdelphij};
170197007Sdelphij
171197007Sdelphij__BEGIN_DECLS
172197007Sdelphij
173197007Sdelphijvoid	x86emu_init_default(struct x86emu *);
174197007Sdelphij
175197007Sdelphij/* decode.c */
176197007Sdelphij
177197007Sdelphijvoid 	x86emu_exec(struct x86emu *);
178197007Sdelphijvoid	x86emu_exec_call(struct x86emu *, uint16_t, uint16_t);
179197007Sdelphijvoid	x86emu_exec_intr(struct x86emu *, uint8_t);
180197019Sdelphijvoid 	x86emu_halt_sys(struct x86emu *) __dead2;
181197007Sdelphij
182197007Sdelphij__END_DECLS
183197007Sdelphij
184197007Sdelphij#endif /* __X86EMU_X86EMU_H */
185