Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 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 | |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 40 | #include <stdarg.h> |
Michal Simek | c73a477 | 2007-08-16 10:46:28 +0200 | [diff] [blame] | 41 | #include <common.h> |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 42 | #include <linux/ctype.h> |
Michal Simek | 952d861 | 2007-08-15 21:15:05 +0200 | [diff] [blame] | 43 | #include "x86emu/x86emui.h" |
| 44 | |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 45 | /*----------------------------- Implementation ----------------------------*/ |
| 46 | |
| 47 | #ifdef DEBUG |
| 48 | |
| 49 | static void print_encoded_bytes(u16 s, u16 o); |
| 50 | static void print_decoded_instruction(void); |
Jason Jin | 7be6e5b | 2008-10-15 10:40:24 +0800 | [diff] [blame] | 51 | static int x86emu_parse_line(char *s, int *ps, int *n); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 52 | |
| 53 | /* should look something like debug's output. */ |
| 54 | void 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 | |
| 66 | void X86EMU_trace_xregs(void) |
| 67 | { |
| 68 | if (DEBUG_TRACE()) { |
| 69 | x86emu_dump_xregs(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void x86emu_just_disassemble(void) |
| 74 | { |
| 75 | /* |
| 76 | * This routine called if the flag DEBUG_DISASSEMBLE is set kind |
| 77 | * of a hack! |
| 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 | |
| 84 | static void disassemble_forward(u16 seg, u16 off, int n) |
| 85 | { |
| 86 | X86EMU_sysEnv tregs; |
| 87 | int i; |
| 88 | u8 op1; |
| 89 | /* |
| 90 | * hack, hack, hack. What we do is use the exact machinery set up |
| 91 | * for execution, except that now there is an additional state |
| 92 | * flag associated with the "execution", and we are using a copy |
| 93 | * of the register struct. All the major opcodes, once fully |
| 94 | * decoded, have the following two steps: TRACE_REGS(r,m); |
| 95 | * SINGLE_STEP(r,m); which disappear if DEBUG is not defined to |
| 96 | * the preprocessor. The TRACE_REGS macro expands to: |
| 97 | * |
| 98 | * if (debug&DEBUG_DISASSEMBLE) |
| 99 | * {just_disassemble(); goto EndOfInstruction;} |
| 100 | * if (debug&DEBUG_TRACE) trace_regs(r,m); |
| 101 | * |
| 102 | * ...... and at the last line of the routine. |
| 103 | * |
| 104 | * EndOfInstruction: end_instr(); |
| 105 | * |
| 106 | * Up to the point where TRACE_REG is expanded, NO modifications |
| 107 | * are done to any register EXCEPT the IP register, for fetch and |
| 108 | * decoding purposes. |
| 109 | * |
| 110 | * This was done for an entirely different reason, but makes a |
| 111 | * nice way to get the system to help debug codes. |
| 112 | */ |
| 113 | tregs = M; |
| 114 | tregs.x86.R_IP = off; |
| 115 | tregs.x86.R_CS = seg; |
| 116 | |
| 117 | /* reset the decoding buffers */ |
| 118 | tregs.x86.enc_str_pos = 0; |
| 119 | tregs.x86.enc_pos = 0; |
| 120 | |
| 121 | /* turn on the "disassemble only, no execute" flag */ |
| 122 | tregs.x86.debug |= DEBUG_DISASSEMBLE_F; |
| 123 | |
| 124 | /* DUMP NEXT n instructions to screen in straight_line fashion */ |
| 125 | /* |
| 126 | * This looks like the regular instruction fetch stream, except |
| 127 | * that when this occurs, each fetched opcode, upon seeing the |
| 128 | * DEBUG_DISASSEMBLE flag set, exits immediately after decoding |
| 129 | * the instruction. XXX --- CHECK THAT MEM IS NOT AFFECTED!!! |
| 130 | * Note the use of a copy of the register structure... |
| 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 | /* end major hack mode. */ |
| 137 | } |
| 138 | |
| 139 | void x86emu_check_ip_access(void) |
| 140 | { |
| 141 | /* NULL as of now */ |
| 142 | } |
| 143 | |
| 144 | void x86emu_check_sp_access(void) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | void x86emu_check_mem_access(u32 dummy) |
| 149 | { |
| 150 | /* check bounds, etc */ |
| 151 | } |
| 152 | |
| 153 | void x86emu_check_data_access(uint dummy1, uint dummy2) |
| 154 | { |
| 155 | /* check bounds, etc */ |
| 156 | } |
| 157 | |
| 158 | void x86emu_inc_decoded_inst_len(int x) |
| 159 | { |
| 160 | M.x86.enc_pos += x; |
| 161 | } |
| 162 | |
| 163 | void 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 | |
| 169 | void 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 | |
| 177 | void x86emu_end_instr(void) |
| 178 | { |
| 179 | M.x86.enc_str_pos = 0; |
| 180 | M.x86.enc_pos = 0; |
| 181 | } |
| 182 | |
| 183 | static 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 | |
| 193 | static void print_decoded_instruction(void) |
| 194 | { |
| 195 | printk("%s", M.x86.decoded_buf); |
| 196 | } |
| 197 | |
| 198 | void 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 | |
| 209 | void 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 | u32 current; |
| 215 | |
| 216 | current = start; |
| 217 | while (end <= off + amt) { |
| 218 | printk("%04x:%04x ", seg, start); |
| 219 | for (i = start; i < off; i++) |
| 220 | printk(" "); |
| 221 | for (; i < end; i++) |
| 222 | printk("%02x ", fetch_data_byte_abs(seg, i)); |
| 223 | printk("\n"); |
| 224 | start = end; |
| 225 | end = start + 16; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void x86emu_single_step(void) |
| 230 | { |
| 231 | char s[1024]; |
| 232 | int ps[10]; |
| 233 | int ntok; |
| 234 | int cmd; |
| 235 | int done; |
| 236 | int segment; |
| 237 | int offset; |
| 238 | static int breakpoint; |
| 239 | static int noDecode = 1; |
| 240 | |
| 241 | char *p; |
| 242 | |
| 243 | if (DEBUG_BREAK()) { |
| 244 | if (M.x86.saved_ip != breakpoint) { |
| 245 | return; |
| 246 | } else { |
| 247 | M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F; |
| 248 | M.x86.debug |= DEBUG_TRACE_F; |
| 249 | M.x86.debug &= ~DEBUG_BREAK_F; |
| 250 | print_decoded_instruction(); |
| 251 | X86EMU_trace_regs(); |
| 252 | } |
| 253 | } |
| 254 | done = 0; |
| 255 | offset = M.x86.saved_ip; |
| 256 | while (!done) { |
| 257 | printk("-"); |
Jason Jin | 7be6e5b | 2008-10-15 10:40:24 +0800 | [diff] [blame] | 258 | cmd = x86emu_parse_line(s, ps, &ntok); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 259 | switch (cmd) { |
| 260 | case 'u': |
| 261 | disassemble_forward(M.x86.saved_cs, (u16) offset, 10); |
| 262 | break; |
| 263 | case 'd': |
| 264 | if (ntok == 2) { |
| 265 | segment = M.x86.saved_cs; |
| 266 | offset = ps[1]; |
| 267 | X86EMU_dump_memory(segment, (u16) offset, 16); |
| 268 | offset += 16; |
| 269 | } else if (ntok == 3) { |
| 270 | segment = ps[1]; |
| 271 | offset = ps[2]; |
| 272 | X86EMU_dump_memory(segment, (u16) offset, 16); |
| 273 | offset += 16; |
| 274 | } else { |
| 275 | segment = M.x86.saved_cs; |
| 276 | X86EMU_dump_memory(segment, (u16) offset, 16); |
| 277 | offset += 16; |
| 278 | } |
| 279 | break; |
| 280 | case 'c': |
| 281 | M.x86.debug ^= DEBUG_TRACECALL_F; |
| 282 | break; |
| 283 | case 's': |
| 284 | M.x86.debug ^= |
| 285 | 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 | if (noDecode) { |
| 297 | M.x86.debug |= DEBUG_DECODE_NOPRINT_F; |
| 298 | } else { |
| 299 | M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F; |
| 300 | } |
| 301 | M.x86.debug &= ~DEBUG_TRACE_F; |
| 302 | M.x86.debug |= DEBUG_BREAK_F; |
| 303 | done = 1; |
| 304 | } |
| 305 | break; |
| 306 | case 'q': |
| 307 | M.x86.debug |= DEBUG_EXIT; |
| 308 | return; |
| 309 | case 'P': |
| 310 | noDecode = (noDecode) ? 0 : 1; |
| 311 | printk("Toggled decoding to %s\n", |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 312 | (noDecode) ? "false" : "true"); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 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 | |
Jason Jin | 7be6e5b | 2008-10-15 10:40:24 +0800 | [diff] [blame] | 332 | static int x86emu_parse_line(char *s, int *ps, int *n) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 333 | { |
| 334 | int cmd; |
| 335 | |
| 336 | *n = 0; |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 337 | while (isblank(*s)) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 338 | s++; |
| 339 | ps[*n] = *s; |
| 340 | switch (*s) { |
| 341 | case '\n': |
| 342 | *n += 1; |
| 343 | return 0; |
| 344 | default: |
| 345 | cmd = *s; |
| 346 | *n += 1; |
| 347 | } |
| 348 | |
| 349 | while (1) { |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 350 | while (!isblank(*s) && *s != '\n') |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 351 | s++; |
| 352 | |
| 353 | if (*s == '\n') |
| 354 | return cmd; |
| 355 | |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 356 | while (isblank(*s)) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 357 | s++; |
| 358 | |
| 359 | *n += 1; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | #endif /* DEBUG */ |
| 364 | |
| 365 | void x86emu_dump_regs(void) |
| 366 | { |
| 367 | printk("\tAX=%04x ", M.x86.R_AX); |
| 368 | printk("BX=%04x ", M.x86.R_BX); |
| 369 | printk("CX=%04x ", M.x86.R_CX); |
| 370 | printk("DX=%04x ", M.x86.R_DX); |
| 371 | printk("SP=%04x ", M.x86.R_SP); |
| 372 | printk("BP=%04x ", M.x86.R_BP); |
| 373 | printk("SI=%04x ", M.x86.R_SI); |
| 374 | printk("DI=%04x\n", M.x86.R_DI); |
| 375 | printk("\tDS=%04x ", M.x86.R_DS); |
| 376 | printk("ES=%04x ", M.x86.R_ES); |
| 377 | printk("SS=%04x ", M.x86.R_SS); |
| 378 | printk("CS=%04x ", M.x86.R_CS); |
| 379 | printk("IP=%04x ", M.x86.R_IP); |
| 380 | if (ACCESS_FLAG(F_OF)) |
| 381 | printk("OV "); /* CHECKED... */ |
| 382 | else |
| 383 | printk("NV "); |
| 384 | if (ACCESS_FLAG(F_DF)) |
| 385 | printk("DN "); |
| 386 | else |
| 387 | printk("UP "); |
| 388 | if (ACCESS_FLAG(F_IF)) |
| 389 | printk("EI "); |
| 390 | else |
| 391 | printk("DI "); |
| 392 | if (ACCESS_FLAG(F_SF)) |
| 393 | printk("NG "); |
| 394 | else |
| 395 | printk("PL "); |
| 396 | if (ACCESS_FLAG(F_ZF)) |
| 397 | printk("ZR "); |
| 398 | else |
| 399 | printk("NZ "); |
| 400 | if (ACCESS_FLAG(F_AF)) |
| 401 | printk("AC "); |
| 402 | else |
| 403 | printk("NA "); |
| 404 | if (ACCESS_FLAG(F_PF)) |
| 405 | printk("PE "); |
| 406 | else |
| 407 | printk("PO "); |
| 408 | if (ACCESS_FLAG(F_CF)) |
| 409 | printk("CY "); |
| 410 | else |
| 411 | printk("NC "); |
| 412 | printk("\n"); |
| 413 | } |
| 414 | |
| 415 | void x86emu_dump_xregs(void) |
| 416 | { |
| 417 | printk("\tEAX=%08x ", M.x86.R_EAX); |
| 418 | printk("EBX=%08x ", M.x86.R_EBX); |
| 419 | printk("ECX=%08x ", M.x86.R_ECX); |
| 420 | printk("EDX=%08x \n", M.x86.R_EDX); |
| 421 | printk("\tESP=%08x ", M.x86.R_ESP); |
| 422 | printk("EBP=%08x ", M.x86.R_EBP); |
| 423 | printk("ESI=%08x ", M.x86.R_ESI); |
| 424 | printk("EDI=%08x\n", M.x86.R_EDI); |
| 425 | printk("\tDS=%04x ", M.x86.R_DS); |
| 426 | printk("ES=%04x ", M.x86.R_ES); |
| 427 | printk("SS=%04x ", M.x86.R_SS); |
| 428 | printk("CS=%04x ", M.x86.R_CS); |
| 429 | printk("EIP=%08x\n\t", M.x86.R_EIP); |
| 430 | if (ACCESS_FLAG(F_OF)) |
| 431 | printk("OV "); /* CHECKED... */ |
| 432 | else |
| 433 | printk("NV "); |
| 434 | if (ACCESS_FLAG(F_DF)) |
| 435 | printk("DN "); |
| 436 | else |
| 437 | printk("UP "); |
| 438 | if (ACCESS_FLAG(F_IF)) |
| 439 | printk("EI "); |
| 440 | else |
| 441 | printk("DI "); |
| 442 | if (ACCESS_FLAG(F_SF)) |
| 443 | printk("NG "); |
| 444 | else |
| 445 | printk("PL "); |
| 446 | if (ACCESS_FLAG(F_ZF)) |
| 447 | printk("ZR "); |
| 448 | else |
| 449 | printk("NZ "); |
| 450 | if (ACCESS_FLAG(F_AF)) |
| 451 | printk("AC "); |
| 452 | else |
| 453 | printk("NA "); |
| 454 | if (ACCESS_FLAG(F_PF)) |
| 455 | printk("PE "); |
| 456 | else |
| 457 | printk("PO "); |
| 458 | if (ACCESS_FLAG(F_CF)) |
| 459 | printk("CY "); |
| 460 | else |
| 461 | printk("NC "); |
| 462 | printk("\n"); |
| 463 | } |