mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 08:06:32 +00:00
将内核定位到高地址(存在bug,中断时会访问低地址)
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
|
||||
.align 0x1000 // 按照4k对齐
|
||||
|
||||
.text
|
||||
.section .text
|
||||
.code16
|
||||
|
||||
ENTRY(_apu_boot_start)
|
||||
@ -72,58 +72,16 @@ _apu_code32:
|
||||
mov %cr4, %eax
|
||||
or $(1<<5), %eax
|
||||
mov %eax, %cr4
|
||||
/*
|
||||
|
||||
movl $enter_head_from_ap_boot, %eax
|
||||
jmpl *%eax
|
||||
hlt
|
||||
*/
|
||||
|
||||
|
||||
|
||||
// 设置页表
|
||||
|
||||
movl $pml4, %eax // 复用bsp处理器初始化时的32位页表
|
||||
movl %eax, %cr3
|
||||
|
||||
// enable long mode
|
||||
movl $0xC0000080, %ecx
|
||||
rdmsr
|
||||
|
||||
bts $8, %eax
|
||||
wrmsr
|
||||
|
||||
// enable PE and paging
|
||||
mov %cr0, %eax
|
||||
bts $0, %eax
|
||||
bts $31, %eax
|
||||
mov %eax, %cr0
|
||||
|
||||
// 跳转到64位代码
|
||||
ljmp *(_apu_code64_vector - _apu_boot_base)(%esi)
|
||||
|
||||
|
||||
.code64
|
||||
.align 0x1000
|
||||
_apu_code64:
|
||||
movq $0x20, %rax
|
||||
movq %rax, %ds
|
||||
movq %rax, %es
|
||||
movq %rax, %ss
|
||||
movq %rax, %fs
|
||||
movq %rax, %gs
|
||||
|
||||
|
||||
//now enable SSE and the like
|
||||
movq %cr0, %rax
|
||||
and $0xFFFB, %ax //clear coprocessor emulation CR0.EM
|
||||
or $0x2, %ax //set coprocessor monitoring CR0.MP
|
||||
movq %rax, %cr0
|
||||
movq %cr4, %rax
|
||||
or $(3 << 9), %ax //set CR4.OSFXSR and CR4.OSXMMEXCPT at the same time
|
||||
movq %rax, %cr4
|
||||
|
||||
// 跳转到地址1MB处执行
|
||||
movq $_start64, %rax
|
||||
jmpq *%rax
|
||||
|
||||
hlt
|
||||
|
||||
|
@ -15,10 +15,11 @@ static struct acpi_Processor_Local_APIC_Structure_t *proc_local_apic_structs[MAX
|
||||
static uint32_t total_processor_num = 0;
|
||||
int current_starting_cpu = 0;
|
||||
|
||||
int num_cpu_started = 1;
|
||||
|
||||
void smp_init()
|
||||
{
|
||||
spin_init(&multi_core_starting_lock); // 初始化多核启动锁
|
||||
|
||||
ul tmp_vaddr[MAX_SUPPORTED_PROCESSOR_NUM] = {0};
|
||||
|
||||
apic_get_ics(ACPI_ICS_TYPE_PROCESSOR_LOCAL_APIC, tmp_vaddr, &total_processor_num);
|
||||
@ -29,17 +30,20 @@ void smp_init()
|
||||
|
||||
//*(uchar *)0x20000 = 0xf4; // 在内存的0x20000处写入HLT指令(AP处理器会执行物理地址0x20000的代码)
|
||||
// 将引导程序复制到物理地址0x20000处
|
||||
memcpy((unsigned char *)0x20000, _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start);
|
||||
|
||||
memcpy((unsigned char *)phys_2_virt(0x20000), _apu_boot_start, (unsigned long)&_apu_boot_end - (unsigned long)&_apu_boot_start);
|
||||
|
||||
// 设置多核IPI中断门
|
||||
for (int i = 200; i < 210; ++i)
|
||||
set_intr_gate(i, 2, SMP_interrupt_table[i - 200]);
|
||||
memset(SMP_IPI_desc, 0, sizeof(irq_desc_t) * SMP_IRQ_NUM);
|
||||
|
||||
memset((void *)SMP_IPI_desc, 0, sizeof(irq_desc_t) * SMP_IRQ_NUM);
|
||||
|
||||
ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0x00, ICR_INIT, ICR_ALL_EXCLUDE_Self, true, 0x00);
|
||||
|
||||
for (int i = 1; i < total_processor_num; ++i) // i从1开始,不初始化bsp
|
||||
{
|
||||
if (proc_local_apic_structs[i]->ACPI_Processor_UID == 0)
|
||||
--total_processor_num;
|
||||
spin_lock(&multi_core_starting_lock);
|
||||
current_starting_cpu = i;
|
||||
|
||||
@ -49,15 +53,34 @@ void smp_init()
|
||||
|
||||
cpu_core_info[i].tss_vaddr = (uint64_t)kmalloc(128, 0);
|
||||
|
||||
set_tss_descriptor(10 + (i * 2), (void *)(cpu_core_info[i].tss_vaddr));
|
||||
set_tss_descriptor(10 + (i * 2), (void *)virt_2_phys(cpu_core_info[i].tss_vaddr));
|
||||
|
||||
set_tss64((uint *)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, cpu_core_info[i].stack_start);
|
||||
kdebug("GDT Table %#018lx, \t %#018lx", GDT_Table[10 + i * 2], GDT_Table[10 + i * 2 + 1]);
|
||||
kdebug("phys_2_virt(GDT_Table)=%#018lx",phys_2_virt(GDT_Table));
|
||||
kdebug("GDT Table %#018lx, \t %#018lx", *(ul *)(phys_2_virt(GDT_Table) + 10 + i * 2), *(ul *)(phys_2_virt(GDT_Table) + 10 + i * 2 + 1));
|
||||
// kdebug("(cpu_core_info[i].tss_vaddr)=%#018lx", (cpu_core_info[i].tss_vaddr));
|
||||
// kdebug("(cpu_core_info[i].stack_start)=%#018lx", (cpu_core_info[i].stack_start));
|
||||
kdebug("(cpu_core_info[i].stack_start)=%#018lx", (cpu_core_info[i].stack_start));
|
||||
// 连续发送两次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]->ACPI_ID);
|
||||
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]->ACPI_ID);
|
||||
}
|
||||
|
||||
while (num_cpu_started != total_processor_num)
|
||||
__asm__ __volatile__("pause" ::
|
||||
: "memory");
|
||||
|
||||
kinfo("Cleaning page table remapping...\n");
|
||||
|
||||
// 由于ap处理器初始化过程需要用到0x00处的地址,因此初始化完毕后才取消内存地址的重映射
|
||||
//todo: 取消低0-2M的地址映射
|
||||
for (int i = 1; i < 128; ++i)
|
||||
{
|
||||
|
||||
*(ul *)(phys_2_virt(global_CR3) + i) = 0UL;
|
||||
}
|
||||
|
||||
kinfo("Successfully cleaned page table remapping!\n");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,8 +89,9 @@ void smp_init()
|
||||
*/
|
||||
void smp_ap_start()
|
||||
{
|
||||
// 切换栈基地址
|
||||
// uint64_t stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
|
||||
|
||||
// 切换栈基地址
|
||||
// uint64_t stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
|
||||
__asm__ __volatile__("movq %0, %%rbp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
|
||||
: "memory");
|
||||
__asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(cpu_core_info[current_starting_cpu].stack_start)
|
||||
@ -78,15 +102,18 @@ void smp_ap_start()
|
||||
__asm__ __volatile__("movq %0, %%rsp \n\t" ::"m"(stack_start)
|
||||
: "memory");*/
|
||||
ksuccess("AP core successfully started!");
|
||||
kdebug("current cpu = %d", current_starting_cpu);
|
||||
apic_init_ap_core_local_apic();
|
||||
|
||||
++num_cpu_started;
|
||||
|
||||
kdebug("current cpu = %d", current_starting_cpu);
|
||||
|
||||
apic_init_ap_core_local_apic();
|
||||
load_TR(10 + current_starting_cpu * 2);
|
||||
spin_unlock(&multi_core_starting_lock);
|
||||
|
||||
sti();
|
||||
kdebug("IDT_addr = %#018lx", &IDT_Table);
|
||||
|
||||
|
||||
while (1) // 这里要循环hlt,原因是当收到中断后,核心会被唤醒,处理完中断之后不会自动hlt
|
||||
kdebug("IDT_addr = %#018lx", phys_2_virt(IDT_Table));
|
||||
|
||||
spin_unlock(&multi_core_starting_lock);
|
||||
while (1) // 这里要循环hlt,原因是当收到中断后,核心会被唤醒,处理完中断之后不会自动hlt
|
||||
hlt();
|
||||
}
|
Reference in New Issue
Block a user