trap.c 11 KB

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