1 #include "apic.h"
2 #include "apic_timer.h"
3 #include <common/kprint.h>
4 #include <common/printk.h>
5 #include <common/cpu.h>
6 #include <common/glib.h>
7 #include <exception/gate.h>
8 #include <driver/acpi/acpi.h>
9
10 #include <exception/softirq.h>
11 #include <process/process.h>
12 #include <sched/sched.h>
13
14 #pragma GCC push_options
15 #pragma GCC optimize("O0")
16 // 导出定义在irq.c中的中段门表
17 extern void (*interrupt_table[24])(void);
18
19 static bool flag_support_apic = false;
20 static bool flag_support_x2apic = false;
21 uint8_t __apic_enable_state = APIC_XAPIC_ENABLED;
22 static uint local_apic_version;
23 static uint local_apic_max_LVT_entries;
24
25 static struct acpi_Multiple_APIC_Description_Table_t *madt;
26 static struct acpi_IO_APIC_Structure_t *io_apic_ICS;
27
28 static void __local_apic_xapic_init();
29 static void __local_apic_x2apic_init();
30
__send_eoi()31 static __always_inline void __send_eoi()
32 {
33 if (CURRENT_APIC_STATE == APIC_X2APIC_ENABLED)
34 {
35 __asm__ __volatile__("movq $0x00, %%rdx \n\t"
36 "movq $0x00, %%rax \n\t"
37 "movq $0x80b, %%rcx \n\t"
38 "wrmsr \n\t" ::
39 : "memory");
40 }
41 else
42 {
43
44 io_mfence();
45 __write4b(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_EOI, 0);
46 io_mfence();
47 }
48 }
49
50 /**
51 * @brief 初始化io_apic
52 *
53 */
apic_io_apic_init()54 void apic_io_apic_init()
55 {
56
57 ul madt_addr;
58 acpi_iter_SDT(acpi_get_MADT, &madt_addr);
59 madt = (struct acpi_Multiple_APIC_Description_Table_t *)madt_addr;
60
61 // kdebug("MADT->local intr controller addr=%#018lx", madt->Local_Interrupt_Controller_Address);
62 // kdebug("MADT->length= %d bytes", madt->header.Length);
63 // 寻找io apic的ICS
64 void *ent = (void *)(madt_addr) + sizeof(struct acpi_Multiple_APIC_Description_Table_t);
65 struct apic_Interrupt_Controller_Structure_header_t *header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
66 while (header->length > 2)
67 {
68 header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
69 if (header->type == 1)
70 {
71 struct acpi_IO_APIC_Structure_t *t = (struct acpi_IO_APIC_Structure_t *)ent;
72 // kdebug("IO apic addr = %#018lx", t->IO_APIC_Address);
73 io_apic_ICS = t;
74 break;
75 }
76
77 ent += header->length;
78 }
79 // kdebug("Global_System_Interrupt_Base=%d", io_apic_ICS->Global_System_Interrupt_Base);
80
81 apic_ioapic_map.addr_phys = io_apic_ICS->IO_APIC_Address;
82 apic_ioapic_map.virtual_index_addr = (unsigned char *)APIC_IO_APIC_VIRT_BASE_ADDR;
83 apic_ioapic_map.virtual_data_addr = (uint *)(APIC_IO_APIC_VIRT_BASE_ADDR + 0x10);
84 apic_ioapic_map.virtual_EOI_addr = (uint *)(APIC_IO_APIC_VIRT_BASE_ADDR + 0x40);
85
86 // kdebug("(ul)apic_ioapic_map.virtual_index_addr=%#018lx", (ul)apic_ioapic_map.virtual_index_addr);
87 // 填写页表,完成地址映射
88 mm_map_phys_addr((ul)apic_ioapic_map.virtual_index_addr, apic_ioapic_map.addr_phys, PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
89
90 // 设置IO APIC ID 为0x0f000000
91 *apic_ioapic_map.virtual_index_addr = 0x00;
92 io_mfence();
93 *apic_ioapic_map.virtual_data_addr = 0x0f000000;
94 io_mfence();
95
96 // kdebug("I/O APIC ID:%#010x", ((*apic_ioapic_map.virtual_data_addr) >> 24) & 0xff);
97 io_mfence();
98
99 // 获取IO APIC Version
100 *apic_ioapic_map.virtual_index_addr = 0x01;
101 io_mfence();
102 kdebug("IO APIC Version=%d, Max Redirection Entries=%d", *apic_ioapic_map.virtual_data_addr & 0xff, (((*apic_ioapic_map.virtual_data_addr) >> 16) & 0xff) + 1);
103
104 // 初始化RTE表项,将所有RTE表项屏蔽
105 for (int i = 0x10; i < 0x40; i += 2)
106 {
107 // 以0x20为起始中断向量号,初始化RTE
108 apic_ioapic_write_rte(i, 0x10020 + ((i - 0x10) >> 1));
109 }
110
111 // 不需要手动启动IO APIC,只要初始化了RTE寄存器之后,io apic就会自动启用了。
112 // 而且不是每台电脑都有RCBA寄存器,因此不需要手动启用IO APIC
113 }
114
115 /**
116 * @brief 初始化AP处理器的Local apic
117 *
118 */
apic_init_ap_core_local_apic()119 void apic_init_ap_core_local_apic()
120 {
121 kinfo("Initializing AP-core's local apic...");
122 uint eax, edx;
123 // 启用xAPIC 和x2APIC
124 uint64_t ia32_apic_base = rdmsr(0x1b);
125 ia32_apic_base |= (1 << 11);
126 if (flag_support_x2apic) // 如果支持x2apic,则启用
127 {
128 ia32_apic_base |= (1 << 10);
129 wrmsr(0x1b, ia32_apic_base);
130 }
131 ia32_apic_base = rdmsr(0x1b);
132 eax = ia32_apic_base & 0xffffffff;
133
134 // 检测是否成功启用xAPIC和x2APIC
135 if ((eax & 0xc00) == 0xc00)
136 kinfo("xAPIC & x2APIC enabled!");
137 else if ((eax & 0x800) == 0x800)
138 kinfo("Only xAPIC enabled!");
139 else
140 kerror("Both xAPIC and x2APIC are not enabled.");
141
142 // 设置SVR寄存器,开启local APIC、禁止EOI广播
143 if (flag_support_x2apic) // 当前为x2APIC
144 __local_apic_x2apic_init();
145 else // 当前为xapic
146 __local_apic_xapic_init();
147 }
148
149 /**
150 * @brief 当前使用xapic来初始化local apic
151 *
152 */
__local_apic_xapic_init()153 static void __local_apic_xapic_init()
154 {
155 __apic_enable_state = APIC_XAPIC_ENABLED;
156 // 设置svr的 apic软件使能位
157 uint64_t qword = *(uint64_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_SVR);
158
159 qword |= (1 << 8);
160 *(uint64_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_SVR) = qword;
161 qword = *(uint64_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_SVR);
162 if (qword & 0x100)
163 kinfo("APIC Software Enabled.");
164 if (qword & 0x1000)
165 kinfo("EOI-Broadcast Suppression Enabled.");
166
167 // 从 Local APIC Version register 获取Local APIC Version
168 qword = *(uint64_t *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_Version);
169 qword &= 0xffffffff;
170
171 local_apic_max_LVT_entries = ((qword >> 16) & 0xff) + 1;
172 local_apic_version = qword & 0xff;
173
174 kdebug("local APIC Version:%#010x,Max LVT Entry:%#010x,SVR(Suppress EOI Broadcast):%#04x\t", local_apic_version, local_apic_max_LVT_entries, (qword >> 24) & 0x1);
175
176 if ((qword & 0xff) < 0x10)
177 {
178 kdebug("82489DX discrete APIC");
179 }
180 else if (((qword & 0xff) >= 0x10) && ((qword & 0xff) <= 0x15))
181 kdebug("Integrated APIC.");
182
183 io_mfence();
184 // 如果写入这里的话,在有的机器上面会报错
185 // *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_CMCI) = APIC_LVT_INT_MASKED;
186 io_mfence();
187 *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_TIMER) = APIC_LVT_INT_MASKED;
188 io_mfence();
189
190 *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_THERMAL) = APIC_LVT_INT_MASKED;
191 io_mfence();
192 *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_PERFORMANCE_MONITOR) = APIC_LVT_INT_MASKED;
193 io_mfence();
194 *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT0) = APIC_LVT_INT_MASKED;
195 io_mfence();
196 *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_LINT1) = APIC_LVT_INT_MASKED;
197 io_mfence();
198 *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_LVT_ERROR) = APIC_LVT_INT_MASKED;
199 io_mfence();
200
201 kdebug("All LVT Masked");
202 }
203
204 /**
205 * @brief 当前使用x2apic来初始化local apic
206 *
207 */
__local_apic_x2apic_init()208 static void __local_apic_x2apic_init()
209 {
210 __apic_enable_state = APIC_X2APIC_ENABLED;
211 uint32_t eax, edx;
212 __asm__ __volatile__("movq $0x80f, %%rcx \n\t"
213 "rdmsr \n\t"
214 "bts $8, %%rax \n\t"
215 // "bts $12, %%rax \n\t"
216 "movq $0x80f, %%rcx \n\t"
217 "wrmsr \n\t"
218 "movq $0x80f , %%rcx \n\t"
219 "rdmsr \n\t"
220 : "=a"(eax), "=d"(edx)::"memory");
221 if (eax & 0x100)
222 kinfo("APIC Software Enabled.");
223 if (eax & 0x1000)
224 kinfo("EOI-Broadcast Suppression Enabled.");
225
226 // 获取Local APIC Version
227 // 0x803处是 Local APIC Version register
228 __asm__ __volatile__("movq $0x803, %%rcx \n\t"
229 "rdmsr \n\t"
230 : "=a"(eax), "=d"(edx)::"memory");
231
232 local_apic_max_LVT_entries = ((eax >> 16) & 0xff) + 1;
233 local_apic_version = eax & 0xff;
234
235 kdebug("local APIC Version:%#010x,Max LVT Entry:%#010x,SVR(Suppress EOI Broadcast):%#04x\t", local_apic_version, local_apic_max_LVT_entries, (eax >> 24) & 0x1);
236
237 if ((eax & 0xff) < 0x10)
238 kdebug("82489DX discrete APIC");
239 else if (((eax & 0xff) >= 0x10) && ((eax & 0xff) <= 0x15))
240 kdebug("Integrated APIC.");
241
242 // 由于尚未配置LVT对应的处理程序,因此先屏蔽所有的LVT
243 __asm__ __volatile__( // "movq $0x82f, %%rcx \n\t" // CMCI
244 // "wrmsr \n\t"
245 "movq $0x832, %%rcx \n\t" // Timer
246 "wrmsr \n\t"
247 "movq $0x833, %%rcx \n\t" // Thermal Monitor
248 "wrmsr \n\t"
249 "movq $0x834, %%rcx \n\t" // Performance Counter
250 "wrmsr \n\t"
251 "movq $0x835, %%rcx \n\t" // LINT0
252 "wrmsr \n\t"
253 "movq $0x836, %%rcx \n\t" // LINT1
254 "wrmsr \n\t"
255 "movq $0x837, %%rcx \n\t" // Error
256 "wrmsr \n\t"
257 :
258 : "a"(0x10000), "d"(0x00)
259 : "memory");
260 kdebug("All LVT Masked");
261 }
262
263 /**
264 * @brief 初始化local apic
265 *
266 */
apic_local_apic_init()267 void apic_local_apic_init()
268 {
269 uint64_t ia32_apic_base = rdmsr(0x1b);
270 // kdebug("apic base=%#018lx", (ia32_apic_base & 0x1FFFFFFFFFF000));
271 // 映射Local APIC 寄存器地址
272 mm_map_phys_addr(APIC_LOCAL_APIC_VIRT_BASE_ADDR, (ia32_apic_base & 0x1FFFFFFFFFFFFF), PAGE_2M_SIZE, PAGE_KERNEL_PAGE | PAGE_PWT | PAGE_PCD, false);
273 uint a, b, c, d;
274
275 cpu_cpuid(1, 0, &a, &b, &c, &d);
276
277 // kdebug("CPUID 0x01, eax:%#010lx, ebx:%#010lx, ecx:%#010lx, edx:%#010lx", a, b, c, d);
278
279 // 判断是否支持APIC和xAPIC
280 if ((1 << 9) & d)
281 {
282 flag_support_apic = true;
283 kdebug("This computer support APIC&xAPIC");
284 }
285 else
286 {
287 flag_support_apic = false;
288 kerror("This computer does not support APIC&xAPIC");
289 while (1)
290 ;
291 }
292
293 // 判断是否支持x2APIC
294 if ((1 << 21) & c)
295 {
296 flag_support_x2apic = true;
297 kdebug("This computer support x2APIC");
298 }
299 else
300 {
301 flag_support_x2apic = false;
302 kwarn("This computer does not support x2APIC");
303 }
304
305 uint eax, edx;
306 // 启用xAPIC 和x2APIC
307 ia32_apic_base = rdmsr(0x1b);
308 ia32_apic_base |= (1 << 11);
309 if (flag_support_x2apic) // 如果支持x2apic,则启用
310 {
311 ia32_apic_base |= (1 << 10);
312 wrmsr(0x1b, ia32_apic_base);
313 }
314 ia32_apic_base = rdmsr(0x1b);
315 eax = ia32_apic_base & 0xffffffff;
316
317 // 检测是否成功启用xAPIC和x2APIC
318 if ((eax & 0xc00) == 0xc00)
319 kinfo("xAPIC & x2APIC enabled!");
320 else if ((eax & 0x800) == 0x800)
321 kinfo("Only xAPIC enabled!");
322 else
323 kerror("Both xAPIC and x2APIC are not enabled.");
324
325 // 设置SVR寄存器,开启local APIC、禁止EOI广播
326 if (flag_support_x2apic) // 当前为x2APIC
327 __local_apic_x2apic_init();
328 else // 当前为xapic
329 __local_apic_xapic_init();
330
331 // 获取Local APIC的基础信息 (参见英特尔开发手册Vol3A 10-39)
332 // Table 10-6. Local APIC Register Address Map Supported by x2APIC
333 // 获取 Local APIC ID
334 // 0x802处是x2APIC ID 位宽32bits 的 Local APIC ID register
335 /*
336 __asm__ __volatile__("movq $0x802, %%rcx \n\t"
337 "rdmsr \n\t"
338 : "=a"(eax), "=d"(edx)::"memory");
339 */
340 // kdebug("get Local APIC ID: edx=%#010x, eax=%#010x", edx, eax);
341 // kdebug("local_apic_id=%#018lx", *(uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_ID));
342 }
343
344 /**
345 * @brief 初始化apic控制器
346 *
347 */
apic_init()348 int apic_init()
349 {
350 // 初始化中断门, 中断使用rsp0防止在软中断时发生嵌套,然后处理器重新加载导致数据被抹掉
351 for (int i = 32; i <= 55; ++i)
352 set_intr_gate(i, 0, interrupt_table[i - 32]);
353
354 // 设置local apic中断门
355 for (int i = 150; i < 160; ++i)
356 set_intr_gate(i, 0, local_apic_interrupt_table[i - 150]);
357
358 // 屏蔽类8259A芯片
359 io_out8(0x21, 0xff);
360
361 io_out8(0xa1, 0xff);
362
363 // 写入8259A pic的EOI位
364 io_out8(0x20, 0x20);
365 io_out8(0xa0, 0x20);
366
367 kdebug("8259A Masked.");
368
369 // enable IMCR
370 io_out8(0x22, 0x70);
371 io_out8(0x23, 0x01);
372
373 apic_local_apic_init();
374
375 apic_io_apic_init();
376
377 // get RCBA address
378 io_out32(0xcf8, 0x8000f8f0);
379 uint32_t RCBA_phys = io_in32(0xcfc);
380
381 // 获取RCBA寄存器的地址
382 if (RCBA_phys > 0xfec00000 && RCBA_phys < 0xfee00000)
383 RCBA_vaddr = SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE + RCBA_phys;
384 else
385 {
386 RCBA_vaddr = 0;
387 kwarn("Cannot get RCBA address. RCBA_phys=%#010lx", RCBA_phys);
388 }
389 sti();
390 return 0;
391 }
392 /**
393 * @brief 中断服务程序
394 *
395 * @param rsp 中断栈指针
396 * @param number 中断向量号
397 */
do_IRQ(struct pt_regs * rsp,ul number)398 void do_IRQ(struct pt_regs *rsp, ul number)
399 {
400
401 if (number < 0x80 && number >= 32) // 以0x80为界限,低于0x80的是外部中断控制器,高于0x80的是Local APIC
402 {
403 // ==========外部中断控制器========
404 irq_desc_t *irq = &interrupt_desc[number - 32];
405
406 // 执行中断上半部处理程序
407 if (irq != NULL && irq->handler != NULL)
408 irq->handler(number, irq->parameter, rsp);
409 else
410 kwarn("Intr vector [%d] does not have a handler!");
411 // 向中断控制器发送应答消息
412 if (irq->controller != NULL && irq->controller->ack != NULL)
413 irq->controller->ack(number);
414 else
415 __send_eoi();
416 }
417 else if (number >= 200)
418 {
419 apic_local_apic_edge_ack(number);
420
421 {
422 irq_desc_t *irq = &SMP_IPI_desc[number - 200];
423 if (irq->handler != NULL)
424 irq->handler(number, irq->parameter, rsp);
425 }
426 }
427 else if (number >= 150 && number < 200)
428 {
429 irq_desc_t *irq = &local_apic_interrupt_desc[number - 150];
430
431 // 执行中断上半部处理程序
432 if (irq != NULL && irq->handler != NULL)
433 irq->handler(number, irq->parameter, rsp);
434 else
435 kwarn("Intr vector [%d] does not have a handler!");
436 // 向中断控制器发送应答消息
437 if (irq->controller != NULL && irq->controller->ack != NULL)
438 irq->controller->ack(number);
439 else
440 __send_eoi(); // 向EOI寄存器写入0x00表示结束中断
441 }
442 else
443 {
444
445 kwarn("do IRQ receive: %d", number);
446 // 忽略未知中断
447 return;
448 }
449
450 // kdebug("before softirq");
451 // 进入软中断处理程序
452 do_softirq();
453
454 // kdebug("after softirq");
455 // 检测当前进程是否持有自旋锁,若持有自旋锁,则不进行抢占式的进程调度
456 if (current_pcb->preempt_count > 0)
457 return;
458 else if (current_pcb->preempt_count < 0)
459 kBUG("current_pcb->preempt_count<0! pid=%d", current_pcb->pid); // should not be here
460
461 // 检测当前进程是否可被调度
462 if (current_pcb->flags & PF_NEED_SCHED && number == APIC_TIMER_IRQ_NUM)
463 {
464 io_mfence();
465 sched();
466 }
467 }
468
469 /**
470 * @brief 读取RTE寄存器
471 * 由于RTE位宽为64位而IO window寄存器只有32位,因此需要两次读取
472 * @param index 索引值
473 * @return ul
474 */
apic_ioapic_read_rte(unsigned char index)475 ul apic_ioapic_read_rte(unsigned char index)
476 {
477 // 由于处理器的乱序执行的问题,需要加入内存屏障以保证结果的正确性。
478 ul ret;
479 // 先读取高32bit
480 *apic_ioapic_map.virtual_index_addr = index + 1;
481 io_mfence();
482
483 ret = *apic_ioapic_map.virtual_data_addr;
484 ret <<= 32;
485 io_mfence();
486
487 // 读取低32bit
488 *apic_ioapic_map.virtual_index_addr = index;
489 io_mfence();
490 ret |= *apic_ioapic_map.virtual_data_addr;
491 io_mfence();
492
493 return ret;
494 }
495
496 /**
497 * @brief 写入RTE寄存器
498 *
499 * @param index 索引值
500 * @param value 要写入的值
501 */
apic_ioapic_write_rte(unsigned char index,ul value)502 void apic_ioapic_write_rte(unsigned char index, ul value)
503 {
504 // 先写入低32bit
505 *apic_ioapic_map.virtual_index_addr = index;
506 io_mfence();
507
508 *apic_ioapic_map.virtual_data_addr = value & 0xffffffff;
509 io_mfence();
510 // 再写入高32bit
511 value >>= 32;
512 io_mfence();
513 *apic_ioapic_map.virtual_index_addr = index + 1;
514 io_mfence();
515 *apic_ioapic_map.virtual_data_addr = value & 0xffffffff;
516 io_mfence();
517 }
518
519 // =========== 中断控制操作接口 ============
apic_ioapic_enable(ul irq_num)520 void apic_ioapic_enable(ul irq_num)
521 {
522 ul index = 0x10 + ((irq_num - 32) << 1);
523 ul value = apic_ioapic_read_rte(index);
524 value &= (~0x10000UL);
525 apic_ioapic_write_rte(index, value);
526 }
527
apic_ioapic_disable(ul irq_num)528 void apic_ioapic_disable(ul irq_num)
529 {
530 ul index = 0x10 + ((irq_num - 32) << 1);
531 ul value = apic_ioapic_read_rte(index);
532 value |= (0x10000UL);
533 apic_ioapic_write_rte(index, value);
534 }
535
apic_ioapic_install(ul irq_num,void * arg)536 ul apic_ioapic_install(ul irq_num, void *arg)
537 {
538 struct apic_IO_APIC_RTE_entry *entry = (struct apic_IO_APIC_RTE_entry *)arg;
539 // RTE表项值写入对应的RTE寄存器
540 apic_ioapic_write_rte(0x10 + ((irq_num - 32) << 1), *(ul *)entry);
541 return 0;
542 }
543
apic_ioapic_uninstall(ul irq_num)544 void apic_ioapic_uninstall(ul irq_num)
545 {
546 // 将对应的RTE表项设置为屏蔽状态
547 apic_ioapic_write_rte(0x10 + ((irq_num - 32) << 1), 0x10000UL);
548 }
549
apic_ioapic_level_ack(ul irq_num)550 void apic_ioapic_level_ack(ul irq_num) // 电平触发
551 {
552 __send_eoi();
553 *apic_ioapic_map.virtual_EOI_addr = irq_num;
554 }
555
apic_ioapic_edge_ack(ul irq_num)556 void apic_ioapic_edge_ack(ul irq_num) // 边沿触发
557 {
558
559 // 向EOI寄存器写入0x00表示结束中断
560 /*
561 uint *eoi = (uint *)(APIC_LOCAL_APIC_VIRT_BASE_ADDR + LOCAL_APIC_OFFSET_Local_APIC_EOI);
562 *eoi = 0x00;
563
564 */
565 __send_eoi();
566 }
567
568 /**
569 * @brief local apic 边沿触发应答
570 *
571 * @param irq_num
572 */
573
apic_local_apic_edge_ack(ul irq_num)574 void apic_local_apic_edge_ack(ul irq_num)
575 {
576 // 向EOI寄存器写入0x00表示结束中断
577 __send_eoi();
578 }
579
580 /**
581 * @brief 读取指定类型的 Interrupt Control Structure
582 *
583 * @param type ics的类型
584 * @param ret_vaddr 对应的ICS的虚拟地址数组
585 * @param total 返回数组的元素总个数
586 * @return uint
587 */
apic_get_ics(const uint type,ul ret_vaddr[],uint * total)588 uint apic_get_ics(const uint type, ul ret_vaddr[], uint *total)
589 {
590 void *ent = (void *)(madt) + sizeof(struct acpi_Multiple_APIC_Description_Table_t);
591 struct apic_Interrupt_Controller_Structure_header_t *header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
592 bool flag = false;
593
594 uint cnt = 0;
595
596 while (header->length > 2)
597 {
598 header = (struct apic_Interrupt_Controller_Structure_header_t *)ent;
599 if (header->type == type)
600 {
601 ret_vaddr[cnt++] = (ul)ent;
602 flag = true;
603 }
604 ent += header->length;
605 }
606
607 *total = cnt;
608 if (!flag)
609 return APIC_E_NOTFOUND;
610 else
611 return APIC_SUCCESS;
612 }
613
614 /**
615 * @brief 构造RTE Entry结构体
616 *
617 * @param entry 返回的结构体
618 * @param vector 中断向量
619 * @param deliver_mode 投递模式
620 * @param dest_mode 目标模式
621 * @param deliver_status 投递状态
622 * @param polarity 电平触发极性
623 * @param irr 远程IRR标志位(只读)
624 * @param trigger 触发模式
625 * @param mask 屏蔽标志位,(0为未屏蔽, 1为已屏蔽)
626 * @param dest_apicID 目标apicID
627 */
apic_make_rte_entry(struct apic_IO_APIC_RTE_entry * entry,uint8_t vector,uint8_t deliver_mode,uint8_t dest_mode,uint8_t deliver_status,uint8_t polarity,uint8_t irr,uint8_t trigger,uint8_t mask,uint8_t dest_apicID)628 void apic_make_rte_entry(struct apic_IO_APIC_RTE_entry *entry, uint8_t vector, uint8_t deliver_mode, uint8_t dest_mode,
629 uint8_t deliver_status, uint8_t polarity, uint8_t irr, uint8_t trigger, uint8_t mask, uint8_t dest_apicID)
630 {
631
632 entry->vector = vector;
633 entry->deliver_mode = deliver_mode;
634 entry->dest_mode = dest_mode;
635 entry->deliver_status = deliver_status;
636 entry->polarity = polarity;
637 entry->remote_IRR = irr;
638 entry->trigger_mode = trigger;
639 entry->mask = mask;
640
641 entry->reserved = 0;
642
643 if (dest_mode == DEST_PHYSICAL)
644 {
645 entry->destination.physical.phy_dest = dest_apicID;
646 entry->destination.physical.reserved1 = 0;
647 entry->destination.physical.reserved2 = 0;
648 }
649 else
650 {
651 entry->destination.logical.logical_dest = dest_apicID;
652 entry->destination.logical.reserved1 = 0;
653 }
654 }
655 #pragma GCC pop_options