trap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "gic.h"
  15. extern struct rt_thread *rt_current_thread;
  16. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  17. extern long list_thread(void);
  18. #endif
  19. /**
  20. * this function will show registers of CPU
  21. *
  22. * @param regs the registers point
  23. */
  24. void rt_hw_show_register(struct rt_hw_exp_stack *regs)
  25. {
  26. rt_kprintf("Execption:\n");
  27. rt_kprintf("r00:0x%08x r01:0x%08x r02:0x%08x r03:0x%08x\n", regs->r0, regs->r1, regs->r2, regs->r3);
  28. rt_kprintf("r04:0x%08x r05:0x%08x r06:0x%08x r07:0x%08x\n", regs->r4, regs->r5, regs->r6, regs->r7);
  29. rt_kprintf("r08:0x%08x r09:0x%08x r10:0x%08x\n", regs->r8, regs->r9, regs->r10);
  30. rt_kprintf("fp :0x%08x ip :0x%08x\n", regs->fp, regs->ip);
  31. rt_kprintf("sp :0x%08x lr :0x%08x pc :0x%08x\n", regs->sp, regs->lr, regs->pc);
  32. rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
  33. }
  34. /**
  35. * When comes across an instruction which it cannot handle,
  36. * it takes the undefined instruction trap.
  37. *
  38. * @param regs system registers
  39. *
  40. * @note never invoke this function in application
  41. */
  42. void rt_hw_trap_undef(struct rt_hw_exp_stack *regs)
  43. {
  44. rt_kprintf("undefined instruction:\n");
  45. rt_hw_show_register(regs);
  46. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  47. list_thread();
  48. #endif
  49. rt_hw_cpu_shutdown();
  50. }
  51. /**
  52. * The software interrupt instruction (SWI) is used for entering
  53. * Supervisor mode, usually to request a particular supervisor
  54. * function.
  55. *
  56. * @param regs system registers
  57. *
  58. * @note never invoke this function in application
  59. */
  60. void rt_hw_trap_swi(struct rt_hw_exp_stack *regs)
  61. {
  62. rt_kprintf("software interrupt:\n");
  63. rt_hw_show_register(regs);
  64. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  65. list_thread();
  66. #endif
  67. rt_hw_cpu_shutdown();
  68. }
  69. /**
  70. * An abort indicates that the current memory access cannot be completed,
  71. * which occurs during an instruction prefetch.
  72. *
  73. * @param regs system registers
  74. *
  75. * @note never invoke this function in application
  76. */
  77. void rt_hw_trap_pabt(struct rt_hw_exp_stack *regs)
  78. {
  79. rt_kprintf("prefetch abort:\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. /**
  87. * An abort indicates that the current memory access cannot be completed,
  88. * which occurs during a data access.
  89. *
  90. * @param regs system registers
  91. *
  92. * @note never invoke this function in application
  93. */
  94. void rt_hw_trap_dabt(struct rt_hw_exp_stack *regs)
  95. {
  96. rt_kprintf("data abort:");
  97. rt_hw_show_register(regs);
  98. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  99. list_thread();
  100. #endif
  101. rt_hw_cpu_shutdown();
  102. }
  103. /**
  104. * Normally, system will never reach here
  105. *
  106. * @param regs system registers
  107. *
  108. * @note never invoke this function in application
  109. */
  110. void rt_hw_trap_resv(struct rt_hw_exp_stack *regs)
  111. {
  112. rt_kprintf("reserved trap:\n");
  113. rt_hw_show_register(regs);
  114. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  115. list_thread();
  116. #endif
  117. rt_hw_cpu_shutdown();
  118. }
  119. #define GIC_ACK_INTID_MASK 0x000003ff
  120. void rt_hw_trap_irq(void)
  121. {
  122. void *param;
  123. unsigned long ir;
  124. unsigned long fullir;
  125. rt_isr_handler_t isr_func;
  126. extern struct rt_irq_desc isr_table[];
  127. fullir = arm_gic_get_active_irq(0);
  128. ir = fullir & GIC_ACK_INTID_MASK;
  129. if (ir == 1023)
  130. {
  131. /* Spurious interrupt */
  132. return;
  133. }
  134. /* get interrupt service routine */
  135. isr_func = isr_table[ir].handler;
  136. #ifdef RT_USING_INTERRUPT_INFO
  137. isr_table[ir].counter++;
  138. #endif
  139. if (isr_func)
  140. {
  141. /* Interrupt for myself. */
  142. param = isr_table[ir].param;
  143. /* turn to interrupt service routine */
  144. isr_func(ir, param);
  145. }
  146. /* end of interrupt */
  147. arm_gic_ack(0, fullir);
  148. }
  149. void rt_hw_trap_fiq(void)
  150. {
  151. void *param;
  152. unsigned long ir;
  153. unsigned long fullir;
  154. rt_isr_handler_t isr_func;
  155. extern struct rt_irq_desc isr_table[];
  156. fullir = arm_gic_get_active_irq(0);
  157. ir = fullir & GIC_ACK_INTID_MASK;
  158. /* get interrupt service routine */
  159. isr_func = isr_table[ir].handler;
  160. param = isr_table[ir].param;
  161. /* turn to interrupt service routine */
  162. isr_func(ir, param);
  163. /* end of interrupt */
  164. arm_gic_ack(0, fullir);
  165. }