fix:在smp模块中增加mfence

This commit is contained in:
fslongjin
2022-08-01 23:18:54 +08:00
parent 83152c176b
commit 8bd6e981f0
8 changed files with 136 additions and 71 deletions

View File

@ -1,4 +1,3 @@
//#pragma GCC optimize("O0")
#include "../common/asm.h"

View File

@ -10,8 +10,7 @@
#include <sched/sched.h>
#include "ipi.h"
#pragma GCC push_options
#pragma GCC optimize("O1")
void ipi_0xc8_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs); // 由BSP转发的HPET中断处理函数
static spinlock_t multi_core_starting_lock; // 多核启动锁
@ -31,21 +30,25 @@ void smp_init()
// kdebug("processor num=%d", total_processor_num);
for (int i = 0; i < total_processor_num; ++i)
{
io_mfence();
proc_local_apic_structs[i] = (struct acpi_Processor_Local_APIC_Structure_t *)(tmp_vaddr[i]);
}
//*(uchar *)0x20000 = 0xf4; // 在内存的0x20000处写入HLT指令(AP处理器会执行物理地址0x20000的代码)
// 将引导程序复制到物理地址0x20000处
memcpy((unsigned char *)phys_2_virt(0x20000), _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start);
io_mfence();
// 设置多核IPI中断门
for (int i = 200; i < 210; ++i)
set_intr_gate(i, 0, SMP_interrupt_table[i - 200]);
memset((void *)SMP_IPI_desc, 0, sizeof(irq_desc_t) * SMP_IRQ_NUM);
io_mfence();
// 注册接收bsp处理器的hpet中断转发的处理函数
ipi_regiserIPI(0xc8, NULL, &ipi_0xc8_handler, NULL, NULL, "IPI 0xc8");
io_mfence();
ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x00, ICR_INIT, ICR_ALL_EXCLUDE_Self, true, 0x00);
kdebug("total_processor_num=%d", total_processor_num);
for (int i = 1; i < total_processor_num; ++i) // i从1开始不初始化bsp
@ -53,22 +56,25 @@ void smp_init()
io_mfence();
if (proc_local_apic_structs[i]->ACPI_Processor_UID == 0)
--total_processor_num;
io_mfence();
if (proc_local_apic_structs[i]->local_apic_id > total_processor_num)
{
--total_processor_num;
continue;
}
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);
io_mfence();
spin_lock(&multi_core_starting_lock);
preempt_enable(); // 由于ap处理器的pcb与bsp的不同因此ap处理器放锁时bsp的自旋锁持有计数不会发生改变,需要手动恢复preempt count
current_starting_cpu = proc_local_apic_structs[i]->local_apic_id;
io_mfence();
// 为每个AP处理器分配栈空间
cpu_core_info[current_starting_cpu].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
cpu_core_info[current_starting_cpu].ist_stack_start = (uint64_t)(kmalloc(STACK_SIZE, 0)) + STACK_SIZE;
io_mfence();
memset((void *)cpu_core_info[current_starting_cpu].stack_start - STACK_SIZE, 0, STACK_SIZE);
memset((void *)cpu_core_info[current_starting_cpu].ist_stack_start - STACK_SIZE, 0, STACK_SIZE);
io_mfence();
// 设置ap处理器的中断栈及内核栈中的cpu_id
((struct process_control_block *)(cpu_core_info[current_starting_cpu].stack_start - STACK_SIZE))->cpu_id = proc_local_apic_structs[i]->local_apic_id;
@ -82,15 +88,15 @@ void smp_init()
io_mfence();
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,
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);
io_mfence();
// 连续发送两次start-up IPI
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]->local_apic_id);
io_mfence();
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]->local_apic_id);
}
io_mfence();
while (num_cpu_started != total_processor_num)
__asm__ __volatile__("pause" ::
: "memory");
pause();
kinfo("Cleaning page table remapping...\n");
@ -98,6 +104,7 @@ void smp_init()
uint64_t *global_CR3 = get_CR3();
for (int i = 0; i < 256; ++i)
{
io_mfence();
*(ul *)(phys_2_virt(global_CR3) + i) = 0UL;
}
kdebug("init proc's preempt_count=%ld", current_pcb->preempt_count);
@ -123,7 +130,7 @@ void smp_ap_start()
__asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(stack_start)
: "memory");*/
ksuccess("AP core successfully started!");
io_mfence();
++num_cpu_started;
kdebug("current cpu = %d", current_starting_cpu);
@ -157,6 +164,7 @@ void smp_ap_start()
// kdebug("IDT_addr = %#018lx", phys_2_virt(IDT_Table));
spin_unlock(&multi_core_starting_lock);
preempt_disable(); // 由于ap处理器的pcb与bsp的不同因此ap处理器放锁时需要手动恢复preempt count
io_mfence();
sti();
while (1)
@ -179,5 +187,3 @@ void ipi_0xc8_handler(uint64_t irq_num, uint64_t param, struct pt_regs *regs)
{
sched_update_jiffies();
}
#pragma GCC optimize("O0")