panic.c 25 KB

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