apic.c 23 KB

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