smp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <common/spinlock.h>
  9. #include <sched/sched.h>
  10. #include "ipi.h"
  11. void ipi_0xc8_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs); // 由BSP转发的HPET中断处理函数
  12. static spinlock_t multi_core_starting_lock = {1}; // 多核启动锁
  13. static struct acpi_Processor_Local_APIC_Structure_t *proc_local_apic_structs[MAX_SUPPORTED_PROCESSOR_NUM];
  14. static uint32_t total_processor_num = 0;
  15. int current_starting_cpu = 0;
  16. int num_cpu_started = 1;
  17. void smp_init()
  18. {
  19. spin_init(&multi_core_starting_lock); // 初始化多核启动锁
  20. ul tmp_vaddr[MAX_SUPPORTED_PROCESSOR_NUM] = {0};
  21. apic_get_ics(ACPI_ICS_TYPE_PROCESSOR_LOCAL_APIC, tmp_vaddr, &total_processor_num);
  22. // kdebug("processor num=%d", total_processor_num);
  23. for (int i = 0; i < total_processor_num; ++i)
  24. {
  25. io_mfence();
  26. proc_local_apic_structs[i] = (struct acpi_Processor_Local_APIC_Structure_t *)(tmp_vaddr[i]);
  27. }
  28. //*(uchar *)0x20000 = 0xf4; // 在内存的0x20000处写入HLT指令(AP处理器会执行物理地址0x20000的代码)
  29. // 将引导程序复制到物理地址0x20000处
  30. memcpy((unsigned char *)phys_2_virt(0x20000), _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start);
  31. io_mfence();
  32. // 设置多核IPI中断门
  33. for (int i = 200; i < 210; ++i)
  34. set_intr_gate(i, 0, SMP_interrupt_table[i - 200]);
  35. memset((void *)SMP_IPI_desc, 0, sizeof(irq_desc_t) * SMP_IRQ_NUM);
  36. io_mfence();
  37. // 注册接收bsp处理器的hpet中断转发的处理函数
  38. ipi_regiserIPI(0xc8, NULL, &ipi_0xc8_handler, NULL, NULL, "IPI 0xc8");
  39. io_mfence();
  40. ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x00, ICR_INIT, ICR_ALL_EXCLUDE_Self, 0x00);
  41. kdebug("total_processor_num=%d", total_processor_num);
  42. kdebug("rflags=%#018lx", get_rflags());
  43. // total_processor_num = 3;
  44. for (int i = 0; i < total_processor_num; ++i) // i从1开始,不初始化bsp
  45. {
  46. io_mfence();
  47. // 跳过BSP
  48. 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]->local_apic_id, proc_local_apic_structs[i]->flags);
  49. if (proc_local_apic_structs[i]->local_apic_id == 0)
  50. {
  51. --total_processor_num;
  52. continue;
  53. }
  54. if (!((proc_local_apic_structs[i]->flags & 0x1) || (proc_local_apic_structs[i]->flags & 0x2)))
  55. {
  56. --total_processor_num;
  57. kdebug("processor %d cannot be enabled.", proc_local_apic_structs[i]->ACPI_Processor_UID);
  58. continue;
  59. }
  60. // continue;
  61. io_mfence();
  62. spin_lock(&multi_core_starting_lock);
  63. preempt_enable(); // 由于ap处理器的pcb与bsp的不同,因此ap处理器放锁时,bsp的自旋锁持有计数不会发生改变,需要手动恢复preempt count
  64. current_starting_cpu = proc_local_apic_structs[i]->local_apic_id;
  65. io_mfence();
  66. // 为每个AP处理器分配栈空间
  67. cpu_core_info[current_starting_cpu].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
  68. cpu_core_info[current_starting_cpu].ist_stack_start = (uint64_t)(kmalloc(STACK_SIZE, 0)) + STACK_SIZE;
  69. io_mfence();
  70. memset((void *)cpu_core_info[current_starting_cpu].stack_start - STACK_SIZE, 0, STACK_SIZE);
  71. memset((void *)cpu_core_info[current_starting_cpu].ist_stack_start - STACK_SIZE, 0, STACK_SIZE);
  72. io_mfence();
  73. // 设置ap处理器的中断栈及内核栈中的cpu_id
  74. ((struct process_control_block *)(cpu_core_info[current_starting_cpu].stack_start - STACK_SIZE))->cpu_id = proc_local_apic_structs[i]->local_apic_id;
  75. ((struct process_control_block *)(cpu_core_info[current_starting_cpu].ist_stack_start - STACK_SIZE))->cpu_id = proc_local_apic_structs[i]->local_apic_id;
  76. cpu_core_info[current_starting_cpu].tss_vaddr = (uint64_t)&initial_tss[current_starting_cpu];
  77. memset(&initial_tss[current_starting_cpu], 0, sizeof(struct tss_struct));
  78. set_tss_descriptor(10 + (current_starting_cpu * 2), (void *)(cpu_core_info[current_starting_cpu].tss_vaddr));
  79. io_mfence();
  80. set_tss64((uint *)cpu_core_info[current_starting_cpu].tss_vaddr, cpu_core_info[current_starting_cpu].stack_start, cpu_core_info[current_starting_cpu].stack_start, cpu_core_info[current_starting_cpu].stack_start,
  81. cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start, cpu_core_info[current_starting_cpu].ist_stack_start);
  82. io_mfence();
  83. kdebug("to send ipi");
  84. // 连续发送两次start-up IPI
  85. ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, proc_local_apic_structs[i]->local_apic_id);
  86. io_mfence();
  87. ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x20, ICR_Start_up, ICR_No_Shorthand, proc_local_apic_structs[i]->local_apic_id);
  88. kdebug("send ipi ok");
  89. }
  90. io_mfence();
  91. while (num_cpu_started != total_processor_num)
  92. pause();
  93. kinfo("Cleaning page table remapping...\n");
  94. // 由于ap处理器初始化过程需要用到0x00处的地址,因此初始化完毕后才取消内存地址的重映射
  95. uint64_t *global_CR3 = get_CR3();
  96. for (int i = 0; i < 256; ++i)
  97. {
  98. io_mfence();
  99. *(ul *)(phys_2_virt(global_CR3) + i) = 0UL;
  100. }
  101. kdebug("init proc's preempt_count=%ld", current_pcb->preempt_count);
  102. kinfo("Successfully cleaned page table remapping!\n");
  103. }
  104. /**
  105. * @brief AP处理器启动后执行的第一个函数
  106. *
  107. */
  108. void smp_ap_start()
  109. {
  110. // 切换栈基地址
  111. // uint64_t stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
  112. __asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
  113. : "memory");
  114. __asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
  115. : "memory");
  116. /*
  117. __asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(stack_start)
  118. : "memory");
  119. __asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(stack_start)
  120. : "memory");*/
  121. ksuccess("AP core successfully started!");
  122. io_mfence();
  123. ++num_cpu_started;
  124. kdebug("current cpu = %d", current_starting_cpu);
  125. apic_init_ap_core_local_apic();
  126. // ============ 为ap处理器初始化IDLE进程 =============
  127. memset(current_pcb, 0, sizeof(struct process_control_block));
  128. barrier();
  129. current_pcb->state = PROC_RUNNING;
  130. current_pcb->flags = PF_KTHREAD;
  131. current_pcb->mm = &initial_mm;
  132. list_init(&current_pcb->list);
  133. current_pcb->addr_limit = KERNEL_BASE_LINEAR_ADDR;
  134. current_pcb->priority = 2;
  135. current_pcb->virtual_runtime = 0;
  136. current_pcb->thread = (struct thread_struct *)(current_pcb + 1); // 将线程结构体放置在pcb后方
  137. current_pcb->thread->rbp = _stack_start;
  138. current_pcb->thread->rsp = _stack_start;
  139. current_pcb->thread->fs = KERNEL_DS;
  140. current_pcb->thread->gs = KERNEL_DS;
  141. current_pcb->cpu_id = current_starting_cpu;
  142. initial_proc[proc_current_cpu_id] = current_pcb;
  143. barrier();
  144. load_TR(10 + current_starting_cpu * 2);
  145. current_pcb->preempt_count = 0;
  146. // kdebug("IDT_addr = %#018lx", phys_2_virt(IDT_Table));
  147. io_mfence();
  148. spin_unlock(&multi_core_starting_lock);
  149. preempt_disable(); // 由于ap处理器的pcb与bsp的不同,因此ap处理器放锁时,需要手动恢复preempt count
  150. io_mfence();
  151. sti();
  152. while (1)
  153. hlt();
  154. /*
  155. if (proc_current_cpu_id == 1)
  156. process_init();
  157. */
  158. while (1)
  159. {
  160. printk_color(BLACK, WHITE, "CPU:%d IDLE process.\n", proc_current_cpu_id);
  161. }
  162. while (1) // 这里要循环hlt,原因是当收到中断后,核心会被唤醒,处理完中断之后不会自动hlt
  163. hlt();
  164. }
  165. // 由BSP转发的HPET中断处理函数
  166. void ipi_0xc8_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs)
  167. {
  168. sched_update_jiffies();
  169. }