trap.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2013-07-20 Bernard first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <board.h>
  13. #include <armv8.h>
  14. #include "interrupt.h"
  15. #include <backtrace.h>
  16. void rt_unwind(struct rt_hw_exp_stack *regs, int pc_adj)
  17. {
  18. }
  19. #ifdef RT_USING_FINSH
  20. extern long list_thread(void);
  21. #endif
  22. #ifdef RT_USING_LWP
  23. #include <lwp.h>
  24. #include <lwp_arch.h>
  25. #ifdef LWP_USING_CORE_DUMP
  26. #include <lwp_core_dump.h>
  27. #endif
  28. void sys_exit(int value);
  29. void check_user_fault(struct rt_hw_exp_stack *regs, uint32_t pc_adj, char *info)
  30. {
  31. uint32_t mode = regs->cpsr;
  32. if ((mode & 0x1f) == 0x00)
  33. {
  34. rt_kprintf("%s! pc = 0x%08x\n", info, regs->pc - pc_adj);
  35. #ifdef LWP_USING_CORE_DUMP
  36. lwp_core_dump(regs, pc_adj);
  37. #endif
  38. sys_exit(-1);
  39. }
  40. }
  41. int check_user_stack(unsigned long esr, struct rt_hw_exp_stack *regs)
  42. {
  43. unsigned char ec;
  44. void *dfar;
  45. int ret = 0;
  46. ec = (unsigned char)((esr >> 26) & 0x3fU);
  47. switch (ec)
  48. {
  49. case 0x20:
  50. case 0x21:
  51. case 0x24:
  52. asm volatile("mrs %0, far_el1":"=r"(dfar));
  53. if (arch_expand_user_stack(dfar))
  54. {
  55. ret = 1;
  56. }
  57. break;
  58. default:
  59. break;
  60. }
  61. return ret;
  62. }
  63. #endif
  64. /**
  65. * this function will show registers of CPU
  66. *
  67. * @param regs the registers point
  68. */
  69. void rt_hw_show_register(struct rt_hw_exp_stack *regs)
  70. {
  71. rt_kprintf("Execption:\n");
  72. rt_kprintf("X00:0x%16.16p X01:0x%16.16p X02:0x%16.16p X03:0x%16.16p\n", (void *)regs->x0, (void *)regs->x1, (void *)regs->x2, (void *)regs->x3);
  73. rt_kprintf("X04:0x%16.16p X05:0x%16.16p X06:0x%16.16p X07:0x%16.16p\n", (void *)regs->x4, (void *)regs->x5, (void *)regs->x6, (void *)regs->x7);
  74. rt_kprintf("X08:0x%16.16p X09:0x%16.16p X10:0x%16.16p X11:0x%16.16p\n", (void *)regs->x8, (void *)regs->x9, (void *)regs->x10, (void *)regs->x11);
  75. rt_kprintf("X12:0x%16.16p X13:0x%16.16p X14:0x%16.16p X15:0x%16.16p\n", (void *)regs->x12, (void *)regs->x13, (void *)regs->x14, (void *)regs->x15);
  76. rt_kprintf("X16:0x%16.16p X17:0x%16.16p X18:0x%16.16p X19:0x%16.16p\n", (void *)regs->x16, (void *)regs->x17, (void *)regs->x18, (void *)regs->x19);
  77. rt_kprintf("X20:0x%16.16p X21:0x%16.16p X22:0x%16.16p X23:0x%16.16p\n", (void *)regs->x20, (void *)regs->x21, (void *)regs->x22, (void *)regs->x23);
  78. rt_kprintf("X24:0x%16.16p X25:0x%16.16p X26:0x%16.16p X27:0x%16.16p\n", (void *)regs->x24, (void *)regs->x25, (void *)regs->x26, (void *)regs->x27);
  79. rt_kprintf("X28:0x%16.16p X29:0x%16.16p X30:0x%16.16p\n", (void *)regs->x28, (void *)regs->x29, (void *)regs->x30);
  80. rt_kprintf("SP_EL0:0x%16.16p\n", (void *)regs->sp_el0);
  81. rt_kprintf("SPSR :0x%16.16p\n", (void *)regs->cpsr);
  82. rt_kprintf("EPC :0x%16.16p\n", (void *)regs->pc);
  83. }
  84. void rt_hw_trap_irq(void)
  85. {
  86. #ifdef SOC_BCM283x
  87. extern rt_uint8_t core_timer_flag;
  88. void *param;
  89. uint32_t irq;
  90. rt_isr_handler_t isr_func;
  91. extern struct rt_irq_desc isr_table[];
  92. uint32_t value = 0;
  93. value = IRQ_PEND_BASIC & 0x3ff;
  94. if(core_timer_flag != 0)
  95. {
  96. uint32_t cpu_id = rt_hw_cpu_id();
  97. uint32_t int_source = CORE_IRQSOURCE(cpu_id);
  98. if (int_source & 0x0f)
  99. {
  100. if (int_source & 0x08)
  101. {
  102. isr_func = isr_table[IRQ_ARM_TIMER].handler;
  103. #ifdef RT_USING_INTERRUPT_INFO
  104. isr_table[IRQ_ARM_TIMER].counter++;
  105. #endif
  106. if (isr_func)
  107. {
  108. param = isr_table[IRQ_ARM_TIMER].param;
  109. isr_func(IRQ_ARM_TIMER, param);
  110. }
  111. }
  112. }
  113. }
  114. /* local interrupt*/
  115. if (value)
  116. {
  117. if (value & (1 << 8))
  118. {
  119. value = IRQ_PEND1;
  120. irq = __rt_ffs(value) - 1;
  121. }
  122. else if (value & (1 << 9))
  123. {
  124. value = IRQ_PEND2;
  125. irq = __rt_ffs(value) + 31;
  126. }
  127. else
  128. {
  129. value &= 0x0f;
  130. irq = __rt_ffs(value) + 63;
  131. }
  132. /* get interrupt service routine */
  133. isr_func = isr_table[irq].handler;
  134. #ifdef RT_USING_INTERRUPT_INFO
  135. isr_table[irq].counter++;
  136. #endif
  137. if (isr_func)
  138. {
  139. /* Interrupt for myself. */
  140. param = isr_table[irq].param;
  141. /* turn to interrupt service routine */
  142. isr_func(irq, param);
  143. }
  144. }
  145. #else
  146. void *param;
  147. int ir, ir_self;
  148. rt_isr_handler_t isr_func;
  149. extern struct rt_irq_desc isr_table[];
  150. ir = rt_hw_interrupt_get_irq();
  151. if (ir == 1023)
  152. {
  153. /* Spurious interrupt */
  154. return;
  155. }
  156. /* bit 10~12 is cpuid, bit 0~9 is interrupt id */
  157. ir_self = ir & 0x3ffUL;
  158. /* get interrupt service routine */
  159. isr_func = isr_table[ir_self].handler;
  160. #ifdef RT_USING_INTERRUPT_INFO
  161. isr_table[ir_self].counter++;
  162. #endif
  163. if (isr_func)
  164. {
  165. /* Interrupt for myself. */
  166. param = isr_table[ir_self].param;
  167. /* turn to interrupt service routine */
  168. isr_func(ir_self, param);
  169. }
  170. /* end of interrupt */
  171. rt_hw_interrupt_ack(ir);
  172. #endif
  173. }
  174. void rt_hw_trap_fiq(void)
  175. {
  176. void *param;
  177. int ir, ir_self;
  178. rt_isr_handler_t isr_func;
  179. extern struct rt_irq_desc isr_table[];
  180. ir = rt_hw_interrupt_get_irq();
  181. /* bit 10~12 is cpuid, bit 0~9 is interrup id */
  182. ir_self = ir & 0x3ffUL;
  183. /* get interrupt service routine */
  184. isr_func = isr_table[ir_self].handler;
  185. param = isr_table[ir_self].param;
  186. /* turn to interrupt service routine */
  187. isr_func(ir_self, param);
  188. /* end of interrupt */
  189. rt_hw_interrupt_ack(ir);
  190. }
  191. void process_exception(unsigned long esr, unsigned long epc);
  192. void SVC_Handler(struct rt_hw_exp_stack *regs);
  193. void rt_hw_trap_exception(struct rt_hw_exp_stack *regs)
  194. {
  195. unsigned long esr;
  196. unsigned char ec;
  197. asm volatile("mrs %0, esr_el1":"=r"(esr));
  198. ec = (unsigned char)((esr >> 26) & 0x3fU);
  199. #ifdef RT_USING_LWP
  200. if (dbg_check_event(regs, esr))
  201. {
  202. return;
  203. }
  204. else
  205. #endif
  206. if (ec == 0x15) /* is 64bit syscall ? */
  207. {
  208. SVC_Handler(regs);
  209. /* never return here */
  210. }
  211. #ifdef RT_USING_LWP
  212. if (check_user_stack(esr, regs))
  213. {
  214. return;
  215. }
  216. #endif
  217. process_exception(esr, regs->pc);
  218. rt_hw_show_register(regs);
  219. rt_kprintf("current: %s\n", rt_thread_self()->name);
  220. #ifdef RT_USING_LWP
  221. check_user_fault(regs, 0, "user fault");
  222. #endif
  223. #ifdef RT_USING_FINSH
  224. list_thread();
  225. #endif
  226. backtrace((unsigned long)regs->pc, (unsigned long)regs->x30, (unsigned long)regs->x29);
  227. rt_hw_cpu_shutdown();
  228. }
  229. void rt_hw_trap_serror(struct rt_hw_exp_stack *regs)
  230. {
  231. rt_kprintf("SError\n");
  232. rt_hw_show_register(regs);
  233. rt_kprintf("current: %s\n", rt_thread_self()->name);
  234. #ifdef RT_USING_FINSH
  235. list_thread();
  236. #endif
  237. rt_hw_cpu_shutdown();
  238. }