panic.c 21 KB

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