panic_handler.c 7.4 KB

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