interrupt.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef __KERNEL_INTERRUPT_H
  2. #define __KERNEL_INTERRUPT_H
  3. #include "stdint.h"
  4. #include "global.h"
  5. #define IDT_DESC_CNT 0x21 // 目前支持的中断数
  6. #define PIC_M_CTRL 0x20 // 主片的控制端口是 0x20
  7. #define PIC_M_DATA 0x21 // 主片的数据端口是 0x21
  8. #define PIC_S_CTRL 0xA0 // 从片的控制端口是 0xA0
  9. #define PIC_S_DATA 0xA1 // 从片的数据端口是 0xA1
  10. typedef void *intr_handler;
  11. void itd_init(void);
  12. /* 定义中断的两种状态:
  13. * INTR_OFF值为 0,表示关中断
  14. * INTR_ON值为 1,表示开中断 */
  15. enum intr_status
  16. {
  17. INTR_OFF,
  18. INTR_ON
  19. };
  20. enum intr_status intr_get_status(void);
  21. enum intr_status intr_set_status(enum intr_status);
  22. enum intr_status intr_enable(void);
  23. enum intr_status intr_disable(void);
  24. /* 中断门描述符结构体 */
  25. // Example type_attributes values that people are likely to use (assuming DPL is 0):
  26. // 32-bit Interrupt Gate: 0x8E (p=1, dpl=0b00, type=0b1110 => type_attributes=0b1000_1110=0x8E)
  27. // 32-bit Trap Gate: 0x8F (p=1, dpl=0b00, type=0b1111 => type_attributes=1000_1111b=0x8F)
  28. // Task Gate: 0x85 (p=1, dpl=0b00, type=0b0101 => type_attributes=0b1000_0101=0x85)
  29. typedef struct
  30. {
  31. uint16_t offset_low; // 16位 偏移量的低 16 位 (0..15)
  32. uint16_t selector; // 选择器 acode segment selector in GDT or LDT
  33. uint8_t dcount; // 置 0
  34. uint8_t attribute; // 述符类型 (bit 7:0) gate type, dpl, and p fields
  35. uint16_t offset_high; // 16 位 偏移量的高 16 位 (16..31)
  36. } __attribute__((packed)) gate_desc;
  37. // 定义中断处理程序数组.在 kernel.S 中定义的intrXXentry只是中断处理程序的入口,最终调用的是ide_table中的处理程序
  38. extern intr_handler intr_entry_table[IDT_DESC_CNT];
  39. /// @brief 注册中断处理函数
  40. /// @param vector_no 中断号
  41. /// @param function 中断处理函数
  42. void register_handler(uint8_t vector_no, intr_handler function);
  43. enum intr_status intr_enable(void);
  44. enum intr_status intr_disable(void);
  45. #endif // __KERNEL_INTERRUPT_H