trap.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (c) 2006-2021, 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 <backtrace.h>
  14. #include "interrupt.h"
  15. #ifdef RT_USING_FINSH
  16. extern long list_thread(void);
  17. #endif
  18. #ifdef RT_USING_LWP
  19. #include <lwp.h>
  20. #include <lwp_arch.h>
  21. #ifdef LWP_USING_CORE_DUMP
  22. #include <lwp_core_dump.h>
  23. #endif
  24. void sys_exit(int value);
  25. void check_user_fault(struct rt_hw_exp_stack *regs, uint32_t pc_adj, char *info)
  26. {
  27. uint32_t mode = regs->cpsr;
  28. if ((mode & 0x1f) == 0x10)
  29. {
  30. rt_kprintf("%s! pc = 0x%08x\n", info, regs->pc - pc_adj);
  31. #ifdef LWP_USING_CORE_DUMP
  32. lwp_core_dump(regs, pc_adj);
  33. #endif
  34. sys_exit(-1);
  35. }
  36. }
  37. int check_user_stack(struct rt_hw_exp_stack *regs)
  38. {
  39. void* dfar = RT_NULL;
  40. asm volatile ("MRC p15, 0, %0, c6, c0, 0":"=r"(dfar));
  41. if (arch_expand_user_stack(dfar))
  42. {
  43. regs->pc -= 8;
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. #endif
  49. /**
  50. * this function will show registers of CPU
  51. *
  52. * @param regs the registers point
  53. */
  54. void rt_hw_show_register(struct rt_hw_exp_stack *regs)
  55. {
  56. rt_kprintf("Execption:\n");
  57. rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n", regs->r0, regs->r1, regs->r2, regs->r3);
  58. rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n", regs->r4, regs->r5, regs->r6, regs->r7);
  59. rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n", regs->r8, regs->r9, regs->r10);
  60. rt_kprintf("fp :0x%08x ip :0x%08x\n", regs->fp, regs->ip);
  61. rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n", regs->sp, regs->lr, regs->pc);
  62. rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
  63. #ifdef RT_USING_USERSPACE
  64. {
  65. uint32_t v;
  66. asm volatile ("MRC p15, 0, %0, c5, c0, 0":"=r"(v));
  67. rt_kprintf("dfsr:0x%08x\n", v);
  68. asm volatile ("MRC p15, 0, %0, c2, c0, 0":"=r"(v));
  69. rt_kprintf("ttbr0:0x%08x\n", v);
  70. asm volatile ("MRC p15, 0, %0, c6, c0, 0":"=r"(v));
  71. rt_kprintf("dfar:0x%08x\n", v);
  72. rt_kprintf("0x%08x -> 0x%08x\n", v, rt_hw_mmu_v2p(&mmu_info, (void *)v));
  73. }
  74. #endif
  75. }
  76. /**
  77. * When comes across an instruction which it cannot handle,
  78. * it takes the undefined instruction trap.
  79. *
  80. * @param regs system registers
  81. *
  82. * @note never invoke this function in application
  83. */
  84. #ifdef RT_USING_FPU
  85. void set_fpexc(rt_uint32_t val);
  86. #endif
  87. void rt_hw_trap_undef(struct rt_hw_exp_stack *regs)
  88. {
  89. #ifdef RT_USING_FPU
  90. {
  91. uint32_t ins;
  92. uint32_t addr;
  93. if (regs->cpsr & (1 << 5))
  94. {
  95. /* thumb mode */
  96. addr = regs->pc - 2;
  97. ins = (uint32_t)*(uint16_t *)addr;
  98. if ((ins & (3 << 11)) != 0)
  99. {
  100. /* 32 bit ins */
  101. ins <<= 16;
  102. ins += *(uint16_t *)(addr + 2);
  103. }
  104. }
  105. else
  106. {
  107. addr = regs->pc - 4;
  108. ins = *(uint32_t *)addr;
  109. }
  110. if ((ins & 0xe00) == 0xa00)
  111. {
  112. /* float ins */
  113. set_fpexc(1U << 30);
  114. regs->pc = addr;
  115. return;
  116. }
  117. }
  118. #endif
  119. #ifdef RT_USING_LWP
  120. check_user_fault(regs, 4, "User undefined instruction");
  121. #endif
  122. rt_unwind(regs, 4);
  123. rt_kprintf("undefined instruction:\n");
  124. rt_hw_show_register(regs);
  125. #ifdef RT_USING_FINSH
  126. list_thread();
  127. #endif
  128. rt_hw_cpu_shutdown();
  129. }
  130. /**
  131. * The software interrupt instruction (SWI) is used for entering
  132. * Supervisor mode, usually to request a particular supervisor
  133. * function.
  134. *
  135. * @param regs system registers
  136. *
  137. * @note never invoke this function in application
  138. */
  139. void rt_hw_trap_swi(struct rt_hw_exp_stack *regs)
  140. {
  141. rt_kprintf("software interrupt:\n");
  142. rt_hw_show_register(regs);
  143. #ifdef RT_USING_FINSH
  144. list_thread();
  145. #endif
  146. rt_hw_cpu_shutdown();
  147. }
  148. /**
  149. * An abort indicates that the current memory access cannot be completed,
  150. * which occurs during an instruction prefetch.
  151. *
  152. * @param regs system registers
  153. *
  154. * @note never invoke this function in application
  155. */
  156. void rt_hw_trap_pabt(struct rt_hw_exp_stack *regs)
  157. {
  158. #ifdef RT_USING_LWP
  159. if (dbg_check_event(regs, 4))
  160. {
  161. return;
  162. }
  163. check_user_fault(regs, 4, "User prefetch abort");
  164. #endif
  165. rt_unwind(regs, 4);
  166. rt_kprintf("prefetch abort:\n");
  167. rt_hw_show_register(regs);
  168. #ifdef RT_USING_FINSH
  169. list_thread();
  170. #endif
  171. rt_hw_cpu_shutdown();
  172. }
  173. /**
  174. * An abort indicates that the current memory access cannot be completed,
  175. * which occurs during a data access.
  176. *
  177. * @param regs system registers
  178. *
  179. * @note never invoke this function in application
  180. */
  181. void rt_hw_trap_dabt(struct rt_hw_exp_stack *regs)
  182. {
  183. #ifdef RT_USING_LWP
  184. if (dbg_check_event(regs, 8))
  185. {
  186. return;
  187. }
  188. if (check_user_stack(regs))
  189. {
  190. return;
  191. }
  192. check_user_fault(regs, 8, "User data abort");
  193. #endif
  194. rt_unwind(regs, 8);
  195. rt_kprintf("data abort:");
  196. rt_hw_show_register(regs);
  197. #ifdef RT_USING_FINSH
  198. list_thread();
  199. #endif
  200. rt_hw_cpu_shutdown();
  201. }
  202. /**
  203. * Normally, system will never reach here
  204. *
  205. * @param regs system registers
  206. *
  207. * @note never invoke this function in application
  208. */
  209. void rt_hw_trap_resv(struct rt_hw_exp_stack *regs)
  210. {
  211. rt_kprintf("reserved trap:\n");
  212. rt_hw_show_register(regs);
  213. #ifdef RT_USING_FINSH
  214. list_thread();
  215. #endif
  216. rt_hw_cpu_shutdown();
  217. }
  218. void rt_hw_trap_irq(void)
  219. {
  220. #ifdef SOC_BCM283x
  221. extern rt_uint8_t core_timer_flag;
  222. void *param;
  223. uint32_t irq;
  224. rt_isr_handler_t isr_func;
  225. extern struct rt_irq_desc isr_table[];
  226. uint32_t value = 0;
  227. value = IRQ_PEND_BASIC & 0x3ff;
  228. if(core_timer_flag != 0)
  229. {
  230. uint32_t cpu_id = rt_hw_cpu_id();
  231. uint32_t int_source = CORE_IRQSOURCE(cpu_id);
  232. if (int_source & 0x0f)
  233. {
  234. if (int_source & 0x08)
  235. {
  236. isr_func = isr_table[IRQ_ARM_TIMER].handler;
  237. #ifdef RT_USING_INTERRUPT_INFO
  238. isr_table[IRQ_ARM_TIMER].counter++;
  239. #endif
  240. if (isr_func)
  241. {
  242. param = isr_table[IRQ_ARM_TIMER].param;
  243. isr_func(IRQ_ARM_TIMER, param);
  244. }
  245. }
  246. }
  247. }
  248. /* local interrupt*/
  249. if (value)
  250. {
  251. if (value & (1 << 8))
  252. {
  253. value = IRQ_PEND1;
  254. irq = __rt_ffs(value) - 1;
  255. }
  256. else if (value & (1 << 9))
  257. {
  258. value = IRQ_PEND2;
  259. irq = __rt_ffs(value) + 31;
  260. }
  261. else
  262. {
  263. value &= 0x0f;
  264. irq = __rt_ffs(value) + 63;
  265. }
  266. /* get interrupt service routine */
  267. isr_func = isr_table[irq].handler;
  268. #ifdef RT_USING_INTERRUPT_INFO
  269. isr_table[irq].counter++;
  270. #endif
  271. if (isr_func)
  272. {
  273. /* Interrupt for myself. */
  274. param = isr_table[irq].param;
  275. /* turn to interrupt service routine */
  276. isr_func(irq, param);
  277. }
  278. }
  279. #else
  280. void *param;
  281. int ir, ir_real;
  282. rt_isr_handler_t isr_func;
  283. extern struct rt_irq_desc isr_table[];
  284. ir = rt_hw_interrupt_get_irq();
  285. ir_real = ir & 0x3ff;
  286. if (ir == 1023)
  287. {
  288. /* Spurious interrupt */
  289. return;
  290. }
  291. /* get interrupt service routine */
  292. isr_func = isr_table[ir_real].handler;
  293. #ifdef RT_USING_INTERRUPT_INFO
  294. isr_table[ir_real].counter++;
  295. #endif
  296. if (isr_func)
  297. {
  298. /* Interrupt for myself. */
  299. param = isr_table[ir_real].param;
  300. /* turn to interrupt service routine */
  301. isr_func(ir, param);
  302. }
  303. /* end of interrupt */
  304. rt_hw_interrupt_ack(ir);
  305. #endif
  306. }
  307. void rt_hw_trap_fiq(void)
  308. {
  309. void *param;
  310. int ir;
  311. rt_isr_handler_t isr_func;
  312. extern struct rt_irq_desc isr_table[];
  313. ir = rt_hw_interrupt_get_irq();
  314. /* get interrupt service routine */
  315. isr_func = isr_table[ir].handler;
  316. param = isr_table[ir].param;
  317. /* turn to interrupt service routine */
  318. isr_func(ir, param);
  319. /* end of interrupt */
  320. rt_hw_interrupt_ack(ir);
  321. }