trap.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 "mm_aspace.h"
  16. #include <backtrace.h>
  17. void rt_unwind(struct rt_hw_exp_stack *regs, int pc_adj)
  18. {
  19. }
  20. #ifdef RT_USING_FINSH
  21. extern long list_thread(void);
  22. #endif
  23. #ifdef RT_USING_LWP
  24. #include <lwp.h>
  25. #include <lwp_arch.h>
  26. #ifdef LWP_USING_CORE_DUMP
  27. #include <lwp_core_dump.h>
  28. #endif
  29. static void _check_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. /* user stack backtrace */
  36. #ifdef RT_USING_LWP
  37. {
  38. rt_thread_t th;
  39. th = rt_thread_self();
  40. if (th && th->lwp)
  41. {
  42. rt_backtrace_user_thread(th);
  43. }
  44. }
  45. #endif
  46. #ifdef LWP_USING_CORE_DUMP
  47. lwp_core_dump(regs, pc_adj);
  48. #endif
  49. sys_exit_group(-1);
  50. }
  51. else
  52. {
  53. /* user stack backtrace */
  54. #ifdef RT_USING_LWP
  55. {
  56. rt_thread_t th;
  57. th = rt_thread_self();
  58. if (th && th->lwp)
  59. {
  60. rt_backtrace_user_thread(th);
  61. }
  62. }
  63. #endif
  64. /* kernel stack backtrace */
  65. backtrace((unsigned long)regs->pc, (unsigned long)regs->x30, (unsigned long)regs->x29);
  66. }
  67. }
  68. int _get_type(unsigned long esr)
  69. {
  70. int ret;
  71. int fsc = esr & 0x3f;
  72. switch (fsc)
  73. {
  74. case 0x4:
  75. case 0x5:
  76. case 0x6:
  77. case 0x7:
  78. ret = MM_FAULT_TYPE_PAGE_FAULT;
  79. break;
  80. case 0x9:
  81. case 0xa:
  82. case 0xb:
  83. ret = MM_FAULT_TYPE_ACCESS_FAULT;
  84. break;
  85. default:
  86. ret = MM_FAULT_TYPE_GENERIC;
  87. }
  88. return ret;
  89. }
  90. int check_user_stack(unsigned long esr, struct rt_hw_exp_stack *regs)
  91. {
  92. unsigned char ec;
  93. void *dfar;
  94. int ret = 0;
  95. ec = (unsigned char)((esr >> 26) & 0x3fU);
  96. enum rt_mm_fault_op fault_op;
  97. enum rt_mm_fault_type fault_type;
  98. struct rt_lwp *lwp;
  99. switch (ec)
  100. {
  101. case 0x20:
  102. fault_op = MM_FAULT_OP_EXECUTE;
  103. fault_type = _get_type(esr);
  104. break;
  105. case 0x21:
  106. case 0x24:
  107. case 0x25:
  108. fault_op = MM_FAULT_OP_WRITE;
  109. fault_type = _get_type(esr);
  110. break;
  111. default:
  112. fault_op = 0;
  113. break;
  114. }
  115. if (fault_op)
  116. {
  117. asm volatile("mrs %0, far_el1":"=r"(dfar));
  118. struct rt_aspace_fault_msg msg = {
  119. .fault_op = fault_op,
  120. .fault_type = fault_type,
  121. .fault_vaddr = dfar,
  122. };
  123. lwp = lwp_self();
  124. RT_ASSERT(lwp);
  125. if (rt_aspace_fault_try_fix(lwp->aspace, &msg))
  126. {
  127. ret = 1;
  128. }
  129. }
  130. return ret;
  131. }
  132. #endif
  133. /**
  134. * this function will show registers of CPU
  135. *
  136. * @param regs the registers point
  137. */
  138. void rt_hw_show_register(struct rt_hw_exp_stack *regs)
  139. {
  140. rt_kprintf("Execption:\n");
  141. 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);
  142. 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);
  143. 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);
  144. 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);
  145. 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);
  146. 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);
  147. 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);
  148. rt_kprintf("X28:0x%16.16p X29:0x%16.16p X30:0x%16.16p\n", (void *)regs->x28, (void *)regs->x29, (void *)regs->x30);
  149. rt_kprintf("SP_EL0:0x%16.16p\n", (void *)regs->sp_el0);
  150. rt_kprintf("SPSR :0x%16.16p\n", (void *)regs->cpsr);
  151. rt_kprintf("EPC :0x%16.16p\n", (void *)regs->pc);
  152. }
  153. void rt_hw_trap_irq(void)
  154. {
  155. #ifdef SOC_BCM283x
  156. extern rt_uint8_t core_timer_flag;
  157. void *param;
  158. uint32_t irq;
  159. rt_isr_handler_t isr_func;
  160. extern struct rt_irq_desc isr_table[];
  161. uint32_t value = 0;
  162. value = IRQ_PEND_BASIC & 0x3ff;
  163. if(core_timer_flag != 0)
  164. {
  165. uint32_t cpu_id = rt_hw_cpu_id();
  166. uint32_t int_source = CORE_IRQSOURCE(cpu_id);
  167. if (int_source & 0x0f)
  168. {
  169. if (int_source & 0x08)
  170. {
  171. isr_func = isr_table[IRQ_ARM_TIMER].handler;
  172. #ifdef RT_USING_INTERRUPT_INFO
  173. isr_table[IRQ_ARM_TIMER].counter++;
  174. #endif
  175. if (isr_func)
  176. {
  177. param = isr_table[IRQ_ARM_TIMER].param;
  178. isr_func(IRQ_ARM_TIMER, param);
  179. }
  180. }
  181. }
  182. }
  183. /* local interrupt*/
  184. if (value)
  185. {
  186. if (value & (1 << 8))
  187. {
  188. value = IRQ_PEND1;
  189. irq = __rt_ffs(value) - 1;
  190. }
  191. else if (value & (1 << 9))
  192. {
  193. value = IRQ_PEND2;
  194. irq = __rt_ffs(value) + 31;
  195. }
  196. else
  197. {
  198. value &= 0x0f;
  199. irq = __rt_ffs(value) + 63;
  200. }
  201. /* get interrupt service routine */
  202. isr_func = isr_table[irq].handler;
  203. #ifdef RT_USING_INTERRUPT_INFO
  204. isr_table[irq].counter++;
  205. #endif
  206. if (isr_func)
  207. {
  208. /* Interrupt for myself. */
  209. param = isr_table[irq].param;
  210. /* turn to interrupt service routine */
  211. isr_func(irq, param);
  212. }
  213. }
  214. #else
  215. void *param;
  216. int ir, ir_self;
  217. rt_isr_handler_t isr_func;
  218. extern struct rt_irq_desc isr_table[];
  219. ir = rt_hw_interrupt_get_irq();
  220. if (ir == 1023)
  221. {
  222. /* Spurious interrupt */
  223. return;
  224. }
  225. /* bit 10~12 is cpuid, bit 0~9 is interrupt id */
  226. ir_self = ir & 0x3ffUL;
  227. /* get interrupt service routine */
  228. isr_func = isr_table[ir_self].handler;
  229. #ifdef RT_USING_INTERRUPT_INFO
  230. isr_table[ir_self].counter++;
  231. #endif
  232. if (isr_func)
  233. {
  234. /* Interrupt for myself. */
  235. param = isr_table[ir_self].param;
  236. /* turn to interrupt service routine */
  237. isr_func(ir_self, param);
  238. }
  239. /* end of interrupt */
  240. rt_hw_interrupt_ack(ir);
  241. #endif
  242. }
  243. void rt_hw_trap_fiq(void)
  244. {
  245. void *param;
  246. int ir, ir_self;
  247. rt_isr_handler_t isr_func;
  248. extern struct rt_irq_desc isr_table[];
  249. ir = rt_hw_interrupt_get_irq();
  250. /* bit 10~12 is cpuid, bit 0~9 is interrup id */
  251. ir_self = ir & 0x3ffUL;
  252. /* get interrupt service routine */
  253. isr_func = isr_table[ir_self].handler;
  254. param = isr_table[ir_self].param;
  255. /* turn to interrupt service routine */
  256. isr_func(ir_self, param);
  257. /* end of interrupt */
  258. rt_hw_interrupt_ack(ir);
  259. }
  260. void process_exception(unsigned long esr, unsigned long epc);
  261. void SVC_Handler(struct rt_hw_exp_stack *regs);
  262. void rt_hw_trap_exception(struct rt_hw_exp_stack *regs)
  263. {
  264. unsigned long esr;
  265. unsigned char ec;
  266. asm volatile("mrs %0, esr_el1":"=r"(esr));
  267. ec = (unsigned char)((esr >> 26) & 0x3fU);
  268. #ifdef RT_USING_LWP
  269. if (dbg_check_event(regs, esr))
  270. {
  271. return;
  272. }
  273. else
  274. #endif
  275. if (ec == 0x15) /* is 64bit syscall ? */
  276. {
  277. SVC_Handler(regs);
  278. /* never return here */
  279. }
  280. #ifdef RT_USING_LWP
  281. if (check_user_stack(esr, regs))
  282. {
  283. return;
  284. }
  285. #endif
  286. process_exception(esr, regs->pc);
  287. rt_hw_show_register(regs);
  288. rt_kprintf("current: %s\n", rt_thread_self()->parent.name);
  289. #ifdef RT_USING_FINSH
  290. list_thread();
  291. #endif
  292. #ifdef RT_USING_LWP
  293. _check_fault(regs, 0, "user fault");
  294. #endif
  295. rt_hw_cpu_shutdown();
  296. }
  297. void rt_hw_trap_serror(struct rt_hw_exp_stack *regs)
  298. {
  299. rt_kprintf("SError\n");
  300. rt_hw_show_register(regs);
  301. rt_kprintf("current: %s\n", rt_thread_self()->parent.name);
  302. #ifdef RT_USING_FINSH
  303. list_thread();
  304. #endif
  305. rt_hw_cpu_shutdown();
  306. }