trap.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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->cpsr;
  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->cpsr);
  81. rt_kprintf("EPC :0x%16.16p\n", (void *)regs->pc);
  82. }
  83. void rt_hw_trap_irq(void)
  84. {
  85. #ifdef SOC_BCM283x
  86. extern rt_uint8_t core_timer_flag;
  87. void *param;
  88. uint32_t irq;
  89. rt_isr_handler_t isr_func;
  90. extern struct rt_irq_desc isr_table[];
  91. uint32_t value = 0;
  92. value = IRQ_PEND_BASIC & 0x3ff;
  93. if(core_timer_flag != 0)
  94. {
  95. uint32_t cpu_id = rt_hw_cpu_id();
  96. uint32_t int_source = CORE_IRQSOURCE(cpu_id);
  97. if (int_source & 0x0f)
  98. {
  99. if (int_source & 0x08)
  100. {
  101. isr_func = isr_table[IRQ_ARM_TIMER].handler;
  102. #ifdef RT_USING_INTERRUPT_INFO
  103. isr_table[IRQ_ARM_TIMER].counter++;
  104. #endif
  105. if (isr_func)
  106. {
  107. param = isr_table[IRQ_ARM_TIMER].param;
  108. isr_func(IRQ_ARM_TIMER, param);
  109. }
  110. }
  111. }
  112. }
  113. /* local interrupt*/
  114. if (value)
  115. {
  116. if (value & (1 << 8))
  117. {
  118. value = IRQ_PEND1;
  119. irq = __rt_ffs(value) - 1;
  120. }
  121. else if (value & (1 << 9))
  122. {
  123. value = IRQ_PEND2;
  124. irq = __rt_ffs(value) + 31;
  125. }
  126. else
  127. {
  128. value &= 0x0f;
  129. irq = __rt_ffs(value) + 63;
  130. }
  131. /* get interrupt service routine */
  132. isr_func = isr_table[irq].handler;
  133. #ifdef RT_USING_INTERRUPT_INFO
  134. isr_table[irq].counter++;
  135. #endif
  136. if (isr_func)
  137. {
  138. /* Interrupt for myself. */
  139. param = isr_table[irq].param;
  140. /* turn to interrupt service routine */
  141. isr_func(irq, param);
  142. }
  143. }
  144. #else
  145. void *param;
  146. int ir, ir_self;
  147. rt_isr_handler_t isr_func;
  148. extern struct rt_irq_desc isr_table[];
  149. ir = rt_hw_interrupt_get_irq();
  150. if (ir == 1023)
  151. {
  152. /* Spurious interrupt */
  153. return;
  154. }
  155. /* bit 10~12 is cpuid, bit 0~9 is interrupt id */
  156. ir_self = ir & 0x3ffUL;
  157. /* get interrupt service routine */
  158. isr_func = isr_table[ir_self].handler;
  159. #ifdef RT_USING_INTERRUPT_INFO
  160. isr_table[ir_self].counter++;
  161. #endif
  162. if (isr_func)
  163. {
  164. /* Interrupt for myself. */
  165. param = isr_table[ir_self].param;
  166. /* turn to interrupt service routine */
  167. isr_func(ir_self, param);
  168. }
  169. /* end of interrupt */
  170. rt_hw_interrupt_ack(ir);
  171. #endif
  172. }
  173. void rt_hw_trap_fiq(void)
  174. {
  175. void *param;
  176. int ir, ir_self;
  177. rt_isr_handler_t isr_func;
  178. extern struct rt_irq_desc isr_table[];
  179. ir = rt_hw_interrupt_get_irq();
  180. /* bit 10~12 is cpuid, bit 0~9 is interrup id */
  181. ir_self = ir & 0x3ffUL;
  182. /* get interrupt service routine */
  183. isr_func = isr_table[ir_self].handler;
  184. param = isr_table[ir_self].param;
  185. /* turn to interrupt service routine */
  186. isr_func(ir_self, param);
  187. /* end of interrupt */
  188. rt_hw_interrupt_ack(ir);
  189. }
  190. void process_exception(unsigned long esr, unsigned long epc);
  191. void SVC_Handler(struct rt_hw_exp_stack *regs);
  192. void rt_hw_trap_exception(struct rt_hw_exp_stack *regs)
  193. {
  194. unsigned long esr;
  195. unsigned char ec;
  196. asm volatile("mrs %0, esr_el1":"=r"(esr));
  197. ec = (unsigned char)((esr >> 26) & 0x3fU);
  198. #ifdef RT_USING_LWP
  199. if (dbg_check_event(regs, esr))
  200. {
  201. return;
  202. }
  203. else
  204. #endif
  205. if (ec == 0x15) /* is 64bit syscall ? */
  206. {
  207. SVC_Handler(regs);
  208. /* never return here */
  209. }
  210. if (check_user_stack(esr, regs))
  211. {
  212. return;
  213. }
  214. process_exception(esr, regs->pc);
  215. rt_hw_show_register(regs);
  216. rt_kprintf("current: %s\n", rt_thread_self()->name);
  217. check_user_fault(regs, 0, "user fault");
  218. #ifdef RT_USING_FINSH
  219. list_thread();
  220. #endif
  221. rt_hw_cpu_shutdown();
  222. }
  223. void rt_hw_trap_serror(struct rt_hw_exp_stack *regs)
  224. {
  225. rt_kprintf("SError\n");
  226. rt_hw_show_register(regs);
  227. rt_kprintf("current: %s\n", rt_thread_self()->name);
  228. #ifdef RT_USING_FINSH
  229. list_thread();
  230. #endif
  231. rt_hw_cpu_shutdown();
  232. }