panic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "esp_err.h"
  9. #include "esp_attr.h"
  10. #include "esp_private/system_internal.h"
  11. #include "esp_private/usb_console.h"
  12. #include "esp_cpu.h"
  13. #include "soc/rtc.h"
  14. #include "hal/timer_hal.h"
  15. #include "hal/wdt_types.h"
  16. #include "hal/wdt_hal.h"
  17. #include "hal/mwdt_ll.h"
  18. #include "esp_private/esp_int_wdt.h"
  19. #include "esp_private/panic_internal.h"
  20. #include "port/panic_funcs.h"
  21. #include "esp_rom_sys.h"
  22. #include "sdkconfig.h"
  23. #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  24. #if __has_include("esp_app_desc.h")
  25. #define WITH_ELF_SHA256
  26. #include "esp_app_desc.h"
  27. #endif
  28. #endif // CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  29. #if CONFIG_ESP_COREDUMP_ENABLE
  30. #include "esp_core_dump.h"
  31. #endif
  32. #if CONFIG_APPTRACE_ENABLE
  33. #include "esp_app_trace.h"
  34. #if CONFIG_APPTRACE_SV_ENABLE
  35. #include "SEGGER_RTT.h"
  36. #endif
  37. #if CONFIG_APPTRACE_ONPANIC_HOST_FLUSH_TMO == -1
  38. #define APPTRACE_ONPANIC_HOST_FLUSH_TMO ESP_APPTRACE_TMO_INFINITE
  39. #else
  40. #define APPTRACE_ONPANIC_HOST_FLUSH_TMO (1000*CONFIG_APPTRACE_ONPANIC_HOST_FLUSH_TMO)
  41. #endif
  42. #endif // CONFIG_APPTRACE_ENABLE
  43. #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  44. #include "hal/uart_hal.h"
  45. #endif
  46. #if CONFIG_ESP_SYSTEM_PANIC_GDBSTUB
  47. #include "esp_gdbstub.h"
  48. #endif
  49. #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG || CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
  50. #include "hal/usb_serial_jtag_ll.h"
  51. #endif
  52. #define MWDT_DEFAULT_TICKS_PER_US 500
  53. bool g_panic_abort = false;
  54. char *g_panic_abort_details = NULL;
  55. static wdt_hal_context_t rtc_wdt_ctx = RWDT_HAL_CONTEXT_DEFAULT();
  56. #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  57. #if CONFIG_ESP_CONSOLE_UART
  58. static uart_hal_context_t s_panic_uart = { .dev = CONFIG_ESP_CONSOLE_UART_NUM == 0 ? &UART0 :&UART1 };
  59. static void panic_print_char_uart(const char c)
  60. {
  61. uint32_t sz = 0;
  62. while (!uart_hal_get_txfifo_len(&s_panic_uart));
  63. uart_hal_write_txfifo(&s_panic_uart, (uint8_t *) &c, 1, &sz);
  64. }
  65. #endif // CONFIG_ESP_CONSOLE_UART
  66. #if CONFIG_ESP_CONSOLE_USB_CDC
  67. static void panic_print_char_usb_cdc(const char c)
  68. {
  69. esp_usb_console_write_buf(&c, 1);
  70. /* result ignored */
  71. }
  72. #endif // CONFIG_ESP_CONSOLE_USB_CDC
  73. #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG || CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
  74. //Timeout; if there's no host listening, the txfifo won't ever
  75. //be writable after the first packet.
  76. #define USBSERIAL_TIMEOUT_MAX_US 50000
  77. static int s_usbserial_timeout = 0;
  78. static void panic_print_char_usb_serial_jtag(const char c)
  79. {
  80. while (!usb_serial_jtag_ll_txfifo_writable() && s_usbserial_timeout < (USBSERIAL_TIMEOUT_MAX_US / 100)) {
  81. esp_rom_delay_us(100);
  82. s_usbserial_timeout++;
  83. }
  84. if (usb_serial_jtag_ll_txfifo_writable()) {
  85. usb_serial_jtag_ll_write_txfifo((const uint8_t *)&c, 1);
  86. s_usbserial_timeout = 0;
  87. }
  88. }
  89. #endif //CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG || CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
  90. void panic_print_char(const char c)
  91. {
  92. #if CONFIG_ESP_CONSOLE_UART
  93. panic_print_char_uart(c);
  94. #endif
  95. #if CONFIG_ESP_CONSOLE_USB_CDC
  96. panic_print_char_usb_cdc(c);
  97. #endif
  98. #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG || CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG
  99. panic_print_char_usb_serial_jtag(c);
  100. #endif
  101. }
  102. void panic_print_str(const char *str)
  103. {
  104. for (int i = 0; str[i] != 0; i++) {
  105. panic_print_char(str[i]);
  106. }
  107. }
  108. void panic_print_hex(int h)
  109. {
  110. int x;
  111. int c;
  112. // Does not print '0x', only the digits (8 digits to print)
  113. for (x = 0; x < 8; x++) {
  114. c = (h >> 28) & 0xf; // extract the leftmost byte
  115. if (c < 10) {
  116. panic_print_char('0' + c);
  117. } else {
  118. panic_print_char('a' + c - 10);
  119. }
  120. h <<= 4; // move the 2nd leftmost byte to the left, to be extracted next
  121. }
  122. }
  123. void panic_print_dec(int d)
  124. {
  125. // can print at most 2 digits!
  126. int n1, n2;
  127. n1 = d % 10; // extract ones digit
  128. n2 = d / 10; // extract tens digit
  129. if (n2 == 0) {
  130. panic_print_char(' ');
  131. } else {
  132. panic_print_char(n2 + '0');
  133. }
  134. panic_print_char(n1 + '0');
  135. }
  136. #endif // CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  137. /*
  138. If watchdogs are enabled, the panic handler runs the risk of getting aborted pre-emptively because
  139. an overzealous watchdog decides to reset it. On the other hand, if we disable all watchdogs, we run
  140. the risk of somehow halting in the panic handler and not resetting. That is why this routine kills
  141. all watchdogs except the timer group 0 watchdog, and it reconfigures that to reset the chip after
  142. one second.
  143. We have to do this before we do anything that might cause issues in the WDT interrupt handlers,
  144. for example stalling the other core on ESP32 may cause the ESP32_ECO3_CACHE_LOCK_FIX
  145. handler to get stuck.
  146. */
  147. void esp_panic_handler_reconfigure_wdts(uint32_t timeout_ms)
  148. {
  149. wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
  150. #if SOC_TIMER_GROUPS >= 2
  151. // IDF-3825
  152. wdt_hal_context_t wdt1_context = {.inst = WDT_MWDT1, .mwdt_dev = &TIMERG1};
  153. #endif
  154. //Todo: Refactor to use Interrupt or Task Watchdog API, and a system level WDT context
  155. //Reconfigure TWDT (Timer Group 0)
  156. wdt_hal_init(&wdt0_context, WDT_MWDT0, MWDT_LL_DEFAULT_CLK_PRESCALER, false); //Prescaler: wdt counts in ticks of TG0_WDT_TICK_US
  157. wdt_hal_write_protect_disable(&wdt0_context);
  158. wdt_hal_config_stage(&wdt0_context, 0, timeout_ms * 1000 / MWDT_DEFAULT_TICKS_PER_US, WDT_STAGE_ACTION_RESET_SYSTEM); //1 second before reset
  159. wdt_hal_enable(&wdt0_context);
  160. wdt_hal_write_protect_enable(&wdt0_context);
  161. #if SOC_TIMER_GROUPS >= 2
  162. //Disable IWDT (Timer Group 1)
  163. wdt_hal_write_protect_disable(&wdt1_context);
  164. wdt_hal_disable(&wdt1_context);
  165. wdt_hal_write_protect_enable(&wdt1_context);
  166. #endif
  167. }
  168. /*
  169. This disables all the watchdogs for when we call the gdbstub.
  170. */
  171. static inline void disable_all_wdts(void)
  172. {
  173. wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
  174. #if SOC_TIMER_GROUPS >= 2
  175. wdt_hal_context_t wdt1_context = {.inst = WDT_MWDT1, .mwdt_dev = &TIMERG1};
  176. #endif
  177. //Todo: Refactor to use Interrupt or Task Watchdog API, and a system level WDT context
  178. //Task WDT is the Main Watchdog Timer of Timer Group 0
  179. wdt_hal_write_protect_disable(&wdt0_context);
  180. wdt_hal_disable(&wdt0_context);
  181. wdt_hal_write_protect_enable(&wdt0_context);
  182. #if SOC_TIMER_GROUPS >= 2
  183. //Interupt WDT is the Main Watchdog Timer of Timer Group 1
  184. wdt_hal_write_protect_disable(&wdt1_context);
  185. wdt_hal_disable(&wdt1_context);
  186. wdt_hal_write_protect_enable(&wdt1_context);
  187. #endif
  188. }
  189. static void print_abort_details(const void *f)
  190. {
  191. panic_print_str(g_panic_abort_details);
  192. }
  193. // Control arrives from chip-specific panic handler, environment prepared for
  194. // the 'main' logic of panic handling. This means that chip-specific stuff have
  195. // already been done, and panic_info_t has been filled.
  196. void esp_panic_handler(panic_info_t *info)
  197. {
  198. // The port-level panic handler has already called this, but call it again
  199. // to reset the TG0WDT period
  200. esp_panic_handler_reconfigure_wdts(1000);
  201. // If the exception was due to an abort, override some of the panic info
  202. if (g_panic_abort) {
  203. info->description = NULL;
  204. info->details = g_panic_abort_details ? print_abort_details : NULL;
  205. info->reason = NULL;
  206. info->exception = PANIC_EXCEPTION_ABORT;
  207. }
  208. /*
  209. * For any supported chip, the panic handler prints the contents of panic_info_t in the following format:
  210. *
  211. *
  212. * Guru Meditation Error: Core <core> (<exception>). <description>
  213. * <details>
  214. *
  215. * <state>
  216. *
  217. * <elf_info>
  218. *
  219. *
  220. * ----------------------------------------------------------------------------------------
  221. * core - core where exception was triggered
  222. * exception - what kind of exception occurred
  223. * description - a short description regarding the exception that occurred
  224. * details - more details about the exception
  225. * state - processor state like register contents, and backtrace
  226. * elf_info - details about the image currently running
  227. *
  228. * NULL fields in panic_info_t are not printed.
  229. *
  230. * */
  231. if (info->reason) {
  232. panic_print_str("Guru Meditation Error: Core ");
  233. panic_print_dec(info->core);
  234. panic_print_str(" panic'ed (");
  235. panic_print_str(info->reason);
  236. panic_print_str("). ");
  237. }
  238. if (info->description) {
  239. panic_print_str(info->description);
  240. }
  241. panic_print_str("\r\n");
  242. PANIC_INFO_DUMP(info, details);
  243. panic_print_str("\r\n");
  244. // If on-chip-debugger is attached, and system is configured to be aware of this,
  245. // then only print up to details. Users should be able to probe for the other information
  246. // in debug mode.
  247. if (esp_cpu_dbgr_is_attached()) {
  248. panic_print_str("Setting breakpoint at 0x");
  249. panic_print_hex((uint32_t)info->addr);
  250. panic_print_str(" and returning...\r\n");
  251. disable_all_wdts();
  252. #if CONFIG_APPTRACE_ENABLE
  253. #if CONFIG_APPTRACE_SV_ENABLE
  254. SEGGER_RTT_ESP_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  255. #else
  256. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  257. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  258. #endif
  259. #endif
  260. esp_cpu_set_breakpoint(0, info->addr); // use breakpoint 0
  261. return;
  262. }
  263. // start panic WDT to restart system if we hang in this handler
  264. if (!wdt_hal_is_enabled(&rtc_wdt_ctx)) {
  265. wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
  266. uint32_t stage_timeout_ticks = (uint32_t)(7000ULL * rtc_clk_slow_freq_get_hz() / 1000ULL);
  267. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  268. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_SYSTEM);
  269. // 64KB of core dump data (stacks of about 30 tasks) will produce ~85KB base64 data.
  270. // @ 115200 UART speed it will take more than 6 sec to print them out.
  271. wdt_hal_enable(&rtc_wdt_ctx);
  272. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  273. }
  274. esp_panic_handler_reconfigure_wdts(1000); // Restart WDT again
  275. PANIC_INFO_DUMP(info, state);
  276. panic_print_str("\r\n");
  277. /* No matter if we come here from abort or an exception, this variable must be reset.
  278. * Else, any exception/error occurring during the current panic handler would considered
  279. * an abort. Do this after PANIC_INFO_DUMP(info, state) as it also checks this variable.
  280. * For example, if coredump triggers a stack overflow and this variable is not reset,
  281. * the second panic would be still be marked as the result of an abort, even the previous
  282. * message reason would be kept. */
  283. g_panic_abort = false;
  284. #ifdef WITH_ELF_SHA256
  285. panic_print_str("\r\nELF file SHA256: ");
  286. panic_print_str(esp_app_get_elf_sha256_str());
  287. panic_print_str("\r\n");
  288. #endif // WITH_ELF_SHA256
  289. panic_print_str("\r\n");
  290. #if CONFIG_APPTRACE_ENABLE
  291. disable_all_wdts();
  292. #if CONFIG_APPTRACE_SV_ENABLE
  293. SEGGER_RTT_ESP_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  294. #else
  295. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  296. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  297. #endif
  298. esp_panic_handler_reconfigure_wdts(1000); // restore WDT config
  299. #endif // CONFIG_APPTRACE_ENABLE
  300. #if CONFIG_ESP_COREDUMP_ENABLE
  301. static bool s_dumping_core;
  302. if (s_dumping_core) {
  303. panic_print_str("Re-entered core dump! Exception happened during core dump!\r\n");
  304. } else {
  305. disable_all_wdts();
  306. s_dumping_core = true;
  307. #if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
  308. esp_core_dump_to_flash(info);
  309. #endif
  310. #if CONFIG_ESP_COREDUMP_ENABLE_TO_UART && !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  311. esp_core_dump_to_uart(info);
  312. #endif
  313. s_dumping_core = false;
  314. esp_panic_handler_reconfigure_wdts(1000);
  315. }
  316. #endif /* CONFIG_ESP_COREDUMP_ENABLE */
  317. #if CONFIG_ESP_SYSTEM_PANIC_GDBSTUB
  318. disable_all_wdts();
  319. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  320. wdt_hal_disable(&rtc_wdt_ctx);
  321. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  322. panic_print_str("Entering gdb stub now.\r\n");
  323. esp_gdbstub_panic_handler((void *)info->frame);
  324. #else
  325. #if CONFIG_ESP_SYSTEM_PANIC_REBOOT_DELAY_SECONDS
  326. // start RTC WDT if it hasn't been started yet and set the timeout to more than the delay time
  327. wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
  328. uint32_t stage_timeout_ticks = (uint32_t)(((CONFIG_ESP_SYSTEM_PANIC_REBOOT_DELAY_SECONDS + 1) * 1000
  329. * rtc_clk_slow_freq_get_hz()) / 1000ULL);
  330. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  331. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_SYSTEM);
  332. // 64KB of core dump data (stacks of about 30 tasks) will produce ~85KB base64 data.
  333. // @ 115200 UART speed it will take more than 6 sec to print them out.
  334. wdt_hal_enable(&rtc_wdt_ctx);
  335. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  336. esp_panic_handler_reconfigure_wdts((CONFIG_ESP_SYSTEM_PANIC_REBOOT_DELAY_SECONDS + 1) * 1000);
  337. panic_print_str("Rebooting in ");
  338. panic_print_dec(CONFIG_ESP_SYSTEM_PANIC_REBOOT_DELAY_SECONDS);
  339. panic_print_str(" seconds...\r\n");
  340. esp_rom_delay_us(CONFIG_ESP_SYSTEM_PANIC_REBOOT_DELAY_SECONDS * 1000000);
  341. esp_panic_handler_reconfigure_wdts(1000);
  342. #endif /* CONFIG_ESP_SYSTEM_PANIC_REBOOT_DELAY_SECONDS */
  343. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  344. wdt_hal_disable(&rtc_wdt_ctx);
  345. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  346. #if CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT || CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  347. if (esp_reset_reason_get_hint() == ESP_RST_UNKNOWN) {
  348. switch (info->exception) {
  349. case PANIC_EXCEPTION_IWDT:
  350. esp_reset_reason_set_hint(ESP_RST_INT_WDT);
  351. break;
  352. case PANIC_EXCEPTION_TWDT:
  353. esp_reset_reason_set_hint(ESP_RST_TASK_WDT);
  354. break;
  355. case PANIC_EXCEPTION_ABORT:
  356. case PANIC_EXCEPTION_FAULT:
  357. default:
  358. esp_reset_reason_set_hint(ESP_RST_PANIC);
  359. break; // do not touch the previously set reset reason hint
  360. }
  361. }
  362. panic_print_str("Rebooting...\r\n");
  363. panic_restart();
  364. #else /* CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT || CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT */
  365. disable_all_wdts();
  366. panic_print_str("CPU halted.\r\n");
  367. esp_system_reset_modules_on_exit();
  368. while (1);
  369. #endif /* CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT || CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT */
  370. #endif /* CONFIG_ESP_SYSTEM_PANIC_GDBSTUB */
  371. }
  372. void IRAM_ATTR __attribute__((noreturn, no_sanitize_undefined)) panic_abort(const char *details)
  373. {
  374. g_panic_abort = true;
  375. g_panic_abort_details = (char *) details;
  376. #if CONFIG_APPTRACE_ENABLE
  377. #if CONFIG_APPTRACE_SV_ENABLE
  378. SEGGER_RTT_ESP_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  379. #else
  380. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  381. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  382. #endif
  383. #endif
  384. *((volatile int *) 0) = 0; // NOLINT(clang-analyzer-core.NullDereference) should be an invalid operation on targets
  385. while (1);
  386. }
  387. /* Weak versions of reset reason hint functions.
  388. * If these weren't provided, reset reason code would be linked into the app
  389. * even if the app never called esp_reset_reason().
  390. */
  391. void IRAM_ATTR __attribute__((weak)) esp_reset_reason_set_hint(esp_reset_reason_t hint)
  392. {
  393. }
  394. esp_reset_reason_t IRAM_ATTR __attribute__((weak)) esp_reset_reason_get_hint(void)
  395. {
  396. return ESP_RST_UNKNOWN;
  397. }