trap.c 11 KB

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