trap.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) 2006-2024, 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 <armv8.h>
  11. #include <gicv3.h>
  12. #include <rtthread.h>
  13. #include <rthw.h>
  14. #include "interrupt.h"
  15. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  16. extern long list_thread(void);
  17. #endif
  18. /**
  19. * The software interrupt instruction (SWI) is used for entering
  20. * Supervisor mode, usually to request a particular supervisor
  21. * function.
  22. *
  23. * @param regs system registers
  24. *
  25. * @note never invoke this function in application
  26. */
  27. void rt_hw_trap_svc(struct rt_hw_exp_stack *regs)
  28. {
  29. rt_kprintf("software interrupt\n");
  30. rt_hw_show_register(regs);
  31. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  32. list_thread();
  33. #endif
  34. rt_hw_cpu_shutdown();
  35. }
  36. static rt_err_t (*rt_exception_hook)(void *context) = RT_NULL;
  37. /**
  38. * This function set the hook, which is invoked on fault exception handling.
  39. *
  40. * @param exception_handle the exception handling hook function.
  41. */
  42. void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context))
  43. {
  44. rt_exception_hook = exception_handle;
  45. }
  46. /**
  47. * this function will show registers of CPU
  48. *
  49. * @param regs the registers point
  50. */
  51. void rt_hw_show_register(struct rt_hw_exp_stack *regs)
  52. {
  53. rt_kprintf("Execption:\n");
  54. rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n", regs->r0, regs->r1, regs->r2, regs->r3);
  55. rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n", regs->r4, regs->r5, regs->r6, regs->r7);
  56. rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n", regs->r8, regs->r9, regs->r10);
  57. rt_kprintf("fp :0x%08x ip :0x%08x\n", regs->fp, regs->ip);
  58. rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n", regs->sp, regs->lr, regs->pc);
  59. rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
  60. if (rt_exception_hook != RT_NULL)
  61. {
  62. rt_err_t result;
  63. result = rt_exception_hook(regs);
  64. if (result == RT_EOK) return;
  65. }
  66. }
  67. void (*rt_trap_hook)(struct rt_hw_exp_stack *regs, const char *ex, unsigned int exception_type);
  68. /**
  69. * This function will set a hook function to trap handler.
  70. *
  71. * @param hook the hook function
  72. */
  73. void rt_hw_trap_set_hook(void (*hook)(struct rt_hw_exp_stack *regs, const char *ex, unsigned int exception_type))
  74. {
  75. rt_trap_hook = hook;
  76. }
  77. /**
  78. * When comes across an instruction which it cannot handle,
  79. * it takes the undefined instruction trap.
  80. *
  81. * @param regs system registers
  82. *
  83. * @note never invoke this function in application
  84. */
  85. void rt_hw_trap_undef(struct rt_hw_exp_stack *regs)
  86. {
  87. #ifdef RT_USING_FPU
  88. {
  89. uint32_t val;
  90. uint32_t addr;
  91. if (regs->cpsr & (1 << 5))
  92. {
  93. /* thumb mode */
  94. addr = regs->pc - 2;
  95. }
  96. else
  97. {
  98. addr = regs->pc - 4;
  99. }
  100. asm volatile ("vmrs %0, fpexc" : "=r"(val)::"memory");
  101. if (!(val & 0x40000000))
  102. {
  103. /* float ins */
  104. val = (1U << 30);
  105. asm volatile ("vmsr fpexc, %0"::"r"(val):"memory");
  106. regs->pc = addr;
  107. return;
  108. }
  109. }
  110. #endif
  111. if (rt_trap_hook == RT_NULL)
  112. {
  113. rt_kprintf("undefined instruction:\n");
  114. rt_hw_show_register(regs);
  115. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  116. list_thread();
  117. #endif
  118. rt_hw_cpu_shutdown();
  119. }
  120. else
  121. {
  122. rt_trap_hook(regs, "undefined instruction", UND_EXCEPTION);
  123. }
  124. }
  125. /**
  126. * The software interrupt instruction (SWI) is used for entering
  127. * Supervisor mode, usually to request a particular supervisor
  128. * function.
  129. *
  130. * @param regs system registers
  131. *
  132. * @note never invoke this function in application
  133. */
  134. void rt_hw_trap_swi(struct rt_hw_exp_stack *regs)
  135. {
  136. if (rt_trap_hook == RT_NULL)
  137. {
  138. rt_kprintf("software interrupt:\n");
  139. rt_hw_show_register(regs);
  140. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  141. list_thread();
  142. #endif
  143. rt_hw_cpu_shutdown();
  144. }
  145. else
  146. {
  147. rt_trap_hook(regs, "software instruction", SWI_EXCEPTION);
  148. }
  149. }
  150. /**
  151. * An abort indicates that the current memory access cannot be completed,
  152. * which occurs during an instruction prefetch.
  153. *
  154. * @param regs system registers
  155. *
  156. * @note never invoke this function in application
  157. */
  158. void rt_hw_trap_pabt(struct rt_hw_exp_stack *regs)
  159. {
  160. if (rt_trap_hook == RT_NULL)
  161. {
  162. rt_kprintf("prefetch abort:\n");
  163. rt_hw_show_register(regs);
  164. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  165. list_thread();
  166. #endif
  167. rt_hw_cpu_shutdown();
  168. }
  169. else
  170. {
  171. rt_trap_hook(regs, "prefetch abort", PABT_EXCEPTION);
  172. }
  173. }
  174. /**
  175. * An abort indicates that the current memory access cannot be completed,
  176. * which occurs during a data access.
  177. *
  178. * @param regs system registers
  179. *
  180. * @note never invoke this function in application
  181. */
  182. void rt_hw_trap_dabt(struct rt_hw_exp_stack *regs)
  183. {
  184. if (rt_trap_hook == RT_NULL)
  185. {
  186. rt_kprintf("data abort:");
  187. rt_hw_show_register(regs);
  188. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  189. list_thread();
  190. #endif
  191. rt_hw_cpu_shutdown();
  192. }
  193. else
  194. {
  195. rt_trap_hook(regs, "data abort", DABT_EXCEPTION);
  196. }
  197. }
  198. /**
  199. * Normally, system will never reach here
  200. *
  201. * @param regs system registers
  202. *
  203. * @note never invoke this function in application
  204. */
  205. void rt_hw_trap_resv(struct rt_hw_exp_stack *regs)
  206. {
  207. if (rt_trap_hook == RT_NULL)
  208. {
  209. rt_kprintf("reserved trap:\n");
  210. rt_hw_show_register(regs);
  211. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  212. list_thread();
  213. #endif
  214. rt_hw_cpu_shutdown();
  215. }
  216. else
  217. {
  218. rt_trap_hook(regs, "reserved trap", RESV_EXCEPTION);
  219. }
  220. }
  221. void rt_hw_trap_irq(void)
  222. {
  223. void *param;
  224. int int_ack;
  225. int ir;
  226. volatile rt_isr_handler_t isr_func;
  227. extern struct rt_irq_desc isr_table[];
  228. int_ack = rt_hw_interrupt_get_irq();
  229. ir = int_ack & GIC_ACK_INTID_MASK;
  230. if (ir == 1023)
  231. {
  232. /* Spurious interrupt */
  233. return;
  234. }
  235. #ifdef SOC_SERIES_R9A07G0
  236. bsp_common_interrupt_handler((uint32_t)ir);
  237. #else
  238. /* get interrupt service routine */
  239. isr_func = isr_table[ir].handler;
  240. #ifdef RT_USING_INTERRUPT_INFO
  241. isr_table[ir].counter++;
  242. #endif
  243. if (isr_func)
  244. {
  245. /* Interrupt for myself. */
  246. param = isr_table[ir].param;
  247. /* turn to interrupt service routine */
  248. isr_func(ir, param);
  249. }
  250. #endif
  251. /* end of interrupt */
  252. rt_hw_interrupt_ack(int_ack);
  253. }
  254. void rt_hw_trap_fiq(void)
  255. {
  256. void *param;
  257. int int_ack;
  258. int ir;
  259. volatile rt_isr_handler_t isr_func;
  260. extern struct rt_irq_desc isr_table[];
  261. int_ack = rt_hw_interrupt_get_irq();
  262. ir = int_ack & GIC_ACK_INTID_MASK;
  263. if (ir == 1023)
  264. {
  265. /* Spurious interrupt */
  266. return;
  267. }
  268. #ifdef SOC_SERIES_R9A07G0
  269. bsp_common_interrupt_handler((uint32_t)ir);
  270. #else
  271. /* get interrupt service routine */
  272. isr_func = isr_table[ir].handler;
  273. #ifdef RT_USING_INTERRUPT_INFO
  274. isr_table[ir].counter++;
  275. #endif
  276. if (isr_func)
  277. {
  278. /* Interrupt for myself. */
  279. param = isr_table[ir].param;
  280. /* turn to interrupt service routine */
  281. isr_func(ir, param);
  282. }
  283. #endif
  284. /* end of interrupt */
  285. rt_hw_interrupt_ack(int_ack);
  286. }