panic_handler.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include "spi_flash_mmap.h"
  8. #include "esp_ipc_isr.h"
  9. #include "esp_private/system_internal.h"
  10. #include "esp_private/cache_utils.h"
  11. #include "soc/soc_memory_layout.h"
  12. #include "esp_cpu.h"
  13. #include "soc/soc_caps.h"
  14. #include "soc/rtc.h"
  15. #include "hal/soc_hal.h"
  16. #include "esp_private/cache_err_int.h"
  17. #include "sdkconfig.h"
  18. #include "esp_rom_sys.h"
  19. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  20. #ifdef CONFIG_IDF_TARGET_ESP32S2
  21. #include "esp32s2/memprot.h"
  22. #else
  23. #include "esp_memprot.h"
  24. #endif
  25. #endif
  26. #include "esp_private/panic_internal.h"
  27. #include "esp_private/panic_reason.h"
  28. #include "hal/wdt_types.h"
  29. #include "hal/wdt_hal.h"
  30. extern int _invalid_pc_placeholder;
  31. extern void esp_panic_handler_reconfigure_wdts(uint32_t timeout_ms);
  32. extern void esp_panic_handler(panic_info_t *);
  33. static wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
  34. void *g_exc_frames[SOC_CPU_CORES_NUM] = {NULL};
  35. /*
  36. Panic handlers; these get called when an unhandled exception occurs or the assembly-level
  37. task switching / interrupt code runs into an unrecoverable error. The default task stack
  38. overflow handler and abort handler are also in here.
  39. */
  40. /*
  41. Note: The linker script will put everything in this file in IRAM/DRAM, so it also works with flash cache disabled.
  42. */
  43. static void print_state_for_core(const void *f, int core)
  44. {
  45. /* On Xtensa (with Window ABI), register dump is not required for backtracing.
  46. * Don't print it on abort to reduce clutter.
  47. * On other architectures, register values need to be known for backtracing.
  48. */
  49. #if (CONFIG_IDF_TARGET_ARCH_XTENSA && defined(XCHAL_HAVE_WINDOWED)) || \
  50. (CONFIG_IDF_TARGET_ARCH_RISCV && CONFIG_ESP_SYSTEM_USE_EH_FRAME)
  51. if (!g_panic_abort) {
  52. #else
  53. if (true) {
  54. #endif
  55. panic_print_registers(f, core);
  56. panic_print_str("\r\n");
  57. }
  58. panic_print_backtrace(f, core);
  59. }
  60. static void print_state(const void *f)
  61. {
  62. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  63. int err_core = f == g_exc_frames[0] ? 0 : 1;
  64. #else
  65. int err_core = 0;
  66. #endif
  67. print_state_for_core(f, err_core);
  68. panic_print_str("\r\n");
  69. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  70. // If there are other frame info, print them as well
  71. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  72. // `f` is the frame for the offending core, see note above.
  73. if (err_core != i && g_exc_frames[i] != NULL) {
  74. print_state_for_core(g_exc_frames[i], i);
  75. panic_print_str("\r\n");
  76. }
  77. }
  78. #endif
  79. }
  80. static void frame_to_panic_info(void *frame, panic_info_t *info, bool pseudo_excause)
  81. {
  82. info->core = esp_cpu_get_core_id();
  83. info->exception = PANIC_EXCEPTION_FAULT;
  84. info->details = NULL;
  85. info->reason = "Unknown";
  86. info->pseudo_excause = pseudo_excause;
  87. if (pseudo_excause) {
  88. panic_soc_fill_info(frame, info);
  89. } else {
  90. panic_arch_fill_info(frame, info);
  91. }
  92. info->state = print_state;
  93. info->frame = frame;
  94. }
  95. static void panic_handler(void *frame, bool pseudo_excause)
  96. {
  97. panic_info_t info = { 0 };
  98. /*
  99. * Setup environment and perform necessary architecture/chip specific
  100. * steps here prior to the system panic handler.
  101. * */
  102. int core_id = esp_cpu_get_core_id();
  103. // If multiple cores arrive at panic handler, save frames for all of them
  104. g_exc_frames[core_id] = frame;
  105. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  106. // These are cases where both CPUs both go into panic handler. The following code ensures
  107. // only one core proceeds to the system panic handler.
  108. if (pseudo_excause) {
  109. #define BUSY_WAIT_IF_TRUE(b) { if (b) while(1); }
  110. // For WDT expiry, pause the non-offending core - offending core handles panic
  111. BUSY_WAIT_IF_TRUE(panic_get_cause(frame) == PANIC_RSN_INTWDT_CPU0 && core_id == 1);
  112. BUSY_WAIT_IF_TRUE(panic_get_cause(frame) == PANIC_RSN_INTWDT_CPU1 && core_id == 0);
  113. // For cache error, pause the non-offending core - offending core handles panic
  114. if (panic_get_cause(frame) == PANIC_RSN_CACHEERR && core_id != esp_cache_err_get_cpuid()) {
  115. // Only print the backtrace for the offending core in case of the cache error
  116. g_exc_frames[core_id] = NULL;
  117. while (1) {
  118. ;
  119. }
  120. }
  121. }
  122. // Need to reconfigure WDTs before we stall any other CPU
  123. esp_panic_handler_reconfigure_wdts(1000);
  124. esp_rom_delay_us(1);
  125. // Stall all other cores
  126. for (uint32_t i = 0; i < SOC_CPU_CORES_NUM; i++) {
  127. if (i != core_id) {
  128. esp_cpu_stall(i);
  129. }
  130. }
  131. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  132. esp_ipc_isr_stall_abort();
  133. if (esp_cpu_dbgr_is_attached()) {
  134. #if __XTENSA__
  135. if (!(esp_ptr_executable(esp_cpu_pc_to_addr(panic_get_address(frame))) && (panic_get_address(frame) & 0xC0000000U))) {
  136. /* Xtensa ABI sets the 2 MSBs of the PC according to the windowed call size
  137. * Incase the PC is invalid, GDB will fail to translate addresses to function names
  138. * Hence replacing the PC to a placeholder address in case of invalid PC
  139. */
  140. panic_set_address(frame, (uint32_t)&_invalid_pc_placeholder);
  141. }
  142. #endif
  143. if (panic_get_cause(frame) == PANIC_RSN_INTWDT_CPU0
  144. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  145. || panic_get_cause(frame) == PANIC_RSN_INTWDT_CPU1
  146. #endif
  147. ) {
  148. wdt_hal_write_protect_disable(&wdt0_context);
  149. wdt_hal_handle_intr(&wdt0_context);
  150. wdt_hal_write_protect_enable(&wdt0_context);
  151. }
  152. }
  153. // Convert architecture exception frame into abstracted panic info
  154. frame_to_panic_info(frame, &info, pseudo_excause);
  155. // Call the system panic handler
  156. esp_panic_handler(&info);
  157. }
  158. /**
  159. * This function must always be in IRAM as it is required to
  160. * re-enable the flash cache.
  161. */
  162. #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  163. static void IRAM_ATTR panic_enable_cache(void)
  164. {
  165. int core_id = esp_cpu_get_core_id();
  166. if (!spi_flash_cache_enabled()) {
  167. esp_ipc_isr_stall_abort();
  168. spi_flash_enable_cache(core_id);
  169. }
  170. }
  171. #endif
  172. void IRAM_ATTR panicHandler(void *frame)
  173. {
  174. #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  175. panic_enable_cache();
  176. #endif
  177. // This panic handler gets called for when the double exception vector,
  178. // kernel exception vector gets used; as well as handling interrupt-based
  179. // faults cache error, wdt expiry. EXCAUSE register gets written with
  180. // one of PANIC_RSN_* values.
  181. panic_handler(frame, true);
  182. }
  183. void IRAM_ATTR xt_unhandled_exception(void *frame)
  184. {
  185. #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  186. panic_enable_cache();
  187. #endif
  188. panic_handler(frame, false);
  189. }
  190. void __attribute__((noreturn)) panic_restart(void)
  191. {
  192. #ifdef CONFIG_IDF_TARGET_ESP32
  193. // On the ESP32, cache error status can only be cleared by system reset
  194. if (esp_cache_err_get_cpuid() != -1) {
  195. esp_restart_noos_dig();
  196. }
  197. #endif
  198. esp_restart_noos();
  199. }