1 /* $Id$ */ 2 3 #include <common.h> 4 5 #include <linux/ctype.h> 6 #include <bedbug/bedbug.h> 7 #include <bedbug/ppc.h> 8 #include <bedbug/regs.h> 9 #include <bedbug/tables.h> 10 11 #define Elf32_Word unsigned long 12 13 /* USE_SOURCE_CODE enables some symbolic debugging functions of this 14 code. This is only useful if the program will have access to the 15 source code for the binary being examined. 16 */ 17 18 /* #define USE_SOURCE_CODE 1 */ 19 20 #ifdef USE_SOURCE_CODE 21 extern int line_info_from_addr __P ((Elf32_Word, char *, char *, int *)); 22 extern struct symreflist *symByAddr; 23 extern char *symbol_name_from_addr __P ((Elf32_Word, int, int *)); 24 #endif /* USE_SOURCE_CODE */ 25 26 int print_operands __P ((struct ppc_ctx *)); 27 int get_operand_value __P ((struct opcode *, unsigned long, 28 enum OP_FIELD, unsigned long *)); 29 struct opcode *find_opcode __P ((unsigned long)); 30 struct opcode *find_opcode_by_name __P ((char *)); 31 char *spr_name __P ((int)); 32 int spr_value __P ((char *)); 33 char *tbr_name __P ((int)); 34 int tbr_value __P ((char *)); 35 int parse_operand __P ((unsigned long, struct opcode *, 36 struct operand *, char *, int *)); 37 int get_word __P ((char **, char *)); 38 long read_number __P ((char *)); 39 int downstring __P ((char *)); 40 41 43 /*====================================================================== 44 * Entry point for the PPC disassembler. 45 * 46 * Arguments: 47 * memaddr The address to start disassembling from. 48 * 49 * virtual If this value is non-zero, then this will be 50 * used as the base address for the output and 51 * symbol lookups. If this value is zero then 52 * memaddr is used as the absolute address. 53 * 54 * num_instr The number of instructions to disassemble. Since 55 * each instruction is 32 bits long, this can be 56 * computed if you know the total size of the region. 57 * 58 * pfunc The address of a function that is called to print 59 * each line of output. The function should take a 60 * single character pointer as its parameters a la puts. 61 * 62 * flags Sets options for the output. This is a 63 * bitwise-inclusive-OR of the following 64 * values. Note that only one of the radix 65 * options may be set. 66 * 67 * F_RADOCTAL - output radix is unsigned base 8. 68 * F_RADUDECIMAL - output radix is unsigned base 10. 69 * F_RADSDECIMAL - output radix is signed base 10. 70 * F_RADHEX - output radix is unsigned base 16. 71 * F_SIMPLE - use simplified mnemonics. 72 * F_SYMBOL - lookup symbols for addresses. 73 * F_INSTR - output raw instruction. 74 * F_LINENO - show line # info if available. 75 * 76 * Returns true if the area was successfully disassembled or false if 77 * a problem was encountered with accessing the memory. 78 */ 79 80 int disppc (unsigned char *memaddr, unsigned char *virtual, int num_instr, 81 int (*pfunc) (const char *), unsigned long flags) 82 { 83 int i; 84 struct ppc_ctx ctx; 85 86 #ifdef USE_SOURCE_CODE 87 int line_no = 0; 88 int last_line_no = 0; 89 char funcname[128] = { 0 }; 90 char filename[256] = { 0 }; 91 char last_funcname[128] = { 0 }; 92 int symoffset; 93 char *symname; 94 char *cursym = (char *) 0; 95 #endif /* USE_SOURCE_CODE */ 96 /*------------------------------------------------------------*/ 97 98 ctx.flags = flags; 99 ctx.virtual = virtual; 100 101 /* Figure out the output radix before we go any further */ 102 103 if (ctx.flags & F_RADOCTAL) { 104 /* Unsigned octal output */ 105 strcpy (ctx.radix_fmt, "O%o"); 106 } else if (ctx.flags & F_RADUDECIMAL) { 107 /* Unsigned decimal output */ 108 strcpy (ctx.radix_fmt, "%u"); 109 } else if (ctx.flags & F_RADSDECIMAL) { 110 /* Signed decimal output */ 111 strcpy (ctx.radix_fmt, "%d"); 112 } else { 113 /* Unsigned hex output */ 114 strcpy (ctx.radix_fmt, "0x%x"); 115 } 116 117 if (ctx.virtual == 0) { 118 ctx.virtual = memaddr; 119 } 120 #ifdef USE_SOURCE_CODE 121 if (ctx.flags & F_SYMBOL) { 122 if (symByAddr == 0) /* no symbols loaded */ 123 ctx.flags &= ~F_SYMBOL; 124 else { 125 cursym = (char *) 0; 126 symoffset = 0; 127 } 128 } 129 #endif /* USE_SOURCE_CODE */ 130 131 /* format each line as "XXXXXXXX: <symbol> IIIIIIII disassembly" where, 132 XXXXXXXX is the memory address in hex, 133 <symbol> is the symbolic location if F_SYMBOL is set. 134 IIIIIIII is the raw machine code in hex if F_INSTR is set, 135 and disassembly is the disassembled machine code with numbers 136 formatted according to the 'radix' parameter */ 137 138 for (i = 0; i < num_instr; ++i, memaddr += 4, ctx.virtual += 4) { 139 #ifdef USE_SOURCE_CODE 140 if (ctx.flags & F_LINENO) { 141 if ((line_info_from_addr ((Elf32_Word) ctx.virtual, 142 filename, funcname, &line_no) == true) && 143 ((line_no != last_line_no) || 144 (strcmp (last_funcname, funcname) != 0))) { 145 print_source_line (filename, funcname, line_no, pfunc); 146 } 147 last_line_no = line_no; 148 strcpy (last_funcname, funcname); 149 } 150 #endif /* USE_SOURCE_CODE */ 151 152 sprintf (ctx.data, "%08lx: ", (unsigned long) ctx.virtual); 153 ctx.datalen = 10; 154 155 #ifdef USE_SOURCE_CODE 156 if (ctx.flags & F_SYMBOL) { 157 if ((symname = 158 symbol_name_from_addr((Elf32_Word) ctx.virtual, 159 true, 0)) != 0) { 160 cursym = symname; 161 symoffset = 0; 162 } else { 163 if ((cursym == 0) && 164 ((symname = 165 symbol_name_from_addr((Elf32_Word) ctx.virtual, 166 false, &symoffset)) != 0)) { 167 cursym = symname; 168 } else { 169 symoffset += 4; 170 } 171 } 172 173 if (cursym != 0) { 174 sprintf (&ctx.data[ctx.datalen], "<%s+", cursym); 175 ctx.datalen = strlen (ctx.data); 176 sprintf (&ctx.data[ctx.datalen], ctx.radix_fmt, symoffset); 177 strcat (ctx.data, ">"); 178 ctx.datalen = strlen (ctx.data); 179 } 180 } 181 #endif /* USE_SOURCE_CODE */ 182 183 ctx.instr = INSTRUCTION (memaddr); 184 185 if (ctx.flags & F_INSTR) { 186 /* Find the opcode structure for this opcode. If one is not found 187 then it must be an illegal instruction */ 188 sprintf (&ctx.data[ctx.datalen], 189 " %02lx %02lx %02lx %02lx ", 190 ((ctx.instr >> 24) & 0xff), 191 ((ctx.instr >> 16) & 0xff), ((ctx.instr >> 8) & 0xff), 192 (ctx.instr & 0xff)); 193 ctx.datalen += 18; 194 } else { 195 strcat (ctx.data, " "); 196 ctx.datalen += 3; 197 } 198 199 if ((ctx.op = find_opcode (ctx.instr)) == 0) { 200 /* Illegal Opcode */ 201 sprintf (&ctx.data[ctx.datalen], " .long 0x%08lx", 202 ctx.instr); 203 ctx.datalen += 24; 204 (*pfunc) (ctx.data); 205 continue; 206 } 207 208 if (((ctx.flags & F_SIMPLE) == 0) || 209 (ctx.op->hfunc == 0) || 210 ((*ctx.op->hfunc) (&ctx) == false)) { 211 sprintf (&ctx.data[ctx.datalen], "%-7s ", ctx.op->name); 212 ctx.datalen += 8; 213 print_operands (&ctx); 214 } 215 216 (*pfunc) (ctx.data); 217 } 218 219 return true; 220 } /* disppc */ 221 222 224 225 /*====================================================================== 226 * Called by the disassembler to print the operands for an instruction. 227 * 228 * Arguments: 229 * ctx A pointer to the disassembler context record. 230 * 231 * always returns 0. 232 */ 233 234 int print_operands (struct ppc_ctx *ctx) 235 { 236 int open_parens = 0; 237 int field; 238 unsigned long operand; 239 struct operand *opr; 240 241 #ifdef USE_SOURCE_CODE 242 char *symname; 243 int offset; 244 #endif /* USE_SOURCE_CODE */ 245 /*------------------------------------------------------------*/ 246 247 /* Walk through the operands and list each in order */ 248 for (field = 0; ctx->op->fields[field] != 0; ++field) { 249 if (ctx->op->fields[field] > n_operands) { 250 continue; /* bad operand ?! */ 251 } 252 253 opr = &operands[ctx->op->fields[field] - 1]; 254 255 if (opr->hint & OH_SILENT) { 256 continue; 257 } 258 259 if ((field > 0) && !open_parens) { 260 strcat (ctx->data, ","); 261 ctx->datalen++; 262 } 263 264 operand = (ctx->instr >> opr->shift) & ((1 << opr->bits) - 1); 265 266 if (opr->hint & OH_ADDR) { 267 if ((operand & (1 << (opr->bits - 1))) != 0) { 268 operand = operand - (1 << opr->bits); 269 } 270 271 if (ctx->op->hint & H_RELATIVE) 272 operand = (operand << 2) + (unsigned long) ctx->virtual; 273 else 274 operand = (operand << 2); 275 276 277 sprintf (&ctx->data[ctx->datalen], "0x%lx", operand); 278 ctx->datalen = strlen (ctx->data); 279 280 #ifdef USE_SOURCE_CODE 281 if ((ctx->flags & F_SYMBOL) && 282 ((symname = 283 symbol_name_from_addr (operand, 0, &offset)) != 0)) { 284 sprintf (&ctx->data[ctx->datalen], " <%s", symname); 285 if (offset != 0) { 286 strcat (ctx->data, "+"); 287 ctx->datalen = strlen (ctx->data); 288 sprintf (&ctx->data[ctx->datalen], ctx->radix_fmt, 289 offset); 290 } 291 strcat (ctx->data, ">"); 292 } 293 #endif /* USE_SOURCE_CODE */ 294 } 295 296 else if (opr->hint & OH_REG) { 297 if ((operand == 0) && 298 (opr->field == O_rA) && (ctx->op->hint & H_RA0_IS_0)) { 299 strcat (ctx->data, "0"); 300 } else { 301 sprintf (&ctx->data[ctx->datalen], "r%d", (short) operand); 302 } 303 304 if (open_parens) { 305 strcat (ctx->data, ")"); 306 open_parens--; 307 } 308 } 309 310 else if (opr->hint & OH_SPR) { 311 strcat (ctx->data, spr_name (operand)); 312 } 313 314 else if (opr->hint & OH_TBR) { 315 strcat (ctx->data, tbr_name (operand)); 316 } 317 318 else if (opr->hint & OH_LITERAL) { 319 switch (opr->field) { 320 case O_cr2: 321 strcat (ctx->data, "cr2"); 322 ctx->datalen += 3; 323 break; 324 325 default: 326 break; 327 } 328 } 329 330 else { 331 sprintf (&ctx->data[ctx->datalen], ctx->radix_fmt, 332 (unsigned short) operand); 333 334 if (open_parens) { 335 strcat (ctx->data, ")"); 336 open_parens--; 337 } 338 339 else if (opr->hint & OH_OFFSET) { 340 strcat (ctx->data, "("); 341 open_parens++; 342 } 343 } 344 345 ctx->datalen = strlen (ctx->data); 346 } 347 348 return 0; 349 } /* print_operands */ 350 351 353 354 /*====================================================================== 355 * Called to get the value of an arbitrary operand with in an instruction. 356 * 357 * Arguments: 358 * op The pointer to the opcode structure to which 359 * the operands belong. 360 * 361 * instr The instruction (32 bits) containing the opcode 362 * and the operands to print. By the time that 363 * this routine is called the operand has already 364 * been added to the output. 365 * 366 * field The field (operand) to get the value of. 367 * 368 * value The address of an unsigned long to be filled in 369 * with the value of the operand if it is found. This 370 * will only be filled in if the function returns 371 * true. This may be passed as 0 if the value is 372 * not required. 373 * 374 * Returns true if the operand was found or false if it was not. 375 */ 376 377 int get_operand_value (struct opcode *op, unsigned long instr, 378 enum OP_FIELD field, unsigned long *value) 379 { 380 int i; 381 struct operand *opr; 382 383 /*------------------------------------------------------------*/ 384 385 if (field > n_operands) { 386 return false; /* bad operand ?! */ 387 } 388 389 /* Walk through the operands and list each in order */ 390 for (i = 0; op->fields[i] != 0; ++i) { 391 if (op->fields[i] != field) { 392 continue; 393 } 394 395 opr = &operands[op->fields[i] - 1]; 396 397 if (value) { 398 *value = (instr >> opr->shift) & ((1 << opr->bits) - 1); 399 } 400 return true; 401 } 402 403 return false; 404 } /* operand_value */ 405 406 408 409 /*====================================================================== 410 * Called by the disassembler to match an opcode value to an opcode structure. 411 * 412 * Arguments: 413 * instr The instruction (32 bits) to match. This value 414 * may contain operand values as well as the opcode 415 * since they will be masked out anyway for this 416 * search. 417 * 418 * Returns the address of an opcode struct (from the opcode table) if the 419 * operand successfully matched an entry, or 0 if no match was found. 420 */ 421 422 struct opcode *find_opcode (unsigned long instr) 423 { 424 struct opcode *ptr; 425 int top = 0; 426 int bottom = n_opcodes - 1; 427 int idx; 428 429 /*------------------------------------------------------------*/ 430 431 while (top <= bottom) { 432 idx = (top + bottom) >> 1; 433 ptr = &opcodes[idx]; 434 435 if ((instr & ptr->mask) < ptr->opcode) { 436 bottom = idx - 1; 437 } else if ((instr & ptr->mask) > ptr->opcode) { 438 top = idx + 1; 439 } else { 440 return ptr; 441 } 442 } 443 444 return (struct opcode *) 0; 445 } /* find_opcode */ 446 447 449 450 /*====================================================================== 451 * Called by the assembler to match an opcode name to an opcode structure. 452 * 453 * Arguments: 454 * name The text name of the opcode, e.g. "b", "mtspr", etc. 455 * 456 * The opcodes are sorted numerically by their instruction binary code 457 * so a search for the name cannot use the binary search used by the 458 * other find routine. 459 * 460 * Returns the address of an opcode struct (from the opcode table) if the 461 * name successfully matched an entry, or 0 if no match was found. 462 */ 463 464 struct opcode *find_opcode_by_name (char *name) 465 { 466 int idx; 467 468 /*------------------------------------------------------------*/ 469 470 downstring (name); 471 472 for (idx = 0; idx < n_opcodes; ++idx) { 473 if (!strcmp (name, opcodes[idx].name)) 474 return &opcodes[idx]; 475 } 476 477 return (struct opcode *) 0; 478 } /* find_opcode_by_name */ 479 480 482 483 /*====================================================================== 484 * Convert the 'spr' operand from its numeric value to its symbolic name. 485 * 486 * Arguments: 487 * value The value of the 'spr' operand. This value should 488 * be unmodified from its encoding in the instruction. 489 * the split-field computations will be performed 490 * here before the switch. 491 * 492 * Returns the address of a character array containing the name of the 493 * special purpose register defined by the 'value' parameter, or the 494 * address of a character array containing "???" if no match was found. 495 */ 496 497 char *spr_name (int value) 498 { 499 unsigned short spr; 500 static char other[10]; 501 int i; 502 503 /*------------------------------------------------------------*/ 504 505 /* spr is a 10 bit field whose interpretation has the high and low 506 five-bit fields reversed from their encoding in the operand */ 507 508 spr = ((value >> 5) & 0x1f) | ((value & 0x1f) << 5); 509 510 for (i = 0; i < n_sprs; ++i) { 511 if (spr == spr_map[i].spr_val) 512 return spr_map[i].spr_name; 513 } 514 515 sprintf (other, "%d", spr); 516 return other; 517 } /* spr_name */ 518 519 521 522 /*====================================================================== 523 * Convert the 'spr' operand from its symbolic name to its numeric value 524 * 525 * Arguments: 526 * name The symbolic name of the 'spr' operand. The 527 * split-field encoding will be done by this routine. 528 * NOTE: name can be a number. 529 * 530 * Returns the numeric value for the spr appropriate for encoding a machine 531 * instruction. Returns 0 if unable to find the SPR. 532 */ 533 534 int spr_value (char *name) 535 { 536 struct spr_info *sprp; 537 int spr; 538 int i; 539 540 /*------------------------------------------------------------*/ 541 542 if (!name || !*name) 543 return 0; 544 545 if (isdigit ((int) name[0])) { 546 i = htonl (read_number (name)); 547 spr = ((i >> 5) & 0x1f) | ((i & 0x1f) << 5); 548 return spr; 549 } 550 551 downstring (name); 552 553 for (i = 0; i < n_sprs; ++i) { 554 sprp = &spr_map[i]; 555 556 if (strcmp (name, sprp->spr_name) == 0) { 557 /* spr is a 10 bit field whose interpretation has the high and low 558 five-bit fields reversed from their encoding in the operand */ 559 i = htonl (sprp->spr_val); 560 spr = ((i >> 5) & 0x1f) | ((i & 0x1f) << 5); 561 562 return spr; 563 } 564 } 565 566 return 0; 567 } /* spr_value */ 568 569 571 572 /*====================================================================== 573 * Convert the 'tbr' operand from its numeric value to its symbolic name. 574 * 575 * Arguments: 576 * value The value of the 'tbr' operand. This value should 577 * be unmodified from its encoding in the instruction. 578 * the split-field computations will be performed 579 * here before the switch. 580 * 581 * Returns the address of a character array containing the name of the 582 * time base register defined by the 'value' parameter, or the address 583 * of a character array containing "???" if no match was found. 584 */ 585 586 char *tbr_name (int value) 587 { 588 unsigned short tbr; 589 590 /*------------------------------------------------------------*/ 591 592 /* tbr is a 10 bit field whose interpretation has the high and low 593 five-bit fields reversed from their encoding in the operand */ 594 595 tbr = ((value >> 5) & 0x1f) | ((value & 0x1f) << 5); 596 597 if (tbr == 268) 598 return "TBL"; 599 600 else if (tbr == 269) 601 return "TBU"; 602 603 604 return "???"; 605 } /* tbr_name */ 606 607 609 610 /*====================================================================== 611 * Convert the 'tbr' operand from its symbolic name to its numeric value. 612 * 613 * Arguments: 614 * name The symbolic name of the 'tbr' operand. The 615 * split-field encoding will be done by this routine. 616 * 617 * Returns the numeric value for the spr appropriate for encoding a machine 618 * instruction. Returns 0 if unable to find the TBR. 619 */ 620 621 int tbr_value (char *name) 622 { 623 int tbr; 624 int val; 625 626 /*------------------------------------------------------------*/ 627 628 if (!name || !*name) 629 return 0; 630 631 downstring (name); 632 633 if (isdigit ((int) name[0])) { 634 val = read_number (name); 635 636 if (val != 268 && val != 269) 637 return 0; 638 } else if (strcmp (name, "tbl") == 0) 639 val = 268; 640 else if (strcmp (name, "tbu") == 0) 641 val = 269; 642 else 643 return 0; 644 645 /* tbr is a 10 bit field whose interpretation has the high and low 646 five-bit fields reversed from their encoding in the operand */ 647 648 val = htonl (val); 649 tbr = ((val >> 5) & 0x1f) | ((val & 0x1f) << 5); 650 return tbr; 651 } /* tbr_name */ 652 653 655 656 /*====================================================================== 657 * The next several functions (handle_xxx) are the routines that handle 658 * disassembling the opcodes with simplified mnemonics. 659 * 660 * Arguments: 661 * ctx A pointer to the disassembler context record. 662 * 663 * Returns true if the simpler form was printed or false if it was not. 664 */ 665 666 int handle_bc (struct ppc_ctx *ctx) 667 { 668 unsigned long bo; 669 unsigned long bi; 670 static struct opcode blt = { B_OPCODE (16, 0, 0), B_MASK, {O_BD, 0}, 671 0, "blt", H_RELATIVE 672 }; 673 static struct opcode bne = 674 { B_OPCODE (16, 0, 0), B_MASK, {O_cr2, O_BD, 0}, 675 0, "bne", H_RELATIVE 676 }; 677 static struct opcode bdnz = { B_OPCODE (16, 0, 0), B_MASK, {O_BD, 0}, 678 0, "bdnz", H_RELATIVE 679 }; 680 681 /*------------------------------------------------------------*/ 682 683 if (get_operand_value(ctx->op, ctx->instr, O_BO, &bo) == false) 684 return false; 685 686 if (get_operand_value(ctx->op, ctx->instr, O_BI, &bi) == false) 687 return false; 688 689 if ((bo == 12) && (bi == 0)) { 690 ctx->op = &blt; 691 sprintf (&ctx->data[ctx->datalen], "%-7s ", ctx->op->name); 692 ctx->datalen += 8; 693 print_operands (ctx); 694 return true; 695 } else if ((bo == 4) && (bi == 10)) { 696 ctx->op = =⃥ 697 sprintf (&ctx->data[ctx->datalen], "%-7s ", ctx->op->name); 698 ctx->datalen += 8; 699 print_operands (ctx); 700 return true; 701 } else if ((bo == 16) && (bi == 0)) { 702 ctx->op = &bdnz; 703 sprintf (&ctx->data[ctx->datalen], "%-7s ", ctx->op->name); 704 ctx->datalen += 8; 705 print_operands (ctx); 706 return true; 707 } 708 709 return false; 710 } /* handle_blt */ 711 712 714 715 /*====================================================================== 716 * Outputs source line information for the disassembler. This should 717 * be modified in the future to lookup the actual line of source code 718 * from the file, but for now this will do. 719 * 720 * Arguments: 721 * filename The address of a character array containing the 722 * absolute path and file name of the source file. 723 * 724 * funcname The address of a character array containing the 725 * name of the function (not C++ demangled (yet)) 726 * to which this code belongs. 727 * 728 * line_no An integer specifying the source line number that 729 * generated this code. 730 * 731 * pfunc The address of a function to call to print the output. 732 * 733 * 734 * Returns true if it was able to output the line info, or false if it was 735 * not. 736 */ 737 738 int print_source_line (char *filename, char *funcname, 739 int line_no, int (*pfunc) (const char *)) 740 { 741 char out_buf[256]; 742 743 /*------------------------------------------------------------*/ 744 745 (*pfunc) (""); /* output a newline */ 746 sprintf (out_buf, "%s %s(): line %d", filename, funcname, line_no); 747 (*pfunc) (out_buf); 748 749 return true; 750 } /* print_source_line */ 751 752 754 755 /*====================================================================== 756 * Entry point for the PPC assembler. 757 * 758 * Arguments: 759 * asm_buf An array of characters containing the assembly opcode 760 * and operands to convert to a POWERPC machine 761 * instruction. 762 * 763 * Returns the machine instruction or zero. 764 */ 765 766 unsigned long asmppc (unsigned long memaddr, char *asm_buf, int *err) 767 { 768 struct opcode *opc; 769 struct operand *oper[MAX_OPERANDS]; 770 unsigned long instr; 771 unsigned long param; 772 char *ptr = asm_buf; 773 char scratch[20]; 774 int i; 775 int w_operands = 0; /* wanted # of operands */ 776 int n_operands = 0; /* # of operands read */ 777 int asm_debug = 0; 778 779 /*------------------------------------------------------------*/ 780 781 if (err) 782 *err = 0; 783 784 if (get_word (&ptr, scratch) == 0) 785 return 0; 786 787 /* Lookup the opcode structure based on the opcode name */ 788 if ((opc = find_opcode_by_name (scratch)) == (struct opcode *) 0) { 789 if (err) 790 *err = E_ASM_BAD_OPCODE; 791 return 0; 792 } 793 794 if (asm_debug) { 795 printf ("asmppc: Opcode = \"%s\"\n", opc->name); 796 } 797 798 for (i = 0; i < 8; ++i) { 799 if (opc->fields[i] == 0) 800 break; 801 ++w_operands; 802 } 803 804 if (asm_debug) { 805 printf ("asmppc: Expecting %d operands\n", w_operands); 806 } 807 808 instr = opc->opcode; 809 810 /* read each operand */ 811 while (n_operands < w_operands) { 812 813 oper[n_operands] = &operands[opc->fields[n_operands] - 1]; 814 815 if (oper[n_operands]->hint & OH_SILENT) { 816 /* Skip silent operands, they are covered in opc->opcode */ 817 818 if (asm_debug) { 819 printf ("asmppc: Operand %d \"%s\" SILENT\n", n_operands, 820 oper[n_operands]->name); 821 } 822 823 ++n_operands; 824 continue; 825 } 826 827 if (get_word (&ptr, scratch) == 0) 828 break; 829 830 if (asm_debug) { 831 printf ("asmppc: Operand %d \"%s\" : \"%s\"\n", n_operands, 832 oper[n_operands]->name, scratch); 833 } 834 835 if ((param = parse_operand (memaddr, opc, oper[n_operands], 836 scratch, err)) == -1) 837 return 0; 838 839 instr |= param; 840 ++n_operands; 841 } 842 843 if (n_operands < w_operands) { 844 if (err) 845 *err = E_ASM_NUM_OPERANDS; 846 return 0; 847 } 848 849 if (asm_debug) { 850 printf ("asmppc: Instruction = 0x%08lx\n", instr); 851 } 852 853 return instr; 854 } /* asmppc */ 855 856 858 859 /*====================================================================== 860 * Called by the assembler to interpret a single operand 861 * 862 * Arguments: 863 * ctx A pointer to the disassembler context record. 864 * 865 * Returns 0 if the operand is ok, or -1 if it is bad. 866 */ 867 868 int parse_operand (unsigned long memaddr, struct opcode *opc, 869 struct operand *oper, char *txt, int *err) 870 { 871 long data; 872 long mask; 873 int is_neg = 0; 874 875 /*------------------------------------------------------------*/ 876 877 mask = (1 << oper->bits) - 1; 878 879 if (oper->hint & OH_ADDR) { 880 data = read_number (txt); 881 882 if (opc->hint & H_RELATIVE) 883 data = data - memaddr; 884 885 if (data < 0) 886 is_neg = 1; 887 888 data >>= 2; 889 data &= (mask >> 1); 890 891 if (is_neg) 892 data |= 1 << (oper->bits - 1); 893 } 894 895 else if (oper->hint & OH_REG) { 896 if (txt[0] == 'r' || txt[0] == 'R') 897 txt++; 898 else if (txt[0] == '%' && (txt[1] == 'r' || txt[1] == 'R')) 899 txt += 2; 900 901 data = read_number (txt); 902 if (data > 31) { 903 if (err) 904 *err = E_ASM_BAD_REGISTER; 905 return -1; 906 } 907 908 data = htonl (data); 909 } 910 911 else if (oper->hint & OH_SPR) { 912 if ((data = spr_value (txt)) == 0) { 913 if (err) 914 *err = E_ASM_BAD_SPR; 915 return -1; 916 } 917 } 918 919 else if (oper->hint & OH_TBR) { 920 if ((data = tbr_value (txt)) == 0) { 921 if (err) 922 *err = E_ASM_BAD_TBR; 923 return -1; 924 } 925 } 926 927 else { 928 data = htonl (read_number (txt)); 929 } 930 931 return (data & mask) << oper->shift; 932 } /* parse_operand */ 933 934 935 char *asm_error_str (int err) 936 { 937 switch (err) { 938 case E_ASM_BAD_OPCODE: 939 return "Bad opcode"; 940 case E_ASM_NUM_OPERANDS: 941 return "Bad number of operands"; 942 case E_ASM_BAD_REGISTER: 943 return "Bad register number"; 944 case E_ASM_BAD_SPR: 945 return "Bad SPR name or number"; 946 case E_ASM_BAD_TBR: 947 return "Bad TBR name or number"; 948 } 949 950 return ""; 951 } /* asm_error_str */ 952 953 955 956 /*====================================================================== 957 * Copy a word from one buffer to another, ignores leading white spaces. 958 * 959 * Arguments: 960 * src The address of a character pointer to the 961 * source buffer. 962 * dest A pointer to a character buffer to write the word 963 * into. 964 * 965 * Returns the number of non-white space characters copied, or zero. 966 */ 967 968 int get_word (char **src, char *dest) 969 { 970 char *ptr = *src; 971 int nchars = 0; 972 973 /*------------------------------------------------------------*/ 974 975 /* Eat white spaces */ 976 while (*ptr && isblank (*ptr)) 977 ptr++; 978 979 if (*ptr == 0) { 980 *src = ptr; 981 return 0; 982 } 983 984 /* Find the text of the word */ 985 while (*ptr && !isblank (*ptr) && (*ptr != ',')) 986 dest[nchars++] = *ptr++; 987 ptr = (*ptr == ',') ? ptr + 1 : ptr; 988 dest[nchars] = 0; 989 990 *src = ptr; 991 return nchars; 992 } /* get_word */ 993 994 996 997 /*====================================================================== 998 * Convert a numeric string to a number, be aware of base notations. 999 * 1000 * Arguments: 1001 * txt The numeric string. 1002 * 1003 * Returns the converted numeric value. 1004 */ 1005 1006 long read_number (char *txt) 1007 { 1008 long val; 1009 int is_neg = 0; 1010 1011 /*------------------------------------------------------------*/ 1012 1013 if (txt == 0 || *txt == 0) 1014 return 0; 1015 1016 if (*txt == '-') { 1017 is_neg = 1; 1018 ++txt; 1019 } 1020 1021 if (txt[0] == '0' && (txt[1] == 'x' || txt[1] == 'X')) /* hex */ 1022 val = simple_strtoul (&txt[2], NULL, 16); 1023 else /* decimal */ 1024 val = simple_strtoul (txt, NULL, 10); 1025 1026 if (is_neg) 1027 val = -val; 1028 1029 return val; 1030 } /* read_number */ 1031 1032 1033 int downstring (char *s) 1034 { 1035 if (!s || !*s) 1036 return 0; 1037 1038 while (*s) { 1039 if (isupper (*s)) 1040 *s = tolower (*s); 1041 s++; 1042 } 1043 1044 return 0; 1045 } /* downstring */ 1046 1047 1049 1050 /*====================================================================== 1051 * Examines the instruction at the current address and determines the 1052 * next address to be executed. This will take into account branches 1053 * of different types so that a "step" and "next" operations can be 1054 * supported. 1055 * 1056 * Arguments: 1057 * nextaddr The address (to be filled in) of the next 1058 * instruction to execute. This will only be a valid 1059 * address if true is returned. 1060 * 1061 * step_over A flag indicating how to compute addresses for 1062 * branch statements: 1063 * true = Step over the branch (next) 1064 * false = step into the branch (step) 1065 * 1066 * Returns true if it was able to compute the address. Returns false if 1067 * it has a problem reading the current instruction or one of the registers. 1068 */ 1069 1070 int find_next_address (unsigned char *nextaddr, int step_over, 1071 struct pt_regs *regs) 1072 { 1073 unsigned long pc; /* SRR0 register from PPC */ 1074 unsigned long ctr; /* CTR register from PPC */ 1075 unsigned long cr; /* CR register from PPC */ 1076 unsigned long lr; /* LR register from PPC */ 1077 unsigned long instr; /* instruction at SRR0 */ 1078 unsigned long next; /* computed instruction for 'next' */ 1079 unsigned long step; /* computed instruction for 'step' */ 1080 unsigned long addr = 0; /* target address operand */ 1081 unsigned long aa = 0; /* AA operand */ 1082 unsigned long lk = 0; /* LK operand */ 1083 unsigned long bo = 0; /* BO operand */ 1084 unsigned long bi = 0; /* BI operand */ 1085 struct opcode *op = 0; /* opcode structure for 'instr' */ 1086 int ctr_ok = 0; 1087 int cond_ok = 0; 1088 int conditional = 0; 1089 int branch = 0; 1090 1091 /*------------------------------------------------------------*/ 1092 1093 if (nextaddr == 0 || regs == 0) { 1094 printf ("find_next_address: bad args"); 1095 return false; 1096 } 1097 1098 pc = regs->nip & 0xfffffffc; 1099 instr = INSTRUCTION (pc); 1100 1101 if ((op = find_opcode (instr)) == (struct opcode *) 0) { 1102 printf ("find_next_address: can't parse opcode 0x%lx", instr); 1103 return false; 1104 } 1105 1106 ctr = regs->ctr; 1107 cr = regs->ccr; 1108 lr = regs->link; 1109 1110 switch (op->opcode) { 1111 case B_OPCODE (16, 0, 0): /* bc */ 1112 case B_OPCODE (16, 0, 1): /* bcl */ 1113 case B_OPCODE (16, 1, 0): /* bca */ 1114 case B_OPCODE (16, 1, 1): /* bcla */ 1115 if (!get_operand_value (op, instr, O_BD, &addr) || 1116 !get_operand_value (op, instr, O_BO, &bo) || 1117 !get_operand_value (op, instr, O_BI, &bi) || 1118 !get_operand_value (op, instr, O_AA, &aa) || 1119 !get_operand_value (op, instr, O_LK, &lk)) 1120 return false; 1121 1122 if ((addr & (1 << 13)) != 0) 1123 addr = addr - (1 << 14); 1124 addr <<= 2; 1125 conditional = 1; 1126 branch = 1; 1127 break; 1128 1129 case I_OPCODE (18, 0, 0): /* b */ 1130 case I_OPCODE (18, 0, 1): /* bl */ 1131 case I_OPCODE (18, 1, 0): /* ba */ 1132 case I_OPCODE (18, 1, 1): /* bla */ 1133 if (!get_operand_value (op, instr, O_LI, &addr) || 1134 !get_operand_value (op, instr, O_AA, &aa) || 1135 !get_operand_value (op, instr, O_LK, &lk)) 1136 return false; 1137 1138 if ((addr & (1 << 23)) != 0) 1139 addr = addr - (1 << 24); 1140 addr <<= 2; 1141 conditional = 0; 1142 branch = 1; 1143 break; 1144 1145 case XL_OPCODE (19, 528, 0): /* bcctr */ 1146 case XL_OPCODE (19, 528, 1): /* bcctrl */ 1147 if (!get_operand_value (op, instr, O_BO, &bo) || 1148 !get_operand_value (op, instr, O_BI, &bi) || 1149 !get_operand_value (op, instr, O_LK, &lk)) 1150 return false; 1151 1152 addr = ctr; 1153 aa = 1; 1154 conditional = 1; 1155 branch = 1; 1156 break; 1157 1158 case XL_OPCODE (19, 16, 0): /* bclr */ 1159 case XL_OPCODE (19, 16, 1): /* bclrl */ 1160 if (!get_operand_value (op, instr, O_BO, &bo) || 1161 !get_operand_value (op, instr, O_BI, &bi) || 1162 !get_operand_value (op, instr, O_LK, &lk)) 1163 return false; 1164 1165 addr = lr; 1166 aa = 1; 1167 conditional = 1; 1168 branch = 1; 1169 break; 1170 1171 default: 1172 conditional = 0; 1173 branch = 0; 1174 break; 1175 } 1176 1177 if (conditional) { 1178 switch ((bo & 0x1e) >> 1) { 1179 case 0: /* 0000y */ 1180 if (--ctr != 0) 1181 ctr_ok = 1; 1182 1183 cond_ok = !(cr & (1 << (31 - bi))); 1184 break; 1185 1186 case 1: /* 0001y */ 1187 if (--ctr == 0) 1188 ctr_ok = 1; 1189 1190 cond_ok = !(cr & (1 << (31 - bi))); 1191 break; 1192 1193 case 2: /* 001zy */ 1194 ctr_ok = 1; 1195 cond_ok = !(cr & (1 << (31 - bi))); 1196 break; 1197 1198 case 4: /* 0100y */ 1199 if (--ctr != 0) 1200 ctr_ok = 1; 1201 1202 cond_ok = cr & (1 << (31 - bi)); 1203 break; 1204 1205 case 5: /* 0101y */ 1206 if (--ctr == 0) 1207 ctr_ok = 1; 1208 1209 cond_ok = cr & (1 << (31 - bi)); 1210 break; 1211 1212 case 6: /* 011zy */ 1213 ctr_ok = 1; 1214 cond_ok = cr & (1 << (31 - bi)); 1215 break; 1216 1217 case 8: /* 1z00y */ 1218 if (--ctr != 0) 1219 ctr_ok = cond_ok = 1; 1220 break; 1221 1222 case 9: /* 1z01y */ 1223 if (--ctr == 0) 1224 ctr_ok = cond_ok = 1; 1225 break; 1226 1227 case 10: /* 1z1zz */ 1228 ctr_ok = cond_ok = 1; 1229 break; 1230 } 1231 } 1232 1233 if (branch && (!conditional || (ctr_ok && cond_ok))) { 1234 if (aa) 1235 step = addr; 1236 else 1237 step = addr + pc; 1238 1239 if (lk) 1240 next = pc + 4; 1241 else 1242 next = step; 1243 } else { 1244 step = next = pc + 4; 1245 } 1246 1247 if (step_over == true) 1248 *(unsigned long *) nextaddr = next; 1249 else 1250 *(unsigned long *) nextaddr = step; 1251 1252 return true; 1253 } /* find_next_address */ 1254 1255 1256 /* 1257 * Copyright (c) 2000 William L. Pitts and W. Gerald Hicks 1258 * All rights reserved. 1259 * 1260 * Redistribution and use in source and binary forms are freely 1261 * permitted provided that the above copyright notice and this 1262 * paragraph and the following disclaimer are duplicated in all 1263 * such forms. 1264 * 1265 * This software is provided "AS IS" and without any express or 1266 * implied warranties, including, without limitation, the implied 1267 * warranties of merchantability and fitness for a particular 1268 * purpose. 1269 */ 1270