trap.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. void rt_unwind(struct rt_hw_exp_stack *regs, int pc_adj)
  16. {
  17. }
  18. #ifdef RT_USING_FINSH
  19. extern long list_thread(void);
  20. #endif
  21. #ifdef RT_USING_LWP
  22. #include <lwp.h>
  23. #include <lwp_arch.h>
  24. #ifdef LWP_USING_CORE_DUMP
  25. #include <lwp_core_dump.h>
  26. #endif
  27. void sys_exit(int value);
  28. void check_user_fault(struct rt_hw_exp_stack *regs, uint32_t pc_adj, char *info)
  29. {
  30. uint32_t mode = regs->spsr;
  31. if ((mode & 0x1f) == 0x00)
  32. {
  33. rt_kprintf("%s! pc = 0x%08x\n", info, regs->pc - pc_adj);
  34. #ifdef LWP_USING_CORE_DUMP
  35. lwp_core_dump(regs, pc_adj);
  36. #endif
  37. sys_exit(-1);
  38. }
  39. }
  40. int check_user_stack(unsigned long esr, struct rt_hw_exp_stack *regs)
  41. {
  42. unsigned char ec;
  43. void *dfar;
  44. int ret = 0;
  45. ec = (unsigned char)((esr >> 26) & 0x3fU);
  46. switch (ec)
  47. {
  48. case 0x20:
  49. case 0x21:
  50. case 0x24:
  51. asm volatile ("mrs %0, far_el1":"=r"(dfar));
  52. if (arch_expand_user_stack(dfar))
  53. {
  54. ret = 1;
  55. }
  56. break;
  57. default:
  58. break;
  59. }
  60. return ret;
  61. }
  62. #endif
  63. /**
  64. * this function will show registers of CPU
  65. *
  66. * @param regs the registers point
  67. */
  68. void rt_hw_show_register(struct rt_hw_exp_stack *regs)
  69. {
  70. rt_kprintf("Execption:\n");
  71. 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);
  72. 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);
  73. 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);
  74. 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);
  75. 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);
  76. 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);
  77. 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);
  78. rt_kprintf("X28:0x%16.16p X29:0x%16.16p X30:0x%16.16p\n", (void *)regs->x28, (void *)regs->x29, (void *)regs->x30);
  79. rt_kprintf("SP_EL0:0x%16.16p\n", (void *)regs->sp_el0);
  80. rt_kprintf("SPSR :0x%16.16p\n", (void *)regs->spsr);
  81. rt_kprintf("EPC :0x%16.16p\n", (void *)regs->pc);
  82. }
  83. /**
  84. * An abort indicates that the current memory access cannot be completed,
  85. * which occurs during an instruction prefetch.
  86. *
  87. * @param regs system registers
  88. *
  89. * @note never invoke this function in application
  90. */
  91. void rt_hw_trap_pabt(struct rt_hw_exp_stack *regs)
  92. {
  93. #ifdef RT_USING_LWP
  94. #ifdef RT_USING_GDBSERVER
  95. if (check_debug_event(regs, 4))
  96. {
  97. return;
  98. }
  99. #endif
  100. check_user_fault(regs, 4, "User prefetch abort");
  101. #endif
  102. rt_unwind(regs, 4);
  103. rt_kprintf("prefetch abort:\n");
  104. rt_hw_show_register(regs);
  105. #ifdef RT_USING_FINSH
  106. list_thread();
  107. #endif
  108. rt_hw_cpu_shutdown();
  109. }
  110. void rt_hw_trap_irq(void)
  111. {
  112. #ifdef SOC_BCM283x
  113. extern rt_uint8_t core_timer_flag;
  114. void *param;
  115. uint32_t irq;
  116. rt_isr_handler_t isr_func;
  117. extern struct rt_irq_desc isr_table[];
  118. uint32_t value = 0;
  119. value = IRQ_PEND_BASIC & 0x3ff;
  120. if(core_timer_flag != 0)
  121. {
  122. uint32_t cpu_id = rt_hw_cpu_id();
  123. uint32_t int_source = CORE_IRQSOURCE(cpu_id);
  124. if (int_source & 0x0f)
  125. {
  126. if (int_source & 0x08)
  127. {
  128. isr_func = isr_table[IRQ_ARM_TIMER].handler;
  129. #ifdef RT_USING_INTERRUPT_INFO
  130. isr_table[IRQ_ARM_TIMER].counter++;
  131. #endif
  132. if (isr_func)
  133. {
  134. param = isr_table[IRQ_ARM_TIMER].param;
  135. isr_func(IRQ_ARM_TIMER, param);
  136. }
  137. }
  138. }
  139. }
  140. /* local interrupt*/
  141. if (value)
  142. {
  143. if (value & (1 << 8))
  144. {
  145. value = IRQ_PEND1;
  146. irq = __rt_ffs(value) - 1;
  147. }
  148. else if (value & (1 << 9))
  149. {
  150. value = IRQ_PEND2;
  151. irq = __rt_ffs(value) + 31;
  152. }
  153. else
  154. {
  155. value &= 0x0f;
  156. irq = __rt_ffs(value) + 63;
  157. }
  158. /* get interrupt service routine */
  159. isr_func = isr_table[irq].handler;
  160. #ifdef RT_USING_INTERRUPT_INFO
  161. isr_table[irq].counter++;
  162. #endif
  163. if (isr_func)
  164. {
  165. /* Interrupt for myself. */
  166. param = isr_table[irq].param;
  167. /* turn to interrupt service routine */
  168. isr_func(irq, param);
  169. }
  170. }
  171. #else
  172. void *param;
  173. int ir;
  174. rt_isr_handler_t isr_func;
  175. extern struct rt_irq_desc isr_table[];
  176. ir = rt_hw_interrupt_get_irq();
  177. if (ir == 1023)
  178. {
  179. /* Spurious interrupt */
  180. return;
  181. }
  182. /* get interrupt service routine */
  183. isr_func = isr_table[ir].handler;
  184. #ifdef RT_USING_INTERRUPT_INFO
  185. isr_table[ir].counter++;
  186. #endif
  187. if (isr_func)
  188. {
  189. /* Interrupt for myself. */
  190. param = isr_table[ir].param;
  191. /* turn to interrupt service routine */
  192. isr_func(ir, param);
  193. }
  194. /* end of interrupt */
  195. rt_hw_interrupt_ack(ir);
  196. #endif
  197. }
  198. void rt_hw_trap_fiq(void)
  199. {
  200. void *param;
  201. int ir;
  202. rt_isr_handler_t isr_func;
  203. extern struct rt_irq_desc isr_table[];
  204. ir = rt_hw_interrupt_get_irq();
  205. /* get interrupt service routine */
  206. isr_func = isr_table[ir].handler;
  207. param = isr_table[ir].param;
  208. /* turn to interrupt service routine */
  209. isr_func(ir, param);
  210. /* end of interrupt */
  211. rt_hw_interrupt_ack(ir);
  212. }
  213. void process_exception(unsigned long esr, unsigned long epc);
  214. void SVC_Handler(struct rt_hw_exp_stack *regs);
  215. void rt_hw_trap_exception(struct rt_hw_exp_stack *regs)
  216. {
  217. unsigned long esr;
  218. unsigned char ec;
  219. asm volatile ("mrs %0, esr_el1":"=r"(esr));
  220. ec = (unsigned char)((esr >> 26) & 0x3fU);
  221. if (ec == 0x15) /* is 64bit syscall ? */
  222. {
  223. SVC_Handler(regs);
  224. /* never return here */
  225. }
  226. if (check_user_stack(esr, regs))
  227. {
  228. return;
  229. }
  230. process_exception(esr, regs->pc);
  231. rt_hw_show_register(regs);
  232. rt_kprintf("current: %s\n", rt_thread_self()->name);
  233. check_user_fault(regs, 0, "user fault");
  234. #ifdef RT_USING_FINSH
  235. list_thread();
  236. #endif
  237. rt_hw_cpu_shutdown();
  238. }
  239. void rt_hw_trap_serror(struct rt_hw_exp_stack *regs)
  240. {
  241. rt_kprintf("SError\n");
  242. rt_hw_show_register(regs);
  243. rt_kprintf("current: %s\n", rt_thread_self()->name);
  244. #ifdef RT_USING_FINSH
  245. list_thread();
  246. #endif
  247. rt_hw_cpu_shutdown();
  248. }