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