panic.c 23 KB

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