xref: /f-stack/freebsd/mips/mips/db_disasm.c (revision 22ce4aff)
1 /*	$OpenBSD: db_disasm.c,v 1.1 1998/03/16 09:03:24 pefo Exp $	*/
2 /*-
3  * SPDX-License-Identifier: BSD-4-Clause
4  *
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Ralph Campbell.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	from: @(#)kadb.c	8.1 (Berkeley) 6/10/93
40  *	Id: db_disasm.c,v 1.1 1998/03/16 09:03:24 pefo Exp
41  *	JNPR: db_disasm.c,v 1.1 2006/08/07 05:38:57 katta
42  */
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <sys/param.h>
48 #include <vm/vm_param.h>
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51 #include <sys/systm.h>
52 
53 #include <machine/mips_opcode.h>
54 #include <machine/db_machdep.h>
55 #include <ddb/ddb.h>
56 #include <ddb/db_output.h>
57 
58 static char *op_name[64] = {
59 /* 0 */ "spec", "bcond","j",	"jal",	"beq",	"bne",	"blez",	"bgtz",
60 /* 8 */ "addi", "addiu","slti",	"sltiu","andi",	"ori",	"xori",	"lui",
61 /*16 */ "cop0", "cop1",	"cop2",	"cop3", "beql",	"bnel",	"blezl","bgtzl",
62 /*24 */ "daddi","daddiu","ldl",	"ldr",	"op34",	"op35",	"op36",	"op37",
63 /*32 */ "lb",	"lh",	"lwl",	"lw",	"lbu",	"lhu",	"lwr",	"lwu",
64 /*40 */ "sb",	"sh",	"swl",	"sw",	"sdl",	"sdr",	"swr",	"cache",
65 /*48 */ "ll",	"lwc1",	"lwc2",	"lwc3", "lld",	"ldc1",	"ldc2",	"ld",
66 /*56 */ "sc",	"swc1",	"swc2",	"swc3", "scd",	"sdc1",	"sdc2",	"sd"
67 };
68 
69 static char *spec_name[64] = {
70 /* 0 */ "sll",	"spec01","srl", "sra",	"sllv",	"spec05","srlv","srav",
71 /* 8 */ "jr",	"jalr",	"spec12","spec13","syscall","break","spec16","sync",
72 /*16 */ "mfhi",	"mthi",	"mflo", "mtlo",	"dsllv","spec25","dsrlv","dsrav",
73 /*24 */ "mult",	"multu","div",	"divu",	"dmult","dmultu","ddiv","ddivu",
74 /*32 */ "add",	"addu",	"sub",	"subu",	"and",	"or",	"xor",	"nor",
75 /*40 */ "spec50","spec51","slt","sltu",	"dadd","daddu","dsub","dsubu",
76 /*48 */ "tge","tgeu","tlt","tltu","teq","spec65","tne","spec67",
77 /*56 */ "dsll","spec71","dsrl","dsra","dsll32","spec75","dsrl32","dsra32"
78 };
79 
80 static char *bcond_name[32] = {
81 /* 0 */ "bltz",	"bgez",	"bltzl", "bgezl", "?", "?", "?", "?",
82 /* 8 */ "tgei",	"tgeiu", "tlti", "tltiu", "teqi", "?", "tnei", "?",
83 /*16 */ "bltzal", "bgezal", "bltzall", "bgezall", "?", "?", "?", "?",
84 /*24 */ "?", "?", "?", "?", "?", "?", "?", "?",
85 };
86 
87 static char *cop1_name[64] = {
88 /* 0 */ "fadd",	"fsub",	"fmpy",	"fdiv",	"fsqrt","fabs",	"fmov",	"fneg",
89 /* 8 */ "fop08","fop09","fop0a","fop0b","fop0c","fop0d","fop0e","fop0f",
90 /*16 */ "fop10","fop11","fop12","fop13","fop14","fop15","fop16","fop17",
91 /*24 */ "fop18","fop19","fop1a","fop1b","fop1c","fop1d","fop1e","fop1f",
92 /*32 */ "fcvts","fcvtd","fcvte","fop23","fcvtw","fop25","fop26","fop27",
93 /*40 */ "fop28","fop29","fop2a","fop2b","fop2c","fop2d","fop2e","fop2f",
94 /*48 */ "fcmp.f","fcmp.un","fcmp.eq","fcmp.ueq","fcmp.olt","fcmp.ult",
95 	"fcmp.ole","fcmp.ule",
96 /*56 */ "fcmp.sf","fcmp.ngle","fcmp.seq","fcmp.ngl","fcmp.lt","fcmp.nge",
97 	"fcmp.le","fcmp.ngt"
98 };
99 
100 static char *fmt_name[16] = {
101 	"s",	"d",	"e",	"fmt3",
102 	"w",	"fmt5",	"fmt6",	"fmt7",
103 	"fmt8",	"fmt9",	"fmta",	"fmtb",
104 	"fmtc",	"fmtd",	"fmte",	"fmtf"
105 };
106 
107 static char *reg_name[32] = {
108 	"zero",	"at",	"v0",	"v1",	"a0",	"a1",	"a2",	"a3",
109 #if defined(__mips_n32) || defined(__mips_n64)
110 	"a4",	"a5",	"a6",	"a7",	"t0",	"t1",	"t2",	"t3",
111 #else
112 	"t0",	"t1",	"t2",	"t3",	"t4",	"t5",	"t6",	"t7",
113 #endif
114 	"s0",	"s1",	"s2",	"s3",	"s4",	"s5",	"s6",	"s7",
115 	"t8",	"t9",	"k0",	"k1",	"gp",	"sp",	"s8",	"ra"
116 };
117 
118 static char *c0_opname[64] = {
119 	"c0op00","tlbr",  "tlbwi", "c0op03","c0op04","c0op05","tlbwr", "c0op07",
120 	"tlbp",	"c0op11","c0op12","c0op13","c0op14","c0op15","c0op16","c0op17",
121 	"rfe",	"c0op21","c0op22","c0op23","c0op24","c0op25","c0op26","c0op27",
122 	"eret","c0op31","c0op32","c0op33","c0op34","c0op35","c0op36","c0op37",
123 	"c0op40","c0op41","c0op42","c0op43","c0op44","c0op45","c0op46","c0op47",
124 	"c0op50","c0op51","c0op52","c0op53","c0op54","c0op55","c0op56","c0op57",
125 	"c0op60","c0op61","c0op62","c0op63","c0op64","c0op65","c0op66","c0op67",
126 	"c0op70","c0op71","c0op72","c0op73","c0op74","c0op75","c0op77","c0op77",
127 };
128 
129 static char *c0_reg[32] = {
130 	"index","random","tlblo0","tlblo1","context","tlbmask","wired","c0r7",
131 	"badvaddr","count","tlbhi","c0r11","sr","cause","epc",	"prid",
132 	"config","lladr","watchlo","watchhi","xcontext","c0r21","c0r22","c0r23",
133 	"c0r24","c0r25","ecc","cacheerr","taglo","taghi","errepc","c0r31"
134 };
135 
136 static int md_printins(int ins, int mdbdot);
137 
138 db_addr_t
db_disasm(db_addr_t loc,bool altfmt)139 db_disasm(db_addr_t loc, bool altfmt)
140 
141 {
142 	int ins;
143 
144 	if (vtophys((vm_offset_t)loc)) {
145 		db_read_bytes((vm_offset_t)loc, (size_t)sizeof(int),
146 		    (char *)&ins);
147 		md_printins(ins, loc);
148 	}
149 
150 	return (loc + sizeof(int));
151 }
152 
153 /* ARGSUSED */
154 static int
md_printins(int ins,int mdbdot)155 md_printins(int ins, int mdbdot)
156 {
157 	InstFmt i;
158 	int delay = 0;
159 
160 	i.word = ins;
161 
162 	switch (i.JType.op) {
163 	case OP_SPECIAL:
164 		if (i.word == 0) {
165 			db_printf("nop");
166 			break;
167 		}
168 		if (i.RType.func == OP_ADDU && i.RType.rt == 0) {
169 			db_printf("move\t%s,%s",
170 			    reg_name[i.RType.rd], reg_name[i.RType.rs]);
171 			break;
172 		}
173 		db_printf("%s", spec_name[i.RType.func]);
174 		switch (i.RType.func) {
175 		case OP_SLL:
176 		case OP_SRL:
177 		case OP_SRA:
178 		case OP_DSLL:
179 		case OP_DSRL:
180 		case OP_DSRA:
181 		case OP_DSLL32:
182 		case OP_DSRL32:
183 		case OP_DSRA32:
184 			db_printf("\t%s,%s,%d", reg_name[i.RType.rd],
185 			    reg_name[i.RType.rt], i.RType.shamt);
186 			break;
187 
188 		case OP_SLLV:
189 		case OP_SRLV:
190 		case OP_SRAV:
191 		case OP_DSLLV:
192 		case OP_DSRLV:
193 		case OP_DSRAV:
194 			db_printf("\t%s,%s,%s", reg_name[i.RType.rd],
195 			    reg_name[i.RType.rt], reg_name[i.RType.rs]);
196 			break;
197 
198 		case OP_MFHI:
199 		case OP_MFLO:
200 			db_printf("\t%s", reg_name[i.RType.rd]);
201 			break;
202 
203 		case OP_JR:
204 		case OP_JALR:
205 			delay = 1;
206 			/* FALLTHROUGH */
207 		case OP_MTLO:
208 		case OP_MTHI:
209 			db_printf("\t%s", reg_name[i.RType.rs]);
210 			break;
211 
212 		case OP_MULT:
213 		case OP_MULTU:
214 		case OP_DMULT:
215 		case OP_DMULTU:
216 		case OP_DIV:
217 		case OP_DIVU:
218 		case OP_DDIV:
219 		case OP_DDIVU:
220 			db_printf("\t%s,%s",
221 			    reg_name[i.RType.rs], reg_name[i.RType.rt]);
222 			break;
223 
224 		case OP_SYSCALL:
225 		case OP_SYNC:
226 			break;
227 
228 		case OP_BREAK:
229 			db_printf("\t%d", (i.RType.rs << 5) | i.RType.rt);
230 			break;
231 
232 		default:
233 			db_printf("\t%s,%s,%s", reg_name[i.RType.rd],
234 			    reg_name[i.RType.rs], reg_name[i.RType.rt]);
235 		}
236 		break;
237 
238 	case OP_BCOND:
239 		db_printf("%s\t%s,", bcond_name[i.IType.rt],
240 		    reg_name[i.IType.rs]);
241 		goto pr_displ;
242 
243 	case OP_BLEZ:
244 	case OP_BLEZL:
245 	case OP_BGTZ:
246 	case OP_BGTZL:
247 		db_printf("%s\t%s,", op_name[i.IType.op],
248 		    reg_name[i.IType.rs]);
249 		goto pr_displ;
250 
251 	case OP_BEQ:
252 	case OP_BEQL:
253 		if (i.IType.rs == 0 && i.IType.rt == 0) {
254 			db_printf("b\t");
255 			goto pr_displ;
256 		}
257 		/* FALLTHROUGH */
258 	case OP_BNE:
259 	case OP_BNEL:
260 		db_printf("%s\t%s,%s,", op_name[i.IType.op],
261 		    reg_name[i.IType.rs], reg_name[i.IType.rt]);
262 	pr_displ:
263 		delay = 1;
264 		db_printf("0x%08x", mdbdot + 4 + ((short)i.IType.imm << 2));
265 		break;
266 
267 	case OP_COP0:
268 		switch (i.RType.rs) {
269 		case OP_BCx:
270 		case OP_BCy:
271 			db_printf("bc0%c\t",
272 			    "ft"[i.RType.rt & COPz_BC_TF_MASK]);
273 			goto pr_displ;
274 
275 		case OP_MT:
276 			db_printf("mtc0\t%s,%s",
277 			    reg_name[i.RType.rt], c0_reg[i.RType.rd]);
278 			break;
279 
280 		case OP_DMT:
281 			db_printf("dmtc0\t%s,%s",
282 			    reg_name[i.RType.rt], c0_reg[i.RType.rd]);
283 			break;
284 
285 		case OP_MF:
286 			db_printf("mfc0\t%s,%s",
287 			    reg_name[i.RType.rt], c0_reg[i.RType.rd]);
288 			break;
289 
290 		case OP_DMF:
291 			db_printf("dmfc0\t%s,%s",
292 			    reg_name[i.RType.rt], c0_reg[i.RType.rd]);
293 			break;
294 
295 		default:
296 			db_printf("%s", c0_opname[i.FRType.func]);
297 		}
298 		break;
299 
300 	case OP_COP1:
301 		switch (i.RType.rs) {
302 		case OP_BCx:
303 		case OP_BCy:
304 			db_printf("bc1%c\t",
305 			    "ft"[i.RType.rt & COPz_BC_TF_MASK]);
306 			goto pr_displ;
307 
308 		case OP_MT:
309 			db_printf("mtc1\t%s,f%d",
310 			    reg_name[i.RType.rt], i.RType.rd);
311 			break;
312 
313 		case OP_MF:
314 			db_printf("mfc1\t%s,f%d",
315 			    reg_name[i.RType.rt], i.RType.rd);
316 			break;
317 
318 		case OP_CT:
319 			db_printf("ctc1\t%s,f%d",
320 			    reg_name[i.RType.rt], i.RType.rd);
321 			break;
322 
323 		case OP_CF:
324 			db_printf("cfc1\t%s,f%d",
325 			    reg_name[i.RType.rt], i.RType.rd);
326 			break;
327 
328 		default:
329 			db_printf("%s.%s\tf%d,f%d,f%d",
330 			    cop1_name[i.FRType.func], fmt_name[i.FRType.fmt],
331 			    i.FRType.fd, i.FRType.fs, i.FRType.ft);
332 		}
333 		break;
334 
335 	case OP_J:
336 	case OP_JAL:
337 		db_printf("%s\t", op_name[i.JType.op]);
338 		db_printf("0x%8x",(mdbdot & 0xF0000000) | (i.JType.target << 2));
339 		delay = 1;
340 		break;
341 
342 	case OP_LWC1:
343 	case OP_SWC1:
344 		db_printf("%s\tf%d,", op_name[i.IType.op], i.IType.rt);
345 		goto loadstore;
346 
347 	case OP_LB:
348 	case OP_LH:
349 	case OP_LW:
350 	case OP_LD:
351 	case OP_LBU:
352 	case OP_LHU:
353 	case OP_LWU:
354 	case OP_SB:
355 	case OP_SH:
356 	case OP_SW:
357 	case OP_SD:
358 		db_printf("%s\t%s,", op_name[i.IType.op],
359 		    reg_name[i.IType.rt]);
360 	loadstore:
361 		db_printf("%d(%s)", (short)i.IType.imm, reg_name[i.IType.rs]);
362 		break;
363 
364 	case OP_ORI:
365 	case OP_XORI:
366 		if (i.IType.rs == 0) {
367 			db_printf("li\t%s,0x%x",
368 			    reg_name[i.IType.rt], i.IType.imm);
369 			break;
370 		}
371 		/* FALLTHROUGH */
372 	case OP_ANDI:
373 		db_printf("%s\t%s,%s,0x%x", op_name[i.IType.op],
374 		    reg_name[i.IType.rt], reg_name[i.IType.rs], i.IType.imm);
375 		break;
376 
377 	case OP_LUI:
378 		db_printf("%s\t%s,0x%x", op_name[i.IType.op],
379 		    reg_name[i.IType.rt], i.IType.imm);
380 		break;
381 
382 	case OP_ADDI:
383 	case OP_DADDI:
384 	case OP_ADDIU:
385 	case OP_DADDIU:
386 		if (i.IType.rs == 0) {
387 			db_printf("li\t%s,%d", reg_name[i.IType.rt],
388 			    (short)i.IType.imm);
389 			break;
390 		}
391 		/* FALLTHROUGH */
392 	default:
393 		db_printf("%s\t%s,%s,%d", op_name[i.IType.op],
394 		    reg_name[i.IType.rt], reg_name[i.IType.rs],
395 		    (short)i.IType.imm);
396 	}
397 	db_printf("\n");
398 	return (delay);
399 }
400