trap.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * File : trap.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-02-08 RT-Thread the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #define INT_IRQ 0x00
  27. #define INT_FIQ 0x01
  28. extern struct rt_thread *rt_current_thread;
  29. #ifdef RT_USING_FINSH
  30. extern long list_thread(void);
  31. #endif
  32. struct rt_hw_register
  33. {
  34. rt_uint32_t r0;
  35. rt_uint32_t r1;
  36. rt_uint32_t r2;
  37. rt_uint32_t r3;
  38. rt_uint32_t r4;
  39. rt_uint32_t r5;
  40. rt_uint32_t r6;
  41. rt_uint32_t r7;
  42. rt_uint32_t r8;
  43. rt_uint32_t r9;
  44. rt_uint32_t r10;
  45. rt_uint32_t fp;
  46. rt_uint32_t ip;
  47. rt_uint32_t sp;
  48. rt_uint32_t lr;
  49. rt_uint32_t pc;
  50. rt_uint32_t cpsr;
  51. rt_uint32_t ORIG_r0;
  52. };
  53. static rt_err_t (*rt_exception_hook)(void *context) = RT_NULL;
  54. void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context))
  55. {
  56. rt_exception_hook = exception_handle;
  57. }
  58. /**
  59. * this function will show registers of CPU
  60. *
  61. * @param regs the registers point
  62. */
  63. void rt_hw_show_register(struct rt_hw_register *regs)
  64. {
  65. rt_kprintf("Execption:\n");
  66. rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n",
  67. regs->r0, regs->r1, regs->r2, regs->r3);
  68. rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n",
  69. regs->r4, regs->r5, regs->r6, regs->r7);
  70. rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n",
  71. regs->r8, regs->r9, regs->r10);
  72. rt_kprintf("fp :0x%08x ip :0x%08x\n",
  73. regs->fp, regs->ip);
  74. rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n",
  75. regs->sp, regs->lr, regs->pc);
  76. rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
  77. }
  78. /**
  79. * When ARM7TDMI 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_udef(struct rt_hw_register *regs)
  87. {
  88. if (rt_exception_hook != RT_NULL)
  89. {
  90. rt_err_t result;
  91. result = rt_exception_hook(regs);
  92. if (result == RT_EOK) return;
  93. }
  94. rt_hw_show_register(regs);
  95. rt_kprintf("undefined instruction\n");
  96. rt_kprintf("thread - %s stack:\n", rt_current_thread->name);
  97. #ifdef RT_USING_FINSH
  98. list_thread();
  99. #endif
  100. rt_hw_cpu_shutdown();
  101. }
  102. /**
  103. * The software interrupt instruction (SWI) is used for entering
  104. * Supervisor mode, usually to request a particular supervisor
  105. * function.
  106. *
  107. * @param regs system registers
  108. *
  109. * @note never invoke this function in application
  110. */
  111. void rt_hw_trap_swi(struct rt_hw_register *regs)
  112. {
  113. if (rt_exception_hook != RT_NULL)
  114. {
  115. rt_err_t result;
  116. result = rt_exception_hook(regs);
  117. if (result == RT_EOK) return;
  118. }
  119. rt_hw_show_register(regs);
  120. rt_kprintf("software interrupt\n");
  121. rt_hw_cpu_shutdown();
  122. }
  123. /**
  124. * An abort indicates that the current memory access cannot be completed,
  125. * which occurs during an instruction prefetch.
  126. *
  127. * @param regs system registers
  128. *
  129. * @note never invoke this function in application
  130. */
  131. void rt_hw_trap_pabt(struct rt_hw_register *regs)
  132. {
  133. if (rt_exception_hook != RT_NULL)
  134. {
  135. rt_err_t result;
  136. result = rt_exception_hook(regs);
  137. if (result == RT_EOK) return;
  138. }
  139. rt_hw_show_register(regs);
  140. rt_kprintf("prefetch abort\n");
  141. rt_kprintf("thread - %s stack:\n", RT_NAME_MAX, rt_current_thread->name);
  142. #ifdef RT_USING_FINSH
  143. list_thread();
  144. #endif
  145. rt_hw_cpu_shutdown();
  146. }
  147. /**
  148. * An abort indicates that the current memory access cannot be completed,
  149. * which occurs during a data access.
  150. *
  151. * @param regs system registers
  152. *
  153. * @note never invoke this function in application
  154. */
  155. void rt_hw_trap_dabt(struct rt_hw_register *regs)
  156. {
  157. if (rt_exception_hook != RT_NULL)
  158. {
  159. rt_err_t result;
  160. result = rt_exception_hook(regs);
  161. if (result == RT_EOK) return;
  162. }
  163. rt_hw_show_register(regs);
  164. rt_kprintf("data abort\n");
  165. rt_kprintf("thread - %s stack:\n", RT_NAME_MAX, rt_current_thread->name);
  166. #ifdef RT_USING_FINSH
  167. list_thread();
  168. #endif
  169. rt_hw_cpu_shutdown();
  170. }
  171. /**
  172. * Normally, system will never reach here
  173. *
  174. * @param regs system registers
  175. *
  176. * @note never invoke this function in application
  177. */
  178. void rt_hw_trap_resv(struct rt_hw_register *regs)
  179. {
  180. if (rt_exception_hook != RT_NULL)
  181. {
  182. rt_err_t result;
  183. result = rt_exception_hook(regs);
  184. if (result == RT_EOK) return;
  185. }
  186. rt_kprintf("not used\n");
  187. rt_hw_show_register(regs);
  188. rt_hw_cpu_shutdown();
  189. }
  190. extern void rt_interrupt_dispatch(void);
  191. void rt_hw_trap_irq(void)
  192. {
  193. rt_interrupt_dispatch();
  194. }
  195. void rt_hw_trap_fiq(void)
  196. {
  197. rt_interrupt_dispatch();
  198. }