trap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 <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. #ifdef RT_USING_GDBSERVER
  25. #include <lwp_gdbserver.h>
  26. #include <hw_breakpoint.h>
  27. static int check_debug_event(struct rt_hw_exp_stack *regs, uint32_t pc_adj)
  28. {
  29. uint32_t mode = regs->cpsr;
  30. if ((mode & 0x1f) == 0x10) /* is user mode */
  31. {
  32. struct rt_channel_msg msg;
  33. gdb_thread_info thread_info;
  34. uint32_t ifsr, dfar, dfsr;
  35. int ret;
  36. if (pc_adj == 4) /* pabt */
  37. {
  38. /* check breakpoint event */
  39. asm volatile ("MRC p15, 0, %0, c5, c0, 1":"=r"(ifsr));
  40. ifsr &= ((1UL << 12) | 0x3fUL); /* status */
  41. if (ifsr == 0x2UL)
  42. {
  43. /* is breakpoint event */
  44. regs->pc -= pc_adj;
  45. do {
  46. struct rt_lwp *gdb_lwp = gdb_get_dbg_lwp();
  47. struct rt_lwp *lwp;
  48. if (!gdb_lwp)
  49. {
  50. break;
  51. }
  52. lwp = lwp_self();
  53. if (lwp == gdb_lwp)
  54. {
  55. break;
  56. }
  57. *(uint32_t *)regs->pc = lwp->bak_first_ins;
  58. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, (void *)regs->pc, 4);
  59. icache_invalid_all();
  60. lwp->debug = 0;
  61. return 1;
  62. } while (0);
  63. thread_info.notify_type = GDB_NOTIFIY_BREAKPOINT;
  64. thread_info.abt_ins = *(uint32_t *)regs->pc;
  65. ret = 1;
  66. }
  67. else
  68. {
  69. return 0; /* not debug pabt */
  70. }
  71. }
  72. else
  73. {
  74. /* watchpoing event */
  75. asm volatile ("MRC p15, 0, %0, c5, c0, 0":"=r"(dfsr));
  76. dfsr = (((dfsr & (1UL << 10)) >> 6) | (dfsr & 0xfUL)); /* status */
  77. if (dfsr == 0x2UL)
  78. {
  79. /* is watchpoint event */
  80. regs->pc -= pc_adj;
  81. asm volatile ("MRC p15, 0, %0, c6, c0, 0":"=r"(dfar));
  82. thread_info.watch_addr = (void *)dfar;
  83. thread_info.rw = (1UL << (((~*(uint32_t *)regs->pc) >> 20) & 1UL));
  84. thread_info.notify_type = GDB_NOTIFIY_WATCHPOINT;
  85. ret = 2;
  86. }
  87. else
  88. {
  89. return 0; /* not debug dabt */
  90. }
  91. }
  92. thread_info.thread = rt_thread_self();
  93. thread_info.thread->regs = regs;
  94. msg.u.d = (void *)&thread_info;
  95. rt_hw_dmb();
  96. thread_info.thread->debug_suspend = 1;
  97. rt_hw_dsb();
  98. rt_thread_suspend_with_flag(thread_info.thread, RT_UNINTERRUPTIBLE);
  99. rt_raw_channel_send(gdb_get_server_channel(), &msg);
  100. rt_schedule();
  101. while (thread_info.thread->debug_suspend)
  102. {
  103. rt_thread_suspend_with_flag(thread_info.thread, RT_UNINTERRUPTIBLE);
  104. rt_schedule();
  105. }
  106. return ret;
  107. }
  108. return 0;
  109. }
  110. #endif
  111. void sys_exit(int value);
  112. void check_user_fault(struct rt_hw_exp_stack *regs, uint32_t pc_adj, char *info)
  113. {
  114. uint32_t mode = regs->cpsr;
  115. if ((mode & 0x1f) == 0x10)
  116. {
  117. rt_kprintf("%s! pc = 0x%08x\n", info, regs->pc - pc_adj);
  118. #ifdef LWP_USING_CORE_DUMP
  119. lwp_core_dump(regs, pc_adj);
  120. #endif
  121. sys_exit(-1);
  122. }
  123. }
  124. int check_user_stack(struct rt_hw_exp_stack *regs)
  125. {
  126. void* dfar = RT_NULL;
  127. asm volatile ("MRC p15, 0, %0, c6, c0, 0":"=r"(dfar));
  128. if (arch_expand_user_stack(dfar))
  129. {
  130. regs->pc -= 8;
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. #endif
  136. /**
  137. * this function will show registers of CPU
  138. *
  139. * @param regs the registers point
  140. */
  141. void rt_hw_show_register(struct rt_hw_exp_stack *regs)
  142. {
  143. rt_kprintf("Execption:\n");
  144. rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n", regs->r0, regs->r1, regs->r2, regs->r3);
  145. rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n", regs->r4, regs->r5, regs->r6, regs->r7);
  146. rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n", regs->r8, regs->r9, regs->r10);
  147. rt_kprintf("fp :0x%08x ip :0x%08x\n", regs->fp, regs->ip);
  148. rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n", regs->sp, regs->lr, regs->pc);
  149. rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
  150. #ifdef RT_USING_USERSPACE
  151. {
  152. uint32_t v;
  153. asm volatile ("MRC p15, 0, %0, c5, c0, 0":"=r"(v));
  154. rt_kprintf("dfsr:0x%08x\n", v);
  155. asm volatile ("MRC p15, 0, %0, c2, c0, 0":"=r"(v));
  156. rt_kprintf("ttbr0:0x%08x\n", v);
  157. asm volatile ("MRC p15, 0, %0, c6, c0, 0":"=r"(v));
  158. rt_kprintf("dfar:0x%08x\n", v);
  159. rt_kprintf("0x%08x -> 0x%08x\n", v, rt_hw_mmu_v2p(&mmu_info, (void *)v));
  160. }
  161. #endif
  162. }
  163. /**
  164. * When comes across an instruction which it cannot handle,
  165. * it takes the undefined instruction trap.
  166. *
  167. * @param regs system registers
  168. *
  169. * @note never invoke this function in application
  170. */
  171. #ifdef RT_USING_FPU
  172. void set_fpexc(rt_uint32_t val);
  173. #endif
  174. void rt_hw_trap_undef(struct rt_hw_exp_stack *regs)
  175. {
  176. #ifdef RT_USING_FPU
  177. {
  178. uint32_t ins;
  179. uint32_t addr;
  180. if (regs->cpsr & (1 << 5))
  181. {
  182. /* thumb mode */
  183. addr = regs->pc - 2;
  184. ins = (uint32_t)*(uint16_t *)addr;
  185. if ((ins & (3 << 11)) != 0)
  186. {
  187. /* 32 bit ins */
  188. ins <<= 16;
  189. ins += *(uint16_t *)(addr + 2);
  190. }
  191. }
  192. else
  193. {
  194. addr = regs->pc - 4;
  195. ins = *(uint32_t *)addr;
  196. }
  197. if ((ins & 0xe00) == 0xa00)
  198. {
  199. /* float ins */
  200. set_fpexc(1U << 30);
  201. regs->pc = addr;
  202. return;
  203. }
  204. }
  205. #endif
  206. #ifdef RT_USING_LWP
  207. check_user_fault(regs, 4, "User undefined instruction");
  208. #endif
  209. rt_unwind(regs, 4);
  210. rt_kprintf("undefined instruction:\n");
  211. rt_hw_show_register(regs);
  212. #ifdef RT_USING_FINSH
  213. list_thread();
  214. #endif
  215. rt_hw_cpu_shutdown();
  216. }
  217. /**
  218. * The software interrupt instruction (SWI) is used for entering
  219. * Supervisor mode, usually to request a particular supervisor
  220. * function.
  221. *
  222. * @param regs system registers
  223. *
  224. * @note never invoke this function in application
  225. */
  226. void rt_hw_trap_swi(struct rt_hw_exp_stack *regs)
  227. {
  228. rt_kprintf("software interrupt:\n");
  229. rt_hw_show_register(regs);
  230. #ifdef RT_USING_FINSH
  231. list_thread();
  232. #endif
  233. rt_hw_cpu_shutdown();
  234. }
  235. /**
  236. * An abort indicates that the current memory access cannot be completed,
  237. * which occurs during an instruction prefetch.
  238. *
  239. * @param regs system registers
  240. *
  241. * @note never invoke this function in application
  242. */
  243. void rt_hw_trap_pabt(struct rt_hw_exp_stack *regs)
  244. {
  245. #ifdef RT_USING_LWP
  246. #ifdef RT_USING_GDBSERVER
  247. if (check_debug_event(regs, 4))
  248. {
  249. return;
  250. }
  251. #endif
  252. check_user_fault(regs, 4, "User prefetch abort");
  253. #endif
  254. rt_unwind(regs, 4);
  255. rt_kprintf("prefetch abort:\n");
  256. rt_hw_show_register(regs);
  257. #ifdef RT_USING_FINSH
  258. list_thread();
  259. #endif
  260. rt_hw_cpu_shutdown();
  261. }
  262. /**
  263. * An abort indicates that the current memory access cannot be completed,
  264. * which occurs during a data access.
  265. *
  266. * @param regs system registers
  267. *
  268. * @note never invoke this function in application
  269. */
  270. void rt_hw_trap_dabt(struct rt_hw_exp_stack *regs)
  271. {
  272. #ifdef RT_USING_LWP
  273. #ifdef RT_USING_GDBSERVER
  274. if (check_debug_event(regs, 8))
  275. {
  276. return;
  277. }
  278. #endif
  279. if (check_user_stack(regs))
  280. {
  281. return;
  282. }
  283. check_user_fault(regs, 8, "User data abort");
  284. #endif
  285. rt_unwind(regs, 8);
  286. rt_kprintf("data abort:");
  287. rt_hw_show_register(regs);
  288. #ifdef RT_USING_FINSH
  289. list_thread();
  290. #endif
  291. rt_hw_cpu_shutdown();
  292. }
  293. /**
  294. * Normally, system will never reach here
  295. *
  296. * @param regs system registers
  297. *
  298. * @note never invoke this function in application
  299. */
  300. void rt_hw_trap_resv(struct rt_hw_exp_stack *regs)
  301. {
  302. rt_kprintf("reserved trap:\n");
  303. rt_hw_show_register(regs);
  304. #ifdef RT_USING_FINSH
  305. list_thread();
  306. #endif
  307. rt_hw_cpu_shutdown();
  308. }
  309. void rt_hw_trap_irq(void)
  310. {
  311. #ifdef SOC_BCM283x
  312. extern rt_uint8_t core_timer_flag;
  313. void *param;
  314. uint32_t irq;
  315. rt_isr_handler_t isr_func;
  316. extern struct rt_irq_desc isr_table[];
  317. uint32_t value = 0;
  318. value = IRQ_PEND_BASIC & 0x3ff;
  319. if(core_timer_flag != 0)
  320. {
  321. uint32_t cpu_id = rt_hw_cpu_id();
  322. uint32_t int_source = CORE_IRQSOURCE(cpu_id);
  323. if (int_source & 0x0f)
  324. {
  325. if (int_source & 0x08)
  326. {
  327. isr_func = isr_table[IRQ_ARM_TIMER].handler;
  328. #ifdef RT_USING_INTERRUPT_INFO
  329. isr_table[IRQ_ARM_TIMER].counter++;
  330. #endif
  331. if (isr_func)
  332. {
  333. param = isr_table[IRQ_ARM_TIMER].param;
  334. isr_func(IRQ_ARM_TIMER, param);
  335. }
  336. }
  337. }
  338. }
  339. /* local interrupt*/
  340. if (value)
  341. {
  342. if (value & (1 << 8))
  343. {
  344. value = IRQ_PEND1;
  345. irq = __rt_ffs(value) - 1;
  346. }
  347. else if (value & (1 << 9))
  348. {
  349. value = IRQ_PEND2;
  350. irq = __rt_ffs(value) + 31;
  351. }
  352. else
  353. {
  354. value &= 0x0f;
  355. irq = __rt_ffs(value) + 63;
  356. }
  357. /* get interrupt service routine */
  358. isr_func = isr_table[irq].handler;
  359. #ifdef RT_USING_INTERRUPT_INFO
  360. isr_table[irq].counter++;
  361. #endif
  362. if (isr_func)
  363. {
  364. /* Interrupt for myself. */
  365. param = isr_table[irq].param;
  366. /* turn to interrupt service routine */
  367. isr_func(irq, param);
  368. }
  369. }
  370. #else
  371. void *param;
  372. int ir;
  373. rt_isr_handler_t isr_func;
  374. extern struct rt_irq_desc isr_table[];
  375. ir = rt_hw_interrupt_get_irq();
  376. if (ir == 1023)
  377. {
  378. /* Spurious interrupt */
  379. return;
  380. }
  381. /* get interrupt service routine */
  382. isr_func = isr_table[ir].handler;
  383. #ifdef RT_USING_INTERRUPT_INFO
  384. isr_table[ir].counter++;
  385. #endif
  386. if (isr_func)
  387. {
  388. /* Interrupt for myself. */
  389. param = isr_table[ir].param;
  390. /* turn to interrupt service routine */
  391. isr_func(ir, param);
  392. }
  393. /* end of interrupt */
  394. rt_hw_interrupt_ack(ir);
  395. #endif
  396. }
  397. void rt_hw_trap_fiq(void)
  398. {
  399. void *param;
  400. int ir;
  401. rt_isr_handler_t isr_func;
  402. extern struct rt_irq_desc isr_table[];
  403. ir = rt_hw_interrupt_get_irq();
  404. /* get interrupt service routine */
  405. isr_func = isr_table[ir].handler;
  406. param = isr_table[ir].param;
  407. /* turn to interrupt service routine */
  408. isr_func(ir, param);
  409. /* end of interrupt */
  410. rt_hw_interrupt_ack(ir);
  411. }