backtrace.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include <arch_helpers.h>
  10. #include <common/debug.h>
  11. #include <drivers/console.h>
  12. /* Maximum number of entries in the backtrace to display */
  13. #define UNWIND_LIMIT 20U
  14. /*
  15. * If -fno-omit-frame-pointer is used:
  16. *
  17. * - AArch64: The AAPCS defines the format of the frame records and mandates the
  18. * usage of r29 as frame pointer.
  19. *
  20. * - AArch32: The format of the frame records is not defined in the AAPCS.
  21. * However, at least GCC and Clang use the same format. When they are forced
  22. * to only generate A32 code (with -marm), they use r11 as frame pointer and a
  23. * similar format as in AArch64. If interworking with T32 is enabled, the
  24. * frame pointer is r7 and the format is different. This is not supported by
  25. * this implementation of backtrace, so it is needed to use -marm.
  26. */
  27. /* Frame records form a linked list in the stack */
  28. struct frame_record {
  29. /* Previous frame record in the list */
  30. struct frame_record *parent;
  31. /* Return address of the function at this level */
  32. uintptr_t return_addr;
  33. };
  34. const char *get_el_str(unsigned int el)
  35. {
  36. if (el == 3U) {
  37. return "EL3";
  38. } else if (el == 2U) {
  39. return "EL2";
  40. } else {
  41. return "S-EL1";
  42. }
  43. }
  44. /*
  45. * Returns true if the address points to a virtual address that can be read at
  46. * the current EL, false otherwise.
  47. */
  48. #ifdef __aarch64__
  49. static bool is_address_readable(uintptr_t addr)
  50. {
  51. unsigned int el = get_current_el();
  52. #if ENABLE_PAUTH
  53. /*
  54. * When pointer authentication is enabled, the LR value saved on the
  55. * stack contains a PAC. It must be stripped to retrieve the return
  56. * address.
  57. */
  58. xpaci(addr);
  59. #endif
  60. if (el == 3U) {
  61. ats1e3r(addr);
  62. } else if (el == 2U) {
  63. ats1e2r(addr);
  64. } else {
  65. AT(ats1e1r, addr);
  66. }
  67. isb();
  68. /* If PAR.F == 1 the address translation was aborted. */
  69. if ((read_par_el1() & PAR_F_MASK) != 0U)
  70. return false;
  71. return true;
  72. }
  73. #else /* !__aarch64__ */
  74. static bool is_address_readable(uintptr_t addr)
  75. {
  76. unsigned int el = get_current_el();
  77. if (el == 3U) {
  78. write_ats1cpr(addr);
  79. } else if (el == 2U) {
  80. write_ats1hr(addr);
  81. } else {
  82. write_ats1cpr(addr);
  83. }
  84. isb();
  85. /* If PAR.F == 1 the address translation was aborted. */
  86. if ((read64_par() & PAR_F_MASK) != 0U)
  87. return false;
  88. return true;
  89. }
  90. #endif /* __aarch64__ */
  91. /*
  92. * Returns true if all the bytes in a given object are in mapped memory and an
  93. * LDR using this pointer would succeed, false otherwise.
  94. */
  95. static bool is_valid_object(uintptr_t addr, size_t size)
  96. {
  97. assert(size > 0U);
  98. if (addr == 0U)
  99. return false;
  100. /* Detect overflows */
  101. if ((addr + size) < addr)
  102. return false;
  103. /* A pointer not aligned properly could trigger an alignment fault. */
  104. if ((addr & (sizeof(uintptr_t) - 1U)) != 0U)
  105. return false;
  106. /* Check that all the object is readable */
  107. for (size_t i = 0; i < size; i++) {
  108. if (!is_address_readable(addr + i))
  109. return false;
  110. }
  111. return true;
  112. }
  113. /*
  114. * Returns true if the specified address is correctly aligned and points to a
  115. * valid memory region.
  116. */
  117. static bool is_valid_jump_address(uintptr_t addr)
  118. {
  119. if (addr == 0U)
  120. return false;
  121. /* Check alignment. Both A64 and A32 use 32-bit opcodes */
  122. if ((addr & (sizeof(uint32_t) - 1U)) != 0U)
  123. return false;
  124. if (!is_address_readable(addr))
  125. return false;
  126. return true;
  127. }
  128. /*
  129. * Returns true if the pointer points at a valid frame record, false otherwise.
  130. */
  131. static bool is_valid_frame_record(struct frame_record *fr)
  132. {
  133. return is_valid_object((uintptr_t)fr, sizeof(struct frame_record));
  134. }
  135. /*
  136. * Adjust the frame-pointer-register value by 4 bytes on AArch32 to have the
  137. * same layout as AArch64.
  138. */
  139. static struct frame_record *adjust_frame_record(struct frame_record *fr)
  140. {
  141. #ifdef __aarch64__
  142. return fr;
  143. #else
  144. return (struct frame_record *)((uintptr_t)fr - 4U);
  145. #endif
  146. }
  147. static void unwind_stack(struct frame_record *fr, uintptr_t current_pc,
  148. uintptr_t link_register)
  149. {
  150. uintptr_t call_site;
  151. static const char *backtrace_str = "%u: %s: 0x%lx\n";
  152. const char *el_str = get_el_str(get_current_el());
  153. if (!is_valid_frame_record(fr)) {
  154. printf("ERROR: Corrupted frame pointer (frame record address = %p)\n",
  155. fr);
  156. return;
  157. }
  158. if (fr->return_addr != link_register) {
  159. printf("ERROR: Corrupted stack (frame record address = %p)\n",
  160. fr);
  161. return;
  162. }
  163. /* The level 0 of the backtrace is the current backtrace function */
  164. printf(backtrace_str, 0U, el_str, current_pc);
  165. /*
  166. * The last frame record pointer in the linked list at the beginning of
  167. * the stack should be NULL unless stack is corrupted.
  168. */
  169. for (unsigned int i = 1U; i < UNWIND_LIMIT; i++) {
  170. /* If an invalid frame record is found, exit. */
  171. if (!is_valid_frame_record(fr))
  172. return;
  173. /*
  174. * A32 and A64 are fixed length so the address from where the
  175. * call was made is the instruction before the return address,
  176. * which is always 4 bytes before it.
  177. */
  178. call_site = fr->return_addr - 4U;
  179. #if ENABLE_PAUTH
  180. /*
  181. * When pointer authentication is enabled, the LR value saved on
  182. * the stack contains a PAC. It must be stripped to retrieve the
  183. * return address.
  184. */
  185. xpaci(call_site);
  186. #endif
  187. /*
  188. * If the address is invalid it means that the frame record is
  189. * probably corrupted.
  190. */
  191. if (!is_valid_jump_address(call_site))
  192. return;
  193. printf(backtrace_str, i, el_str, call_site);
  194. fr = adjust_frame_record(fr->parent);
  195. }
  196. printf("ERROR: Max backtrace depth reached\n");
  197. }
  198. /*
  199. * Display a backtrace. The cookie string parameter is displayed along the
  200. * trace to help filter the log messages.
  201. *
  202. * Many things can prevent displaying the expected backtrace. For example,
  203. * compiler optimizations can use a branch instead of branch with link when it
  204. * detects a tail call. The backtrace level for this caller will not be
  205. * displayed, as it does not appear in the call stack anymore. Also, assembly
  206. * functions will not be displayed unless they setup AAPCS compliant frame
  207. * records on AArch64 and compliant with GCC-specific frame record format on
  208. * AArch32.
  209. *
  210. * Usage of the trace: addr2line can be used to map the addresses to function
  211. * and source code location when given the ELF file compiled with debug
  212. * information. The "-i" flag is highly recommended to improve display of
  213. * inlined function. The *.dump files generated when building each image can
  214. * also be used.
  215. *
  216. * WARNING: In case of corrupted stack, this function could display security
  217. * sensitive information past the beginning of the stack so it must not be used
  218. * in production build. This function is only compiled in when ENABLE_BACKTRACE
  219. * is set to 1.
  220. */
  221. void backtrace(const char *cookie)
  222. {
  223. uintptr_t return_address = (uintptr_t)__builtin_return_address(0U);
  224. struct frame_record *fr = __builtin_frame_address(0U);
  225. /* Printing the backtrace may crash the system, flush before starting */
  226. console_flush();
  227. fr = adjust_frame_record(fr);
  228. printf("BACKTRACE: START: %s\n", cookie);
  229. unwind_stack(fr, (uintptr_t)&backtrace, return_address);
  230. printf("BACKTRACE: END: %s\n", cookie);
  231. }