panic.c 13 KB

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