smp.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "smp.h"
  2. #include "../common/kprint.h"
  3. #include "../driver/interrupt/apic/apic.h"
  4. #include "../exception/gate.h"
  5. #include "../common/cpu.h"
  6. #include "../mm/slab.h"
  7. #include "../process/process.h"
  8. #include "../process/spinlock.h"
  9. #include "ipi.h"
  10. static spinlock_t multi_core_starting_lock; // 多核启动锁
  11. static struct acpi_Processor_Local_APIC_Structure_t *proc_local_apic_structs[MAX_SUPPORTED_PROCESSOR_NUM];
  12. static uint32_t total_processor_num = 0;
  13. int current_starting_cpu = 0;
  14. int num_cpu_started = 1;
  15. void smp_init()
  16. {
  17. spin_init(&multi_core_starting_lock); // 初始化多核启动锁
  18. ul tmp_vaddr[MAX_SUPPORTED_PROCESSOR_NUM] = {0};
  19. apic_get_ics(ACPI_ICS_TYPE_PROCESSOR_LOCAL_APIC, tmp_vaddr, &total_processor_num);
  20. kdebug("processor num=%d", total_processor_num);
  21. for (int i = 0; i < total_processor_num; ++i)
  22. proc_local_apic_structs[i] = (struct acpi_Processor_Local_APIC_Structure_t *)(tmp_vaddr[i]);
  23. //*(uchar *)0x20000 = 0xf4; // 在内存的0x20000处写入HLT指令(AP处理器会执行物理地址0x20000的代码)
  24. // 将引导程序复制到物理地址0x20000处
  25. memcpy((unsigned char *)phys_2_virt(0x20000), _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start);
  26. // 设置多核IPI中断门
  27. for (int i = 200; i < 210; ++i)
  28. set_intr_gate(i, 2, SMP_interrupt_table[i - 200]);
  29. memset((void *)SMP_IPI_desc, 0, sizeof(irq_desc_t) * SMP_IRQ_NUM);
  30. ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x00, ICR_INIT, ICR_ALL_EXCLUDE_Self, true, 0x00);
  31. for (int i = 1; i < total_processor_num; ++i) // i从1开始,不初始化bsp
  32. {
  33. if (proc_local_apic_structs[i]->ACPI_Processor_UID == 0)
  34. --total_processor_num;
  35. spin_lock(&multi_core_starting_lock);
  36. current_starting_cpu = i;
  37. kdebug("[core %d] acpi processor UID=%d, APIC ID=%d, flags=%#010lx", i, proc_local_apic_structs[i]->ACPI_Processor_UID, proc_local_apic_structs[i]->ACPI_ID, proc_local_apic_structs[i]->flags);
  38. // 为每个AP处理器分配栈空间、tss空间
  39. cpu_core_info[i].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
  40. cpu_core_info[i].tss_vaddr = (uint64_t)kmalloc(128, 0);
  41. set_tss_descriptor(10 + (i * 2), (void *)virt_2_phys(cpu_core_info[i].tss_vaddr));
  42. set_tss64((uint *)cpu_core_info[i].tss_vaddr, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start, cpu_core_info[i].stack_start);
  43. kdebug("phys_2_virt(GDT_Table)=%#018lx",phys_2_virt(GDT_Table));
  44. kdebug("GDT Table %#018lx, \t %#018lx", *(ul *)(phys_2_virt(GDT_Table) + 10 + i * 2), *(ul *)(phys_2_virt(GDT_Table) + 10 + i * 2 + 1));
  45. // kdebug("(cpu_core_info[i].tss_vaddr)=%#018lx", (cpu_core_info[i].tss_vaddr));
  46. kdebug("(cpu_core_info[i].stack_start)=%#018lx", (cpu_core_info[i].stack_start));
  47. // 连续发送两次start-up IPI
  48. ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->ACPI_ID);
  49. ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, true, proc_local_apic_structs[i]->ACPI_ID);
  50. }
  51. while (num_cpu_started != total_processor_num)
  52. __asm__ __volatile__("pause" ::
  53. : "memory");
  54. kinfo("Cleaning page table remapping...\n");
  55. // 由于ap处理器初始化过程需要用到0x00处的地址,因此初始化完毕后才取消内存地址的重映射
  56. //todo: 取消低0-2M的地址映射
  57. for (int i = 1; i < 128; ++i)
  58. {
  59. *(ul *)(phys_2_virt(global_CR3) + i) = 0UL;
  60. }
  61. kinfo("Successfully cleaned page table remapping!\n");
  62. }
  63. /**
  64. * @brief AP处理器启动后执行的第一个函数
  65. *
  66. */
  67. void smp_ap_start()
  68. {
  69. // 切换栈基地址
  70. // uint64_t stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
  71. __asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
  72. : "memory");
  73. __asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
  74. : "memory");
  75. /*
  76. __asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(stack_start)
  77. : "memory");
  78. __asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(stack_start)
  79. : "memory");*/
  80. ksuccess("AP core successfully started!");
  81. ++num_cpu_started;
  82. kdebug("current cpu = %d", current_starting_cpu);
  83. apic_init_ap_core_local_apic();
  84. load_TR(10 + current_starting_cpu * 2);
  85. sti();
  86. kdebug("IDT_addr = %#018lx", phys_2_virt(IDT_Table));
  87. spin_unlock(&multi_core_starting_lock);
  88. while (1) // 这里要循环hlt,原因是当收到中断后,核心会被唤醒,处理完中断之后不会自动hlt
  89. hlt();
  90. }