apic.c 22 KB

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