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