trap.c 8.3 KB

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