backtrace.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #ifndef _TILE_BACKTRACE_H
  15. #define _TILE_BACKTRACE_H
  16. #include <linux/types.h>
  17. #include <arch/chip.h>
  18. #if CHIP_VA_WIDTH() > 32
  19. typedef unsigned long long VirtualAddress;
  20. #else
  21. typedef unsigned int VirtualAddress;
  22. #endif
  23. /** Reads 'size' bytes from 'address' and writes the data to 'result'.
  24. * Returns true if successful, else false (e.g. memory not readable).
  25. */
  26. typedef bool (*BacktraceMemoryReader)(void *result,
  27. VirtualAddress address,
  28. unsigned int size,
  29. void *extra);
  30. typedef struct {
  31. /** Current PC. */
  32. VirtualAddress pc;
  33. /** Current stack pointer value. */
  34. VirtualAddress sp;
  35. /** Current frame pointer value (i.e. caller's stack pointer) */
  36. VirtualAddress fp;
  37. /** Internal use only: caller's PC for first frame. */
  38. VirtualAddress initial_frame_caller_pc;
  39. /** Internal use only: callback to read memory. */
  40. BacktraceMemoryReader read_memory_func;
  41. /** Internal use only: arbitrary argument to read_memory_func. */
  42. void *read_memory_func_extra;
  43. } BacktraceIterator;
  44. /** Initializes a backtracer to start from the given location.
  45. *
  46. * If the frame pointer cannot be determined it is set to -1.
  47. *
  48. * @param state The state to be filled in.
  49. * @param read_memory_func A callback that reads memory. If NULL, a default
  50. * value is provided.
  51. * @param read_memory_func_extra An arbitrary argument to read_memory_func.
  52. * @param pc The current PC.
  53. * @param lr The current value of the 'lr' register.
  54. * @param sp The current value of the 'sp' register.
  55. * @param r52 The current value of the 'r52' register.
  56. */
  57. extern void backtrace_init(BacktraceIterator *state,
  58. BacktraceMemoryReader read_memory_func,
  59. void *read_memory_func_extra,
  60. VirtualAddress pc, VirtualAddress lr,
  61. VirtualAddress sp, VirtualAddress r52);
  62. /** Advances the backtracing state to the calling frame, returning
  63. * true iff successful.
  64. */
  65. extern bool backtrace_next(BacktraceIterator *state);
  66. typedef enum {
  67. /* We have no idea what the caller's pc is. */
  68. PC_LOC_UNKNOWN,
  69. /* The caller's pc is currently in lr. */
  70. PC_LOC_IN_LR,
  71. /* The caller's pc can be found by dereferencing the caller's sp. */
  72. PC_LOC_ON_STACK
  73. } CallerPCLocation;
  74. typedef enum {
  75. /* We have no idea what the caller's sp is. */
  76. SP_LOC_UNKNOWN,
  77. /* The caller's sp is currently in r52. */
  78. SP_LOC_IN_R52,
  79. /* The caller's sp can be found by adding a certain constant
  80. * to the current value of sp.
  81. */
  82. SP_LOC_OFFSET
  83. } CallerSPLocation;
  84. /* Bit values ORed into CALLER_* values for info ops. */
  85. enum {
  86. /* Setting the low bit on any of these values means the info op
  87. * applies only to one bundle ago.
  88. */
  89. ONE_BUNDLE_AGO_FLAG = 1,
  90. /* Setting this bit on a CALLER_SP_* value means the PC is in LR.
  91. * If not set, PC is on the stack.
  92. */
  93. PC_IN_LR_FLAG = 2,
  94. /* This many of the low bits of a CALLER_SP_* value are for the
  95. * flag bits above.
  96. */
  97. NUM_INFO_OP_FLAGS = 2,
  98. /* We cannot have one in the memory pipe so this is the maximum. */
  99. MAX_INFO_OPS_PER_BUNDLE = 2
  100. };
  101. /** Internal constants used to define 'info' operands. */
  102. enum {
  103. /* 0 and 1 are reserved, as are all negative numbers. */
  104. CALLER_UNKNOWN_BASE = 2,
  105. CALLER_SP_IN_R52_BASE = 4,
  106. CALLER_SP_OFFSET_BASE = 8
  107. };
  108. /** Current backtracer state describing where it thinks the caller is. */
  109. typedef struct {
  110. /*
  111. * Public fields
  112. */
  113. /* How do we find the caller's PC? */
  114. CallerPCLocation pc_location : 8;
  115. /* How do we find the caller's SP? */
  116. CallerSPLocation sp_location : 8;
  117. /* If sp_location == SP_LOC_OFFSET, then caller_sp == sp +
  118. * loc->sp_offset. Else this field is undefined.
  119. */
  120. uint16_t sp_offset;
  121. /* In the most recently visited bundle a terminating bundle? */
  122. bool at_terminating_bundle;
  123. /*
  124. * Private fields
  125. */
  126. /* Will the forward scanner see someone clobbering sp
  127. * (i.e. changing it with something other than addi sp, sp, N?)
  128. */
  129. bool sp_clobber_follows;
  130. /* Operand to next "visible" info op (no more than one bundle past
  131. * the next terminating bundle), or -32768 if none.
  132. */
  133. int16_t next_info_operand;
  134. /* Is the info of in next_info_op in the very next bundle? */
  135. bool is_next_info_operand_adjacent;
  136. } CallerLocation;
  137. #endif /* _TILE_BACKTRACE_H */