存在bug,apu启动后触发异常会Reset

This commit is contained in:
fslongjin
2022-04-06 15:11:14 +08:00
parent a62454587e
commit b7df2805ed
8 changed files with 214 additions and 99 deletions

View File

@ -1,7 +1,7 @@
#include "../common/asm.h"
.balign 0x1000 // 4k
.align 0x1000 // 4k
.text
.code16
@ -53,7 +53,7 @@ _apu_boot_base = .
.code32
.balign 4
.align 0x1000
_apu_code32:
#
@ -63,11 +63,22 @@ _apu_code32:
mov %ax, %ss
mov %ax, %fs
mov %ax, %gs
// 1. PAE
mov %cr4, %eax
or $(1<<5), %eax
mov %eax, %cr4
movl $enter_head_from_ap_boot, %eax
jmpl *%eax
hlt
//
leal (_apu_boot_tmp_stack_end - _apu_boot_base)(%esi), %eax
movl %eax, %esp
// open PAE
movl %cr4, %eax
bts $5, %eax
@ -77,16 +88,17 @@ _apu_code32:
movl $pml4, %eax // bsp32
movl %eax, %cr3
movl $0xC0000080, %ecx
rdmsr
mov $0xC0000080, %ecx
rdmsr
or $(1<<8), %eax
wrmsr
// enable PE and paging
bts $8, %eax
wrmsr
// enable PE and paging
mov %cr0, %eax
or $(1<<31), %eax
or $(1<<0), %eax
mov %eax, %cr0
// 64
@ -94,7 +106,7 @@ _apu_code32:
.code64
.balign 4
.align 0x1000
_apu_code64:
movq $0x20, %rax
movq %rax, %ds
@ -119,12 +131,12 @@ _apu_code64:
hlt
.balign 4
.align 0x1000
_apu_tmp_idt:
.word 0
.word 0,0
.balign 4
.align 0x1000
_apu_tmp_gdt:
.short _apu_tmp_gdt_end - _apu_tmp_gdt -1
.long _apu_tmp_gdt - _apu_boot_base
@ -135,19 +147,19 @@ _apu_tmp_gdt:
.quad 0x0000920000000000
_apu_tmp_gdt_end:
.balign 4
.align 0x1000
_apu_code32_vector:
.long _apu_code32 - _apu_boot_base
.word 0x08,0
.balign 4
.align 0x1000
_apu_code64_vector:
.long _apu_code64 - _apu_boot_base
.word 0x18,0
.balign 4
.align 0x1000
_apu_boot_tmp_stack_start:
.org 0x400
// .org 0x400
_apu_boot_tmp_stack_end:
ENTRY(_apu_boot_end)

View File

@ -23,8 +23,8 @@ 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);
wrmsr(0x830, 0xc4500); // init IPI
// wrmsr(0x830, 0xc4500); // init IPI
struct INT_CMD_REG icr_entry;
icr_entry.dest_mode = DEST_PHYSICAL;
icr_entry.deliver_status = IDLE;
@ -37,6 +37,14 @@ void smp_init()
for (int i = 1; i < total_processor_num; ++i) // i从1开始不初始化bsp
{
current_starting_cpu = i;
icr_entry.vector = 0x00;
icr_entry.deliver_mode = ICR_INIT;
icr_entry.dest_shorthand = ICR_ALL_EXCLUDE_Self;
icr_entry.destination.x2apic_destination = current_starting_cpu;
wrmsr(0x830, *(unsigned long *)&icr_entry); // INIT IPI
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]->ACPI_ID, proc_local_apic_structs[i]->flags);
// 为每个AP处理器分配栈空间、tss空间
cpu_core_info[i].stack_start = (uint64_t)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
@ -45,7 +53,8 @@ void smp_init()
set_tss_descriptor(10 + (i * 2), (void *)(cpu_core_info[i].tss_vaddr));
set_TSS64(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);
kdebug("GDT Table %#018lx, \t %#018lx", GDT_Table[10 + i * 2], 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));
icr_entry.vector = 0x20;
icr_entry.deliver_mode = ICR_Start_up;
icr_entry.dest_shorthand = ICR_No_Shorthand;
@ -57,7 +66,10 @@ void smp_init()
wrmsr(0x830, *(ul *)&icr_entry); // start-up IPI
wrmsr(0x830, *(ul *)&icr_entry); // start-up IPI
}
hlt();
}
/**
@ -66,11 +78,69 @@ void smp_init()
*/
void smp_ap_start()
{
// 切换栈基地址
// 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)
: "memory");
ksuccess("AP core successfully started!");
kdebug("current=%d", current_starting_cpu);
load_TR(10 + current_starting_cpu * 2);
apic_init_ap_core_local_apic();
int a =1/0; // 在这儿会出现异常cs fs gs ss寄存器会被改变
// apic_init_ap_core_local_apic();
/*
kinfo("Initializing AP-core's local apic...");
uint eax, edx;
// 启用xAPIC 和x2APIC
__asm__ __volatile__("movq $0x1b, %%rcx \n\t" // 读取IA32_APIC_BASE寄存器
"rdmsr \n\t"
"bts $10, %%rax \n\t"
"bts $11, %%rax \n\t"
"wrmsr \n\t"
"movq $0x1b, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)::"memory");
// kdebug("After enable xAPIC and x2APIC: edx=%#010x, eax=%#010x", edx, eax);
// 检测是否成功启用xAPIC和x2APIC
if (eax & 0xc00)
kinfo("xAPIC & x2APIC enabled!");
// 设置SVR寄存器开启local APIC、禁止EOI广播
// enable SVR[8]
__asm__ __volatile__("movq $0x80f, %%rcx \n\t"
"rdmsr \n\t"
"bts $8, %%rax \n\t"
// "bts $12, %%rax\n\t"
"wrmsr \n\t"
"movq $0x80f, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)
:
: "memory");
if (eax & 0x100)
printk_color(RED, YELLOW, "SVR[8] enabled\n");
if (edx & 0x1000)
printk_color(RED, YELLOW, "SVR[12] enabled\n");
// get local APIC ID
__asm__ __volatile__("movq $0x802, %%rcx \n\t"
"rdmsr \n\t"
: "=a"(eax), "=d"(edx)
:
: "memory");
printk_color(RED, YELLOW, "x2APIC ID:%#010x\n", eax);
*/
load_TR(10 + current_starting_cpu * 2);
sti();
kdebug("IDT_addr = %#018lx", &IDT_Table);
sti();
//int a = 1 / 0; // 在这儿会出现异常cs fs gs ss寄存器会被改变
kdebug("IDT_addr = %#018lx", &IDT_Table);
hlt();
hlt();
}