1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <stdarg.h>
41#include <common.h>
42#include "x86emu/x86emui.h"
43
44
45
46#ifdef DEBUG
47
48static void print_encoded_bytes(u16 s, u16 o);
49static void print_decoded_instruction(void);
50static int x86emu_parse_line(char *s, int *ps, int *n);
51
52
53void 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
65void X86EMU_trace_xregs(void)
66{
67 if (DEBUG_TRACE()) {
68 x86emu_dump_xregs();
69 }
70}
71
72void x86emu_just_disassemble(void)
73{
74
75
76
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
83static void disassemble_forward(u16 seg, u16 off, int n)
84{
85 X86EMU_sysEnv tregs;
86 int i;
87 u8 op1;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 tregs = M;
113 tregs.x86.R_IP = off;
114 tregs.x86.R_CS = seg;
115
116
117 tregs.x86.enc_str_pos = 0;
118 tregs.x86.enc_pos = 0;
119
120
121 tregs.x86.debug |= DEBUG_DISASSEMBLE_F;
122
123
124
125
126
127
128
129
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
136}
137
138void x86emu_check_ip_access(void)
139{
140
141}
142
143void x86emu_check_sp_access(void)
144{
145}
146
147void x86emu_check_mem_access(u32 dummy)
148{
149
150}
151
152void x86emu_check_data_access(uint dummy1, uint dummy2)
153{
154
155}
156
157void x86emu_inc_decoded_inst_len(int x)
158{
159 M.x86.enc_pos += x;
160}
161
162void 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
168void 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
176void x86emu_end_instr(void)
177{
178 M.x86.enc_str_pos = 0;
179 M.x86.enc_pos = 0;
180}
181
182static 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
192static void print_decoded_instruction(void)
193{
194 printk("%s", M.x86.decoded_buf);
195}
196
197void x86emu_print_int_vect(u16 iv)
198{
199 u16 seg, off;
200
201 if (iv > 256)
202 return;
203 seg = fetch_data_word_abs(0, iv * 4);
204 off = fetch_data_word_abs(0, iv * 4 + 2);
205 printk("%04x:%04x ", seg, off);
206}
207
208void X86EMU_dump_memory(u16 seg, u16 off, u32 amt)
209{
210 u32 start = off & 0xfffffff0;
211 u32 end = (off + 16) & 0xfffffff0;
212 u32 i;
213 u32 current;
214
215 current = start;
216 while (end <= off + amt) {
217 printk("%04x:%04x ", seg, start);
218 for (i = start; i < off; i++)
219 printk(" ");
220 for (; i < end; i++)
221 printk("%02x ", fetch_data_byte_abs(seg, i));
222 printk("\n");
223 start = end;
224 end = start + 16;
225 }
226}
227
228void x86emu_single_step(void)
229{
230 char s[1024];
231 int ps[10];
232 int ntok;
233 int cmd;
234 int done;
235 int segment;
236 int offset;
237 static int breakpoint;
238 static int noDecode = 1;
239
240 char *p;
241
242 if (DEBUG_BREAK()) {
243 if (M.x86.saved_ip != breakpoint) {
244 return;
245 } else {
246 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
247 M.x86.debug |= DEBUG_TRACE_F;
248 M.x86.debug &= ~DEBUG_BREAK_F;
249 print_decoded_instruction();
250 X86EMU_trace_regs();
251 }
252 }
253 done = 0;
254 offset = M.x86.saved_ip;
255 while (!done) {
256 printk("-");
257 cmd = x86emu_parse_line(s, ps, &ntok);
258 switch (cmd) {
259 case 'u':
260 disassemble_forward(M.x86.saved_cs, (u16) offset, 10);
261 break;
262 case 'd':
263 if (ntok == 2) {
264 segment = M.x86.saved_cs;
265 offset = ps[1];
266 X86EMU_dump_memory(segment, (u16) offset, 16);
267 offset += 16;
268 } else if (ntok == 3) {
269 segment = ps[1];
270 offset = ps[2];
271 X86EMU_dump_memory(segment, (u16) offset, 16);
272 offset += 16;
273 } else {
274 segment = M.x86.saved_cs;
275 X86EMU_dump_memory(segment, (u16) offset, 16);
276 offset += 16;
277 }
278 break;
279 case 'c':
280 M.x86.debug ^= DEBUG_TRACECALL_F;
281 break;
282 case 's':
283 M.x86.debug ^=
284 DEBUG_SVC_F | DEBUG_SYS_F | DEBUG_SYSINT_F;
285 break;
286 case 'r':
287 X86EMU_trace_regs();
288 break;
289 case 'x':
290 X86EMU_trace_xregs();
291 break;
292 case 'g':
293 if (ntok == 2) {
294 breakpoint = ps[1];
295 if (noDecode) {
296 M.x86.debug |= DEBUG_DECODE_NOPRINT_F;
297 } else {
298 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
299 }
300 M.x86.debug &= ~DEBUG_TRACE_F;
301 M.x86.debug |= DEBUG_BREAK_F;
302 done = 1;
303 }
304 break;
305 case 'q':
306 M.x86.debug |= DEBUG_EXIT;
307 return;
308 case 'P':
309 noDecode = (noDecode) ? 0 : 1;
310 printk("Toggled decoding to %s\n",
311 (noDecode) ? "FALSE" : "TRUE");
312 break;
313 case 't':
314 case 0:
315 done = 1;
316 break;
317 }
318 }
319}
320
321int X86EMU_trace_on(void)
322{
323 return M.x86.debug |= DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F;
324}
325
326int X86EMU_trace_off(void)
327{
328 return M.x86.debug &= ~(DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F);
329}
330
331static int x86emu_parse_line(char *s, int *ps, int *n)
332{
333 int cmd;
334
335 *n = 0;
336 while (*s == ' ' || *s == '\t')
337 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')
350 s++;
351
352 if (*s == '\n')
353 return cmd;
354
355 while (*s == ' ' || *s == '\t')
356 s++;
357
358 *n += 1;
359 }
360}
361
362#endif
363
364void x86emu_dump_regs(void)
365{
366 printk("\tAX=%04x ", M.x86.R_AX);
367 printk("BX=%04x ", M.x86.R_BX);
368 printk("CX=%04x ", M.x86.R_CX);
369 printk("DX=%04x ", M.x86.R_DX);
370 printk("SP=%04x ", M.x86.R_SP);
371 printk("BP=%04x ", M.x86.R_BP);
372 printk("SI=%04x ", M.x86.R_SI);
373 printk("DI=%04x\n", M.x86.R_DI);
374 printk("\tDS=%04x ", M.x86.R_DS);
375 printk("ES=%04x ", M.x86.R_ES);
376 printk("SS=%04x ", M.x86.R_SS);
377 printk("CS=%04x ", M.x86.R_CS);
378 printk("IP=%04x ", M.x86.R_IP);
379 if (ACCESS_FLAG(F_OF))
380 printk("OV ");
381 else
382 printk("NV ");
383 if (ACCESS_FLAG(F_DF))
384 printk("DN ");
385 else
386 printk("UP ");
387 if (ACCESS_FLAG(F_IF))
388 printk("EI ");
389 else
390 printk("DI ");
391 if (ACCESS_FLAG(F_SF))
392 printk("NG ");
393 else
394 printk("PL ");
395 if (ACCESS_FLAG(F_ZF))
396 printk("ZR ");
397 else
398 printk("NZ ");
399 if (ACCESS_FLAG(F_AF))
400 printk("AC ");
401 else
402 printk("NA ");
403 if (ACCESS_FLAG(F_PF))
404 printk("PE ");
405 else
406 printk("PO ");
407 if (ACCESS_FLAG(F_CF))
408 printk("CY ");
409 else
410 printk("NC ");
411 printk("\n");
412}
413
414void x86emu_dump_xregs(void)
415{
416 printk("\tEAX=%08x ", M.x86.R_EAX);
417 printk("EBX=%08x ", M.x86.R_EBX);
418 printk("ECX=%08x ", M.x86.R_ECX);
419 printk("EDX=%08x \n", M.x86.R_EDX);
420 printk("\tESP=%08x ", M.x86.R_ESP);
421 printk("EBP=%08x ", M.x86.R_EBP);
422 printk("ESI=%08x ", M.x86.R_ESI);
423 printk("EDI=%08x\n", M.x86.R_EDI);
424 printk("\tDS=%04x ", M.x86.R_DS);
425 printk("ES=%04x ", M.x86.R_ES);
426 printk("SS=%04x ", M.x86.R_SS);
427 printk("CS=%04x ", M.x86.R_CS);
428 printk("EIP=%08x\n\t", M.x86.R_EIP);
429 if (ACCESS_FLAG(F_OF))
430 printk("OV ");
431 else
432 printk("NV ");
433 if (ACCESS_FLAG(F_DF))
434 printk("DN ");
435 else
436 printk("UP ");
437 if (ACCESS_FLAG(F_IF))
438 printk("EI ");
439 else
440 printk("DI ");
441 if (ACCESS_FLAG(F_SF))
442 printk("NG ");
443 else
444 printk("PL ");
445 if (ACCESS_FLAG(F_ZF))
446 printk("ZR ");
447 else
448 printk("NZ ");
449 if (ACCESS_FLAG(F_AF))
450 printk("AC ");
451 else
452 printk("NA ");
453 if (ACCESS_FLAG(F_PF))
454 printk("PE ");
455 else
456 printk("PO ");
457 if (ACCESS_FLAG(F_CF))
458 printk("CY ");
459 else
460 printk("NC ");
461 printk("\n");
462}
463