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