panic_handler.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 "freertos/xtensa_context.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "esp_spi_flash.h"
  18. #include "esp_private/panic_reason.h"
  19. #include "esp_private/system_internal.h"
  20. #include "esp_debug_helpers.h"
  21. #include "soc/soc_memory_layout.h"
  22. #include "soc/cpu.h"
  23. #include "soc/soc_caps.h"
  24. #include "soc/rtc.h"
  25. #include "hal/soc_hal.h"
  26. #include "hal/cpu_hal.h"
  27. #include "hal/wdt_types.h"
  28. #include "hal/wdt_hal.h"
  29. #include "sdkconfig.h"
  30. #if CONFIG_IDF_TARGET_ESP32
  31. #include "esp32/cache_err_int.h"
  32. #include "esp32/dport_access.h"
  33. #include "esp32/rom/uart.h"
  34. #elif CONFIG_IDF_TARGET_ESP32S2
  35. #include "esp32s2/cache_err_int.h"
  36. #include "esp32s2/rom/uart.h"
  37. #include "esp32s2/memprot.h"
  38. #include "soc/extmem_reg.h"
  39. #include "soc/cache_memory.h"
  40. #include "soc/rtc_cntl_reg.h"
  41. #endif
  42. #include "esp_private/panic_internal.h"
  43. extern int _invalid_pc_placeholder;
  44. extern void esp_panic_handler_reconfigure_wdts(void);
  45. extern void esp_panic_handler(panic_info_t *);
  46. static wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
  47. static XtExcFrame *xt_exc_frames[SOC_CPU_CORES_NUM] = {NULL};
  48. /*
  49. Panic handlers; these get called when an unhandled exception occurs or the assembly-level
  50. task switching / interrupt code runs into an unrecoverable error. The default task stack
  51. overflow handler and abort handler are also in here.
  52. */
  53. /*
  54. Note: The linker script will put everything in this file in IRAM/DRAM, so it also works with flash cache disabled.
  55. */
  56. static void print_illegal_instruction_details(const void *f)
  57. {
  58. XtExcFrame *frame = (XtExcFrame *) f;
  59. /* Print out memory around the instruction word */
  60. uint32_t epc = frame->pc;
  61. epc = (epc & ~0x3) - 4;
  62. /* check that the address was sane */
  63. if (epc < SOC_IROM_MASK_LOW || epc >= SOC_IROM_HIGH) {
  64. return;
  65. }
  66. volatile uint32_t *pepc = (uint32_t *)epc;
  67. panic_print_str("Memory dump at 0x");
  68. panic_print_hex(epc);
  69. panic_print_str(": ");
  70. panic_print_hex(*pepc);
  71. panic_print_str(" ");
  72. panic_print_hex(*(pepc + 1));
  73. panic_print_str(" ");
  74. panic_print_hex(*(pepc + 2));
  75. }
  76. static void print_debug_exception_details(const void *f)
  77. {
  78. int debug_rsn;
  79. asm("rsr.debugcause %0":"=r"(debug_rsn));
  80. panic_print_str("Debug exception reason: ");
  81. if (debug_rsn & XCHAL_DEBUGCAUSE_ICOUNT_MASK) {
  82. panic_print_str("SingleStep ");
  83. }
  84. if (debug_rsn & XCHAL_DEBUGCAUSE_IBREAK_MASK) {
  85. panic_print_str("HwBreakpoint ");
  86. }
  87. if (debug_rsn & XCHAL_DEBUGCAUSE_DBREAK_MASK) {
  88. //Unlike what the ISA manual says, this core seemingly distinguishes from a DBREAK
  89. //reason caused by watchdog 0 and one caused by watchdog 1 by setting bit 8 of the
  90. //debugcause if the cause is watchpoint 1 and clearing it if it's watchpoint 0.
  91. if (debug_rsn & (1 << 8)) {
  92. #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
  93. int core = 0;
  94. #if !CONFIG_FREERTOS_UNICORE
  95. if (f == xt_exc_frames[1]) {
  96. core = 1;
  97. }
  98. #endif
  99. const char *name = pcTaskGetTaskName(xTaskGetCurrentTaskHandleForCPU(core));
  100. panic_print_str("Stack canary watchpoint triggered (");
  101. panic_print_str(name);
  102. panic_print_str(") ");
  103. #else
  104. panic_print_str("Watchpoint 1 triggered ");
  105. #endif
  106. } else {
  107. panic_print_str("Watchpoint 0 triggered ");
  108. }
  109. }
  110. if (debug_rsn & XCHAL_DEBUGCAUSE_BREAK_MASK) {
  111. panic_print_str("BREAK instr ");
  112. }
  113. if (debug_rsn & XCHAL_DEBUGCAUSE_BREAKN_MASK) {
  114. panic_print_str("BREAKN instr ");
  115. }
  116. if (debug_rsn & XCHAL_DEBUGCAUSE_DEBUGINT_MASK) {
  117. panic_print_str("DebugIntr ");
  118. }
  119. }
  120. static void print_backtrace_entry(uint32_t pc, uint32_t sp)
  121. {
  122. panic_print_str("0x");
  123. panic_print_hex(pc);
  124. panic_print_str(":0x");
  125. panic_print_hex(sp);
  126. }
  127. static void print_backtrace(const void *f, int core)
  128. {
  129. XtExcFrame *frame = (XtExcFrame *) f;
  130. int depth = 100;
  131. //Initialize stk_frame with first frame of stack
  132. esp_backtrace_frame_t stk_frame = {.pc = frame->pc, .sp = frame->a1, .next_pc = frame->a0};
  133. panic_print_str("\r\nBacktrace:");
  134. print_backtrace_entry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp);
  135. //Check if first frame is valid
  136. bool corrupted = !(esp_stack_ptr_is_sane(stk_frame.sp) &&
  137. (esp_ptr_executable((void *)esp_cpu_process_stack_pc(stk_frame.pc)) ||
  138. /* Ignore the first corrupted PC in case of InstrFetchProhibited */
  139. frame->exccause == EXCCAUSE_INSTR_PROHIBITED));
  140. uint32_t i = ((depth <= 0) ? INT32_MAX : depth) - 1; //Account for stack frame that's already printed
  141. while (i-- > 0 && stk_frame.next_pc != 0 && !corrupted) {
  142. if (!esp_backtrace_get_next_frame(&stk_frame)) { //Get next stack frame
  143. corrupted = true;
  144. }
  145. panic_print_str(" ");
  146. print_backtrace_entry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp);
  147. }
  148. //Print backtrace termination marker
  149. if (corrupted) {
  150. panic_print_str(" |<-CORRUPTED");
  151. } else if (stk_frame.next_pc != 0) { //Backtrace continues
  152. panic_print_str(" |<-CONTINUES");
  153. }
  154. }
  155. static void print_registers(const void *f, int core)
  156. {
  157. XtExcFrame *frame = (XtExcFrame *) f;
  158. int *regs = (int *)frame;
  159. int x, y;
  160. const char *sdesc[] = {
  161. "PC ", "PS ", "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ",
  162. "A6 ", "A7 ", "A8 ", "A9 ", "A10 ", "A11 ", "A12 ", "A13 ",
  163. "A14 ", "A15 ", "SAR ", "EXCCAUSE", "EXCVADDR", "LBEG ", "LEND ", "LCOUNT "
  164. };
  165. /* only dump registers for 'real' crashes, if crashing via abort()
  166. the register window is no longer useful.
  167. */
  168. panic_print_str("Core ");
  169. panic_print_dec(core);
  170. panic_print_str(" register dump:");
  171. for (x = 0; x < 24; x += 4) {
  172. panic_print_str("\r\n");
  173. for (y = 0; y < 4; y++) {
  174. if (sdesc[x + y][0] != 0) {
  175. panic_print_str(sdesc[x + y]);
  176. panic_print_str(": 0x");
  177. panic_print_hex(regs[x + y + 1]);
  178. panic_print_str(" ");
  179. }
  180. }
  181. }
  182. // If the core which triggers the interrupt watchpoint was in ISR context, dump the epc registers.
  183. if (xPortInterruptedFromISRContext()
  184. #if !CONFIG_FREERTOS_UNICORE
  185. && ((core == 0 && frame->exccause == PANIC_RSN_INTWDT_CPU0) ||
  186. (core == 1 && frame->exccause == PANIC_RSN_INTWDT_CPU1))
  187. #endif //!CONFIG_FREERTOS_UNICORE
  188. ) {
  189. panic_print_str("\r\n");
  190. uint32_t __value;
  191. panic_print_str("Core ");
  192. panic_print_dec(core);
  193. panic_print_str(" was running in ISR context:\r\n");
  194. __asm__("rsr.epc1 %0" : "=a"(__value));
  195. panic_print_str("EPC1 : 0x");
  196. panic_print_hex(__value);
  197. __asm__("rsr.epc2 %0" : "=a"(__value));
  198. panic_print_str(" EPC2 : 0x");
  199. panic_print_hex(__value);
  200. __asm__("rsr.epc3 %0" : "=a"(__value));
  201. panic_print_str(" EPC3 : 0x");
  202. panic_print_hex(__value);
  203. __asm__("rsr.epc4 %0" : "=a"(__value));
  204. panic_print_str(" EPC4 : 0x");
  205. panic_print_hex(__value);
  206. }
  207. }
  208. static void print_state_for_core(const void *f, int core)
  209. {
  210. if (!g_panic_abort) {
  211. print_registers(f, core);
  212. panic_print_str("\r\n");
  213. }
  214. print_backtrace(f, core);
  215. }
  216. static void print_state(const void *f)
  217. {
  218. #if !CONFIG_FREERTOS_UNICORE
  219. int err_core = f == xt_exc_frames[0] ? 0 : 1;
  220. #else
  221. int err_core = 0;
  222. #endif
  223. print_state_for_core(f, err_core);
  224. panic_print_str("\r\n");
  225. #if !CONFIG_FREERTOS_UNICORE
  226. // If there are other frame info, print them as well
  227. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  228. // `f` is the frame for the offending core, see note above.
  229. if (err_core != i && xt_exc_frames[i] != NULL) {
  230. print_state_for_core(xt_exc_frames[i], i);
  231. panic_print_str("\r\n");
  232. }
  233. }
  234. #endif
  235. }
  236. #if CONFIG_IDF_TARGET_ESP32S2
  237. static inline void print_cache_err_details(const void *f)
  238. {
  239. uint32_t vaddr = 0, size = 0;
  240. uint32_t status[2];
  241. status[0] = REG_READ(EXTMEM_CACHE_DBG_STATUS0_REG);
  242. status[1] = REG_READ(EXTMEM_CACHE_DBG_STATUS1_REG);
  243. for (int i = 0; i < 32; i++) {
  244. switch (status[0] & BIT(i)) {
  245. case EXTMEM_IC_SYNC_SIZE_FAULT_ST:
  246. vaddr = REG_READ(EXTMEM_PRO_ICACHE_MEM_SYNC0_REG);
  247. size = REG_READ(EXTMEM_PRO_ICACHE_MEM_SYNC1_REG);
  248. panic_print_str("Icache sync parameter configuration error, the error address and size is 0x");
  249. panic_print_hex(vaddr);
  250. panic_print_str("(0x");
  251. panic_print_hex(size);
  252. panic_print_str(")\r\n");
  253. break;
  254. case EXTMEM_IC_PRELOAD_SIZE_FAULT_ST:
  255. vaddr = REG_READ(EXTMEM_PRO_ICACHE_PRELOAD_ADDR_REG);
  256. size = REG_READ(EXTMEM_PRO_ICACHE_PRELOAD_SIZE_REG);
  257. panic_print_str("Icache preload parameter configuration error, the error address and size is 0x");
  258. panic_print_hex(vaddr);
  259. panic_print_str("(0x");
  260. panic_print_hex(size);
  261. panic_print_str(")\r\n");
  262. break;
  263. case EXTMEM_ICACHE_REJECT_ST:
  264. vaddr = REG_READ(EXTMEM_PRO_ICACHE_REJECT_VADDR_REG);
  265. panic_print_str("Icache reject error occurred while accessing the address 0x");
  266. panic_print_hex(vaddr);
  267. if (REG_READ(EXTMEM_PRO_CACHE_MMU_FAULT_CONTENT_REG) & MMU_INVALID) {
  268. panic_print_str(" (invalid mmu entry)");
  269. }
  270. panic_print_str("\r\n");
  271. break;
  272. default:
  273. break;
  274. }
  275. switch (status[1] & BIT(i)) {
  276. case EXTMEM_DC_SYNC_SIZE_FAULT_ST:
  277. vaddr = REG_READ(EXTMEM_PRO_DCACHE_MEM_SYNC0_REG);
  278. size = REG_READ(EXTMEM_PRO_DCACHE_MEM_SYNC1_REG);
  279. panic_print_str("Dcache sync parameter configuration error, the error address and size is 0x");
  280. panic_print_hex(vaddr);
  281. panic_print_str("(0x");
  282. panic_print_hex(size);
  283. panic_print_str(")\r\n");
  284. break;
  285. case EXTMEM_DC_PRELOAD_SIZE_FAULT_ST:
  286. vaddr = REG_READ(EXTMEM_PRO_DCACHE_PRELOAD_ADDR_REG);
  287. size = REG_READ(EXTMEM_PRO_DCACHE_PRELOAD_SIZE_REG);
  288. panic_print_str("Dcache preload parameter configuration error, the error address and size is 0x");
  289. panic_print_hex(vaddr);
  290. panic_print_str("(0x");
  291. panic_print_hex(size);
  292. panic_print_str(")\r\n");
  293. break;
  294. case EXTMEM_DCACHE_WRITE_FLASH_ST:
  295. panic_print_str("Write back error occurred while dcache tries to write back to flash\r\n");
  296. break;
  297. case EXTMEM_DCACHE_REJECT_ST:
  298. vaddr = REG_READ(EXTMEM_PRO_DCACHE_REJECT_VADDR_REG);
  299. panic_print_str("Dcache reject error occurred while accessing the address 0x");
  300. panic_print_hex(vaddr);
  301. if (REG_READ(EXTMEM_PRO_CACHE_MMU_FAULT_CONTENT_REG) & MMU_INVALID) {
  302. panic_print_str(" (invalid mmu entry)");
  303. }
  304. panic_print_str("\r\n");
  305. break;
  306. case EXTMEM_MMU_ENTRY_FAULT_ST:
  307. vaddr = REG_READ(EXTMEM_PRO_CACHE_MMU_FAULT_VADDR_REG);
  308. panic_print_str("MMU entry fault error occurred while accessing the address 0x");
  309. panic_print_hex(vaddr);
  310. if (REG_READ(EXTMEM_PRO_CACHE_MMU_FAULT_CONTENT_REG) & MMU_INVALID) {
  311. panic_print_str(" (invalid mmu entry)");
  312. }
  313. panic_print_str("\r\n");
  314. break;
  315. default:
  316. break;
  317. }
  318. }
  319. }
  320. static inline void print_memprot_err_details(const void *f)
  321. {
  322. uint32_t *fault_addr;
  323. uint32_t op_type, op_subtype;
  324. mem_type_prot_t mem_type = esp_memprot_get_intr_memtype();
  325. esp_memprot_get_fault_status( mem_type, &fault_addr, &op_type, &op_subtype );
  326. const char *operation_type = "Write";
  327. if ( op_type == 0 ) {
  328. operation_type = (mem_type == MEMPROT_IRAM0 && op_subtype == 0) ? "Instruction fetch" : "Read";
  329. }
  330. panic_print_str( operation_type );
  331. panic_print_str( " operation at address 0x" );
  332. panic_print_hex( (uint32_t)fault_addr );
  333. panic_print_str(" not permitted.\r\n");
  334. }
  335. #endif
  336. static void frame_to_panic_info(XtExcFrame *frame, panic_info_t *info, bool pseudo_excause)
  337. {
  338. info->core = cpu_hal_get_core_id();
  339. info->exception = PANIC_EXCEPTION_FAULT;
  340. info->details = NULL;
  341. info->pseudo_excause = pseudo_excause;
  342. if (pseudo_excause) {
  343. if (frame->exccause == PANIC_RSN_INTWDT_CPU0) {
  344. info->core = 0;
  345. info->exception = PANIC_EXCEPTION_IWDT;
  346. } else if (frame->exccause == PANIC_RSN_INTWDT_CPU1) {
  347. info->core = 1;
  348. info->exception = PANIC_EXCEPTION_IWDT;
  349. } else if (frame->exccause == PANIC_RSN_CACHEERR) {
  350. info->core = esp_cache_err_get_cpuid();
  351. } else {}
  352. //Please keep in sync with PANIC_RSN_* defines
  353. static const char *pseudo_reason[] = {
  354. "Unknown reason",
  355. "Unhandled debug exception",
  356. "Double exception",
  357. "Unhandled kernel exception",
  358. "Coprocessor exception",
  359. "Interrupt wdt timeout on CPU0",
  360. "Interrupt wdt timeout on CPU1",
  361. #if CONFIG_IDF_TARGET_ESP32
  362. "Cache disabled but cached memory region accessed",
  363. #elif CONFIG_IDF_TARGET_ESP32S2
  364. "Cache exception",
  365. #endif
  366. };
  367. info->reason = pseudo_reason[0];
  368. info->description = NULL;
  369. if (frame->exccause <= PANIC_RSN_MAX) {
  370. info->reason = pseudo_reason[frame->exccause];
  371. }
  372. if (frame->exccause == PANIC_RSN_DEBUGEXCEPTION) {
  373. info->details = print_debug_exception_details;
  374. info->exception = PANIC_EXCEPTION_DEBUG;
  375. }
  376. #if CONFIG_IDF_TARGET_ESP32S2
  377. if (frame->exccause == PANIC_RSN_CACHEERR) {
  378. if ( esp_memprot_is_assoc_intr_any() ) {
  379. info->details = print_memprot_err_details;
  380. info->reason = "Memory protection fault";
  381. } else {
  382. info->details = print_cache_err_details;
  383. }
  384. }
  385. #endif
  386. } else {
  387. static const char *reason[] = {
  388. "IllegalInstruction", "Syscall", "InstructionFetchError", "LoadStoreError",
  389. "Level1Interrupt", "Alloca", "IntegerDivideByZero", "PCValue",
  390. "Privileged", "LoadStoreAlignment", "res", "res",
  391. "InstrPDAddrError", "LoadStorePIFDataError", "InstrPIFAddrError", "LoadStorePIFAddrError",
  392. "InstTLBMiss", "InstTLBMultiHit", "InstFetchPrivilege", "res",
  393. "InstrFetchProhibited", "res", "res", "res",
  394. "LoadStoreTLBMiss", "LoadStoreTLBMultihit", "LoadStorePrivilege", "res",
  395. "LoadProhibited", "StoreProhibited", "res", "res",
  396. "Cp0Dis", "Cp1Dis", "Cp2Dis", "Cp3Dis",
  397. "Cp4Dis", "Cp5Dis", "Cp6Dis", "Cp7Dis"
  398. };
  399. if (frame->exccause < (sizeof(reason) / sizeof(char *))) {
  400. info->reason = (reason[frame->exccause]);
  401. } else {
  402. info->reason = "Unknown";
  403. }
  404. info->description = "Exception was unhandled.";
  405. if (frame->exccause == EXCCAUSE_ILLEGAL) {
  406. info->details = print_illegal_instruction_details;
  407. }
  408. }
  409. info->state = print_state;
  410. info->addr = ((void *) ((XtExcFrame *) frame)->pc);
  411. info->frame = frame;
  412. }
  413. static void panic_handler(XtExcFrame *frame, bool pseudo_excause)
  414. {
  415. /*
  416. * Setup environment and perform necessary architecture/chip specific
  417. * steps here prior to the system panic handler.
  418. * */
  419. int core_id = cpu_hal_get_core_id();
  420. // If multiple cores arrive at panic handler, save frames for all of them
  421. xt_exc_frames[core_id] = frame;
  422. #if !CONFIG_FREERTOS_UNICORE
  423. // These are cases where both CPUs both go into panic handler. The following code ensures
  424. // only one core proceeds to the system panic handler.
  425. if (pseudo_excause) {
  426. #define BUSY_WAIT_IF_TRUE(b) { if (b) while(1); }
  427. // For WDT expiry, pause the non-offending core - offending core handles panic
  428. BUSY_WAIT_IF_TRUE(frame->exccause == PANIC_RSN_INTWDT_CPU0 && core_id == 1);
  429. BUSY_WAIT_IF_TRUE(frame->exccause == PANIC_RSN_INTWDT_CPU1 && core_id == 0);
  430. // For cache error, pause the non-offending core - offending core handles panic
  431. if (frame->exccause == PANIC_RSN_CACHEERR && core_id != esp_cache_err_get_cpuid()) {
  432. // Only print the backtrace for the offending core in case of the cache error
  433. xt_exc_frames[core_id] = NULL;
  434. while (1) {
  435. ;
  436. }
  437. }
  438. }
  439. // Need to reconfigure WDTs before we stall any other CPU
  440. esp_panic_handler_reconfigure_wdts();
  441. ets_delay_us(1);
  442. SOC_HAL_STALL_OTHER_CORES();
  443. #endif
  444. #if CONFIG_IDF_TARGET_ESP32
  445. esp_dport_access_int_abort();
  446. #endif
  447. #if !CONFIG_ESP_PANIC_HANDLER_IRAM
  448. // Re-enable CPU cache for current CPU if it was disabled
  449. if (!spi_flash_cache_enabled()) {
  450. spi_flash_enable_cache(core_id);
  451. panic_print_str("Re-enable cpu cache.\r\n");
  452. }
  453. #endif
  454. if (esp_cpu_in_ocd_debug_mode()) {
  455. if (!(esp_ptr_executable(cpu_ll_pc_to_ptr(frame->pc)) && (frame->pc & 0xC0000000U))) {
  456. /* Xtensa ABI sets the 2 MSBs of the PC according to the windowed call size
  457. * Incase the PC is invalid, GDB will fail to translate addresses to function names
  458. * Hence replacing the PC to a placeholder address in case of invalid PC
  459. */
  460. frame->pc = (uint32_t)&_invalid_pc_placeholder;
  461. }
  462. if (frame->exccause == PANIC_RSN_INTWDT_CPU0 ||
  463. frame->exccause == PANIC_RSN_INTWDT_CPU1) {
  464. wdt_hal_write_protect_disable(&wdt0_context);
  465. wdt_hal_handle_intr(&wdt0_context);
  466. wdt_hal_write_protect_enable(&wdt0_context);
  467. }
  468. }
  469. // Convert architecture exception frame into abstracted panic info
  470. panic_info_t info;
  471. frame_to_panic_info(frame, &info, pseudo_excause);
  472. // Call the system panic handler
  473. esp_panic_handler(&info);
  474. }
  475. void panicHandler(XtExcFrame *frame)
  476. {
  477. // This panic handler gets called for when the double exception vector,
  478. // kernel exception vector gets used; as well as handling interrupt-based
  479. // faults cache error, wdt expiry. EXCAUSE register gets written with
  480. // one of PANIC_RSN_* values.
  481. panic_handler(frame, true);
  482. }
  483. void xt_unhandled_exception(XtExcFrame *frame)
  484. {
  485. panic_handler(frame, false);
  486. }
  487. void __attribute__((noreturn)) panic_restart(void)
  488. {
  489. bool digital_reset_needed = false;
  490. #ifdef CONFIG_IDF_TARGET_ESP32
  491. // On the ESP32, cache error status can only be cleared by system reset
  492. if (esp_cache_err_get_cpuid() != -1) {
  493. digital_reset_needed = true;
  494. }
  495. #endif
  496. #if CONFIG_IDF_TARGET_ESP32S2
  497. if (esp_memprot_is_intr_ena_any() || esp_memprot_is_locked_any()) {
  498. digital_reset_needed = true;
  499. }
  500. #endif
  501. if (digital_reset_needed) {
  502. esp_restart_noos_dig();
  503. }
  504. esp_restart_noos();
  505. }