panic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "freertos/xtensa_api.h"
  19. #include "soc/uart_reg.h"
  20. #include "soc/io_mux_reg.h"
  21. #include "soc/dport_reg.h"
  22. #include "soc/rtc_cntl_reg.h"
  23. #include "soc/timer_group_struct.h"
  24. #include "soc/timer_group_reg.h"
  25. #include "soc/cpu.h"
  26. #include "esp_gdbstub.h"
  27. #include "esp_panic.h"
  28. #include "esp_attr.h"
  29. #include "esp_err.h"
  30. #include "esp_core_dump.h"
  31. #include "esp_spi_flash.h"
  32. /*
  33. Panic handlers; these get called when an unhandled exception occurs or the assembly-level
  34. task switching / interrupt code runs into an unrecoverable error. The default task stack
  35. overflow handler and abort handler are also in here.
  36. */
  37. /*
  38. Note: The linker script will put everything in this file in IRAM/DRAM, so it also works with flash cache disabled.
  39. */
  40. #if !CONFIG_ESP32_PANIC_SILENT_REBOOT
  41. //printf may be broken, so we fix our own printing fns...
  42. static void panicPutChar(char c)
  43. {
  44. while (((READ_PERI_REG(UART_STATUS_REG(0)) >> UART_TXFIFO_CNT_S)&UART_TXFIFO_CNT) >= 126) ;
  45. WRITE_PERI_REG(UART_FIFO_REG(0), c);
  46. }
  47. static void panicPutStr(const char *c)
  48. {
  49. int x = 0;
  50. while (c[x] != 0) {
  51. panicPutChar(c[x]);
  52. x++;
  53. }
  54. }
  55. static void panicPutHex(int a)
  56. {
  57. int x;
  58. int c;
  59. for (x = 0; x < 8; x++) {
  60. c = (a >> 28) & 0xf;
  61. if (c < 10) {
  62. panicPutChar('0' + c);
  63. } else {
  64. panicPutChar('a' + c - 10);
  65. }
  66. a <<= 4;
  67. }
  68. }
  69. static void panicPutDec(int a)
  70. {
  71. int n1, n2;
  72. n1 = a % 10;
  73. n2 = a / 10;
  74. if (n2 == 0) {
  75. panicPutChar(' ');
  76. } else {
  77. panicPutChar(n2 + '0');
  78. }
  79. panicPutChar(n1 + '0');
  80. }
  81. #else
  82. //No printing wanted. Stub out these functions.
  83. static void panicPutChar(char c) { }
  84. static void panicPutStr(const char *c) { }
  85. static void panicPutHex(int a) { }
  86. static void panicPutDec(int a) { }
  87. #endif
  88. void __attribute__((weak)) vApplicationStackOverflowHook( TaskHandle_t xTask, signed char *pcTaskName )
  89. {
  90. panicPutStr("***ERROR*** A stack overflow in task ");
  91. panicPutStr((char *)pcTaskName);
  92. panicPutStr(" has been detected.\r\n");
  93. abort();
  94. }
  95. static bool abort_called;
  96. static __attribute__((noreturn)) inline void invoke_abort()
  97. {
  98. abort_called = true;
  99. while(1) {
  100. __asm__ ("break 0,0");
  101. *((int*) 0) = 0;
  102. }
  103. }
  104. void abort()
  105. {
  106. #if !CONFIG_ESP32_PANIC_SILENT_REBOOT
  107. ets_printf("abort() was called at PC 0x%08x\n", (intptr_t)__builtin_return_address(0) - 3);
  108. #endif
  109. invoke_abort();
  110. }
  111. static const char *edesc[] = {
  112. "IllegalInstruction", "Syscall", "InstructionFetchError", "LoadStoreError",
  113. "Level1Interrupt", "Alloca", "IntegerDivideByZero", "PCValue",
  114. "Privileged", "LoadStoreAlignment", "res", "res",
  115. "InstrPDAddrError", "LoadStorePIFDataError", "InstrPIFAddrError", "LoadStorePIFAddrError",
  116. "InstTLBMiss", "InstTLBMultiHit", "InstFetchPrivilege", "res",
  117. "InstrFetchProhibited", "res", "res", "res",
  118. "LoadStoreTLBMiss", "LoadStoreTLBMultihit", "LoadStorePrivilege", "res",
  119. "LoadProhibited", "StoreProhibited", "res", "res",
  120. "Cp0Dis", "Cp1Dis", "Cp2Dis", "Cp3Dis",
  121. "Cp4Dis", "Cp5Dis", "Cp6Dis", "Cp7Dis"
  122. };
  123. static void commonErrorHandler(XtExcFrame *frame);
  124. //The fact that we've panic'ed probably means the other CPU is now running wild, possibly
  125. //messing up the serial output, so we stall it here.
  126. static void haltOtherCore()
  127. {
  128. esp_cpu_stall( xPortGetCoreID() == 0 ? 1 : 0 );
  129. }
  130. void panicHandler(XtExcFrame *frame)
  131. {
  132. int *regs = (int *)frame;
  133. //Please keep in sync with PANIC_RSN_* defines
  134. const char *reasons[] = {
  135. "Unknown reason",
  136. "Unhandled debug exception",
  137. "Double exception",
  138. "Unhandled kernel exception",
  139. "Coprocessor exception",
  140. "Interrupt wdt timeout on CPU0",
  141. "Interrupt wdt timeout on CPU1",
  142. };
  143. const char *reason = reasons[0];
  144. //The panic reason is stored in the EXCCAUSE register.
  145. if (regs[20] <= PANIC_RSN_MAX) {
  146. reason = reasons[regs[20]];
  147. }
  148. haltOtherCore();
  149. panicPutStr("Guru Meditation Error: Core ");
  150. panicPutDec(xPortGetCoreID());
  151. panicPutStr(" panic'ed (");
  152. if (!abort_called) {
  153. panicPutStr(reason);
  154. panicPutStr(")\r\n");
  155. if (regs[20]==PANIC_RSN_DEBUGEXCEPTION) {
  156. int debugRsn;
  157. asm("rsr.debugcause %0":"=r"(debugRsn));
  158. panicPutStr("Debug exception reason: ");
  159. if (debugRsn&XCHAL_DEBUGCAUSE_ICOUNT_MASK) panicPutStr("SingleStep ");
  160. if (debugRsn&XCHAL_DEBUGCAUSE_IBREAK_MASK) panicPutStr("HwBreakpoint ");
  161. if (debugRsn&XCHAL_DEBUGCAUSE_DBREAK_MASK) {
  162. //Unlike what the ISA manual says, this core seemingly distinguishes from a DBREAK
  163. //reason caused by watchdog 0 and one caused by watchdog 1 by setting bit 8 of the
  164. //debugcause if the cause is watchdog 1 and clearing it if it's watchdog 0.
  165. if (debugRsn&(1<<8)) {
  166. #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
  167. panicPutStr("Stack canary watchpoint triggered ");
  168. #else
  169. panicPutStr("Watchpoint 1 triggered ");
  170. #endif
  171. } else {
  172. panicPutStr("Watchpoint 0 triggered ");
  173. }
  174. }
  175. if (debugRsn&XCHAL_DEBUGCAUSE_BREAK_MASK) panicPutStr("BREAK instr ");
  176. if (debugRsn&XCHAL_DEBUGCAUSE_BREAKN_MASK) panicPutStr("BREAKN instr ");
  177. if (debugRsn&XCHAL_DEBUGCAUSE_DEBUGINT_MASK) panicPutStr("DebugIntr ");
  178. panicPutStr("\r\n");
  179. }
  180. } else {
  181. panicPutStr("abort)\r\n");
  182. }
  183. if (esp_cpu_in_ocd_debug_mode()) {
  184. asm("break.n 1");
  185. }
  186. commonErrorHandler(frame);
  187. }
  188. static void setFirstBreakpoint(uint32_t pc)
  189. {
  190. asm(
  191. "wsr.ibreaka0 %0\n" \
  192. "rsr.ibreakenable a3\n" \
  193. "movi a4,1\n" \
  194. "or a4, a4, a3\n" \
  195. "wsr.ibreakenable a4\n" \
  196. ::"r"(pc):"a3", "a4");
  197. }
  198. void xt_unhandled_exception(XtExcFrame *frame)
  199. {
  200. int *regs = (int *)frame;
  201. int x;
  202. haltOtherCore();
  203. panicPutStr("Guru Meditation Error of type ");
  204. x = regs[20];
  205. if (x < 40) {
  206. panicPutStr(edesc[x]);
  207. } else {
  208. panicPutStr("Unknown");
  209. }
  210. panicPutStr(" occurred on core ");
  211. panicPutDec(xPortGetCoreID());
  212. if (esp_cpu_in_ocd_debug_mode()) {
  213. panicPutStr(" at pc=");
  214. panicPutHex(regs[1]);
  215. panicPutStr(". Setting bp and returning..\r\n");
  216. //Stick a hardware breakpoint on the address the handler returns to. This way, the OCD debugger
  217. //will kick in exactly at the context the error happened.
  218. setFirstBreakpoint(regs[1]);
  219. return;
  220. }
  221. panicPutStr(". Exception was unhandled.\r\n");
  222. commonErrorHandler(frame);
  223. }
  224. /*
  225. If watchdogs are enabled, the panic handler runs the risk of getting aborted pre-emptively because
  226. an overzealous watchdog decides to reset it. On the other hand, if we disable all watchdogs, we run
  227. the risk of somehow halting in the panic handler and not resetting. That is why this routine kills
  228. all watchdogs except the timer group 0 watchdog, and it reconfigures that to reset the chip after
  229. one second.
  230. */
  231. static void reconfigureAllWdts()
  232. {
  233. TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  234. TIMERG0.wdt_feed = 1;
  235. TIMERG0.wdt_config0.sys_reset_length = 7; //3.2uS
  236. TIMERG0.wdt_config0.cpu_reset_length = 7; //3.2uS
  237. TIMERG0.wdt_config0.stg0 = TIMG_WDT_STG_SEL_RESET_SYSTEM; //1st stage timeout: reset system
  238. TIMERG0.wdt_config1.clk_prescale = 80 * 500; //Prescaler: wdt counts in ticks of 0.5mS
  239. TIMERG0.wdt_config2 = 2000; //1 second before reset
  240. TIMERG0.wdt_config0.en = 1;
  241. TIMERG0.wdt_wprotect = 0;
  242. //Disable wdt 1
  243. TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  244. TIMERG1.wdt_config0.en = 0;
  245. TIMERG1.wdt_wprotect = 0;
  246. }
  247. #if CONFIG_ESP32_PANIC_GDBSTUB || CONFIG_ESP32_PANIC_PRINT_HALT || CONFIG_ESP32_ENABLE_COREDUMP
  248. /*
  249. This disables all the watchdogs for when we call the gdbstub.
  250. */
  251. static void disableAllWdts()
  252. {
  253. TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  254. TIMERG0.wdt_config0.en = 0;
  255. TIMERG0.wdt_wprotect = 0;
  256. TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  257. TIMERG1.wdt_config0.en = 0;
  258. TIMERG0.wdt_wprotect = 0;
  259. }
  260. #endif
  261. static inline bool stackPointerIsSane(uint32_t sp)
  262. {
  263. return !(sp < 0x3ffae010 || sp > 0x3ffffff0 || ((sp & 0xf) != 0));
  264. }
  265. static void putEntry(uint32_t pc, uint32_t sp)
  266. {
  267. if (pc & 0x80000000) {
  268. pc = (pc & 0x3fffffff) | 0x40000000;
  269. }
  270. panicPutStr(" 0x");
  271. panicPutHex(pc);
  272. panicPutStr(":0x");
  273. panicPutHex(sp);
  274. }
  275. static void doBacktrace(XtExcFrame *frame)
  276. {
  277. uint32_t i = 0, pc = frame->pc, sp = frame->a1;
  278. panicPutStr("\r\nBacktrace:");
  279. /* Do not check sanity on first entry, PC could be smashed. */
  280. putEntry(pc, sp);
  281. pc = frame->a0;
  282. while (i++ < 100) {
  283. uint32_t psp = sp;
  284. if (!stackPointerIsSane(sp) || i++ > 100) {
  285. break;
  286. }
  287. sp = *((uint32_t *) (sp - 0x10 + 4));
  288. putEntry(pc, sp);
  289. pc = *((uint32_t *) (psp - 0x10));
  290. if (pc < 0x40000000) {
  291. break;
  292. }
  293. }
  294. panicPutStr("\r\n\r\n");
  295. }
  296. void esp_restart_noos() __attribute__ ((noreturn));
  297. /*
  298. We arrive here after a panic or unhandled exception, when no OCD is detected. Dump the registers to the
  299. serial port and either jump to the gdb stub, halt the CPU or reboot.
  300. */
  301. static void commonErrorHandler(XtExcFrame *frame)
  302. {
  303. int *regs = (int *)frame;
  304. int x, y;
  305. const char *sdesc[] = {
  306. "PC ", "PS ", "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ",
  307. "A6 ", "A7 ", "A8 ", "A9 ", "A10 ", "A11 ", "A12 ", "A13 ",
  308. "A14 ", "A15 ", "SAR ", "EXCCAUSE", "EXCVADDR", "LBEG ", "LEND ", "LCOUNT "
  309. };
  310. //Feed the watchdogs, so they will give us time to print out debug info
  311. reconfigureAllWdts();
  312. /* only dump registers for 'real' crashes, if crashing via abort()
  313. the register window is no longer useful.
  314. */
  315. if (!abort_called) {
  316. panicPutStr("Register dump:\r\n");
  317. for (x = 0; x < 24; x += 4) {
  318. for (y = 0; y < 4; y++) {
  319. if (sdesc[x + y][0] != 0) {
  320. panicPutStr(sdesc[x + y]);
  321. panicPutStr(": 0x");
  322. panicPutHex(regs[x + y + 1]);
  323. panicPutStr(" ");
  324. }
  325. }
  326. panicPutStr("\r\n");
  327. }
  328. }
  329. /* With windowed ABI backtracing is easy, let's do it. */
  330. doBacktrace(frame);
  331. #if CONFIG_ESP32_PANIC_GDBSTUB
  332. disableAllWdts();
  333. panicPutStr("Entering gdb stub now.\r\n");
  334. esp_gdbstub_panic_handler(frame);
  335. #else
  336. #if CONFIG_ESP32_ENABLE_COREDUMP
  337. disableAllWdts();
  338. #if CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH
  339. esp_core_dump_to_flash(frame);
  340. #endif
  341. #if CONFIG_ESP32_ENABLE_COREDUMP_TO_UART && !CONFIG_ESP32_PANIC_SILENT_REBOOT
  342. esp_core_dump_to_uart(frame);
  343. #endif
  344. reconfigureAllWdts();
  345. #endif
  346. #if CONFIG_ESP32_PANIC_PRINT_REBOOT || CONFIG_ESP32_PANIC_SILENT_REBOOT
  347. panicPutStr("Rebooting...\r\n");
  348. esp_restart_noos();
  349. #else
  350. disableAllWdts();
  351. panicPutStr("CPU halted.\r\n");
  352. while (1);
  353. #endif
  354. #endif
  355. }
  356. void esp_set_breakpoint_if_jtag(void *fn)
  357. {
  358. if (esp_cpu_in_ocd_debug_mode()) {
  359. setFirstBreakpoint((uint32_t)fn);
  360. }
  361. }
  362. esp_err_t esp_set_watchpoint(int no, void *adr, int size, int flags)
  363. {
  364. int x;
  365. if (no<0 || no>1) return ESP_ERR_INVALID_ARG;
  366. if (flags&(~0xC0000000)) return ESP_ERR_INVALID_ARG;
  367. int dbreakc=0x3F;
  368. //We support watching 2^n byte values, from 1 to 64. Calculate the mask for that.
  369. for (x=0; x<7; x++) {
  370. if (size==(1<<x)) break;
  371. dbreakc<<=1;
  372. }
  373. if (x==7) return ESP_ERR_INVALID_ARG;
  374. //Mask mask and add in flags.
  375. dbreakc=(dbreakc&0x3f)|flags;
  376. if (no==0) {
  377. asm volatile(
  378. "wsr.dbreaka0 %0\n" \
  379. "wsr.dbreakc0 %1\n" \
  380. ::"r"(adr),"r"(dbreakc));
  381. } else {
  382. asm volatile(
  383. "wsr.dbreaka1 %0\n" \
  384. "wsr.dbreakc1 %1\n" \
  385. ::"r"(adr),"r"(dbreakc));
  386. }
  387. return ESP_OK;
  388. }
  389. void esp_clear_watchpoint(int no)
  390. {
  391. //Setting a dbreakc register to 0 makes it trigger on neither load nor store, effectively disabling it.
  392. int dbreakc=0;
  393. if (no==0) {
  394. asm volatile(
  395. "wsr.dbreakc0 %0\n" \
  396. ::"r"(dbreakc));
  397. } else {
  398. asm volatile(
  399. "wsr.dbreakc1 %0\n" \
  400. ::"r"(dbreakc));
  401. }
  402. }
  403. void _esp_error_check_failed(esp_err_t rc, const char *file, int line, const char *function, const char *expression)
  404. {
  405. ets_printf("ESP_ERROR_CHECK failed: esp_err_t 0x%x at 0x%08x\n", rc, (intptr_t)__builtin_return_address(0) - 3);
  406. if (spi_flash_cache_enabled()) { // strings may be in flash cache
  407. ets_printf("file: \"%s\" line %d\nfunc: %s\nexpression: %s\n", file, line, function, expression);
  408. }
  409. invoke_abort();
  410. }