interrupt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "interrupt.h"
  2. #include "stdint.h"
  3. #include "global.h"
  4. #include "../lib/kernel/print.h"
  5. #include "../lib/kernel/io.h"
  6. char *intr_name[IDT_DESC_CNT]; // 用于保存异常的名字
  7. intr_handler idt_table[IDT_DESC_CNT]; // 中断处理函数数组
  8. #define EFLAGS_IF 0x200 // eflags寄存器中的if位为1
  9. #define GET_EFLAGS(EFLAG_VAR) asm volatile("pushfl; popl %0" : "=g"(EFLAG_VAR))
  10. static gate_desc idt[IDT_DESC_CNT]; // 中断描述符表,本质上就是个中断门描述符数组
  11. /*创建中断门描述符*/
  12. static void make_idt_desc(gate_desc *p_gdesc, uint8_t attr, intr_handler function)
  13. {
  14. p_gdesc->offset_low = (uint32_t)function & 0x0000FFFF;
  15. p_gdesc->selector = SELECTOR_K_CODE;
  16. p_gdesc->dcount = 0;
  17. p_gdesc->attribute = attr;
  18. p_gdesc->offset_high = ((uint32_t)function & 0xFFFF0000) >> 16;
  19. }
  20. /* 初始化中断描述符表*/
  21. static void itd_desc_init(void)
  22. {
  23. int i;
  24. for (i = 0; i < IDT_DESC_CNT; i++)
  25. {
  26. make_idt_desc(&idt[i], IDT_DESC_ATTR_DPL0, intr_entry_table[i]);
  27. }
  28. put_str(" idt_desc_init done\n");
  29. }
  30. /* 初始化可编程中断控制器 8259A */
  31. static void pic_init(void)
  32. {
  33. /* 初始化主片*/
  34. outb(PIC_M_CTRL, 0x11); // ICW1: 边沿触发,级联8259, 需要 ICW4
  35. outb(PIC_M_DATA, 0x20); // ICW2: 起始中断向量号 0x20, 也就是 IRQ[0~7] 为 0x20~0x27
  36. outb(PIC_M_DATA, 0x04); // ICW3: IR2 接从片
  37. outb(PIC_M_DATA, 0x01); // ICW4: 8086 模式, 正常 EOI
  38. /* 初始化从片 */
  39. outb(PIC_S_CTRL, 0x11); // ICW1: 边沿触发,级联8259, 需要 ICW4
  40. outb(PIC_S_DATA, 0x28); // ICW2: 起始中断向量号 0x28, 也就是 IRQ[8~15] 以 0x28~0x2F
  41. outb(PIC_S_DATA, 0x02); // ICW3: 设置从片连接到主片的 IR2 引脚
  42. outb(PIC_S_DATA, 0x01); // ICW4: 8086 模式, 正常 EOI
  43. /* 打开主片上 IR0, 也就是目前只接受时钟产生的中断*/
  44. outb(PIC_M_DATA, 0xfe);
  45. outb(PIC_S_DATA, 0xff);
  46. put_str(" pic_init done\n");
  47. }
  48. static void general_intr_handler(uint8_t vec_nr)
  49. {
  50. if (vec_nr == 0x27 || vec_nr == 0x2f)
  51. {
  52. // IRQ7 和 IRQ15 会产生伪中断,无需处理
  53. // 0x2f 是从片8259A上的最后一个 IRQ 引脚,保留项
  54. return;
  55. }
  56. put_str("int vector: 0x");
  57. put_int(vec_nr);
  58. put_char('\n');
  59. }
  60. static void exception_init(void)
  61. {
  62. int i;
  63. for (i = 0; i < IDT_DESC_CNT; i++)
  64. {
  65. /*idt_table 数组中的函数是进入中断后根据中断向量号调用的,见 kernel/kernel.S 的 call [itd_table + %1*4]*/
  66. idt_table[i] = general_intr_handler; // 默认为 general_intr_handler
  67. intr_name[i] = "unknown";
  68. }
  69. intr_name[0] = "#DE Divide Error";
  70. intr_name[1] = "#DB Debug Exception";
  71. intr_name[2] = "NMI Interrupt";
  72. intr_name[3] = "#BP Breakpoint Exception";
  73. intr_name[4] = "#OF Overflow Exception";
  74. intr_name[5] = "#BR BOUND Range Exceeded Exception";
  75. intr_name[6] = "#UD Invalid Opcode Exception";
  76. intr_name[7] = "#NM Device Not Available Exception";
  77. intr_name[8] = "#DF Double Fault Exception";
  78. intr_name[9] = "Coprocessor Segment Overrun";
  79. intr_name[10] = "#TS Invalid TSS Exception";
  80. intr_name[11] = "#NP Segment Not Present";
  81. intr_name[12] = "#SS Stack Fault Exception";
  82. intr_name[13] = "#GP General Protection Exception";
  83. intr_name[14] = "#PF Page-Fault Exception";
  84. // intr_name[15] 第 15 项是 intel 保留项,未使用
  85. intr_name[16] = "#MF x87 FPU Floating-Point Error";
  86. intr_name[17] = "#AC Alignment Check Exception";
  87. intr_name[18] = "#MC Machine-Check Exception";
  88. intr_name[19] = "#XF SIMD Floating-Point Exception";
  89. put_str(" exception_init done\n");
  90. }
  91. /* 中断初始化主函数 */
  92. void itd_init(void)
  93. {
  94. put_str("init_idt start\n");
  95. itd_desc_init(); // 初始化中断描述符表
  96. exception_init(); // 完成一般中断处理函数注册及异常名称注册
  97. pic_init(); // 初始化可编程中断控制器8259A
  98. /* 加载 idt */
  99. uint64_t idt_operand = ((sizeof(idt) - 1) | ((uint64_t)((uint32_t)idt << 16)));
  100. asm volatile("lidt %0" ::"m"(idt_operand));
  101. put_str("itd_init done\n");
  102. }
  103. enum intr_status intr_get_status(void)
  104. {
  105. uint32_t eflags = 0;
  106. GET_EFLAGS(eflags);
  107. return (EFLAGS_IF & eflags) ? INTR_ON : INTR_OFF;
  108. }
  109. enum intr_status intr_set_status(enum intr_status status)
  110. {
  111. return status & INTR_ON ? intr_enable() : intr_disable();
  112. }
  113. enum intr_status intr_enable(void)
  114. {
  115. enum intr_status old_status;
  116. if (INTR_ON == intr_get_status())
  117. {
  118. old_status = INTR_ON;
  119. return old_status;
  120. }
  121. else
  122. {
  123. old_status = INTR_OFF;
  124. asm volatile("sti"); // 开中断, sti 指令将IF位置1
  125. return old_status;
  126. }
  127. }
  128. enum intr_status intr_disable(void)
  129. {
  130. enum intr_status old_status;
  131. if (INTR_ON == intr_get_status())
  132. {
  133. old_status = INTR_ON;
  134. asm volatile("cli" : : : "memory"); // 关中断, cli 指令将IF位置0
  135. return old_status;
  136. }
  137. else
  138. {
  139. old_status = INTR_OFF;
  140. return old_status;
  141. }
  142. }