panic.c 13 KB

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