trap.c 6.4 KB

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