trap.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. void rt_hw_trap_irq(void)
  124. {
  125. void *param;
  126. rt_isr_handler_t isr_func;
  127. extern struct rt_irq_desc isr_table[];
  128. // vectNum = RESERVED[31:13] | CPUID[12:10] | INTERRUPT_ID[9:0]
  129. // send ack and get ID source
  130. uint32_t vectNum = gic_read_irq_ack();
  131. // Check that INT_ID isn't 1023 or 1022 (spurious interrupt)
  132. if (vectNum & 0x0200)
  133. {
  134. gic_write_end_of_irq(vectNum); // send end of irq
  135. }
  136. else
  137. {
  138. // copy the local value to the global image of CPUID
  139. unsigned cpu = (vectNum >> 10) & 0x7;
  140. unsigned irq = vectNum & 0x1FF;
  141. /* skip warning */
  142. cpu = cpu;
  143. // Call the service routine stored in the handlers array. If there isn't
  144. // one for this IRQ, then call the default handler.
  145. /* get interrupt service routine */
  146. isr_func = isr_table[irq].handler;
  147. #ifdef RT_USING_INTERRUPT_INFO
  148. isr_table[irq].counter++;
  149. #endif
  150. if (isr_func)
  151. {
  152. /* Interrupt for myself. */
  153. param = isr_table[irq].param;
  154. /* turn to interrupt service routine */
  155. isr_func(irq, param);
  156. }
  157. // Signal the end of the irq.
  158. gic_write_end_of_irq(vectNum);
  159. }
  160. }
  161. void rt_hw_trap_fiq(void)
  162. {
  163. /* TODO */
  164. }