trap.c 5.9 KB

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