apic.c 22 KB

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