trap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #ifdef RT_USING_FINSH
  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. /**
  34. * When comes across an instruction which it cannot handle,
  35. * it takes the undefined instruction trap.
  36. *
  37. * @param regs system registers
  38. *
  39. * @note never invoke this function in application
  40. */
  41. void rt_hw_trap_undef(struct rt_hw_exp_stack *regs)
  42. {
  43. #ifdef RT_USING_FPU
  44. {
  45. uint32_t val;
  46. uint32_t addr;
  47. if (regs->cpsr & (1 << 5))
  48. {
  49. /* thumb mode */
  50. addr = regs->pc - 2;
  51. }
  52. else
  53. {
  54. addr = regs->pc - 4;
  55. }
  56. asm volatile ("vmrs %0, fpexc" : "=r"(val)::"memory");
  57. if (!(val & 0x40000000))
  58. {
  59. /* float ins */
  60. val = (1U << 30);
  61. asm volatile ("vmsr fpexc, %0"::"r"(val):"memory");
  62. regs->pc = addr;
  63. return;
  64. }
  65. }
  66. #endif
  67. rt_kprintf("undefined instruction:\n");
  68. rt_hw_show_register(regs);
  69. #ifdef RT_USING_FINSH
  70. list_thread();
  71. #endif
  72. rt_hw_cpu_shutdown();
  73. }
  74. /**
  75. * The software interrupt instruction (SWI) is used for entering
  76. * Supervisor mode, usually to request a particular supervisor
  77. * function.
  78. *
  79. * @param regs system registers
  80. *
  81. * @note never invoke this function in application
  82. */
  83. void rt_hw_trap_swi(struct rt_hw_exp_stack *regs)
  84. {
  85. rt_kprintf("software interrupt:\n");
  86. rt_hw_show_register(regs);
  87. #ifdef RT_USING_FINSH
  88. list_thread();
  89. #endif
  90. rt_hw_cpu_shutdown();
  91. }
  92. /**
  93. * An abort indicates that the current memory access cannot be completed,
  94. * which occurs during an instruction prefetch.
  95. *
  96. * @param regs system registers
  97. *
  98. * @note never invoke this function in application
  99. */
  100. void rt_hw_trap_pabt(struct rt_hw_exp_stack *regs)
  101. {
  102. rt_kprintf("prefetch abort:\n");
  103. rt_hw_show_register(regs);
  104. #ifdef RT_USING_FINSH
  105. list_thread();
  106. #endif
  107. rt_hw_cpu_shutdown();
  108. }
  109. /**
  110. * An abort indicates that the current memory access cannot be completed,
  111. * which occurs during a data access.
  112. *
  113. * @param regs system registers
  114. *
  115. * @note never invoke this function in application
  116. */
  117. void rt_hw_trap_dabt(struct rt_hw_exp_stack *regs)
  118. {
  119. rt_kprintf("data abort:");
  120. rt_hw_show_register(regs);
  121. #ifdef RT_USING_FINSH
  122. list_thread();
  123. #endif
  124. rt_hw_cpu_shutdown();
  125. }
  126. /**
  127. * Normally, system will never reach here
  128. *
  129. * @param regs system registers
  130. *
  131. * @note never invoke this function in application
  132. */
  133. void rt_hw_trap_resv(struct rt_hw_exp_stack *regs)
  134. {
  135. rt_kprintf("reserved trap:\n");
  136. rt_hw_show_register(regs);
  137. #ifdef RT_USING_FINSH
  138. list_thread();
  139. #endif
  140. rt_hw_cpu_shutdown();
  141. }
  142. void rt_hw_trap_irq(void)
  143. {
  144. void *param;
  145. int ir;
  146. rt_isr_handler_t isr_func;
  147. extern struct rt_irq_desc isr_table[];
  148. ir = rt_hw_interrupt_get_irq();
  149. if (ir == 1023)
  150. {
  151. /* Spurious interrupt */
  152. return;
  153. }
  154. /* get interrupt service routine */
  155. isr_func = isr_table[ir].handler;
  156. #ifdef RT_USING_INTERRUPT_INFO
  157. isr_table[ir].counter++;
  158. #endif
  159. if (isr_func)
  160. {
  161. /* Interrupt for myself. */
  162. param = isr_table[ir].param;
  163. /* turn to interrupt service routine */
  164. isr_func(ir, param);
  165. }
  166. /* end of interrupt */
  167. rt_hw_interrupt_ack(ir);
  168. }
  169. void rt_hw_trap_fiq(void)
  170. {
  171. void *param;
  172. int ir;
  173. rt_isr_handler_t isr_func;
  174. extern struct rt_irq_desc isr_table[];
  175. ir = rt_hw_interrupt_get_irq();
  176. /* get interrupt service routine */
  177. isr_func = isr_table[ir].handler;
  178. param = isr_table[ir].param;
  179. /* turn to interrupt service routine */
  180. isr_func(ir, param);
  181. /* end of interrupt */
  182. rt_hw_interrupt_ack(ir);
  183. }