instructions.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. static struct ins_ops *powerpc__associate_instruction_ops(struct arch *arch, const char *name)
  4. {
  5. int i;
  6. struct ins_ops *ops;
  7. /*
  8. * - Interested only if instruction starts with 'b'.
  9. * - Few start with 'b', but aren't branch instructions.
  10. */
  11. if (name[0] != 'b' ||
  12. !strncmp(name, "bcd", 3) ||
  13. !strncmp(name, "brinc", 5) ||
  14. !strncmp(name, "bper", 4))
  15. return NULL;
  16. ops = &jump_ops;
  17. i = strlen(name) - 1;
  18. if (i < 0)
  19. return NULL;
  20. /* ignore optional hints at the end of the instructions */
  21. if (name[i] == '+' || name[i] == '-')
  22. i--;
  23. if (name[i] == 'l' || (name[i] == 'a' && name[i-1] == 'l')) {
  24. /*
  25. * if the instruction ends up with 'l' or 'la', then
  26. * those are considered 'calls' since they update LR.
  27. * ... except for 'bnl' which is branch if not less than
  28. * and the absolute form of the same.
  29. */
  30. if (strcmp(name, "bnl") && strcmp(name, "bnl+") &&
  31. strcmp(name, "bnl-") && strcmp(name, "bnla") &&
  32. strcmp(name, "bnla+") && strcmp(name, "bnla-"))
  33. ops = &call_ops;
  34. }
  35. if (name[i] == 'r' && name[i-1] == 'l')
  36. /*
  37. * instructions ending with 'lr' are considered to be
  38. * return instructions
  39. */
  40. ops = &ret_ops;
  41. arch__associate_ins_ops(arch, name, ops);
  42. return ops;
  43. }
  44. #define PPC_OP(op) (((op) >> 26) & 0x3F)
  45. #define PPC_21_30(R) (((R) >> 1) & 0x3ff)
  46. #define PPC_22_30(R) (((R) >> 1) & 0x1ff)
  47. struct insn_offset {
  48. const char *name;
  49. int value;
  50. };
  51. /*
  52. * There are memory instructions with opcode 31 which are
  53. * of X Form, Example:
  54. * ldx RT,RA,RB
  55. * ______________________________________
  56. * | 31 | RT | RA | RB | 21 |/|
  57. * --------------------------------------
  58. * 0 6 11 16 21 30 31
  59. *
  60. * But all instructions with opcode 31 are not memory.
  61. * Example: add RT,RA,RB
  62. *
  63. * Use bits 21 to 30 to check memory insns with 31 as opcode.
  64. * In ins_array below, for ldx instruction:
  65. * name => OP_31_XOP_LDX
  66. * value => 21
  67. */
  68. static struct insn_offset ins_array[] = {
  69. { .name = "OP_31_XOP_LXSIWZX", .value = 12, },
  70. { .name = "OP_31_XOP_LWARX", .value = 20, },
  71. { .name = "OP_31_XOP_LDX", .value = 21, },
  72. { .name = "OP_31_XOP_LWZX", .value = 23, },
  73. { .name = "OP_31_XOP_LDUX", .value = 53, },
  74. { .name = "OP_31_XOP_LWZUX", .value = 55, },
  75. { .name = "OP_31_XOP_LXSIWAX", .value = 76, },
  76. { .name = "OP_31_XOP_LDARX", .value = 84, },
  77. { .name = "OP_31_XOP_LBZX", .value = 87, },
  78. { .name = "OP_31_XOP_LVX", .value = 103, },
  79. { .name = "OP_31_XOP_LBZUX", .value = 119, },
  80. { .name = "OP_31_XOP_STXSIWX", .value = 140, },
  81. { .name = "OP_31_XOP_STDX", .value = 149, },
  82. { .name = "OP_31_XOP_STWX", .value = 151, },
  83. { .name = "OP_31_XOP_STDUX", .value = 181, },
  84. { .name = "OP_31_XOP_STWUX", .value = 183, },
  85. { .name = "OP_31_XOP_STBX", .value = 215, },
  86. { .name = "OP_31_XOP_STVX", .value = 231, },
  87. { .name = "OP_31_XOP_STBUX", .value = 247, },
  88. { .name = "OP_31_XOP_LHZX", .value = 279, },
  89. { .name = "OP_31_XOP_LHZUX", .value = 311, },
  90. { .name = "OP_31_XOP_LXVDSX", .value = 332, },
  91. { .name = "OP_31_XOP_LWAX", .value = 341, },
  92. { .name = "OP_31_XOP_LHAX", .value = 343, },
  93. { .name = "OP_31_XOP_LWAUX", .value = 373, },
  94. { .name = "OP_31_XOP_LHAUX", .value = 375, },
  95. { .name = "OP_31_XOP_STHX", .value = 407, },
  96. { .name = "OP_31_XOP_STHUX", .value = 439, },
  97. { .name = "OP_31_XOP_LXSSPX", .value = 524, },
  98. { .name = "OP_31_XOP_LDBRX", .value = 532, },
  99. { .name = "OP_31_XOP_LSWX", .value = 533, },
  100. { .name = "OP_31_XOP_LWBRX", .value = 534, },
  101. { .name = "OP_31_XOP_LFSUX", .value = 567, },
  102. { .name = "OP_31_XOP_LXSDX", .value = 588, },
  103. { .name = "OP_31_XOP_LSWI", .value = 597, },
  104. { .name = "OP_31_XOP_LFDX", .value = 599, },
  105. { .name = "OP_31_XOP_LFDUX", .value = 631, },
  106. { .name = "OP_31_XOP_STXSSPX", .value = 652, },
  107. { .name = "OP_31_XOP_STDBRX", .value = 660, },
  108. { .name = "OP_31_XOP_STXWX", .value = 661, },
  109. { .name = "OP_31_XOP_STWBRX", .value = 662, },
  110. { .name = "OP_31_XOP_STFSX", .value = 663, },
  111. { .name = "OP_31_XOP_STFSUX", .value = 695, },
  112. { .name = "OP_31_XOP_STXSDX", .value = 716, },
  113. { .name = "OP_31_XOP_STSWI", .value = 725, },
  114. { .name = "OP_31_XOP_STFDX", .value = 727, },
  115. { .name = "OP_31_XOP_STFDUX", .value = 759, },
  116. { .name = "OP_31_XOP_LXVW4X", .value = 780, },
  117. { .name = "OP_31_XOP_LHBRX", .value = 790, },
  118. { .name = "OP_31_XOP_LXVD2X", .value = 844, },
  119. { .name = "OP_31_XOP_LFIWAX", .value = 855, },
  120. { .name = "OP_31_XOP_LFIWZX", .value = 887, },
  121. { .name = "OP_31_XOP_STXVW4X", .value = 908, },
  122. { .name = "OP_31_XOP_STHBRX", .value = 918, },
  123. { .name = "OP_31_XOP_STXVD2X", .value = 972, },
  124. { .name = "OP_31_XOP_STFIWX", .value = 983, },
  125. };
  126. /*
  127. * Arithmetic instructions which are having opcode as 31.
  128. * These instructions are tracked to save the register state
  129. * changes. Example:
  130. *
  131. * lwz r10,264(r3)
  132. * add r31, r3, r3
  133. * lwz r9, 0(r31)
  134. *
  135. * Here instruction tracking needs to identify the "add"
  136. * instruction and save data type of r3 to r31. If a sample
  137. * is hit at next "lwz r9, 0(r31)", by this instruction tracking,
  138. * data type of r31 can be resolved.
  139. */
  140. static struct insn_offset arithmetic_ins_op_31[] = {
  141. { .name = "SUB_CARRY_XO_FORM", .value = 8, },
  142. { .name = "MUL_HDW_XO_FORM1", .value = 9, },
  143. { .name = "ADD_CARRY_XO_FORM", .value = 10, },
  144. { .name = "MUL_HW_XO_FORM1", .value = 11, },
  145. { .name = "SUB_XO_FORM", .value = 40, },
  146. { .name = "MUL_HDW_XO_FORM", .value = 73, },
  147. { .name = "MUL_HW_XO_FORM", .value = 75, },
  148. { .name = "SUB_EXT_XO_FORM", .value = 136, },
  149. { .name = "ADD_EXT_XO_FORM", .value = 138, },
  150. { .name = "SUB_ZERO_EXT_XO_FORM", .value = 200, },
  151. { .name = "ADD_ZERO_EXT_XO_FORM", .value = 202, },
  152. { .name = "SUB_EXT_XO_FORM2", .value = 232, },
  153. { .name = "MUL_DW_XO_FORM", .value = 233, },
  154. { .name = "ADD_EXT_XO_FORM2", .value = 234, },
  155. { .name = "MUL_W_XO_FORM", .value = 235, },
  156. { .name = "ADD_XO_FORM", .value = 266, },
  157. { .name = "DIV_DW_XO_FORM1", .value = 457, },
  158. { .name = "DIV_W_XO_FORM1", .value = 459, },
  159. { .name = "DIV_DW_XO_FORM", .value = 489, },
  160. { .name = "DIV_W_XO_FORM", .value = 491, },
  161. };
  162. static struct insn_offset arithmetic_two_ops[] = {
  163. { .name = "mulli", .value = 7, },
  164. { .name = "subfic", .value = 8, },
  165. { .name = "addic", .value = 12, },
  166. { .name = "addic.", .value = 13, },
  167. { .name = "addi", .value = 14, },
  168. { .name = "addis", .value = 15, },
  169. };
  170. static int cmp_offset(const void *a, const void *b)
  171. {
  172. const struct insn_offset *val1 = a;
  173. const struct insn_offset *val2 = b;
  174. return (val1->value - val2->value);
  175. }
  176. static struct ins_ops *check_ppc_insn(struct disasm_line *dl)
  177. {
  178. int raw_insn = dl->raw.raw_insn;
  179. int opcode = PPC_OP(raw_insn);
  180. int mem_insn_31 = PPC_21_30(raw_insn);
  181. struct insn_offset *ret;
  182. struct insn_offset mem_insns_31_opcode = {
  183. "OP_31_INSN",
  184. mem_insn_31
  185. };
  186. char name_insn[32];
  187. /*
  188. * Instructions with opcode 32 to 63 are memory
  189. * instructions in powerpc
  190. */
  191. if ((opcode & 0x20)) {
  192. /*
  193. * Set name in case of raw instruction to
  194. * opcode to be used in insn-stat
  195. */
  196. if (!strlen(dl->ins.name)) {
  197. sprintf(name_insn, "%d", opcode);
  198. dl->ins.name = strdup(name_insn);
  199. }
  200. return &load_store_ops;
  201. } else if (opcode == 31) {
  202. /* Check for memory instructions with opcode 31 */
  203. ret = bsearch(&mem_insns_31_opcode, ins_array, ARRAY_SIZE(ins_array), sizeof(ins_array[0]), cmp_offset);
  204. if (ret) {
  205. if (!strlen(dl->ins.name))
  206. dl->ins.name = strdup(ret->name);
  207. return &load_store_ops;
  208. } else {
  209. mem_insns_31_opcode.value = PPC_22_30(raw_insn);
  210. ret = bsearch(&mem_insns_31_opcode, arithmetic_ins_op_31, ARRAY_SIZE(arithmetic_ins_op_31),
  211. sizeof(arithmetic_ins_op_31[0]), cmp_offset);
  212. if (ret != NULL)
  213. return &arithmetic_ops;
  214. /* Bits 21 to 30 has value 444 for "mr" insn ie, OR X form */
  215. if (PPC_21_30(raw_insn) == 444)
  216. return &arithmetic_ops;
  217. }
  218. } else {
  219. mem_insns_31_opcode.value = opcode;
  220. ret = bsearch(&mem_insns_31_opcode, arithmetic_two_ops, ARRAY_SIZE(arithmetic_two_ops),
  221. sizeof(arithmetic_two_ops[0]), cmp_offset);
  222. if (ret != NULL)
  223. return &arithmetic_ops;
  224. }
  225. return NULL;
  226. }
  227. /*
  228. * Instruction tracking function to track register state moves.
  229. * Example sequence:
  230. * ld r10,264(r3)
  231. * mr r31,r3
  232. * <<after some sequence>
  233. * ld r9,312(r31)
  234. *
  235. * Previous instruction sequence shows that register state of r3
  236. * is moved to r31. update_insn_state_powerpc tracks these state
  237. * changes
  238. */
  239. #ifdef HAVE_DWARF_SUPPORT
  240. static void update_insn_state_powerpc(struct type_state *state,
  241. struct data_loc_info *dloc, Dwarf_Die * cu_die __maybe_unused,
  242. struct disasm_line *dl)
  243. {
  244. struct annotated_insn_loc loc;
  245. struct annotated_op_loc *src = &loc.ops[INSN_OP_SOURCE];
  246. struct annotated_op_loc *dst = &loc.ops[INSN_OP_TARGET];
  247. struct type_state_reg *tsr;
  248. u32 insn_offset = dl->al.offset;
  249. if (annotate_get_insn_location(dloc->arch, dl, &loc) < 0)
  250. return;
  251. /*
  252. * Value 444 for bits 21:30 is for "mr"
  253. * instruction. "mr" is extended OR. So set the
  254. * source and destination reg correctly
  255. */
  256. if (PPC_21_30(dl->raw.raw_insn) == 444) {
  257. int src_reg = src->reg1;
  258. src->reg1 = dst->reg1;
  259. dst->reg1 = src_reg;
  260. }
  261. if (!has_reg_type(state, dst->reg1))
  262. return;
  263. tsr = &state->regs[dst->reg1];
  264. if (!has_reg_type(state, src->reg1) ||
  265. !state->regs[src->reg1].ok) {
  266. tsr->ok = false;
  267. return;
  268. }
  269. tsr->type = state->regs[src->reg1].type;
  270. tsr->kind = state->regs[src->reg1].kind;
  271. tsr->ok = true;
  272. pr_debug_dtp("mov [%x] reg%d -> reg%d",
  273. insn_offset, src->reg1, dst->reg1);
  274. pr_debug_type_name(&tsr->type, tsr->kind);
  275. }
  276. #endif /* HAVE_DWARF_SUPPORT */
  277. static int powerpc__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
  278. {
  279. if (!arch->initialized) {
  280. arch->initialized = true;
  281. arch->associate_instruction_ops = powerpc__associate_instruction_ops;
  282. arch->objdump.comment_char = '#';
  283. annotate_opts.show_asm_raw = true;
  284. }
  285. return 0;
  286. }