1/*	$NetBSD: db_disasm.c,v 1.39 2009/03/14 15:36:07 dsl Exp $	*/
2
3/*
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * All Rights Reserved.
7 *
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21 *  School of Computer Science
22 *  Carnegie Mellon University
23 *  Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 *
28 *	Id: db_disasm.c,v 2.3 91/02/05 17:11:03 mrt (CMU)
29 */
30
31/*
32 * Instruction disassembler.
33 */
34
35#include <sys/cdefs.h>
36__KERNEL_RCSID(0, "$NetBSD: db_disasm.c,v 1.39 2009/03/14 15:36:07 dsl Exp $");
37
38#include <sys/param.h>
39#include <sys/proc.h>
40#include <machine/db_machdep.h>
41
42#include <ddb/db_access.h>
43#include <ddb/db_sym.h>
44#include <ddb/db_output.h>
45#include <ddb/db_interface.h>
46
47/*
48 * Size attributes
49 */
50#define	BYTE	0
51#define	WORD	1
52#define	LONG	2
53#define	QUAD	3
54#define	SNGL	4
55#define	DBLR	5
56#define	EXTR	6
57#define	SDEP	7
58#define	NONE	8
59
60/*
61 * Addressing modes
62 */
63#define	E	1			/* general effective address */
64#define	Eind	2			/* indirect address (jump, call) */
65#define	Ew	3			/* address, word size */
66#define	Eb	4			/* address, byte size */
67#define	R	5			/* register, in 'reg' field */
68#define	Rw	6			/* word register, in 'reg' field */
69#define	Ri	7			/* register in instruction */
70#define	S	8			/* segment reg, in 'reg' field */
71#define	Si	9			/* segment reg, in instruction */
72#define	A	10			/* accumulator */
73#define	BX	11			/* (bx) */
74#define	CL	12			/* cl, for shifts */
75#define	DX	13			/* dx, for IO */
76#define	SI	14			/* si */
77#define	DI	15			/* di */
78#define	CR	16			/* control register */
79#define	DR	17			/* debug register */
80#define	TR	18			/* test register */
81#define	I	19			/* immediate, unsigned */
82#define	Is	20			/* immediate, signed */
83#define	Ib	21			/* byte immediate, unsigned */
84#define	Ibs	22			/* byte immediate, signed */
85#define	Iw	23			/* word immediate, unsigned */
86#define	Il	24			/* long immediate */
87#define	O	25			/* direct address */
88#define	Db	26			/* byte displacement from EIP */
89#define	Dl	27			/* long displacement from EIP */
90#define	o1	28			/* constant 1 */
91#define	o3	29			/* constant 3 */
92#define	OS	30			/* immediate offset/segment */
93#define	ST	31			/* FP stack top */
94#define	STI	32			/* FP stack */
95#define	X	33			/* extended FP op */
96#define	XA	34			/* for 'fstcw %ax' */
97
98struct inst {
99	const char *i_name;		/* name */
100	short	i_has_modrm;		/* has regmodrm byte */
101	short	i_size;			/* operand size */
102	int	i_mode;			/* addressing modes */
103	const char *i_extra;		/* pointer to extra opcode table */
104};
105
106#define	op1(x)		(x)
107#define	op2(x,y)	((x)|((y)<<8))
108#define	op3(x,y,z)	((x)|((y)<<8)|((z)<<16))
109
110struct finst {
111	const char *f_name;		/* name for memory instruction */
112	int	f_size;			/* size for memory instruction */
113	int	f_rrmode;		/* mode for rr instruction */
114	const char *f_rrname;		/* name for rr instruction
115					   (or pointer to table) */
116};
117
118const char * const db_Grp6[] = {
119	"sldt",
120	"str",
121	"lldt",
122	"ltr",
123	"verr",
124	"verw",
125	"",
126	""
127};
128
129const char * const db_Grp7[] = {
130	"sgdt",
131	"sidt",
132	"lgdt",
133	"lidt",
134	"smsw",
135	"",
136	"lmsw",
137	"invlpg"
138};
139
140const char * const db_Grp8[] = {
141	"",
142	"",
143	"",
144	"",
145	"bt",
146	"bts",
147	"btr",
148	"btc"
149};
150
151const char * const db_Grp9[] = {
152	"",
153	"cmpxchg8b",
154	"",
155	"",
156	"",
157	"",
158	"",
159	"",
160};
161
162const struct inst db_inst_0f0x[] = {
163/*00*/	{ "",	   true,  NONE,  op1(Ew),     (const char *)db_Grp6 },
164/*01*/	{ "",	   true,  NONE,  op1(Ew),     (const char *)db_Grp7 },
165/*02*/	{ "lar",   true,  LONG,  op2(E,R),    0 },
166/*03*/	{ "lsl",   true,  LONG,  op2(E,R),    0 },
167/*04*/	{ "",      false, NONE,  0,	      0 },
168/*05*/	{ "",      false, NONE,  0,	      0 },
169/*06*/	{ "clts",  false, NONE,  0,	      0 },
170/*07*/	{ "",      false, NONE,  0,	      0 },
171
172/*08*/	{ "invd",  false, NONE,  0,	      0 },
173/*09*/	{ "wbinvd",false, NONE,  0,	      0 },
174/*0a*/	{ "",      false, NONE,  0,	      0 },
175/*0b*/	{ "",      false, NONE,  0,	      0 },
176/*0c*/	{ "",      false, NONE,  0,	      0 },
177/*0d*/	{ "",      false, NONE,  0,	      0 },
178/*0e*/	{ "",      false, NONE,  0,	      0 },
179/*0f*/	{ "",      false, NONE,  0,	      0 },
180};
181
182const struct inst	db_inst_0f2x[] = {
183/*20*/	{ "mov",   true,  LONG,  op2(CR,E),   0 }, /* use E for reg */
184/*21*/	{ "mov",   true,  LONG,  op2(DR,E),   0 }, /* since mod == 11 */
185/*22*/	{ "mov",   true,  LONG,  op2(E,CR),   0 },
186/*23*/	{ "mov",   true,  LONG,  op2(E,DR),   0 },
187/*24*/	{ "mov",   true,  LONG,  op2(TR,E),   0 },
188/*25*/	{ "",      false, NONE,  0,	      0 },
189/*26*/	{ "mov",   true,  LONG,  op2(E,TR),   0 },
190/*27*/	{ "",      false, NONE,  0,	      0 },
191
192/*28*/	{ "",      false, NONE,  0,	      0 },
193/*29*/	{ "",      false, NONE,  0,	      0 },
194/*2a*/	{ "",      false, NONE,  0,	      0 },
195/*2b*/	{ "",      false, NONE,  0,	      0 },
196/*2c*/	{ "",      false, NONE,  0,	      0 },
197/*2d*/	{ "",      false, NONE,  0,	      0 },
198/*2e*/	{ "",      false, NONE,  0,	      0 },
199/*2f*/	{ "",      false, NONE,  0,	      0 },
200};
201
202const struct inst	db_inst_0f3x[] = {
203/*30*/	{ "wrmsr", false, NONE,  0,	      0 },
204/*31*/	{ "rdtsc", false, NONE,  0,	      0 },
205/*32*/	{ "rdmsr", false, NONE,  0,	      0 },
206/*33*/	{ "rdpmc", false, NONE,  0,	      0 },
207/*34*/	{ "",	   false, NONE,  0,	      0 },
208/*35*/	{ "",	   false, NONE,  0,	      0 },
209/*36*/	{ "",	   false, NONE,  0,	      0 },
210/*37*/	{ "",	   false, NONE,  0,	      0 },
211
212/*38*/	{ "",	   false, NONE,  0,	      0 },
213/*39*/	{ "",	   false, NONE,  0,	      0 },
214/*3a*/	{ "",	   false, NONE,  0,	      0 },
215/*3v*/	{ "",	   false, NONE,  0,	      0 },
216/*3c*/	{ "",	   false, NONE,  0,	      0 },
217/*3d*/	{ "",	   false, NONE,  0,	      0 },
218/*3e*/	{ "",	   false, NONE,  0,	      0 },
219/*3f*/	{ "",	   false, NONE,  0,	      0 },
220};
221
222const struct inst	db_inst_0f4x[] = {
223/*40*/	{ "cmovo",  true,  LONG,  op2(E,R),    0 },
224/*41*/	{ "cmovno", true,  LONG,  op2(E,R),    0 },
225/*42*/	{ "cmovc",  true,  LONG,  op2(E,R),    0 },
226/*43*/	{ "cmovnc", true,  LONG,  op2(E,R),    0 },
227/*44*/	{ "cmovz",  true,  LONG,  op2(E,R),    0 },
228/*45*/	{ "cmovnz", true,  LONG,  op2(E,R),    0 },
229/*46*/	{ "cmovbe", true,  LONG,  op2(E,R),    0 },
230/*47*/	{ "cmovmbe",true,  LONG,  op2(E,R),    0 },
231/*48*/	{ "cmovs",  true,  LONG,  op2(E,R),    0 },
232/*49*/	{ "cmovns", true,  LONG,  op2(E,R),    0 },
233/*4a*/	{ "cmovp",  true,  LONG,  op2(E,R),    0 },
234/*4b*/	{ "cmovnp", true,  LONG,  op2(E,R),    0 },
235/*4c*/	{ "cmovl",  true,  LONG,  op2(E,R),    0 },
236/*4d*/	{ "cmovnl", true,  LONG,  op2(E,R),    0 },
237/*4e*/	{ "cmovle", true,  LONG,  op2(E,R),    0 },
238/*4f*/	{ "cmovnle",true,  LONG,  op2(E,R),    0 },
239};
240
241const struct inst	db_inst_0f8x[] = {
242/*80*/	{ "jo",    false, NONE,  op1(Dl),     0 },
243/*81*/	{ "jno",   false, NONE,  op1(Dl),     0 },
244/*82*/	{ "jb",    false, NONE,  op1(Dl),     0 },
245/*83*/	{ "jnb",   false, NONE,  op1(Dl),     0 },
246/*84*/	{ "jz",    false, NONE,  op1(Dl),     0 },
247/*85*/	{ "jnz",   false, NONE,  op1(Dl),     0 },
248/*86*/	{ "jbe",   false, NONE,  op1(Dl),     0 },
249/*87*/	{ "jnbe",  false, NONE,  op1(Dl),     0 },
250
251/*88*/	{ "js",    false, NONE,  op1(Dl),     0 },
252/*89*/	{ "jns",   false, NONE,  op1(Dl),     0 },
253/*8a*/	{ "jp",    false, NONE,  op1(Dl),     0 },
254/*8b*/	{ "jnp",   false, NONE,  op1(Dl),     0 },
255/*8c*/	{ "jl",    false, NONE,  op1(Dl),     0 },
256/*8d*/	{ "jnl",   false, NONE,  op1(Dl),     0 },
257/*8e*/	{ "jle",   false, NONE,  op1(Dl),     0 },
258/*8f*/	{ "jnle",  false, NONE,  op1(Dl),     0 },
259};
260
261const struct inst	db_inst_0f9x[] = {
262/*90*/	{ "seto",  true,  NONE,  op1(Eb),     0 },
263/*91*/	{ "setno", true,  NONE,  op1(Eb),     0 },
264/*92*/	{ "setb",  true,  NONE,  op1(Eb),     0 },
265/*93*/	{ "setnb", true,  NONE,  op1(Eb),     0 },
266/*94*/	{ "setz",  true,  NONE,  op1(Eb),     0 },
267/*95*/	{ "setnz", true,  NONE,  op1(Eb),     0 },
268/*96*/	{ "setbe", true,  NONE,  op1(Eb),     0 },
269/*97*/	{ "setnbe",true,  NONE,  op1(Eb),     0 },
270
271/*98*/	{ "sets",  true,  NONE,  op1(Eb),     0 },
272/*99*/	{ "setns", true,  NONE,  op1(Eb),     0 },
273/*9a*/	{ "setp",  true,  NONE,  op1(Eb),     0 },
274/*9b*/	{ "setnp", true,  NONE,  op1(Eb),     0 },
275/*9c*/	{ "setl",  true,  NONE,  op1(Eb),     0 },
276/*9d*/	{ "setnl", true,  NONE,  op1(Eb),     0 },
277/*9e*/	{ "setle", true,  NONE,  op1(Eb),     0 },
278/*9f*/	{ "setnle",true,  NONE,  op1(Eb),     0 },
279};
280
281const struct inst	db_inst_0fax[] = {
282/*a0*/	{ "push",  false, NONE,  op1(Si),     0 },
283/*a1*/	{ "pop",   false, NONE,  op1(Si),     0 },
284/*a2*/	{ "cpuid", false, NONE,  0,	      0 },
285/*a3*/	{ "bt",    true,  LONG,  op2(R,E),    0 },
286/*a4*/	{ "shld",  true,  LONG,  op3(Ib,E,R), 0 },
287/*a5*/	{ "shld",  true,  LONG,  op3(CL,E,R), 0 },
288/*a6*/	{ "",      false, NONE,  0,	      0 },
289/*a7*/	{ "",      false, NONE,  0,	      0 },
290
291/*a8*/	{ "push",  false, NONE,  op1(Si),     0 },
292/*a9*/	{ "pop",   false, NONE,  op1(Si),     0 },
293/*aa*/	{ "rsm",   false, NONE,  0,	      0 },
294/*ab*/	{ "bts",   true,  LONG,  op2(R,E),    0 },
295/*ac*/	{ "shrd",  true,  LONG,  op3(Ib,E,R), 0 },
296/*ad*/	{ "shrd",  true,  LONG,  op3(CL,E,R), 0 },
297/*ae*/	{ "fxsave",true,  LONG,  0,	      0 },
298/*af*/	{ "imul",  true,  LONG,  op2(E,R),    0 },
299};
300
301const struct inst	db_inst_0fbx[] = {
302/*b0*/	{ "cmpxchg",true, BYTE,	 op2(R, E),   0 },
303/*b1*/	{ "cmpxchg",true, LONG,	 op2(R, E),   0 },
304/*b2*/	{ "lss",   true,  LONG,  op2(E, R),   0 },
305/*b3*/	{ "btr",   true,  LONG,  op2(R, E),   0 },
306/*b4*/	{ "lfs",   true,  LONG,  op2(E, R),   0 },
307/*b5*/	{ "lgs",   true,  LONG,  op2(E, R),   0 },
308/*b6*/	{ "movzb", true,  LONG,  op2(E, R),   0 },
309/*b7*/	{ "movzw", true,  LONG,  op2(E, R),   0 },
310
311/*b8*/	{ "",      false, NONE,  0,	      0 },
312/*b9*/	{ "",      false, NONE,  0,	      0 },
313/*ba*/	{ "",      true,  LONG,  op2(Ib, E),  (const char *)db_Grp8 },
314/*bb*/	{ "btc",   true,  LONG,  op2(R, E),   0 },
315/*bc*/	{ "bsf",   true,  LONG,  op2(E, R),   0 },
316/*bd*/	{ "bsr",   true,  LONG,  op2(E, R),   0 },
317/*be*/	{ "movsb", true,  LONG,  op2(E, R),   0 },
318/*bf*/	{ "movsw", true,  LONG,  op2(E, R),   0 },
319};
320
321const struct inst	db_inst_0fcx[] = {
322/*c0*/	{ "xadd",  true,  BYTE,	 op2(R, E),   0 },
323/*c1*/	{ "xadd",  true,  LONG,	 op2(R, E),   0 },
324/*c2*/	{ "",	   false, NONE,	 0,	      0 },
325/*c3*/	{ "",	   false, NONE,	 0,	      0 },
326/*c4*/	{ "",	   false, NONE,	 0,	      0 },
327/*c5*/	{ "",	   false, NONE,	 0,	      0 },
328/*c6*/	{ "",	   false, NONE,	 0,	      0 },
329/*c7*/	{ "",	   true,  NONE,	 op1(E),      (const char *)db_Grp9 },
330/*c8*/	{ "bswap", false, LONG,  op1(Ri),     0 },
331/*c9*/	{ "bswap", false, LONG,  op1(Ri),     0 },
332/*ca*/	{ "bswap", false, LONG,  op1(Ri),     0 },
333/*cb*/	{ "bswap", false, LONG,  op1(Ri),     0 },
334/*cc*/	{ "bswap", false, LONG,  op1(Ri),     0 },
335/*cd*/	{ "bswap", false, LONG,  op1(Ri),     0 },
336/*ce*/	{ "bswap", false, LONG,  op1(Ri),     0 },
337/*cf*/	{ "bswap", false, LONG,  op1(Ri),     0 },
338};
339
340const struct inst * const db_inst_0f[] = {
341	db_inst_0f0x,
342	0,
343	db_inst_0f2x,
344	db_inst_0f3x,
345	db_inst_0f4x,
346	0,
347	0,
348	0,
349	db_inst_0f8x,
350	db_inst_0f9x,
351	db_inst_0fax,
352	db_inst_0fbx,
353	db_inst_0fcx,
354	0,
355	0,
356	0
357};
358
359const char * const db_Esc92[] = {
360	"fnop",	"",	"",	"",	"",	"",	"",	""
361};
362const char * const db_Esc93[] = {
363	"",	"",	"",	"",	"",	"",	"",	""
364};
365const char * const db_Esc94[] = {
366	"fchs",	"fabs",	"",	"",	"ftst",	"fxam",	"",	""
367};
368const char * const db_Esc95[] = {
369	"fld1",	"fldl2t","fldl2e","fldpi","fldlg2","fldln2","fldz",""
370};
371const char * const db_Esc96[] = {
372	"f2xm1","fyl2x","fptan","fpatan","fxtract","fprem1","fdecstp",
373	"fincstp"
374};
375const char * const db_Esc97[] = {
376	"fprem","fyl2xp1","fsqrt","fsincos","frndint","fscale","fsin","fcos"
377};
378
379const char * const db_Esca4[] = {
380	"",	"fucompp","",	"",	"",	"",	"",	""
381};
382
383const char * const db_Escb4[] = {
384	"",	"",	"fnclex","fninit","",	"",	"",	""
385};
386
387const char * const db_Esce3[] = {
388	"",	"fcompp","",	"",	"",	"",	"",	""
389};
390
391const char * const db_Escf4[] = {
392	"fnstsw","",	"",	"",	"",	"",	"",	""
393};
394
395const struct finst db_Esc8[] = {
396/*0*/	{ "fadd",   SNGL,  op2(STI,ST),	0 },
397/*1*/	{ "fmul",   SNGL,  op2(STI,ST),	0 },
398/*2*/	{ "fcom",   SNGL,  op2(STI,ST),	0 },
399/*3*/	{ "fcomp",  SNGL,  op2(STI,ST),	0 },
400/*4*/	{ "fsub",   SNGL,  op2(STI,ST),	0 },
401/*5*/	{ "fsubr",  SNGL,  op2(STI,ST),	0 },
402/*6*/	{ "fdiv",   SNGL,  op2(STI,ST),	0 },
403/*7*/	{ "fdivr",  SNGL,  op2(STI,ST),	0 },
404};
405
406const struct finst db_Esc9[] = {
407/*0*/	{ "fld",    SNGL,  op1(STI),	0 },
408/*1*/	{ "",       NONE,  op1(STI),	"fxch" },
409/*2*/	{ "fst",    SNGL,  op1(X),	(const char *)db_Esc92 },
410/*3*/	{ "fstp",   SNGL,  op1(X),	(const char *)db_Esc93 },
411/*4*/	{ "fldenv", NONE,  op1(X),	(const char *)db_Esc94 },
412/*5*/	{ "fldcw",  NONE,  op1(X),	(const char *)db_Esc95 },
413/*6*/	{ "fnstenv",NONE,  op1(X),	(const char *)db_Esc96 },
414/*7*/	{ "fnstcw", NONE,  op1(X),	(const char *)db_Esc97 },
415};
416
417const struct finst db_Esca[] = {
418/*0*/	{ "fiadd",  WORD,  0,		0 },
419/*1*/	{ "fimul",  WORD,  0,		0 },
420/*2*/	{ "ficom",  WORD,  0,		0 },
421/*3*/	{ "ficomp", WORD,  0,		0 },
422/*4*/	{ "fisub",  WORD,  op1(X),	(const char *)db_Esca4 },
423/*5*/	{ "fisubr", WORD,  0,		0 },
424/*6*/	{ "fidiv",  WORD,  0,		0 },
425/*7*/	{ "fidivr", WORD,  0,		0 }
426};
427
428const struct finst db_Escb[] = {
429/*0*/	{ "fild",   WORD,  0,		0 },
430/*1*/	{ "",       NONE,  0,		0 },
431/*2*/	{ "fist",   WORD,  0,		0 },
432/*3*/	{ "fistp",  WORD,  0,		0 },
433/*4*/	{ "",       WORD,  op1(X),	(const char *)db_Escb4 },
434/*5*/	{ "fld",    EXTR,  0,		0 },
435/*6*/	{ "",       WORD,  0,		0 },
436/*7*/	{ "fstp",   EXTR,  0,		0 },
437};
438
439const struct finst db_Escc[] = {
440/*0*/	{ "fadd",   DBLR,  op2(ST,STI),	0 },
441/*1*/	{ "fmul",   DBLR,  op2(ST,STI),	0 },
442/*2*/	{ "fcom",   DBLR,  op2(ST,STI),	0 },
443/*3*/	{ "fcomp",  DBLR,  op2(ST,STI),	0 },
444/*4*/	{ "fsub",   DBLR,  op2(ST,STI),	"fsubr" },
445/*5*/	{ "fsubr",  DBLR,  op2(ST,STI),	"fsub" },
446/*6*/	{ "fdiv",   DBLR,  op2(ST,STI),	"fdivr" },
447/*7*/	{ "fdivr",  DBLR,  op2(ST,STI),	"fdiv" },
448};
449
450const struct finst db_Escd[] = {
451/*0*/	{ "fld",    DBLR,  op1(STI),	"ffree" },
452/*1*/	{ "",       NONE,  0,		0 },
453/*2*/	{ "fst",    DBLR,  op1(STI),	0 },
454/*3*/	{ "fstp",   DBLR,  op1(STI),	0 },
455/*4*/	{ "frstor", NONE,  op1(STI),	"fucom" },
456/*5*/	{ "",       NONE,  op1(STI),	"fucomp" },
457/*6*/	{ "fnsave", NONE,  0,		0 },
458/*7*/	{ "fnstsw", NONE,  0,		0 },
459};
460
461const struct finst db_Esce[] = {
462/*0*/	{ "fiadd",  LONG,  op2(ST,STI),	"faddp" },
463/*1*/	{ "fimul",  LONG,  op2(ST,STI),	"fmulp" },
464/*2*/	{ "ficom",  LONG,  0,		0 },
465/*3*/	{ "ficomp", LONG,  op1(X),	(const char *)db_Esce3 },
466/*4*/	{ "fisub",  LONG,  op2(ST,STI),	"fsubrp" },
467/*5*/	{ "fisubr", LONG,  op2(ST,STI),	"fsubp" },
468/*6*/	{ "fidiv",  LONG,  op2(ST,STI),	"fdivrp" },
469/*7*/	{ "fidivr", LONG,  op2(ST,STI),	"fdivp" },
470};
471
472const struct finst db_Escf[] = {
473/*0*/	{ "fild",   LONG,  0,		0 },
474/*1*/	{ "",       LONG,  0,		0 },
475/*2*/	{ "fist",   LONG,  0,		0 },
476/*3*/	{ "fistp",  LONG,  0,		0 },
477/*4*/	{ "fbld",   NONE,  op1(XA),	(const char *)db_Escf4 },
478/*5*/	{ "fld",    QUAD,  0,		0 },
479/*6*/	{ "fbstp",  NONE,  0,		0 },
480/*7*/	{ "fstp",   QUAD,  0,		0 },
481};
482
483const struct finst * const db_Esc_inst[] = {
484	db_Esc8, db_Esc9, db_Esca, db_Escb,
485	db_Escc, db_Escd, db_Esce, db_Escf
486};
487
488const char * const db_Grp1[] = {
489	"add",
490	"or",
491	"adc",
492	"sbb",
493	"and",
494	"sub",
495	"xor",
496	"cmp"
497};
498
499const char * const db_Grp2[] = {
500	"rol",
501	"ror",
502	"rcl",
503	"rcr",
504	"shl",
505	"shr",
506	"shl",
507	"sar"
508};
509
510const struct inst db_Grp3[] = {
511	{ "test",  true, NONE, op2(I,E), 0 },
512	{ "test",  true, NONE, op2(I,E), 0 },
513	{ "not",   true, NONE, op1(E),   0 },
514	{ "neg",   true, NONE, op1(E),   0 },
515	{ "mul",   true, NONE, op2(E,A), 0 },
516	{ "imul",  true, NONE, op2(E,A), 0 },
517	{ "div",   true, NONE, op2(E,A), 0 },
518	{ "idiv",  true, NONE, op2(E,A), 0 },
519};
520
521const struct inst	db_Grp4[] = {
522	{ "inc",   true, BYTE, op1(E),   0 },
523	{ "dec",   true, BYTE, op1(E),   0 },
524	{ "",      true, NONE, 0,	 0 },
525	{ "",      true, NONE, 0,	 0 },
526	{ "",      true, NONE, 0,	 0 },
527	{ "",      true, NONE, 0,	 0 },
528	{ "",      true, NONE, 0,	 0 },
529	{ "",      true, NONE, 0,	 0 }
530};
531
532const struct inst	db_Grp5[] = {
533	{ "inc",   true, LONG, op1(E),   0 },
534	{ "dec",   true, LONG, op1(E),   0 },
535	{ "call",  true, NONE, op1(Eind),0 },
536	{ "lcall", true, NONE, op1(Eind),0 },
537	{ "jmp",   true, NONE, op1(Eind),0 },
538	{ "ljmp",  true, NONE, op1(Eind),0 },
539	{ "push",  true, LONG, op1(E),   0 },
540	{ "",      true, NONE, 0,	 0 }
541};
542
543const struct inst db_inst_table[256] = {
544/*00*/	{ "add",   true,  BYTE,  op2(R, E),  0 },
545/*01*/	{ "add",   true,  LONG,  op2(R, E),  0 },
546/*02*/	{ "add",   true,  BYTE,  op2(E, R),  0 },
547/*03*/	{ "add",   true,  LONG,  op2(E, R),  0 },
548/*04*/	{ "add",   false, BYTE,  op2(Is, A), 0 },
549/*05*/	{ "add",   false, LONG,  op2(Is, A), 0 },
550/*06*/	{ "push",  false, NONE,  op1(Si),    0 },
551/*07*/	{ "pop",   false, NONE,  op1(Si),    0 },
552
553/*08*/	{ "or",    true,  BYTE,  op2(R, E),  0 },
554/*09*/	{ "or",    true,  LONG,  op2(R, E),  0 },
555/*0a*/	{ "or",    true,  BYTE,  op2(E, R),  0 },
556/*0b*/	{ "or",    true,  LONG,  op2(E, R),  0 },
557/*0c*/	{ "or",    false, BYTE,  op2(I, A),  0 },
558/*0d*/	{ "or",    false, LONG,  op2(I, A),  0 },
559/*0e*/	{ "push",  false, NONE,  op1(Si),    0 },
560/*0f*/	{ "",      false, NONE,  0,	     0 },
561
562/*10*/	{ "adc",   true,  BYTE,  op2(R, E),  0 },
563/*11*/	{ "adc",   true,  LONG,  op2(R, E),  0 },
564/*12*/	{ "adc",   true,  BYTE,  op2(E, R),  0 },
565/*13*/	{ "adc",   true,  LONG,  op2(E, R),  0 },
566/*14*/	{ "adc",   false, BYTE,  op2(Is, A), 0 },
567/*15*/	{ "adc",   false, LONG,  op2(Is, A), 0 },
568/*16*/	{ "push",  false, NONE,  op1(Si),    0 },
569/*17*/	{ "pop",   false, NONE,  op1(Si),    0 },
570
571/*18*/	{ "sbb",   true,  BYTE,  op2(R, E),  0 },
572/*19*/	{ "sbb",   true,  LONG,  op2(R, E),  0 },
573/*1a*/	{ "sbb",   true,  BYTE,  op2(E, R),  0 },
574/*1b*/	{ "sbb",   true,  LONG,  op2(E, R),  0 },
575/*1c*/	{ "sbb",   false, BYTE,  op2(Is, A), 0 },
576/*1d*/	{ "sbb",   false, LONG,  op2(Is, A), 0 },
577/*1e*/	{ "push",  false, NONE,  op1(Si),    0 },
578/*1f*/	{ "pop",   false, NONE,  op1(Si),    0 },
579
580/*20*/	{ "and",   true,  BYTE,  op2(R, E),  0 },
581/*21*/	{ "and",   true,  LONG,  op2(R, E),  0 },
582/*22*/	{ "and",   true,  BYTE,  op2(E, R),  0 },
583/*23*/	{ "and",   true,  LONG,  op2(E, R),  0 },
584/*24*/	{ "and",   false, BYTE,  op2(I, A),  0 },
585/*25*/	{ "and",   false, LONG,  op2(I, A),  0 },
586/*26*/	{ "",      false, NONE,  0,	     0 },
587/*27*/	{ "aaa",   false, NONE,  0,	     0 },
588
589/*28*/	{ "sub",   true,  BYTE,  op2(R, E),  0 },
590/*29*/	{ "sub",   true,  LONG,  op2(R, E),  0 },
591/*2a*/	{ "sub",   true,  BYTE,  op2(E, R),  0 },
592/*2b*/	{ "sub",   true,  LONG,  op2(E, R),  0 },
593/*2c*/	{ "sub",   false, BYTE,  op2(Is, A), 0 },
594/*2d*/	{ "sub",   false, LONG,  op2(Is, A), 0 },
595/*2e*/	{ "",      false, NONE,  0,	     0 },
596/*2f*/	{ "das",   false, NONE,  0,	     0 },
597
598/*30*/	{ "xor",   true,  BYTE,  op2(R, E),  0 },
599/*31*/	{ "xor",   true,  LONG,  op2(R, E),  0 },
600/*32*/	{ "xor",   true,  BYTE,  op2(E, R),  0 },
601/*33*/	{ "xor",   true,  LONG,  op2(E, R),  0 },
602/*34*/	{ "xor",   false, BYTE,  op2(I, A),  0 },
603/*35*/	{ "xor",   false, LONG,  op2(I, A),  0 },
604/*36*/	{ "",      false, NONE,  0,	     0 },
605/*37*/	{ "daa",   false, NONE,  0,	     0 },
606
607/*38*/	{ "cmp",   true,  BYTE,  op2(R, E),  0 },
608/*39*/	{ "cmp",   true,  LONG,  op2(R, E),  0 },
609/*3a*/	{ "cmp",   true,  BYTE,  op2(E, R),  0 },
610/*3b*/	{ "cmp",   true,  LONG,  op2(E, R),  0 },
611/*3c*/	{ "cmp",   false, BYTE,  op2(Is, A), 0 },
612/*3d*/	{ "cmp",   false, LONG,  op2(Is, A), 0 },
613/*3e*/	{ "",      false, NONE,  0,	     0 },
614/*3f*/	{ "aas",   false, NONE,  0,	     0 },
615
616/*40*/	{ "inc",   false, LONG,  op1(Ri),    0 },
617/*41*/	{ "inc",   false, LONG,  op1(Ri),    0 },
618/*42*/	{ "inc",   false, LONG,  op1(Ri),    0 },
619/*43*/	{ "inc",   false, LONG,  op1(Ri),    0 },
620/*44*/	{ "inc",   false, LONG,  op1(Ri),    0 },
621/*45*/	{ "inc",   false, LONG,  op1(Ri),    0 },
622/*46*/	{ "inc",   false, LONG,  op1(Ri),    0 },
623/*47*/	{ "inc",   false, LONG,  op1(Ri),    0 },
624
625/*48*/	{ "dec",   false, LONG,  op1(Ri),    0 },
626/*49*/	{ "dec",   false, LONG,  op1(Ri),    0 },
627/*4a*/	{ "dec",   false, LONG,  op1(Ri),    0 },
628/*4b*/	{ "dec",   false, LONG,  op1(Ri),    0 },
629/*4c*/	{ "dec",   false, LONG,  op1(Ri),    0 },
630/*4d*/	{ "dec",   false, LONG,  op1(Ri),    0 },
631/*4e*/	{ "dec",   false, LONG,  op1(Ri),    0 },
632/*4f*/	{ "dec",   false, LONG,  op1(Ri),    0 },
633
634/*50*/	{ "push",  false, LONG,  op1(Ri),    0 },
635/*51*/	{ "push",  false, LONG,  op1(Ri),    0 },
636/*52*/	{ "push",  false, LONG,  op1(Ri),    0 },
637/*53*/	{ "push",  false, LONG,  op1(Ri),    0 },
638/*54*/	{ "push",  false, LONG,  op1(Ri),    0 },
639/*55*/	{ "push",  false, LONG,  op1(Ri),    0 },
640/*56*/	{ "push",  false, LONG,  op1(Ri),    0 },
641/*57*/	{ "push",  false, LONG,  op1(Ri),    0 },
642
643/*58*/	{ "pop",   false, LONG,  op1(Ri),    0 },
644/*59*/	{ "pop",   false, LONG,  op1(Ri),    0 },
645/*5a*/	{ "pop",   false, LONG,  op1(Ri),    0 },
646/*5b*/	{ "pop",   false, LONG,  op1(Ri),    0 },
647/*5c*/	{ "pop",   false, LONG,  op1(Ri),    0 },
648/*5d*/	{ "pop",   false, LONG,  op1(Ri),    0 },
649/*5e*/	{ "pop",   false, LONG,  op1(Ri),    0 },
650/*5f*/	{ "pop",   false, LONG,  op1(Ri),    0 },
651
652/*60*/	{ "pusha", false, LONG,  0,	     0 },
653/*61*/	{ "popa",  false, LONG,  0,	     0 },
654/*62*/  { "bound", true,  LONG,  op2(E, R),  0 },
655/*63*/	{ "arpl",  true,  NONE,  op2(Ew,Rw), 0 },
656
657/*64*/	{ "",      false, NONE,  0,	     0 },
658/*65*/	{ "",      false, NONE,  0,	     0 },
659/*66*/	{ "",      false, NONE,  0,	     0 },
660/*67*/	{ "",      false, NONE,  0,	     0 },
661
662/*68*/	{ "push",  false, LONG,  op1(I),     0 },
663/*69*/  { "imul",  true,  LONG,  op3(I,E,R), 0 },
664/*6a*/	{ "push",  false, LONG,  op1(Ib),    0 },
665/*6b*/  { "imul",  true,  LONG,  op3(Ibs,E,R),0 },
666/*6c*/	{ "ins",   false, BYTE,  op2(DX, DI), 0 },
667/*6d*/	{ "ins",   false, LONG,  op2(DX, DI), 0 },
668/*6e*/	{ "outs",  false, BYTE,  op2(SI, DX), 0 },
669/*6f*/	{ "outs",  false, LONG,  op2(SI, DX), 0 },
670
671/*70*/	{ "jo",    false, NONE,  op1(Db),     0 },
672/*71*/	{ "jno",   false, NONE,  op1(Db),     0 },
673/*72*/	{ "jb",    false, NONE,  op1(Db),     0 },
674/*73*/	{ "jnb",   false, NONE,  op1(Db),     0 },
675/*74*/	{ "jz",    false, NONE,  op1(Db),     0 },
676/*75*/	{ "jnz",   false, NONE,  op1(Db),     0 },
677/*76*/	{ "jbe",   false, NONE,  op1(Db),     0 },
678/*77*/	{ "jnbe",  false, NONE,  op1(Db),     0 },
679
680/*78*/	{ "js",    false, NONE,  op1(Db),     0 },
681/*79*/	{ "jns",   false, NONE,  op1(Db),     0 },
682/*7a*/	{ "jp",    false, NONE,  op1(Db),     0 },
683/*7b*/	{ "jnp",   false, NONE,  op1(Db),     0 },
684/*7c*/	{ "jl",    false, NONE,  op1(Db),     0 },
685/*7d*/	{ "jnl",   false, NONE,  op1(Db),     0 },
686/*7e*/	{ "jle",   false, NONE,  op1(Db),     0 },
687/*7f*/	{ "jnle",  false, NONE,  op1(Db),     0 },
688
689/*80*/  { "",	   true,  BYTE,  op2(I, E),   (const char *)db_Grp1 },
690/*81*/  { "",	   true,  LONG,  op2(I, E),   (const char *)db_Grp1 },
691/*82*/  { "",	   true,  BYTE,  op2(Is,E),   (const char *)db_Grp1 },
692/*83*/  { "",	   true,  LONG,  op2(Ibs,E),  (const char *)db_Grp1 },
693/*84*/	{ "test",  true,  BYTE,  op2(R, E),   0 },
694/*85*/	{ "test",  true,  LONG,  op2(R, E),   0 },
695/*86*/	{ "xchg",  true,  BYTE,  op2(R, E),   0 },
696/*87*/	{ "xchg",  true,  LONG,  op2(R, E),   0 },
697
698/*88*/	{ "mov",   true,  BYTE,  op2(R, E),   0 },
699/*89*/	{ "mov",   true,  LONG,  op2(R, E),   0 },
700/*8a*/	{ "mov",   true,  BYTE,  op2(E, R),   0 },
701/*8b*/	{ "mov",   true,  LONG,  op2(E, R),   0 },
702/*8c*/  { "mov",   true,  NONE,  op2(S, Ew),  0 },
703/*8d*/	{ "lea",   true,  LONG,  op2(E, R),   0 },
704/*8e*/	{ "mov",   true,  NONE,  op2(Ew, S),  0 },
705/*8f*/	{ "pop",   true,  LONG,  op1(E),      0 },
706
707/*90*/	{ "nop",   false, NONE,  0,	      0 },
708/*91*/	{ "xchg",  false, LONG,  op2(A, Ri),  0 },
709/*92*/	{ "xchg",  false, LONG,  op2(A, Ri),  0 },
710/*93*/	{ "xchg",  false, LONG,  op2(A, Ri),  0 },
711/*94*/	{ "xchg",  false, LONG,  op2(A, Ri),  0 },
712/*95*/	{ "xchg",  false, LONG,  op2(A, Ri),  0 },
713/*96*/	{ "xchg",  false, LONG,  op2(A, Ri),  0 },
714/*97*/	{ "xchg",  false, LONG,  op2(A, Ri),  0 },
715
716/*98*/	{ "cbw",   false, SDEP,  0,	      "cwde" },	/* cbw/cwde */
717/*99*/	{ "cwd",   false, SDEP,  0,	      "cdq"  },	/* cwd/cdq */
718/*9a*/	{ "lcall", false, NONE,  op1(OS),     0 },
719/*9b*/	{ "wait",  false, NONE,  0,	      0 },
720/*9c*/	{ "pushf", false, LONG,  0,	      0 },
721/*9d*/	{ "popf",  false, LONG,  0,	      0 },
722/*9e*/	{ "sahf",  false, NONE,  0,	      0 },
723/*9f*/	{ "lahf",  false, NONE,  0,	      0 },
724
725/*a0*/	{ "mov",   false, BYTE,  op2(O, A),   0 },
726/*a1*/	{ "mov",   false, LONG,  op2(O, A),   0 },
727/*a2*/	{ "mov",   false, BYTE,  op2(A, O),   0 },
728/*a3*/	{ "mov",   false, LONG,  op2(A, O),   0 },
729/*a4*/	{ "movs",  false, BYTE,  op2(SI,DI),  0 },
730/*a5*/	{ "movs",  false, LONG,  op2(SI,DI),  0 },
731/*a6*/	{ "cmps",  false, BYTE,  op2(SI,DI),  0 },
732/*a7*/	{ "cmps",  false, LONG,  op2(SI,DI),  0 },
733
734/*a8*/	{ "test",  false, BYTE,  op2(I, A),   0 },
735/*a9*/	{ "test",  false, LONG,  op2(I, A),   0 },
736/*aa*/	{ "stos",  false, BYTE,  op1(DI),     0 },
737/*ab*/	{ "stos",  false, LONG,  op1(DI),     0 },
738/*ac*/	{ "lods",  false, BYTE,  op1(SI),     0 },
739/*ad*/	{ "lods",  false, LONG,  op1(SI),     0 },
740/*ae*/	{ "scas",  false, BYTE,  op1(SI),     0 },
741/*af*/	{ "scas",  false, LONG,  op1(SI),     0 },
742
743/*b0*/	{ "mov",   false, BYTE,  op2(I, Ri),  0 },
744/*b1*/	{ "mov",   false, BYTE,  op2(I, Ri),  0 },
745/*b2*/	{ "mov",   false, BYTE,  op2(I, Ri),  0 },
746/*b3*/	{ "mov",   false, BYTE,  op2(I, Ri),  0 },
747/*b4*/	{ "mov",   false, BYTE,  op2(I, Ri),  0 },
748/*b5*/	{ "mov",   false, BYTE,  op2(I, Ri),  0 },
749/*b6*/	{ "mov",   false, BYTE,  op2(I, Ri),  0 },
750/*b7*/	{ "mov",   false, BYTE,  op2(I, Ri),  0 },
751
752/*b8*/	{ "mov",   false, LONG,  op2(I, Ri),  0 },
753/*b9*/	{ "mov",   false, LONG,  op2(I, Ri),  0 },
754/*ba*/	{ "mov",   false, LONG,  op2(I, Ri),  0 },
755/*bb*/	{ "mov",   false, LONG,  op2(I, Ri),  0 },
756/*bc*/	{ "mov",   false, LONG,  op2(I, Ri),  0 },
757/*bd*/	{ "mov",   false, LONG,  op2(I, Ri),  0 },
758/*be*/	{ "mov",   false, LONG,  op2(I, Ri),  0 },
759/*bf*/	{ "mov",   false, LONG,  op2(I, Ri),  0 },
760
761/*c0*/	{ "",	   true,  BYTE,  op2(Ib, E),  (const char *)db_Grp2 },
762/*c1*/	{ "",	   true,  LONG,  op2(Ib, E),  (const char *)db_Grp2 },
763/*c2*/	{ "ret",   false, NONE,  op1(Iw),     0 },
764/*c3*/	{ "ret",   false, NONE,  0,	      0 },
765/*c4*/	{ "les",   true,  LONG,  op2(E, R),   0 },
766/*c5*/	{ "lds",   true,  LONG,  op2(E, R),   0 },
767/*c6*/	{ "mov",   true,  BYTE,  op2(I, E),   0 },
768/*c7*/	{ "mov",   true,  LONG,  op2(I, E),   0 },
769
770/*c8*/	{ "enter", false, NONE,  op2(Ib, Iw), 0 },
771/*c9*/	{ "leave", false, NONE,  0,           0 },
772/*ca*/	{ "lret",  false, NONE,  op1(Iw),     0 },
773/*cb*/	{ "lret",  false, NONE,  0,	      0 },
774/*cc*/	{ "int",   false, NONE,  op1(o3),     0 },
775/*cd*/	{ "int",   false, NONE,  op1(Ib),     0 },
776/*ce*/	{ "into",  false, NONE,  0,	      0 },
777/*cf*/	{ "iret",  false, NONE,  0,	      0 },
778
779/*d0*/	{ "",	   true,  BYTE,  op2(o1, E),  (const char *)db_Grp2 },
780/*d1*/	{ "",	   true,  LONG,  op2(o1, E),  (const char *)db_Grp2 },
781/*d2*/	{ "",	   true,  BYTE,  op2(CL, E),  (const char *)db_Grp2 },
782/*d3*/	{ "",	   true,  LONG,  op2(CL, E),  (const char *)db_Grp2 },
783/*d4*/	{ "aam",   true,  NONE,  0,	      0 },
784/*d5*/	{ "aad",   true,  NONE,  0,	      0 },
785/*d6*/	{ "",      false, NONE,  0,	      0 },
786/*d7*/	{ "xlat",  false, BYTE,  op1(BX),     0 },
787
788/*d8*/  { "",      true,  NONE,  0,	      (const char *)db_Esc8 },
789/*d9*/  { "",      true,  NONE,  0,	      (const char *)db_Esc9 },
790/*da*/  { "",      true,  NONE,  0,	      (const char *)db_Esca },
791/*db*/  { "",      true,  NONE,  0,	      (const char *)db_Escb },
792/*dc*/  { "",      true,  NONE,  0,	      (const char *)db_Escc },
793/*dd*/  { "",      true,  NONE,  0,	      (const char *)db_Escd },
794/*de*/  { "",      true,  NONE,  0,	      (const char *)db_Esce },
795/*df*/  { "",      true,  NONE,  0,	      (const char *)db_Escf },
796
797/*e0*/	{ "loopne",false, NONE,  op1(Db),     0 },
798/*e1*/	{ "loope", false, NONE,  op1(Db),     0 },
799/*e2*/	{ "loop",  false, NONE,  op1(Db),     0 },
800/*e3*/	{ "jcxz",  false, SDEP,  op1(Db),     "jecxz" },
801/*e4*/	{ "in",    false, BYTE,  op2(Ib, A),  0 },
802/*e5*/	{ "in",    false, LONG,  op2(Ib, A) , 0 },
803/*e6*/	{ "out",   false, BYTE,  op2(A, Ib),  0 },
804/*e7*/	{ "out",   false, LONG,  op2(A, Ib) , 0 },
805
806/*e8*/	{ "call",  false, NONE,  op1(Dl),     0 },
807/*e9*/	{ "jmp",   false, NONE,  op1(Dl),     0 },
808/*ea*/	{ "ljmp",  false, NONE,  op1(OS),     0 },
809/*eb*/	{ "jmp",   false, NONE,  op1(Db),     0 },
810/*ec*/	{ "in",    false, BYTE,  op2(DX, A),  0 },
811/*ed*/	{ "in",    false, LONG,  op2(DX, A) , 0 },
812/*ee*/	{ "out",   false, BYTE,  op2(A, DX),  0 },
813/*ef*/	{ "out",   false, LONG,  op2(A, DX) , 0 },
814
815/*f0*/	{ "",      false, NONE,  0,	     0 },
816/*f1*/	{ "",      false, NONE,  0,	     0 },
817/*f2*/	{ "",      false, NONE,  0,	     0 },
818/*f3*/	{ "",      false, NONE,  0,	     0 },
819/*f4*/	{ "hlt",   false, NONE,  0,	     0 },
820/*f5*/	{ "cmc",   false, NONE,  0,	     0 },
821/*f6*/	{ "",      true,  BYTE,  0,	     (const char *)db_Grp3 },
822/*f7*/	{ "",	   true,  LONG,  0,	     (const char *)db_Grp3 },
823
824/*f8*/	{ "clc",   false, NONE,  0,	     0 },
825/*f9*/	{ "stc",   false, NONE,  0,	     0 },
826/*fa*/	{ "cli",   false, NONE,  0,	     0 },
827/*fb*/	{ "sti",   false, NONE,  0,	     0 },
828/*fc*/	{ "cld",   false, NONE,  0,	     0 },
829/*fd*/	{ "std",   false, NONE,  0,	     0 },
830/*fe*/	{ "",	   true,  NONE,  0,	     (const char *)db_Grp4 },
831/*ff*/	{ "",	   true,  NONE,  0,	     (const char *)db_Grp5 },
832};
833
834const struct inst	db_bad_inst =
835	{ "???",   false, NONE,  0,	      0 }
836;
837
838#define	f_mod(byte)	((byte)>>6)
839#define	f_reg(byte)	(((byte)>>3)&0x7)
840#define	f_rm(byte)	((byte)&0x7)
841
842#define	sib_ss(byte)	((byte)>>6)
843#define	sib_index(byte)	(((byte)>>3)&0x7)
844#define	sib_base(byte)	((byte)&0x7)
845
846struct i_addr {
847	int		is_reg;	/* if reg, reg number is in 'disp' */
848	int		disp;
849	const char *	base;
850	const char *	index;
851	int		ss;
852};
853
854const char * const db_index_reg_16[8] = {
855	"%bx,%si",
856	"%bx,%di",
857	"%bp,%si",
858	"%bp,%di",
859	"%si",
860	"%di",
861	"%bp",
862	"%bx"
863};
864
865const char * const db_reg[3][8] = {
866	{ "%al",  "%cl",  "%dl",  "%bl",  "%ah",  "%ch",  "%dh",  "%bh" },
867	{ "%ax",  "%cx",  "%dx",  "%bx",  "%sp",  "%bp",  "%si",  "%di" },
868	{ "%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi" }
869};
870
871const char * const db_seg_reg[8] = {
872	"%es", "%cs", "%ss", "%ds", "%fs", "%gs", "", ""
873};
874
875/*
876 * lengths for size attributes
877 */
878const int db_lengths[] = {
879	1,	/* BYTE */
880	2,	/* WORD */
881	4,	/* LONG */
882	8,	/* QUAD */
883	4,	/* SNGL */
884	8,	/* DBLR */
885	10,	/* EXTR */
886};
887
888#define	get_value_inc(result, loc, size, is_signed) \
889	do { \
890		result = db_get_value((loc), (size), (is_signed)); \
891		(loc) += (size); \
892	} while (0)
893
894
895db_addr_t db_read_address(db_addr_t, int, int, struct i_addr *);
896void db_print_address(const char *, int, struct i_addr *);
897db_addr_t db_disasm_esc(db_addr_t, int, int, int, const char *);
898
899/*
900 * Read address at location and return updated location.
901 */
902db_addr_t
903db_read_address(db_addr_t loc, int short_addr, int regmodrm, struct i_addr *addrp)
904	/* addrp:		 out */
905{
906	int		mod, rm, sib, index, disp;
907
908	mod = f_mod(regmodrm);
909	rm  = f_rm(regmodrm);
910
911	if (mod == 3) {
912		addrp->is_reg = true;
913		addrp->disp = rm;
914		return (loc);
915	}
916	addrp->is_reg = false;
917	addrp->index = 0;
918
919	if (short_addr) {
920		addrp->index = 0;
921		addrp->ss = 0;
922		switch (mod) {
923		    case 0:
924			if (rm == 6) {
925				get_value_inc(disp, loc, 2, true);
926				addrp->disp = disp;
927				addrp->base = 0;
928			} else {
929				addrp->disp = 0;
930				addrp->base = db_index_reg_16[rm];
931			}
932			break;
933		    case 1:
934			get_value_inc(disp, loc, 1, true);
935			addrp->disp = disp;
936			addrp->base = db_index_reg_16[rm];
937			break;
938		    case 2:
939			get_value_inc(disp, loc, 2, true);
940			addrp->disp = disp;
941			addrp->base = db_index_reg_16[rm];
942			break;
943		}
944	} else {
945		if (mod != 3 && rm == 4) {
946			get_value_inc(sib, loc, 1, false);
947			rm = sib_base(sib);
948			index = sib_index(sib);
949			if (index != 4)
950				addrp->index = db_reg[LONG][index];
951			addrp->ss = sib_ss(sib);
952		}
953
954		switch (mod) {
955		    case 0:
956			if (rm == 5) {
957				get_value_inc(addrp->disp, loc, 4, false);
958				addrp->base = 0;
959			} else {
960				addrp->disp = 0;
961				addrp->base = db_reg[LONG][rm];
962			}
963			break;
964		    case 1:
965			get_value_inc(disp, loc, 1, true);
966			addrp->disp = disp;
967			addrp->base = db_reg[LONG][rm];
968			break;
969		    case 2:
970			get_value_inc(disp, loc, 4, false);
971			addrp->disp = disp;
972			addrp->base = db_reg[LONG][rm];
973			break;
974		}
975	}
976	return (loc);
977}
978
979void
980db_print_address(const char * seg, int size, struct i_addr *addrp)
981{
982	if (addrp->is_reg) {
983		db_printf("%s", db_reg[size][addrp->disp]);
984		return;
985	}
986
987	if (seg)
988		db_printf("%s:", seg);
989
990	db_printsym((db_addr_t)addrp->disp, DB_STGY_ANY, db_printf);
991	if (addrp->base != 0 || addrp->index != 0) {
992		db_printf("(");
993		if (addrp->base)
994			db_printf("%s", addrp->base);
995		if (addrp->index)
996			db_printf(",%s,%d", addrp->index, 1<<addrp->ss);
997		db_printf(")");
998	}
999}
1000
1001/*
1002 * Disassemble floating-point ("escape") instruction
1003 * and return updated location.
1004 */
1005db_addr_t
1006db_disasm_esc(
1007    db_addr_t	loc,
1008    int		inst,
1009    int		short_addr,
1010    int		size,
1011    const char *seg)
1012{
1013	int		regmodrm;
1014	const struct finst	*fp;
1015	int		mod;
1016	struct i_addr	address;
1017	const char *	name;
1018
1019	get_value_inc(regmodrm, loc, 1, false);
1020	fp = &db_Esc_inst[inst - 0xd8][f_reg(regmodrm)];
1021	mod = f_mod(regmodrm);
1022	if (mod != 3) {
1023		/*
1024		 * Normal address modes.
1025		 */
1026		loc = db_read_address(loc, short_addr, regmodrm, &address);
1027		db_printf("%s", fp->f_name);
1028		switch(fp->f_size) {
1029		    case SNGL:
1030			db_printf("s");
1031			break;
1032		    case DBLR:
1033			db_printf("l");
1034			break;
1035		    case EXTR:
1036			db_printf("t");
1037			break;
1038		    case WORD:
1039			db_printf("s");
1040			break;
1041		    case LONG:
1042			db_printf("l");
1043			break;
1044		    case QUAD:
1045			db_printf("q");
1046			break;
1047		    default:
1048			break;
1049		}
1050		db_printf("\t");
1051		db_print_address(seg, BYTE, &address);
1052	} else {
1053		/*
1054		 * 'reg-reg' - special formats
1055		 */
1056		switch (fp->f_rrmode) {
1057		    case op2(ST,STI):
1058			name = (fp->f_rrname) ? fp->f_rrname : fp->f_name;
1059			db_printf("%s\t%%st,%%st(%d)",name,f_rm(regmodrm));
1060			break;
1061		    case op2(STI,ST):
1062			name = (fp->f_rrname) ? fp->f_rrname : fp->f_name;
1063			db_printf("%s\t%%st(%d),%%st",name, f_rm(regmodrm));
1064			break;
1065		    case op1(STI):
1066			name = (fp->f_rrname) ? fp->f_rrname : fp->f_name;
1067			db_printf("%s\t%%st(%d)",name, f_rm(regmodrm));
1068			break;
1069		    case op1(X):
1070			db_printf("%s", ((const char *const*)fp->f_rrname)[f_rm(regmodrm)]);
1071			break;
1072		    case op1(XA):
1073			db_printf("%s\t%%ax",
1074				  ((const char *const*)fp->f_rrname)[f_rm(regmodrm)]);
1075			break;
1076		    default:
1077			db_printf("<bad instruction>");
1078			break;
1079		}
1080	}
1081
1082	return (loc);
1083}
1084
1085/*
1086 * Disassemble instruction at 'loc'.  'altfmt' specifies an
1087 * (optional) alternate format.  Return address of start of
1088 * next instruction.
1089 */
1090db_addr_t
1091db_disasm(
1092    db_addr_t	loc,
1093    bool	altfmt)
1094{
1095	int	inst;
1096	int	size;
1097	int	short_addr;
1098	const char *	seg;
1099	const struct inst *	ip;
1100	const char *	i_name;
1101	int	i_size;
1102	int	i_mode;
1103	int	regmodrm = 0;
1104	bool	first;
1105	int	displ;
1106	int	prefix;
1107	int	imm;
1108	int	imm2;
1109	int	len;
1110	struct i_addr	address;
1111
1112#ifdef _KERNEL
1113	pt_entry_t *pte, *pde;
1114
1115	/*
1116	 * Don't try to disassemble the location if the mapping is invalid.
1117	 * If we do, we'll fault, and end up debugging the debugger!
1118	 * in the case of largepages, "pte" is really the pde and "pde" is
1119	 * really the entry for the pdp itself.
1120	 */
1121	if ((vaddr_t)loc >= VM_MIN_KERNEL_ADDRESS)
1122		pte = kvtopte((vaddr_t)loc);
1123	else
1124		pte = vtopte((vaddr_t)loc);
1125	pde = vtopte((vaddr_t)pte);
1126	if ((*pde & PG_V) == 0 || (*pte & PG_V) == 0) {
1127		db_printf("invalid address\n");
1128		return (loc);
1129	}
1130#endif
1131
1132	get_value_inc(inst, loc, 1, false);
1133	short_addr = false;
1134	size = LONG;
1135	seg = 0;
1136
1137	/*
1138	 * Get prefixes
1139	 */
1140	prefix = true;
1141	do {
1142		switch (inst) {
1143		    case 0x66:		/* data16 */
1144			size = WORD;
1145			break;
1146		    case 0x67:
1147			short_addr = true;
1148			break;
1149		    case 0x26:
1150			seg = "%es";
1151			break;
1152		    case 0x36:
1153			seg = "%ss";
1154			break;
1155		    case 0x2e:
1156			seg = "%cs";
1157			break;
1158		    case 0x3e:
1159			seg = "%ds";
1160			break;
1161		    case 0x64:
1162			seg = "%fs";
1163			break;
1164		    case 0x65:
1165			seg = "%gs";
1166			break;
1167		    case 0xf0:
1168			db_printf("lock ");
1169			break;
1170		    case 0xf2:
1171			db_printf("repne ");
1172			break;
1173		    case 0xf3:
1174			db_printf("repe ");	/* XXX repe VS rep */
1175			break;
1176		    default:
1177			prefix = false;
1178			break;
1179		}
1180		if (prefix)
1181			get_value_inc(inst, loc, 1, false);
1182	} while (prefix);
1183
1184	if (inst >= 0xd8 && inst <= 0xdf) {
1185		loc = db_disasm_esc(loc, inst, short_addr, size, seg);
1186		db_printf("\n");
1187		return (loc);
1188	}
1189
1190	if (inst == 0x0f) {
1191		get_value_inc(inst, loc, 1, false);
1192		ip = db_inst_0f[inst>>4];
1193		if (ip == 0)
1194			ip = &db_bad_inst;
1195		else
1196			ip = &ip[inst&0xf];
1197	} else {
1198		ip = &db_inst_table[inst];
1199	}
1200
1201	if (ip->i_has_modrm) {
1202		get_value_inc(regmodrm, loc, 1, false);
1203		loc = db_read_address(loc, short_addr, regmodrm, &address);
1204	}
1205
1206	i_name = ip->i_name;
1207	i_size = ip->i_size;
1208	i_mode = ip->i_mode;
1209
1210	if (ip->i_extra == (const char *)db_Grp1 ||
1211	    ip->i_extra == (const char *)db_Grp2 ||
1212	    ip->i_extra == (const char *)db_Grp6 ||
1213	    ip->i_extra == (const char *)db_Grp7 ||
1214	    ip->i_extra == (const char *)db_Grp8) {
1215		i_name = ((const char *const*)ip->i_extra)[f_reg(regmodrm)];
1216	} else if (ip->i_extra == (const char *)db_Grp3) {
1217		ip = (const struct inst *)ip->i_extra;
1218		ip = &ip[f_reg(regmodrm)];
1219		i_name = ip->i_name;
1220		i_mode = ip->i_mode;
1221	} else if (ip->i_extra == (const char *)db_Grp4 ||
1222		   ip->i_extra == (const char *)db_Grp5) {
1223		ip = (const struct inst *)ip->i_extra;
1224		ip = &ip[f_reg(regmodrm)];
1225		i_name = ip->i_name;
1226		i_mode = ip->i_mode;
1227		i_size = ip->i_size;
1228	}
1229
1230	if (i_size == SDEP) {
1231		if (size == WORD)
1232			db_printf("%s", i_name);
1233		else
1234			db_printf("%s", ip->i_extra);
1235	} else {
1236		db_printf("%s", i_name);
1237		if (i_size != NONE) {
1238			if (i_size == BYTE) {
1239				db_printf("b");
1240				size = BYTE;
1241			} else if (i_size == WORD) {
1242				db_printf("w");
1243				size = WORD;
1244			} else if (size == WORD) {
1245				db_printf("w");
1246			} else {
1247				db_printf("l");
1248			}
1249		}
1250	}
1251	db_printf("\t");
1252	for (first = true;
1253	     i_mode != 0;
1254	     i_mode >>= 8, first = false) {
1255		char tbuf[24];
1256
1257		if (!first)
1258			db_printf(",");
1259
1260		switch (i_mode & 0xFF) {
1261		    case E:
1262			db_print_address(seg, size, &address);
1263			break;
1264		    case Eind:
1265			db_printf("*");
1266			db_print_address(seg, size, &address);
1267			break;
1268		    case Ew:
1269			db_print_address(seg, WORD, &address);
1270			break;
1271		    case Eb:
1272			db_print_address(seg, BYTE, &address);
1273			break;
1274		    case R:
1275			db_printf("%s", db_reg[size][f_reg(regmodrm)]);
1276			break;
1277		    case Rw:
1278			db_printf("%s", db_reg[WORD][f_reg(regmodrm)]);
1279			break;
1280		    case Ri:
1281			db_printf("%s", db_reg[size][f_rm(inst)]);
1282			break;
1283		    case S:
1284			db_printf("%s", db_seg_reg[f_reg(regmodrm)]);
1285			break;
1286		    case Si:
1287			db_printf("%s", db_seg_reg[f_reg(inst)]);
1288			break;
1289		    case A:
1290			db_printf("%s", db_reg[size][0]);	/* acc */
1291			break;
1292		    case BX:
1293			if (seg)
1294				db_printf("%s:", seg);
1295			db_printf("(%s)", short_addr ? "%bx" : "%ebx");
1296			break;
1297		    case CL:
1298			db_printf("%%cl");
1299			break;
1300		    case DX:
1301			db_printf("%%dx");
1302			break;
1303		    case SI:
1304			if (seg)
1305				db_printf("%s:", seg);
1306			db_printf("(%s)", short_addr ? "%si" : "%esi");
1307			break;
1308		    case DI:
1309			db_printf("%%es:(%s)", short_addr ? "%di" : "%edi");
1310			break;
1311		    case CR:
1312			db_printf("%%cr%d", f_reg(regmodrm));
1313			break;
1314		    case DR:
1315			db_printf("%%dr%d", f_reg(regmodrm));
1316			break;
1317		    case TR:
1318			db_printf("%%tr%d", f_reg(regmodrm));
1319			break;
1320		    case I:
1321			len = db_lengths[size];
1322			get_value_inc(imm, loc, len, false);/* unsigned */
1323			db_format_radix(tbuf, 24, (unsigned int)imm, true);
1324			db_printf("$%s", tbuf);
1325			break;
1326		    case Is:
1327			len = db_lengths[size];
1328			get_value_inc(imm, loc, len, true);	/* signed */
1329			db_format_radix(tbuf, 24, imm, true);
1330			db_printf("$%s", tbuf);
1331			break;
1332		    case Ib:
1333			get_value_inc(imm, loc, 1, false);	/* unsigned */
1334			db_format_radix(tbuf, 24, (unsigned int)imm, true);
1335			db_printf("$%s", tbuf);
1336			break;
1337		    case Ibs:
1338			get_value_inc(imm, loc, 1, true);	/* signed */
1339			db_format_radix(tbuf, 24, imm, true);
1340			db_printf("$%s", tbuf);
1341			break;
1342		    case Iw:
1343			get_value_inc(imm, loc, 2, false);	/* unsigned */
1344			db_format_radix(tbuf, 24, (unsigned int)imm, true);
1345			db_printf("$%s", tbuf);
1346			break;
1347		    case Il:
1348			get_value_inc(imm, loc, 4, false);
1349			db_format_radix(tbuf, 24, (unsigned int)imm, true);
1350			db_printf("$%s", tbuf);
1351			break;
1352		    case O:
1353			if (short_addr)
1354				get_value_inc(displ, loc, 2, true);
1355			else
1356				get_value_inc(displ, loc, 4, true);
1357			if (seg) {
1358				db_format_radix(tbuf, 24, displ, true);
1359				db_printf("%s:%s", seg, tbuf);
1360			} else
1361				db_printsym((db_addr_t)displ, DB_STGY_ANY,
1362				    db_printf);
1363			break;
1364		    case Db:
1365			get_value_inc(displ, loc, 1, true);
1366			db_printsym((db_addr_t)(displ + loc), DB_STGY_XTRN,
1367			    db_printf);
1368			break;
1369		    case Dl:
1370			get_value_inc(displ, loc, 4, true);
1371			db_printsym((db_addr_t)(displ + loc), DB_STGY_XTRN,
1372			    db_printf);
1373			break;
1374		    case o1:
1375			db_printf("$1");
1376			break;
1377		    case o3:
1378			db_printf("$3");
1379			break;
1380		    case OS:
1381			get_value_inc(imm, loc, 4, false);	/* offset */
1382			db_format_radix(tbuf, 24, (unsigned int)imm, true);
1383			db_printf("$%s", tbuf);
1384			get_value_inc(imm2, loc, 2, false);	/* segment */
1385			db_format_radix(tbuf, 24, (unsigned int)imm2, true);
1386			db_printf(",%s", tbuf);
1387			break;
1388		}
1389	}
1390
1391	db_printf("\n");
1392	return (loc);
1393}
1394