panic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 "esp32s2beta/rom/rtc.h"
  16. #include "esp32s2beta/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/soc_memory_layout.h"
  28. #include "soc/rtc.h"
  29. #include "soc/rtc_wdt.h"
  30. #include "esp_private/gdbstub.h"
  31. #include "esp_debug_helpers.h"
  32. #include "esp_private/panic_reason.h"
  33. #include "esp_attr.h"
  34. #include "esp_err.h"
  35. #include "esp_core_dump.h"
  36. #include "esp_spi_flash.h"
  37. #include "esp32s2beta/cache_err_int.h"
  38. #include "esp_app_trace.h"
  39. #include "esp_private/system_internal.h"
  40. #include "sdkconfig.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_ESP32S2_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_AHB_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. static bool abort_called;
  113. static __attribute__((noreturn)) inline void invoke_abort(void)
  114. {
  115. abort_called = true;
  116. #if CONFIG_ESP32_APPTRACE_ENABLE
  117. #if CONFIG_SYSVIEW_ENABLE
  118. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_TRAX_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  119. #else
  120. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_TRAX_THRESH,
  121. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  122. #endif
  123. #endif
  124. while (1) {
  125. if (esp_cpu_in_ocd_debug_mode()) {
  126. __asm__ ("break 0,0");
  127. }
  128. *((int *) 0) = 0;
  129. }
  130. }
  131. void abort(void)
  132. {
  133. #if !CONFIG_ESP32S2_PANIC_SILENT_REBOOT
  134. ets_printf("abort() was called at PC 0x%08x on core %d\r\n", (intptr_t)__builtin_return_address(0) - 3, xPortGetCoreID());
  135. #endif
  136. invoke_abort();
  137. }
  138. static const char *edesc[] = {
  139. "IllegalInstruction", "Syscall", "InstructionFetchError", "LoadStoreError",
  140. "Level1Interrupt", "Alloca", "IntegerDivideByZero", "PCValue",
  141. "Privileged", "LoadStoreAlignment", "res", "res",
  142. "InstrPDAddrError", "LoadStorePIFDataError", "InstrPIFAddrError", "LoadStorePIFAddrError",
  143. "InstTLBMiss", "InstTLBMultiHit", "InstFetchPrivilege", "res",
  144. "InstrFetchProhibited", "res", "res", "res",
  145. "LoadStoreTLBMiss", "LoadStoreTLBMultihit", "LoadStorePrivilege", "res",
  146. "LoadProhibited", "StoreProhibited", "res", "res",
  147. "Cp0Dis", "Cp1Dis", "Cp2Dis", "Cp3Dis",
  148. "Cp4Dis", "Cp5Dis", "Cp6Dis", "Cp7Dis"
  149. };
  150. #define NUM_EDESCS (sizeof(edesc) / sizeof(char *))
  151. static void commonErrorHandler(XtExcFrame *frame);
  152. static inline void disableAllWdts(void);
  153. //The fact that we've panic'ed probably means the other CPU is now running wild, possibly
  154. //messing up the serial output, so we stall it here.
  155. static void haltOtherCore(void)
  156. {
  157. esp_cpu_stall( xPortGetCoreID() == 0 ? 1 : 0 );
  158. }
  159. static void setFirstBreakpoint(uint32_t pc)
  160. {
  161. asm(
  162. "wsr.ibreaka0 %0\n" \
  163. "rsr.ibreakenable a3\n" \
  164. "movi a4,1\n" \
  165. "or a4, a4, a3\n" \
  166. "wsr.ibreakenable a4\n" \
  167. ::"r"(pc):"a3", "a4");
  168. }
  169. static inline void printCacheError(void)
  170. {
  171. uint32_t vaddr = 0, size = 0;
  172. uint32_t status[2];
  173. status[0] = REG_READ(DPORT_CACHE_DBG_STATUS0_REG);
  174. status[1] = REG_READ(DPORT_CACHE_DBG_STATUS1_REG);
  175. for (int i = 0; i < 32; i++) {
  176. switch (status[0] & BIT(i))
  177. {
  178. case DPORT_IC_SYNC_SIZE_FAULT_ST:
  179. vaddr = REG_READ(DPORT_PRO_ICACHE_MEM_SYNC0_REG);
  180. size = REG_READ(DPORT_PRO_ICACHE_MEM_SYNC1_REG);
  181. panicPutStr("Icache sync parameter configuration error, the error address and size is 0x");
  182. panicPutHex(vaddr);
  183. panicPutStr("(0x");
  184. panicPutHex(size);
  185. panicPutStr(")\r\n");
  186. break;
  187. case DPORT_IC_PRELOAD_SIZE_FAULT_ST:
  188. vaddr = REG_READ(DPORT_PRO_ICACHE_PRELOAD_ADDR_REG);
  189. size = REG_READ(DPORT_PRO_ICACHE_PRELOAD_SIZE_REG);
  190. panicPutStr("Icache preload parameter configuration error, the error address and size is 0x");
  191. panicPutHex(vaddr);
  192. panicPutStr("(0x");
  193. panicPutHex(size);
  194. panicPutStr(")\r\n");
  195. break;
  196. case DPORT_ICACHE_REJECT_ST:
  197. vaddr = REG_READ(DPORT_PRO_ICACHE_REJECT_VADDR_REG);
  198. panicPutStr("Icache reject error occurred while accessing the address 0x");
  199. panicPutHex(vaddr);
  200. if (REG_READ(DPORT_PRO_CACHE_MMU_ERROR_CONTENT_REG) & DPORT_MMU_INVALID) {
  201. panicPutStr(" (invalid mmu entry)");
  202. }
  203. panicPutStr("\r\n");
  204. break;
  205. default:
  206. break;
  207. }
  208. switch (status[1] & BIT(i))
  209. {
  210. case DPORT_DC_SYNC_SIZE_FAULT_ST:
  211. vaddr = REG_READ(DPORT_PRO_DCACHE_MEM_SYNC0_REG);
  212. size = REG_READ(DPORT_PRO_DCACHE_MEM_SYNC1_REG);
  213. panicPutStr("Dcache sync parameter configuration error, the error address and size is 0x");
  214. panicPutHex(vaddr);
  215. panicPutStr("(0x");
  216. panicPutHex(size);
  217. panicPutStr(")\r\n");
  218. break;
  219. case DPORT_DC_PRELOAD_SIZE_FAULT_ST:
  220. vaddr = REG_READ(DPORT_PRO_DCACHE_PRELOAD_ADDR_REG);
  221. size = REG_READ(DPORT_PRO_DCACHE_PRELOAD_SIZE_REG);
  222. panicPutStr("Dcache preload parameter configuration error, the error address and size is 0x");
  223. panicPutHex(vaddr);
  224. panicPutStr("(0x");
  225. panicPutHex(size);
  226. panicPutStr(")\r\n");
  227. break;
  228. case DPORT_DCACHE_WRITE_FLASH_ST:
  229. panicPutStr("Write back error occurred while dcache tries to write back to flash\r\n");
  230. break;
  231. case DPORT_DCACHE_REJECT_ST:
  232. vaddr = REG_READ(DPORT_PRO_DCACHE_REJECT_VADDR_REG);
  233. panicPutStr("Dcache reject error occurred while accessing the address 0x");
  234. panicPutHex(vaddr);
  235. if (REG_READ(DPORT_PRO_CACHE_MMU_ERROR_CONTENT_REG) & DPORT_MMU_INVALID) {
  236. panicPutStr(" (invalid mmu entry)");
  237. }
  238. panicPutStr("\r\n");
  239. break;
  240. case DPORT_MMU_ENTRY_FAULT_ST:
  241. vaddr = REG_READ(DPORT_PRO_CACHE_MMU_ERROR_VADDR_REG);
  242. panicPutStr("MMU entry fault error occurred while accessing the address 0x");
  243. panicPutHex(vaddr);
  244. if (REG_READ(DPORT_PRO_CACHE_MMU_ERROR_CONTENT_REG) & DPORT_MMU_INVALID) {
  245. panicPutStr(" (invalid mmu entry)");
  246. }
  247. panicPutStr("\r\n");
  248. break;
  249. default:
  250. break;
  251. }
  252. }
  253. panicPutStr("\r\n");
  254. }
  255. //When interrupt watchdog happen in one core, both cores will be interrupted.
  256. //The core which doesn't trigger the interrupt watchdog will save the frame and return.
  257. //The core which triggers the interrupt watchdog will use the saved frame, and dump frames for both cores.
  258. #if !CONFIG_FREERTOS_UNICORE
  259. static volatile XtExcFrame * other_core_frame = NULL;
  260. #endif //!CONFIG_FREERTOS_UNICORE
  261. void panicHandler(XtExcFrame *frame)
  262. {
  263. int core_id = xPortGetCoreID();
  264. //Please keep in sync with PANIC_RSN_* defines
  265. const char *reasons[] = {
  266. "Unknown reason",
  267. "Unhandled debug exception",
  268. "Double exception",
  269. "Unhandled kernel exception",
  270. "Coprocessor exception",
  271. "Interrupt wdt timeout on CPU0",
  272. "Interrupt wdt timeout on CPU1",
  273. "Cache exception",
  274. };
  275. const char *reason = reasons[0];
  276. //The panic reason is stored in the EXCCAUSE register.
  277. if (frame->exccause <= PANIC_RSN_MAX) {
  278. reason = reasons[frame->exccause];
  279. }
  280. #if !CONFIG_FREERTOS_UNICORE
  281. //Save frame for other core.
  282. if ((frame->exccause == PANIC_RSN_INTWDT_CPU0 && core_id == 1) || (frame->exccause == PANIC_RSN_INTWDT_CPU1 && core_id == 0)) {
  283. other_core_frame = frame;
  284. while (1);
  285. }
  286. //The core which triggers the interrupt watchdog will delay 1 us, so the other core can save its frame.
  287. if (frame->exccause == PANIC_RSN_INTWDT_CPU0 || frame->exccause == PANIC_RSN_INTWDT_CPU1) {
  288. ets_delay_us(1);
  289. }
  290. if (frame->exccause == PANIC_RSN_CACHEERR && esp_cache_err_get_cpuid() != core_id) {
  291. // Cache error interrupt will be handled by the panic handler
  292. // on the other CPU.
  293. while (1);
  294. }
  295. #endif //!CONFIG_FREERTOS_UNICORE
  296. haltOtherCore();
  297. panicPutStr("Guru Meditation Error: Core ");
  298. panicPutDec(core_id);
  299. panicPutStr(" panic'ed (");
  300. panicPutStr(reason);
  301. panicPutStr(")\r\n");
  302. if (frame->exccause == PANIC_RSN_DEBUGEXCEPTION) {
  303. int debugRsn;
  304. asm("rsr.debugcause %0":"=r"(debugRsn));
  305. panicPutStr("Debug exception reason: ");
  306. if (debugRsn & XCHAL_DEBUGCAUSE_ICOUNT_MASK) {
  307. panicPutStr("SingleStep ");
  308. }
  309. if (debugRsn & XCHAL_DEBUGCAUSE_IBREAK_MASK) {
  310. panicPutStr("HwBreakpoint ");
  311. }
  312. if (debugRsn & XCHAL_DEBUGCAUSE_DBREAK_MASK) {
  313. //Unlike what the ISA manual says, this core seemingly distinguishes from a DBREAK
  314. //reason caused by watchdog 0 and one caused by watchdog 1 by setting bit 8 of the
  315. //debugcause if the cause is watchdog 1 and clearing it if it's watchdog 0.
  316. if (debugRsn & (1 << 8)) {
  317. #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
  318. const char *name = pcTaskGetTaskName(xTaskGetCurrentTaskHandleForCPU(core_id));
  319. panicPutStr("Stack canary watchpoint triggered (");
  320. panicPutStr(name);
  321. panicPutStr(") ");
  322. #else
  323. panicPutStr("Watchpoint 1 triggered ");
  324. #endif
  325. } else {
  326. panicPutStr("Watchpoint 0 triggered ");
  327. }
  328. }
  329. if (debugRsn & XCHAL_DEBUGCAUSE_BREAK_MASK) {
  330. panicPutStr("BREAK instr ");
  331. }
  332. if (debugRsn & XCHAL_DEBUGCAUSE_BREAKN_MASK) {
  333. panicPutStr("BREAKN instr ");
  334. }
  335. if (debugRsn & XCHAL_DEBUGCAUSE_DEBUGINT_MASK) {
  336. panicPutStr("DebugIntr ");
  337. }
  338. panicPutStr("\r\n");
  339. } else if (frame->exccause == PANIC_RSN_CACHEERR) {
  340. panicPutStr(" ^~~~~~~~~~~~~~~\r\n");
  341. printCacheError();
  342. }
  343. if (esp_cpu_in_ocd_debug_mode()) {
  344. disableAllWdts();
  345. if (frame->exccause == PANIC_RSN_INTWDT_CPU0 ||
  346. frame->exccause == PANIC_RSN_INTWDT_CPU1) {
  347. TIMERG1.int_clr.wdt = 1;
  348. }
  349. #if CONFIG_ESP32_APPTRACE_ENABLE
  350. #if CONFIG_SYSVIEW_ENABLE
  351. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_TRAX_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  352. #else
  353. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_TRAX_THRESH,
  354. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  355. #endif
  356. #endif
  357. setFirstBreakpoint(frame->pc);
  358. return;
  359. }
  360. commonErrorHandler(frame);
  361. }
  362. void xt_unhandled_exception(XtExcFrame *frame)
  363. {
  364. haltOtherCore();
  365. if (!abort_called) {
  366. panicPutStr("Guru Meditation Error: Core ");
  367. panicPutDec(xPortGetCoreID());
  368. panicPutStr(" panic'ed (");
  369. int exccause = frame->exccause;
  370. if (exccause < NUM_EDESCS) {
  371. panicPutStr(edesc[exccause]);
  372. } else {
  373. panicPutStr("Unknown");
  374. }
  375. panicPutStr(")");
  376. #ifdef PANIC_COMPLETE_IN_ESP32C
  377. if (esp_cpu_in_ocd_debug_mode()) {
  378. panicPutStr(" at pc=");
  379. panicPutHex(frame->pc);
  380. panicPutStr(". Setting bp and returning..\r\n");
  381. #if CONFIG_ESP32_APPTRACE_ENABLE
  382. #if CONFIG_SYSVIEW_ENABLE
  383. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_TRAX_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  384. #else
  385. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_TRAX_THRESH,
  386. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  387. #endif
  388. #endif
  389. //Stick a hardware breakpoint on the address the handler returns to. This way, the OCD debugger
  390. //will kick in exactly at the context the error happened.
  391. setFirstBreakpoint(frame->pc);
  392. return;
  393. }
  394. #endif
  395. panicPutStr(". Exception was unhandled.\r\n");
  396. }
  397. commonErrorHandler(frame);
  398. }
  399. /*
  400. If watchdogs are enabled, the panic handler runs the risk of getting aborted pre-emptively because
  401. an overzealous watchdog decides to reset it. On the other hand, if we disable all watchdogs, we run
  402. the risk of somehow halting in the panic handler and not resetting. That is why this routine kills
  403. all watchdogs except the timer group 0 watchdog, and it reconfigures that to reset the chip after
  404. one second.
  405. */
  406. static void reconfigureAllWdts(void)
  407. {
  408. TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  409. TIMERG0.wdt_feed = 1;
  410. TIMERG0.wdt_config0.sys_reset_length = 7; //3.2uS
  411. TIMERG0.wdt_config0.cpu_reset_length = 7; //3.2uS
  412. TIMERG0.wdt_config0.stg0 = TIMG_WDT_STG_SEL_RESET_SYSTEM; //1st stage timeout: reset system
  413. TIMERG0.wdt_config1.clk_prescale = 80 * 500; //Prescaler: wdt counts in ticks of 0.5mS
  414. TIMERG0.wdt_config2 = 2000; //1 second before reset
  415. TIMERG0.wdt_config0.en = 1;
  416. TIMERG0.wdt_wprotect = 0;
  417. //Disable wdt 1
  418. TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  419. TIMERG1.wdt_config0.en = 0;
  420. TIMERG1.wdt_wprotect = 0;
  421. }
  422. /*
  423. This disables all the watchdogs for when we call the gdbstub.
  424. */
  425. static inline void disableAllWdts(void)
  426. {
  427. TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  428. TIMERG0.wdt_config0.en = 0;
  429. TIMERG0.wdt_wprotect = 0;
  430. TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  431. TIMERG1.wdt_config0.en = 0;
  432. TIMERG1.wdt_wprotect = 0;
  433. }
  434. #if CONFIG_ESP32S2_PANIC_PRINT_REBOOT || CONFIG_ESP32S2_PANIC_SILENT_REBOOT
  435. static void esp_panic_dig_reset(void) __attribute__((noreturn));
  436. static void esp_panic_dig_reset(void)
  437. {
  438. // make sure all the panic handler output is sent from UART FIFO
  439. uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  440. // switch to XTAL (otherwise we will keep running from the PLL)
  441. rtc_clk_cpu_freq_set(RTC_CPU_FREQ_XTAL);
  442. // reset the digital part
  443. esp_cpu_unstall(PRO_CPU_NUM);
  444. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_SW_SYS_RST);
  445. while (true) {
  446. ;
  447. }
  448. }
  449. #endif
  450. static void putEntry(uint32_t pc, uint32_t sp)
  451. {
  452. if (pc & 0x80000000) {
  453. pc = (pc & 0x3fffffff) | 0x40000000;
  454. }
  455. panicPutStr(" 0x");
  456. panicPutHex(pc);
  457. panicPutStr(":0x");
  458. panicPutHex(sp);
  459. }
  460. static void doBacktrace(XtExcFrame *frame)
  461. {
  462. uint32_t i = 0, pc = frame->pc, sp = frame->a1;
  463. panicPutStr("\r\nBacktrace:");
  464. /* Do not check sanity on first entry, PC could be smashed. */
  465. putEntry(pc, sp);
  466. pc = frame->a0;
  467. while (i++ < 100) {
  468. uint32_t psp = sp;
  469. if (!esp_stack_ptr_is_sane(sp) || i++ > 100) {
  470. break;
  471. }
  472. sp = *((uint32_t *) (sp - 0x10 + 4));
  473. putEntry(pc - 3, sp); // stack frame addresses are return addresses, so subtract 3 to get the CALL address
  474. pc = *((uint32_t *) (psp - 0x10));
  475. if (pc < 0x40000000) {
  476. break;
  477. }
  478. }
  479. panicPutStr("\r\n\r\n");
  480. }
  481. /*
  482. * Dump registers and do backtrace.
  483. */
  484. static void commonErrorHandler_dump(XtExcFrame *frame, int core_id)
  485. {
  486. int *regs = (int *)frame;
  487. int x, y;
  488. const char *sdesc[] = {
  489. "PC ", "PS ", "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ",
  490. "A6 ", "A7 ", "A8 ", "A9 ", "A10 ", "A11 ", "A12 ", "A13 ",
  491. "A14 ", "A15 ", "SAR ", "EXCCAUSE", "EXCVADDR", "LBEG ", "LEND ", "LCOUNT "
  492. };
  493. /* only dump registers for 'real' crashes, if crashing via abort()
  494. the register window is no longer useful.
  495. */
  496. if (!abort_called) {
  497. panicPutStr("Core");
  498. panicPutDec(core_id);
  499. panicPutStr(" register dump:\r\n");
  500. for (x = 0; x < 24; x += 4) {
  501. for (y = 0; y < 4; y++) {
  502. if (sdesc[x + y][0] != 0) {
  503. panicPutStr(sdesc[x + y]);
  504. panicPutStr(": 0x");
  505. panicPutHex(regs[x + y + 1]);
  506. panicPutStr(" ");
  507. }
  508. }
  509. panicPutStr("\r\n");
  510. }
  511. if (xPortInterruptedFromISRContext()
  512. #if !CONFIG_FREERTOS_UNICORE
  513. && other_core_frame != frame
  514. #endif //!CONFIG_FREERTOS_UNICORE
  515. ) {
  516. //If the core which triggers the interrupt watchdog was in ISR context, dump the epc registers.
  517. uint32_t __value;
  518. panicPutStr("Core");
  519. panicPutDec(core_id);
  520. panicPutStr(" was running in ISR context:\r\n");
  521. __asm__("rsr.epc1 %0" : "=a"(__value));
  522. panicPutStr("EPC1 : 0x");
  523. panicPutHex(__value);
  524. __asm__("rsr.epc2 %0" : "=a"(__value));
  525. panicPutStr(" EPC2 : 0x");
  526. panicPutHex(__value);
  527. __asm__("rsr.epc3 %0" : "=a"(__value));
  528. panicPutStr(" EPC3 : 0x");
  529. panicPutHex(__value);
  530. __asm__("rsr.epc4 %0" : "=a"(__value));
  531. panicPutStr(" EPC4 : 0x");
  532. panicPutHex(__value);
  533. panicPutStr("\r\n");
  534. }
  535. }
  536. /* With windowed ABI backtracing is easy, let's do it. */
  537. doBacktrace(frame);
  538. }
  539. /*
  540. We arrive here after a panic or unhandled exception, when no OCD is detected. Dump the registers to the
  541. serial port and either jump to the gdb stub, halt the CPU or reboot.
  542. */
  543. static __attribute__((noreturn)) void commonErrorHandler(XtExcFrame *frame)
  544. {
  545. int core_id = xPortGetCoreID();
  546. // start panic WDT to restart system if we hang in this handler
  547. if (!rtc_wdt_is_on()) {
  548. rtc_wdt_protect_off();
  549. rtc_wdt_disable();
  550. rtc_wdt_set_length_of_reset_signal(RTC_WDT_SYS_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  551. rtc_wdt_set_length_of_reset_signal(RTC_WDT_CPU_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  552. rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_SYSTEM);
  553. // 64KB of core dump data (stacks of about 30 tasks) will produce ~85KB base64 data.
  554. // @ 115200 UART speed it will take more than 6 sec to print them out.
  555. rtc_wdt_set_time(RTC_WDT_STAGE0, 7000);
  556. rtc_wdt_enable();
  557. rtc_wdt_protect_on();
  558. }
  559. //Feed the watchdogs, so they will give us time to print out debug info
  560. reconfigureAllWdts();
  561. commonErrorHandler_dump(frame, core_id);
  562. #if !CONFIG_FREERTOS_UNICORE
  563. if (other_core_frame != NULL) {
  564. commonErrorHandler_dump((XtExcFrame *)other_core_frame, (core_id ? 0 : 1));
  565. }
  566. #endif //!CONFIG_FREERTOS_UNICORE
  567. #if CONFIG_ESP32_APPTRACE_ENABLE
  568. disableAllWdts();
  569. #if CONFIG_SYSVIEW_ENABLE
  570. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_TRAX_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  571. #else
  572. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_ESP32_APPTRACE_POSTMORTEM_FLUSH_TRAX_THRESH,
  573. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  574. #endif
  575. reconfigureAllWdts();
  576. #endif
  577. #if CONFIG_ESP32S2_PANIC_GDBSTUB
  578. disableAllWdts();
  579. rtc_wdt_disable();
  580. panicPutStr("Entering gdb stub now.\r\n");
  581. esp_gdbstub_panic_handler(frame);
  582. #else
  583. #if CONFIG_ESP32_ENABLE_COREDUMP
  584. static bool s_dumping_core;
  585. if (s_dumping_core) {
  586. panicPutStr("Re-entered core dump! Exception happened during core dump!\r\n");
  587. } else {
  588. disableAllWdts();
  589. s_dumping_core = true;
  590. #if CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH
  591. esp_core_dump_to_flash(frame);
  592. #endif
  593. #if CONFIG_ESP32_ENABLE_COREDUMP_TO_UART && !CONFIG_ESP32S2_PANIC_SILENT_REBOOT
  594. esp_core_dump_to_uart(frame);
  595. #endif
  596. s_dumping_core = false;
  597. reconfigureAllWdts();
  598. }
  599. #endif /* CONFIG_ESP32_ENABLE_COREDUMP */
  600. rtc_wdt_disable();
  601. #if CONFIG_ESP32S2_PANIC_PRINT_REBOOT || CONFIG_ESP32S2_PANIC_SILENT_REBOOT
  602. panicPutStr("Rebooting...\r\n");
  603. if (frame->exccause != PANIC_RSN_CACHEERR) {
  604. esp_restart_noos();
  605. } else {
  606. // The only way to clear invalid cache access interrupt is to reset the digital part
  607. esp_panic_dig_reset();
  608. }
  609. #else
  610. disableAllWdts();
  611. panicPutStr("CPU halted.\r\n");
  612. while (1);
  613. #endif /* CONFIG_ESP32S2_PANIC_PRINT_REBOOT || CONFIG_ESP32S2_PANIC_SILENT_REBOOT */
  614. #endif /* CONFIG_ESP32S2_PANIC_GDBSTUB */
  615. }
  616. void esp_set_breakpoint_if_jtag(void *fn)
  617. {
  618. if (esp_cpu_in_ocd_debug_mode()) {
  619. setFirstBreakpoint((uint32_t)fn);
  620. }
  621. }
  622. esp_err_t esp_set_watchpoint(int no, void *adr, int size, int flags)
  623. {
  624. int x;
  625. if (no < 0 || no > 1) {
  626. return ESP_ERR_INVALID_ARG;
  627. }
  628. if (flags & (~0xC0000000)) {
  629. return ESP_ERR_INVALID_ARG;
  630. }
  631. int dbreakc = 0x3F;
  632. //We support watching 2^n byte values, from 1 to 64. Calculate the mask for that.
  633. for (x = 0; x < 7; x++) {
  634. if (size == (1 << x)) {
  635. break;
  636. }
  637. dbreakc <<= 1;
  638. }
  639. if (x == 7) {
  640. return ESP_ERR_INVALID_ARG;
  641. }
  642. //Mask mask and add in flags.
  643. dbreakc = (dbreakc & 0x3f) | flags;
  644. if (no == 0) {
  645. asm volatile(
  646. "wsr.dbreaka0 %0\n" \
  647. "wsr.dbreakc0 %1\n" \
  648. ::"r"(adr), "r"(dbreakc));
  649. } else {
  650. asm volatile(
  651. "wsr.dbreaka1 %0\n" \
  652. "wsr.dbreakc1 %1\n" \
  653. ::"r"(adr), "r"(dbreakc));
  654. }
  655. return ESP_OK;
  656. }
  657. void esp_clear_watchpoint(int no)
  658. {
  659. //Setting a dbreakc register to 0 makes it trigger on neither load nor store, effectively disabling it.
  660. int dbreakc = 0;
  661. if (no == 0) {
  662. asm volatile(
  663. "wsr.dbreakc0 %0\n" \
  664. ::"r"(dbreakc));
  665. } else {
  666. asm volatile(
  667. "wsr.dbreakc1 %0\n" \
  668. ::"r"(dbreakc));
  669. }
  670. }
  671. void _esp_error_check_failed(esp_err_t rc, const char *file, int line, const char *function, const char *expression)
  672. {
  673. ets_printf("ESP_ERROR_CHECK failed: esp_err_t 0x%x", rc);
  674. #ifdef CONFIG_ESP_ERR_TO_NAME_LOOKUP
  675. ets_printf(" (%s)", esp_err_to_name(rc));
  676. #endif //CONFIG_ESP_ERR_TO_NAME_LOOKUP
  677. ets_printf(" at 0x%08x\n", (intptr_t)__builtin_return_address(0) - 3);
  678. if (spi_flash_cache_enabled()) { // strings may be in flash cache
  679. ets_printf("file: \"%s\" line %d\nfunc: %s\nexpression: %s\n", file, line, function, expression);
  680. }
  681. invoke_abort();
  682. }