interrupt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #define IDT_DESC_CNT 0x30 // 目前支持的中断数
  7. #define PIC_M_CTRL 0x20 // 主片的控制端口是 0x20
  8. #define PIC_M_DATA 0x21 // 主片的数据端口是 0x21
  9. #define PIC_S_CTRL 0xA0 // 从片的控制端口是 0xA0
  10. #define PIC_S_DATA 0xA1 // 从片的数据端口是 0xA1
  11. #define EFLAGS_IF 0x200 // eflags寄存器中的if位为1
  12. #define GET_EFLAGS(EFLAG_VAR) asm volatile("pushfl; popl %0" : "=g"(EFLAG_VAR))
  13. /* 中断门描述符结构体 */
  14. // Example type_attributes values that people are likely to use (assuming DPL is 0):
  15. // 32-bit Interrupt Gate: 0x8E (p=1, dpl=0b00, type=0b1110 => type_attributes=0b1000_1110=0x8E)
  16. // 32-bit Trap Gate: 0x8F (p=1, dpl=0b00, type=0b1111 => type_attributes=1000_1111b=0x8F)
  17. // Task Gate: 0x85 (p=1, dpl=0b00, type=0b0101 => type_attributes=0b1000_0101=0x85)
  18. typedef struct
  19. {
  20. uint16_t offset_low; // 16位 偏移量的低 16 位 (0..15)
  21. uint16_t selector; // 选择器 acode segment selector in GDT or LDT
  22. uint8_t dcount; // 置 0
  23. uint8_t attribute; // 述符类型 (bit 7:0) gate type, dpl, and p fields
  24. uint16_t offset_high; // 16 位 偏移量的高 16 位 (16..31)
  25. } __attribute__((packed)) gate_desc;
  26. char *intr_name[IDT_DESC_CNT]; // 用于保存异常的名字
  27. /****************************** 定义中断数组 **********************
  28. * 在 kernel.S 中定义的 intrXXentry 只是中断处理程序的入口,
  29. * 最终调用的 idt_table 中的处理程序*/
  30. intr_handler idt_table[IDT_DESC_CNT]; // 中断处理函数数组
  31. /***************************************************************/
  32. static gate_desc idt[IDT_DESC_CNT]; // 中断描述符表,本质上就是个中断门描述符数组
  33. extern intr_handler intr_entry_table[IDT_DESC_CNT]; // 声明引用定义在 kernel.S 中的中断处理函数入口数组
  34. /*创建中断门描述符*/
  35. static void make_idt_desc(gate_desc *p_gdesc, uint8_t attr, intr_handler function)
  36. {
  37. p_gdesc->offset_low = (uint32_t)((uintptr_t)function & 0x0000FFFF);
  38. p_gdesc->selector = SELECTOR_K_CODE;
  39. p_gdesc->dcount = 0;
  40. p_gdesc->attribute = attr;
  41. p_gdesc->offset_high = (uint32_t)(((uintptr_t)function & 0xFFFF0000) >> 16);
  42. }
  43. /* 初始化中断描述符表*/
  44. static void itd_desc_init(void)
  45. {
  46. int i;
  47. for (i = 0; i < IDT_DESC_CNT; i++)
  48. {
  49. make_idt_desc(&idt[i], IDT_DESC_ATTR_DPL0, intr_entry_table[i]);
  50. }
  51. put_str(" idt_desc_init done\n");
  52. }
  53. /* 初始化可编程中断控制器 8259A */
  54. static void pic_init(void)
  55. {
  56. /* 初始化主片*/
  57. outb(PIC_M_CTRL, 0x11); // ICW1: 边沿触发,级联8259, 需要 ICW4
  58. outb(PIC_M_DATA, 0x20); // ICW2: 起始中断向量号 0x20, 也就是 IRQ[0~7] 为 0x20~0x27
  59. outb(PIC_M_DATA, 0x04); // ICW3: IR2 接从片
  60. outb(PIC_M_DATA, 0x01); // ICW4: 8086 模式, 正常 EOI
  61. /* 初始化从片 */
  62. outb(PIC_S_CTRL, 0x11); // ICW1: 边沿触发,级联8259, 需要 ICW4
  63. outb(PIC_S_DATA, 0x28); // ICW2: 起始中断向量号 0x28, 也就是 IRQ[8~15] 以 0x28~0x2F
  64. outb(PIC_S_DATA, 0x02); // ICW3: 设置从片连接到主片的 IR2 引脚
  65. outb(PIC_S_DATA, 0x01); // ICW4: 8086 模式, 正常 EOI
  66. /* 打开主片上 IR0, 也就是目前只接受时钟产生的中断*/
  67. // outb(PIC_M_DATA, 0xfe);
  68. // outb(PIC_M_DATA, 0xfd); // 测试键盘,只打开键盘中断,其他全部关闭
  69. outb(PIC_M_DATA, 0xfc); // 测试键盘,时钟,开键盘,时钟中断,其他全部关闭 1111 1100
  70. outb(PIC_S_DATA, 0xff);
  71. put_str(" pic_init done\n");
  72. }
  73. // 通用中断处理函数, 一般用于处理异常
  74. static void general_intr_handler(uint8_t vec_nr)
  75. {
  76. if (vec_nr == 0x27 || vec_nr == 0x2f)
  77. {
  78. // IRQ7 和 IRQ15 会产生伪中断,无需处理
  79. // 0x2f 是从片8259A上的最后一个 IRQ 引脚,保留项
  80. return;
  81. }
  82. set_cursor(0); // 将光标置为屏幕左上角,从此处打印异常信息,方便阅读
  83. int cursor_pos = 0;
  84. while (cursor_pos < 320)
  85. { // 清空 4 行位置的字符
  86. put_char(' ');
  87. cursor_pos++;
  88. }
  89. set_cursor(0); // 重置光标位置
  90. put_str("!!!!!!! exception message begin !!!!!!!!!!!!!\n");
  91. set_cursor(88); // 从第 2 行第 8 个字符开始打印, 80 字符宽
  92. put_str("intr name: ");
  93. put_str(intr_name[vec_nr]);
  94. if (vec_nr == 14) // 若为 Pagefault, 将缺失的地址打印出来并悬停
  95. {
  96. int page_fault_vaddr = 0;
  97. asm("movl %%cr2, %0" : "=r"(page_fault_vaddr)); // cr2是存放page_fault的线性地址
  98. put_str("\nPage fault addr is: 0X"); // 输出缺失的地址
  99. put_int(page_fault_vaddr);
  100. }
  101. put_str("\n!!!!!!! exception message end !!!!!!!!!!!!!\n");
  102. // 能进入中断处理程序就表示已经处在关中断情况下, 不会出现调度进程的情况。
  103. // 所以此处的 while 不会再被中断打断,此处的死循环是为了保证当前任务继续运行
  104. while (1)
  105. ;
  106. }
  107. static void exception_init(void)
  108. {
  109. int i;
  110. for (i = 0; i < IDT_DESC_CNT; i++)
  111. {
  112. /*idt_table 数组中的函数是进入中断后根据中断向量号调用的,见 kernel/kernel.S 的 call [itd_table + %1*4]*/
  113. idt_table[i] = general_intr_handler; // 默认为 general_intr_handler
  114. intr_name[i] = "unknown";
  115. }
  116. intr_name[0] = "#DE Divide Error";
  117. intr_name[1] = "#DB Debug Exception";
  118. intr_name[2] = "#NMI Interrupt";
  119. intr_name[3] = "#BP Breakpoint Exception";
  120. intr_name[4] = "#OF Overflow Exception";
  121. intr_name[5] = "#BR BOUND Range Exceeded Exception";
  122. intr_name[6] = "#UD Invalid Opcode Exception";
  123. intr_name[7] = "#NM Device Not Available Exception";
  124. intr_name[8] = "#DF Double Fault Exception";
  125. intr_name[9] = "#Coprocessor Segment Overrun";
  126. intr_name[10] = "#TS Invalid TSS Exception";
  127. intr_name[11] = "#NP Segment Not Present";
  128. intr_name[12] = "#SS Stack Fault Exception";
  129. intr_name[13] = "#GP General Protection Exception";
  130. intr_name[14] = "#PF Page-Fault Exception";
  131. // intr_name[15] 第 15 项是 intel 保留项,未使用
  132. intr_name[16] = "#MF x87 FPU Floating-Point Error";
  133. intr_name[17] = "#AC Alignment Check Exception";
  134. intr_name[18] = "#MC Machine-Check Exception";
  135. intr_name[19] = "#XF SIMD Floating-Point Exception";
  136. put_str(" exception_init done\n");
  137. }
  138. /* 中断初始化主函数 */
  139. void itd_init(void)
  140. {
  141. put_str("init_idt start\n");
  142. itd_desc_init(); // 初始化中断描述符表
  143. exception_init(); // 完成一般中断处理函数注册及异常名称注册
  144. pic_init(); // 初始化可编程中断控制器8259A
  145. /* 加载 idt */
  146. uint64_t idt_operand = ((sizeof(idt) - 1) | ((uint64_t)((uintptr_t)idt << 16)));
  147. asm volatile("lidt %0" ::"m"(idt_operand));
  148. put_str("itd_init done\n");
  149. }
  150. enum intr_status intr_get_status(void)
  151. {
  152. uint32_t eflags = 0;
  153. GET_EFLAGS(eflags);
  154. return (EFLAGS_IF & eflags) ? INTR_ON : INTR_OFF;
  155. }
  156. enum intr_status intr_set_status(enum intr_status status)
  157. {
  158. return status & INTR_ON ? intr_enable() : intr_disable();
  159. }
  160. enum intr_status intr_enable(void)
  161. {
  162. enum intr_status old_status;
  163. if (INTR_ON == intr_get_status())
  164. {
  165. old_status = INTR_ON;
  166. return old_status;
  167. }
  168. else
  169. {
  170. old_status = INTR_OFF;
  171. asm volatile("sti"); // 开中断, sti 指令将IF位置1
  172. return old_status;
  173. }
  174. }
  175. enum intr_status intr_disable(void)
  176. {
  177. enum intr_status old_status;
  178. if (INTR_ON == intr_get_status())
  179. {
  180. old_status = INTR_ON;
  181. asm volatile("cli" : : : "memory"); // 关中断, cli 指令将IF位置0
  182. return old_status;
  183. }
  184. else
  185. {
  186. old_status = INTR_OFF;
  187. return old_status;
  188. }
  189. }
  190. void register_handler(uint8_t vector_no, intr_handler function)
  191. { // idt_table 数组中的函数是进入中断后根据中断向量号调用的,
  192. // 见 kernel/kernel.S 的 call [itd_table + %1*4]
  193. idt_table[vector_no] = function;
  194. }