panic.c 12 KB

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