panic.c 14 KB

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