trap.c 8.4 KB

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