backtrace.c 6.9 KB

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