trap.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * File : trap.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development 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://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. */
  13. #include <rtthread.h>
  14. #include <rthw.h>
  15. #include <bsp.h>
  16. /* Interrupt descriptor table. (Must be built at run time because
  17. * shifted function addresses can't be represented in relocation records.)
  18. */
  19. struct Gatedesc idt[256] = { {0}, };
  20. struct Pseudodesc idt_pd =
  21. {
  22. 0, sizeof(idt) - 1, (unsigned long) idt,
  23. };
  24. /* exception and interrupt handler table */
  25. extern rt_isr_handler_t isr_table[];
  26. extern rt_isr_handler_t trap_func[];
  27. extern rt_isr_handler_t hdinterrupt_func[];
  28. /**
  29. * @addtogroup I386
  30. */
  31. /*@{*/
  32. /**
  33. * this function initializes the interrupt descript table
  34. *
  35. */
  36. void rt_hw_idt_init(void)
  37. {
  38. extern void Xdefault;
  39. int i, j, func;
  40. for(i=0; i<MAX_HANDLERS; i++)
  41. {
  42. isr_table[i] = rt_hw_interrupt_handle;
  43. }
  44. // install a default handler
  45. for (i = 0; i < sizeof(idt)/sizeof(idt[0]); i++)
  46. SETGATE(idt[i], 0, GD_KT, &Xdefault, 0);
  47. /*install trap handler*/
  48. for(i = 0; i < 16; i++)
  49. {
  50. func = (int)trap_func[i];
  51. SETGATE(idt[i], 0, GD_KT, func, 0);
  52. }
  53. func = (int)trap_func[3];
  54. SETGATE(idt[3], 0, GD_KT, func, 3);
  55. i = 0;
  56. /*install exteral interrupt handler*/
  57. for(j = IRQ_OFFSET; j < IRQ_OFFSET + MAX_HANDLERS; j++)
  58. {
  59. func = (int)hdinterrupt_func[i];
  60. SETGATE(idt[j], 0, GD_KT, func, 0);
  61. i++;
  62. }
  63. // Load the IDT
  64. asm volatile("lidt idt_pd + 2");
  65. }
  66. /**
  67. * this function will deal with all kinds of kernel trap
  68. *
  69. *@param trapno the trap number
  70. *
  71. */
  72. void rt_hw_trap_irq(int trapno)
  73. {
  74. switch(trapno)
  75. {
  76. case T_DIVIDE:
  77. rt_kprintf("Divide error interrupt\n");
  78. RT_ASSERT(0);
  79. case T_PGFLT:
  80. rt_kprintf("Page fault interrupt\n");
  81. RT_ASSERT(0);
  82. case T_GPFLT:
  83. rt_kprintf("General protection interrupt\n");
  84. RT_ASSERT(0);
  85. case T_DEFAULT:
  86. rt_hw_interrupt_handle(T_DEFAULT);
  87. return;
  88. }
  89. /*kernel bug if run here*/
  90. RT_ASSERT(0);
  91. }
  92. /*@}*/