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