trap.c 4.4 KB

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