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