panic_handler.c 7.5 KB

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