trap.c 7.6 KB

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