backtrace.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <asm/backtrace.h>
  17. #include <asm/opcode-tile.h>
  18. #include <arch/abi.h>
  19. #ifdef __tilegx__
  20. #define tile_bundle_bits tilegx_bundle_bits
  21. #define TILE_MAX_INSTRUCTIONS_PER_BUNDLE TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE
  22. #define TILE_BUNDLE_ALIGNMENT_IN_BYTES TILEGX_BUNDLE_ALIGNMENT_IN_BYTES
  23. #define tile_decoded_instruction tilegx_decoded_instruction
  24. #define tile_mnemonic tilegx_mnemonic
  25. #define parse_insn_tile parse_insn_tilegx
  26. #define TILE_OPC_IRET TILEGX_OPC_IRET
  27. #define TILE_OPC_ADDI TILEGX_OPC_ADDI
  28. #define TILE_OPC_ADDLI TILEGX_OPC_ADDLI
  29. #define TILE_OPC_INFO TILEGX_OPC_INFO
  30. #define TILE_OPC_INFOL TILEGX_OPC_INFOL
  31. #define TILE_OPC_JRP TILEGX_OPC_JRP
  32. #define TILE_OPC_MOVE TILEGX_OPC_MOVE
  33. #define OPCODE_STORE TILEGX_OPC_ST
  34. typedef long long bt_int_reg_t;
  35. #else
  36. #define OPCODE_STORE TILE_OPC_SW
  37. typedef int bt_int_reg_t;
  38. #endif
  39. /* A decoded bundle used for backtracer analysis. */
  40. struct BacktraceBundle {
  41. tile_bundle_bits bits;
  42. int num_insns;
  43. struct tile_decoded_instruction
  44. insns[TILE_MAX_INSTRUCTIONS_PER_BUNDLE];
  45. };
  46. /* Locates an instruction inside the given bundle that
  47. * has the specified mnemonic, and whose first 'num_operands_to_match'
  48. * operands exactly match those in 'operand_values'.
  49. */
  50. static const struct tile_decoded_instruction *find_matching_insn(
  51. const struct BacktraceBundle *bundle,
  52. tile_mnemonic mnemonic,
  53. const int *operand_values,
  54. int num_operands_to_match)
  55. {
  56. int i, j;
  57. bool match;
  58. for (i = 0; i < bundle->num_insns; i++) {
  59. const struct tile_decoded_instruction *insn =
  60. &bundle->insns[i];
  61. if (insn->opcode->mnemonic != mnemonic)
  62. continue;
  63. match = true;
  64. for (j = 0; j < num_operands_to_match; j++) {
  65. if (operand_values[j] != insn->operand_values[j]) {
  66. match = false;
  67. break;
  68. }
  69. }
  70. if (match)
  71. return insn;
  72. }
  73. return NULL;
  74. }
  75. /* Does this bundle contain an 'iret' instruction? */
  76. static inline bool bt_has_iret(const struct BacktraceBundle *bundle)
  77. {
  78. return find_matching_insn(bundle, TILE_OPC_IRET, NULL, 0) != NULL;
  79. }
  80. /* Does this bundle contain an 'addi sp, sp, OFFSET' or
  81. * 'addli sp, sp, OFFSET' instruction, and if so, what is OFFSET?
  82. */
  83. static bool bt_has_addi_sp(const struct BacktraceBundle *bundle, int *adjust)
  84. {
  85. static const int vals[2] = { TREG_SP, TREG_SP };
  86. const struct tile_decoded_instruction *insn =
  87. find_matching_insn(bundle, TILE_OPC_ADDI, vals, 2);
  88. if (insn == NULL)
  89. insn = find_matching_insn(bundle, TILE_OPC_ADDLI, vals, 2);
  90. #ifdef __tilegx__
  91. if (insn == NULL)
  92. insn = find_matching_insn(bundle, TILEGX_OPC_ADDXLI, vals, 2);
  93. if (insn == NULL)
  94. insn = find_matching_insn(bundle, TILEGX_OPC_ADDXI, vals, 2);
  95. #endif
  96. if (insn == NULL)
  97. return false;
  98. *adjust = insn->operand_values[2];
  99. return true;
  100. }
  101. /* Does this bundle contain any 'info OP' or 'infol OP'
  102. * instruction, and if so, what are their OP? Note that OP is interpreted
  103. * as an unsigned value by this code since that's what the caller wants.
  104. * Returns the number of info ops found.
  105. */
  106. static int bt_get_info_ops(const struct BacktraceBundle *bundle,
  107. int operands[MAX_INFO_OPS_PER_BUNDLE])
  108. {
  109. int num_ops = 0;
  110. int i;
  111. for (i = 0; i < bundle->num_insns; i++) {
  112. const struct tile_decoded_instruction *insn =
  113. &bundle->insns[i];
  114. if (insn->opcode->mnemonic == TILE_OPC_INFO ||
  115. insn->opcode->mnemonic == TILE_OPC_INFOL) {
  116. operands[num_ops++] = insn->operand_values[0];
  117. }
  118. }
  119. return num_ops;
  120. }
  121. /* Does this bundle contain a jrp instruction, and if so, to which
  122. * register is it jumping?
  123. */
  124. static bool bt_has_jrp(const struct BacktraceBundle *bundle, int *target_reg)
  125. {
  126. const struct tile_decoded_instruction *insn =
  127. find_matching_insn(bundle, TILE_OPC_JRP, NULL, 0);
  128. if (insn == NULL)
  129. return false;
  130. *target_reg = insn->operand_values[0];
  131. return true;
  132. }
  133. /* Does this bundle modify the specified register in any way? */
  134. static bool bt_modifies_reg(const struct BacktraceBundle *bundle, int reg)
  135. {
  136. int i, j;
  137. for (i = 0; i < bundle->num_insns; i++) {
  138. const struct tile_decoded_instruction *insn =
  139. &bundle->insns[i];
  140. if (insn->opcode->implicitly_written_register == reg)
  141. return true;
  142. for (j = 0; j < insn->opcode->num_operands; j++)
  143. if (insn->operands[j]->is_dest_reg &&
  144. insn->operand_values[j] == reg)
  145. return true;
  146. }
  147. return false;
  148. }
  149. /* Does this bundle modify sp? */
  150. static inline bool bt_modifies_sp(const struct BacktraceBundle *bundle)
  151. {
  152. return bt_modifies_reg(bundle, TREG_SP);
  153. }
  154. /* Does this bundle modify lr? */
  155. static inline bool bt_modifies_lr(const struct BacktraceBundle *bundle)
  156. {
  157. return bt_modifies_reg(bundle, TREG_LR);
  158. }
  159. /* Does this bundle contain the instruction 'move fp, sp'? */
  160. static inline bool bt_has_move_r52_sp(const struct BacktraceBundle *bundle)
  161. {
  162. static const int vals[2] = { 52, TREG_SP };
  163. return find_matching_insn(bundle, TILE_OPC_MOVE, vals, 2) != NULL;
  164. }
  165. /* Does this bundle contain a store of lr to sp? */
  166. static inline bool bt_has_sw_sp_lr(const struct BacktraceBundle *bundle)
  167. {
  168. static const int vals[2] = { TREG_SP, TREG_LR };
  169. return find_matching_insn(bundle, OPCODE_STORE, vals, 2) != NULL;
  170. }
  171. #ifdef __tilegx__
  172. /* Track moveli values placed into registers. */
  173. static inline void bt_update_moveli(const struct BacktraceBundle *bundle,
  174. int moveli_args[])
  175. {
  176. int i;
  177. for (i = 0; i < bundle->num_insns; i++) {
  178. const struct tile_decoded_instruction *insn =
  179. &bundle->insns[i];
  180. if (insn->opcode->mnemonic == TILEGX_OPC_MOVELI) {
  181. int reg = insn->operand_values[0];
  182. moveli_args[reg] = insn->operand_values[1];
  183. }
  184. }
  185. }
  186. /* Does this bundle contain an 'add sp, sp, reg' instruction
  187. * from a register that we saw a moveli into, and if so, what
  188. * is the value in the register?
  189. */
  190. static bool bt_has_add_sp(const struct BacktraceBundle *bundle, int *adjust,
  191. int moveli_args[])
  192. {
  193. static const int vals[2] = { TREG_SP, TREG_SP };
  194. const struct tile_decoded_instruction *insn =
  195. find_matching_insn(bundle, TILEGX_OPC_ADDX, vals, 2);
  196. if (insn) {
  197. int reg = insn->operand_values[2];
  198. if (moveli_args[reg]) {
  199. *adjust = moveli_args[reg];
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205. #endif
  206. /* Locates the caller's PC and SP for a program starting at the
  207. * given address.
  208. */
  209. static void find_caller_pc_and_caller_sp(CallerLocation *location,
  210. const unsigned long start_pc,
  211. BacktraceMemoryReader read_memory_func,
  212. void *read_memory_func_extra)
  213. {
  214. /* Have we explicitly decided what the sp is,
  215. * rather than just the default?
  216. */
  217. bool sp_determined = false;
  218. /* Has any bundle seen so far modified lr? */
  219. bool lr_modified = false;
  220. /* Have we seen a move from sp to fp? */
  221. bool sp_moved_to_r52 = false;
  222. /* Have we seen a terminating bundle? */
  223. bool seen_terminating_bundle = false;
  224. /* Cut down on round-trip reading overhead by reading several
  225. * bundles at a time.
  226. */
  227. tile_bundle_bits prefetched_bundles[32];
  228. int num_bundles_prefetched = 0;
  229. int next_bundle = 0;
  230. unsigned long pc;
  231. #ifdef __tilegx__
  232. /* Naively try to track moveli values to support addx for -m32. */
  233. int moveli_args[TILEGX_NUM_REGISTERS] = { 0 };
  234. #endif
  235. /* Default to assuming that the caller's sp is the current sp.
  236. * This is necessary to handle the case where we start backtracing
  237. * right at the end of the epilog.
  238. */
  239. location->sp_location = SP_LOC_OFFSET;
  240. location->sp_offset = 0;
  241. /* Default to having no idea where the caller PC is. */
  242. location->pc_location = PC_LOC_UNKNOWN;
  243. /* Don't even try if the PC is not aligned. */
  244. if (start_pc % TILE_BUNDLE_ALIGNMENT_IN_BYTES != 0)
  245. return;
  246. for (pc = start_pc;; pc += sizeof(tile_bundle_bits)) {
  247. struct BacktraceBundle bundle;
  248. int num_info_ops, info_operands[MAX_INFO_OPS_PER_BUNDLE];
  249. int one_ago, jrp_reg;
  250. bool has_jrp;
  251. if (next_bundle >= num_bundles_prefetched) {
  252. /* Prefetch some bytes, but don't cross a page
  253. * boundary since that might cause a read failure we
  254. * don't care about if we only need the first few
  255. * bytes. Note: we don't care what the actual page
  256. * size is; using the minimum possible page size will
  257. * prevent any problems.
  258. */
  259. unsigned int bytes_to_prefetch = 4096 - (pc & 4095);
  260. if (bytes_to_prefetch > sizeof prefetched_bundles)
  261. bytes_to_prefetch = sizeof prefetched_bundles;
  262. if (!read_memory_func(prefetched_bundles, pc,
  263. bytes_to_prefetch,
  264. read_memory_func_extra)) {
  265. if (pc == start_pc) {
  266. /* The program probably called a bad
  267. * address, such as a NULL pointer.
  268. * So treat this as if we are at the
  269. * start of the function prolog so the
  270. * backtrace will show how we got here.
  271. */
  272. location->pc_location = PC_LOC_IN_LR;
  273. return;
  274. }
  275. /* Unreadable address. Give up. */
  276. break;
  277. }
  278. next_bundle = 0;
  279. num_bundles_prefetched =
  280. bytes_to_prefetch / sizeof(tile_bundle_bits);
  281. }
  282. /* Decode the next bundle. */
  283. bundle.bits = prefetched_bundles[next_bundle++];
  284. bundle.num_insns =
  285. parse_insn_tile(bundle.bits, pc, bundle.insns);
  286. num_info_ops = bt_get_info_ops(&bundle, info_operands);
  287. /* First look at any one_ago info ops if they are interesting,
  288. * since they should shadow any non-one-ago info ops.
  289. */
  290. for (one_ago = (pc != start_pc) ? 1 : 0;
  291. one_ago >= 0; one_ago--) {
  292. int i;
  293. for (i = 0; i < num_info_ops; i++) {
  294. int info_operand = info_operands[i];
  295. if (info_operand < CALLER_UNKNOWN_BASE) {
  296. /* Weird; reserved value, ignore it. */
  297. continue;
  298. }
  299. /* Skip info ops which are not in the
  300. * "one_ago" mode we want right now.
  301. */
  302. if (((info_operand & ONE_BUNDLE_AGO_FLAG) != 0)
  303. != (one_ago != 0))
  304. continue;
  305. /* Clear the flag to make later checking
  306. * easier. */
  307. info_operand &= ~ONE_BUNDLE_AGO_FLAG;
  308. /* Default to looking at PC_IN_LR_FLAG. */
  309. if (info_operand & PC_IN_LR_FLAG)
  310. location->pc_location =
  311. PC_LOC_IN_LR;
  312. else
  313. location->pc_location =
  314. PC_LOC_ON_STACK;
  315. switch (info_operand) {
  316. case CALLER_UNKNOWN_BASE:
  317. location->pc_location = PC_LOC_UNKNOWN;
  318. location->sp_location = SP_LOC_UNKNOWN;
  319. return;
  320. case CALLER_SP_IN_R52_BASE:
  321. case CALLER_SP_IN_R52_BASE | PC_IN_LR_FLAG:
  322. location->sp_location = SP_LOC_IN_R52;
  323. return;
  324. default:
  325. {
  326. const unsigned int val = info_operand
  327. - CALLER_SP_OFFSET_BASE;
  328. const unsigned int sp_offset =
  329. (val >> NUM_INFO_OP_FLAGS) * 8;
  330. if (sp_offset < 32768) {
  331. /* This is a properly encoded
  332. * SP offset. */
  333. location->sp_location =
  334. SP_LOC_OFFSET;
  335. location->sp_offset =
  336. sp_offset;
  337. return;
  338. } else {
  339. /* This looked like an SP
  340. * offset, but it's outside
  341. * the legal range, so this
  342. * must be an unrecognized
  343. * info operand. Ignore it.
  344. */
  345. }
  346. }
  347. break;
  348. }
  349. }
  350. }
  351. if (seen_terminating_bundle) {
  352. /* We saw a terminating bundle during the previous
  353. * iteration, so we were only looking for an info op.
  354. */
  355. break;
  356. }
  357. if (bundle.bits == 0) {
  358. /* Wacky terminating bundle. Stop looping, and hope
  359. * we've already seen enough to find the caller.
  360. */
  361. break;
  362. }
  363. /*
  364. * Try to determine caller's SP.
  365. */
  366. if (!sp_determined) {
  367. int adjust;
  368. if (bt_has_addi_sp(&bundle, &adjust)
  369. #ifdef __tilegx__
  370. || bt_has_add_sp(&bundle, &adjust, moveli_args)
  371. #endif
  372. ) {
  373. location->sp_location = SP_LOC_OFFSET;
  374. if (adjust <= 0) {
  375. /* We are in prolog about to adjust
  376. * SP. */
  377. location->sp_offset = 0;
  378. } else {
  379. /* We are in epilog restoring SP. */
  380. location->sp_offset = adjust;
  381. }
  382. sp_determined = true;
  383. } else {
  384. if (bt_has_move_r52_sp(&bundle)) {
  385. /* Maybe in prolog, creating an
  386. * alloca-style frame. But maybe in
  387. * the middle of a fixed-size frame
  388. * clobbering r52 with SP.
  389. */
  390. sp_moved_to_r52 = true;
  391. }
  392. if (bt_modifies_sp(&bundle)) {
  393. if (sp_moved_to_r52) {
  394. /* We saw SP get saved into
  395. * r52 earlier (or now), which
  396. * must have been in the
  397. * prolog, so we now know that
  398. * SP is still holding the
  399. * caller's sp value.
  400. */
  401. location->sp_location =
  402. SP_LOC_OFFSET;
  403. location->sp_offset = 0;
  404. } else {
  405. /* Someone must have saved
  406. * aside the caller's SP value
  407. * into r52, so r52 holds the
  408. * current value.
  409. */
  410. location->sp_location =
  411. SP_LOC_IN_R52;
  412. }
  413. sp_determined = true;
  414. }
  415. }
  416. #ifdef __tilegx__
  417. /* Track moveli arguments for -m32 mode. */
  418. bt_update_moveli(&bundle, moveli_args);
  419. #endif
  420. }
  421. if (bt_has_iret(&bundle)) {
  422. /* This is a terminating bundle. */
  423. seen_terminating_bundle = true;
  424. continue;
  425. }
  426. /*
  427. * Try to determine caller's PC.
  428. */
  429. jrp_reg = -1;
  430. has_jrp = bt_has_jrp(&bundle, &jrp_reg);
  431. if (has_jrp)
  432. seen_terminating_bundle = true;
  433. if (location->pc_location == PC_LOC_UNKNOWN) {
  434. if (has_jrp) {
  435. if (jrp_reg == TREG_LR && !lr_modified) {
  436. /* Looks like a leaf function, or else
  437. * lr is already restored. */
  438. location->pc_location =
  439. PC_LOC_IN_LR;
  440. } else {
  441. location->pc_location =
  442. PC_LOC_ON_STACK;
  443. }
  444. } else if (bt_has_sw_sp_lr(&bundle)) {
  445. /* In prolog, spilling initial lr to stack. */
  446. location->pc_location = PC_LOC_IN_LR;
  447. } else if (bt_modifies_lr(&bundle)) {
  448. lr_modified = true;
  449. }
  450. }
  451. }
  452. }
  453. /* Initializes a backtracer to start from the given location.
  454. *
  455. * If the frame pointer cannot be determined it is set to -1.
  456. *
  457. * state: The state to be filled in.
  458. * read_memory_func: A callback that reads memory.
  459. * read_memory_func_extra: An arbitrary argument to read_memory_func.
  460. * pc: The current PC.
  461. * lr: The current value of the 'lr' register.
  462. * sp: The current value of the 'sp' register.
  463. * r52: The current value of the 'r52' register.
  464. */
  465. void backtrace_init(BacktraceIterator *state,
  466. BacktraceMemoryReader read_memory_func,
  467. void *read_memory_func_extra,
  468. unsigned long pc, unsigned long lr,
  469. unsigned long sp, unsigned long r52)
  470. {
  471. CallerLocation location;
  472. unsigned long fp, initial_frame_caller_pc;
  473. /* Find out where we are in the initial frame. */
  474. find_caller_pc_and_caller_sp(&location, pc,
  475. read_memory_func, read_memory_func_extra);
  476. switch (location.sp_location) {
  477. case SP_LOC_UNKNOWN:
  478. /* Give up. */
  479. fp = -1;
  480. break;
  481. case SP_LOC_IN_R52:
  482. fp = r52;
  483. break;
  484. case SP_LOC_OFFSET:
  485. fp = sp + location.sp_offset;
  486. break;
  487. default:
  488. /* Give up. */
  489. fp = -1;
  490. break;
  491. }
  492. /* If the frame pointer is not aligned to the basic word size
  493. * something terrible happened and we should mark it as invalid.
  494. */
  495. if (fp % sizeof(bt_int_reg_t) != 0)
  496. fp = -1;
  497. /* -1 means "don't know initial_frame_caller_pc". */
  498. initial_frame_caller_pc = -1;
  499. switch (location.pc_location) {
  500. case PC_LOC_UNKNOWN:
  501. /* Give up. */
  502. fp = -1;
  503. break;
  504. case PC_LOC_IN_LR:
  505. if (lr == 0 || lr % TILE_BUNDLE_ALIGNMENT_IN_BYTES != 0) {
  506. /* Give up. */
  507. fp = -1;
  508. } else {
  509. initial_frame_caller_pc = lr;
  510. }
  511. break;
  512. case PC_LOC_ON_STACK:
  513. /* Leave initial_frame_caller_pc as -1,
  514. * meaning check the stack.
  515. */
  516. break;
  517. default:
  518. /* Give up. */
  519. fp = -1;
  520. break;
  521. }
  522. state->pc = pc;
  523. state->sp = sp;
  524. state->fp = fp;
  525. state->initial_frame_caller_pc = initial_frame_caller_pc;
  526. state->read_memory_func = read_memory_func;
  527. state->read_memory_func_extra = read_memory_func_extra;
  528. }
  529. /* Handle the case where the register holds more bits than the VA. */
  530. static bool valid_addr_reg(bt_int_reg_t reg)
  531. {
  532. return ((unsigned long)reg == reg);
  533. }
  534. /* Advances the backtracing state to the calling frame, returning
  535. * true iff successful.
  536. */
  537. bool backtrace_next(BacktraceIterator *state)
  538. {
  539. unsigned long next_fp, next_pc;
  540. bt_int_reg_t next_frame[2];
  541. if (state->fp == -1) {
  542. /* No parent frame. */
  543. return false;
  544. }
  545. /* Try to read the frame linkage data chaining to the next function. */
  546. if (!state->read_memory_func(&next_frame, state->fp, sizeof next_frame,
  547. state->read_memory_func_extra)) {
  548. return false;
  549. }
  550. next_fp = next_frame[1];
  551. if (!valid_addr_reg(next_frame[1]) ||
  552. next_fp % sizeof(bt_int_reg_t) != 0) {
  553. /* Caller's frame pointer is suspect, so give up. */
  554. return false;
  555. }
  556. if (state->initial_frame_caller_pc != -1) {
  557. /* We must be in the initial stack frame and already know the
  558. * caller PC.
  559. */
  560. next_pc = state->initial_frame_caller_pc;
  561. /* Force reading stack next time, in case we were in the
  562. * initial frame. We don't do this above just to paranoidly
  563. * avoid changing the struct at all when we return false.
  564. */
  565. state->initial_frame_caller_pc = -1;
  566. } else {
  567. /* Get the caller PC from the frame linkage area. */
  568. next_pc = next_frame[0];
  569. if (!valid_addr_reg(next_frame[0]) || next_pc == 0 ||
  570. next_pc % TILE_BUNDLE_ALIGNMENT_IN_BYTES != 0) {
  571. /* The PC is suspect, so give up. */
  572. return false;
  573. }
  574. }
  575. /* Update state to become the caller's stack frame. */
  576. state->pc = next_pc;
  577. state->sp = state->fp;
  578. state->fp = next_fp;
  579. return true;
  580. }