panic_handler.c 7.2 KB

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