trap.c 4.5 KB

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