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