smp.c 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. void smp_init()
  15. {
  16. spin_init(&multi_core_starting_lock); // 初始化多核启动锁
  17. ul tmp_vaddr[MAX_SUPPORTED_PROCESSOR_NUM] = {0};
  18. apic_get_ics(ACPI_ICS_TYPE_PROCESSOR_LOCAL_APIC, tmp_vaddr, &total_processor_num);
  19. kdebug("processor num=%d", total_processor_num);
  20. for (int i = 0; i < total_processor_num; ++i)
  21. proc_local_apic_structs[i] = (struct acpi_Processor_Local_APIC_Structure_t *)(tmp_vaddr[i]);
  22. //*(uchar *)0x20000 = 0xf4; // 在内存的0x20000处写入HLT指令(AP处理器会执行物理地址0x20000的代码)
  23. // 将引导程序复制到物理地址0x20000处
  24. memcpy((unsigned char *)0x20000, _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start);
  25. // 设置多核IPI中断门
  26. for (int i = 200; i < 210; ++i)
  27. set_intr_gate(i, 2, SMP_interrupt_table[i - 200]);
  28. memset(SMP_IPI_desc, 0, sizeof(irq_desc_t) * SMP_IRQ_NUM);
  29. ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x00, ICR_INIT, ICR_ALL_EXCLUDE_Self, true, 0x00);
  30. for (int i = 1; i < total_processor_num; ++i) // i从1开始,不初始化bsp
  31. {
  32. spin_lock(&multi_core_starting_lock);
  33. current_starting_cpu = i;
  34. 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);
  35. // 为每个AP处理器分配栈空间、tss空间
  36. cpu_core_info[i].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0);
  37. kdebug("cpu_core_info[i].stack_start =%#018lx", (uint64_t)kmalloc(STACK_SIZE, 0));
  38. cpu_core_info[i].stack_start += STACK_SIZE;
  39. kdebug("cpu_core_info[i].stack_base =%#018lx", (uint64_t)kmalloc(STACK_SIZE, 0));
  40. cpu_core_info[i].tss_vaddr = (uint64_t)kmalloc(128, 0);
  41. set_tss_descriptor(10 + (i * 2), (void *)(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("GDT Table %#018lx, \t %#018lx", GDT_Table[10 + i * 2], GDT_Table[10 + i * 2 + 1]);
  44. kdebug("(cpu_core_info[i].tss_vaddr)=%#018lx", (cpu_core_info[i].tss_vaddr));
  45. kdebug("(cpu_core_info[i].stack_start)=%#018lx", (cpu_core_info[i].stack_start));
  46. // 连续发送两次start-up IPI
  47. 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);
  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. }
  50. }
  51. /**
  52. * @brief AP处理器启动后执行的第一个函数
  53. *
  54. */
  55. void smp_ap_start()
  56. {
  57. // 切换栈基地址
  58. // uint64_t stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
  59. __asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
  60. : "memory");
  61. __asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
  62. : "memory");
  63. /*
  64. __asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(stack_start)
  65. : "memory");
  66. __asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(stack_start)
  67. : "memory");*/
  68. ksuccess("AP core successfully started!");
  69. kdebug("current=%d", current_starting_cpu);
  70. apic_init_ap_core_local_apic();
  71. load_TR(10 + current_starting_cpu * 2);
  72. spin_unlock(&multi_core_starting_lock);
  73. sti();
  74. kdebug("IDT_addr = %#018lx", &IDT_Table);
  75. // sti();
  76. // int a = 1 / 0; // 在这儿会出现异常,cs fs gs ss寄存器会被改变
  77. kdebug("IDT_addr = %#018lx", &IDT_Table);
  78. while (1) // 这里要循环hlt,原因是当收到中断后,核心会被唤醒,处理完中断之后不会自动hlt
  79. hlt();
  80. }