panic.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /*
  30. Panic handlers; these get called when an unhandled exception occurs or the assembly-level
  31. task switching / interrupt code runs into an unrecoverable error. The default task stack
  32. overflow handler also is in here.
  33. */
  34. /*
  35. Note: The linker script will put everything in this file in IRAM/DRAM, so it also works with flash cache disabled.
  36. */
  37. #if !CONFIG_ESP32_PANIC_SILENT_REBOOT
  38. //printf may be broken, so we fix our own printing fns...
  39. inline static void panicPutChar(char c)
  40. {
  41. while (((READ_PERI_REG(UART_STATUS_REG(0)) >> UART_TXFIFO_CNT_S)&UART_TXFIFO_CNT) >= 126) ;
  42. WRITE_PERI_REG(UART_FIFO_REG(0), c);
  43. }
  44. inline static void panicPutStr(const char *c)
  45. {
  46. int x = 0;
  47. while (c[x] != 0) {
  48. panicPutChar(c[x]);
  49. x++;
  50. }
  51. }
  52. inline static void panicPutHex(int a)
  53. {
  54. int x;
  55. int c;
  56. for (x = 0; x < 8; x++) {
  57. c = (a >> 28) & 0xf;
  58. if (c < 10) {
  59. panicPutChar('0' + c);
  60. } else {
  61. panicPutChar('a' + c - 10);
  62. }
  63. a <<= 4;
  64. }
  65. }
  66. inline static void panicPutDec(int a)
  67. {
  68. int n1, n2;
  69. n1 = a % 10;
  70. n2 = a / 10;
  71. if (n2 == 0) {
  72. panicPutChar(' ');
  73. } else {
  74. panicPutChar(n2 + '0');
  75. }
  76. panicPutChar(n1 + '0');
  77. }
  78. #else
  79. //No printing wanted. Stub out these functions.
  80. inline static void panicPutChar(char c) { }
  81. inline static void panicPutStr(const char *c) { }
  82. inline static void panicPutHex(int a) { }
  83. inline static void panicPutDec(int a) { }
  84. #endif
  85. void __attribute__((weak)) vApplicationStackOverflowHook( TaskHandle_t xTask, signed char *pcTaskName )
  86. {
  87. panicPutStr("***ERROR*** A stack overflow in task ");
  88. panicPutStr((char *)pcTaskName);
  89. panicPutStr(" has been detected.\r\n");
  90. configASSERT(0);
  91. }
  92. static const char *edesc[] = {
  93. "IllegalInstruction", "Syscall", "InstructionFetchError", "LoadStoreError",
  94. "Level1Interrupt", "Alloca", "IntegerDivideByZero", "PCValue",
  95. "Privileged", "LoadStoreAlignment", "res", "res",
  96. "InstrPDAddrError", "LoadStorePIFDataError", "InstrPIFAddrError", "LoadStorePIFAddrError",
  97. "InstTLBMiss", "InstTLBMultiHit", "InstFetchPrivilege", "res",
  98. "InstrFetchProhibited", "res", "res", "res",
  99. "LoadStoreTLBMiss", "LoadStoreTLBMultihit", "LoadStorePrivilege", "res",
  100. "LoadProhibited", "StoreProhibited", "res", "res",
  101. "Cp0Dis", "Cp1Dis", "Cp2Dis", "Cp3Dis",
  102. "Cp4Dis", "Cp5Dis", "Cp6Dis", "Cp7Dis"
  103. };
  104. void commonErrorHandler(XtExcFrame *frame);
  105. //The fact that we've panic'ed probably means the other CPU is now running wild, possibly
  106. //messing up the serial output, so we stall it here.
  107. static void haltOtherCore()
  108. {
  109. esp_cpu_stall( xPortGetCoreID() == 0 ? 1 : 0 );
  110. }
  111. //Returns true when a debugger is attached using JTAG.
  112. static int inOCDMode()
  113. {
  114. #if CONFIG_ESP32_DEBUG_OCDAWARE
  115. int dcr;
  116. int reg = 0x10200C; //DSRSET register
  117. asm("rer %0,%1":"=r"(dcr):"r"(reg));
  118. return (dcr & 0x1);
  119. #else
  120. return 0; //Always return no debugger is attached.
  121. #endif
  122. }
  123. void panicHandler(XtExcFrame *frame)
  124. {
  125. int *regs = (int *)frame;
  126. //Please keep in sync with PANIC_RSN_* defines
  127. const char *reasons[] = {
  128. "Unknown reason",
  129. "Unhandled debug exception",
  130. "Double exception",
  131. "Unhandled kernel exception",
  132. "Coprocessor exception",
  133. "Interrupt wdt timeout on CPU0",
  134. "Interrupt wdt timeout on CPU1",
  135. };
  136. const char *reason = reasons[0];
  137. //The panic reason is stored in the EXCCAUSE register.
  138. if (regs[20] <= PANIC_RSN_MAX) {
  139. reason = reasons[regs[20]];
  140. }
  141. haltOtherCore();
  142. panicPutStr("Guru Meditation Error: Core ");
  143. panicPutDec(xPortGetCoreID());
  144. panicPutStr(" panic'ed (");
  145. panicPutStr(reason);
  146. panicPutStr(")\r\n");
  147. if (inOCDMode()) {
  148. asm("break.n 1");
  149. }
  150. commonErrorHandler(frame);
  151. }
  152. static void setFirstBreakpoint(uint32_t pc)
  153. {
  154. asm(
  155. "wsr.ibreaka0 %0\n" \
  156. "rsr.ibreakenable a3\n" \
  157. "movi a4,1\n" \
  158. "or a4, a4, a3\n" \
  159. "wsr.ibreakenable a4\n" \
  160. ::"r"(pc):"a3", "a4");
  161. }
  162. void xt_unhandled_exception(XtExcFrame *frame)
  163. {
  164. int *regs = (int *)frame;
  165. int x;
  166. haltOtherCore();
  167. panicPutStr("Guru Meditation Error of type ");
  168. x = regs[20];
  169. if (x < 40) {
  170. panicPutStr(edesc[x]);
  171. } else {
  172. panicPutStr("Unknown");
  173. }
  174. panicPutStr(" occurred on core ");
  175. panicPutDec(xPortGetCoreID());
  176. if (inOCDMode()) {
  177. panicPutStr(" at pc=");
  178. panicPutHex(regs[1]);
  179. panicPutStr(". Setting bp and returning..\r\n");
  180. //Stick a hardware breakpoint on the address the handler returns to. This way, the OCD debugger
  181. //will kick in exactly at the context the error happened.
  182. setFirstBreakpoint(regs[1]);
  183. return;
  184. }
  185. panicPutStr(". Exception was unhandled.\r\n");
  186. commonErrorHandler(frame);
  187. }
  188. /*
  189. If watchdogs are enabled, the panic handler runs the risk of getting aborted pre-emptively because
  190. an overzealous watchdog decides to reset it. On the other hand, if we disable all watchdogs, we run
  191. the risk of somehow halting in the panic handler and not resetting. That is why this routine kills
  192. all watchdogs except the timer group 0 watchdog, and it reconfigures that to reset the chip after
  193. one second.
  194. */
  195. static void reconfigureAllWdts()
  196. {
  197. TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  198. TIMERG0.wdt_feed = 1;
  199. TIMERG0.wdt_config0.sys_reset_length = 7; //3.2uS
  200. TIMERG0.wdt_config0.cpu_reset_length = 7; //3.2uS
  201. TIMERG0.wdt_config0.stg0 = TIMG_WDT_STG_SEL_RESET_SYSTEM; //1st stage timeout: reset system
  202. TIMERG0.wdt_config1.clk_prescale = 80 * 500; //Prescaler: wdt counts in ticks of 0.5mS
  203. TIMERG0.wdt_config2 = 2000; //1 second before reset
  204. TIMERG0.wdt_config0.en = 1;
  205. TIMERG0.wdt_wprotect = 0;
  206. //Disable wdt 1
  207. TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  208. TIMERG1.wdt_config0.en = 0;
  209. TIMERG1.wdt_wprotect = 0;
  210. }
  211. #if CONFIG_ESP32_PANIC_GDBSTUB || CONFIG_ESP32_PANIC_PRINT_HALT
  212. /*
  213. This disables all the watchdogs for when we call the gdbstub.
  214. */
  215. static void disableAllWdts()
  216. {
  217. TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  218. TIMERG0.wdt_config0.en = 0;
  219. TIMERG0.wdt_wprotect = 0;
  220. TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
  221. TIMERG1.wdt_config0.en = 0;
  222. TIMERG0.wdt_wprotect = 0;
  223. }
  224. #endif
  225. static inline bool stackPointerIsSane(uint32_t sp)
  226. {
  227. return !(sp < 0x3ffae010 || sp > 0x3ffffff0 || ((sp & 0xf) != 0));
  228. }
  229. static void putEntry(uint32_t pc, uint32_t sp)
  230. {
  231. if (pc & 0x80000000) {
  232. pc = (pc & 0x3fffffff) | 0x40000000;
  233. }
  234. panicPutStr(" 0x");
  235. panicPutHex(pc);
  236. panicPutStr(":0x");
  237. panicPutHex(sp);
  238. }
  239. void doBacktrace(XtExcFrame *frame)
  240. {
  241. uint32_t i = 0, pc = frame->pc, sp = frame->a1;
  242. panicPutStr("\nBacktrace:");
  243. /* Do not check sanity on first entry, PC could be smashed. */
  244. putEntry(pc, sp);
  245. pc = frame->a0;
  246. while (i++ < 100) {
  247. uint32_t psp = sp;
  248. if (!stackPointerIsSane(sp) || i++ > 100) {
  249. break;
  250. }
  251. sp = *((uint32_t *) (sp - 0x10 + 4));
  252. putEntry(pc, sp);
  253. pc = *((uint32_t *) (psp - 0x10));
  254. if (pc < 0x40000000) {
  255. break;
  256. }
  257. }
  258. panicPutStr("\n\n");
  259. }
  260. /*
  261. We arrive here after a panic or unhandled exception, when no OCD is detected. Dump the registers to the
  262. serial port and either jump to the gdb stub, halt the CPU or reboot.
  263. */
  264. void commonErrorHandler(XtExcFrame *frame)
  265. {
  266. int *regs = (int *)frame;
  267. int x, y;
  268. const char *sdesc[] = {
  269. "PC ", "PS ", "A0 ", "A1 ", "A2 ", "A3 ", "A4 ", "A5 ",
  270. "A6 ", "A7 ", "A8 ", "A9 ", "A10 ", "A11 ", "A12 ", "A13 ",
  271. "A14 ", "A15 ", "SAR ", "EXCCAUSE", "EXCVADDR", "LBEG ", "LEND ", "LCOUNT "
  272. };
  273. //Feed the watchdogs, so they will give us time to print out debug info
  274. reconfigureAllWdts();
  275. panicPutStr("Register dump:\r\n");
  276. for (x = 0; x < 24; x += 4) {
  277. for (y = 0; y < 4; y++) {
  278. if (sdesc[x + y][0] != 0) {
  279. panicPutStr(sdesc[x + y]);
  280. panicPutStr(": 0x");
  281. panicPutHex(regs[x + y + 1]);
  282. panicPutStr(" ");
  283. }
  284. }
  285. panicPutStr("\r\n");
  286. }
  287. /* With windowed ABI backtracing is easy, let's do it. */
  288. doBacktrace(frame);
  289. #if CONFIG_ESP32_PANIC_GDBSTUB
  290. disableAllWdts();
  291. panicPutStr("Entering gdb stub now.\r\n");
  292. esp_gdbstub_panic_handler(frame);
  293. #elif CONFIG_ESP32_PANIC_PRINT_REBOOT || CONFIG_ESP32_PANIC_SILENT_REBOOT
  294. panicPutStr("Rebooting...\r\n");
  295. for (x = 0; x < 100; x++) {
  296. ets_delay_us(1000);
  297. }
  298. software_reset();
  299. #else
  300. disableAllWdts();
  301. panicPutStr("CPU halted.\r\n");
  302. while (1);
  303. #endif
  304. }
  305. void esp_set_breakpoint_if_jtag(void *fn)
  306. {
  307. if (!inOCDMode()) {
  308. return;
  309. }
  310. setFirstBreakpoint((uint32_t)fn);
  311. }