panic.c 13 KB

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