smp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. static struct acpi_Processor_Local_APIC_Structure_t *proc_local_apic_structs[MAX_SUPPORTED_PROCESSOR_NUM];
  9. static uint32_t total_processor_num = 0;
  10. int current_starting_cpu = 0;
  11. void smp_init()
  12. {
  13. ul tmp_vaddr[MAX_SUPPORTED_PROCESSOR_NUM] = {0};
  14. apic_get_ics(ACPI_ICS_TYPE_PROCESSOR_LOCAL_APIC, tmp_vaddr, &total_processor_num);
  15. kdebug("processor num=%d", total_processor_num);
  16. for (int i = 0; i < total_processor_num; ++i)
  17. proc_local_apic_structs[i] = (struct acpi_Processor_Local_APIC_Structure_t *)(tmp_vaddr[i]);
  18. //*(uchar *)0x20000 = 0xf4; // 在内存的0x20000处写入HLT指令(AP处理器会执行物理地址0x20000的代码)
  19. // 将引导程序复制到物理地址0x20000处
  20. memcpy((unsigned char *)0x20000, _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start);
  21. // wrmsr(0x830, 0xc4500); // init IPI
  22. struct INT_CMD_REG icr_entry;
  23. icr_entry.dest_mode = DEST_PHYSICAL;
  24. icr_entry.deliver_status = IDLE;
  25. icr_entry.res_1 = 0;
  26. icr_entry.level = ICR_LEVEL_DE_ASSERT;
  27. icr_entry.trigger = EDGE_TRIGGER;
  28. icr_entry.res_2 = 0;
  29. icr_entry.res_3 = 0;
  30. for (int i = 1; i < total_processor_num; ++i) // i从1开始,不初始化bsp
  31. {
  32. current_starting_cpu = i;
  33. icr_entry.vector = 0x00;
  34. icr_entry.deliver_mode = ICR_INIT;
  35. icr_entry.dest_shorthand = ICR_ALL_EXCLUDE_Self;
  36. icr_entry.destination.x2apic_destination = current_starting_cpu;
  37. wrmsr(0x830, *(unsigned long *)&icr_entry); // INIT IPI
  38. 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);
  39. // 为每个AP处理器分配栈空间、tss空间
  40. cpu_core_info[i].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
  41. cpu_core_info[i].tss_vaddr = (uint64_t)kmalloc(128, 0);
  42. set_tss_descriptor(10 + (i * 2), (void *)(cpu_core_info[i].tss_vaddr));
  43. set_TSS64(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);
  44. kdebug("GDT Table %#018lx, \t %#018lx", GDT_Table[10 + i * 2], 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. icr_entry.vector = 0x20;
  48. icr_entry.deliver_mode = ICR_Start_up;
  49. icr_entry.dest_shorthand = ICR_No_Shorthand;
  50. icr_entry.destination.x2apic_destination = current_starting_cpu;
  51. // 先init ipi, 然后连续发送两次start-up IPI
  52. // x2APIC下,ICR寄存器地址为0x830
  53. // xAPIC下则为0xfee00300(31-0) 0xfee00310 (63-32)
  54. wrmsr(0x830, *(ul *)&icr_entry); // start-up IPI
  55. wrmsr(0x830, *(ul *)&icr_entry); // start-up IPI
  56. }
  57. hlt();
  58. }
  59. /**
  60. * @brief AP处理器启动后执行的第一个函数
  61. *
  62. */
  63. void smp_ap_start()
  64. {
  65. // 切换栈基地址
  66. // uint64_t stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
  67. __asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
  68. : "memory");
  69. __asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
  70. : "memory");
  71. ksuccess("AP core successfully started!");
  72. kdebug("current=%d", current_starting_cpu);
  73. apic_init_ap_core_local_apic();
  74. // apic_init_ap_core_local_apic();
  75. /*
  76. kinfo("Initializing AP-core's local apic...");
  77. uint eax, edx;
  78. // 启用xAPIC 和x2APIC
  79. __asm__ __volatile__("movq $0x1b, %%rcx \n\t" // 读取IA32_APIC_BASE寄存器
  80. "rdmsr \n\t"
  81. "bts $10, %%rax \n\t"
  82. "bts $11, %%rax \n\t"
  83. "wrmsr \n\t"
  84. "movq $0x1b, %%rcx \n\t"
  85. "rdmsr \n\t"
  86. : "=a"(eax), "=d"(edx)::"memory");
  87. // kdebug("After enable xAPIC and x2APIC: edx=%#010x, eax=%#010x", edx, eax);
  88. // 检测是否成功启用xAPIC和x2APIC
  89. if (eax & 0xc00)
  90. kinfo("xAPIC & x2APIC enabled!");
  91. // 设置SVR寄存器,开启local APIC、禁止EOI广播
  92. // enable SVR[8]
  93. __asm__ __volatile__("movq $0x80f, %%rcx \n\t"
  94. "rdmsr \n\t"
  95. "bts $8, %%rax \n\t"
  96. // "bts $12, %%rax\n\t"
  97. "wrmsr \n\t"
  98. "movq $0x80f, %%rcx \n\t"
  99. "rdmsr \n\t"
  100. : "=a"(eax), "=d"(edx)
  101. :
  102. : "memory");
  103. if (eax & 0x100)
  104. printk_color(RED, YELLOW, "SVR[8] enabled\n");
  105. if (edx & 0x1000)
  106. printk_color(RED, YELLOW, "SVR[12] enabled\n");
  107. // get local APIC ID
  108. __asm__ __volatile__("movq $0x802, %%rcx \n\t"
  109. "rdmsr \n\t"
  110. : "=a"(eax), "=d"(edx)
  111. :
  112. : "memory");
  113. printk_color(RED, YELLOW, "x2APIC ID:%#010x\n", eax);
  114. */
  115. load_TR(10 + current_starting_cpu * 2);
  116. sti();
  117. kdebug("IDT_addr = %#018lx", &IDT_Table);
  118. sti();
  119. //int a = 1 / 0; // 在这儿会出现异常,cs fs gs ss寄存器会被改变
  120. kdebug("IDT_addr = %#018lx", &IDT_Table);
  121. hlt();
  122. hlt();
  123. }