trap.c 4.6 KB

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