panic.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 <xtensa/config/core.h>
  15. #include "esp32/rom/rtc.h"
  16. #include "esp32/rom/uart.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "freertos/xtensa_api.h"
  20. #include "soc/uart_periph.h"
  21. #include "soc/gpio_periph.h"
  22. #include "soc/dport_reg.h"
  23. #include "soc/rtc_periph.h"
  24. #include "soc/timer_periph.h"
  25. #include "soc/cpu.h"
  26. #include "soc/rtc.h"
  27. #include "soc/rtc_wdt.h"
  28. #include "soc/soc_memory_layout.h"
  29. #include "esp_private/gdbstub.h"
  30. #include "esp_debug_helpers.h"
  31. #include "esp_private/panic_reason.h"
  32. #include "esp_attr.h"
  33. #include "esp_err.h"
  34. #include "esp_core_dump.h"
  35. #include "esp_spi_flash.h"
  36. #include "esp32/cache_err_int.h"
  37. #include "esp_app_trace.h"
  38. #include "esp_private/system_internal.h"
  39. #include "sdkconfig.h"
  40. #include "esp_ota_ops.h"
  41. #if CONFIG_SYSVIEW_ENABLE
  42. #include "SEGGER_RTT.h"
  43. #endif
  44. #if CONFIG_ESP32_APPTRACE_ONPANIC_HOST_FLUSH_TMO == -1
  45. #define APPTRACE_ONPANIC_HOST_FLUSH_TMO ESP_APPTRACE_TMO_INFINITE
  46. #else
  47. #define APPTRACE_ONPANIC_HOST_FLUSH_TMO (1000*CONFIG_ESP32_APPTRACE_ONPANIC_HOST_FLUSH_TMO)
  48. #endif
  49. /*
  50. Panic handlers; these get called when an unhandled exception occurs or the assembly-level
  51. task switching / interrupt code runs into an unrecoverable error. The default task stack
  52. overflow handler and abort handler are also in here.
  53. */
  54. /*
  55. Note: The linker script will put everything in this file in IRAM/DRAM, so it also works with flash cache disabled.
  56. */
  57. #if !CONFIG_ESP32_PANIC_SILENT_REBOOT
  58. //printf may be broken, so we fix our own printing fns...
  59. static void panicPutChar(char c)
  60. {
  61. while (((READ_PERI_REG(UART_STATUS_REG(CONFIG_ESP_CONSOLE_UART_NUM)) >> UART_TXFIFO_CNT_S)&UART_TXFIFO_CNT) >= 126) ;
  62. WRITE_PERI_REG(UART_FIFO_REG(CONFIG_ESP_CONSOLE_UART_NUM), c);
  63. }
  64. static void panicPutStr(const char *c)
  65. {
  66. int x = 0;
  67. while (c[x] != 0) {
  68. panicPutChar(c[x]);
  69. x++;
  70. }
  71. }
  72. static void panicPutHex(int a)
  73. {
  74. int x;
  75. int c;
  76. for (x = 0; x < 8; x++) {
  77. c = (a >> 28) & 0xf;
  78. if (c < 10) {
  79. panicPutChar('0' + c);
  80. } else {
  81. panicPutChar('a' + c - 10);
  82. }
  83. a <<= 4;
  84. }
  85. }
  86. static void panicPutDec(int a)
  87. {
  88. int n1, n2;
  89. n1 = a % 10;
  90. n2 = a / 10;
  91. if (n2 == 0) {
  92. panicPutChar(' ');
  93. } else {
  94. panicPutChar(n2 + '0');
  95. }
  96. panicPutChar(n1 + '0');
  97. }
  98. #else
  99. //No printing wanted. Stub out these functions.
  100. static void panicPutChar(char c) { }
  101. static void panicPutStr(const char *c) { }
  102. static void panicPutHex(int a) { }
  103. static void panicPutDec(int a) { }
  104. #endif
  105. void __attribute__((weak)) vApplicationStackOverflowHook( TaskHandle_t xTask, signed char *pcTaskName )
  106. {
  107. panicPutStr("***ERROR*** A stack overflow in task ");
  108. panicPutStr((char *)pcTaskName);
  109. panicPutStr(" has been detected.\r\n");
  110. abort();
  111. }
  112. /* These two weak stubs for esp_reset_reason_{get,set}_hint are used when
  113. * the application does not call esp_reset_reason() function, and
  114. * reset_reason.c is not linked into the output file.
  115. */
  116. void __attribute__((weak)) esp_reset_reason_set_hint(esp_reset_reason_t hint)
  117. {
  118. }
  119. esp_reset_reason_t __attribute__((weak)) esp_reset_reason_get_hint(void)
  120. {
  121. return ESP_RST_UNKNOWN;
  122. }
  123. static bool abort_called;
  124. static __attribute__((noreturn)) inline void invoke_abort()
  125. {
  126. abort_called = true;
  127. #if CONFIG_ESP32_APPTRACE_ENABLE
  128. #if CONFIG_SYSVIEW_ENABLE
  129. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  130. #else
  131. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  132. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  133. #endif
  134. #endif
  135. while (1) {
  136. if (esp_cpu_in_ocd_debug_mode()) {
  137. __asm__ ("break 0,0");
  138. }
  139. *((int *) 0) = 0;
  140. }
  141. }
  142. void abort()
  143. {
  144. #if !CONFIG_ESP32_PANIC_SILENT_REBOOT
  145. ets_printf("abort() was called at PC 0x%08x on core %d\r\n", (intptr_t)__builtin_return_address(0) - 3, xPortGetCoreID());
  146. #endif
  147. /* Calling code might have set other reset reason hint (such as Task WDT),
  148. * don't overwrite that.
  149. */
  150. if (esp_reset_reason_get_hint() == ESP_RST_UNKNOWN) {
  151. esp_reset_reason_set_hint(ESP_RST_PANIC);
  152. }
  153. invoke_abort();
  154. }
  155. static const char *edesc[] = {
  156. "IllegalInstruction", "Syscall", "InstructionFetchError", "LoadStoreError",
  157. "Level1Interrupt", "Alloca", "IntegerDivideByZero", "PCValue",
  158. "Privileged", "LoadStoreAlignment", "res", "res",
  159. "InstrPDAddrError", "LoadStorePIFDataError", "InstrPIFAddrError", "LoadStorePIFAddrError",
  160. "InstTLBMiss", "InstTLBMultiHit", "InstFetchPrivilege", "res",
  161. "InstrFetchProhibited", "res", "res", "res",
  162. "LoadStoreTLBMiss", "LoadStoreTLBMultihit", "LoadStorePrivilege", "res",
  163. "LoadProhibited", "StoreProhibited", "res", "res",
  164. "Cp0Dis", "Cp1Dis", "Cp2Dis", "Cp3Dis",
  165. "Cp4Dis", "Cp5Dis", "Cp6Dis", "Cp7Dis"
  166. };
  167. #define NUM_EDESCS (sizeof(edesc) / sizeof(char *))
  168. extern int _invalid_pc_placeholder;
  169. static void commonErrorHandler(XtExcFrame *frame);
  170. static inline void disableAllWdts();
  171. static void illegal_instruction_helper(XtExcFrame *frame);
  172. //The fact that we've panic'ed probably means the other CPU is now running wild, possibly
  173. //messing up the serial output, so we stall it here.
  174. static void haltOtherCore()
  175. {
  176. esp_cpu_stall( xPortGetCoreID() == 0 ? 1 : 0 );
  177. }
  178. static void setFirstBreakpoint(uint32_t pc)
  179. {
  180. asm(
  181. "wsr.ibreaka0 %0\n" \
  182. "rsr.ibreakenable a3\n" \
  183. "movi a4,1\n" \
  184. "or a4, a4, a3\n" \
  185. "wsr.ibreakenable a4\n" \
  186. ::"r"(pc):"a3", "a4");
  187. }
  188. //When interrupt watchdog happen in one core, both cores will be interrupted.
  189. //The core which doesn't trigger the interrupt watchdog will save the frame and return.
  190. //The core which triggers the interrupt watchdog will use the saved frame, and dump frames for both cores.
  191. #if !CONFIG_FREERTOS_UNICORE
  192. static volatile XtExcFrame * other_core_frame = NULL;
  193. #endif //!CONFIG_FREERTOS_UNICORE
  194. void panicHandler(XtExcFrame *frame)
  195. {
  196. int core_id = xPortGetCoreID();
  197. //Please keep in sync with PANIC_RSN_* defines
  198. const char *reasons[] = {
  199. "Unknown reason",
  200. "Unhandled debug exception",
  201. "Double exception",
  202. "Unhandled kernel exception",
  203. "Coprocessor exception",
  204. "Interrupt wdt timeout on CPU0",
  205. "Interrupt wdt timeout on CPU1",
  206. "Cache disabled but cached memory region accessed",
  207. };
  208. const char *reason = reasons[0];
  209. //The panic reason is stored in the EXCCAUSE register.
  210. if (frame->exccause <= PANIC_RSN_MAX) {
  211. reason = reasons[frame->exccause];
  212. }
  213. #if !CONFIG_FREERTOS_UNICORE
  214. //Save frame for other core.
  215. if ((frame->exccause == PANIC_RSN_INTWDT_CPU0 && core_id == 1) || (frame->exccause == PANIC_RSN_INTWDT_CPU1 && core_id == 0)) {
  216. other_core_frame = frame;
  217. while (1);
  218. }
  219. //The core which triggers the interrupt watchdog will delay 500 us, so the other core can save its frame.
  220. if (frame->exccause == PANIC_RSN_INTWDT_CPU0 || frame->exccause == PANIC_RSN_INTWDT_CPU1) {
  221. ets_delay_us(500);
  222. }
  223. if (frame->exccause == PANIC_RSN_CACHEERR && esp_cache_err_get_cpuid() != core_id) {
  224. // Cache error interrupt will be handled by the panic handler
  225. // on the other CPU.
  226. while (1);
  227. }
  228. #endif //!CONFIG_FREERTOS_UNICORE
  229. if (frame->exccause == PANIC_RSN_INTWDT_CPU0 || frame->exccause == PANIC_RSN_INTWDT_CPU1) {
  230. esp_reset_reason_set_hint(ESP_RST_INT_WDT);
  231. }
  232. haltOtherCore();
  233. esp_dport_access_int_abort();
  234. panicPutStr("Guru Meditation Error: Core ");
  235. panicPutDec(core_id);
  236. panicPutStr(" panic'ed (");
  237. panicPutStr(reason);
  238. panicPutStr(")\r\n");
  239. if (frame->exccause == PANIC_RSN_DEBUGEXCEPTION) {
  240. int debugRsn;
  241. asm("rsr.debugcause %0":"=r"(debugRsn));
  242. panicPutStr("Debug exception reason: ");
  243. if (debugRsn & XCHAL_DEBUGCAUSE_ICOUNT_MASK) {
  244. panicPutStr("SingleStep ");
  245. }
  246. if (debugRsn & XCHAL_DEBUGCAUSE_IBREAK_MASK) {
  247. panicPutStr("HwBreakpoint ");
  248. }
  249. if (debugRsn & XCHAL_DEBUGCAUSE_DBREAK_MASK) {
  250. //Unlike what the ISA manual says, this core seemingly distinguishes from a DBREAK
  251. //reason caused by watchdog 0 and one caused by watchdog 1 by setting bit 8 of the
  252. //debugcause if the cause is watchdog 1 and clearing it if it's watchdog 0.
  253. if (debugRsn & (1 << 8)) {
  254. #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
  255. const char *name = pcTaskGetTaskName(xTaskGetCurrentTaskHandleForCPU(core_id));
  256. panicPutStr("Stack canary watchpoint triggered (");
  257. panicPutStr(name);
  258. panicPutStr(") ");
  259. #else
  260. panicPutStr("Watchpoint 1 triggered ");
  261. #endif
  262. } else {
  263. panicPutStr("Watchpoint 0 triggered ");
  264. }
  265. }
  266. if (debugRsn & XCHAL_DEBUGCAUSE_BREAK_MASK) {
  267. panicPutStr("BREAK instr ");
  268. }
  269. if (debugRsn & XCHAL_DEBUGCAUSE_BREAKN_MASK) {
  270. panicPutStr("BREAKN instr ");
  271. }
  272. if (debugRsn & XCHAL_DEBUGCAUSE_DEBUGINT_MASK) {
  273. panicPutStr("DebugIntr ");
  274. }
  275. panicPutStr("\r\n");
  276. }
  277. if (esp_cpu_in_ocd_debug_mode()) {
  278. disableAllWdts();
  279. if (!(esp_ptr_executable((void *)((frame->pc & 0x3fffffffU) | 0x40000000U)) && (frame->pc & 0xC0000000U))) {
  280. /* Xtensa ABI sets the 2 MSBs of the PC according to the windowed call size
  281. * Incase the PC is invalid, GDB will fail to translate addresses to function names
  282. * Hence replacing the PC to a placeholder address in case of invalid PC
  283. */
  284. frame->pc = (uint32_t)&_invalid_pc_placeholder;
  285. }
  286. if (frame->exccause == PANIC_RSN_INTWDT_CPU0 ||
  287. frame->exccause == PANIC_RSN_INTWDT_CPU1) {
  288. TIMERG1.int_clr_timers.wdt = 1;
  289. }
  290. #if CONFIG_ESP32_APPTRACE_ENABLE
  291. #if CONFIG_SYSVIEW_ENABLE
  292. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  293. #else
  294. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  295. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  296. #endif
  297. #endif
  298. setFirstBreakpoint(frame->pc);
  299. return;
  300. }
  301. commonErrorHandler(frame);
  302. }
  303. void xt_unhandled_exception(XtExcFrame *frame)
  304. {
  305. haltOtherCore();
  306. esp_dport_access_int_abort();
  307. if (!abort_called) {
  308. panicPutStr("Guru Meditation Error: Core ");
  309. panicPutDec(xPortGetCoreID());
  310. panicPutStr(" panic'ed (");
  311. int exccause = frame->exccause;
  312. if (exccause < NUM_EDESCS) {
  313. panicPutStr(edesc[exccause]);
  314. } else {
  315. panicPutStr("Unknown");
  316. }
  317. panicPutStr(")");
  318. if (esp_cpu_in_ocd_debug_mode()) {
  319. panicPutStr(" at pc=");
  320. panicPutHex(frame->pc);
  321. panicPutStr(". Setting bp and returning..\r\n");
  322. #if CONFIG_ESP32_APPTRACE_ENABLE
  323. #if CONFIG_SYSVIEW_ENABLE
  324. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  325. #else
  326. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  327. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  328. #endif
  329. #endif
  330. if (!(esp_ptr_executable((void *)((frame->pc & 0x3fffffffU) | 0x40000000U)) && (frame->pc & 0xC0000000U))) {
  331. /* Xtensa ABI sets the 2 MSBs of the PC according to the windowed call size
  332. * Incase the PC is invalid, GDB will fail to translate addresses to function names
  333. * Hence replacing the PC to a placeholder address in case of invalid PC
  334. */
  335. frame->pc = (uint32_t)&_invalid_pc_placeholder;
  336. }
  337. //Stick a hardware breakpoint on the address the handler returns to. This way, the OCD debugger
  338. //will kick in exactly at the context the error happened.
  339. setFirstBreakpoint(frame->pc);
  340. return;
  341. }
  342. panicPutStr(". Exception was unhandled.\r\n");
  343. if (exccause == 0 /* IllegalInstruction */) {
  344. illegal_instruction_helper(frame);
  345. }
  346. esp_reset_reason_set_hint(ESP_RST_PANIC);
  347. }
  348. commonErrorHandler(frame);
  349. }
  350. static void illegal_instruction_helper(XtExcFrame *frame)
  351. {
  352. /* Print out memory around the instruction word */
  353. uint32_t epc = frame->pc;
  354. epc = (epc & ~0x3) - 4;
  355. /* check that the address was sane */
  356. if (epc < SOC_IROM_MASK_LOW || epc >= SOC_IROM_HIGH) {
  357. return;
  358. }
  359. volatile uint32_t* pepc = (uint32_t*)epc;
  360. panicPutStr("Memory dump at 0x");
  361. panicPutHex(epc);
  362. panicPutStr(": ");
  363. panicPutHex(*pepc);
  364. panicPutStr(" ");
  365. panicPutHex(*(pepc + 1));
  366. panicPutStr(" ");
  367. panicPutHex(*(pepc + 2));
  368. panicPutStr("\r\n");
  369. }
  370. /*
  371. If watchdogs are enabled, the panic handler runs the risk of getting aborted pre-emptively because
  372. an overzealous watchdog decides to reset it. On the other hand, if we disable all watchdogs, we run
  373. the risk of somehow halting in the panic handler and not resetting. That is why this routine kills
  374. all watchdogs except the timer group 0 watchdog, and it reconfigures that to reset the chip after
  375. one second.
  376. */
  377. static void reconfigureAllWdts()
  378. {
  379. TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  380. TIMERG0.wdt_feed = 1;
  381. TIMERG0.wdt_config0.sys_reset_length = 7; //3.2uS
  382. TIMERG0.wdt_config0.cpu_reset_length = 7; //3.2uS
  383. TIMERG0.wdt_config0.stg0 = TIMG_WDT_STG_SEL_RESET_SYSTEM; //1st stage timeout: reset system
  384. TIMERG0.wdt_config1.clk_prescale = 80 * 500; //Prescaler: wdt counts in ticks of 0.5mS
  385. TIMERG0.wdt_config2 = 2000; //1 second before reset
  386. TIMERG0.wdt_config0.en = 1;
  387. TIMERG0.wdt_wprotect = 0;
  388. //Disable wdt 1
  389. TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  390. TIMERG1.wdt_config0.en = 0;
  391. TIMERG1.wdt_wprotect = 0;
  392. }
  393. /*
  394. This disables all the watchdogs for when we call the gdbstub.
  395. */
  396. static inline void disableAllWdts()
  397. {
  398. TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  399. TIMERG0.wdt_config0.en = 0;
  400. TIMERG0.wdt_wprotect = 0;
  401. TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  402. TIMERG1.wdt_config0.en = 0;
  403. TIMERG1.wdt_wprotect = 0;
  404. }
  405. #if CONFIG_ESP32_PANIC_PRINT_REBOOT || CONFIG_ESP32_PANIC_SILENT_REBOOT
  406. static void esp_panic_dig_reset() __attribute__((noreturn));
  407. static void esp_panic_dig_reset()
  408. {
  409. // make sure all the panic handler output is sent from UART FIFO
  410. uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  411. // switch to XTAL (otherwise we will keep running from the PLL)
  412. rtc_clk_cpu_freq_set_xtal();
  413. // reset the digital part
  414. esp_cpu_unstall(PRO_CPU_NUM);
  415. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST);
  416. while (true) {
  417. ;
  418. }
  419. }
  420. #endif
  421. static void putEntry(uint32_t pc, uint32_t sp)
  422. {
  423. panicPutStr(" 0x");
  424. panicPutHex(pc);
  425. panicPutStr(":0x");
  426. panicPutHex(sp);
  427. }
  428. static void doBacktrace(XtExcFrame *exc_frame, int depth)
  429. {
  430. //Initialize stk_frame with first frame of stack
  431. esp_backtrace_frame_t stk_frame = {.pc = exc_frame->pc, .sp = exc_frame->a1, .next_pc = exc_frame->a0};
  432. panicPutStr("\r\nBacktrace:");
  433. putEntry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp);
  434. //Check if first frame is valid
  435. bool corrupted = (esp_stack_ptr_is_sane(stk_frame.sp) &&
  436. esp_ptr_executable((void*)esp_cpu_process_stack_pc(stk_frame.pc))) ?
  437. false : true;
  438. uint32_t i = ((depth <= 0) ? INT32_MAX : depth) - 1; //Account for stack frame that's already printed
  439. while (i-- > 0 && stk_frame.next_pc != 0 && !corrupted) {
  440. if (!esp_backtrace_get_next_frame(&stk_frame)) { //Get next stack frame
  441. corrupted = true;
  442. }
  443. putEntry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp);
  444. }
  445. //Print backtrace termination marker
  446. if (corrupted) {
  447. panicPutStr(" |<-CORRUPTED");
  448. } else if (stk_frame.next_pc != 0) { //Backtrace continues
  449. panicPutStr(" |<-CONTINUES");
  450. }
  451. panicPutStr("\r\n");
  452. }
  453. /*
  454. * Dump registers and do backtrace.
  455. */
  456. static void commonErrorHandler_dump(XtExcFrame *frame, int core_id)
  457. {
  458. int *regs = (int *)frame;
  459. int x, y;
  460. const char *sdesc[] = {
  461. "PC ", "PS ", "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ",
  462. "A6 ", "A7 ", "A8 ", "A9 ", "A10 ", "A11 ", "A12 ", "A13 ",
  463. "A14 ", "A15 ", "SAR ", "EXCCAUSE", "EXCVADDR", "LBEG ", "LEND ", "LCOUNT "
  464. };
  465. /* only dump registers for 'real' crashes, if crashing via abort()
  466. the register window is no longer useful.
  467. */
  468. if (!abort_called) {
  469. panicPutStr("Core");
  470. panicPutDec(core_id);
  471. panicPutStr(" register dump:\r\n");
  472. for (x = 0; x < 24; x += 4) {
  473. for (y = 0; y < 4; y++) {
  474. if (sdesc[x + y][0] != 0) {
  475. panicPutStr(sdesc[x + y]);
  476. panicPutStr(": 0x");
  477. panicPutHex(regs[x + y + 1]);
  478. panicPutStr(" ");
  479. }
  480. }
  481. panicPutStr("\r\n");
  482. }
  483. if (xPortInterruptedFromISRContext()
  484. #if !CONFIG_FREERTOS_UNICORE
  485. && other_core_frame != frame
  486. #endif //!CONFIG_FREERTOS_UNICORE
  487. ) {
  488. //If the core which triggers the interrupt watchdog was in ISR context, dump the epc registers.
  489. uint32_t __value;
  490. panicPutStr("Core");
  491. panicPutDec(core_id);
  492. panicPutStr(" was running in ISR context:\r\n");
  493. __asm__("rsr.epc1 %0" : "=a"(__value));
  494. panicPutStr("EPC1 : 0x");
  495. panicPutHex(__value);
  496. __asm__("rsr.epc2 %0" : "=a"(__value));
  497. panicPutStr(" EPC2 : 0x");
  498. panicPutHex(__value);
  499. __asm__("rsr.epc3 %0" : "=a"(__value));
  500. panicPutStr(" EPC3 : 0x");
  501. panicPutHex(__value);
  502. __asm__("rsr.epc4 %0" : "=a"(__value));
  503. panicPutStr(" EPC4 : 0x");
  504. panicPutHex(__value);
  505. panicPutStr("\r\n");
  506. }
  507. }
  508. panicPutStr("\r\nELF file SHA256: ");
  509. char sha256_buf[65];
  510. esp_ota_get_app_elf_sha256(sha256_buf, sizeof(sha256_buf));
  511. panicPutStr(sha256_buf);
  512. panicPutStr("\r\n");
  513. /* With windowed ABI backtracing is easy, let's do it. */
  514. doBacktrace(frame, 100);
  515. panicPutStr("\r\n");
  516. }
  517. /*
  518. We arrive here after a panic or unhandled exception, when no OCD is detected. Dump the registers to the
  519. serial port and either jump to the gdb stub, halt the CPU or reboot.
  520. */
  521. static __attribute__((noreturn)) void commonErrorHandler(XtExcFrame *frame)
  522. {
  523. int core_id = xPortGetCoreID();
  524. // start panic WDT to restart system if we hang in this handler
  525. if (!rtc_wdt_is_on()) {
  526. rtc_wdt_protect_off();
  527. rtc_wdt_disable();
  528. rtc_wdt_set_length_of_reset_signal(RTC_WDT_SYS_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  529. rtc_wdt_set_length_of_reset_signal(RTC_WDT_CPU_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  530. rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_SYSTEM);
  531. // 64KB of core dump data (stacks of about 30 tasks) will produce ~85KB base64 data.
  532. // @ 115200 UART speed it will take more than 6 sec to print them out.
  533. rtc_wdt_set_time(RTC_WDT_STAGE0, 7000);
  534. rtc_wdt_enable();
  535. rtc_wdt_protect_on();
  536. }
  537. //Feed the watchdogs, so they will give us time to print out debug info
  538. reconfigureAllWdts();
  539. commonErrorHandler_dump(frame, core_id);
  540. #if !CONFIG_FREERTOS_UNICORE
  541. if (other_core_frame != NULL) {
  542. commonErrorHandler_dump((XtExcFrame *)other_core_frame, (core_id ? 0 : 1));
  543. }
  544. #endif //!CONFIG_FREERTOS_UNICORE
  545. #if CONFIG_ESP32_APPTRACE_ENABLE
  546. disableAllWdts();
  547. #if CONFIG_SYSVIEW_ENABLE
  548. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  549. #else
  550. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  551. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  552. #endif
  553. reconfigureAllWdts();
  554. #endif
  555. #if CONFIG_ESP32_PANIC_GDBSTUB
  556. disableAllWdts();
  557. rtc_wdt_disable();
  558. panicPutStr("Entering gdb stub now.\r\n");
  559. esp_gdbstub_panic_handler(frame);
  560. #else
  561. #if CONFIG_ESP32_ENABLE_COREDUMP
  562. static bool s_dumping_core;
  563. if (s_dumping_core) {
  564. panicPutStr("Re-entered core dump! Exception happened during core dump!\r\n");
  565. } else {
  566. disableAllWdts();
  567. s_dumping_core = true;
  568. #if CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH
  569. esp_core_dump_to_flash(frame);
  570. #endif
  571. #if CONFIG_ESP32_ENABLE_COREDUMP_TO_UART && !CONFIG_ESP32_PANIC_SILENT_REBOOT
  572. esp_core_dump_to_uart(frame);
  573. #endif
  574. s_dumping_core = false;
  575. reconfigureAllWdts();
  576. }
  577. #endif /* CONFIG_ESP32_ENABLE_COREDUMP */
  578. rtc_wdt_disable();
  579. #if CONFIG_ESP32_PANIC_PRINT_REBOOT || CONFIG_ESP32_PANIC_SILENT_REBOOT
  580. panicPutStr("Rebooting...\r\n");
  581. if (esp_cache_err_get_cpuid() == -1) {
  582. esp_restart_noos();
  583. } else {
  584. // The only way to clear invalid cache access interrupt is to reset the digital part
  585. esp_panic_dig_reset();
  586. }
  587. #else
  588. disableAllWdts();
  589. panicPutStr("CPU halted.\r\n");
  590. while (1);
  591. #endif /* CONFIG_ESP32_PANIC_PRINT_REBOOT || CONFIG_ESP32_PANIC_SILENT_REBOOT */
  592. #endif /* CONFIG_ESP32_PANIC_GDBSTUB */
  593. }
  594. void esp_set_breakpoint_if_jtag(void *fn)
  595. {
  596. if (esp_cpu_in_ocd_debug_mode()) {
  597. setFirstBreakpoint((uint32_t)fn);
  598. }
  599. }
  600. esp_err_t esp_set_watchpoint(int no, void *adr, int size, int flags)
  601. {
  602. int x;
  603. if (no < 0 || no > 1) {
  604. return ESP_ERR_INVALID_ARG;
  605. }
  606. if (flags & (~0xC0000000)) {
  607. return ESP_ERR_INVALID_ARG;
  608. }
  609. int dbreakc = 0x3F;
  610. //We support watching 2^n byte values, from 1 to 64. Calculate the mask for that.
  611. for (x = 0; x < 7; x++) {
  612. if (size == (1 << x)) {
  613. break;
  614. }
  615. dbreakc <<= 1;
  616. }
  617. if (x == 7) {
  618. return ESP_ERR_INVALID_ARG;
  619. }
  620. //Mask mask and add in flags.
  621. dbreakc = (dbreakc & 0x3f) | flags;
  622. if (no == 0) {
  623. asm volatile(
  624. "wsr.dbreaka0 %0\n" \
  625. "wsr.dbreakc0 %1\n" \
  626. ::"r"(adr), "r"(dbreakc));
  627. } else {
  628. asm volatile(
  629. "wsr.dbreaka1 %0\n" \
  630. "wsr.dbreakc1 %1\n" \
  631. ::"r"(adr), "r"(dbreakc));
  632. }
  633. return ESP_OK;
  634. }
  635. void esp_clear_watchpoint(int no)
  636. {
  637. //Setting a dbreakc register to 0 makes it trigger on neither load nor store, effectively disabling it.
  638. int dbreakc = 0;
  639. if (no == 0) {
  640. asm volatile(
  641. "wsr.dbreakc0 %0\n" \
  642. ::"r"(dbreakc));
  643. } else {
  644. asm volatile(
  645. "wsr.dbreakc1 %0\n" \
  646. ::"r"(dbreakc));
  647. }
  648. }
  649. static void esp_error_check_failed_print(const char *msg, esp_err_t rc, const char *file, int line, const char *function, const char *expression)
  650. {
  651. ets_printf("%s failed: esp_err_t 0x%x", msg, rc);
  652. #ifdef CONFIG_ESP_ERR_TO_NAME_LOOKUP
  653. ets_printf(" (%s)", esp_err_to_name(rc));
  654. #endif //CONFIG_ESP_ERR_TO_NAME_LOOKUP
  655. ets_printf(" at 0x%08x\n", (intptr_t)__builtin_return_address(0) - 3);
  656. if (spi_flash_cache_enabled()) { // strings may be in flash cache
  657. ets_printf("file: \"%s\" line %d\nfunc: %s\nexpression: %s\n", file, line, function, expression);
  658. }
  659. }
  660. void _esp_error_check_failed_without_abort(esp_err_t rc, const char *file, int line, const char *function, const char *expression)
  661. {
  662. esp_error_check_failed_print("ESP_ERROR_CHECK_WITHOUT_ABORT", rc, file, line, function, expression);
  663. }
  664. void _esp_error_check_failed(esp_err_t rc, const char *file, int line, const char *function, const char *expression)
  665. {
  666. esp_error_check_failed_print("ESP_ERROR_CHECK", rc, file, line, function, expression);
  667. invoke_abort();
  668. }