xref: /rk3399_rockchip-uboot/drivers/bios_emulator/x86emu/debug.c (revision 5b4de9309d7a03aa1db2e5391ab696363391f460)
1 /****************************************************************************
2 *
3 *                       Realmode X86 Emulator Library
4 *
5 *               Copyright (C) 1991-2004 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:  This file contains the code to handle debugging of the
36 *               emulator.
37 *
38 ****************************************************************************/
39 
40 #include <stdarg.h>
41 
42 #if defined(CONFIG_BIOSEMU)
43 
44 #include "x86emu/x86emui.h"
45 
46 /*----------------------------- Implementation ----------------------------*/
47 
48 #ifdef DEBUG
49 
50 static void print_encoded_bytes(u16 s, u16 o);
51 static void print_decoded_instruction(void);
52 static int parse_line(char *s, int *ps, int *n);
53 
54 /* should look something like debug's output. */
55 void X86EMU_trace_regs(void)
56 {
57 	if (DEBUG_TRACE()) {
58 		x86emu_dump_regs();
59 	}
60 	if (DEBUG_DECODE() && !DEBUG_DECODE_NOPRINT()) {
61 		printk("%04x:%04x ", M.x86.saved_cs, M.x86.saved_ip);
62 		print_encoded_bytes(M.x86.saved_cs, M.x86.saved_ip);
63 		print_decoded_instruction();
64 	}
65 }
66 
67 void X86EMU_trace_xregs(void)
68 {
69 	if (DEBUG_TRACE()) {
70 		x86emu_dump_xregs();
71 	}
72 }
73 
74 void x86emu_just_disassemble(void)
75 {
76 	/*
77 	 * This routine called if the flag DEBUG_DISASSEMBLE is set kind
78 	 * of a hack!
79 	 */
80 	printk("%04x:%04x ", M.x86.saved_cs, M.x86.saved_ip);
81 	print_encoded_bytes(M.x86.saved_cs, M.x86.saved_ip);
82 	print_decoded_instruction();
83 }
84 
85 static void disassemble_forward(u16 seg, u16 off, int n)
86 {
87 	X86EMU_sysEnv tregs;
88 	int i;
89 	u8 op1;
90 	/*
91 	 * hack, hack, hack.  What we do is use the exact machinery set up
92 	 * for execution, except that now there is an additional state
93 	 * flag associated with the "execution", and we are using a copy
94 	 * of the register struct.  All the major opcodes, once fully
95 	 * decoded, have the following two steps: TRACE_REGS(r,m);
96 	 * SINGLE_STEP(r,m); which disappear if DEBUG is not defined to
97 	 * the preprocessor.  The TRACE_REGS macro expands to:
98 	 *
99 	 * if (debug&DEBUG_DISASSEMBLE)
100 	 *     {just_disassemble(); goto EndOfInstruction;}
101 	 *     if (debug&DEBUG_TRACE) trace_regs(r,m);
102 	 *
103 	 * ......  and at the last line of the routine.
104 	 *
105 	 * EndOfInstruction: end_instr();
106 	 *
107 	 * Up to the point where TRACE_REG is expanded, NO modifications
108 	 * are done to any register EXCEPT the IP register, for fetch and
109 	 * decoding purposes.
110 	 *
111 	 * This was done for an entirely different reason, but makes a
112 	 * nice way to get the system to help debug codes.
113 	 */
114 	tregs = M;
115 	tregs.x86.R_IP = off;
116 	tregs.x86.R_CS = seg;
117 
118 	/* reset the decoding buffers */
119 	tregs.x86.enc_str_pos = 0;
120 	tregs.x86.enc_pos = 0;
121 
122 	/* turn on the "disassemble only, no execute" flag */
123 	tregs.x86.debug |= DEBUG_DISASSEMBLE_F;
124 
125 	/* DUMP NEXT n instructions to screen in straight_line fashion */
126 	/*
127 	 * This looks like the regular instruction fetch stream, except
128 	 * that when this occurs, each fetched opcode, upon seeing the
129 	 * DEBUG_DISASSEMBLE flag set, exits immediately after decoding
130 	 * the instruction.  XXX --- CHECK THAT MEM IS NOT AFFECTED!!!
131 	 * Note the use of a copy of the register structure...
132 	 */
133 	for (i = 0; i < n; i++) {
134 		op1 = (*sys_rdb) (((u32) M.x86.R_CS << 4) + (M.x86.R_IP++));
135 		(x86emu_optab[op1]) (op1);
136 	}
137 	/* end major hack mode. */
138 }
139 
140 void x86emu_check_ip_access(void)
141 {
142 	/* NULL as of now */
143 }
144 
145 void x86emu_check_sp_access(void)
146 {
147 }
148 
149 void x86emu_check_mem_access(u32 dummy)
150 {
151 	/*  check bounds, etc */
152 }
153 
154 void x86emu_check_data_access(uint dummy1, uint dummy2)
155 {
156 	/*  check bounds, etc */
157 }
158 
159 void x86emu_inc_decoded_inst_len(int x)
160 {
161 	M.x86.enc_pos += x;
162 }
163 
164 void x86emu_decode_printf(char *x)
165 {
166 	sprintf(M.x86.decoded_buf + M.x86.enc_str_pos, "%s", x);
167 	M.x86.enc_str_pos += strlen(x);
168 }
169 
170 void x86emu_decode_printf2(char *x, int y)
171 {
172 	char temp[100];
173 	sprintf(temp, x, y);
174 	sprintf(M.x86.decoded_buf + M.x86.enc_str_pos, "%s", temp);
175 	M.x86.enc_str_pos += strlen(temp);
176 }
177 
178 void x86emu_end_instr(void)
179 {
180 	M.x86.enc_str_pos = 0;
181 	M.x86.enc_pos = 0;
182 }
183 
184 static void print_encoded_bytes(u16 s, u16 o)
185 {
186 	int i;
187 	char buf1[64];
188 	for (i = 0; i < M.x86.enc_pos; i++) {
189 		sprintf(buf1 + 2 * i, "%02x", fetch_data_byte_abs(s, o + i));
190 	}
191 	printk("%-20s", buf1);
192 }
193 
194 static void print_decoded_instruction(void)
195 {
196 	printk("%s", M.x86.decoded_buf);
197 }
198 
199 void x86emu_print_int_vect(u16 iv)
200 {
201 	u16 seg, off;
202 
203 	if (iv > 256)
204 		return;
205 	seg = fetch_data_word_abs(0, iv * 4);
206 	off = fetch_data_word_abs(0, iv * 4 + 2);
207 	printk("%04x:%04x ", seg, off);
208 }
209 
210 void X86EMU_dump_memory(u16 seg, u16 off, u32 amt)
211 {
212 	u32 start = off & 0xfffffff0;
213 	u32 end = (off + 16) & 0xfffffff0;
214 	u32 i;
215 	u32 current;
216 
217 	current = start;
218 	while (end <= off + amt) {
219 		printk("%04x:%04x ", seg, start);
220 		for (i = start; i < off; i++)
221 			printk("   ");
222 		for (; i < end; i++)
223 			printk("%02x ", fetch_data_byte_abs(seg, i));
224 		printk("\n");
225 		start = end;
226 		end = start + 16;
227 	}
228 }
229 
230 void x86emu_single_step(void)
231 {
232 	char s[1024];
233 	int ps[10];
234 	int ntok;
235 	int cmd;
236 	int done;
237 	int segment;
238 	int offset;
239 	static int breakpoint;
240 	static int noDecode = 1;
241 
242 	char *p;
243 
244 	if (DEBUG_BREAK()) {
245 		if (M.x86.saved_ip != breakpoint) {
246 			return;
247 		} else {
248 			M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
249 			M.x86.debug |= DEBUG_TRACE_F;
250 			M.x86.debug &= ~DEBUG_BREAK_F;
251 			print_decoded_instruction();
252 			X86EMU_trace_regs();
253 		}
254 	}
255 	done = 0;
256 	offset = M.x86.saved_ip;
257 	while (!done) {
258 		printk("-");
259 		cmd = parse_line(s, ps, &ntok);
260 		switch (cmd) {
261 		case 'u':
262 			disassemble_forward(M.x86.saved_cs, (u16) offset, 10);
263 			break;
264 		case 'd':
265 			if (ntok == 2) {
266 				segment = M.x86.saved_cs;
267 				offset = ps[1];
268 				X86EMU_dump_memory(segment, (u16) offset, 16);
269 				offset += 16;
270 			} else if (ntok == 3) {
271 				segment = ps[1];
272 				offset = ps[2];
273 				X86EMU_dump_memory(segment, (u16) offset, 16);
274 				offset += 16;
275 			} else {
276 				segment = M.x86.saved_cs;
277 				X86EMU_dump_memory(segment, (u16) offset, 16);
278 				offset += 16;
279 			}
280 			break;
281 		case 'c':
282 			M.x86.debug ^= DEBUG_TRACECALL_F;
283 			break;
284 		case 's':
285 			M.x86.debug ^=
286 			    DEBUG_SVC_F | DEBUG_SYS_F | DEBUG_SYSINT_F;
287 			break;
288 		case 'r':
289 			X86EMU_trace_regs();
290 			break;
291 		case 'x':
292 			X86EMU_trace_xregs();
293 			break;
294 		case 'g':
295 			if (ntok == 2) {
296 				breakpoint = ps[1];
297 				if (noDecode) {
298 					M.x86.debug |= DEBUG_DECODE_NOPRINT_F;
299 				} else {
300 					M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
301 				}
302 				M.x86.debug &= ~DEBUG_TRACE_F;
303 				M.x86.debug |= DEBUG_BREAK_F;
304 				done = 1;
305 			}
306 			break;
307 		case 'q':
308 			M.x86.debug |= DEBUG_EXIT;
309 			return;
310 		case 'P':
311 			noDecode = (noDecode) ? 0 : 1;
312 			printk("Toggled decoding to %s\n",
313 			       (noDecode) ? "FALSE" : "TRUE");
314 			break;
315 		case 't':
316 		case 0:
317 			done = 1;
318 			break;
319 		}
320 	}
321 }
322 
323 int X86EMU_trace_on(void)
324 {
325 	return M.x86.debug |= DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F;
326 }
327 
328 int X86EMU_trace_off(void)
329 {
330 	return M.x86.debug &= ~(DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F);
331 }
332 
333 static int parse_line(char *s, int *ps, int *n)
334 {
335 	int cmd;
336 
337 	*n = 0;
338 	while (*s == ' ' || *s == '\t')
339 		s++;
340 	ps[*n] = *s;
341 	switch (*s) {
342 	case '\n':
343 		*n += 1;
344 		return 0;
345 	default:
346 		cmd = *s;
347 		*n += 1;
348 	}
349 
350 	while (1) {
351 		while (*s != ' ' && *s != '\t' && *s != '\n')
352 			s++;
353 
354 		if (*s == '\n')
355 			return cmd;
356 
357 		while (*s == ' ' || *s == '\t')
358 			s++;
359 
360 		*n += 1;
361 	}
362 }
363 
364 #endif				/* DEBUG */
365 
366 void x86emu_dump_regs(void)
367 {
368 	printk("\tAX=%04x  ", M.x86.R_AX);
369 	printk("BX=%04x  ", M.x86.R_BX);
370 	printk("CX=%04x  ", M.x86.R_CX);
371 	printk("DX=%04x  ", M.x86.R_DX);
372 	printk("SP=%04x  ", M.x86.R_SP);
373 	printk("BP=%04x  ", M.x86.R_BP);
374 	printk("SI=%04x  ", M.x86.R_SI);
375 	printk("DI=%04x\n", M.x86.R_DI);
376 	printk("\tDS=%04x  ", M.x86.R_DS);
377 	printk("ES=%04x  ", M.x86.R_ES);
378 	printk("SS=%04x  ", M.x86.R_SS);
379 	printk("CS=%04x  ", M.x86.R_CS);
380 	printk("IP=%04x   ", M.x86.R_IP);
381 	if (ACCESS_FLAG(F_OF))
382 		printk("OV ");	/* CHECKED... */
383 	else
384 		printk("NV ");
385 	if (ACCESS_FLAG(F_DF))
386 		printk("DN ");
387 	else
388 		printk("UP ");
389 	if (ACCESS_FLAG(F_IF))
390 		printk("EI ");
391 	else
392 		printk("DI ");
393 	if (ACCESS_FLAG(F_SF))
394 		printk("NG ");
395 	else
396 		printk("PL ");
397 	if (ACCESS_FLAG(F_ZF))
398 		printk("ZR ");
399 	else
400 		printk("NZ ");
401 	if (ACCESS_FLAG(F_AF))
402 		printk("AC ");
403 	else
404 		printk("NA ");
405 	if (ACCESS_FLAG(F_PF))
406 		printk("PE ");
407 	else
408 		printk("PO ");
409 	if (ACCESS_FLAG(F_CF))
410 		printk("CY ");
411 	else
412 		printk("NC ");
413 	printk("\n");
414 }
415 
416 void x86emu_dump_xregs(void)
417 {
418 	printk("\tEAX=%08x  ", M.x86.R_EAX);
419 	printk("EBX=%08x  ", M.x86.R_EBX);
420 	printk("ECX=%08x  ", M.x86.R_ECX);
421 	printk("EDX=%08x  \n", M.x86.R_EDX);
422 	printk("\tESP=%08x  ", M.x86.R_ESP);
423 	printk("EBP=%08x  ", M.x86.R_EBP);
424 	printk("ESI=%08x  ", M.x86.R_ESI);
425 	printk("EDI=%08x\n", M.x86.R_EDI);
426 	printk("\tDS=%04x  ", M.x86.R_DS);
427 	printk("ES=%04x  ", M.x86.R_ES);
428 	printk("SS=%04x  ", M.x86.R_SS);
429 	printk("CS=%04x  ", M.x86.R_CS);
430 	printk("EIP=%08x\n\t", M.x86.R_EIP);
431 	if (ACCESS_FLAG(F_OF))
432 		printk("OV ");	/* CHECKED... */
433 	else
434 		printk("NV ");
435 	if (ACCESS_FLAG(F_DF))
436 		printk("DN ");
437 	else
438 		printk("UP ");
439 	if (ACCESS_FLAG(F_IF))
440 		printk("EI ");
441 	else
442 		printk("DI ");
443 	if (ACCESS_FLAG(F_SF))
444 		printk("NG ");
445 	else
446 		printk("PL ");
447 	if (ACCESS_FLAG(F_ZF))
448 		printk("ZR ");
449 	else
450 		printk("NZ ");
451 	if (ACCESS_FLAG(F_AF))
452 		printk("AC ");
453 	else
454 		printk("NA ");
455 	if (ACCESS_FLAG(F_PF))
456 		printk("PE ");
457 	else
458 		printk("PO ");
459 	if (ACCESS_FLAG(F_CF))
460 		printk("CY ");
461 	else
462 		printk("NC ");
463 	printk("\n");
464 }
465 
466 #endif
467