trap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 "arch.h"
  15. #include "interrupt.h"
  16. #ifdef RT_USING_FINSH
  17. extern long list_thread(void);
  18. #endif
  19. #ifdef RT_USING_LWP
  20. #include <lwp.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(thread_info.thread);
  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(thread_info.thread);
  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. #endif
  120. /**
  121. * this function will show registers of CPU
  122. *
  123. * @param regs the registers point
  124. */
  125. void rt_hw_show_register(struct rt_hw_exp_stack *regs)
  126. {
  127. rt_kprintf("Execption:\n");
  128. rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n", regs->r0, regs->r1, regs->r2, regs->r3);
  129. rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n", regs->r4, regs->r5, regs->r6, regs->r7);
  130. rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n", regs->r8, regs->r9, regs->r10);
  131. rt_kprintf("fp :0x%08x ip :0x%08x\n", regs->fp, regs->ip);
  132. rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n", regs->sp, regs->lr, regs->pc);
  133. rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
  134. #ifdef RT_USING_USERSPACE
  135. {
  136. uint32_t v;
  137. asm volatile ("MRC p15, 0, %0, c5, c0, 0":"=r"(v));
  138. rt_kprintf("dfsr:0x%08x\n", v);
  139. asm volatile ("MRC p15, 0, %0, c2, c0, 0":"=r"(v));
  140. rt_kprintf("ttbr0:0x%08x\n", v);
  141. asm volatile ("MRC p15, 0, %0, c6, c0, 0":"=r"(v));
  142. rt_kprintf("dfar:0x%08x\n", v);
  143. rt_kprintf("0x%08x -> 0x%08x\n", v, rt_hw_mmu_v2p(&mmu_info, (void*)v));
  144. }
  145. #endif
  146. }
  147. /**
  148. * When comes across an instruction which it cannot handle,
  149. * it takes the undefined instruction trap.
  150. *
  151. * @param regs system registers
  152. *
  153. * @note never invoke this function in application
  154. */
  155. #ifdef RT_USING_FPU
  156. void set_fpexc(rt_uint32_t val);
  157. #endif
  158. void rt_hw_trap_undef(struct rt_hw_exp_stack *regs)
  159. {
  160. #ifdef RT_USING_FPU
  161. {
  162. uint32_t ins;
  163. uint32_t addr;
  164. if (regs->cpsr & (1 << 5))
  165. {
  166. /* thumb mode */
  167. addr = regs->pc - 2;
  168. ins = (uint32_t)*(uint16_t*)addr;
  169. if ((ins & (3 << 11)) != 0)
  170. {
  171. /* 32 bit ins */
  172. ins <<= 16;
  173. ins += *(uint16_t*)(addr + 2);
  174. }
  175. }
  176. else
  177. {
  178. addr = regs->pc - 4;
  179. ins = *(uint32_t*)addr;
  180. }
  181. if ((ins & 0xe00) == 0xa00)
  182. {
  183. /* float ins */
  184. set_fpexc(1U << 30);
  185. regs->pc = addr;
  186. return;
  187. }
  188. }
  189. #endif
  190. #ifdef RT_USING_LWP
  191. check_user_fault(regs, 4, "User undefined instruction");
  192. #endif
  193. rt_unwind(regs, 4);
  194. rt_kprintf("undefined instruction:\n");
  195. rt_hw_show_register(regs);
  196. #ifdef RT_USING_FINSH
  197. list_thread();
  198. #endif
  199. rt_hw_cpu_shutdown();
  200. }
  201. /**
  202. * The software interrupt instruction (SWI) is used for entering
  203. * Supervisor mode, usually to request a particular supervisor
  204. * function.
  205. *
  206. * @param regs system registers
  207. *
  208. * @note never invoke this function in application
  209. */
  210. void rt_hw_trap_swi(struct rt_hw_exp_stack *regs)
  211. {
  212. rt_kprintf("software interrupt:\n");
  213. rt_hw_show_register(regs);
  214. #ifdef RT_USING_FINSH
  215. list_thread();
  216. #endif
  217. rt_hw_cpu_shutdown();
  218. }
  219. /**
  220. * An abort indicates that the current memory access cannot be completed,
  221. * which occurs during an instruction prefetch.
  222. *
  223. * @param regs system registers
  224. *
  225. * @note never invoke this function in application
  226. */
  227. void rt_hw_trap_pabt(struct rt_hw_exp_stack *regs)
  228. {
  229. #ifdef RT_USING_LWP
  230. #ifdef RT_USING_GDBSERVER
  231. if (check_debug_event(regs, 4))
  232. {
  233. return;
  234. }
  235. #endif
  236. check_user_fault(regs, 4, "User prefetch abort");
  237. #endif
  238. rt_unwind(regs, 4);
  239. rt_kprintf("prefetch abort:\n");
  240. rt_hw_show_register(regs);
  241. #ifdef RT_USING_FINSH
  242. list_thread();
  243. #endif
  244. rt_hw_cpu_shutdown();
  245. }
  246. /**
  247. * An abort indicates that the current memory access cannot be completed,
  248. * which occurs during a data access.
  249. *
  250. * @param regs system registers
  251. *
  252. * @note never invoke this function in application
  253. */
  254. void rt_hw_trap_dabt(struct rt_hw_exp_stack *regs)
  255. {
  256. #ifdef RT_USING_LWP
  257. #ifdef RT_USING_GDBSERVER
  258. if (check_debug_event(regs, 8))
  259. {
  260. return;
  261. }
  262. #endif
  263. check_user_fault(regs, 8, "User data abort");
  264. #endif
  265. rt_unwind(regs, 8);
  266. rt_kprintf("data abort:");
  267. rt_hw_show_register(regs);
  268. #ifdef RT_USING_FINSH
  269. list_thread();
  270. #endif
  271. rt_hw_cpu_shutdown();
  272. }
  273. /**
  274. * Normally, system will never reach here
  275. *
  276. * @param regs system registers
  277. *
  278. * @note never invoke this function in application
  279. */
  280. void rt_hw_trap_resv(struct rt_hw_exp_stack *regs)
  281. {
  282. rt_kprintf("reserved trap:\n");
  283. rt_hw_show_register(regs);
  284. #ifdef RT_USING_FINSH
  285. list_thread();
  286. #endif
  287. rt_hw_cpu_shutdown();
  288. }
  289. void rt_hw_trap_irq(void)
  290. {
  291. #ifdef SOC_BCM283x
  292. extern rt_uint8_t core_timer_flag;
  293. void *param;
  294. uint32_t irq;
  295. rt_isr_handler_t isr_func;
  296. extern struct rt_irq_desc isr_table[];
  297. uint32_t value = 0;
  298. value = IRQ_PEND_BASIC & 0x3ff;
  299. if(core_timer_flag != 0)
  300. {
  301. uint32_t cpu_id = rt_hw_cpu_id();
  302. uint32_t int_source = CORE_IRQSOURCE(cpu_id);
  303. if (int_source & 0x0f)
  304. {
  305. if (int_source & 0x08)
  306. {
  307. isr_func = isr_table[IRQ_ARM_TIMER].handler;
  308. #ifdef RT_USING_INTERRUPT_INFO
  309. isr_table[IRQ_ARM_TIMER].counter++;
  310. #endif
  311. if (isr_func)
  312. {
  313. param = isr_table[IRQ_ARM_TIMER].param;
  314. isr_func(IRQ_ARM_TIMER, param);
  315. }
  316. }
  317. }
  318. }
  319. /* local interrupt*/
  320. if (value)
  321. {
  322. if (value & (1 << 8))
  323. {
  324. value = IRQ_PEND1;
  325. irq = __rt_ffs(value) - 1;
  326. }
  327. else if (value & (1 << 9))
  328. {
  329. value = IRQ_PEND2;
  330. irq = __rt_ffs(value) + 31;
  331. }
  332. else
  333. {
  334. value &= 0x0f;
  335. irq = __rt_ffs(value) + 63;
  336. }
  337. /* get interrupt service routine */
  338. isr_func = isr_table[irq].handler;
  339. #ifdef RT_USING_INTERRUPT_INFO
  340. isr_table[irq].counter++;
  341. #endif
  342. if (isr_func)
  343. {
  344. /* Interrupt for myself. */
  345. param = isr_table[irq].param;
  346. /* turn to interrupt service routine */
  347. isr_func(irq, param);
  348. }
  349. }
  350. #else
  351. void *param;
  352. int ir;
  353. rt_isr_handler_t isr_func;
  354. extern struct rt_irq_desc isr_table[];
  355. ir = rt_hw_interrupt_get_irq();
  356. if (ir == 1023)
  357. {
  358. /* Spurious interrupt */
  359. return;
  360. }
  361. /* get interrupt service routine */
  362. isr_func = isr_table[ir].handler;
  363. #ifdef RT_USING_INTERRUPT_INFO
  364. isr_table[ir].counter++;
  365. #endif
  366. if (isr_func)
  367. {
  368. /* Interrupt for myself. */
  369. param = isr_table[ir].param;
  370. /* turn to interrupt service routine */
  371. isr_func(ir, param);
  372. }
  373. /* end of interrupt */
  374. rt_hw_interrupt_ack(ir);
  375. #endif
  376. }
  377. void rt_hw_trap_fiq(void)
  378. {
  379. void *param;
  380. int ir;
  381. rt_isr_handler_t isr_func;
  382. extern struct rt_irq_desc isr_table[];
  383. ir = rt_hw_interrupt_get_irq();
  384. /* get interrupt service routine */
  385. isr_func = isr_table[ir].handler;
  386. param = isr_table[ir].param;
  387. /* turn to interrupt service routine */
  388. isr_func(ir, param);
  389. /* end of interrupt */
  390. rt_hw_interrupt_ack(ir);
  391. }