panic_handler.c 7.0 KB

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