trap.c 2.1 KB

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