1 /* $NetBSD: db_disasm.c,v 1.19 2009/03/18 16:00:16 cegger Exp $ */ 2 /* 3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Ludd by 7 * Bertram Barth. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed at Ludd, University of 20 * Lule}, Sweden and its contributors. 21 * 4. The name of the author may not be used to endorse or promote products 22 * derived from this software without specific prior written permission 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: db_disasm.c,v 1.19 2009/03/18 16:00:16 cegger Exp $"); 38 39 #include <sys/param.h> 40 #include <sys/proc.h> 41 #include <sys/reboot.h> 42 #include <sys/systm.h> 43 44 #include <machine/db_machdep.h> 45 #include <ddb/db_sym.h> 46 #include <ddb/db_variables.h> 47 #include <ddb/db_interface.h> 48 #include <ddb/db_output.h> 49 50 #include <vax/vax/db_disasm.h> 51 52 #ifdef VMS_MODE 53 #define DEFERRED '@' 54 #define LITERAL '#' 55 #else 56 #define DEFERRED '*' 57 #define LITERAL '$' 58 #endif 59 /* 60 * disassembling vax instructions works as follows: 61 * 62 * 1. get first byte as opcode (check for two-byte opcodes!) 63 * 2. lookup in op-table for mnemonic and operand-list 64 * 2.a store the mnemonic 65 * 3. for each operand in list: get the size/type 66 * 3.a evaluate addressing mode for this operand 67 * 3.b store each operand(s) 68 * 4. db_printf the opcode and the (value of the) operands 69 * 5. return the start of the next instruction 70 * 71 * - if jump/branch calculate (and display) the target-address 72 */ 73 74 /* 75 #define BROKEN_DB_REGS 76 */ 77 #ifdef BROKEN_DB_REGS 78 const struct { /* Due to order and contents of db_regs[], we can't */ 79 const char *name; /* use this array to extract register-names. */ 80 void *valuep; /* eg. "psl" vs "pc", "pc" vs "sp" */ 81 } my_db_regs[16] = { 82 { "r0", NULL }, 83 { "r1", NULL }, 84 { "r2", NULL }, 85 { "r3", NULL }, 86 { "r4", NULL }, 87 { "r5", NULL }, 88 { "r6", NULL }, 89 { "r7", NULL }, 90 { "r8", NULL }, 91 { "r9", NULL }, 92 { "r10", NULL }, 93 { "r11", NULL }, 94 { "ap", NULL }, /* aka "r12" */ 95 { "fp", NULL }, /* aka "r13" */ 96 { "sp", NULL }, /* aka "r14" */ 97 { "pc", NULL }, /* aka "r15" */ 98 }; 99 #else 100 #define my_db_regs db_regs 101 #endif 102 103 typedef struct { 104 char dasm[256]; /* disassebled instruction as text */ 105 char *curp; /* pointer into result */ 106 char *ppc; /* pseudo PC */ 107 int opc; /* op-code */ 108 const char *argp; /* pointer into argument-list */ 109 int itype; /* instruction-type, eg. branch, call, unspec */ 110 int atype; /* argument-type, eg. byte, long, address */ 111 int off; /* offset specified by last argument */ 112 int addr; /* address specified by last argument */ 113 } inst_buffer; 114 115 #define ITYPE_INVALID -1 116 #define ITYPE_UNSPEC 0 117 #define ITYPE_BRANCH 1 118 #define ITYPE_CALL 2 119 120 static inline int get_byte(inst_buffer * ib); 121 static inline int get_word(inst_buffer * ib); 122 static inline int get_long(inst_buffer * ib); 123 124 static int get_opcode(inst_buffer * ib); 125 static int get_operands(inst_buffer * ib); 126 static int get_operand(inst_buffer * ib, int size); 127 128 static inline void add_char(inst_buffer * ib, char c); 129 static inline void add_str(inst_buffer * ib, const char *s); 130 static void add_int(inst_buffer * ib, int i); 131 static void add_xint(inst_buffer * ib, int i); 132 static void add_sym(inst_buffer * ib, int i); 133 static void add_off(inst_buffer * ib, int i); 134 135 #define err_print printf 136 137 /* 138 * Disassemble instruction at 'loc'. 'altfmt' specifies an 139 * (optional) alternate format (altfmt for vax: don't assume 140 * that each external label is a procedure entry mask). 141 * Return address of start of next instruction. 142 * Since this function is used by 'examine' and by 'step' 143 * "next instruction" does NOT mean the next instruction to 144 * be executed but the 'linear' next instruction. 145 */ 146 db_addr_t 147 db_disasm(db_addr_t loc, bool altfmt) 148 { 149 db_expr_t diff; 150 db_sym_t sym; 151 const char *symname; 152 153 inst_buffer ib; 154 155 memset(&ib, 0, sizeof(ib)); 156 ib.ppc = (void *) loc; 157 ib.curp = ib.dasm; 158 159 if (!altfmt) { /* ignore potential entry masks in altfmt */ 160 diff = INT_MAX; 161 symname = NULL; 162 sym = db_search_symbol(loc, DB_STGY_PROC, &diff); 163 db_symbol_values(sym, &symname, 0); 164 165 if (symname && !diff) { /* symbol at loc */ 166 db_printf("function \"%s()\", entry-mask 0x%x\n\t\t", 167 symname, (unsigned short) get_word(&ib)); 168 ib.ppc += 2; 169 } 170 } 171 get_opcode(&ib); 172 get_operands(&ib); 173 db_printf("%s\n", ib.dasm); 174 175 return ((u_int) ib.ppc); 176 } 177 178 int 179 get_opcode(inst_buffer *ib) 180 { 181 ib->opc = get_byte(ib); 182 if (ib->opc >> 2 == 0x3F) { /* two byte op-code */ 183 ib->opc = ib->opc << 8; 184 ib->opc += get_byte(ib); 185 } 186 switch (ib->opc) { 187 case 0xFA: /* CALLG */ 188 case 0xFB: /* CALLS */ 189 case 0xFC: /* XFC */ 190 ib->itype = ITYPE_CALL; 191 break; 192 case 0x16: /* JSB */ 193 case 0x17: /* JMP */ 194 ib->itype = ITYPE_BRANCH; 195 break; 196 default: 197 ib->itype = ITYPE_UNSPEC; 198 } 199 if (ib->opc < 0 || ib->opc > 0xFF) { 200 add_str(ib, "invalid or two-byte opcode "); 201 add_xint(ib, ib->opc); 202 ib->itype = ITYPE_INVALID; 203 } else { 204 add_str(ib, vax_inst[ib->opc].mnemonic); 205 add_char(ib, '\t'); 206 } 207 return (ib->opc); 208 } 209 210 int 211 get_operands(inst_buffer *ib) 212 { 213 int aa = 0; /* absolute address mode ? */ 214 int size; 215 216 if (ib->opc < 0 || ib->opc > 0xFF) { 217 /* invalid or two-byte opcode */ 218 ib->argp = NULL; 219 return (-1); 220 } 221 ib->argp = vax_inst[ib->opc].argdesc; 222 223 while (*ib->argp) { 224 switch (*ib->argp) { 225 226 case 'b': /* branch displacement */ 227 switch (*(++ib->argp)) { 228 case 'b': 229 ib->off = (signed char) get_byte(ib); 230 break; 231 case 'w': 232 ib->off = (short) get_word(ib); 233 break; 234 case 'l': 235 ib->off = get_long(ib); 236 break; 237 default: 238 err_print("XXX eror\n"); 239 } 240 /* add_int(ib, ib->off); */ 241 ib->addr = (u_int) ib->ppc + ib->off; 242 add_off(ib, ib->addr); 243 break; 244 245 case 'a': /* absolute addressing mode */ 246 aa = 1; /* do not break here ! */ 247 248 default: 249 switch (*(++ib->argp)) { 250 case 'b': /* Byte */ 251 size = SIZE_BYTE; 252 break; 253 case 'w': /* Word */ 254 size = SIZE_WORD; 255 break; 256 case 'l': /* Long-Word */ 257 case 'f': /* F_Floating */ 258 size = SIZE_LONG; 259 break; 260 case 'q': /* Quad-Word */ 261 case 'd': /* D_Floating */ 262 case 'g': /* G_Floating */ 263 size = SIZE_QWORD; 264 break; 265 case 'o': /* Octa-Word */ 266 case 'h': /* H_Floating */ 267 size = SIZE_OWORD; 268 break; 269 default: 270 err_print("invalid op-type %X (%c) found.\n", 271 *ib->argp, *ib->argp); 272 size = 0; 273 } 274 if (aa) { 275 /* get the address */ 276 ib->addr = get_operand(ib, size); 277 add_sym(ib, ib->addr); 278 } else { 279 /* get the operand */ 280 ib->addr = get_operand(ib, size); 281 add_off(ib, ib->addr); 282 } 283 } 284 285 if (!*ib->argp || !*++ib->argp) 286 break; 287 if (*ib->argp++ == ',') { 288 add_char(ib, ','); 289 add_char(ib, ' '); 290 } else { 291 err_print("XXX error\n"); 292 add_char(ib, '\0'); 293 return (-1); 294 } 295 } 296 297 add_char(ib, '\0'); 298 return (0); 299 } 300 301 int 302 get_operand(inst_buffer *ib, int size) 303 { 304 int c = get_byte(ib); 305 int mode = c >> 4; 306 int reg = c & 0x0F; 307 int lit = c & 0x3F; 308 int tmp = 0; 309 char buf[16]; 310 311 switch (mode) { 312 case 0: /* literal */ 313 case 1: /* literal */ 314 case 2: /* literal */ 315 case 3: /* literal */ 316 add_char(ib, LITERAL); 317 add_int(ib, lit); 318 tmp = lit; 319 break; 320 321 case 4: /* indexed */ 322 sprintf(buf, "[%s]", my_db_regs[reg].name); 323 get_operand(ib, 0); 324 add_str(ib, buf); 325 break; 326 327 case 5: /* register */ 328 add_str(ib, my_db_regs[reg].name); 329 break; 330 331 case 6: /* register deferred */ 332 add_char(ib, '('); 333 add_str(ib, my_db_regs[reg].name); 334 add_char(ib, ')'); 335 break; 336 337 case 7: /* autodecrement */ 338 add_char(ib, '-'); 339 add_char(ib, '('); 340 add_str(ib, my_db_regs[reg].name); 341 add_char(ib, ')'); 342 if (reg == 0x0F) { /* pc is not allowed in this mode */ 343 err_print("autodecrement not allowd for PC.\n"); 344 } 345 break; 346 347 case 9: /* autoincrement deferred */ 348 add_char(ib, DEFERRED); 349 if (reg == 0x0F) { /* pc: immediate deferred */ 350 /* 351 * addresses are always longwords! 352 */ 353 tmp = get_long(ib); 354 add_off(ib, tmp); 355 break; 356 } 357 /* fall through */ 358 case 8: /* autoincrement */ 359 if (reg == 0x0F) { /* pc: immediate ==> special syntax */ 360 switch (size) { 361 case SIZE_BYTE: 362 tmp = (signed char) get_byte(ib); 363 break; 364 case SIZE_WORD: 365 tmp = (signed short) get_word(ib); 366 break; 367 case SIZE_LONG: 368 tmp = get_long(ib); 369 break; 370 default: 371 err_print("illegal op-type %d\n", size); 372 tmp = -1; 373 } 374 if (mode == 8) 375 add_char(ib, LITERAL); 376 add_int(ib, tmp); 377 break; 378 } 379 add_char(ib, '('); 380 add_str(ib, my_db_regs[reg].name); 381 add_char(ib, ')'); 382 add_char(ib, '+'); 383 break; 384 385 case 11: /* byte displacement deferred/ relative deferred */ 386 add_char(ib, DEFERRED); 387 case 10: /* byte displacement / relative mode */ 388 tmp = (signed char) get_byte(ib); 389 if (reg == 0x0F) { 390 add_off(ib, (u_int) ib->ppc + tmp); 391 break; 392 } 393 /* add_str (ib, "b^"); */ 394 add_int(ib, tmp); 395 add_char(ib, '('); 396 add_str(ib, my_db_regs[reg].name); 397 add_char(ib, ')'); 398 break; 399 400 case 13: /* word displacement deferred */ 401 add_char(ib, DEFERRED); 402 case 12: /* word displacement */ 403 tmp = (signed short) get_word(ib); 404 if (reg == 0x0F) { 405 add_off(ib, (u_int) ib->ppc + tmp); 406 break; 407 } 408 /* add_str (ib, "w^"); */ 409 add_int(ib, tmp); 410 add_char(ib, '('); 411 add_str(ib, my_db_regs[reg].name); 412 add_char(ib, ')'); 413 break; 414 415 case 15: /* long displacement referred */ 416 add_char(ib, DEFERRED); 417 case 14: /* long displacement */ 418 tmp = get_long(ib); 419 if (reg == 0x0F) { 420 add_off(ib, (u_int) ib->ppc + tmp); 421 break; 422 } 423 /* add_str (ib, "l^"); */ 424 add_int(ib, tmp); 425 add_char(ib, '('); 426 add_str(ib, my_db_regs[reg].name); 427 add_char(ib, ')'); 428 break; 429 430 default: 431 err_print("can\'t evaluate operand (%02X).\n", lit); 432 break; 433 } 434 435 return (0); 436 } 437 438 int 439 get_byte(inst_buffer *ib) 440 { 441 return ((unsigned char) *(ib->ppc++)); 442 } 443 444 int 445 get_word(inst_buffer *ib) 446 { 447 int tmp = *(uint16_t *)ib->ppc; 448 ib->ppc += 2; 449 return tmp; 450 } 451 452 int 453 get_long(inst_buffer *ib) 454 { 455 int tmp = *(int *)ib->ppc; 456 ib->ppc += 4; 457 return (tmp); 458 } 459 460 void 461 add_char(inst_buffer *ib, char c) 462 { 463 *ib->curp++ = c; 464 } 465 466 void 467 add_str(inst_buffer *ib, const char *s) 468 { 469 while ((*ib->curp++ = *s++)); 470 --ib->curp; 471 } 472 473 void 474 add_int(inst_buffer *ib, int i) 475 { 476 char buf[32]; 477 if (i < 100 && i > -100) 478 sprintf(ib->curp, "%d", i); 479 else 480 sprintf(buf, "0x%x", i); 481 add_str(ib, buf); 482 } 483 484 void 485 add_xint(inst_buffer *ib, int val) 486 { 487 char buf[32]; 488 sprintf(buf, "0x%x", val); 489 add_str(ib, buf); 490 } 491 492 void 493 add_sym(inst_buffer *ib, int loc) 494 { 495 db_expr_t diff; 496 db_sym_t sym; 497 const char *symname; 498 499 if (!loc) 500 return; 501 502 diff = INT_MAX; 503 symname = NULL; 504 sym = db_search_symbol(loc, DB_STGY_ANY, &diff); 505 db_symbol_values(sym, &symname, 0); 506 507 if (symname && !diff) { 508 /* add_char(ib, '<'); */ 509 add_str(ib, symname); 510 /* add_char(ib, '>'); */ 511 } else 512 add_xint(ib, loc); 513 } 514 515 void 516 add_off(inst_buffer *ib, int loc) 517 { 518 db_expr_t diff; 519 db_sym_t sym; 520 const char *symname; 521 522 if (!loc) 523 return; 524 525 diff = INT_MAX; 526 symname = NULL; 527 sym = db_search_symbol(loc, DB_STGY_ANY, &diff); 528 db_symbol_values(sym, &symname, 0); 529 530 if (symname) { 531 /* add_char(ib, '<'); */ 532 add_str(ib, symname); 533 if (diff) { 534 add_char(ib, '+'); 535 add_xint(ib, diff); 536 } 537 /* add_char(ib, '>'); */ 538 } else 539 add_xint(ib, loc); 540 } 541