apic.c 23 KB

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