1
2 #include "irq.h"
3 #include <common/errno.h>
4
5 #if _INTR_8259A_
6 #include <driver/interrupt/8259A/8259A.h>
7 #else
8 #include <driver/interrupt/apic/apic.h>
9 #endif
10
11 #include "gate.h"
12 #include <common/asm.h>
13 #include <common/printk.h>
14 #include <common/string.h>
15 #include <mm/slab.h>
16 extern void ignore_int();
17
18 #pragma GCC push_options
19 #pragma GCC optimize("O0")
20 // 保存函数调用现场的寄存器
21 #define SAVE_ALL_REGS \
22 "cld; \n\t" \
23 "pushq %rax; \n\t" \
24 "pushq %rax; \n\t" \
25 "movq %es, %rax; \n\t" \
26 "pushq %rax; \n\t" \
27 "movq %ds, %rax; \n\t" \
28 "pushq %rax; \n\t" \
29 "xorq %rax, %rax;\n\t" \
30 "pushq %rbp; \n\t" \
31 "pushq %rdi; \n\t" \
32 "pushq %rsi; \n\t" \
33 "pushq %rdx; \n\t" \
34 "pushq %rcx; \n\t" \
35 "pushq %rbx; \n\t" \
36 "pushq %r8 ; \n\t" \
37 "pushq %r9 ; \n\t" \
38 "pushq %r10; \n\t" \
39 "pushq %r11; \n\t" \
40 "pushq %r12; \n\t" \
41 "pushq %r13; \n\t" \
42 "pushq %r14; \n\t" \
43 "pushq %r15; \n\t" \
44 "movq $0x10, %rdx;\n\t" \
45 "movq %rdx, %ds; \n\t" \
46 "movq %rdx, %es; \n\t"
47
48 // 定义IRQ处理函数的名字格式:IRQ+中断号+interrupt
49 #define IRQ_NAME2(name1) name1##interrupt(void)
50 #define IRQ_NAME(number) IRQ_NAME2(IRQ##number)
51
52 // 构造中断entry
53 // 为了复用返回函数的代码,需要压入一个错误码0
54 // todo: 将这里改为volatile,也许能解决编译选项为O1时,系统崩溃的问题
55 #define Build_IRQ(number) \
56 void IRQ_NAME(number); \
57 __asm__(SYMBOL_NAME_STR(IRQ) #number "interrupt: \n\t" \
58 "pushq $0x00 \n\t" SAVE_ALL_REGS "movq %rsp, %rdi \n\t" \
59 "leaq ret_from_intr(%rip), %rax \n\t" \
60 "pushq %rax \n\t" \
61 "movq $" #number ", %rsi \n\t" \
62 "jmp do_IRQ \n\t");
63
64 // 构造中断入口
65 Build_IRQ(0x20);
66 Build_IRQ(0x21);
67 Build_IRQ(0x22);
68 Build_IRQ(0x23);
69 Build_IRQ(0x24);
70 Build_IRQ(0x25);
71 Build_IRQ(0x26);
72 Build_IRQ(0x27);
73 Build_IRQ(0x28);
74 Build_IRQ(0x29);
75 Build_IRQ(0x2a);
76 Build_IRQ(0x2b);
77 Build_IRQ(0x2c);
78 Build_IRQ(0x2d);
79 Build_IRQ(0x2e);
80 Build_IRQ(0x2f);
81 Build_IRQ(0x30);
82 Build_IRQ(0x31);
83 Build_IRQ(0x32);
84 Build_IRQ(0x33);
85 Build_IRQ(0x34);
86 Build_IRQ(0x35);
87 Build_IRQ(0x36);
88 Build_IRQ(0x37);
89
90 // 初始化中断数组
91 void (*interrupt_table[24])(void) = {
92 IRQ0x20interrupt, IRQ0x21interrupt, IRQ0x22interrupt, IRQ0x23interrupt, IRQ0x24interrupt, IRQ0x25interrupt,
93 IRQ0x26interrupt, IRQ0x27interrupt, IRQ0x28interrupt, IRQ0x29interrupt, IRQ0x2ainterrupt, IRQ0x2binterrupt,
94 IRQ0x2cinterrupt, IRQ0x2dinterrupt, IRQ0x2einterrupt, IRQ0x2finterrupt, IRQ0x30interrupt, IRQ0x31interrupt,
95 IRQ0x32interrupt, IRQ0x33interrupt, IRQ0x34interrupt, IRQ0x35interrupt, IRQ0x36interrupt, IRQ0x37interrupt,
96 };
97
98 /**
99 * @brief 声明10个IPI消息处理程序,向量号从200(0xc8)开始
100 *
101 */
102
103 /*
104 */
105 Build_IRQ(0xc8);
106 Build_IRQ(0xc9);
107 Build_IRQ(0xca);
108 Build_IRQ(0xcb);
109 Build_IRQ(0xcc);
110 Build_IRQ(0xcd);
111 Build_IRQ(0xce);
112 Build_IRQ(0xcf);
113 Build_IRQ(0xd0);
114 Build_IRQ(0xd1);
115
116 Build_IRQ(0x80); // 系统调用入口
117 void (*syscall_intr_table[1])(void) = {IRQ0x80interrupt};
118
119 // 初始化IPI中断服务程序数组
120 void (*SMP_interrupt_table[SMP_IRQ_NUM])(void) = {
121 IRQ0xc8interrupt, IRQ0xc9interrupt, IRQ0xcainterrupt, IRQ0xcbinterrupt, IRQ0xccinterrupt,
122 IRQ0xcdinterrupt, IRQ0xceinterrupt, IRQ0xcfinterrupt, IRQ0xd0interrupt, IRQ0xd1interrupt,
123 };
124
125 // 初始化local apic中断服务程序数组
126 Build_IRQ(0x96);
127 Build_IRQ(0x97);
128 Build_IRQ(0x98);
129 Build_IRQ(0x99);
130 Build_IRQ(0x9a);
131 Build_IRQ(0x9b);
132 Build_IRQ(0x9c);
133 Build_IRQ(0x9d);
134 Build_IRQ(0x9e);
135 Build_IRQ(0x9f);
136 void (*local_apic_interrupt_table[LOCAL_APIC_IRQ_NUM])(void) = {
137 IRQ0x96interrupt, IRQ0x97interrupt, IRQ0x98interrupt, IRQ0x99interrupt, IRQ0x9ainterrupt,
138 IRQ0x9binterrupt, IRQ0x9cinterrupt, IRQ0x9dinterrupt, IRQ0x9einterrupt, IRQ0x9finterrupt,
139 };
140
141 /**
142 * @brief 中断注册函数
143 *
144 * @param irq_num 中断向量号
145 * @param arg 传递给中断安装接口的参数
146 * @param handler 中断处理函数
147 * @param paramater 中断处理函数的参数
148 * @param controller 中断控制器结构
149 * @param irq_name 中断名
150 * @return int
151 */
irq_register(ul irq_num,void * arg,void (* handler)(ul irq_num,ul parameter,struct pt_regs * regs),ul paramater,hardware_intr_controller * controller,char * irq_name)152 int irq_register(ul irq_num, void *arg, void (*handler)(ul irq_num, ul parameter, struct pt_regs *regs), ul paramater,
153 hardware_intr_controller *controller, char *irq_name)
154 {
155 // 由于为I/O APIC分配的中断向量号是从32开始的,因此要减去32才是对应的interrupt_desc的元素
156 irq_desc_t *p = NULL;
157 if (irq_num >= 32 && irq_num < 0x80)
158 p = &interrupt_desc[irq_num - 32];
159 else if (irq_num >= 150 && irq_num < 200)
160 p = &local_apic_interrupt_desc[irq_num - 150];
161 else
162 {
163 kerror("irq_register(): invalid irq num: %ld.", irq_num);
164 return -EINVAL;
165 }
166 p->controller = controller;
167 if (p->irq_name == NULL)
168 {
169 int namelen = sizeof(strlen(irq_name) + 1);
170 p->irq_name = (char *)kmalloc(namelen, 0);
171 memset(p->irq_name, 0, namelen);
172 strncpy(p->irq_name, irq_name, namelen);
173 }
174
175 p->parameter = paramater;
176 p->flags = 0;
177 p->handler = handler;
178 io_mfence();
179 p->controller->install(irq_num, arg);
180 io_mfence();
181 p->controller->enable(irq_num);
182 io_mfence();
183
184 return 0;
185 }
186
187 /**
188 * @brief 中断注销函数
189 *
190 * @param irq_num 中断向量号
191 * @return int
192 */
irq_unregister(ul irq_num)193 int irq_unregister(ul irq_num)
194 {
195 irq_desc_t *p = &interrupt_desc[irq_num - 32];
196 p->controller->disable(irq_num);
197 p->controller->uninstall(irq_num);
198
199 p->controller = NULL;
200 if (p->irq_name)
201 kfree(p->irq_name);
202 p->irq_name = NULL;
203 p->parameter = NULL;
204 p->flags = 0;
205 p->handler = NULL;
206
207 return 0;
208 }
209
210 /**
211 * @brief 初始化中断模块
212 */
irq_init()213 void irq_init()
214 {
215 #if _INTR_8259A_
216 init_8259A();
217 #else
218
219 memset((void *)interrupt_desc, 0, sizeof(irq_desc_t) * IRQ_NUM);
220 apic_init();
221
222 #endif
223 }
224 #pragma GCC optimize("O0")
225