1ece92f85SJason Jin /****************************************************************************
2ece92f85SJason Jin *
3ece92f85SJason Jin *			Realmode X86 Emulator Library
4ece92f85SJason Jin *
5ece92f85SJason Jin *		Copyright (C) 1991-2004 SciTech Software, Inc.
6ece92f85SJason Jin *		     Copyright (C) David Mosberger-Tang
7ece92f85SJason Jin *		       Copyright (C) 1999 Egbert Eich
8ece92f85SJason Jin *
9ece92f85SJason Jin *  ========================================================================
10ece92f85SJason Jin *
11ece92f85SJason Jin *  Permission to use, copy, modify, distribute, and sell this software and
12ece92f85SJason Jin *  its documentation for any purpose is hereby granted without fee,
13ece92f85SJason Jin *  provided that the above copyright notice appear in all copies and that
14ece92f85SJason Jin *  both that copyright notice and this permission notice appear in
15ece92f85SJason Jin *  supporting documentation, and that the name of the authors not be used
16ece92f85SJason Jin *  in advertising or publicity pertaining to distribution of the software
17ece92f85SJason Jin *  without specific, written prior permission.	The authors makes no
18ece92f85SJason Jin *  representations about the suitability of this software for any purpose.
19ece92f85SJason Jin *  It is provided "as is" without express or implied warranty.
20ece92f85SJason Jin *
21ece92f85SJason Jin *  THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22ece92f85SJason Jin *  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
23ece92f85SJason Jin *  EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24ece92f85SJason Jin *  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
25ece92f85SJason Jin *  USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26ece92f85SJason Jin *  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27ece92f85SJason Jin *  PERFORMANCE OF THIS SOFTWARE.
28ece92f85SJason Jin *
29ece92f85SJason Jin *  ========================================================================
30ece92f85SJason Jin *
31ece92f85SJason Jin * Language:	ANSI C
32ece92f85SJason Jin * Environment:	Any
33ece92f85SJason Jin * Developer:	Kendall Bennett
34ece92f85SJason Jin *
35ece92f85SJason Jin * Description:	Header file for debug definitions.
36ece92f85SJason Jin *
37ece92f85SJason Jin ****************************************************************************/
38ece92f85SJason Jin 
39ece92f85SJason Jin #ifndef __X86EMU_DEBUG_H
40ece92f85SJason Jin #define __X86EMU_DEBUG_H
41ece92f85SJason Jin 
42ece92f85SJason Jin /*---------------------- Macros and type definitions ----------------------*/
43ece92f85SJason Jin 
44ece92f85SJason Jin /* checks to be enabled for "runtime" */
45ece92f85SJason Jin 
46ece92f85SJason Jin #define CHECK_IP_FETCH_F		0x1
47ece92f85SJason Jin #define CHECK_SP_ACCESS_F		0x2
48ece92f85SJason Jin #define CHECK_MEM_ACCESS_F		0x4	/*using regular linear pointer */
49ece92f85SJason Jin #define CHECK_DATA_ACCESS_F		0x8	/*using segment:offset */
50ece92f85SJason Jin 
51b3521f2eSSimon Glass #ifdef CONFIG_X86EMU_DEBUG
52ece92f85SJason Jin # define CHECK_IP_FETCH()		(M.x86.check & CHECK_IP_FETCH_F)
53ece92f85SJason Jin # define CHECK_SP_ACCESS()		(M.x86.check & CHECK_SP_ACCESS_F)
54ece92f85SJason Jin # define CHECK_MEM_ACCESS()		(M.x86.check & CHECK_MEM_ACCESS_F)
55ece92f85SJason Jin # define CHECK_DATA_ACCESS()		(M.x86.check & CHECK_DATA_ACCESS_F)
56ece92f85SJason Jin #else
57ece92f85SJason Jin # define CHECK_IP_FETCH()
58ece92f85SJason Jin # define CHECK_SP_ACCESS()
59ece92f85SJason Jin # define CHECK_MEM_ACCESS()
60ece92f85SJason Jin # define CHECK_DATA_ACCESS()
61ece92f85SJason Jin #endif
62ece92f85SJason Jin 
63b3521f2eSSimon Glass #ifdef CONFIG_X86EMU_DEBUG
64ece92f85SJason Jin # define DEBUG_INSTRUMENT()	(M.x86.debug & DEBUG_INSTRUMENT_F)
65ece92f85SJason Jin # define DEBUG_DECODE()		(M.x86.debug & DEBUG_DECODE_F)
66ece92f85SJason Jin # define DEBUG_TRACE()		(M.x86.debug & DEBUG_TRACE_F)
67ece92f85SJason Jin # define DEBUG_STEP()		(M.x86.debug & DEBUG_STEP_F)
68ece92f85SJason Jin # define DEBUG_DISASSEMBLE()	(M.x86.debug & DEBUG_DISASSEMBLE_F)
69ece92f85SJason Jin # define DEBUG_BREAK()		(M.x86.debug & DEBUG_BREAK_F)
70ece92f85SJason Jin # define DEBUG_SVC()		(M.x86.debug & DEBUG_SVC_F)
71ece92f85SJason Jin # define DEBUG_SAVE_IP_CS()	(M.x86.debug & DEBUG_SAVE_CS_IP)
72ece92f85SJason Jin 
73ece92f85SJason Jin # define DEBUG_FS()		(M.x86.debug & DEBUG_FS_F)
74ece92f85SJason Jin # define DEBUG_PROC()		(M.x86.debug & DEBUG_PROC_F)
75ece92f85SJason Jin # define DEBUG_SYSINT()		(M.x86.debug & DEBUG_SYSINT_F)
76ece92f85SJason Jin # define DEBUG_TRACECALL()	(M.x86.debug & DEBUG_TRACECALL_F)
77ece92f85SJason Jin # define DEBUG_TRACECALLREGS()	(M.x86.debug & DEBUG_TRACECALL_REGS_F)
78ece92f85SJason Jin # define DEBUG_SYS()		(M.x86.debug & DEBUG_SYS_F)
79ece92f85SJason Jin # define DEBUG_MEM_TRACE()	(M.x86.debug & DEBUG_MEM_TRACE_F)
80ece92f85SJason Jin # define DEBUG_IO_TRACE()	(M.x86.debug & DEBUG_IO_TRACE_F)
81ece92f85SJason Jin # define DEBUG_DECODE_NOPRINT() (M.x86.debug & DEBUG_DECODE_NOPRINT_F)
82ece92f85SJason Jin #else
83ece92f85SJason Jin # define DEBUG_INSTRUMENT()	0
84ece92f85SJason Jin # define DEBUG_DECODE()		0
85ece92f85SJason Jin # define DEBUG_TRACE()		0
86ece92f85SJason Jin # define DEBUG_STEP()		0
87ece92f85SJason Jin # define DEBUG_DISASSEMBLE()	0
88ece92f85SJason Jin # define DEBUG_BREAK()		0
89ece92f85SJason Jin # define DEBUG_SVC()		0
90ece92f85SJason Jin # define DEBUG_SAVE_IP_CS()	0
91ece92f85SJason Jin # define DEBUG_FS()		0
92ece92f85SJason Jin # define DEBUG_PROC()		0
93ece92f85SJason Jin # define DEBUG_SYSINT()		0
94ece92f85SJason Jin # define DEBUG_TRACECALL()	0
95ece92f85SJason Jin # define DEBUG_TRACECALLREGS()	0
96ece92f85SJason Jin # define DEBUG_SYS()		0
97ece92f85SJason Jin # define DEBUG_MEM_TRACE()	0
98ece92f85SJason Jin # define DEBUG_IO_TRACE()	0
99ece92f85SJason Jin # define DEBUG_DECODE_NOPRINT() 0
100ece92f85SJason Jin #endif
101ece92f85SJason Jin 
1028c6ec412SSimon Glass # define ERR_PRINTF(x)		printf(x)
1038c6ec412SSimon Glass # define ERR_PRINTF2(x, y)	printf(x, y)
1048c6ec412SSimon Glass 
105*e5bc9757SSimon Glass #ifdef CONFIG_X86EMU_DEBUG
1068c6ec412SSimon Glass 
107ece92f85SJason Jin 
108ece92f85SJason Jin # define DECODE_PRINTF(x)	if (DEBUG_DECODE()) \
109ece92f85SJason Jin 				    x86emu_decode_printf(x)
110ece92f85SJason Jin # define DECODE_PRINTF2(x,y)	if (DEBUG_DECODE()) \
111ece92f85SJason Jin 				    x86emu_decode_printf2(x,y)
112ece92f85SJason Jin 
113ece92f85SJason Jin /*
114ece92f85SJason Jin  * The following allow us to look at the bytes of an instruction.  The
115ece92f85SJason Jin  * first INCR_INSTRN_LEN, is called everytime bytes are consumed in
116ece92f85SJason Jin  * the decoding process.  The SAVE_IP_CS is called initially when the
117ece92f85SJason Jin  * major opcode of the instruction is accessed.
118ece92f85SJason Jin  */
119ece92f85SJason Jin #define INC_DECODED_INST_LEN(x)			    \
120ece92f85SJason Jin     if (DEBUG_DECODE())				    \
121ece92f85SJason Jin 	x86emu_inc_decoded_inst_len(x)
122ece92f85SJason Jin 
123ece92f85SJason Jin #define SAVE_IP_CS(x,y)						\
124ece92f85SJason Jin     if (DEBUG_DECODE() | DEBUG_TRACECALL() | DEBUG_BREAK() \
125ece92f85SJason Jin 	      | DEBUG_IO_TRACE() | DEBUG_SAVE_IP_CS()) { \
126ece92f85SJason Jin 	M.x86.saved_cs = x;					\
127ece92f85SJason Jin 	M.x86.saved_ip = y;					\
128ece92f85SJason Jin     }
129ece92f85SJason Jin #else
130ece92f85SJason Jin # define INC_DECODED_INST_LEN(x)
131ece92f85SJason Jin # define DECODE_PRINTF(x)
132ece92f85SJason Jin # define DECODE_PRINTF2(x,y)
133ece92f85SJason Jin # define SAVE_IP_CS(x,y)
134ece92f85SJason Jin #endif
135ece92f85SJason Jin 
136b3521f2eSSimon Glass #ifdef CONFIG_X86EMU_DEBUG
137ece92f85SJason Jin #define TRACE_REGS()					    \
138ece92f85SJason Jin     if (DEBUG_DISASSEMBLE()) {				    \
139ece92f85SJason Jin 	x86emu_just_disassemble();			    \
140ece92f85SJason Jin 	goto EndOfTheInstructionProcedure;		    \
141ece92f85SJason Jin     }							    \
142ece92f85SJason Jin     if (DEBUG_TRACE() || DEBUG_DECODE()) X86EMU_trace_regs()
143ece92f85SJason Jin #else
144ece92f85SJason Jin # define TRACE_REGS()
145ece92f85SJason Jin #endif
146ece92f85SJason Jin 
147b3521f2eSSimon Glass #ifdef CONFIG_X86EMU_DEBUG
148ece92f85SJason Jin # define SINGLE_STEP()	    if (DEBUG_STEP()) x86emu_single_step()
149ece92f85SJason Jin #else
150ece92f85SJason Jin # define SINGLE_STEP()
151ece92f85SJason Jin #endif
152ece92f85SJason Jin 
153ece92f85SJason Jin #define TRACE_AND_STEP()    \
154ece92f85SJason Jin     TRACE_REGS();	    \
155ece92f85SJason Jin     SINGLE_STEP()
156ece92f85SJason Jin 
157b3521f2eSSimon Glass #ifdef CONFIG_X86EMU_DEBUG
158ece92f85SJason Jin # define START_OF_INSTR()
159ece92f85SJason Jin # define END_OF_INSTR()	    EndOfTheInstructionProcedure: x86emu_end_instr();
160ece92f85SJason Jin # define END_OF_INSTR_NO_TRACE()    x86emu_end_instr();
161ece92f85SJason Jin #else
162ece92f85SJason Jin # define START_OF_INSTR()
163ece92f85SJason Jin # define END_OF_INSTR()
164ece92f85SJason Jin # define END_OF_INSTR_NO_TRACE()
165ece92f85SJason Jin #endif
166ece92f85SJason Jin 
167b3521f2eSSimon Glass #ifdef CONFIG_X86EMU_DEBUG
168ece92f85SJason Jin # define  CALL_TRACE(u,v,w,x,s)					\
169ece92f85SJason Jin     if (DEBUG_TRACECALLREGS())					\
170ece92f85SJason Jin 	x86emu_dump_regs();					\
171ece92f85SJason Jin     if (DEBUG_TRACECALL())					\
172ece92f85SJason Jin 	printk("%04x:%04x: CALL %s%04x:%04x\n", u , v, s, w, x);
173ece92f85SJason Jin # define RETURN_TRACE(n,u,v)					\
174ece92f85SJason Jin     if (DEBUG_TRACECALLREGS())					\
175ece92f85SJason Jin 	x86emu_dump_regs();					\
176ece92f85SJason Jin     if (DEBUG_TRACECALL())					\
177ece92f85SJason Jin 	printk("%04x:%04x: %s\n",u,v,n);
178ece92f85SJason Jin #else
179ece92f85SJason Jin # define CALL_TRACE(u,v,w,x,s)
180ece92f85SJason Jin # define RETURN_TRACE(n,u,v)
181ece92f85SJason Jin #endif
182ece92f85SJason Jin 
183b3521f2eSSimon Glass #ifdef CONFIG_X86EMU_DEBUG
184ece92f85SJason Jin #define DB(x)	x
185ece92f85SJason Jin #else
186ece92f85SJason Jin #define DB(x)
187ece92f85SJason Jin #endif
188ece92f85SJason Jin 
189ece92f85SJason Jin /*-------------------------- Function Prototypes --------------------------*/
190ece92f85SJason Jin 
191ece92f85SJason Jin #ifdef	__cplusplus
192ece92f85SJason Jin extern "C" {			/* Use "C" linkage when in C++ mode */
193ece92f85SJason Jin #endif
194ece92f85SJason Jin 
195ece92f85SJason Jin 	extern void x86emu_inc_decoded_inst_len(int x);
196ece92f85SJason Jin 	extern void x86emu_decode_printf(char *x);
197ece92f85SJason Jin 	extern void x86emu_decode_printf2(char *x, int y);
198ece92f85SJason Jin 	extern void x86emu_just_disassemble(void);
199ece92f85SJason Jin 	extern void x86emu_single_step(void);
200ece92f85SJason Jin 	extern void x86emu_end_instr(void);
201ece92f85SJason Jin 	extern void x86emu_dump_regs(void);
202ece92f85SJason Jin 	extern void x86emu_dump_xregs(void);
203ece92f85SJason Jin 	extern void x86emu_print_int_vect(u16 iv);
204ece92f85SJason Jin 	extern void x86emu_instrument_instruction(void);
205ece92f85SJason Jin 	extern void x86emu_check_ip_access(void);
206ece92f85SJason Jin 	extern void x86emu_check_sp_access(void);
207ece92f85SJason Jin 	extern void x86emu_check_mem_access(u32 p);
208ece92f85SJason Jin 	extern void x86emu_check_data_access(uint s, uint o);
209ece92f85SJason Jin 
210ece92f85SJason Jin #ifdef	__cplusplus
211ece92f85SJason Jin }				/* End of "C" linkage for C++	    */
212ece92f85SJason Jin #endif
213ece92f85SJason Jin #endif				/* __X86EMU_DEBUG_H */
214