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