bedbug.c 30 KB

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