trap.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * 2018-02-08 RT-Thread the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #define INT_IRQ 0x00
  13. #define INT_FIQ 0x01
  14. extern struct rt_thread *rt_current_thread;
  15. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  16. extern long list_thread(void);
  17. #endif
  18. struct rt_hw_register
  19. {
  20. rt_uint32_t r0;
  21. rt_uint32_t r1;
  22. rt_uint32_t r2;
  23. rt_uint32_t r3;
  24. rt_uint32_t r4;
  25. rt_uint32_t r5;
  26. rt_uint32_t r6;
  27. rt_uint32_t r7;
  28. rt_uint32_t r8;
  29. rt_uint32_t r9;
  30. rt_uint32_t r10;
  31. rt_uint32_t fp;
  32. rt_uint32_t ip;
  33. rt_uint32_t sp;
  34. rt_uint32_t lr;
  35. rt_uint32_t pc;
  36. rt_uint32_t cpsr;
  37. rt_uint32_t ORIG_r0;
  38. };
  39. static rt_err_t (*rt_exception_hook)(void *context) = RT_NULL;
  40. void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context))
  41. {
  42. rt_exception_hook = exception_handle;
  43. }
  44. /**
  45. * this function will show registers of CPU
  46. *
  47. * @param regs the registers point
  48. */
  49. void rt_hw_show_register(struct rt_hw_register *regs)
  50. {
  51. rt_kprintf("Execption:\n");
  52. rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n",
  53. regs->r0, regs->r1, regs->r2, regs->r3);
  54. rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n",
  55. regs->r4, regs->r5, regs->r6, regs->r7);
  56. rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n",
  57. regs->r8, regs->r9, regs->r10);
  58. rt_kprintf("fp :0x%08x ip :0x%08x\n",
  59. regs->fp, regs->ip);
  60. rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n",
  61. regs->sp, regs->lr, regs->pc);
  62. rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
  63. }
  64. /**
  65. * When ARM7TDMI comes across an instruction which it cannot handle,
  66. * it takes the undefined instruction trap.
  67. *
  68. * @param regs system registers
  69. *
  70. * @note never invoke this function in application
  71. */
  72. void rt_hw_trap_udef(struct rt_hw_register *regs)
  73. {
  74. if (rt_exception_hook != RT_NULL)
  75. {
  76. rt_err_t result;
  77. result = rt_exception_hook(regs);
  78. if (result == RT_EOK) return;
  79. }
  80. rt_hw_show_register(regs);
  81. rt_kprintf("undefined instruction\n");
  82. rt_kprintf("thread - %s stack:\n", rt_current_thread->parent.name);
  83. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  84. list_thread();
  85. #endif
  86. rt_hw_cpu_shutdown();
  87. }
  88. /**
  89. * The software interrupt instruction (SWI) is used for entering
  90. * Supervisor mode, usually to request a particular supervisor
  91. * function.
  92. *
  93. * @param regs system registers
  94. *
  95. * @note never invoke this function in application
  96. */
  97. void rt_hw_trap_swi(struct rt_hw_register *regs)
  98. {
  99. if (rt_exception_hook != RT_NULL)
  100. {
  101. rt_err_t result;
  102. result = rt_exception_hook(regs);
  103. if (result == RT_EOK) return;
  104. }
  105. rt_hw_show_register(regs);
  106. rt_kprintf("software interrupt\n");
  107. rt_hw_cpu_shutdown();
  108. }
  109. /**
  110. * An abort indicates that the current memory access cannot be completed,
  111. * which occurs during an instruction prefetch.
  112. *
  113. * @param regs system registers
  114. *
  115. * @note never invoke this function in application
  116. */
  117. void rt_hw_trap_pabt(struct rt_hw_register *regs)
  118. {
  119. if (rt_exception_hook != RT_NULL)
  120. {
  121. rt_err_t result;
  122. result = rt_exception_hook(regs);
  123. if (result == RT_EOK) return;
  124. }
  125. rt_hw_show_register(regs);
  126. rt_kprintf("prefetch abort\n");
  127. rt_kprintf("thread - %s stack:\n", RT_NAME_MAX, rt_current_thread->parent.name);
  128. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  129. list_thread();
  130. #endif
  131. rt_hw_cpu_shutdown();
  132. }
  133. /**
  134. * An abort indicates that the current memory access cannot be completed,
  135. * which occurs during a data access.
  136. *
  137. * @param regs system registers
  138. *
  139. * @note never invoke this function in application
  140. */
  141. void rt_hw_trap_dabt(struct rt_hw_register *regs)
  142. {
  143. if (rt_exception_hook != RT_NULL)
  144. {
  145. rt_err_t result;
  146. result = rt_exception_hook(regs);
  147. if (result == RT_EOK) return;
  148. }
  149. rt_hw_show_register(regs);
  150. rt_kprintf("data abort\n");
  151. rt_kprintf("thread - %s stack:\n", RT_NAME_MAX, rt_current_thread->parent.name);
  152. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  153. list_thread();
  154. #endif
  155. rt_hw_cpu_shutdown();
  156. }
  157. /**
  158. * Normally, system will never reach here
  159. *
  160. * @param regs system registers
  161. *
  162. * @note never invoke this function in application
  163. */
  164. void rt_hw_trap_resv(struct rt_hw_register *regs)
  165. {
  166. if (rt_exception_hook != RT_NULL)
  167. {
  168. rt_err_t result;
  169. result = rt_exception_hook(regs);
  170. if (result == RT_EOK) return;
  171. }
  172. rt_kprintf("not used\n");
  173. rt_hw_show_register(regs);
  174. rt_hw_cpu_shutdown();
  175. }
  176. extern void rt_interrupt_dispatch(void);
  177. void rt_hw_trap_irq(void)
  178. {
  179. rt_interrupt_dispatch();
  180. }
  181. void rt_hw_trap_fiq(void)
  182. {
  183. rt_interrupt_dispatch();
  184. }