trap.c 8.4 KB

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