| 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 "x86emui.h" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#ifdef DEBUG |
| 45 |
|
| 46 |
static void print_encoded_bytes (u16 s, u16 o); |
| 47 |
static void print_decoded_instruction (void); |
| 48 |
int parse_line (char *s, int *ps, int *n); |
| 49 |
|
| 50 |
|
| 51 |
void X86EMU_trace_regs (void) |
| 52 |
{ |
| 53 |
if (DEBUG_TRACE()) { |
| 54 |
if (M.x86.mode & (SYSMODE_PREFIX_DATA | SYSMODE_PREFIX_ADDR)) { |
| 55 |
x86emu_dump_xregs(); |
| 56 |
} else { |
| 57 |
x86emu_dump_regs(); |
| 58 |
} |
| 59 |
} |
| 60 |
if (DEBUG_DECODE() && ! DEBUG_DECODE_NOPRINT()) { |
| 61 |
printf("%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 |
|
| 78 |
|
| 79 |
|
| 80 |
printf("%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 |
void disassemble_forward (u16 seg, u16 off, int n) |
| 86 |
{ |
| 87 |
X86EMU_sysEnv tregs; |
| 88 |
int i; |
| 89 |
u8 op1; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
tregs = M; |
| 115 |
tregs.x86.R_IP = off; |
| 116 |
tregs.x86.R_CS = seg; |
| 117 |
|
| 118 |
|
| 119 |
tregs.x86.enc_str_pos = 0; |
| 120 |
tregs.x86.enc_pos = 0; |
| 121 |
|
| 122 |
|
| 123 |
tregs.x86.debug |= DEBUG_DISASSEMBLE_F; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 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 |
|
| 138 |
} |
| 139 |
|
| 140 |
void x86emu_check_ip_access (void) |
| 141 |
{ |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
void x86emu_check_sp_access (void) |
| 146 |
{ |
| 147 |
} |
| 148 |
|
| 149 |
void x86emu_check_mem_access (u32 dummy) |
| 150 |
{ |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
void x86emu_check_data_access (uint dummy1, uint dummy2) |
| 155 |
{ |
| 156 |
|
| 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 (const char *x) |
| 165 |
{ |
| 166 |
strcpy(M.x86.decoded_buf+M.x86.enc_str_pos,x); |
| 167 |
M.x86.enc_str_pos += strlen(x); |
| 168 |
} |
| 169 |
|
| 170 |
void x86emu_decode_printf2 (const char *x, int y) |
| 171 |
{ |
| 172 |
char temp[100]; |
| 173 |
snprintf(temp, sizeof(temp), x,y); |
| 174 |
strcpy(M.x86.decoded_buf+M.x86.enc_str_pos,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 |
snprintf(buf1+2*i, 64 - 2 * i, "%02x", fetch_data_byte_abs(s,o+i)); |
| 190 |
} |
| 191 |
printf("%-20s ",buf1); |
| 192 |
} |
| 193 |
|
| 194 |
static void print_decoded_instruction (void) |
| 195 |
{ |
| 196 |
printf("%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) return; |
| 204 |
seg = fetch_data_word_abs(0,iv*4); |
| 205 |
off = fetch_data_word_abs(0,iv*4+2); |
| 206 |
printf("%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 |
|
| 215 |
while (end <= off + amt) { |
| 216 |
printf("%04x:%04x ", seg, start); |
| 217 |
for (i=start; i< off; i++) |
| 218 |
printf(" "); |
| 219 |
for ( ; i< end; i++) |
| 220 |
printf("%02x ", fetch_data_byte_abs(seg,i)); |
| 221 |
printf("\n"); |
| 222 |
start = end; |
| 223 |
end = start + 16; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
void x86emu_single_step (void) |
| 228 |
{ |
| 229 |
#if 0 |
| 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 |
if (DEBUG_BREAK()) { |
| 241 |
if (M.x86.saved_ip != breakpoint) { |
| 242 |
return; |
| 243 |
} else { |
| 244 |
M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F; |
| 245 |
M.x86.debug |= DEBUG_TRACE_F; |
| 246 |
M.x86.debug &= ~DEBUG_BREAK_F; |
| 247 |
print_decoded_instruction (); |
| 248 |
X86EMU_trace_regs(); |
| 249 |
} |
| 250 |
} |
| 251 |
done=0; |
| 252 |
offset = M.x86.saved_ip; |
| 253 |
while (!done) { |
| 254 |
printf("-"); |
| 255 |
(void)fgets(s, 1023, stdin); |
| 256 |
cmd = 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 ^= DEBUG_SVC_F | DEBUG_SYS_F | DEBUG_SYSINT_F; |
| 283 |
break; |
| 284 |
case 'r': |
| 285 |
X86EMU_trace_regs(); |
| 286 |
break; |
| 287 |
case 'x': |
| 288 |
X86EMU_trace_xregs(); |
| 289 |
break; |
| 290 |
case 'g': |
| 291 |
if (ntok == 2) { |
| 292 |
breakpoint = ps[1]; |
| 293 |
if (noDecode) { |
| 294 |
M.x86.debug |= DEBUG_DECODE_NOPRINT_F; |
| 295 |
} else { |
| 296 |
M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F; |
| 297 |
} |
| 298 |
M.x86.debug &= ~DEBUG_TRACE_F; |
| 299 |
M.x86.debug |= DEBUG_BREAK_F; |
| 300 |
done = 1; |
| 301 |
} |
| 302 |
break; |
| 303 |
case 'q': |
| 304 |
M.x86.debug |= DEBUG_EXIT; |
| 305 |
return; |
| 306 |
case 'P': |
| 307 |
noDecode = (noDecode)?0:1; |
| 308 |
printf("Toggled decoding to %s\n",(noDecode)?"FALSE":"TRUE"); |
| 309 |
break; |
| 310 |
case 't': |
| 311 |
case 0: |
| 312 |
done = 1; |
| 313 |
break; |
| 314 |
} |
| 315 |
} |
| 316 |
#endif |
| 317 |
} |
| 318 |
|
| 319 |
int X86EMU_trace_on(void) |
| 320 |
{ |
| 321 |
return M.x86.debug |= DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F; |
| 322 |
} |
| 323 |
|
| 324 |
int X86EMU_trace_off(void) |
| 325 |
{ |
| 326 |
return M.x86.debug &= ~(DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F); |
| 327 |
} |
| 328 |
|
| 329 |
int parse_line (char *s, int *ps, int *n) |
| 330 |
{ |
| 331 |
#if 0 |
| 332 |
int cmd; |
| 333 |
|
| 334 |
*n = 0; |
| 335 |
while(*s == ' ' || *s == '\t') s++; |
| 336 |
ps[*n] = *s; |
| 337 |
switch (*s) { |
| 338 |
case '\n': |
| 339 |
*n += 1; |
| 340 |
return 0; |
| 341 |
default: |
| 342 |
cmd = *s; |
| 343 |
*n += 1; |
| 344 |
} |
| 345 |
|
| 346 |
while (1) { |
| 347 |
while (*s != ' ' && *s != '\t' && *s != '\n') s++; |
| 348 |
|
| 349 |
if (*s == '\n') |
| 350 |
return cmd; |
| 351 |
|
| 352 |
while(*s == ' ' || *s == '\t') s++; |
| 353 |
|
| 354 |
sscanf(s,"%x",&ps[*n]); |
| 355 |
*n += 1; |
| 356 |
} |
| 357 |
#else |
| 358 |
return 0; |
| 359 |
#endif |
| 360 |
} |
| 361 |
|
| 362 |
#endif |
| 363 |
|
| 364 |
void x86emu_dump_regs (void) |
| 365 |
{ |
| 366 |
printf("\tAX=%04x ", M.x86.R_AX ); |
| 367 |
printf("BX=%04x ", M.x86.R_BX ); |
| 368 |
printf("CX=%04x ", M.x86.R_CX ); |
| 369 |
printf("DX=%04x ", M.x86.R_DX ); |
| 370 |
printf("SP=%04x ", M.x86.R_SP ); |
| 371 |
printf("BP=%04x ", M.x86.R_BP ); |
| 372 |
printf("SI=%04x ", M.x86.R_SI ); |
| 373 |
printf("DI=%04x\n", M.x86.R_DI ); |
| 374 |
printf("\tDS=%04x ", M.x86.R_DS ); |
| 375 |
printf("ES=%04x ", M.x86.R_ES ); |
| 376 |
printf("SS=%04x ", M.x86.R_SS ); |
| 377 |
printf("CS=%04x ", M.x86.R_CS ); |
| 378 |
printf("IP=%04x ", M.x86.R_IP ); |
| 379 |
if (ACCESS_FLAG(F_OF)) printf("OV "); |
| 380 |
else printf("NV "); |
| 381 |
if (ACCESS_FLAG(F_DF)) printf("DN "); |
| 382 |
else printf("UP "); |
| 383 |
if (ACCESS_FLAG(F_IF)) printf("EI "); |
| 384 |
else printf("DI "); |
| 385 |
if (ACCESS_FLAG(F_SF)) printf("NG "); |
| 386 |
else printf("PL "); |
| 387 |
if (ACCESS_FLAG(F_ZF)) printf("ZR "); |
| 388 |
else printf("NZ "); |
| 389 |
if (ACCESS_FLAG(F_AF)) printf("AC "); |
| 390 |
else printf("NA "); |
| 391 |
if (ACCESS_FLAG(F_PF)) printf("PE "); |
| 392 |
else printf("PO "); |
| 393 |
if (ACCESS_FLAG(F_CF)) printf("CY "); |
| 394 |
else printf("NC "); |
| 395 |
printf("\n"); |
| 396 |
} |
| 397 |
|
| 398 |
void x86emu_dump_xregs (void) |
| 399 |
{ |
| 400 |
printf("\tEAX=%08x ", M.x86.R_EAX ); |
| 401 |
printf("EBX=%08x ", M.x86.R_EBX ); |
| 402 |
printf("ECX=%08x ", M.x86.R_ECX ); |
| 403 |
printf("EDX=%08x\n", M.x86.R_EDX ); |
| 404 |
printf("\tESP=%08x ", M.x86.R_ESP ); |
| 405 |
printf("EBP=%08x ", M.x86.R_EBP ); |
| 406 |
printf("ESI=%08x ", M.x86.R_ESI ); |
| 407 |
printf("EDI=%08x\n", M.x86.R_EDI ); |
| 408 |
printf("\tDS=%04x ", M.x86.R_DS ); |
| 409 |
printf("ES=%04x ", M.x86.R_ES ); |
| 410 |
printf("SS=%04x ", M.x86.R_SS ); |
| 411 |
printf("CS=%04x ", M.x86.R_CS ); |
| 412 |
printf("EIP=%08x\n\t", M.x86.R_EIP ); |
| 413 |
if (ACCESS_FLAG(F_OF)) printf("OV "); |
| 414 |
else printf("NV "); |
| 415 |
if (ACCESS_FLAG(F_DF)) printf("DN "); |
| 416 |
else printf("UP "); |
| 417 |
if (ACCESS_FLAG(F_IF)) printf("EI "); |
| 418 |
else printf("DI "); |
| 419 |
if (ACCESS_FLAG(F_SF)) printf("NG "); |
| 420 |
else printf("PL "); |
| 421 |
if (ACCESS_FLAG(F_ZF)) printf("ZR "); |
| 422 |
else printf("NZ "); |
| 423 |
if (ACCESS_FLAG(F_AF)) printf("AC "); |
| 424 |
else printf("NA "); |
| 425 |
if (ACCESS_FLAG(F_PF)) printf("PE "); |
| 426 |
else printf("PO "); |
| 427 |
if (ACCESS_FLAG(F_CF)) printf("CY "); |
| 428 |
else printf("NC "); |
| 429 |
printf("\n"); |
| 430 |
} |