panic.c 13 KB

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