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