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 <linux/ctype.h>
43#include "x86emu/x86emui.h"
44
45
46
47#ifdef CONFIG_X86EMU_DEBUG
48
49static void print_encoded_bytes(u16 s, u16 o);
50static void print_decoded_instruction(void);
51static int x86emu_parse_line(char *s, int *ps, int *n);
52
53
54void X86EMU_trace_regs(void)
55{
56 if (DEBUG_TRACE()) {
57 x86emu_dump_regs();
58 }
59 if (DEBUG_DECODE() && !DEBUG_DECODE_NOPRINT()) {
60 printk("%04x:%04x ", M.x86.saved_cs, M.x86.saved_ip);
61 print_encoded_bytes(M.x86.saved_cs, M.x86.saved_ip);
62 print_decoded_instruction();
63 }
64}
65
66void X86EMU_trace_xregs(void)
67{
68 if (DEBUG_TRACE()) {
69 x86emu_dump_xregs();
70 }
71}
72
73void x86emu_just_disassemble(void)
74{
75
76
77
78
79 printk("%04x:%04x ", M.x86.saved_cs, M.x86.saved_ip);
80 print_encoded_bytes(M.x86.saved_cs, M.x86.saved_ip);
81 print_decoded_instruction();
82}
83
84static void disassemble_forward(u16 seg, u16 off, int n)
85{
86 X86EMU_sysEnv tregs;
87 int i;
88 u8 op1;
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 tregs = M;
114 tregs.x86.R_IP = off;
115 tregs.x86.R_CS = seg;
116
117
118 tregs.x86.enc_str_pos = 0;
119 tregs.x86.enc_pos = 0;
120
121
122 tregs.x86.debug |= DEBUG_DISASSEMBLE_F;
123
124
125
126
127
128
129
130
131
132 for (i = 0; i < n; i++) {
133 op1 = (*sys_rdb) (((u32) M.x86.R_CS << 4) + (M.x86.R_IP++));
134 (x86emu_optab[op1]) (op1);
135 }
136
137}
138
139void x86emu_check_ip_access(void)
140{
141
142}
143
144void x86emu_check_sp_access(void)
145{
146}
147
148void x86emu_check_mem_access(u32 dummy)
149{
150
151}
152
153void x86emu_check_data_access(uint dummy1, uint dummy2)
154{
155
156}
157
158void x86emu_inc_decoded_inst_len(int x)
159{
160 M.x86.enc_pos += x;
161}
162
163void x86emu_decode_printf(char *x)
164{
165 sprintf(M.x86.decoded_buf + M.x86.enc_str_pos, "%s", x);
166 M.x86.enc_str_pos += strlen(x);
167}
168
169void x86emu_decode_printf2(char *x, int y)
170{
171 char temp[100];
172 sprintf(temp, x, y);
173 sprintf(M.x86.decoded_buf + M.x86.enc_str_pos, "%s", temp);
174 M.x86.enc_str_pos += strlen(temp);
175}
176
177void x86emu_end_instr(void)
178{
179 M.x86.enc_str_pos = 0;
180 M.x86.enc_pos = 0;
181}
182
183static void print_encoded_bytes(u16 s, u16 o)
184{
185 int i;
186 char buf1[64];
187 for (i = 0; i < M.x86.enc_pos; i++) {
188 sprintf(buf1 + 2 * i, "%02x", fetch_data_byte_abs(s, o + i));
189 }
190 printk("%-20s", buf1);
191}
192
193static void print_decoded_instruction(void)
194{
195 printk("%s", M.x86.decoded_buf);
196}
197
198void x86emu_print_int_vect(u16 iv)
199{
200 u16 seg, off;
201
202 if (iv > 256)
203 return;
204 seg = fetch_data_word_abs(0, iv * 4);
205 off = fetch_data_word_abs(0, iv * 4 + 2);
206 printk("%04x:%04x ", seg, off);
207}
208
209void X86EMU_dump_memory(u16 seg, u16 off, u32 amt)
210{
211 u32 start = off & 0xfffffff0;
212 u32 end = (off + 16) & 0xfffffff0;
213 u32 i;
214
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
227void 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 if (DEBUG_BREAK()) {
240 if (M.x86.saved_ip != breakpoint) {
241 return;
242 } else {
243 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
244 M.x86.debug |= DEBUG_TRACE_F;
245 M.x86.debug &= ~DEBUG_BREAK_F;
246 print_decoded_instruction();
247 X86EMU_trace_regs();
248 }
249 }
250 done = 0;
251 offset = M.x86.saved_ip;
252 while (!done) {
253 printk("-");
254 ps[1] = 0;
255 ps[2] = 0;
256 cmd = x86emu_parse_line(s, ps, &ntok);
257 switch (cmd) {
258 case 'u':
259 disassemble_forward(M.x86.saved_cs, (u16) offset, 10);
260 break;
261 case 'd':
262 if (ntok == 2) {
263 segment = M.x86.saved_cs;
264 offset = ps[1];
265 X86EMU_dump_memory(segment, (u16) offset, 16);
266 offset += 16;
267 } else if (ntok == 3) {
268 segment = ps[1];
269 offset = ps[2];
270 X86EMU_dump_memory(segment, (u16) offset, 16);
271 offset += 16;
272 } else {
273 segment = M.x86.saved_cs;
274 X86EMU_dump_memory(segment, (u16) offset, 16);
275 offset += 16;
276 }
277 break;
278 case 'c':
279 M.x86.debug ^= DEBUG_TRACECALL_F;
280 break;
281 case 's':
282 M.x86.debug ^=
283 DEBUG_SVC_F | DEBUG_SYS_F | DEBUG_SYSINT_F;
284 break;
285 case 'r':
286 X86EMU_trace_regs();
287 break;
288 case 'x':
289 X86EMU_trace_xregs();
290 break;
291 case 'g':
292 if (ntok == 2) {
293 breakpoint = ps[1];
294 if (noDecode) {
295 M.x86.debug |= DEBUG_DECODE_NOPRINT_F;
296 } else {
297 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
298 }
299 M.x86.debug &= ~DEBUG_TRACE_F;
300 M.x86.debug |= DEBUG_BREAK_F;
301 done = 1;
302 }
303 break;
304 case 'q':
305 M.x86.debug |= DEBUG_EXIT;
306 return;
307 case 'P':
308 noDecode = (noDecode) ? 0 : 1;
309 printk("Toggled decoding to %s\n",
310 (noDecode) ? "false" : "true");
311 break;
312 case 't':
313 case 0:
314 done = 1;
315 break;
316 }
317 }
318}
319
320int X86EMU_trace_on(void)
321{
322 return M.x86.debug |= DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F;
323}
324
325int X86EMU_trace_off(void)
326{
327 return M.x86.debug &= ~(DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F);
328}
329
330static int x86emu_parse_line(char *s, int *ps, int *n)
331{
332 int cmd;
333
334 *n = 0;
335 while (isblank(*s))
336 s++;
337 ps[*n] = *s;
338 switch (*s) {
339 case '\n':
340 *n += 1;
341 return 0;
342 default:
343 cmd = *s;
344 *n += 1;
345 }
346
347 while (1) {
348 while (!isblank(*s) && *s != '\n')
349 s++;
350
351 if (*s == '\n')
352 return cmd;
353
354 while (isblank(*s))
355 s++;
356
357 *n += 1;
358 }
359}
360
361#endif
362
363void x86emu_dump_regs(void)
364{
365 printk("\tAX=%04x ", M.x86.R_AX);
366 printk("BX=%04x ", M.x86.R_BX);
367 printk("CX=%04x ", M.x86.R_CX);
368 printk("DX=%04x ", M.x86.R_DX);
369 printk("SP=%04x ", M.x86.R_SP);
370 printk("BP=%04x ", M.x86.R_BP);
371 printk("SI=%04x ", M.x86.R_SI);
372 printk("DI=%04x\n", M.x86.R_DI);
373 printk("\tDS=%04x ", M.x86.R_DS);
374 printk("ES=%04x ", M.x86.R_ES);
375 printk("SS=%04x ", M.x86.R_SS);
376 printk("CS=%04x ", M.x86.R_CS);
377 printk("IP=%04x ", M.x86.R_IP);
378 if (ACCESS_FLAG(F_OF))
379 printk("OV ");
380 else
381 printk("NV ");
382 if (ACCESS_FLAG(F_DF))
383 printk("DN ");
384 else
385 printk("UP ");
386 if (ACCESS_FLAG(F_IF))
387 printk("EI ");
388 else
389 printk("DI ");
390 if (ACCESS_FLAG(F_SF))
391 printk("NG ");
392 else
393 printk("PL ");
394 if (ACCESS_FLAG(F_ZF))
395 printk("ZR ");
396 else
397 printk("NZ ");
398 if (ACCESS_FLAG(F_AF))
399 printk("AC ");
400 else
401 printk("NA ");
402 if (ACCESS_FLAG(F_PF))
403 printk("PE ");
404 else
405 printk("PO ");
406 if (ACCESS_FLAG(F_CF))
407 printk("CY ");
408 else
409 printk("NC ");
410 printk("\n");
411}
412
413void x86emu_dump_xregs(void)
414{
415 printk("\tEAX=%08x ", M.x86.R_EAX);
416 printk("EBX=%08x ", M.x86.R_EBX);
417 printk("ECX=%08x ", M.x86.R_ECX);
418 printk("EDX=%08x \n", M.x86.R_EDX);
419 printk("\tESP=%08x ", M.x86.R_ESP);
420 printk("EBP=%08x ", M.x86.R_EBP);
421 printk("ESI=%08x ", M.x86.R_ESI);
422 printk("EDI=%08x\n", M.x86.R_EDI);
423 printk("\tDS=%04x ", M.x86.R_DS);
424 printk("ES=%04x ", M.x86.R_ES);
425 printk("SS=%04x ", M.x86.R_SS);
426 printk("CS=%04x ", M.x86.R_CS);
427 printk("EIP=%08x\n\t", M.x86.R_EIP);
428 if (ACCESS_FLAG(F_OF))
429 printk("OV ");
430 else
431 printk("NV ");
432 if (ACCESS_FLAG(F_DF))
433 printk("DN ");
434 else
435 printk("UP ");
436 if (ACCESS_FLAG(F_IF))
437 printk("EI ");
438 else
439 printk("DI ");
440 if (ACCESS_FLAG(F_SF))
441 printk("NG ");
442 else
443 printk("PL ");
444 if (ACCESS_FLAG(F_ZF))
445 printk("ZR ");
446 else
447 printk("NZ ");
448 if (ACCESS_FLAG(F_AF))
449 printk("AC ");
450 else
451 printk("NA ");
452 if (ACCESS_FLAG(F_PF))
453 printk("PE ");
454 else
455 printk("PO ");
456 if (ACCESS_FLAG(F_CF))
457 printk("CY ");
458 else
459 printk("NC ");
460 printk("\n");
461}
462