Blob


1 /*
2 * 386 definition
3 */
4 #include <u.h>
5 #include <libc.h>
6 #include <bio.h>
7 #include <mach.h>
8 #include "ureg386.h"
10 #define REGOFF(x) (ulong)(&((struct Ureg *) 0)->x)
12 #define REGSIZE sizeof(struct Ureg)
13 #define FP_CTL(x) (REGSIZE+4*(x))
14 #define FP_REG(x) (FP_CTL(7)+10*(x))
15 #define FPREGSIZE (6*4+8*10)
17 /*
18 * i386-specific debugger interface
19 */
21 static char *i386excep(Map*, Regs*);
23 /*
24 static int i386trace(Map*, ulong, ulong, ulong, Tracer);
25 static ulong i386frame(Map*, ulong, ulong, ulong, ulong);
26 */
27 static int i386foll(Map*, Regs*, ulong, ulong*);
28 static int i386hexinst(Map*, ulong, char*, int);
29 static int i386das(Map*, ulong, char, char*, int);
30 static int i386instlen(Map*, ulong);
31 static char *i386windregs[];
32 static int i386unwind(Map*, Regs*, ulong*, Symbol*);
34 static Regdesc i386reglist[] = {
35 {"DI", REGOFF(di), RINT, 'X'},
36 {"SI", REGOFF(si), RINT, 'X'},
37 {"BP", REGOFF(bp), RINT, 'X'},
38 {"BX", REGOFF(bx), RINT, 'X'},
39 {"DX", REGOFF(dx), RINT, 'X'},
40 {"CX", REGOFF(cx), RINT, 'X'},
41 {"AX", REGOFF(ax), RINT, 'X'},
42 {"GS", REGOFF(gs), RINT, 'X'},
43 {"FS", REGOFF(fs), RINT, 'X'},
44 {"ES", REGOFF(es), RINT, 'X'},
45 {"DS", REGOFF(ds), RINT, 'X'},
46 {"TRAP", REGOFF(trap), RINT, 'X'},
47 {"ECODE", REGOFF(ecode), RINT, 'X'},
48 {"PC", REGOFF(pc), RINT, 'X'},
49 {"CS", REGOFF(cs), RINT, 'X'},
50 {"EFLAGS", REGOFF(flags), RINT, 'X'},
51 {"SP", REGOFF(sp), RINT, 'X'},
52 {"SS", REGOFF(ss), RINT, 'X'},
54 {"E0", FP_CTL(0), RFLT, 'X'},
55 {"E1", FP_CTL(1), RFLT, 'X'},
56 {"E2", FP_CTL(2), RFLT, 'X'},
57 {"E3", FP_CTL(3), RFLT, 'X'},
58 {"E4", FP_CTL(4), RFLT, 'X'},
59 {"E5", FP_CTL(5), RFLT, 'X'},
60 {"E6", FP_CTL(6), RFLT, 'X'},
61 {"F0", FP_REG(7), RFLT, '3'},
62 {"F1", FP_REG(6), RFLT, '3'},
63 {"F2", FP_REG(5), RFLT, '3'},
64 {"F3", FP_REG(4), RFLT, '3'},
65 {"F4", FP_REG(3), RFLT, '3'},
66 {"F5", FP_REG(2), RFLT, '3'},
67 {"F6", FP_REG(1), RFLT, '3'},
68 {"F7", FP_REG(0), RFLT, '3'},
69 { 0 }
70 };
72 Mach mach386 =
73 {
74 "386",
75 M386, /* machine type */
76 i386reglist, /* register list */
77 REGSIZE, /* size of registers in bytes */
78 FPREGSIZE, /* size of fp registers in bytes */
79 "PC", /* name of PC */
80 "SP", /* name of SP */
81 "BP", /* name of FP */
82 0, /* link register */
83 "setSB", /* static base register name (bogus anyways) */
84 0, /* static base register value */
85 0x1000, /* page size */
86 0x80100000, /* kernel base */
87 0, /* kernel text mask */
88 1, /* quantization of pc */
89 4, /* szaddr */
90 4, /* szreg */
91 4, /* szfloat */
92 8, /* szdouble */
94 i386windregs, /* locations unwound in stack trace */
95 9,
97 {0xCC, 0, 0, 0}, /* break point: INT 3 */
98 1, /* break point size */
100 i386foll, /* following addresses */
101 i386excep, /* print exception */
102 i386unwind, /* stack unwind */
104 leswap2, /* convert short to local byte order */
105 leswap4, /* convert long to local byte order */
106 leswap8, /* convert vlong to local byte order */
107 leieeeftoa32, /* single precision float pointer */
108 leieeeftoa64, /* double precision float pointer */
109 leieeeftoa80, /* long double precision floating point */
111 i386das, /* dissembler */
112 i386das, /* plan9-format disassembler */
113 0, /* commercial disassembler */
114 i386hexinst, /* print instruction */
115 i386instlen, /* instruction size calculation */
116 };
118 static char *i386windregs[] = {
119 "PC",
120 "SP",
121 "BP",
122 "AX",
123 "CX",
124 "DX",
125 "BX",
126 "SI",
127 "DI",
128 0,
129 };
131 /*
132 * The wrapper code around Linux system calls
133 * saves AX on the stack before calling some calls
134 * (at least, __libc_nanosleep), when running in
135 * threaded programs.
136 */
137 static void
138 syscallhack(Map *map, Regs *regs, int *spoff)
140 ulong pc;
141 char buf[60];
143 rget(regs, "PC", &pc);
144 if(i386das(map, pc-2, 0, buf, sizeof buf) != 2 || strncmp(buf, "INTB\t$", 6) != 0)
145 return;
146 if(i386das(map, pc, 0, buf, sizeof buf) != 2 || strcmp(buf, "MOVL\tDX,BX") != 0)
147 return;
148 if(i386das(map, pc+2, 0, buf, sizeof buf) != 3 || strcmp(buf, "XCHGL\tAX,0(SP)") != 0)
149 return;
150 *spoff += 4;
153 static int
154 i386unwind(Map *map, Regs *regs, ulong *next, Symbol *sym)
156 int i, isp, ipc, ibp, havebp, n, spoff, off[9];
157 ulong pc;
158 u32int v;
159 char buf[60], *p;
161 //print("i386unwind %s\n", sym ? sym->name : nil);
162 isp = windindex("SP");
163 ipc = windindex("PC");
164 ibp = windindex("BP");
165 if(isp < 0 || ipc < 0 || ibp < 0){
166 werrstr("i386unwind: cannot happen");
167 return -1;
170 /*
171 * Disassemble entry to figure out
172 * where values have been saved.
173 * Perhaps should disassemble exit path
174 * instead -- a random walk on the code
175 * should suffice to get us to a RET.
176 */
177 if(sym){
178 pc = sym->loc.addr;
179 //print("startpc %lux\n", pc);
180 memset(off, 0xff, sizeof off);
181 spoff = 0;
182 havebp = 0;
183 for(;;){
184 if((n = i386das(map, pc, 0, buf, sizeof buf)) < 0)
185 break;
186 //print("%s\n", buf);
187 pc += n;
188 if(strncmp(buf, "PUSHL\t", 6) == 0){
189 spoff += 4;
190 if((i = windindex(buf+6)) >= 0)
191 off[i] = spoff;
192 }else if(strcmp(buf, "MOVL\tSP,BP") == 0 && spoff == 4 && off[ibp] == 4){
193 havebp = 1;
194 }else if(strncmp(buf, "SUBL\t$", 6) == 0){
195 if((p = strrchr(buf, ',')) && strcmp(p, ",SP") == 0){
196 //print("spoff %s\n", buf+6);
197 spoff += strtol(buf+6, 0, 16);
199 break;
200 }else if(strncmp(buf, "XORL\t", 5) == 0 || strncmp(buf, "MOVL\t", 5) == 0){
201 /*
202 * Hope these are rescheduled non-prologue instructions
203 * like XORL AX, AX or MOVL $0x3, AX and thus ignorable.
204 */
205 }else
206 break;
209 syscallhack(map, regs, &spoff);
211 if(havebp){
212 //print("havebp\n");
213 rget(regs, "BP", &next[isp]);
214 get4(map, next[isp], &v);
215 next[ibp] = v;
216 next[isp] += 4;
217 }else{
218 rget(regs, "SP", &next[isp]);
219 //print("old sp %lux + %d\n", next[isp], spoff);
220 next[isp] += spoff;
222 for(i=0; i<nelem(off); i++)
223 if(off[i] != -1){
224 get4(map, next[isp]-off[i], &v);
225 next[i] = v;
228 if(get4(map, next[isp], &v) < 0)
229 return -1;
230 //print("new pc %lux => %lux\n", next[isp], v);
231 next[ipc] = v;
232 next[isp] += 4;
233 return 0;
236 /*
237 * Rely on bp chaining
238 */
239 if(rget(regs, "BP", &next[isp]) < 0
240 || get4(map, next[isp], &v) < 0)
241 return -1;
242 next[ibp] = v;
243 next[isp] += 4;
244 if(get4(map, next[isp], &v) < 0)
245 return -1;
246 next[ipc] = v;
247 next[isp] += 4;
248 return 0;
251 //static char STARTSYM[] = "_main";
252 //static char PROFSYM[] = "_mainp";
253 static char FRAMENAME[] = ".frame";
254 static char *excname[] =
256 "divide error", /* 0 */
257 "debug exception", /* 1 */
258 0,0, /* 2, 3 */
259 "overflow", /* 4 */
260 "bounds check", /* 5 */
261 "invalid opcode", /* 6 */
262 "math coprocessor emulation", /* 7 */
263 "double fault", /* 8 */
264 "math coprocessor overrun", /* 9 */
265 "invalid TSS", /* 10 */
266 "segment not present", /* 11 */
267 "stack exception", /* 12 */
268 "general protection violation", /* 13 */
269 "page fault", /* 14 */
270 0, /* 15 */
271 "math coprocessor error", /* 16 */
272 0,0,0,0,0,0,0, /* 17-23 */
273 "clock", /* 24 */
274 "keyboard", /* 25 */
275 0, /* 26 */
276 "modem status", /* 27 */
277 "serial line status", /* 28 */
278 0, /* 29 */
279 "floppy disk", /* 30 */
280 0,0,0,0,0, /* 31-35 */
281 "mouse", /* 36 */
282 "math coprocessor", /* 37 */
283 "hard disk", /* 38 */
284 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,/* 39-54 */
285 0,0,0,0,0,0,0,0,0, /* 55-63 */
286 "system call", /* 64 */
287 };
289 static char*
290 i386excep(Map *map, Regs *regs)
292 ulong c;
293 ulong pc;
294 static char buf[16];
296 if(rget(regs, "TRAP", &c) < 0)
297 return "no trap register";
299 if(c > 64 || excname[c] == 0) {
300 if (c == 3) {
301 if (rget(regs, "PC", &pc) >= 0)
302 if (get1(map, pc, (uchar*)buf, mach->bpsize) > 0)
303 if (memcmp(buf, mach->bpinst, mach->bpsize) == 0)
304 return "breakpoint";
306 sprint(buf, "exception %ld", c);
307 return buf;
308 } else
309 return excname[c];
312 /* I386/486 - Disassembler and related functions */
314 /*
315 * an instruction
316 */
317 typedef struct Instr Instr;
318 struct Instr
320 uchar mem[1+1+1+1+2+1+1+4+4]; /* raw instruction */
321 ulong addr; /* address of start of instruction */
322 int n; /* number of bytes in instruction */
323 char *prefix; /* instr prefix */
324 char *segment; /* segment override */
325 uchar jumptype; /* set to the operand type for jump/ret/call */
326 char osize; /* 'W' or 'L' */
327 char asize; /* address size 'W' or 'L' */
328 uchar mod; /* bits 6-7 of mod r/m field */
329 uchar reg; /* bits 3-5 of mod r/m field */
330 char ss; /* bits 6-7 of SIB */
331 char index; /* bits 3-5 of SIB */
332 char base; /* bits 0-2 of SIB */
333 short seg; /* segment of far address */
334 ulong disp; /* displacement */
335 ulong imm; /* immediate */
336 ulong imm2; /* second immediate operand */
337 char *curr; /* fill level in output buffer */
338 char *end; /* end of output buffer */
339 char *err; /* error message */
340 };
342 /* 386 register (ha!) set */
343 enum{
344 AX=0,
345 CX,
346 DX,
347 BX,
348 SP,
349 BP,
350 SI,
351 DI,
352 };
353 /* Operand Format codes */
354 /*
355 %A - address size register modifier (!asize -> 'E')
356 %C - Control register CR0/CR1/CR2
357 %D - Debug register DR0/DR1/DR2/DR3/DR6/DR7
358 %I - second immediate operand
359 %O - Operand size register modifier (!osize -> 'E')
360 %T - Test register TR6/TR7
361 %S - size code ('W' or 'L')
362 %X - Weird opcode: OSIZE == 'W' => "CBW"; else => "CWDE"
363 %d - displacement 16-32 bits
364 %e - effective address - Mod R/M value
365 %f - floating point register F0-F7 - from Mod R/M register
366 %g - segment register
367 %i - immediate operand 8-32 bits
368 %p - PC-relative - signed displacement in immediate field
369 %r - Reg from Mod R/M
370 %x - Weird opcode: OSIZE == 'W' => "CWD"; else => "CDQ"
371 */
373 typedef struct Optable Optable;
374 struct Optable
376 char operand[2];
377 void *proto; /* actually either (char*) or (Optable*) */
378 };
379 /* Operand decoding codes */
380 enum {
381 Ib = 1, /* 8-bit immediate - (no sign extension)*/
382 Ibs, /* 8-bit immediate (sign extended) */
383 Jbs, /* 8-bit sign-extended immediate in jump or call */
384 Iw, /* 16-bit immediate -> imm */
385 Iw2, /* 16-bit immediate -> imm2 */
386 Iwd, /* Operand-sized immediate (no sign extension)*/
387 Awd, /* Address offset */
388 Iwds, /* Operand-sized immediate (sign extended) */
389 RM, /* Word or long R/M field with register (/r) */
390 RMB, /* Byte R/M field with register (/r) */
391 RMOP, /* Word or long R/M field with op code (/digit) */
392 RMOPB, /* Byte R/M field with op code (/digit) */
393 RMR, /* R/M register only (mod = 11) */
394 RMM, /* R/M memory only (mod = 0/1/2) */
395 R0, /* Base reg of Mod R/M is literal 0x00 */
396 R1, /* Base reg of Mod R/M is literal 0x01 */
397 FRMOP, /* Floating point R/M field with opcode */
398 FRMEX, /* Extended floating point R/M field with opcode */
399 JUMP, /* Jump or Call flag - no operand */
400 RET, /* Return flag - no operand */
401 OA, /* literal 0x0a byte */
402 PTR, /* Seg:Displacement addr (ptr16:16 or ptr16:32) */
403 AUX, /* Multi-byte op code - Auxiliary table */
404 PRE, /* Instr Prefix */
405 SEG, /* Segment Prefix */
406 OPOVER, /* Operand size override */
407 ADDOVER, /* Address size override */
408 };
410 static Optable optab0F00[8]=
412 0,0, "MOVW LDT,%e", /* 0x00 */
413 0,0, "MOVW TR,%e", /* 0x01 */
414 0,0, "MOVW %e,LDT", /* 0x02 */
415 0,0, "MOVW %e,TR", /* 0x03 */
416 0,0, "VERR %e", /* 0x04 */
417 0,0, "VERW %e", /* 0x05 */
418 };
420 static Optable optab0F01[8]=
422 0,0, "MOVL GDTR,%e", /* 0x00 */
423 0,0, "MOVL IDTR,%e", /* 0x01 */
424 0,0, "MOVL %e,GDTR", /* 0x02 */
425 0,0, "MOVL %e,IDTR", /* 0x03 */
426 0,0, "MOVW MSW,%e", /* 0x04 */ /* word */
427 0,0, "", /* 0x05 */
428 0,0, "MOVW %e,MSW", /* 0x06 */ /* word */
429 };
431 static Optable optab0FBA[8]=
433 0,0, "", /* 0x00 */
434 0,0, "", /* 0x01 */
435 0,0, "", /* 0x02 */
436 0,0, "", /* 0x03 */
437 Ib,0, "BT%S %i,%e", /* 0x04 */
438 Ib,0, "BTS%S %i,%e", /* 0x05 */
439 Ib,0, "BTR%S %i,%e", /* 0x06 */
440 Ib,0, "BTC%S %i,%e", /* 0x07 */
441 };
443 static Optable optab0F[256]=
445 RMOP,0, optab0F00, /* 0x00 */
446 RMOP,0, optab0F01, /* 0x01 */
447 RM,0, "LAR %e,%r", /* 0x02 */
448 RM,0, "LSL %e,%r", /* 0x03 */
449 0,0, "", /* 0x04 */
450 0,0, "", /* 0x05 */
451 0,0, "CLTS", /* 0x06 */
452 0,0, "", /* 0x07 */
453 0,0, "INVD", /* 0x08 */
454 0,0, "WBINVD", /* 0x09 */
455 0,0, "", /* 0x0a */
456 0,0, "", /* 0x0b */
457 0,0, "", /* 0x0c */
458 0,0, "", /* 0x0d */
459 0,0, "", /* 0x0e */
460 0,0, "", /* 0x0f */
461 0,0, "", /* 0x10 */
462 0,0, "", /* 0x11 */
463 0,0, "", /* 0x12 */
464 0,0, "", /* 0x13 */
465 0,0, "", /* 0x14 */
466 0,0, "", /* 0x15 */
467 0,0, "", /* 0x16 */
468 0,0, "", /* 0x17 */
469 0,0, "", /* 0x18 */
470 0,0, "", /* 0x19 */
471 0,0, "", /* 0x1a */
472 0,0, "", /* 0x1b */
473 0,0, "", /* 0x1c */
474 0,0, "", /* 0x1d */
475 0,0, "", /* 0x1e */
476 0,0, "", /* 0x1f */
477 RMR,0, "MOVL %C,%e", /* 0x20 */
478 RMR,0, "MOVL %D,%e", /* 0x21 */
479 RMR,0, "MOVL %e,%C", /* 0x22 */
480 RMR,0, "MOVL %e,%D", /* 0x23 */
481 RMR,0, "MOVL %T,%e", /* 0x24 */
482 0,0, "", /* 0x25 */
483 RMR,0, "MOVL %e,%T", /* 0x26 */
484 0,0, "", /* 0x27 */
485 0,0, "", /* 0x28 */
486 0,0, "", /* 0x29 */
487 0,0, "", /* 0x2a */
488 0,0, "", /* 0x2b */
489 0,0, "", /* 0x2c */
490 0,0, "", /* 0x2d */
491 0,0, "", /* 0x2e */
492 0,0, "", /* 0x2f */
493 0,0, "WRMSR", /* 0x30 */
494 0,0, "RDTSC", /* 0x31 */
495 0,0, "RDMSR", /* 0x32 */
496 0,0, "", /* 0x33 */
497 0,0, "", /* 0x34 */
498 0,0, "", /* 0x35 */
499 0,0, "", /* 0x36 */
500 0,0, "", /* 0x37 */
501 0,0, "", /* 0x38 */
502 0,0, "", /* 0x39 */
503 0,0, "", /* 0x3a */
504 0,0, "", /* 0x3b */
505 0,0, "", /* 0x3c */
506 0,0, "", /* 0x3d */
507 0,0, "", /* 0x3e */
508 0,0, "", /* 0x3f */
509 0,0, "", /* 0x40 */
510 0,0, "", /* 0x41 */
511 0,0, "", /* 0x42 */
512 0,0, "", /* 0x43 */
513 0,0, "", /* 0x44 */
514 0,0, "", /* 0x45 */
515 0,0, "", /* 0x46 */
516 0,0, "", /* 0x47 */
517 0,0, "", /* 0x48 */
518 0,0, "", /* 0x49 */
519 0,0, "", /* 0x4a */
520 0,0, "", /* 0x4b */
521 0,0, "", /* 0x4c */
522 0,0, "", /* 0x4d */
523 0,0, "", /* 0x4e */
524 0,0, "", /* 0x4f */
525 0,0, "", /* 0x50 */
526 0,0, "", /* 0x51 */
527 0,0, "", /* 0x52 */
528 0,0, "", /* 0x53 */
529 0,0, "", /* 0x54 */
530 0,0, "", /* 0x55 */
531 0,0, "", /* 0x56 */
532 0,0, "", /* 0x57 */
533 0,0, "", /* 0x58 */
534 0,0, "", /* 0x59 */
535 0,0, "", /* 0x5a */
536 0,0, "", /* 0x5b */
537 0,0, "", /* 0x5c */
538 0,0, "", /* 0x5d */
539 0,0, "", /* 0x5e */
540 0,0, "", /* 0x5f */
541 0,0, "", /* 0x60 */
542 0,0, "", /* 0x61 */
543 0,0, "", /* 0x62 */
544 0,0, "", /* 0x63 */
545 0,0, "", /* 0x64 */
546 0,0, "", /* 0x65 */
547 0,0, "", /* 0x66 */
548 0,0, "", /* 0x67 */
549 0,0, "", /* 0x68 */
550 0,0, "", /* 0x69 */
551 0,0, "", /* 0x6a */
552 0,0, "", /* 0x6b */
553 0,0, "", /* 0x6c */
554 0,0, "", /* 0x6d */
555 0,0, "", /* 0x6e */
556 0,0, "", /* 0x6f */
557 0,0, "", /* 0x70 */
558 0,0, "", /* 0x71 */
559 0,0, "", /* 0x72 */
560 0,0, "", /* 0x73 */
561 0,0, "", /* 0x74 */
562 0,0, "", /* 0x75 */
563 0,0, "", /* 0x76 */
564 0,0, "", /* 0x77 */
565 0,0, "", /* 0x78 */
566 0,0, "", /* 0x79 */
567 0,0, "", /* 0x7a */
568 0,0, "", /* 0x7b */
569 0,0, "", /* 0x7c */
570 0,0, "", /* 0x7d */
571 0,0, "", /* 0x7e */
572 0,0, "", /* 0x7f */
573 Iwds,0, "JOS %p", /* 0x80 */
574 Iwds,0, "JOC %p", /* 0x81 */
575 Iwds,0, "JCS %p", /* 0x82 */
576 Iwds,0, "JCC %p", /* 0x83 */
577 Iwds,0, "JEQ %p", /* 0x84 */
578 Iwds,0, "JNE %p", /* 0x85 */
579 Iwds,0, "JLS %p", /* 0x86 */
580 Iwds,0, "JHI %p", /* 0x87 */
581 Iwds,0, "JMI %p", /* 0x88 */
582 Iwds,0, "JPL %p", /* 0x89 */
583 Iwds,0, "JPS %p", /* 0x8a */
584 Iwds,0, "JPC %p", /* 0x8b */
585 Iwds,0, "JLT %p", /* 0x8c */
586 Iwds,0, "JGE %p", /* 0x8d */
587 Iwds,0, "JLE %p", /* 0x8e */
588 Iwds,0, "JGT %p", /* 0x8f */
589 RMB,0, "SETOS %e", /* 0x90 */
590 RMB,0, "SETOC %e", /* 0x91 */
591 RMB,0, "SETCS %e", /* 0x92 */
592 RMB,0, "SETCC %e", /* 0x93 */
593 RMB,0, "SETEQ %e", /* 0x94 */
594 RMB,0, "SETNE %e", /* 0x95 */
595 RMB,0, "SETLS %e", /* 0x96 */
596 RMB,0, "SETHI %e", /* 0x97 */
597 RMB,0, "SETMI %e", /* 0x98 */
598 RMB,0, "SETPL %e", /* 0x99 */
599 RMB,0, "SETPS %e", /* 0x9a */
600 RMB,0, "SETPC %e", /* 0x9b */
601 RMB,0, "SETLT %e", /* 0x9c */
602 RMB,0, "SETGE %e", /* 0x9d */
603 RMB,0, "SETLE %e", /* 0x9e */
604 RMB,0, "SETGT %e", /* 0x9f */
605 0,0, "PUSHL FS", /* 0xa0 */
606 0,0, "POPL FS", /* 0xa1 */
607 0,0, "CPUID", /* 0xa2 */
608 RM,0, "BT%S %r,%e", /* 0xa3 */
609 RM,Ib, "SHLD%S %r,%i,%e", /* 0xa4 */
610 RM,0, "SHLD%S %r,CL,%e", /* 0xa5 */
611 0,0, "", /* 0xa6 */
612 0,0, "", /* 0xa7 */
613 0,0, "PUSHL GS", /* 0xa8 */
614 0,0, "POPL GS", /* 0xa9 */
615 0,0, "", /* 0xaa */
616 RM,0, "BTS%S %r,%e", /* 0xab */
617 RM,Ib, "SHRD%S %r,%i,%e", /* 0xac */
618 RM,0, "SHRD%S %r,CL,%e", /* 0xad */
619 0,0, "", /* 0xae */
620 RM,0, "IMUL%S %e,%r", /* 0xaf */
621 0,0, "", /* 0xb0 */
622 0,0, "", /* 0xb1 */
623 RMM,0, "LSS %e,%r", /* 0xb2 */
624 RM,0, "BTR%S %r,%e", /* 0xb3 */
625 RMM,0, "LFS %e,%r", /* 0xb4 */
626 RMM,0, "LGS %e,%r", /* 0xb5 */
627 RMB,0, "MOVBZX %e,%R", /* 0xb6 */
628 RM,0, "MOVWZX %e,%R", /* 0xb7 */
629 0,0, "", /* 0xb8 */
630 0,0, "", /* 0xb9 */
631 RMOP,0, optab0FBA, /* 0xba */
632 RM,0, "BTC%S %e,%r", /* 0xbb */
633 RM,0, "BSF%S %e,%r", /* 0xbc */
634 RM,0, "BSR%S %e,%r", /* 0xbd */
635 RMB,0, "MOVBSX %e,%R", /* 0xbe */
636 RM,0, "MOVWSX %e,%R", /* 0xbf */
637 };
639 static Optable optab80[8]=
641 Ib,0, "ADDB %i,%e", /* 0x00 */
642 Ib,0, "ORB %i,%e", /* 0x01 */
643 Ib,0, "ADCB %i,%e", /* 0x02 */
644 Ib,0, "SBBB %i,%e", /* 0x03 */
645 Ib,0, "ANDB %i,%e", /* 0x04 */
646 Ib,0, "SUBB %i,%e", /* 0x05 */
647 Ib,0, "XORB %i,%e", /* 0x06 */
648 Ib,0, "CMPB %e,%i", /* 0x07 */
649 };
651 static Optable optab81[8]=
653 Iwd,0, "ADD%S %i,%e", /* 0x00 */
654 Iwd,0, "OR%S %i,%e", /* 0x01 */
655 Iwd,0, "ADC%S %i,%e", /* 0x02 */
656 Iwd,0, "SBB%S %i,%e", /* 0x03 */
657 Iwd,0, "AND%S %i,%e", /* 0x04 */
658 Iwd,0, "SUB%S %i,%e", /* 0x05 */
659 Iwd,0, "XOR%S %i,%e", /* 0x06 */
660 Iwd,0, "CMP%S %e,%i", /* 0x07 */
661 };
663 static Optable optab83[8]=
665 Ibs,0, "ADD%S %i,%e", /* 0x00 */
666 Ibs,0, "OR%S %i,%e", /* 0x01 */
667 Ibs,0, "ADC%S %i,%e", /* 0x02 */
668 Ibs,0, "SBB%S %i,%e", /* 0x03 */
669 Ibs,0, "AND%S %i,%e", /* 0x04 */
670 Ibs,0, "SUB%S %i,%e", /* 0x05 */
671 Ibs,0, "XOR%S %i,%e", /* 0x06 */
672 Ibs,0, "CMP%S %e,%i", /* 0x07 */
673 };
675 static Optable optabC0[8] =
677 Ib,0, "ROLB %i,%e", /* 0x00 */
678 Ib,0, "RORB %i,%e", /* 0x01 */
679 Ib,0, "RCLB %i,%e", /* 0x02 */
680 Ib,0, "RCRB %i,%e", /* 0x03 */
681 Ib,0, "SHLB %i,%e", /* 0x04 */
682 Ib,0, "SHRB %i,%e", /* 0x05 */
683 0,0, "", /* 0x06 */
684 Ib,0, "SARB %i,%e", /* 0x07 */
685 };
687 static Optable optabC1[8] =
689 Ib,0, "ROL%S %i,%e", /* 0x00 */
690 Ib,0, "ROR%S %i,%e", /* 0x01 */
691 Ib,0, "RCL%S %i,%e", /* 0x02 */
692 Ib,0, "RCR%S %i,%e", /* 0x03 */
693 Ib,0, "SHL%S %i,%e", /* 0x04 */
694 Ib,0, "SHR%S %i,%e", /* 0x05 */
695 0,0, "", /* 0x06 */
696 Ib,0, "SAR%S %i,%e", /* 0x07 */
697 };
699 static Optable optabD0[8] =
701 0,0, "ROLB %e", /* 0x00 */
702 0,0, "RORB %e", /* 0x01 */
703 0,0, "RCLB %e", /* 0x02 */
704 0,0, "RCRB %e", /* 0x03 */
705 0,0, "SHLB %e", /* 0x04 */
706 0,0, "SHRB %e", /* 0x05 */
707 0,0, "", /* 0x06 */
708 0,0, "SARB %e", /* 0x07 */
709 };
711 static Optable optabD1[8] =
713 0,0, "ROL%S %e", /* 0x00 */
714 0,0, "ROR%S %e", /* 0x01 */
715 0,0, "RCL%S %e", /* 0x02 */
716 0,0, "RCR%S %e", /* 0x03 */
717 0,0, "SHL%S %e", /* 0x04 */
718 0,0, "SHR%S %e", /* 0x05 */
719 0,0, "", /* 0x06 */
720 0,0, "SAR%S %e", /* 0x07 */
721 };
723 static Optable optabD2[8] =
725 0,0, "ROLB CL,%e", /* 0x00 */
726 0,0, "RORB CL,%e", /* 0x01 */
727 0,0, "RCLB CL,%e", /* 0x02 */
728 0,0, "RCRB CL,%e", /* 0x03 */
729 0,0, "SHLB CL,%e", /* 0x04 */
730 0,0, "SHRB CL,%e", /* 0x05 */
731 0,0, "", /* 0x06 */
732 0,0, "SARB CL,%e", /* 0x07 */
733 };
735 static Optable optabD3[8] =
737 0,0, "ROL%S CL,%e", /* 0x00 */
738 0,0, "ROR%S CL,%e", /* 0x01 */
739 0,0, "RCL%S CL,%e", /* 0x02 */
740 0,0, "RCR%S CL,%e", /* 0x03 */
741 0,0, "SHL%S CL,%e", /* 0x04 */
742 0,0, "SHR%S CL,%e", /* 0x05 */
743 0,0, "", /* 0x06 */
744 0,0, "SAR%S CL,%e", /* 0x07 */
745 };
747 static Optable optabD8[8+8] =
749 0,0, "FADDF %e,F0", /* 0x00 */
750 0,0, "FMULF %e,F0", /* 0x01 */
751 0,0, "FCOMF %e,F0", /* 0x02 */
752 0,0, "FCOMFP %e,F0", /* 0x03 */
753 0,0, "FSUBF %e,F0", /* 0x04 */
754 0,0, "FSUBRF %e,F0", /* 0x05 */
755 0,0, "FDIVF %e,F0", /* 0x06 */
756 0,0, "FDIVRF %e,F0", /* 0x07 */
757 0,0, "FADDD %f,F0", /* 0x08 */
758 0,0, "FMULD %f,F0", /* 0x09 */
759 0,0, "FCOMD %f,F0", /* 0x0a */
760 0,0, "FCOMPD %f,F0", /* 0x0b */
761 0,0, "FSUBD %f,F0", /* 0x0c */
762 0,0, "FSUBRD %f,F0", /* 0x0d */
763 0,0, "FDIVD %f,F0", /* 0x0e */
764 0,0, "FDIVRD %f,F0", /* 0x0f */
765 };
766 /*
767 * optabD9 and optabDB use the following encoding:
768 * if (0 <= modrm <= 2) instruction = optabDx[modrm&0x07];
769 * else instruction = optabDx[(modrm&0x3f)+8];
771 * the instructions for MOD == 3, follow the 8 instructions
772 * for the other MOD values stored at the front of the table.
773 */
774 static Optable optabD9[64+8] =
776 0,0, "FMOVF %e,F0", /* 0x00 */
777 0,0, "", /* 0x01 */
778 0,0, "FMOVF F0,%e", /* 0x02 */
779 0,0, "FMOVFP F0,%e", /* 0x03 */
780 0,0, "FLDENV%S %e", /* 0x04 */
781 0,0, "FLDCW %e", /* 0x05 */
782 0,0, "FSTENV%S %e", /* 0x06 */
783 0,0, "FSTCW %e", /* 0x07 */
784 0,0, "FMOVD F0,F0", /* 0x08 */ /* Mod R/M = 11xx xxxx*/
785 0,0, "FMOVD F1,F0", /* 0x09 */
786 0,0, "FMOVD F2,F0", /* 0x0a */
787 0,0, "FMOVD F3,F0", /* 0x0b */
788 0,0, "FMOVD F4,F0", /* 0x0c */
789 0,0, "FMOVD F5,F0", /* 0x0d */
790 0,0, "FMOVD F6,F0", /* 0x0e */
791 0,0, "FMOVD F7,F0", /* 0x0f */
792 0,0, "FXCHD F0,F0", /* 0x10 */
793 0,0, "FXCHD F1,F0", /* 0x11 */
794 0,0, "FXCHD F2,F0", /* 0x12 */
795 0,0, "FXCHD F3,F0", /* 0x13 */
796 0,0, "FXCHD F4,F0", /* 0x14 */
797 0,0, "FXCHD F5,F0", /* 0x15 */
798 0,0, "FXCHD F6,F0", /* 0x16 */
799 0,0, "FXCHD F7,F0", /* 0x17 */
800 0,0, "FNOP", /* 0x18 */
801 0,0, "", /* 0x19 */
802 0,0, "", /* 0x1a */
803 0,0, "", /* 0x1b */
804 0,0, "", /* 0x1c */
805 0,0, "", /* 0x1d */
806 0,0, "", /* 0x1e */
807 0,0, "", /* 0x1f */
808 0,0, "", /* 0x20 */
809 0,0, "", /* 0x21 */
810 0,0, "", /* 0x22 */
811 0,0, "", /* 0x23 */
812 0,0, "", /* 0x24 */
813 0,0, "", /* 0x25 */
814 0,0, "", /* 0x26 */
815 0,0, "", /* 0x27 */
816 0,0, "FCHS", /* 0x28 */
817 0,0, "FABS", /* 0x29 */
818 0,0, "", /* 0x2a */
819 0,0, "", /* 0x2b */
820 0,0, "FTST", /* 0x2c */
821 0,0, "FXAM", /* 0x2d */
822 0,0, "", /* 0x2e */
823 0,0, "", /* 0x2f */
824 0,0, "FLD1", /* 0x30 */
825 0,0, "FLDL2T", /* 0x31 */
826 0,0, "FLDL2E", /* 0x32 */
827 0,0, "FLDPI", /* 0x33 */
828 0,0, "FLDLG2", /* 0x34 */
829 0,0, "FLDLN2", /* 0x35 */
830 0,0, "FLDZ", /* 0x36 */
831 0,0, "", /* 0x37 */
832 0,0, "F2XM1", /* 0x38 */
833 0,0, "FYL2X", /* 0x39 */
834 0,0, "FPTAN", /* 0x3a */
835 0,0, "FPATAN", /* 0x3b */
836 0,0, "FXTRACT", /* 0x3c */
837 0,0, "FPREM1", /* 0x3d */
838 0,0, "FDECSTP", /* 0x3e */
839 0,0, "FNCSTP", /* 0x3f */
840 0,0, "FPREM", /* 0x40 */
841 0,0, "FYL2XP1", /* 0x41 */
842 0,0, "FSQRT", /* 0x42 */
843 0,0, "FSINCOS", /* 0x43 */
844 0,0, "FRNDINT", /* 0x44 */
845 0,0, "FSCALE", /* 0x45 */
846 0,0, "FSIN", /* 0x46 */
847 0,0, "FCOS", /* 0x47 */
848 };
850 static Optable optabDA[8+8] =
852 0,0, "FADDL %e,F0", /* 0x00 */
853 0,0, "FMULL %e,F0", /* 0x01 */
854 0,0, "FCOML %e,F0", /* 0x02 */
855 0,0, "FCOMLP %e,F0", /* 0x03 */
856 0,0, "FSUBL %e,F0", /* 0x04 */
857 0,0, "FSUBRL %e,F0", /* 0x05 */
858 0,0, "FDIVL %e,F0", /* 0x06 */
859 0,0, "FDIVRL %e,F0", /* 0x07 */
860 0,0, "", /* 0x08 */
861 0,0, "", /* 0x09 */
862 0,0, "", /* 0x0a */
863 0,0, "", /* 0x0b */
864 0,0, "", /* 0x0c */
865 R1,0, "FUCOMPP", /* 0x0d */
866 };
868 static Optable optabDB[8+64] =
870 0,0, "FMOVL %e,F0", /* 0x00 */
871 0,0, "", /* 0x01 */
872 0,0, "FMOVL F0,%e", /* 0x02 */
873 0,0, "FMOVLP F0,%e", /* 0x03 */
874 0,0, "", /* 0x04 */
875 0,0, "FMOVX %e,F0", /* 0x05 */
876 0,0, "", /* 0x06 */
877 0,0, "FMOVXP F0,%e", /* 0x07 */
878 0,0, "", /* 0x08 */
879 0,0, "", /* 0x09 */
880 0,0, "", /* 0x0a */
881 0,0, "", /* 0x0b */
882 0,0, "", /* 0x0c */
883 0,0, "", /* 0x0d */
884 0,0, "", /* 0x0e */
885 0,0, "", /* 0x0f */
886 0,0, "", /* 0x10 */
887 0,0, "", /* 0x11 */
888 0,0, "", /* 0x12 */
889 0,0, "", /* 0x13 */
890 0,0, "", /* 0x14 */
891 0,0, "", /* 0x15 */
892 0,0, "", /* 0x16 */
893 0,0, "", /* 0x17 */
894 0,0, "", /* 0x18 */
895 0,0, "", /* 0x19 */
896 0,0, "", /* 0x1a */
897 0,0, "", /* 0x1b */
898 0,0, "", /* 0x1c */
899 0,0, "", /* 0x1d */
900 0,0, "", /* 0x1e */
901 0,0, "", /* 0x1f */
902 0,0, "", /* 0x20 */
903 0,0, "", /* 0x21 */
904 0,0, "", /* 0x22 */
905 0,0, "", /* 0x23 */
906 0,0, "", /* 0x24 */
907 0,0, "", /* 0x25 */
908 0,0, "", /* 0x26 */
909 0,0, "", /* 0x27 */
910 0,0, "", /* 0x28 */
911 0,0, "", /* 0x29 */
912 0,0, "FCLEX", /* 0x2a */
913 0,0, "FINIT", /* 0x2b */
914 };
916 static Optable optabDC[8+8] =
918 0,0, "FADDD %e,F0", /* 0x00 */
919 0,0, "FMULD %e,F0", /* 0x01 */
920 0,0, "FCOMD %e,F0", /* 0x02 */
921 0,0, "FCOMDP %e,F0", /* 0x03 */
922 0,0, "FSUBD %e,F0", /* 0x04 */
923 0,0, "FSUBRD %e,F0", /* 0x05 */
924 0,0, "FDIVD %e,F0", /* 0x06 */
925 0,0, "FDIVRD %e,F0", /* 0x07 */
926 0,0, "FADDD F0,%f", /* 0x08 */
927 0,0, "FMULD F0,%f", /* 0x09 */
928 0,0, "", /* 0x0a */
929 0,0, "", /* 0x0b */
930 0,0, "FSUBRD F0,%f", /* 0x0c */
931 0,0, "FSUBD F0,%f", /* 0x0d */
932 0,0, "FDIVRD F0,%f", /* 0x0e */
933 0,0, "FDIVD F0,%f", /* 0x0f */
934 };
936 static Optable optabDD[8+8] =
938 0,0, "FMOVD %e,F0", /* 0x00 */
939 0,0, "", /* 0x01 */
940 0,0, "FMOVD F0,%e", /* 0x02 */
941 0,0, "FMOVDP F0,%e", /* 0x03 */
942 0,0, "FRSTOR%S %e", /* 0x04 */
943 0,0, "", /* 0x05 */
944 0,0, "FSAVE%S %e", /* 0x06 */
945 0,0, "FSTSW %e", /* 0x07 */
946 0,0, "FFREED %f", /* 0x08 */
947 0,0, "", /* 0x09 */
948 0,0, "FMOVD %f,F0", /* 0x0a */
949 0,0, "FMOVDP %f,F0", /* 0x0b */
950 0,0, "FUCOMD %f,F0", /* 0x0c */
951 0,0, "FUCOMDP %f,F0", /* 0x0d */
952 };
954 static Optable optabDE[8+8] =
956 0,0, "FADDW %e,F0", /* 0x00 */
957 0,0, "FMULW %e,F0", /* 0x01 */
958 0,0, "FCOMW %e,F0", /* 0x02 */
959 0,0, "FCOMWP %e,F0", /* 0x03 */
960 0,0, "FSUBW %e,F0", /* 0x04 */
961 0,0, "FSUBRW %e,F0", /* 0x05 */
962 0,0, "FDIVW %e,F0", /* 0x06 */
963 0,0, "FDIVRW %e,F0", /* 0x07 */
964 0,0, "FADDDP F0,%f", /* 0x08 */
965 0,0, "FMULDP F0,%f", /* 0x09 */
966 0,0, "", /* 0x0a */
967 R1,0, "FCOMPDP", /* 0x0b */
968 0,0, "FSUBRDP F0,%f", /* 0x0c */
969 0,0, "FSUBDP F0,%f", /* 0x0d */
970 0,0, "FDIVRDP F0,%f", /* 0x0e */
971 0,0, "FDIVDP F0,%f", /* 0x0f */
972 };
974 static Optable optabDF[8+8] =
976 0,0, "FMOVW %e,F0", /* 0x00 */
977 0,0, "", /* 0x01 */
978 0,0, "FMOVW F0,%e", /* 0x02 */
979 0,0, "FMOVWP F0,%e", /* 0x03 */
980 0,0, "FBLD %e", /* 0x04 */
981 0,0, "FMOVL %e,F0", /* 0x05 */
982 0,0, "FBSTP %e", /* 0x06 */
983 0,0, "FMOVLP F0,%e", /* 0x07 */
984 0,0, "", /* 0x08 */
985 0,0, "", /* 0x09 */
986 0,0, "", /* 0x0a */
987 0,0, "", /* 0x0b */
988 R0,0, "FSTSW %OAX", /* 0x0c */
989 };
991 static Optable optabF6[8] =
993 Ib,0, "TESTB %i,%e", /* 0x00 */
994 0,0, "", /* 0x01 */
995 0,0, "NOTB %e", /* 0x02 */
996 0,0, "NEGB %e", /* 0x03 */
997 0,0, "MULB AL,%e", /* 0x04 */
998 0,0, "IMULB AL,%e", /* 0x05 */
999 0,0, "DIVB AL,%e", /* 0x06 */
1000 0,0, "IDIVB AL,%e", /* 0x07 */
1003 static Optable optabF7[8] =
1005 Iwd,0, "TEST%S %i,%e", /* 0x00 */
1006 0,0, "", /* 0x01 */
1007 0,0, "NOT%S %e", /* 0x02 */
1008 0,0, "NEG%S %e", /* 0x03 */
1009 0,0, "MUL%S %OAX,%e", /* 0x04 */
1010 0,0, "IMUL%S %OAX,%e", /* 0x05 */
1011 0,0, "DIV%S %OAX,%e", /* 0x06 */
1012 0,0, "IDIV%S %OAX,%e", /* 0x07 */
1015 static Optable optabFE[8] =
1017 0,0, "INCB %e", /* 0x00 */
1018 0,0, "DECB %e", /* 0x01 */
1021 static Optable optabFF[8] =
1023 0,0, "INC%S %e", /* 0x00 */
1024 0,0, "DEC%S %e", /* 0x01 */
1025 JUMP,0, "CALL*%S %e", /* 0x02 */
1026 JUMP,0, "CALLF*%S %e", /* 0x03 */
1027 JUMP,0, "JMP*%S %e", /* 0x04 */
1028 JUMP,0, "JMPF*%S %e", /* 0x05 */
1029 0,0, "PUSHL %e", /* 0x06 */
1032 static Optable optable[256] =
1034 RMB,0, "ADDB %r,%e", /* 0x00 */
1035 RM,0, "ADD%S %r,%e", /* 0x01 */
1036 RMB,0, "ADDB %e,%r", /* 0x02 */
1037 RM,0, "ADD%S %e,%r", /* 0x03 */
1038 Ib,0, "ADDB %i,AL", /* 0x04 */
1039 Iwd,0, "ADD%S %i,%OAX", /* 0x05 */
1040 0,0, "PUSHL ES", /* 0x06 */
1041 0,0, "POPL ES", /* 0x07 */
1042 RMB,0, "ORB %r,%e", /* 0x08 */
1043 RM,0, "OR%S %r,%e", /* 0x09 */
1044 RMB,0, "ORB %e,%r", /* 0x0a */
1045 RM,0, "OR%S %e,%r", /* 0x0b */
1046 Ib,0, "ORB %i,AL", /* 0x0c */
1047 Iwd,0, "OR%S %i,%OAX", /* 0x0d */
1048 0,0, "PUSHL CS", /* 0x0e */
1049 AUX,0, optab0F, /* 0x0f */
1050 RMB,0, "ADCB %r,%e", /* 0x10 */
1051 RM,0, "ADC%S %r,%e", /* 0x11 */
1052 RMB,0, "ADCB %e,%r", /* 0x12 */
1053 RM,0, "ADC%S %e,%r", /* 0x13 */
1054 Ib,0, "ADCB %i,AL", /* 0x14 */
1055 Iwd,0, "ADC%S %i,%OAX", /* 0x15 */
1056 0,0, "PUSHL SS", /* 0x16 */
1057 0,0, "POPL SS", /* 0x17 */
1058 RMB,0, "SBBB %r,%e", /* 0x18 */
1059 RM,0, "SBB%S %r,%e", /* 0x19 */
1060 RMB,0, "SBBB %e,%r", /* 0x1a */
1061 RM,0, "SBB%S %e,%r", /* 0x1b */
1062 Ib,0, "SBBB %i,AL", /* 0x1c */
1063 Iwd,0, "SBB%S %i,%OAX", /* 0x1d */
1064 0,0, "PUSHL DS", /* 0x1e */
1065 0,0, "POPL DS", /* 0x1f */
1066 RMB,0, "ANDB %r,%e", /* 0x20 */
1067 RM,0, "AND%S %r,%e", /* 0x21 */
1068 RMB,0, "ANDB %e,%r", /* 0x22 */
1069 RM,0, "AND%S %e,%r", /* 0x23 */
1070 Ib,0, "ANDB %i,AL", /* 0x24 */
1071 Iwd,0, "AND%S %i,%OAX", /* 0x25 */
1072 SEG,0, "ES:", /* 0x26 */
1073 0,0, "DAA", /* 0x27 */
1074 RMB,0, "SUBB %r,%e", /* 0x28 */
1075 RM,0, "SUB%S %r,%e", /* 0x29 */
1076 RMB,0, "SUBB %e,%r", /* 0x2a */
1077 RM,0, "SUB%S %e,%r", /* 0x2b */
1078 Ib,0, "SUBB %i,AL", /* 0x2c */
1079 Iwd,0, "SUB%S %i,%OAX", /* 0x2d */
1080 SEG,0, "CS:", /* 0x2e */
1081 0,0, "DAS", /* 0x2f */
1082 RMB,0, "XORB %r,%e", /* 0x30 */
1083 RM,0, "XOR%S %r,%e", /* 0x31 */
1084 RMB,0, "XORB %e,%r", /* 0x32 */
1085 RM,0, "XOR%S %e,%r", /* 0x33 */
1086 Ib,0, "XORB %i,AL", /* 0x34 */
1087 Iwd,0, "XOR%S %i,%OAX", /* 0x35 */
1088 SEG,0, "SS:", /* 0x36 */
1089 0,0, "AAA", /* 0x37 */
1090 RMB,0, "CMPB %r,%e", /* 0x38 */
1091 RM,0, "CMP%S %r,%e", /* 0x39 */
1092 RMB,0, "CMPB %e,%r", /* 0x3a */
1093 RM,0, "CMP%S %e,%r", /* 0x3b */
1094 Ib,0, "CMPB %i,AL", /* 0x3c */
1095 Iwd,0, "CMP%S %i,%OAX", /* 0x3d */
1096 SEG,0, "DS:", /* 0x3e */
1097 0,0, "AAS", /* 0x3f */
1098 0,0, "INC%S %OAX", /* 0x40 */
1099 0,0, "INC%S %OCX", /* 0x41 */
1100 0,0, "INC%S %ODX", /* 0x42 */
1101 0,0, "INC%S %OBX", /* 0x43 */
1102 0,0, "INC%S %OSP", /* 0x44 */
1103 0,0, "INC%S %OBP", /* 0x45 */
1104 0,0, "INC%S %OSI", /* 0x46 */
1105 0,0, "INC%S %ODI", /* 0x47 */
1106 0,0, "DEC%S %OAX", /* 0x48 */
1107 0,0, "DEC%S %OCX", /* 0x49 */
1108 0,0, "DEC%S %ODX", /* 0x4a */
1109 0,0, "DEC%S %OBX", /* 0x4b */
1110 0,0, "DEC%S %OSP", /* 0x4c */
1111 0,0, "DEC%S %OBP", /* 0x4d */
1112 0,0, "DEC%S %OSI", /* 0x4e */
1113 0,0, "DEC%S %ODI", /* 0x4f */
1114 0,0, "PUSH%S %OAX", /* 0x50 */
1115 0,0, "PUSH%S %OCX", /* 0x51 */
1116 0,0, "PUSH%S %ODX", /* 0x52 */
1117 0,0, "PUSH%S %OBX", /* 0x53 */
1118 0,0, "PUSH%S %OSP", /* 0x54 */
1119 0,0, "PUSH%S %OBP", /* 0x55 */
1120 0,0, "PUSH%S %OSI", /* 0x56 */
1121 0,0, "PUSH%S %ODI", /* 0x57 */
1122 0,0, "POP%S %OAX", /* 0x58 */
1123 0,0, "POP%S %OCX", /* 0x59 */
1124 0,0, "POP%S %ODX", /* 0x5a */
1125 0,0, "POP%S %OBX", /* 0x5b */
1126 0,0, "POP%S %OSP", /* 0x5c */
1127 0,0, "POP%S %OBP", /* 0x5d */
1128 0,0, "POP%S %OSI", /* 0x5e */
1129 0,0, "POP%S %ODI", /* 0x5f */
1130 0,0, "PUSHA%S", /* 0x60 */
1131 0,0, "POPA%S", /* 0x61 */
1132 RMM,0, "BOUND %e,%r", /* 0x62 */
1133 RM,0, "ARPL %r,%e", /* 0x63 */
1134 SEG,0, "FS:", /* 0x64 */
1135 SEG,0, "GS:", /* 0x65 */
1136 OPOVER,0, "", /* 0x66 */
1137 ADDOVER,0, "", /* 0x67 */
1138 Iwd,0, "PUSH%S %i", /* 0x68 */
1139 RM,Iwd, "IMUL%S %e,%i,%r", /* 0x69 */
1140 Ib,0, "PUSH%S %i", /* 0x6a */
1141 RM,Ibs, "IMUL%S %e,%i,%r", /* 0x6b */
1142 0,0, "INSB DX,(%ODI)", /* 0x6c */
1143 0,0, "INS%S DX,(%ODI)", /* 0x6d */
1144 0,0, "OUTSB (%ASI),DX", /* 0x6e */
1145 0,0, "OUTS%S (%ASI),DX", /* 0x6f */
1146 Jbs,0, "JOS %p", /* 0x70 */
1147 Jbs,0, "JOC %p", /* 0x71 */
1148 Jbs,0, "JCS %p", /* 0x72 */
1149 Jbs,0, "JCC %p", /* 0x73 */
1150 Jbs,0, "JEQ %p", /* 0x74 */
1151 Jbs,0, "JNE %p", /* 0x75 */
1152 Jbs,0, "JLS %p", /* 0x76 */
1153 Jbs,0, "JHI %p", /* 0x77 */
1154 Jbs,0, "JMI %p", /* 0x78 */
1155 Jbs,0, "JPL %p", /* 0x79 */
1156 Jbs,0, "JPS %p", /* 0x7a */
1157 Jbs,0, "JPC %p", /* 0x7b */
1158 Jbs,0, "JLT %p", /* 0x7c */
1159 Jbs,0, "JGE %p", /* 0x7d */
1160 Jbs,0, "JLE %p", /* 0x7e */
1161 Jbs,0, "JGT %p", /* 0x7f */
1162 RMOPB,0, optab80, /* 0x80 */
1163 RMOP,0, optab81, /* 0x81 */
1164 0,0, "", /* 0x82 */
1165 RMOP,0, optab83, /* 0x83 */
1166 RMB,0, "TESTB %r,%e", /* 0x84 */
1167 RM,0, "TEST%S %r,%e", /* 0x85 */
1168 RMB,0, "XCHGB %r,%e", /* 0x86 */
1169 RM,0, "XCHG%S %r,%e", /* 0x87 */
1170 RMB,0, "MOVB %r,%e", /* 0x88 */
1171 RM,0, "MOV%S %r,%e", /* 0x89 */
1172 RMB,0, "MOVB %e,%r", /* 0x8a */
1173 RM,0, "MOV%S %e,%r", /* 0x8b */
1174 RM,0, "MOVW %g,%e", /* 0x8c */
1175 RM,0, "LEA %e,%r", /* 0x8d */
1176 RM,0, "MOVW %e,%g", /* 0x8e */
1177 RM,0, "POP%S %e", /* 0x8f */
1178 0,0, "NOP", /* 0x90 */
1179 0,0, "XCHG %OCX,%OAX", /* 0x91 */
1180 0,0, "XCHG %OCX,%OAX", /* 0x92 */
1181 0,0, "XCHG %OCX,%OAX", /* 0x93 */
1182 0,0, "XCHG %OSP,%OAX", /* 0x94 */
1183 0,0, "XCHG %OBP,%OAX", /* 0x95 */
1184 0,0, "XCHG %OSI,%OAX", /* 0x96 */
1185 0,0, "XCHG %ODI,%OAX", /* 0x97 */
1186 0,0, "%X", /* 0x98 */ /* miserable CBW or CWDE */
1187 0,0, "%x", /* 0x99 */ /* idiotic CWD or CDQ */
1188 PTR,0, "CALL%S %d", /* 0x9a */
1189 0,0, "WAIT", /* 0x9b */
1190 0,0, "PUSHF", /* 0x9c */
1191 0,0, "POPF", /* 0x9d */
1192 0,0, "SAHF", /* 0x9e */
1193 0,0, "LAHF", /* 0x9f */
1194 Awd,0, "MOVB %i,AL", /* 0xa0 */
1195 Awd,0, "MOV%S %i,%OAX", /* 0xa1 */
1196 Awd,0, "MOVB AL,%i", /* 0xa2 */
1197 Awd,0, "MOV%S %OAX,%i", /* 0xa3 */
1198 0,0, "MOVSB (%ASI),(%ADI)", /* 0xa4 */
1199 0,0, "MOVS%S (%ASI),(%ADI)", /* 0xa5 */
1200 0,0, "CMPSB (%ASI),(%ADI)", /* 0xa6 */
1201 0,0, "CMPS%S (%ASI),(%ADI)", /* 0xa7 */
1202 Ib,0, "TESTB %i,AL", /* 0xa8 */
1203 Iwd,0, "TEST%S %i,%OAX", /* 0xa9 */
1204 0,0, "STOSB AL,(%ADI)", /* 0xaa */
1205 0,0, "STOS%S %OAX,(%ADI)", /* 0xab */
1206 0,0, "LODSB (%ASI),AL", /* 0xac */
1207 0,0, "LODS%S (%ASI),%OAX", /* 0xad */
1208 0,0, "SCASB (%ADI),AL", /* 0xae */
1209 0,0, "SCAS%S (%ADI),%OAX", /* 0xaf */
1210 Ib,0, "MOVB %i,AL", /* 0xb0 */
1211 Ib,0, "MOVB %i,CL", /* 0xb1 */
1212 Ib,0, "MOVB %i,DL", /* 0xb2 */
1213 Ib,0, "MOVB %i,BL", /* 0xb3 */
1214 Ib,0, "MOVB %i,AH", /* 0xb4 */
1215 Ib,0, "MOVB %i,CH", /* 0xb5 */
1216 Ib,0, "MOVB %i,DH", /* 0xb6 */
1217 Ib,0, "MOVB %i,BH", /* 0xb7 */
1218 Iwd,0, "MOV%S %i,%OAX", /* 0xb8 */
1219 Iwd,0, "MOV%S %i,%OCX", /* 0xb9 */
1220 Iwd,0, "MOV%S %i,%ODX", /* 0xba */
1221 Iwd,0, "MOV%S %i,%OBX", /* 0xbb */
1222 Iwd,0, "MOV%S %i,%OSP", /* 0xbc */
1223 Iwd,0, "MOV%S %i,%OBP", /* 0xbd */
1224 Iwd,0, "MOV%S %i,%OSI", /* 0xbe */
1225 Iwd,0, "MOV%S %i,%ODI", /* 0xbf */
1226 RMOPB,0, optabC0, /* 0xc0 */
1227 RMOP,0, optabC1, /* 0xc1 */
1228 Iw,0, "RET %i", /* 0xc2 */
1229 RET,0, "RET", /* 0xc3 */
1230 RM,0, "LES %e,%r", /* 0xc4 */
1231 RM,0, "LDS %e,%r", /* 0xc5 */
1232 RMB,Ib, "MOVB %i,%e", /* 0xc6 */
1233 RM,Iwd, "MOV%S %i,%e", /* 0xc7 */
1234 Iw2,Ib, "ENTER %i,%I", /* 0xc8 */ /* loony ENTER */
1235 RET,0, "LEAVE", /* 0xc9 */ /* bizarre LEAVE */
1236 Iw,0, "RETF %i", /* 0xca */
1237 RET,0, "RETF", /* 0xcb */
1238 0,0, "INT 3", /* 0xcc */
1239 Ib,0, "INTB %i", /* 0xcd */
1240 0,0, "INTO", /* 0xce */
1241 0,0, "IRET", /* 0xcf */
1242 RMOPB,0, optabD0, /* 0xd0 */
1243 RMOP,0, optabD1, /* 0xd1 */
1244 RMOPB,0, optabD2, /* 0xd2 */
1245 RMOP,0, optabD3, /* 0xd3 */
1246 OA,0, "AAM", /* 0xd4 */
1247 OA,0, "AAD", /* 0xd5 */
1248 0,0, "", /* 0xd6 */
1249 0,0, "XLAT", /* 0xd7 */
1250 FRMOP,0, optabD8, /* 0xd8 */
1251 FRMEX,0, optabD9, /* 0xd9 */
1252 FRMOP,0, optabDA, /* 0xda */
1253 FRMEX,0, optabDB, /* 0xdb */
1254 FRMOP,0, optabDC, /* 0xdc */
1255 FRMOP,0, optabDD, /* 0xdd */
1256 FRMOP,0, optabDE, /* 0xde */
1257 FRMOP,0, optabDF, /* 0xdf */
1258 Jbs,0, "LOOPNE %p", /* 0xe0 */
1259 Jbs,0, "LOOPE %p", /* 0xe1 */
1260 Jbs,0, "LOOP %p", /* 0xe2 */
1261 Jbs,0, "JCXZ %p", /* 0xe3 */
1262 Ib,0, "INB %i,AL", /* 0xe4 */
1263 Ib,0, "IN%S %i,%OAX", /* 0xe5 */
1264 Ib,0, "OUTB AL,%i", /* 0xe6 */
1265 Ib,0, "OUT%S %OAX,%i", /* 0xe7 */
1266 Iwds,0, "CALL %p", /* 0xe8 */
1267 Iwds,0, "JMP %p", /* 0xe9 */
1268 PTR,0, "JMP %d", /* 0xea */
1269 Jbs,0, "JMP %p", /* 0xeb */
1270 0,0, "INB DX,AL", /* 0xec */
1271 0,0, "IN%S DX,%OAX", /* 0xed */
1272 0,0, "OUTB AL,DX", /* 0xee */
1273 0,0, "OUT%S %OAX,DX", /* 0xef */
1274 PRE,0, "LOCK", /* 0xf0 */
1275 0,0, "", /* 0xf1 */
1276 PRE,0, "REPNE", /* 0xf2 */
1277 PRE,0, "REP", /* 0xf3 */
1278 0,0, "HALT", /* 0xf4 */
1279 0,0, "CMC", /* 0xf5 */
1280 RMOPB,0, optabF6, /* 0xf6 */
1281 RMOP,0, optabF7, /* 0xf7 */
1282 0,0, "CLC", /* 0xf8 */
1283 0,0, "STC", /* 0xf9 */
1284 0,0, "CLI", /* 0xfa */
1285 0,0, "STI", /* 0xfb */
1286 0,0, "CLD", /* 0xfc */
1287 0,0, "STD", /* 0xfd */
1288 RMOPB,0, optabFE, /* 0xfe */
1289 RMOP,0, optabFF, /* 0xff */
1293 * get a byte of the instruction
1295 static int
1296 igetc(Map * map, Instr *ip, uchar *c)
1298 if(ip->n+1 > sizeof(ip->mem)){
1299 werrstr("instruction too long");
1300 return -1;
1302 if (get1(map, ip->addr+ip->n, c, 1) < 0) {
1303 werrstr("can't read instruction: %r");
1304 return -1;
1306 ip->mem[ip->n++] = *c;
1307 return 1;
1311 * get two bytes of the instruction
1313 static int
1314 igets(Map *map, Instr *ip, ushort *sp)
1316 uchar c;
1317 ushort s;
1319 if (igetc(map, ip, &c) < 0)
1320 return -1;
1321 s = c;
1322 if (igetc(map, ip, &c) < 0)
1323 return -1;
1324 s |= (c<<8);
1325 *sp = s;
1326 return 1;
1330 * get 4 bytes of the instruction
1332 static int
1333 igetl(Map *map, Instr *ip, ulong *lp)
1335 ushort s;
1336 long l;
1338 if (igets(map, ip, &s) < 0)
1339 return -1;
1340 l = s;
1341 if (igets(map, ip, &s) < 0)
1342 return -1;
1343 l |= (s<<16);
1344 *lp = l;
1345 return 1;
1348 static int
1349 getdisp(Map *map, Instr *ip, int mod, int rm, int code)
1351 uchar c;
1352 ushort s;
1354 if (mod > 2)
1355 return 1;
1356 if (mod == 1) {
1357 if (igetc(map, ip, &c) < 0)
1358 return -1;
1359 if (c&0x80)
1360 ip->disp = c|0xffffff00;
1361 else
1362 ip->disp = c&0xff;
1363 } else if (mod == 2 || rm == code) {
1364 if (ip->asize == 'E') {
1365 if (igetl(map, ip, &ip->disp) < 0)
1366 return -1;
1367 } else {
1368 if (igets(map, ip, &s) < 0)
1369 return -1;
1370 if (s&0x8000)
1371 ip->disp = s|0xffff0000;
1372 else
1373 ip->disp = s;
1375 if (mod == 0)
1376 ip->base = -1;
1378 return 1;
1381 static int
1382 modrm(Map *map, Instr *ip, uchar c)
1384 uchar rm, mod;
1386 mod = (c>>6)&3;
1387 rm = c&7;
1388 ip->mod = mod;
1389 ip->base = rm;
1390 ip->reg = (c>>3)&7;
1391 if (mod == 3) /* register */
1392 return 1;
1393 if (ip->asize == 0) { /* 16-bit mode */
1394 switch(rm)
1396 case 0:
1397 ip->base = BX; ip->index = SI;
1398 break;
1399 case 1:
1400 ip->base = BX; ip->index = DI;
1401 break;
1402 case 2:
1403 ip->base = BP; ip->index = SI;
1404 break;
1405 case 3:
1406 ip->base = BP; ip->index = DI;
1407 break;
1408 case 4:
1409 ip->base = SI;
1410 break;
1411 case 5:
1412 ip->base = DI;
1413 break;
1414 case 6:
1415 ip->base = BP;
1416 break;
1417 case 7:
1418 ip->base = BX;
1419 break;
1420 default:
1421 break;
1423 return getdisp(map, ip, mod, rm, 6);
1425 if (rm == 4) { /* scummy sib byte */
1426 if (igetc(map, ip, &c) < 0)
1427 return -1;
1428 ip->ss = (c>>6)&0x03;
1429 ip->index = (c>>3)&0x07;
1430 if (ip->index == 4)
1431 ip->index = -1;
1432 ip->base = c&0x07;
1433 return getdisp(map, ip, mod, ip->base, 5);
1435 return getdisp(map, ip, mod, rm, 5);
1438 static Optable *
1439 mkinstr(Map *map, Instr *ip, ulong pc)
1441 int i, n;
1442 uchar c;
1443 ushort s;
1444 Optable *op, *obase;
1445 char buf[128];
1447 memset(ip, 0, sizeof(*ip));
1448 ip->base = -1;
1449 ip->index = -1;
1450 if(0) /* asstype == AI8086) */
1451 ip->osize = 'W';
1452 else {
1453 ip->osize = 'L';
1454 ip->asize = 'E';
1456 ip->addr = pc;
1457 if (igetc(map, ip, &c) < 0)
1458 return 0;
1459 obase = optable;
1460 newop:
1461 op = &obase[c];
1462 if (op->proto == 0) {
1463 badop:
1464 n = snprint(buf, sizeof(buf), "opcode: ??");
1465 for (i = 0; i < ip->n && n < sizeof(buf)-3; i++, n+=2)
1466 _hexify(buf+n, ip->mem[i], 1);
1467 strcpy(buf+n, "??");
1468 werrstr(buf);
1469 return 0;
1471 for(i = 0; i < 2 && op->operand[i]; i++) {
1472 switch(op->operand[i])
1474 case Ib: /* 8-bit immediate - (no sign extension)*/
1475 if (igetc(map, ip, &c) < 0)
1476 return 0;
1477 ip->imm = c&0xff;
1478 break;
1479 case Jbs: /* 8-bit jump immediate (sign extended) */
1480 if (igetc(map, ip, &c) < 0)
1481 return 0;
1482 if (c&0x80)
1483 ip->imm = c|0xffffff00;
1484 else
1485 ip->imm = c&0xff;
1486 ip->jumptype = Jbs;
1487 break;
1488 case Ibs: /* 8-bit immediate (sign extended) */
1489 if (igetc(map, ip, &c) < 0)
1490 return 0;
1491 if (c&0x80)
1492 if (ip->osize == 'L')
1493 ip->imm = c|0xffffff00;
1494 else
1495 ip->imm = c|0xff00;
1496 else
1497 ip->imm = c&0xff;
1498 break;
1499 case Iw: /* 16-bit immediate -> imm */
1500 if (igets(map, ip, &s) < 0)
1501 return 0;
1502 ip->imm = s&0xffff;
1503 ip->jumptype = Iw;
1504 break;
1505 case Iw2: /* 16-bit immediate -> in imm2*/
1506 if (igets(map, ip, &s) < 0)
1507 return 0;
1508 ip->imm2 = s&0xffff;
1509 break;
1510 case Iwd: /* Operand-sized immediate (no sign extension)*/
1511 if (ip->osize == 'L') {
1512 if (igetl(map, ip, &ip->imm) < 0)
1513 return 0;
1514 } else {
1515 if (igets(map, ip, &s)< 0)
1516 return 0;
1517 ip->imm = s&0xffff;
1519 break;
1520 case Awd: /* Address-sized immediate (no sign extension)*/
1521 if (ip->asize == 'E') {
1522 if (igetl(map, ip, &ip->imm) < 0)
1523 return 0;
1524 } else {
1525 if (igets(map, ip, &s)< 0)
1526 return 0;
1527 ip->imm = s&0xffff;
1529 break;
1530 case Iwds: /* Operand-sized immediate (sign extended) */
1531 if (ip->osize == 'L') {
1532 if (igetl(map, ip, &ip->imm) < 0)
1533 return 0;
1534 } else {
1535 if (igets(map, ip, &s)< 0)
1536 return 0;
1537 if (s&0x8000)
1538 ip->imm = s|0xffff0000;
1539 else
1540 ip->imm = s&0xffff;
1542 ip->jumptype = Iwds;
1543 break;
1544 case OA: /* literal 0x0a byte */
1545 if (igetc(map, ip, &c) < 0)
1546 return 0;
1547 if (c != 0x0a)
1548 goto badop;
1549 break;
1550 case R0: /* base register must be R0 */
1551 if (ip->base != 0)
1552 goto badop;
1553 break;
1554 case R1: /* base register must be R1 */
1555 if (ip->base != 1)
1556 goto badop;
1557 break;
1558 case RMB: /* R/M field with byte register (/r)*/
1559 if (igetc(map, ip, &c) < 0)
1560 return 0;
1561 if (modrm(map, ip, c) < 0)
1562 return 0;
1563 ip->osize = 'B';
1564 break;
1565 case RM: /* R/M field with register (/r) */
1566 if (igetc(map, ip, &c) < 0)
1567 return 0;
1568 if (modrm(map, ip, c) < 0)
1569 return 0;
1570 break;
1571 case RMOPB: /* R/M field with op code (/digit) */
1572 if (igetc(map, ip, &c) < 0)
1573 return 0;
1574 if (modrm(map, ip, c) < 0)
1575 return 0;
1576 c = ip->reg; /* secondary op code */
1577 obase = (Optable*)op->proto;
1578 ip->osize = 'B';
1579 goto newop;
1580 case RMOP: /* R/M field with op code (/digit) */
1581 if (igetc(map, ip, &c) < 0)
1582 return 0;
1583 if (modrm(map, ip, c) < 0)
1584 return 0;
1585 c = ip->reg;
1586 obase = (Optable*)op->proto;
1587 goto newop;
1588 case FRMOP: /* FP R/M field with op code (/digit) */
1589 if (igetc(map, ip, &c) < 0)
1590 return 0;
1591 if (modrm(map, ip, c) < 0)
1592 return 0;
1593 if ((c&0xc0) == 0xc0)
1594 c = ip->reg+8; /* 16 entry table */
1595 else
1596 c = ip->reg;
1597 obase = (Optable*)op->proto;
1598 goto newop;
1599 case FRMEX: /* Extended FP R/M field with op code (/digit) */
1600 if (igetc(map, ip, &c) < 0)
1601 return 0;
1602 if (modrm(map, ip, c) < 0)
1603 return 0;
1604 if ((c&0xc0) == 0xc0)
1605 c = (c&0x3f)+8; /* 64-entry table */
1606 else
1607 c = ip->reg;
1608 obase = (Optable*)op->proto;
1609 goto newop;
1610 case RMR: /* R/M register only (mod = 11) */
1611 if (igetc(map, ip, &c) < 0)
1612 return 0;
1613 if ((c&0xc0) != 0xc0) {
1614 werrstr("invalid R/M register: %x", c);
1615 return 0;
1617 if (modrm(map, ip, c) < 0)
1618 return 0;
1619 break;
1620 case RMM: /* R/M register only (mod = 11) */
1621 if (igetc(map, ip, &c) < 0)
1622 return 0;
1623 if ((c&0xc0) == 0xc0) {
1624 werrstr("invalid R/M memory mode: %x", c);
1625 return 0;
1627 if (modrm(map, ip, c) < 0)
1628 return 0;
1629 break;
1630 case PTR: /* Seg:Displacement addr (ptr16:16 or ptr16:32) */
1631 if (ip->osize == 'L') {
1632 if (igetl(map, ip, &ip->disp) < 0)
1633 return 0;
1634 } else {
1635 if (igets(map, ip, &s)< 0)
1636 return 0;
1637 ip->disp = s&0xffff;
1639 if (igets(map, ip, (ushort*)&ip->seg) < 0)
1640 return 0;
1641 ip->jumptype = PTR;
1642 break;
1643 case AUX: /* Multi-byte op code - Auxiliary table */
1644 obase = (Optable*)op->proto;
1645 if (igetc(map, ip, &c) < 0)
1646 return 0;
1647 goto newop;
1648 case PRE: /* Instr Prefix */
1649 ip->prefix = (char*)op->proto;
1650 if (igetc(map, ip, &c) < 0)
1651 return 0;
1652 goto newop;
1653 case SEG: /* Segment Prefix */
1654 ip->segment = (char*)op->proto;
1655 if (igetc(map, ip, &c) < 0)
1656 return 0;
1657 goto newop;
1658 case OPOVER: /* Operand size override */
1659 ip->osize = 'W';
1660 if (igetc(map, ip, &c) < 0)
1661 return 0;
1662 goto newop;
1663 case ADDOVER: /* Address size override */
1664 ip->asize = 0;
1665 if (igetc(map, ip, &c) < 0)
1666 return 0;
1667 goto newop;
1668 case JUMP: /* mark instruction as JUMP or RET */
1669 case RET:
1670 ip->jumptype = op->operand[i];
1671 break;
1672 default:
1673 werrstr("bad operand type %d", op->operand[i]);
1674 return 0;
1677 return op;
1680 static void
1681 bprint(Instr *ip, char *fmt, ...)
1683 va_list arg;
1685 va_start(arg, fmt);
1686 ip->curr = vseprint(ip->curr, ip->end, fmt, arg);
1687 va_end(arg);
1691 * if we want to call 16 bit regs AX,BX,CX,...
1692 * and 32 bit regs EAX,EBX,ECX,... then
1693 * change the defs of ANAME and ONAME to:
1694 * #define ANAME(ip) ((ip->asize == 'E' ? "E" : "")
1695 * #define ONAME(ip) ((ip)->osize == 'L' ? "E" : "")
1697 #define ANAME(ip) ""
1698 #define ONAME(ip) ""
1700 static char *reg[] = {
1701 "AX", /* 0 */
1702 "CX", /* 1 */
1703 "DX", /* 2 */
1704 "BX", /* 3 */
1705 "SP", /* 4 */
1706 "BP", /* 5 */
1707 "SI", /* 6 */
1708 "DI", /* 7 */
1711 static char *breg[] = { "AL", "CL", "DL", "BL", "AH", "CH", "DH", "BH" };
1712 static char *sreg[] = { "ES", "CS", "SS", "DS", "FS", "GS" };
1714 static void
1715 plocal(Instr *ip)
1717 Symbol s;
1718 char *name;
1719 Loc l, li;
1721 l.type = LOFFSET;
1722 l.offset = ip->disp;
1723 if(ip->base == SP)
1724 l.reg = "SP";
1725 else
1726 l.reg = "BP";
1728 li.type = LADDR;
1729 li.addr = ip->addr;
1730 if(findsym(li, CTEXT, &s) < 0)
1731 goto raw;
1733 name = nil;
1734 if(ip->base==SP && lookuplsym(&s, FRAMENAME, &s) >= 0){
1735 /* translate stack offset to offset from plan 9 frame pointer */
1736 /* XXX not sure how to do this */
1739 if(name==nil && findlsym(&s, l, &s) >= 0)
1740 name = s.name;
1742 if(name)
1743 bprint(ip, "%s+", name);
1745 raw:
1746 bprint(ip, "%lx(%s)", l.offset, l.reg);
1749 static int
1750 isjmp(Instr *ip)
1752 switch(ip->jumptype){
1753 case Iwds:
1754 case Jbs:
1755 case JUMP:
1756 return 1;
1757 default:
1758 return 0;
1763 * This is too smart for its own good, but it really is nice
1764 * to have accurate translations when debugging, and it
1765 * helps us identify which code is different in binaries that
1766 * are changed on sources.
1768 static int
1769 issymref(Instr *ip, Symbol *s, long w, long val)
1771 Symbol next, tmp;
1772 long isstring, size;
1774 if (isjmp(ip))
1775 return 1;
1776 if (s->class==CTEXT && w==0)
1777 return 1;
1778 if (s->class==CDATA) {
1779 /* use first bss symbol (or "end") rather than edata */
1780 if (s->name[0]=='e' && strcmp(s->name, "edata") == 0){
1781 if((indexsym(s->index+1, &tmp) && loccmp(&tmp.loc, &s->loc)==0)
1782 || (indexsym(s->index-1, &tmp) && loccmp(&tmp.loc, &s->loc)==0))
1783 *s = tmp;
1785 if (w == 0)
1786 return 1;
1787 for (next=*s; next.loc.addr==s->loc.addr; next=tmp)
1788 if (!indexsym(next.index+1, &tmp))
1789 break;
1790 size = next.loc.addr - s->loc.addr;
1791 if (w >= size)
1792 return 0;
1793 if (w > size-w)
1794 w = size-w;
1795 /* huge distances are usually wrong except in .string */
1796 isstring = (s->name[0]=='.' && strcmp(s->name, ".string") == 0);
1797 if (w > 8192 && !isstring)
1798 return 0;
1799 /* medium distances are tricky - look for constants */
1800 /* near powers of two */
1801 if ((val&(val-1)) == 0 || (val&(val+1)) == 0)
1802 return 0;
1803 return 1;
1805 return 0;
1808 static void
1809 immediate(Instr *ip, long val)
1811 Symbol s;
1812 long w;
1813 Loc l;
1815 l.type = LADDR;
1816 l.addr = val;
1817 if (findsym(l, CANY, &s) >= 0) {
1818 w = val - s.loc.addr;
1819 if (w < 0)
1820 w = -w;
1821 if (issymref(ip, &s, w, val)) {
1822 if (w)
1823 bprint(ip, "%s+%lux(SB)", s.name, w);
1824 else
1825 bprint(ip, "%s(SB)", s.name);
1826 return;
1828 if (s.class==CDATA && indexsym(s.index+1, &s) >= 0) {
1829 w = s.loc.addr - val;
1830 if (w < 0)
1831 w = -w;
1832 if (w < 4096) {
1833 bprint(ip, "%s-%lux(SB)", s.name, w);
1834 return;
1838 bprint(ip, "%lux", val);
1841 static void
1842 pea(Instr *ip)
1844 if (ip->mod == 3) {
1845 if (ip->osize == 'B')
1846 bprint(ip, breg[(uchar)ip->base]);
1847 else
1848 bprint(ip, "%s%s", ANAME(ip), reg[(uchar)ip->base]);
1849 return;
1851 if (ip->segment)
1852 bprint(ip, ip->segment);
1853 if (ip->asize == 'E' && (ip->base == SP || ip->base == BP))
1854 plocal(ip);
1855 else {
1856 if (ip->base < 0)
1857 immediate(ip, ip->disp);
1858 else {
1859 bprint(ip, "%lux", ip->disp);
1860 bprint(ip,"(%s%s)", ANAME(ip), reg[(uchar)ip->base]);
1863 if (ip->index >= 0)
1864 bprint(ip,"(%s%s*%d)", ANAME(ip), reg[(uchar)ip->index], 1<<ip->ss);
1867 static void
1868 prinstr(Instr *ip, char *fmt)
1870 if (ip->prefix)
1871 bprint(ip, "%s ", ip->prefix);
1872 for (; *fmt && ip->curr < ip->end; fmt++) {
1873 if (*fmt != '%')
1874 *ip->curr++ = *fmt;
1875 else switch(*++fmt)
1877 case '%':
1878 *ip->curr++ = '%';
1879 break;
1880 case 'A':
1881 bprint(ip, "%s", ANAME(ip));
1882 break;
1883 case 'C':
1884 bprint(ip, "CR%d", ip->reg);
1885 break;
1886 case 'D':
1887 if (ip->reg < 4 || ip->reg == 6 || ip->reg == 7)
1888 bprint(ip, "DR%d",ip->reg);
1889 else
1890 bprint(ip, "???");
1891 break;
1892 case 'I':
1893 bprint(ip, "$");
1894 immediate(ip, ip->imm2);
1895 break;
1896 case 'O':
1897 bprint(ip,"%s", ONAME(ip));
1898 break;
1899 case 'i':
1900 bprint(ip, "$");
1901 immediate(ip,ip->imm);
1902 break;
1903 case 'R':
1904 bprint(ip, "%s%s", ONAME(ip), reg[ip->reg]);
1905 break;
1906 case 'S':
1907 bprint(ip, "%c", ip->osize);
1908 break;
1909 case 'T':
1910 if (ip->reg == 6 || ip->reg == 7)
1911 bprint(ip, "TR%d",ip->reg);
1912 else
1913 bprint(ip, "???");
1914 break;
1915 case 'X':
1916 if (ip->osize == 'L')
1917 bprint(ip,"CWDE");
1918 else
1919 bprint(ip, "CBW");
1920 break;
1921 case 'd':
1922 bprint(ip,"%lux:%lux",ip->seg,ip->disp);
1923 break;
1924 case 'e':
1925 pea(ip);
1926 break;
1927 case 'f':
1928 bprint(ip, "F%d", ip->base);
1929 break;
1930 case 'g':
1931 if (ip->reg < 6)
1932 bprint(ip,"%s",sreg[ip->reg]);
1933 else
1934 bprint(ip,"???");
1935 break;
1936 case 'p':
1937 immediate(ip, ip->imm+ip->addr+ip->n);
1938 break;
1939 case 'r':
1940 if (ip->osize == 'B')
1941 bprint(ip,"%s",breg[ip->reg]);
1942 else
1943 bprint(ip, reg[ip->reg]);
1944 break;
1945 case 'x':
1946 if (ip->osize == 'L')
1947 bprint(ip,"CDQ");
1948 else
1949 bprint(ip, "CWD");
1950 break;
1951 default:
1952 bprint(ip, "%%%c", *fmt);
1953 break;
1956 *ip->curr = 0; /* there's always room for 1 byte */
1959 static int
1960 i386das(Map *map, ulong pc, char modifier, char *buf, int n)
1962 Instr instr;
1963 Optable *op;
1965 USED(modifier);
1966 op = mkinstr(map, &instr, pc);
1967 if (op == 0) {
1968 errstr(buf, n);
1969 return -1;
1971 instr.curr = buf;
1972 instr.end = buf+n-1;
1973 prinstr(&instr, op->proto);
1974 return instr.n;
1977 static int
1978 i386hexinst(Map *map, ulong pc, char *buf, int n)
1980 Instr instr;
1981 int i;
1983 if (mkinstr(map, &instr, pc) == 0) {
1984 errstr(buf, n);
1985 return -1;
1987 for(i = 0; i < instr.n && n > 2; i++) {
1988 _hexify(buf, instr.mem[i], 1);
1989 buf += 2;
1990 n -= 2;
1992 *buf = 0;
1993 return instr.n;
1996 static int
1997 i386instlen(Map *map, ulong pc)
1999 Instr i;
2001 if (mkinstr(map, &i, pc))
2002 return i.n;
2003 return -1;
2006 static int
2007 i386foll(Map *map, Regs *regs, ulong pc, ulong *foll)
2009 Instr i;
2010 Optable *op;
2011 ushort s;
2012 ulong addr;
2013 u32int l;
2014 int n;
2016 op = mkinstr(map, &i, pc);
2017 if (!op)
2018 return -1;
2020 n = 0;
2022 switch(i.jumptype) {
2023 case RET: /* RETURN or LEAVE */
2024 case Iw: /* RETURN */
2025 if (strcmp(op->proto, "LEAVE") == 0) {
2026 if (lget4(map, regs, locindir("BP", 0), &l) < 0)
2027 return -1;
2028 } else if (lget4(map, regs, locindir(mach->sp, 0), &l) < 0)
2029 return -1;
2030 foll[0] = l;
2031 return 1;
2032 case Iwds: /* pc relative JUMP or CALL*/
2033 case Jbs: /* pc relative JUMP or CALL */
2034 foll[0] = pc+i.imm+i.n;
2035 n = 1;
2036 break;
2037 case PTR: /* seg:displacement JUMP or CALL */
2038 foll[0] = (i.seg<<4)+i.disp;
2039 return 1;
2040 case JUMP: /* JUMP or CALL EA */
2042 if(i.mod == 3) {
2043 if (rget(regs, reg[(uchar)i.base], &foll[0]) < 0)
2044 return -1;
2045 return 1;
2047 /* calculate the effective address */
2048 addr = i.disp;
2049 if (i.base >= 0) {
2050 if (lget4(map, regs, locindir(reg[(uchar)i.base], 0), &l) < 0)
2051 return -1;
2052 addr += l;
2054 if (i.index >= 0) {
2055 if (lget4(map, regs, locindir(reg[(uchar)i.index], 0), &l) < 0)
2056 return -1;
2057 addr += l*(1<<i.ss);
2059 /* now retrieve a seg:disp value at that address */
2060 if (get2(map, addr, &s) < 0) /* seg */
2061 return -1;
2062 foll[0] = s<<4;
2063 addr += 2;
2064 if (i.asize == 'L') {
2065 if (get4(map, addr, &l) < 0) /* disp32 */
2066 return -1;
2067 foll[0] += l;
2068 } else { /* disp16 */
2069 if (get2(map, addr, &s) < 0)
2070 return -1;
2071 foll[0] += s;
2073 return 1;
2074 default:
2075 break;
2077 if (strncmp(op->proto,"JMP", 3) == 0 || strncmp(op->proto,"CALL", 4) == 0)
2078 return 1;
2079 foll[n++] = pc+i.n;
2080 return n;