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