faults.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <arch/arm.h>
  25. #include <kernel/thread.h>
  26. #include <platform/ram_console.h>
  27. // #include <platform/mtk_wdt.h> // for mtk_wdt_restart();
  28. void mtk_wdt_restart(void);
  29. void dump_code_section(void)
  30. {
  31. #ifdef DEBUG
  32. #if (DEBUG >= SPEW)
  33. // Dump 1 MB of the code section.
  34. const char *code = (char*)MEMBASE;
  35. const int one_eigth_MB = 131072;
  36. dprintf(CRITICAL, "Dump code from 0x%p:\n", code);
  37. for (int i = 0; i < 8; i++) {
  38. mtk_wdt_restart();
  39. hexdump((void*)code, one_eigth_MB);
  40. code += one_eigth_MB;
  41. }
  42. #endif // (DEBUG >= SPEW)
  43. #endif // DEBUG
  44. }
  45. static inline uint32_t arch_read_dfsr(void)
  46. {
  47. uint32_t reg;
  48. __asm__ volatile("mrc p15, 0, %0, c5, c0, 0" : "=r" (reg));
  49. return reg;
  50. }
  51. static inline uint32_t arch_read_dfar(void)
  52. {
  53. uint32_t reg;
  54. __asm__ volatile("mrc p15, 0, %0, c6, c0, 0" : "=r" (reg));
  55. return reg;
  56. }
  57. static void dump_fault_frame(struct arm_fault_frame *frame)
  58. {
  59. dprintf(CRITICAL, "r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n", frame->r[0], frame->r[1], frame->r[2], frame->r[3]);
  60. dprintf(CRITICAL, "r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n", frame->r[4], frame->r[5], frame->r[6], frame->r[7]);
  61. dprintf(CRITICAL, "r8 0x%08x r9 0x%08x r10 0x%08x r11 0x%08x\n", frame->r[8], frame->r[9], frame->r[10], frame->r[11]);
  62. dprintf(CRITICAL, "r12 0x%08x usp 0x%08x ulr 0x%08x pc 0x%08x\n", frame->r[12], frame->usp, frame->ulr, frame->pc);
  63. dprintf(CRITICAL, "spsr 0x%08x\n", frame->spsr);
  64. dprintf(CRITICAL, "spsr 0x%08x dfsr 0x%08x dfar 0x%08x\n", frame->spsr, arch_read_dfsr(), arch_read_dfar());
  65. struct arm_mode_regs regs;
  66. arm_save_mode_regs(&regs);
  67. dprintf(CRITICAL, "%c%s r13 0x%08x r14 0x%08x\n", ((frame->spsr & MODE_MASK) == MODE_FIQ) ? '*' : ' ', "fiq", regs.fiq_r13, regs.fiq_r14);
  68. dprintf(CRITICAL, "%c%s r13 0x%08x r14 0x%08x\n", ((frame->spsr & MODE_MASK) == MODE_IRQ) ? '*' : ' ', "irq", regs.irq_r13, regs.irq_r14);
  69. dprintf(CRITICAL, "%c%s r13 0x%08x r14 0x%08x\n", ((frame->spsr & MODE_MASK) == MODE_SVC) ? '*' : ' ', "svc", regs.svc_r13, regs.svc_r14);
  70. dprintf(CRITICAL, "%c%s r13 0x%08x r14 0x%08x\n", ((frame->spsr & MODE_MASK) == MODE_ABT) ? '*' : ' ', "abt", regs.abt_r13, regs.abt_r14);
  71. dprintf(CRITICAL, "%c%s r13 0x%08x r14 0x%08x\n", ((frame->spsr & MODE_MASK) == MODE_UND) ? '*' : ' ', "und", regs.und_r13, regs.und_r14);
  72. dprintf(CRITICAL, "%c%s r13 0x%08x r14 0x%08x\n", ((frame->spsr & MODE_MASK) == MODE_SYS) ? '*' : ' ', "sys", regs.sys_r13, regs.sys_r14);
  73. // dump the bottom of the current stack
  74. addr_t stack;
  75. switch (frame->spsr & MODE_MASK) {
  76. case MODE_FIQ:
  77. stack = regs.fiq_r13;
  78. break;
  79. case MODE_IRQ:
  80. stack = regs.irq_r13;
  81. break;
  82. case MODE_SVC:
  83. stack = regs.svc_r13;
  84. break;
  85. case MODE_UND:
  86. stack = regs.und_r13;
  87. break;
  88. case MODE_SYS:
  89. stack = regs.sys_r13;
  90. break;
  91. default:
  92. stack = 0;
  93. }
  94. dprintf(CRITICAL, "Code: %04x %04x %04x %04x <%04x> %04x %04x %04x %04x\n",
  95. *(uint16_t *)(frame->pc-8),*(uint16_t *)(frame->pc-6),*(uint16_t *)(frame->pc-4), *(uint16_t *)(frame->pc-2), *(uint16_t *)(frame->pc),
  96. *(uint16_t *)(frame->pc+2), *(uint16_t *)(frame->pc+4), *(uint16_t *)(frame->pc+6), *(uint16_t *)(frame->pc+8));
  97. if (current_thread)
  98. {
  99. char *stack_limit = current_thread->stack;
  100. char *stack_bottom = stack_limit + current_thread->stack_size;
  101. dprintf(CRITICAL, "thread: %s, bottom(stack) = %p, limit(stack) = %p\n",
  102. current_thread->name, stack_bottom, stack_limit);
  103. if (stack != 0) {
  104. unsigned long length, len_ahead = 64;
  105. if (current_thread->stack_size > 0) {
  106. length = (unsigned long)(current_thread->stack + current_thread->stack_size)- (unsigned long)stack + len_ahead;
  107. if ((length <= len_ahead) || (length > DEFAULT_STACK_SIZE))
  108. length = 4096; //4K
  109. }
  110. else //i.e.: idle thread
  111. length = 384;
  112. dprintf(CRITICAL, "top of stack at 0x%08x:\n", (unsigned int)stack);
  113. hexdump((void*)((char*)stack - len_ahead), length);
  114. }
  115. }
  116. #ifdef DUMP_CODE_SECTION
  117. dump_code_section();
  118. #endif
  119. }
  120. static void exception_die(struct arm_fault_frame *frame, int pc_off, const char *msg)
  121. {
  122. ram_console_set_exp_type(AEE_EXP_TYPE_LK_CRASH);
  123. inc_critical_section();
  124. frame->pc += pc_off;
  125. dprintf(CRITICAL, "%s", msg);
  126. dump_fault_frame(frame);
  127. halt();
  128. for(;;);
  129. }
  130. void arm_syscall_handler(struct arm_fault_frame *frame)
  131. {
  132. exception_die(frame, -4, "unhandled syscall, halting\n");
  133. }
  134. void arm_undefined_handler(struct arm_fault_frame *frame)
  135. {
  136. exception_die(frame, -4, "undefined abort, halting\n");
  137. }
  138. void arm_data_abort_handler(struct arm_fault_frame *frame)
  139. {
  140. exception_die(frame, -8, "data abort, halting\n");
  141. }
  142. void arm_prefetch_abort_handler(struct arm_fault_frame *frame)
  143. {
  144. exception_die(frame, -4, "prefetch abort, halting\n");
  145. }